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.
5 package org.chromium.content.browser;
7 import android.test.suitebuilder.annotation.LargeTest;
9 import junit.framework.Assert;
11 import org.chromium.base.test.util.DisabledTest;
12 import org.chromium.base.test.util.Feature;
13 import org.chromium.base.test.util.UrlUtils;
14 import org.chromium.content.browser.test.util.Criteria;
15 import org.chromium.content.browser.test.util.CriteriaHelper;
16 import org.chromium.content.browser.test.util.DOMUtils;
17 import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
18 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPageFinishedHelper;
19 import org.chromium.content_shell_apk.ContentShellTestBase;
21 import java.util.concurrent.TimeUnit;
23 public class GestureDetectorResetTest extends ContentShellTestBase {
24 private static final int WAIT_TIMEOUT_SECONDS = 2;
25 private static final String CLICK_TEST_URL = UrlUtils.encodeHtmlDataUri(
27 "<button id=\"button\" " +
28 " onclick=\"document.getElementById('test').textContent = 'clicked';\">" +
31 "<div id=\"test\">not clicked</div><br/>" +
34 private static class NodeContentsIsEqualToCriteria implements Criteria {
35 private final ContentView mView;
36 private final TestCallbackHelperContainer mViewClient;
37 private final String mNodeId;
38 private final String mExpectedContents;
40 public NodeContentsIsEqualToCriteria(
42 TestCallbackHelperContainer viewClient,
43 String nodeId, String expectedContents) {
45 mViewClient = viewClient;
47 mExpectedContents = expectedContents;
48 assert mExpectedContents != null;
52 public boolean isSatisfied() {
54 String contents = DOMUtils.getNodeContents(mView, mViewClient, mNodeId);
55 return mExpectedContents.equals(contents);
56 } catch (Throwable e) {
57 Assert.fail("Failed to retrieve node contents: " + e);
63 public GestureDetectorResetTest() {
66 private void verifyClicksAreRegistered(
67 String disambiguation,
68 ContentView view, TestCallbackHelperContainer viewClient)
69 throws InterruptedException, Exception, Throwable {
70 // Initially the text on the page should say "not clicked".
71 assertTrue("The page contents is invalid " + disambiguation,
72 CriteriaHelper.pollForCriteria(new NodeContentsIsEqualToCriteria(
73 view, viewClient, "test", "not clicked")));
76 DOMUtils.clickNode(this, view, viewClient, "button");
78 // After the click, the text on the page should say "clicked".
79 assertTrue("The page contents didn't change after a click " + disambiguation,
80 CriteriaHelper.pollForCriteria(new NodeContentsIsEqualToCriteria(
81 view, viewClient, "test", "clicked")));
85 * Tests that showing a select popup and having the page reload while the popup is showing does
89 * @Feature({"Browser"})
93 public void testSeparateClicksAreRegisteredOnReload()
94 throws InterruptedException, Exception, Throwable {
95 // Load the test page.
96 launchContentShellWithUrl(CLICK_TEST_URL);
97 assertTrue("Page failed to load", waitForActiveShellToBeDoneLoading());
99 final ContentView view = getActivity().getActiveContentView();
100 final TestCallbackHelperContainer viewClient =
101 new TestCallbackHelperContainer(view);
102 final OnPageFinishedHelper onPageFinishedHelper =
103 viewClient.getOnPageFinishedHelper();
105 // Test that the button click works.
106 verifyClicksAreRegistered("on initial load", view, viewClient);
108 // Reload the test page.
109 int currentCallCount = onPageFinishedHelper.getCallCount();
110 getInstrumentation().runOnMainSync(new Runnable() {
113 getActivity().getActiveShell().loadUrl(CLICK_TEST_URL);
116 onPageFinishedHelper.waitForCallback(currentCallCount, 1,
117 WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
119 // Test that the button click still works.
120 verifyClicksAreRegistered("after reload", view, viewClient);
122 // Directly navigate to the test page.
123 currentCallCount = onPageFinishedHelper.getCallCount();
124 getInstrumentation().runOnMainSync(new Runnable() {
127 getActivity().getActiveShell().getContentView().loadUrl(
128 new LoadUrlParams(CLICK_TEST_URL));
131 onPageFinishedHelper.waitForCallback(currentCallCount, 1,
132 WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
134 // Test that the button click still works.
135 verifyClicksAreRegistered("after direct navigation", view, viewClient);