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.
5 package org.chromium.content.browser;
7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
9 import junit.framework.Assert;
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;
20 import java.util.concurrent.TimeUnit;
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(
26 "<button id=\"button\" " +
27 " onclick=\"document.getElementById('test').textContent = 'clicked';\">" +
30 "<div id=\"test\">not clicked</div><br/>" +
33 private static class NodeContentsIsEqualToCriteria implements Criteria {
34 private final ContentViewCore mViewCore;
35 private final String mNodeId;
36 private final String mExpectedContents;
38 public NodeContentsIsEqualToCriteria(
39 ContentViewCore viewCore,
40 String nodeId, String expectedContents) {
43 mExpectedContents = expectedContents;
44 assert mExpectedContents != null;
48 public boolean isSatisfied() {
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);
59 public GestureDetectorResetTest() {
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")));
72 DOMUtils.clickNode(this, contentViewCore, "button");
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")));
81 * Tests that showing a select popup and having the page reload while the popup is showing does
85 * @Feature({"Browser"})
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());
95 final ContentViewCore viewCore = getContentViewCore();
96 final TestCallbackHelperContainer viewClient =
97 new TestCallbackHelperContainer(viewCore);
98 final OnPageFinishedHelper onPageFinishedHelper =
99 viewClient.getOnPageFinishedHelper();
101 // Test that the button click works.
102 verifyClicksAreRegistered("on initial load", viewCore);
104 // Reload the test page.
105 int currentCallCount = onPageFinishedHelper.getCallCount();
106 getInstrumentation().runOnMainSync(new Runnable() {
109 getActivity().getActiveShell().loadUrl(CLICK_TEST_URL);
112 onPageFinishedHelper.waitForCallback(currentCallCount, 1,
113 WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
115 // Test that the button click still works.
116 verifyClicksAreRegistered("after reload", viewCore);
118 // Directly navigate to the test page.
119 currentCallCount = onPageFinishedHelper.getCallCount();
120 getInstrumentation().runOnMainSync(new Runnable() {
123 getActivity().getActiveShell().getContentViewCore().loadUrl(
124 new LoadUrlParams(CLICK_TEST_URL));
127 onPageFinishedHelper.waitForCallback(currentCallCount, 1,
128 WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
130 // Test that the button click still works.
131 verifyClicksAreRegistered("after direct navigation", viewCore);