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