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