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.browser.NavigationEntry;
17 import org.chromium.content.browser.NavigationHistory;
18 import org.chromium.net.test.util.TestWebServer;
20 import org.xwalk.core.XWalkClient;
21 import org.xwalk.core.XWalkNavigationHistory;
22 import org.xwalk.core.XWalkView;
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.setXWalkClient(new XWalkViewTestBase.TestXWalkClient());
61 mUrls = new String[NUM_NAVIGATIONS];
62 mWebServer = new TestWebServer(false);
66 public void tearDown() throws Exception {
67 if (mWebServer != null) {
68 mWebServer.shutdown();
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>",
78 mUrls[i] = mWebServer.setResponse(PATHS[i], html, null);
79 loadUrlSync(mUrls[i]);
83 private XWalkNavigationHistory getNavigationHistoryOnUiThread(
84 final XWalkView content) throws Throwable{
85 return runTestOnUiThreadAndGetResult(new Callable<XWalkNavigationHistory>() {
87 public XWalkNavigationHistory call() throws Exception {
88 return content.getNavigationHistory();
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());
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());
108 private void saveAndRestoreStateOnUiThread() throws Throwable {
109 getInstrumentation().runOnMainSync(new Runnable() {
112 Bundle bundle = new Bundle();
113 mXWalkView.saveState(bundle);
114 mRestoreXWalkView.restoreState(bundle);
120 @Feature({"SaveRestoreState"})
121 public void testSaveRestoreStateWithTitle() throws Throwable {
122 setServerResponseAndLoad(1);
123 saveAndRestoreStateOnUiThread();
124 assertTrue(pollOnUiThread(new Callable<Boolean>() {
126 public Boolean call() throws Exception {
127 // TODO(hengzhi): add the judge about updated title.
128 return TITLES[0].equals(mXWalkView.getTitle());
134 //@Feature({"SaveRestoreState"})
136 public void testSaveRestoreStateWithHistoryItemList() throws Throwable {
137 setServerResponseAndLoad(NUM_NAVIGATIONS);
138 saveAndRestoreStateOnUiThread();
139 checkHistoryItemList(mRestoreXWalkView);
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>() {
152 public Boolean call() throws Exception {
153 return mXWalkView.restoreState(invalidState);
160 @Feature({"SaveRestoreState"})
161 public void testSaveStateForNoNavigationFails() throws Throwable {
162 final Bundle state = new Bundle();
163 boolean result = runTestOnUiThreadAndGetResult(new Callable<Boolean>() {
165 public Boolean call() throws Exception {
166 return mXWalkView.restoreState(state);