Chromium Code Reviews| Index: android_webview/javatests/src/org/chromium/android_webview/test/SaveRestoreStateTest.java |
| diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/SaveRestoreStateTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/SaveRestoreStateTest.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0855c703871c99235455a77346d41189d8ec246c |
| --- /dev/null |
| +++ b/android_webview/javatests/src/org/chromium/android_webview/test/SaveRestoreStateTest.java |
| @@ -0,0 +1,148 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.android_webview.test; |
| + |
| +import android.os.Bundle; |
| + |
| +import org.chromium.android_webview.AwContents; |
| +import org.chromium.android_webview.test.util.CommonResources; |
| +import org.chromium.content.browser.ContentViewCore; |
| +import org.chromium.net.test.util.TestWebServer; |
| + |
| +import java.util.concurrent.Callable; |
| + |
| +public class SaveRestoreStateTest extends AndroidWebViewTestBase { |
| + private static class TestVars { |
|
mkosiba (inactive)
2012/11/20 17:38:56
why do you need this? IIRC all of these can be acc
boliu
2012/11/20 18:58:16
For convenience only. As you can see from the cons
|
| + public final TestAwContentsClient contentsClient; |
| + public final AwTestContainerView testView; |
| + public final AwContents awContents; |
| + public final ContentViewCore contentViewCore; |
| + |
| + public TestVars(TestAwContentsClient contentsClient, |
| + AwTestContainerView testView) { |
| + this.contentsClient = contentsClient; |
| + this.testView = testView; |
| + this.awContents = testView.getAwContents(); |
| + this.contentViewCore = this.awContents.getContentViewCore(); |
| + } |
| + } |
| + |
| + private TestVars createNewView() throws Exception { |
| + TestAwContentsClient contentsClient = new TestAwContentsClient();; |
| + AwTestContainerView testView = createAwTestContainerViewOnMainSync(contentsClient); |
| + return new TestVars(contentsClient, testView); |
| + } |
| + |
| + private TestVars mVars; |
| + private TestWebServer mWebServer; |
| + |
| + private static final int NUM_NAVIGATIONS = 3; |
| + private static final String TITLES[] = { |
| + "page 1 title foo", |
| + "page 2 title bar", |
| + "page 3 title baz" |
| + }; |
| + private static final String PATHS[] = { |
| + "/p1foo.html", |
| + "/p2bar.html", |
| + "/p3baz.html", |
| + }; |
| + |
| + private String mUrls[]; |
| + |
| + @Override |
| + public void setUp() throws Exception { |
| + super.setUp(); |
| + mVars = createNewView(); |
| + mUrls = new String[NUM_NAVIGATIONS]; |
| + mWebServer = new TestWebServer(false); |
| + } |
| + |
| + @Override |
| + public void tearDown() throws Exception { |
| + if (mWebServer != null) { |
| + mWebServer.shutdown(); |
| + } |
| + super.tearDown(); |
| + } |
| + |
| + private void setServerResponseAndLoad(TestVars vars, int upto) throws Throwable { |
| + for (int i = 0; i < upto; ++i) { |
| + String html = CommonResources.makeHtmlPageFrom( |
| + "<title>" + TITLES[i] + "</title>", |
| + ""); |
| + mUrls[i] = mWebServer.setResponse(PATHS[i], html, null); |
| + |
| + loadUrlSync(vars.awContents, |
| + vars.contentsClient.getOnPageFinishedHelper(), |
| + mUrls[i]); |
| + } |
| + } |
| + |
| + private AwContents.HistoryItemList getHistoryItemListOnUiThread( |
| + final TestVars vars) throws Throwable{ |
| + return runTestOnUiThreadAndGetResult(new Callable<AwContents.HistoryItemList>() { |
| + @Override |
| + public AwContents.HistoryItemList call() throws Exception { |
| + return vars.awContents.getHistoryItemList(); |
| + } |
| + }); |
| + } |
| + |
| + private void checkHistoryItemList(TestVars vars) throws Throwable { |
| + AwContents.HistoryItemList list = getHistoryItemListOnUiThread(vars); |
| + assertEquals(NUM_NAVIGATIONS, list.historyItemList.size()); |
| + assertEquals(NUM_NAVIGATIONS - 1, list.currentIndex); |
| + |
| + for (int i = 0; i < NUM_NAVIGATIONS; ++i) { |
| + // TODO(boliu): Test redirects. |
| + // TODO(boliu): Test favicon. |
| + assertEquals(mUrls[i], list.historyItemList.get(i).originalUrl); |
| + assertEquals(mUrls[i], list.historyItemList.get(i).url); |
| + assertEquals(TITLES[i], list.historyItemList.get(i).title); |
| + } |
| + } |
| + |
| + private TestVars saveAndRestoreStateOnUiThread(final TestVars vars) throws Throwable { |
| + final TestVars restoredVars = createNewView(); |
| + getInstrumentation().runOnMainSync(new Runnable() { |
| + @Override |
| + public void run() { |
| + Bundle bundle = new Bundle(); |
| + vars.awContents.saveState(bundle); |
| + restoredVars.awContents.restoreState(bundle); |
| + } |
| + }); |
| + return restoredVars; |
| + } |
| + |
| + public void testHistoryItemListNoNavigation() throws Throwable { |
|
mkosiba (inactive)
2012/11/20 17:38:56
maybe we should have a separate test case for t
boliu
2012/11/20 18:58:16
not applicable anymore, I found a whole other file
|
| + AwContents.HistoryItemList list = getHistoryItemListOnUiThread(mVars); |
| + assertNotNull(list); |
| + assertEquals(0, list.historyItemList.size()); |
| + } |
| + |
| + public void testHistoryItemList() throws Throwable { |
| + setServerResponseAndLoad(mVars, NUM_NAVIGATIONS); |
| + checkHistoryItemList(mVars); |
| + } |
| + |
| + public void testSaveRestoreStateWithTitle() throws Throwable { |
| + setServerResponseAndLoad(mVars, 1); |
| + final TestVars restoredVars = saveAndRestoreStateOnUiThread(mVars); |
| + assertTrue(pollOnUiThread(new Callable<Boolean>() { |
| + @Override |
| + public Boolean call() throws Exception { |
| + return TITLES[0].equals(restoredVars.contentViewCore.getTitle()); |
| + } |
| + })); |
| + } |
| + |
| + public void testSaveRestoreStateWithHistoryItemList() throws Throwable { |
| + setServerResponseAndLoad(mVars, NUM_NAVIGATIONS); |
| + TestVars restoredVars = saveAndRestoreStateOnUiThread(mVars); |
| + checkHistoryItemList(restoredVars); |
| + } |
| +} |