1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.android_webview.test;
7 import android.os.Bundle;
8 import android.test.suitebuilder.annotation.SmallTest;
10 import org.chromium.android_webview.AwContents;
11 import org.chromium.android_webview.test.util.CommonResources;
12 import org.chromium.base.test.util.Feature;
13 import org.chromium.content.browser.ContentViewCore;
14 import org.chromium.content.browser.NavigationEntry;
15 import org.chromium.content.browser.NavigationHistory;
16 import org.chromium.net.test.util.TestWebServer;
18 import java.util.concurrent.Callable;
20 public class SaveRestoreStateTest extends AwTestBase {
21 private static class TestVars {
22 public final TestAwContentsClient contentsClient;
23 public final AwTestContainerView testView;
24 public final AwContents awContents;
25 public final ContentViewCore contentViewCore;
27 public TestVars(TestAwContentsClient contentsClient,
28 AwTestContainerView testView) {
29 this.contentsClient = contentsClient;
30 this.testView = testView;
31 this.awContents = testView.getAwContents();
32 this.contentViewCore = this.awContents.getContentViewCore();
36 private TestVars createNewView() throws Exception {
37 TestAwContentsClient contentsClient = new TestAwContentsClient();;
38 AwTestContainerView testView = createAwTestContainerViewOnMainSync(contentsClient);
39 return new TestVars(contentsClient, testView);
42 private TestVars mVars;
43 private TestWebServer mWebServer;
45 private static final int NUM_NAVIGATIONS = 3;
46 private static final String TITLES[] = {
51 private static final String PATHS[] = {
57 private String mUrls[];
60 public void setUp() throws Exception {
62 mVars = createNewView();
63 mUrls = new String[NUM_NAVIGATIONS];
64 mWebServer = new TestWebServer(false);
68 public void tearDown() throws Exception {
69 if (mWebServer != null) {
70 mWebServer.shutdown();
75 private void setServerResponseAndLoad(TestVars vars, int upto) throws Throwable {
76 for (int i = 0; i < upto; ++i) {
77 String html = CommonResources.makeHtmlPageFrom(
78 "<title>" + TITLES[i] + "</title>",
80 mUrls[i] = mWebServer.setResponse(PATHS[i], html, null);
82 loadUrlSync(vars.awContents,
83 vars.contentsClient.getOnPageFinishedHelper(),
88 private NavigationHistory getNavigationHistoryOnUiThread(
89 final TestVars vars) throws Throwable{
90 return runTestOnUiThreadAndGetResult(new Callable<NavigationHistory>() {
92 public NavigationHistory call() throws Exception {
93 return vars.contentViewCore.getNavigationHistory();
98 private void checkHistoryItemList(TestVars vars) throws Throwable {
99 NavigationHistory history = getNavigationHistoryOnUiThread(vars);
100 assertEquals(NUM_NAVIGATIONS, history.getEntryCount());
101 assertEquals(NUM_NAVIGATIONS - 1, history.getCurrentEntryIndex());
103 // Note this is not meant to be a thorough test of NavigationHistory,
104 // but is only meant to test enough to make sure state is restored.
105 // See NavigationHistoryTest for more thorough tests.
106 for (int i = 0; i < NUM_NAVIGATIONS; ++i) {
107 assertEquals(mUrls[i], history.getEntryAtIndex(i).getOriginalUrl());
108 assertEquals(mUrls[i], history.getEntryAtIndex(i).getUrl());
109 assertEquals(TITLES[i], history.getEntryAtIndex(i).getTitle());
113 private TestVars saveAndRestoreStateOnUiThread(final TestVars vars) throws Throwable {
114 final TestVars restoredVars = createNewView();
115 getInstrumentation().runOnMainSync(new Runnable() {
118 Bundle bundle = new Bundle();
119 boolean result = vars.awContents.saveState(bundle);
121 result = restoredVars.awContents.restoreState(bundle);
129 @Feature({"AndroidWebView"})
130 public void testSaveRestoreStateWithTitle() throws Throwable {
131 setServerResponseAndLoad(mVars, 1);
132 final TestVars restoredVars = saveAndRestoreStateOnUiThread(mVars);
133 assertTrue(pollOnUiThread(new Callable<Boolean>() {
135 public Boolean call() throws Exception {
136 return TITLES[0].equals(restoredVars.contentViewCore.getTitle()) &&
137 TITLES[0].equals(restoredVars.contentsClient.getUpdatedTitle());
143 @Feature({"AndroidWebView"})
144 public void testSaveRestoreStateWithHistoryItemList() throws Throwable {
145 setServerResponseAndLoad(mVars, NUM_NAVIGATIONS);
146 TestVars restoredVars = saveAndRestoreStateOnUiThread(mVars);
147 checkHistoryItemList(restoredVars);
151 @Feature({"AndroidWebView"})
152 public void testRestoreFromInvalidStateFails() throws Throwable {
153 final Bundle invalidState = new Bundle();
154 invalidState.putByteArray(AwContents.SAVE_RESTORE_STATE_KEY,
155 "invalid state".getBytes());
156 boolean result = runTestOnUiThreadAndGetResult(new Callable<Boolean>() {
158 public Boolean call() throws Exception {
159 return mVars.awContents.restoreState(invalidState);
166 @Feature({"AndroidWebView"})
167 public void testSaveStateForNoNavigationFails() throws Throwable {
168 final Bundle state = new Bundle();
169 boolean result = runTestOnUiThreadAndGetResult(new Callable<Boolean>() {
171 public Boolean call() throws Exception {
172 return mVars.awContents.restoreState(state);