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 static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
8
9import android.net.Uri;
10
11import org.chromium.base.test.util.UrlUtils;
12import org.chromium.content.browser.test.util.DOMUtils;
13import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
14import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPageFinishedHelper;
15import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnStartContentIntentHelper;
16import org.chromium.content_shell_apk.ContentShellTestBase;
17
18import java.util.concurrent.TimeUnit;
19
20/**
21 * Base class for content detection test suites.
22 */
23public 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()
56                .getWebContents().getUrl());
57    }
58
59    /**
60     * Scrolls to the node with the provided id, taps on it and waits for an intent to come.
61     * @param id Id of the node to scroll and tap.
62     * @return The content url of the received intent or null if none.
63     */
64    protected String scrollAndTapExpectingIntent(String id) throws Throwable {
65        TestCallbackHelperContainer callbackHelperContainer = getTestCallbackHelperContainer();
66        OnStartContentIntentHelper onStartContentIntentHelper =
67                callbackHelperContainer.getOnStartContentIntentHelper();
68        int currentCallCount = onStartContentIntentHelper.getCallCount();
69
70        DOMUtils.scrollNodeIntoView(getContentViewCore(), id);
71        DOMUtils.clickNode(this, getContentViewCore(), id);
72
73        onStartContentIntentHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS,
74                TimeUnit.SECONDS);
75        getInstrumentation().waitForIdleSync();
76        return onStartContentIntentHelper.getIntentUrl();
77    }
78
79    /**
80     * Scrolls to the node with the provided id, taps on it and waits for a new page load to finish.
81     * Useful when tapping on links that take to other pages.
82     * @param id Id of the node to scroll and tap.
83     * @return The content url of the received intent or null if none.
84     */
85    protected void scrollAndTapNavigatingOut(String id) throws Throwable {
86        TestCallbackHelperContainer callbackHelperContainer = getTestCallbackHelperContainer();
87        OnPageFinishedHelper onPageFinishedHelper =
88                callbackHelperContainer.getOnPageFinishedHelper();
89        int currentCallCount = onPageFinishedHelper.getCallCount();
90
91        DOMUtils.scrollNodeIntoView(getContentViewCore(), id);
92        DOMUtils.clickNode(this, getContentViewCore(), id);
93
94        onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS,
95                TimeUnit.SECONDS);
96        getInstrumentation().waitForIdleSync();
97    }
98}
99