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