Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / public / android / javatests / src / org / chromium / content / browser / ContentDetectionTestBase.java
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
5 package org.chromium.content.browser;
6
7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
8
9 import android.net.Uri;
10
11 import org.chromium.base.test.util.UrlUtils;
12 import org.chromium.content.browser.test.util.DOMUtils;
13 import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
14 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPageFinishedHelper;
15 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnStartContentIntentHelper;
16 import org.chromium.content_shell_apk.ContentShellTestBase;
17
18 import java.util.concurrent.TimeUnit;
19
20 /**
21  * Base class for content detection test suites.
22  */
23 public class ContentDetectionTestBase extends ContentShellTestBase {
24
25     private static final long WAIT_TIMEOUT_SECONDS = scaleTimeout(10);
26
27     private TestCallbackHelperContainer mCallbackHelper;
28
29     /**
30      * Returns the TestCallbackHelperContainer associated with this ContentView,
31      * or creates it lazily.
32      */
33     protected TestCallbackHelperContainer getTestCallbackHelperContainer() {
34         if (mCallbackHelper == null) {
35             mCallbackHelper = new TestCallbackHelperContainer(getContentViewCore());
36         }
37         return mCallbackHelper;
38     }
39
40     /**
41      * Encodes the provided content string into an escaped url as intents do.
42      * @param content Content to escape into a url.
43      * @return Escaped url.
44      */
45     protected String urlForContent(String content) {
46         return Uri.encode(content).replaceAll("%20", "+");
47     }
48
49     /**
50      * Checks if the provided test url is the current url in the content view.
51      * @param testUrl Test url to check.
52      * @return true if the test url is the current one, false otherwise.
53      */
54     protected boolean isCurrentTestUrl(String testUrl) {
55         return UrlUtils.getTestFileUrl(testUrl).equals(getContentViewCore().getUrl());
56     }
57
58     /**
59      * Scrolls to the node with the provided id, taps on it and waits for an intent to come.
60      * @param id Id of the node to scroll and tap.
61      * @return The content url of the received intent or null if none.
62      */
63     protected String scrollAndTapExpectingIntent(String id) throws Throwable {
64         TestCallbackHelperContainer callbackHelperContainer = getTestCallbackHelperContainer();
65         OnStartContentIntentHelper onStartContentIntentHelper =
66                 callbackHelperContainer.getOnStartContentIntentHelper();
67         int currentCallCount = onStartContentIntentHelper.getCallCount();
68
69         DOMUtils.scrollNodeIntoView(getContentViewCore(), id);
70         DOMUtils.clickNode(this, getContentViewCore(), id);
71
72         onStartContentIntentHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS,
73                 TimeUnit.SECONDS);
74         getInstrumentation().waitForIdleSync();
75         return onStartContentIntentHelper.getIntentUrl();
76     }
77
78     /**
79      * Scrolls to the node with the provided id, taps on it and waits for a new page load to finish.
80      * Useful when tapping on links that take to other pages.
81      * @param id Id of the node to scroll and tap.
82      * @return The content url of the received intent or null if none.
83      */
84     protected void scrollAndTapNavigatingOut(String id) throws Throwable {
85         TestCallbackHelperContainer callbackHelperContainer = getTestCallbackHelperContainer();
86         OnPageFinishedHelper onPageFinishedHelper =
87                 callbackHelperContainer.getOnPageFinishedHelper();
88         int currentCallCount = onPageFinishedHelper.getCallCount();
89
90         DOMUtils.scrollNodeIntoView(getContentViewCore(), id);
91         DOMUtils.clickNode(this, getContentViewCore(), id);
92
93         onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS,
94                 TimeUnit.SECONDS);
95         getInstrumentation().waitForIdleSync();
96     }
97 }