| 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. |
| 4 | |
| 5 | package org.chromium.android_webview.test; |
| 6 | |
| 7 | import android.os.Bundle; |
| 8 | import android.test.suitebuilder.annotation.SmallTest; |
| 9 | |
| 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; |
| 17 | |
| 18 | import java.util.concurrent.Callable; |
| 19 | |
| 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; |
| 26 | |
| 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(); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | private TestVars createNewView() throws Exception { |
| 37 | TestAwContentsClient contentsClient = new TestAwContentsClient();; |
| 38 | AwTestContainerView testView = createAwTestContainerViewOnMainSync(contentsClient); |
| 39 | return new TestVars(contentsClient, testView); |
| 40 | } |
| 41 | |
| 42 | private TestVars mVars; |
| 43 | private TestWebServer mWebServer; |
| 44 | |
| 45 | private static final int NUM_NAVIGATIONS = 3; |
| 46 | private static final String TITLES[] = { |
| 47 | "page 1 title foo", |
| 48 | "page 2 title bar", |
| 49 | "page 3 title baz" |
| 50 | }; |
| 51 | private static final String PATHS[] = { |
| 52 | "/p1foo.html", |
| 53 | "/p2bar.html", |
| 54 | "/p3baz.html", |
| 55 | }; |
| 56 | |
| 57 | private String mUrls[]; |
| 58 | |
| 59 | @Override |
| 60 | public void setUp() throws Exception { |
| 61 | super.setUp(); |
| 62 | mVars = createNewView(); |
| 63 | mUrls = new String[NUM_NAVIGATIONS]; |
| 64 | mWebServer = new TestWebServer(false); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void tearDown() throws Exception { |
| 69 | if (mWebServer != null) { |
| 70 | mWebServer.shutdown(); |
| 71 | } |
| 72 | super.tearDown(); |
| 73 | } |
| 74 | |
| 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>", |
| 79 | ""); |
| 80 | mUrls[i] = mWebServer.setResponse(PATHS[i], html, null); |
| 81 | |
| 82 | loadUrlSync(vars.awContents, |
| 83 | vars.contentsClient.getOnPageFinishedHelper(), |
| 84 | mUrls[i]); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | private NavigationHistory getNavigationHistoryOnUiThread( |
| 89 | final TestVars vars) throws Throwable{ |
| 90 | return runTestOnUiThreadAndGetResult(new Callable<NavigationHistory>() { |
| 91 | @Override |
| 92 | public NavigationHistory call() throws Exception { |
| 93 | return vars.contentViewCore.getNavigationHistory(); |
| 94 | } |
| 95 | }); |
| 96 | } |
| 97 | |
| 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()); |
| 102 | |
| 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()); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | private TestVars saveAndRestoreStateOnUiThread(final TestVars vars) throws Throwable { |
| 114 | final TestVars restoredVars = createNewView(); |
| 115 | getInstrumentation().runOnMainSync(new Runnable() { |
| 116 | @Override |
| 117 | public void run() { |
| 118 | Bundle bundle = new Bundle(); |
| 119 | boolean result = vars.awContents.saveState(bundle); |
| 120 | assertTrue(result); |
| 121 | result = restoredVars.awContents.restoreState(bundle); |
| 122 | assertTrue(result); |
| 123 | } |
| 124 | }); |
| 125 | return restoredVars; |
| 126 | } |
| 127 | |
| 128 | @SmallTest |
| 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>() { |
| 134 | @Override |
| 135 | public Boolean call() throws Exception { |
| 136 | return TITLES[0].equals(restoredVars.contentViewCore.getTitle()) && |
| 137 | TITLES[0].equals(restoredVars.contentsClient.getUpdatedTitle()); |
| 138 | } |
| 139 | })); |
| 140 | } |
| 141 | |
| 142 | @SmallTest |
| 143 | @Feature({"AndroidWebView"}) |
| 144 | public void testSaveRestoreStateWithHistoryItemList() throws Throwable { |
| 145 | setServerResponseAndLoad(mVars, NUM_NAVIGATIONS); |
| 146 | TestVars restoredVars = saveAndRestoreStateOnUiThread(mVars); |
| 147 | checkHistoryItemList(restoredVars); |
| 148 | } |
| 149 | |
| 150 | @SmallTest |
| 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>() { |
| 157 | @Override |
| 158 | public Boolean call() throws Exception { |
| 159 | return mVars.awContents.restoreState(invalidState); |
| 160 | } |
| 161 | }); |
| 162 | assertFalse(result); |
| 163 | } |
| 164 | |
| 165 | @SmallTest |
| 166 | @Feature({"AndroidWebView"}) |
| 167 | public void testSaveStateForNoNavigationFails() throws Throwable { |
| 168 | final Bundle state = new Bundle(); |
| 169 | boolean result = runTestOnUiThreadAndGetResult(new Callable<Boolean>() { |
| 170 | @Override |
| 171 | public Boolean call() throws Exception { |
| 172 | return mVars.awContents.restoreState(state); |
| 173 | } |
| 174 | }); |
| 175 | assertFalse(result); |
| 176 | } |
| 177 | } |