1 // Copyright 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_shell_apk.ContentShellActivity;
16 import org.chromium.content_shell_apk.ContentShellTestBase;
18 import java.util.concurrent.Callable;
19 import java.util.concurrent.ExecutionException;
22 * Tests for interstitial pages.
24 public class InterstitialPageTest extends ContentShellTestBase {
26 private static final String URL = UrlUtils.encodeHtmlDataUri(
27 "<html><head></head><body>test</body></html>");
29 private static class TestWebContentsObserverAndroid extends WebContentsObserverAndroid {
30 private boolean mInterstitialShowing;
32 public TestWebContentsObserverAndroid(ContentViewCore contentViewCore) {
33 super(contentViewCore);
36 public boolean isInterstitialShowing() throws ExecutionException {
37 return ThreadUtils.runOnUiThreadBlocking(new Callable<Boolean>() {
39 public Boolean call() throws Exception {
40 return mInterstitialShowing;
46 public void didAttachInterstitialPage() {
47 mInterstitialShowing = true;
51 public void didDetachInterstitialPage() {
52 mInterstitialShowing = false;
57 protected void setUp() throws Exception {
59 ContentShellActivity activity = launchContentShellWithUrl(URL);
60 assertNotNull(activity);
61 waitForActiveShellToBeDoneLoading();
64 private boolean waitForInterstitial(final boolean shouldBeShown) throws InterruptedException {
65 return CriteriaHelper.pollForCriteria(new Criteria() {
67 public boolean isSatisfied() {
69 return ThreadUtils.runOnUiThreadBlocking(new Callable<Boolean>() {
71 public Boolean call() throws Exception {
73 == getContentViewCore().isShowingInterstitialPage();
76 } catch (ExecutionException e) {
84 * Tests that showing and hiding an interstitial works.
87 @Feature({"Navigation"})
88 public void testCloseInterstitial() throws InterruptedException, ExecutionException {
89 final String proceedCommand = "PROCEED";
90 final String htmlContent =
94 "function sendCommand(command) {" +
95 "window.domAutomationController.setAutomationId(1);" +
96 "window.domAutomationController.send(command);" +
100 "<body style='background-color:#FF0000' " +
101 "onclick='sendCommand(\"" + proceedCommand + "\");'>" +
102 "<h1>This is a scary interstitial page</h1>" +
105 final InterstitialPageDelegateAndroid delegate =
106 new InterstitialPageDelegateAndroid(htmlContent) {
108 protected void commandReceived(String command) {
109 assertEquals(command, proceedCommand);
113 TestWebContentsObserverAndroid observer = ThreadUtils.runOnUiThreadBlocking(
114 new Callable<TestWebContentsObserverAndroid>() {
116 public TestWebContentsObserverAndroid call() throws Exception {
117 getContentViewCore().showInterstitialPage(URL, delegate);
118 return new TestWebContentsObserverAndroid(getContentViewCore());
122 assertTrue("Interstitial never shown.", waitForInterstitial(true));
123 assertTrue("WebContentsObserver not notified of interstitial showing",
124 observer.isInterstitialShowing());
125 TouchCommon touchCommon = new TouchCommon(this);
126 touchCommon.singleClickViewRelative(getContentViewCore().getContainerView(), 10, 10);
127 assertTrue("Interstitial never hidden.", waitForInterstitial(false));
128 assertTrue("WebContentsObserver not notified of interstitial hiding",
129 !observer.isInterstitialShowing());