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