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.
6 package org.xwalk.core.xwview.test;
8 import android.app.Activity;
9 import android.os.Bundle;
10 import android.test.suitebuilder.annotation.SmallTest;
11 import android.util.Log;
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;
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;
25 import java.util.concurrent.Callable;
27 public class SaveRestoreStateTest extends XWalkViewTestBase {
29 private TestWebServer mWebServer;
31 private static final int NUM_NAVIGATIONS = 3;
32 private static final String TITLES[] = {
37 private static final String PATHS[] = {
43 private String mUrls[];
44 private XWalkView mXWalkView;
45 private XWalkView mRestoreXWalkView;
48 public void setUp() throws Exception {
51 final Activity activity = getActivity();
52 getInstrumentation().runOnMainSync(new Runnable() {
55 mXWalkView = getXWalkView();
56 mRestoreXWalkView = new XWalkView(activity, activity);
57 mXWalkView.setUIClient(new XWalkViewTestBase.TestXWalkUIClient());
58 mXWalkView.setResourceClient(new XWalkViewTestBase.TestXWalkResourceClient());
62 mUrls = new String[NUM_NAVIGATIONS];
63 mWebServer = new TestWebServer(false);
67 public void tearDown() throws Exception {
68 if (mWebServer != null) {
69 mWebServer.shutdown();
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>",
79 mUrls[i] = mWebServer.setResponse(PATHS[i], html, null);
80 loadUrlSync(mUrls[i]);
84 private XWalkNavigationHistory getNavigationHistoryOnUiThread(
85 final XWalkView content) throws Throwable{
86 return runTestOnUiThreadAndGetResult(new Callable<XWalkNavigationHistory>() {
88 public XWalkNavigationHistory call() throws Exception {
89 return content.getNavigationHistory();
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());
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());
109 private void saveAndRestoreStateOnUiThread() throws Throwable {
110 getInstrumentation().runOnMainSync(new Runnable() {
113 Bundle bundle = new Bundle();
114 mXWalkView.saveState(bundle);
115 mRestoreXWalkView.restoreState(bundle);
121 @Feature({"SaveRestoreState"})
122 public void testSaveRestoreStateWithTitle() throws Throwable {
123 setServerResponseAndLoad(1);
124 saveAndRestoreStateOnUiThread();
125 assertTrue(pollOnUiThread(new Callable<Boolean>() {
127 public Boolean call() throws Exception {
128 // TODO(hengzhi): add the judge about updated title.
129 return TITLES[0].equals(mXWalkView.getTitle());
135 //@Feature({"SaveRestoreState"})
137 public void testSaveRestoreStateWithHistoryItemList() throws Throwable {
138 setServerResponseAndLoad(NUM_NAVIGATIONS);
139 saveAndRestoreStateOnUiThread();
140 checkHistoryItemList(mRestoreXWalkView);
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>() {
153 public Boolean call() throws Exception {
154 return mXWalkView.restoreState(invalidState);
161 @Feature({"SaveRestoreState"})
162 public void testSaveStateForNoNavigationFails() throws Throwable {
163 final Bundle state = new Bundle();
164 boolean result = runTestOnUiThreadAndGetResult(new Callable<Boolean>() {
166 public Boolean call() throws Exception {
167 return mXWalkView.restoreState(state);