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