1 // Copyright 2013 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.content.browser;
7 import android.test.suitebuilder.annotation.MediumTest;
9 import org.chromium.base.test.util.Feature;
10 import org.chromium.base.test.util.UrlUtils;
11 import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
12 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper;
13 import org.chromium.content_public.browser.LoadUrlParams;
14 import org.chromium.content_public.browser.NavigationController;
15 import org.chromium.content_public.browser.NavigationHistory;
16 import org.chromium.content_shell_apk.ContentShellActivity;
17 import org.chromium.content_shell_apk.ContentShellTestBase;
20 * Tests for various aspects of navigation.
22 public class NavigationTest extends ContentShellTestBase {
24 private static final String URL_1 = UrlUtils.encodeHtmlDataUri("<html>1</html>");
25 private static final String URL_2 = UrlUtils.encodeHtmlDataUri("<html>2</html>");
26 private static final String URL_3 = UrlUtils.encodeHtmlDataUri("<html>3</html>");
27 private static final String URL_4 = UrlUtils.encodeHtmlDataUri("<html>4</html>");
28 private static final String URL_5 = UrlUtils.encodeHtmlDataUri("<html>5</html>");
29 private static final String URL_6 = UrlUtils.encodeHtmlDataUri("<html>6</html>");
30 private static final String URL_7 = UrlUtils.encodeHtmlDataUri("<html>7</html>");
32 private void goBack(final NavigationController navigationController,
33 TestCallbackHelperContainer testCallbackHelperContainer) throws Throwable {
34 handleBlockingCallbackAction(
35 testCallbackHelperContainer.getOnPageFinishedHelper(),
39 navigationController.goBack();
44 private void reload(final NavigationController navigationController,
45 TestCallbackHelperContainer testCallbackHelperContainer) throws Throwable {
46 handleBlockingCallbackAction(
47 testCallbackHelperContainer.getOnPageFinishedHelper(),
51 navigationController.reload(true);
57 @Feature({"Navigation"})
58 public void testDirectedNavigationHistory() throws Throwable {
59 ContentShellActivity activity = launchContentShellWithUrl(URL_1);
60 waitForActiveShellToBeDoneLoading();
61 ContentViewCore contentViewCore = activity.getActiveContentViewCore();
62 NavigationController navigationController = contentViewCore.getWebContents()
63 .getNavigationController();
64 TestCallbackHelperContainer testCallbackHelperContainer =
65 new TestCallbackHelperContainer(contentViewCore);
67 loadUrl(navigationController, testCallbackHelperContainer, new LoadUrlParams(URL_2));
68 loadUrl(navigationController, testCallbackHelperContainer, new LoadUrlParams(URL_3));
69 loadUrl(navigationController, testCallbackHelperContainer, new LoadUrlParams(URL_4));
70 loadUrl(navigationController, testCallbackHelperContainer, new LoadUrlParams(URL_5));
71 loadUrl(navigationController, testCallbackHelperContainer, new LoadUrlParams(URL_6));
72 loadUrl(navigationController, testCallbackHelperContainer, new LoadUrlParams(URL_7));
74 NavigationHistory history = navigationController.getDirectedNavigationHistory(false, 3);
75 assertEquals(3, history.getEntryCount());
76 assertEquals(URL_6, history.getEntryAtIndex(0).getUrl());
77 assertEquals(URL_5, history.getEntryAtIndex(1).getUrl());
78 assertEquals(URL_4, history.getEntryAtIndex(2).getUrl());
80 history = navigationController.getDirectedNavigationHistory(true, 3);
81 assertEquals(history.getEntryCount(), 0);
83 goBack(navigationController, testCallbackHelperContainer);
84 goBack(navigationController, testCallbackHelperContainer);
85 goBack(navigationController, testCallbackHelperContainer);
87 history = navigationController.getDirectedNavigationHistory(false, 4);
88 assertEquals(3, history.getEntryCount());
89 assertEquals(URL_3, history.getEntryAtIndex(0).getUrl());
90 assertEquals(URL_2, history.getEntryAtIndex(1).getUrl());
91 assertEquals(URL_1, history.getEntryAtIndex(2).getUrl());
93 history = navigationController.getDirectedNavigationHistory(true, 4);
94 assertEquals(3, history.getEntryCount());
95 assertEquals(URL_5, history.getEntryAtIndex(0).getUrl());
96 assertEquals(URL_6, history.getEntryAtIndex(1).getUrl());
97 assertEquals(URL_7, history.getEntryAtIndex(2).getUrl());
101 * Tests whether a page was successfully reloaded.
102 * Checks to make sure that OnPageFinished events were fired and that the timestamps of when
103 * the page loaded are different after the reload.
106 @Feature({"Navigation"})
107 public void testPageReload() throws Throwable {
108 final String htmlLoadTime = "<html><head>" +
109 "<script type=\"text/javascript\">var loadTimestamp = new Date().getTime();" +
110 "function getLoadtime() { return loadTimestamp; }</script></head></html>";
111 final String urlLoadTime = UrlUtils.encodeHtmlDataUri(htmlLoadTime);
113 ContentShellActivity activity = launchContentShellWithUrl(urlLoadTime);
114 waitForActiveShellToBeDoneLoading();
115 ContentViewCore contentViewCore = activity.getActiveContentViewCore();
116 TestCallbackHelperContainer testCallbackHelperContainer =
117 new TestCallbackHelperContainer(contentViewCore);
118 OnEvaluateJavaScriptResultHelper javascriptHelper = new OnEvaluateJavaScriptResultHelper();
120 // Grab the first timestamp.
121 javascriptHelper.evaluateJavaScript(contentViewCore.getWebContents(), "getLoadtime();");
122 javascriptHelper.waitUntilHasValue();
123 String firstTimestamp = javascriptHelper.getJsonResultAndClear();
124 assertNotNull("Timestamp was null.", firstTimestamp);
126 // Grab the timestamp after a reload and make sure they don't match.
127 reload(contentViewCore.getWebContents().getNavigationController(),
128 testCallbackHelperContainer);
129 javascriptHelper.evaluateJavaScript(contentViewCore.getWebContents(), "getLoadtime();");
130 javascriptHelper.waitUntilHasValue();
131 String secondTimestamp = javascriptHelper.getJsonResultAndClear();
132 assertNotNull("Timestamp was null.", secondTimestamp);
133 assertFalse("Timestamps matched.", firstTimestamp.equals(secondTimestamp));