d7eb871216b311785f407bd47782f9d6bb0f95da
[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_public.browser.NavigationEntry;
17 import org.chromium.content_public.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.setUIClient(new XWalkViewTestBase.TestXWalkUIClient());
58                 mXWalkView.setResourceClient(new XWalkViewTestBase.TestXWalkResourceClient());
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 XWalkNavigationHistory getNavigationHistoryOnUiThread(
85             final XWalkView content) throws Throwable{
86         return runTestOnUiThreadAndGetResult(new Callable<XWalkNavigationHistory>() {
87             @Override
88             public XWalkNavigationHistory call() throws Exception {
89                 return content.getNavigationHistory();
90             }
91         });
92     }
93
94     private void checkHistoryItemList(XWalkView content) throws Throwable {
95         XWalkNavigationHistory history = getNavigationHistoryOnUiThread(content);
96         assertEquals(NUM_NAVIGATIONS, history.size());
97         assertEquals(NUM_NAVIGATIONS - 1, history.getCurrentIndex());
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.getItemAt(i).getOriginalUrl());
104             assertEquals(mUrls[i], history.getItemAt(i).getUrl());
105             assertEquals(TITLES[i], history.getItemAt(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(mXWalkView.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);
141     }
142
143     @SmallTest
144     @Feature({"SaveRestoreState"})
145     public void testRestoreFromInvalidStateFails() throws Throwable {
146         final Bundle invalidState = new Bundle();
147         // TODO(yongsheng): how to share this with
148         // XWalkContent.SAVE_RESTORE_STATE_KEY?
149         invalidState.putByteArray("XWALKVIEW_STATE",
150                                   "invalid state".getBytes());
151         boolean result = runTestOnUiThreadAndGetResult(new Callable<Boolean>() {
152             @Override
153             public Boolean call() throws Exception {
154                 return mXWalkView.restoreState(invalidState);
155             }
156         });
157         assertFalse(result);
158     }
159
160     @SmallTest
161     @Feature({"SaveRestoreState"})
162     public void testSaveStateForNoNavigationFails() throws Throwable {
163         final Bundle state = new Bundle();
164         boolean result = runTestOnUiThreadAndGetResult(new Callable<Boolean>() {
165             @Override
166             public Boolean call() throws Exception {
167                 return mXWalkView.restoreState(state);
168             }
169         });
170         assertFalse(result);
171     }
172 }