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