InterstitialPageTest.java revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
1// Copyright (c) 2012 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.LargeTest;
8
9import org.chromium.base.ThreadUtils;
10import org.chromium.base.test.util.Feature;
11import org.chromium.base.test.util.UrlUtils;
12import org.chromium.content.browser.test.util.Criteria;
13import org.chromium.content.browser.test.util.CriteriaHelper;
14import org.chromium.content.browser.test.util.TouchCommon;
15import org.chromium.content.browser.test.util.UiUtils;
16import org.chromium.content_shell_apk.ContentShellActivity;
17import org.chromium.content_shell_apk.ContentShellTestBase;
18
19import java.util.concurrent.Callable;
20import java.util.concurrent.ExecutionException;
21
22/**
23 * Tests for interstitial pages.
24 */
25public class InterstitialPageTest extends ContentShellTestBase {
26
27    private static final String URL = UrlUtils.encodeHtmlDataUri(
28            "<html><head></head><body>test</body></html>");
29
30    @Override
31    protected void setUp() throws Exception {
32        super.setUp();
33        ContentShellActivity activity = launchContentShellWithUrl(URL);
34        assertNotNull(activity);
35        waitForActiveShellToBeDoneLoading();
36    }
37
38    private ContentViewCore getActiveContentViewCore() {
39        return getActivity().getActiveContentView().getContentViewCore();
40    }
41
42    private boolean waitForInterstitial(final boolean shouldBeShown) throws InterruptedException {
43        return CriteriaHelper.pollForCriteria(new Criteria() {
44            @Override
45            public boolean isSatisfied() {
46                try {
47                    return ThreadUtils.runOnUiThreadBlocking(new Callable<Boolean>() {
48                        @Override
49                        public Boolean call() throws Exception {
50                            return shouldBeShown
51                                    == getActiveContentViewCore().isShowingInterstitialPage();
52                        }
53                    });
54                } catch (ExecutionException e) {
55                    return false;
56                }
57            }
58        });
59    }
60
61    /**
62     * Tests that showing and hiding an interstitial works.
63     */
64    @LargeTest
65    @Feature({"Navigation"})
66    public void testCloseInterstitial() throws InterruptedException {
67        final String proceedCommand = "PROCEED";
68        final String htmlContent =
69                "<html>" +
70                        "<head>" +
71                        "<script>" +
72                                "function sendCommand(command) {" +
73                                        "window.domAutomationController.setAutomationId(1);" +
74                                        "window.domAutomationController.send(command);" +
75                                "}" +
76                        "</script>" +
77                        "</head>" +
78                        "<body style='background-color:#FF0000' " +
79                                "onclick='sendCommand(\"" + proceedCommand + "\");'>" +
80                                "<h1>This is a scary interstitial page</h1>" +
81                        "</body>" +
82                "</html>";
83        final InterstitialPageDelegateAndroid delegate =
84                new InterstitialPageDelegateAndroid(htmlContent) {
85            @Override
86            protected void commandReceived(String command) {
87                assertEquals(command, proceedCommand);
88                proceed();
89            }
90        };
91        UiUtils.runOnUiThread(getActivity(), new Runnable() {
92            @Override
93            public void run() {
94                getActiveContentViewCore().showInterstitialPage(URL, delegate);
95            }
96        });
97        assertTrue("Interstitial never shown.", waitForInterstitial(true));
98        TouchCommon touchCommon = new TouchCommon(this);
99        touchCommon.singleClickViewRelative(getActivity().getActiveContentView(), 10, 10);
100        assertTrue("Interstitial never hidden.", waitForInterstitial(false));
101    }
102}
103