Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / public / android / javatests / src / org / chromium / content / browser / NavigationTest.java
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.
4
5 package org.chromium.content.browser;
6
7 import android.test.suitebuilder.annotation.MediumTest;
8
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_shell_apk.ContentShellActivity;
14 import org.chromium.content_shell_apk.ContentShellTestBase;
15
16 /**
17  * Tests for various aspects of navigation.
18  */
19 public class NavigationTest extends ContentShellTestBase {
20
21     private static final String URL_1 = UrlUtils.encodeHtmlDataUri("<html>1</html>");
22     private static final String URL_2 = UrlUtils.encodeHtmlDataUri("<html>2</html>");
23     private static final String URL_3 = UrlUtils.encodeHtmlDataUri("<html>3</html>");
24     private static final String URL_4 = UrlUtils.encodeHtmlDataUri("<html>4</html>");
25     private static final String URL_5 = UrlUtils.encodeHtmlDataUri("<html>5</html>");
26     private static final String URL_6 = UrlUtils.encodeHtmlDataUri("<html>6</html>");
27     private static final String URL_7 = UrlUtils.encodeHtmlDataUri("<html>7</html>");
28
29     private void goBack(final ContentViewCore contentViewCore,
30             TestCallbackHelperContainer testCallbackHelperContainer) throws Throwable {
31         handleBlockingCallbackAction(
32                 testCallbackHelperContainer.getOnPageFinishedHelper(),
33                 new Runnable() {
34                     @Override
35                     public void run() {
36                         contentViewCore.goBack();
37                     }
38                 });
39     }
40
41     private void reload(final ContentViewCore contentViewCore,
42             TestCallbackHelperContainer testCallbackHelperContainer) throws Throwable {
43         handleBlockingCallbackAction(
44                 testCallbackHelperContainer.getOnPageFinishedHelper(),
45                 new Runnable() {
46                     @Override
47                     public void run() {
48                         contentViewCore.reload(true);
49                     }
50                 });
51     }
52
53     @MediumTest
54     @Feature({"Navigation"})
55     public void testDirectedNavigationHistory() throws Throwable {
56         ContentShellActivity activity = launchContentShellWithUrl(URL_1);
57         waitForActiveShellToBeDoneLoading();
58         ContentViewCore contentViewCore = activity.getActiveContentViewCore();
59         TestCallbackHelperContainer testCallbackHelperContainer =
60                 new TestCallbackHelperContainer(contentViewCore);
61
62         loadUrl(contentViewCore, testCallbackHelperContainer, new LoadUrlParams(URL_2));
63         loadUrl(contentViewCore, testCallbackHelperContainer, new LoadUrlParams(URL_3));
64         loadUrl(contentViewCore, testCallbackHelperContainer, new LoadUrlParams(URL_4));
65         loadUrl(contentViewCore, testCallbackHelperContainer, new LoadUrlParams(URL_5));
66         loadUrl(contentViewCore, testCallbackHelperContainer, new LoadUrlParams(URL_6));
67         loadUrl(contentViewCore, testCallbackHelperContainer, new LoadUrlParams(URL_7));
68
69         NavigationHistory history = contentViewCore.getDirectedNavigationHistory(false, 3);
70         assertEquals(3, history.getEntryCount());
71         assertEquals(URL_6, history.getEntryAtIndex(0).getUrl());
72         assertEquals(URL_5, history.getEntryAtIndex(1).getUrl());
73         assertEquals(URL_4, history.getEntryAtIndex(2).getUrl());
74
75         history = contentViewCore.getDirectedNavigationHistory(true, 3);
76         assertEquals(history.getEntryCount(), 0);
77
78         goBack(contentViewCore, testCallbackHelperContainer);
79         goBack(contentViewCore, testCallbackHelperContainer);
80         goBack(contentViewCore, testCallbackHelperContainer);
81
82         history = contentViewCore.getDirectedNavigationHistory(false, 4);
83         assertEquals(3, history.getEntryCount());
84         assertEquals(URL_3, history.getEntryAtIndex(0).getUrl());
85         assertEquals(URL_2, history.getEntryAtIndex(1).getUrl());
86         assertEquals(URL_1, history.getEntryAtIndex(2).getUrl());
87
88         history = contentViewCore.getDirectedNavigationHistory(true, 4);
89         assertEquals(3, history.getEntryCount());
90         assertEquals(URL_5, history.getEntryAtIndex(0).getUrl());
91         assertEquals(URL_6, history.getEntryAtIndex(1).getUrl());
92         assertEquals(URL_7, history.getEntryAtIndex(2).getUrl());
93     }
94
95     /**
96      * Tests whether a page was successfully reloaded.
97      * Checks to make sure that OnPageFinished events were fired and that the timestamps of when
98      * the page loaded are different after the reload.
99      */
100     @MediumTest
101     @Feature({"Navigation"})
102     public void testPageReload() throws Throwable {
103         final String HTML_LOADTIME = "<html><head>" +
104                 "<script type=\"text/javascript\">var loadTimestamp = new Date().getTime();" +
105                 "function getLoadtime() { return loadTimestamp; }</script></head></html>";
106         final String URL_LOADTIME = UrlUtils.encodeHtmlDataUri(HTML_LOADTIME);
107
108         ContentShellActivity activity = launchContentShellWithUrl(URL_LOADTIME);
109         waitForActiveShellToBeDoneLoading();
110         ContentViewCore contentViewCore = activity.getActiveContentViewCore();
111         TestCallbackHelperContainer testCallbackHelperContainer =
112                 new TestCallbackHelperContainer(contentViewCore);
113         OnEvaluateJavaScriptResultHelper javascriptHelper = new OnEvaluateJavaScriptResultHelper();
114
115         // Grab the first timestamp.
116         javascriptHelper.evaluateJavaScript(contentViewCore, "getLoadtime();");
117         javascriptHelper.waitUntilHasValue();
118         String firstTimestamp = javascriptHelper.getJsonResultAndClear();
119         assertNotNull("Timestamp was null.", firstTimestamp);
120
121         // Grab the timestamp after a reload and make sure they don't match.
122         reload(contentViewCore, testCallbackHelperContainer);
123         javascriptHelper.evaluateJavaScript(contentViewCore, "getLoadtime();");
124         javascriptHelper.waitUntilHasValue();
125         String secondTimestamp = javascriptHelper.getJsonResultAndClear();
126         assertNotNull("Timestamp was null.", secondTimestamp);
127         assertFalse("Timestamps matched.", firstTimestamp.equals(secondTimestamp));
128     }
129 }