Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / public / android / javatests / src / org / chromium / content / browser / InterstitialPageTest.java
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
5 package org.chromium.content.browser;
6
7 import android.test.suitebuilder.annotation.LargeTest;
8
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;
17
18 import java.util.concurrent.Callable;
19 import java.util.concurrent.ExecutionException;
20
21 /**
22  * Tests for interstitial pages.
23  */
24 public 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 boolean waitForInterstitial(final boolean shouldBeShown) throws InterruptedException {
65         return CriteriaHelper.pollForCriteria(new Criteria() {
66             @Override
67             public boolean isSatisfied() {
68                 try {
69                     return ThreadUtils.runOnUiThreadBlocking(new Callable<Boolean>() {
70                         @Override
71                         public Boolean call() throws Exception {
72                             return shouldBeShown
73                                     == getContentViewCore().isShowingInterstitialPage();
74                         }
75                     });
76                 } catch (ExecutionException e) {
77                     return false;
78                 }
79             }
80         });
81     }
82
83     /**
84      * Tests that showing and hiding an interstitial works.
85      */
86     @LargeTest
87     @Feature({"Navigation"})
88     public void testCloseInterstitial() throws InterruptedException, ExecutionException {
89         final String proceedCommand = "PROCEED";
90         final String htmlContent =
91                 "<html>" +
92                         "<head>" +
93                         "<script>" +
94                                 "function sendCommand(command) {" +
95                                         "window.domAutomationController.setAutomationId(1);" +
96                                         "window.domAutomationController.send(command);" +
97                                 "}" +
98                         "</script>" +
99                         "</head>" +
100                         "<body style='background-color:#FF0000' " +
101                                 "onclick='sendCommand(\"" + proceedCommand + "\");'>" +
102                                 "<h1>This is a scary interstitial page</h1>" +
103                         "</body>" +
104                 "</html>";
105         final InterstitialPageDelegateAndroid delegate =
106                 new InterstitialPageDelegateAndroid(htmlContent) {
107             @Override
108             protected void commandReceived(String command) {
109                 assertEquals(command, proceedCommand);
110                 proceed();
111             }
112         };
113         TestWebContentsObserverAndroid observer = ThreadUtils.runOnUiThreadBlocking(
114                 new Callable<TestWebContentsObserverAndroid>() {
115                     @Override
116                     public TestWebContentsObserverAndroid call() throws Exception {
117                         getContentViewCore().showInterstitialPage(URL, delegate);
118                         return new TestWebContentsObserverAndroid(getContentViewCore());
119                     }
120                 });
121
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());
130     }
131 }