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_public.browser.WebContents;
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;
23 * Tests for interstitial pages.
25 public class InterstitialPageTest extends ContentShellTestBase {
27 private static final String URL = UrlUtils.encodeHtmlDataUri(
28 "<html><head></head><body>test</body></html>");
30 private static class TestWebContentsObserver extends WebContentsObserver {
31 private boolean mInterstitialShowing;
33 public TestWebContentsObserver(WebContents webContents) {
37 public boolean isInterstitialShowing() throws ExecutionException {
38 return ThreadUtils.runOnUiThreadBlocking(new Callable<Boolean>() {
40 public Boolean call() throws Exception {
41 return mInterstitialShowing;
47 public void didAttachInterstitialPage() {
48 mInterstitialShowing = true;
52 public void didDetachInterstitialPage() {
53 mInterstitialShowing = false;
58 protected void setUp() throws Exception {
60 ContentShellActivity activity = launchContentShellWithUrl(URL);
61 assertNotNull(activity);
62 waitForActiveShellToBeDoneLoading();
65 private boolean waitForInterstitial(final boolean shouldBeShown) throws InterruptedException {
66 return CriteriaHelper.pollForCriteria(new Criteria() {
68 public boolean isSatisfied() {
70 return ThreadUtils.runOnUiThreadBlocking(new Callable<Boolean>() {
72 public Boolean call() throws Exception {
74 == getWebContents().isShowingInterstitialPage();
77 } catch (ExecutionException e) {
85 * Tests that showing and hiding an interstitial works.
88 @Feature({"Navigation"})
89 public void testCloseInterstitial() throws InterruptedException, ExecutionException {
90 final String proceedCommand = "PROCEED";
91 final String htmlContent =
95 "function sendCommand(command) {" +
96 "window.domAutomationController.setAutomationId(1);" +
97 "window.domAutomationController.send(command);" +
101 "<body style='background-color:#FF0000' " +
102 "onclick='sendCommand(\"" + proceedCommand + "\");'>" +
103 "<h1>This is a scary interstitial page</h1>" +
106 final InterstitialPageDelegateAndroid delegate =
107 new InterstitialPageDelegateAndroid(htmlContent) {
109 protected void commandReceived(String command) {
110 assertEquals(command, proceedCommand);
114 TestWebContentsObserver observer = ThreadUtils.runOnUiThreadBlocking(
115 new Callable<TestWebContentsObserver>() {
117 public TestWebContentsObserver call() throws Exception {
118 getWebContents().showInterstitialPage(URL, delegate.getNative());
119 return new TestWebContentsObserver(getWebContents());
123 assertTrue("Interstitial never shown.", waitForInterstitial(true));
124 assertTrue("WebContentsObserver not notified of interstitial showing",
125 observer.isInterstitialShowing());
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());