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.
5 package org.chromium.content.browser;
7 import android.test.suitebuilder.annotation.LargeTest;
9 import org.chromium.base.ThreadUtils;
10 import org.chromium.base.test.util.Feature;
11 import org.chromium.base.test.util.UrlUtils;
12 import org.chromium.content.browser.test.util.Criteria;
13 import org.chromium.content.browser.test.util.CriteriaHelper;
14 import org.chromium.content.browser.test.util.TouchCommon;
15 import org.chromium.content.browser.test.util.UiUtils;
16 import org.chromium.content_shell_apk.ContentShellActivity;
17 import org.chromium.content_shell_apk.ContentShellTestBase;
19 import java.util.concurrent.Callable;
20 import java.util.concurrent.ExecutionException;
21 import java.util.concurrent.atomic.AtomicReference;
24 * Tests for interstitial pages.
26 public class InterstitialPageTest extends ContentShellTestBase {
28 private static final String URL = UrlUtils.encodeHtmlDataUri(
29 "<html><head></head><body>test</body></html>");
31 private static class TestWebContentsObserverAndroid extends WebContentsObserverAndroid {
32 private boolean mInterstitialShowing;
34 public TestWebContentsObserverAndroid(ContentViewCore contentViewCore) {
35 super(contentViewCore);
38 public boolean isInterstitialShowing() throws ExecutionException {
39 return ThreadUtils.runOnUiThreadBlocking(new Callable<Boolean>() {
41 public Boolean call() throws Exception {
42 return mInterstitialShowing;
48 public void didAttachInterstitialPage() {
49 mInterstitialShowing = true;
53 public void didDetachInterstitialPage() {
54 mInterstitialShowing = false;
59 protected void setUp() throws Exception {
61 ContentShellActivity activity = launchContentShellWithUrl(URL);
62 assertNotNull(activity);
63 waitForActiveShellToBeDoneLoading();
66 private ContentViewCore getActiveContentViewCore() {
67 return getActivity().getActiveContentView().getContentViewCore();
70 private boolean waitForInterstitial(final boolean shouldBeShown) throws InterruptedException {
71 return CriteriaHelper.pollForCriteria(new Criteria() {
73 public boolean isSatisfied() {
75 return ThreadUtils.runOnUiThreadBlocking(new Callable<Boolean>() {
77 public Boolean call() throws Exception {
79 == getActiveContentViewCore().isShowingInterstitialPage();
82 } catch (ExecutionException e) {
90 * Tests that showing and hiding an interstitial works.
93 @Feature({"Navigation"})
94 public void testCloseInterstitial() throws InterruptedException, ExecutionException {
95 final String proceedCommand = "PROCEED";
96 final String htmlContent =
100 "function sendCommand(command) {" +
101 "window.domAutomationController.setAutomationId(1);" +
102 "window.domAutomationController.send(command);" +
106 "<body style='background-color:#FF0000' " +
107 "onclick='sendCommand(\"" + proceedCommand + "\");'>" +
108 "<h1>This is a scary interstitial page</h1>" +
111 final InterstitialPageDelegateAndroid delegate =
112 new InterstitialPageDelegateAndroid(htmlContent) {
114 protected void commandReceived(String command) {
115 assertEquals(command, proceedCommand);
119 TestWebContentsObserverAndroid observer = ThreadUtils.runOnUiThreadBlocking(
120 new Callable<TestWebContentsObserverAndroid>() {
122 public TestWebContentsObserverAndroid call() throws Exception {
123 getActiveContentViewCore().showInterstitialPage(URL, delegate);
124 return new TestWebContentsObserverAndroid(getActiveContentViewCore());
128 assertTrue("Interstitial never shown.", waitForInterstitial(true));
129 assertTrue("WebContentsObserver not notified of interstitial showing",
130 observer.isInterstitialShowing());
131 TouchCommon touchCommon = new TouchCommon(this);
132 touchCommon.singleClickViewRelative(getActivity().getActiveContentView(), 10, 10);
133 assertTrue("Interstitial never hidden.", waitForInterstitial(false));
134 assertTrue("WebContentsObserver not notified of interstitial hiding",
135 !observer.isInterstitialShowing());