InterstitialPageTest.java revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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. 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_shell_apk.ContentShellActivity; 16import org.chromium.content_shell_apk.ContentShellTestBase; 17 18import java.util.concurrent.Callable; 19import java.util.concurrent.ExecutionException; 20 21/** 22 * Tests for interstitial pages. 23 */ 24public class InterstitialPageTest extends ContentShellTestBase { 25 26 private static final String URL = UrlUtils.encodeHtmlDataUri( 27 "<html><head></head><body>test</body></html>"); 28 29 private static class TestWebContentsObserverAndroid extends WebContentsObserverAndroid { 30 private boolean mInterstitialShowing; 31 32 public TestWebContentsObserverAndroid(ContentViewCore contentViewCore) { 33 super(contentViewCore); 34 } 35 36 public boolean isInterstitialShowing() throws ExecutionException { 37 return ThreadUtils.runOnUiThreadBlocking(new Callable<Boolean>() { 38 @Override 39 public Boolean call() throws Exception { 40 return mInterstitialShowing; 41 } 42 }).booleanValue(); 43 } 44 45 @Override 46 public void didAttachInterstitialPage() { 47 mInterstitialShowing = true; 48 } 49 50 @Override 51 public void didDetachInterstitialPage() { 52 mInterstitialShowing = false; 53 } 54 } 55 56 @Override 57 protected void setUp() throws Exception { 58 super.setUp(); 59 ContentShellActivity activity = launchContentShellWithUrl(URL); 60 assertNotNull(activity); 61 waitForActiveShellToBeDoneLoading(); 62 } 63 64 private ContentViewCore getActiveContentViewCore() { 65 return getActivity().getActiveContentView().getContentViewCore(); 66 } 67 68 private boolean waitForInterstitial(final boolean shouldBeShown) throws InterruptedException { 69 return CriteriaHelper.pollForCriteria(new Criteria() { 70 @Override 71 public boolean isSatisfied() { 72 try { 73 return ThreadUtils.runOnUiThreadBlocking(new Callable<Boolean>() { 74 @Override 75 public Boolean call() throws Exception { 76 return shouldBeShown 77 == getActiveContentViewCore().isShowingInterstitialPage(); 78 } 79 }); 80 } catch (ExecutionException e) { 81 return false; 82 } 83 } 84 }); 85 } 86 87 /** 88 * Tests that showing and hiding an interstitial works. 89 */ 90 @LargeTest 91 @Feature({"Navigation"}) 92 public void testCloseInterstitial() throws InterruptedException, ExecutionException { 93 final String proceedCommand = "PROCEED"; 94 final String htmlContent = 95 "<html>" + 96 "<head>" + 97 "<script>" + 98 "function sendCommand(command) {" + 99 "window.domAutomationController.setAutomationId(1);" + 100 "window.domAutomationController.send(command);" + 101 "}" + 102 "</script>" + 103 "</head>" + 104 "<body style='background-color:#FF0000' " + 105 "onclick='sendCommand(\"" + proceedCommand + "\");'>" + 106 "<h1>This is a scary interstitial page</h1>" + 107 "</body>" + 108 "</html>"; 109 final InterstitialPageDelegateAndroid delegate = 110 new InterstitialPageDelegateAndroid(htmlContent) { 111 @Override 112 protected void commandReceived(String command) { 113 assertEquals(command, proceedCommand); 114 proceed(); 115 } 116 }; 117 TestWebContentsObserverAndroid observer = ThreadUtils.runOnUiThreadBlocking( 118 new Callable<TestWebContentsObserverAndroid>() { 119 @Override 120 public TestWebContentsObserverAndroid call() throws Exception { 121 getActiveContentViewCore().showInterstitialPage(URL, delegate); 122 return new TestWebContentsObserverAndroid(getActiveContentViewCore()); 123 } 124 }); 125 126 assertTrue("Interstitial never shown.", waitForInterstitial(true)); 127 assertTrue("WebContentsObserver not notified of interstitial showing", 128 observer.isInterstitialShowing()); 129 TouchCommon touchCommon = new TouchCommon(this); 130 touchCommon.singleClickViewRelative(getActivity().getActiveContentView(), 10, 10); 131 assertTrue("Interstitial never hidden.", waitForInterstitial(false)); 132 assertTrue("WebContentsObserver not notified of interstitial hiding", 133 !observer.isInterstitialShowing()); 134 } 135} 136