- add sources.
[platform/framework/web/crosswalk.git] / src / content / public / android / javatests / src / org / chromium / content / browser / GestureDetectorResetTest.java
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.
4
5 package org.chromium.content.browser;
6
7 import android.test.suitebuilder.annotation.LargeTest;
8
9 import junit.framework.Assert;
10
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;
20
21 import java.util.concurrent.TimeUnit;
22
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(
26             "<html><body>" +
27             "<button id=\"button\" " +
28             "  onclick=\"document.getElementById('test').textContent = 'clicked';\">" +
29             "Button" +
30             "</button><br/>" +
31             "<div id=\"test\">not clicked</div><br/>" +
32             "</body></html>");
33
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;
39
40         public NodeContentsIsEqualToCriteria(
41                 ContentView view,
42                 TestCallbackHelperContainer viewClient,
43                 String nodeId, String expectedContents) {
44             mView = view;
45             mViewClient = viewClient;
46             mNodeId = nodeId;
47             mExpectedContents = expectedContents;
48             assert mExpectedContents != null;
49         }
50
51         @Override
52         public boolean isSatisfied() {
53             try {
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);
58                 return false;
59             }
60         }
61     }
62
63     public GestureDetectorResetTest() {
64     }
65
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")));
74
75         // Click the button.
76         DOMUtils.clickNode(this, view, viewClient, "button");
77
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")));
82     }
83
84     /**
85      * Tests that showing a select popup and having the page reload while the popup is showing does
86      * not assert.
87      *
88      * @LargeTest
89      * @Feature({"Browser"})
90      * BUG 172967
91      */
92     @DisabledTest
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());
98
99         final ContentView view = getActivity().getActiveContentView();
100         final TestCallbackHelperContainer viewClient =
101                 new TestCallbackHelperContainer(view);
102         final OnPageFinishedHelper onPageFinishedHelper =
103                 viewClient.getOnPageFinishedHelper();
104
105         // Test that the button click works.
106         verifyClicksAreRegistered("on initial load", view, viewClient);
107
108         // Reload the test page.
109         int currentCallCount = onPageFinishedHelper.getCallCount();
110         getInstrumentation().runOnMainSync(new Runnable() {
111             @Override
112             public void run() {
113                 getActivity().getActiveShell().loadUrl(CLICK_TEST_URL);
114             }
115         });
116         onPageFinishedHelper.waitForCallback(currentCallCount, 1,
117                 WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
118
119         // Test that the button click still works.
120         verifyClicksAreRegistered("after reload", view, viewClient);
121
122         // Directly navigate to the test page.
123         currentCallCount = onPageFinishedHelper.getCallCount();
124         getInstrumentation().runOnMainSync(new Runnable() {
125             @Override
126             public void run() {
127                 getActivity().getActiveShell().getContentView().loadUrl(
128                         new LoadUrlParams(CLICK_TEST_URL));
129             }
130         });
131         onPageFinishedHelper.waitForCallback(currentCallCount, 1,
132                 WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
133
134         // Test that the button click still works.
135         verifyClicksAreRegistered("after direct navigation", view, viewClient);
136     }
137 }