Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / content / public / android / javatests / src / org / chromium / content / browser / GestureDetectorResetTest.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 static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
8
9 import junit.framework.Assert;
10
11 import org.chromium.base.test.util.DisabledTest;
12 import org.chromium.base.test.util.UrlUtils;
13 import org.chromium.content.browser.test.util.Criteria;
14 import org.chromium.content.browser.test.util.CriteriaHelper;
15 import org.chromium.content.browser.test.util.DOMUtils;
16 import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
17 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPageFinishedHelper;
18 import org.chromium.content_public.browser.LoadUrlParams;
19 import org.chromium.content_shell_apk.ContentShellTestBase;
20
21 import java.util.concurrent.TimeUnit;
22
23 /**
24  * Provides test environment for Gesture Detector Reset for Content Shell.
25  * This is a helper class for Content Shell tests.
26 */
27 public class GestureDetectorResetTest extends ContentShellTestBase {
28     private static final long WAIT_TIMEOUT_SECONDS = scaleTimeout(2);
29     private static final String CLICK_TEST_URL = UrlUtils.encodeHtmlDataUri(
30             "<html><body>" +
31             "<button id=\"button\" " +
32             "  onclick=\"document.getElementById('test').textContent = 'clicked';\">" +
33             "Button" +
34             "</button><br/>" +
35             "<div id=\"test\">not clicked</div><br/>" +
36             "</body></html>");
37
38     private static class NodeContentsIsEqualToCriteria implements Criteria {
39         private final ContentViewCore mViewCore;
40         private final String mNodeId;
41         private final String mExpectedContents;
42
43         public NodeContentsIsEqualToCriteria(
44                 ContentViewCore viewCore,
45                 String nodeId, String expectedContents) {
46             mViewCore = viewCore;
47             mNodeId = nodeId;
48             mExpectedContents = expectedContents;
49             assert mExpectedContents != null;
50         }
51
52         @Override
53         public boolean isSatisfied() {
54             try {
55                 String contents = DOMUtils.getNodeContents(mViewCore.getWebContents(), mNodeId);
56                 return mExpectedContents.equals(contents);
57             } catch (Throwable e) {
58                 Assert.fail("Failed to retrieve node contents: " + e);
59                 return false;
60             }
61         }
62     }
63
64     public GestureDetectorResetTest() {
65     }
66
67     private void verifyClicksAreRegistered(
68             String disambiguation,
69             ContentViewCore contentViewCore)
70                     throws InterruptedException, Exception, Throwable {
71         // Initially the text on the page should say "not clicked".
72         assertTrue("The page contents is invalid " + disambiguation,
73                 CriteriaHelper.pollForCriteria(new NodeContentsIsEqualToCriteria(
74                         contentViewCore, "test", "not clicked")));
75
76         // Click the button.
77         DOMUtils.clickNode(this, contentViewCore, "button");
78
79         // After the click, the text on the page should say "clicked".
80         assertTrue("The page contents didn't change after a click " + disambiguation,
81                 CriteriaHelper.pollForCriteria(new NodeContentsIsEqualToCriteria(
82                         contentViewCore, "test", "clicked")));
83     }
84
85     /**
86      * Tests that showing a select popup and having the page reload while the popup is showing does
87      * not assert.
88      *
89      * @LargeTest
90      * @Feature({"Browser"})
91      * BUG 172967
92      */
93     @DisabledTest
94     public void testSeparateClicksAreRegisteredOnReload()
95             throws InterruptedException, Exception, Throwable {
96         // Load the test page.
97         launchContentShellWithUrl(CLICK_TEST_URL);
98         assertTrue("Page failed to load", waitForActiveShellToBeDoneLoading());
99
100         final ContentViewCore viewCore = getContentViewCore();
101         final TestCallbackHelperContainer viewClient =
102                 new TestCallbackHelperContainer(viewCore);
103         final OnPageFinishedHelper onPageFinishedHelper =
104                 viewClient.getOnPageFinishedHelper();
105
106         // Test that the button click works.
107         verifyClicksAreRegistered("on initial load", viewCore);
108
109         // Reload the test page.
110         int currentCallCount = onPageFinishedHelper.getCallCount();
111         getInstrumentation().runOnMainSync(new Runnable() {
112             @Override
113             public void run() {
114                 getActivity().getActiveShell().loadUrl(CLICK_TEST_URL);
115             }
116         });
117         onPageFinishedHelper.waitForCallback(currentCallCount, 1,
118                 WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
119
120         // Test that the button click still works.
121         verifyClicksAreRegistered("after reload", viewCore);
122
123         // Directly navigate to the test page.
124         currentCallCount = onPageFinishedHelper.getCallCount();
125         getInstrumentation().runOnMainSync(new Runnable() {
126             @Override
127             public void run() {
128                 getActivity().getActiveShell().getContentViewCore().getWebContents()
129                         .getNavigationController().loadUrl(
130                                 new LoadUrlParams(CLICK_TEST_URL));
131             }
132         });
133         onPageFinishedHelper.waitForCallback(currentCallCount, 1,
134                 WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
135
136         // Test that the button click still works.
137         verifyClicksAreRegistered("after direct navigation", viewCore);
138     }
139 }