Upstream version 8.36.161.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
20 import org.xwalk.core.XWalkNavigationHistory;
21 import org.xwalk.core.XWalkView;
22 import org.xwalk.core.internal.XWalkClient;
23 import org.xwalk.core.xwview.test.util.CommonResources;
24
25 import java.util.concurrent.Callable;
26
27 public class SaveRestoreStateTest extends XWalkViewTestBase {
28
29     private TestWebServer mWebServer;
30
31     private static final int NUM_NAVIGATIONS = 3;
32     private static final String TITLES[] = {
33             "page 1 title foo",
34             "page 2 title bar",
35             "page 3 title baz"
36     };
37     private static final String PATHS[] = {
38             "/p1foo.html",
39             "/p2bar.html",
40             "/p3baz.html",
41     };
42
43     private String mUrls[];
44     private XWalkView mXWalkView;
45     private XWalkView mRestoreXWalkView;
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             }
59         });
60
61         mUrls = new String[NUM_NAVIGATIONS];
62         mWebServer = new TestWebServer(false);
63     }
64
65     @Override
66     public void tearDown() throws Exception {
67         if (mWebServer != null) {
68             mWebServer.shutdown();
69         }
70         super.tearDown();
71     }
72
73     private void setServerResponseAndLoad(int upto) throws Throwable {
74         for (int i = 0; i < upto; ++i) {
75             String html = CommonResources.makeHtmlPageFrom(
76                     "<title>" + TITLES[i] + "</title>",
77                     "");
78             mUrls[i] = mWebServer.setResponse(PATHS[i], html, null);
79             loadUrlSync(mUrls[i]);
80         }
81     }
82
83     private XWalkNavigationHistory getNavigationHistoryOnUiThread(
84             final XWalkView content) throws Throwable{
85         return runTestOnUiThreadAndGetResult(new Callable<XWalkNavigationHistory>() {
86             @Override
87             public XWalkNavigationHistory call() throws Exception {
88                 return content.getNavigationHistory();
89             }
90         });
91     }
92
93     private void checkHistoryItemList(XWalkView content) throws Throwable {
94         XWalkNavigationHistory history = getNavigationHistoryOnUiThread(content);
95         assertEquals(NUM_NAVIGATIONS, history.size());
96         assertEquals(NUM_NAVIGATIONS - 1, history.getCurrentIndex());
97
98         // Note this is not meant to be a thorough test of NavigationHistory,
99         // but is only meant to test enough to make sure state is restored.
100         // See NavigationHistoryTest for more thorough tests.
101         for (int i = 0; i < NUM_NAVIGATIONS; ++i) {
102             assertEquals(mUrls[i], history.getItemAt(i).getOriginalUrl());
103             assertEquals(mUrls[i], history.getItemAt(i).getUrl());
104             assertEquals(TITLES[i], history.getItemAt(i).getTitle());
105         }
106     }
107
108     private void saveAndRestoreStateOnUiThread() throws Throwable {
109         getInstrumentation().runOnMainSync(new Runnable() {
110             @Override
111             public void run() {
112                 Bundle bundle = new Bundle();
113                 mXWalkView.saveState(bundle);
114                 mRestoreXWalkView.restoreState(bundle);
115             }
116         });
117     }
118
119     @SmallTest
120     @Feature({"SaveRestoreState"})
121     public void testSaveRestoreStateWithTitle() throws Throwable {
122         setServerResponseAndLoad(1);
123         saveAndRestoreStateOnUiThread();
124         assertTrue(pollOnUiThread(new Callable<Boolean>() {
125             @Override
126             public Boolean call() throws Exception {
127                 // TODO(hengzhi): add the judge about updated title.
128                 return TITLES[0].equals(mXWalkView.getTitle());
129             }
130         }));
131     }
132
133     //@SmallTest
134     //@Feature({"SaveRestoreState"})
135     @DisabledTest
136     public void testSaveRestoreStateWithHistoryItemList() throws Throwable {
137         setServerResponseAndLoad(NUM_NAVIGATIONS);
138         saveAndRestoreStateOnUiThread();
139         checkHistoryItemList(mRestoreXWalkView);
140     }
141
142     @SmallTest
143     @Feature({"SaveRestoreState"})
144     public void testRestoreFromInvalidStateFails() throws Throwable {
145         final Bundle invalidState = new Bundle();
146         // TODO(yongsheng): how to share this with
147         // XWalkContent.SAVE_RESTORE_STATE_KEY?
148         invalidState.putByteArray("XWALKVIEW_STATE",
149                                   "invalid state".getBytes());
150         boolean result = runTestOnUiThreadAndGetResult(new Callable<Boolean>() {
151             @Override
152             public Boolean call() throws Exception {
153                 return mXWalkView.restoreState(invalidState);
154             }
155         });
156         assertFalse(result);
157     }
158
159     @SmallTest
160     @Feature({"SaveRestoreState"})
161     public void testSaveStateForNoNavigationFails() throws Throwable {
162         final Bundle state = new Bundle();
163         boolean result = runTestOnUiThreadAndGetResult(new Callable<Boolean>() {
164             @Override
165             public Boolean call() throws Exception {
166                 return mXWalkView.restoreState(state);
167             }
168         });
169         assertFalse(result);
170     }
171 }