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;
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;
24 import java.util.concurrent.Callable;
26 public class SaveRestoreStateTest extends XWalkViewTestBase {
28 private TestWebServer mWebServer;
30 private static final int NUM_NAVIGATIONS = 3;
31 private static final String TITLES[] = {
36 private static final String PATHS[] = {
42 private String mUrls[];
43 private XWalkView mXWalkView;
44 private XWalkView mRestoreXWalkView;
45 private XWalkContent mXWalkContent;
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());
58 mXWalkContent = mXWalkView.getXWalkViewContentForTest();
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 NavigationHistory getNavigationHistoryOnUiThread(
85 final XWalkContent content) throws Throwable{
86 return runTestOnUiThreadAndGetResult(new Callable<NavigationHistory>() {
88 public NavigationHistory call() throws Exception {
89 return content.getContentViewCoreForTest().getNavigationHistory();
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());
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());
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(mXWalkContent.getContentViewCoreForTest().getTitle());
135 //@Feature({"SaveRestoreState"})
137 public void testSaveRestoreStateWithHistoryItemList() throws Throwable {
138 setServerResponseAndLoad(NUM_NAVIGATIONS);
139 saveAndRestoreStateOnUiThread();
140 checkHistoryItemList(mRestoreXWalkView.getXWalkViewContentForTest());
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>() {
151 public Boolean call() throws Exception {
152 return mXWalkView.restoreState(invalidState);
159 @Feature({"SaveRestoreState"})
160 public void testSaveStateForNoNavigationFails() throws Throwable {
161 final Bundle state = new Bundle();
162 boolean result = runTestOnUiThreadAndGetResult(new Callable<Boolean>() {
164 public Boolean call() throws Exception {
165 return mXWalkView.restoreState(state);