Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / xwalk / test / android / core / javatests / src / org / xwalk / core / xwview / test / SaveRestoreStateTest.java
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Copyright (c) 2013 Intel Corporation. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 package org.xwalk.core.xwview.test;
7
8 import android.app.Activity;
9 import android.os.Bundle;
10 import android.test.suitebuilder.annotation.SmallTest;
11 import android.util.Log;
12
13 import org.chromium.base.test.util.DisabledTest;
14 import org.chromium.base.test.util.Feature;
15 import org.chromium.content.browser.ContentViewCore;
16 import org.chromium.content.browser.NavigationEntry;
17 import org.chromium.content.browser.NavigationHistory;
18 import org.chromium.net.test.util.TestWebServer;
19 import org.xwalk.core.xwview.test.util.CommonResources;
20 import org.xwalk.core.XWalkClient;
21 import org.xwalk.core.XWalkContent;
22 import org.xwalk.core.XWalkView;
23
24 import java.util.concurrent.Callable;
25
26 public class SaveRestoreStateTest extends XWalkViewTestBase {
27
28     private TestWebServer mWebServer;
29
30     private static final int NUM_NAVIGATIONS = 3;
31     private static final String TITLES[] = {
32             "page 1 title foo",
33             "page 2 title bar",
34             "page 3 title baz"
35     };
36     private static final String PATHS[] = {
37             "/p1foo.html",
38             "/p2bar.html",
39             "/p3baz.html",
40     };
41
42     private String mUrls[];
43     private XWalkView mXWalkView;
44     private XWalkView mRestoreXWalkView;
45     private XWalkContent mXWalkContent;
46
47     @Override
48     public void setUp() throws Exception {
49         super.setUp();
50
51         final Activity activity = getActivity();
52         getInstrumentation().runOnMainSync(new Runnable() {
53             @Override
54             public void run() {
55                 mXWalkView = getXWalkView();
56                 mRestoreXWalkView = new XWalkView(activity, activity);
57                 mXWalkView.setXWalkClient(new XWalkViewTestBase.TestXWalkClient());
58                 mXWalkContent = mXWalkView.getXWalkViewContentForTest();
59             }
60         });
61
62         mUrls = new String[NUM_NAVIGATIONS];
63         mWebServer = new TestWebServer(false);
64     }
65
66     @Override
67     public void tearDown() throws Exception {
68         if (mWebServer != null) {
69             mWebServer.shutdown();
70         }
71         super.tearDown();
72     }
73
74     private void setServerResponseAndLoad(int upto) throws Throwable {
75         for (int i = 0; i < upto; ++i) {
76             String html = CommonResources.makeHtmlPageFrom(
77                     "<title>" + TITLES[i] + "</title>",
78                     "");
79             mUrls[i] = mWebServer.setResponse(PATHS[i], html, null);
80             loadUrlSync(mUrls[i]);
81         }
82     }
83
84     private NavigationHistory getNavigationHistoryOnUiThread(
85             final XWalkContent content) throws Throwable{
86         return runTestOnUiThreadAndGetResult(new Callable<NavigationHistory>() {
87             @Override
88             public NavigationHistory call() throws Exception {
89                 return content.getContentViewCoreForTest().getNavigationHistory();
90             }
91         });
92     }
93
94     private void checkHistoryItemList(XWalkContent content) throws Throwable {
95         NavigationHistory history = getNavigationHistoryOnUiThread(content);
96         assertEquals(NUM_NAVIGATIONS, history.getEntryCount());
97         assertEquals(NUM_NAVIGATIONS - 1, history.getCurrentEntryIndex());
98
99         // Note this is not meant to be a thorough test of NavigationHistory,
100         // but is only meant to test enough to make sure state is restored.
101         // See NavigationHistoryTest for more thorough tests.
102         for (int i = 0; i < NUM_NAVIGATIONS; ++i) {
103             assertEquals(mUrls[i], history.getEntryAtIndex(i).getOriginalUrl());
104             assertEquals(mUrls[i], history.getEntryAtIndex(i).getUrl());
105             assertEquals(TITLES[i], history.getEntryAtIndex(i).getTitle());
106         }
107     }
108
109     private void saveAndRestoreStateOnUiThread() throws Throwable {
110         getInstrumentation().runOnMainSync(new Runnable() {
111             @Override
112             public void run() {
113                 Bundle bundle = new Bundle();
114                 mXWalkView.saveState(bundle);
115                 mRestoreXWalkView.restoreState(bundle);
116             }
117         });
118     }
119
120     @SmallTest
121     @Feature({"SaveRestoreState"})
122     public void testSaveRestoreStateWithTitle() throws Throwable {
123         setServerResponseAndLoad(1);
124         saveAndRestoreStateOnUiThread();
125         assertTrue(pollOnUiThread(new Callable<Boolean>() {
126             @Override
127             public Boolean call() throws Exception {
128                 // TODO(hengzhi): add the judge about updated title.
129                 return TITLES[0].equals(mXWalkContent.getContentViewCoreForTest().getTitle());
130             }
131         }));
132     }
133
134     //@SmallTest
135     //@Feature({"SaveRestoreState"})
136     @DisabledTest
137     public void testSaveRestoreStateWithHistoryItemList() throws Throwable {
138         setServerResponseAndLoad(NUM_NAVIGATIONS);
139         saveAndRestoreStateOnUiThread();
140         checkHistoryItemList(mRestoreXWalkView.getXWalkViewContentForTest());
141     }
142
143     @SmallTest
144     @Feature({"SaveRestoreState"})
145     public void testRestoreFromInvalidStateFails() throws Throwable {
146         final Bundle invalidState = new Bundle();
147         invalidState.putByteArray(mXWalkContent.SAVE_RESTORE_STATE_KEY,
148                                   "invalid state".getBytes());
149         boolean result = runTestOnUiThreadAndGetResult(new Callable<Boolean>() {
150             @Override
151             public Boolean call() throws Exception {
152                 return mXWalkView.restoreState(invalidState);
153             }
154         });
155         assertFalse(result);
156     }
157
158     @SmallTest
159     @Feature({"SaveRestoreState"})
160     public void testSaveStateForNoNavigationFails() throws Throwable {
161         final Bundle state = new Bundle();
162         boolean result = runTestOnUiThreadAndGetResult(new Callable<Boolean>() {
163             @Override
164             public Boolean call() throws Exception {
165                 return mXWalkView.restoreState(state);
166             }
167         });
168         assertFalse(result);
169     }
170 }