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_public.browser.LoadUrlParams;
19 import org.chromium.content_shell_apk.ContentShellTestBase;
21 import java.util.concurrent.TimeUnit;
24 * Provides test environment for Gesture Detector Reset for Content Shell.
25 * This is a helper class for Content Shell tests.
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(
31 "<button id=\"button\" " +
32 " onclick=\"document.getElementById('test').textContent = 'clicked';\">" +
35 "<div id=\"test\">not clicked</div><br/>" +
38 private static class NodeContentsIsEqualToCriteria implements Criteria {
39 private final ContentViewCore mViewCore;
40 private final String mNodeId;
41 private final String mExpectedContents;
43 public NodeContentsIsEqualToCriteria(
44 ContentViewCore viewCore,
45 String nodeId, String expectedContents) {
48 mExpectedContents = expectedContents;
49 assert mExpectedContents != null;
53 public boolean isSatisfied() {
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);
64 public GestureDetectorResetTest() {
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")));
77 DOMUtils.clickNode(this, contentViewCore, "button");
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")));
86 * Tests that showing a select popup and having the page reload while the popup is showing does
90 * @Feature({"Browser"})
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());
100 final ContentViewCore viewCore = getContentViewCore();
101 final TestCallbackHelperContainer viewClient =
102 new TestCallbackHelperContainer(viewCore);
103 final OnPageFinishedHelper onPageFinishedHelper =
104 viewClient.getOnPageFinishedHelper();
106 // Test that the button click works.
107 verifyClicksAreRegistered("on initial load", viewCore);
109 // Reload the test page.
110 int currentCallCount = onPageFinishedHelper.getCallCount();
111 getInstrumentation().runOnMainSync(new Runnable() {
114 getActivity().getActiveShell().loadUrl(CLICK_TEST_URL);
117 onPageFinishedHelper.waitForCallback(currentCallCount, 1,
118 WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
120 // Test that the button click still works.
121 verifyClicksAreRegistered("after reload", viewCore);
123 // Directly navigate to the test page.
124 currentCallCount = onPageFinishedHelper.getCallCount();
125 getInstrumentation().runOnMainSync(new Runnable() {
128 getActivity().getActiveShell().getContentViewCore().getWebContents()
129 .getNavigationController().loadUrl(
130 new LoadUrlParams(CLICK_TEST_URL));
133 onPageFinishedHelper.waitForCallback(currentCallCount, 1,
134 WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
136 // Test that the button click still works.
137 verifyClicksAreRegistered("after direct navigation", viewCore);