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.
5 package org.chromium.content.browser;
7 import android.content.Context;
8 import android.net.Uri;
10 import java.util.concurrent.TimeUnit;
12 import org.chromium.base.test.util.UrlUtils;
13 import org.chromium.content.browser.test.util.Criteria;
14 import org.chromium.content.browser.test.util.CriteriaHelper;
15 import org.chromium.content.browser.test.util.DOMUtils;
16 import org.chromium.content.browser.test.util.JavaScriptUtils;
17 import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
18 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPageFinishedHelper;
19 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnStartContentIntentHelper;
20 import org.chromium.content_shell_apk.ContentShellTestBase;
23 * Base class for content detection test suites.
25 public class ContentDetectionTestBase extends ContentShellTestBase {
27 private static final int WAIT_TIMEOUT_SECONDS = 10;
29 private TestCallbackHelperContainer mCallbackHelper;
32 * Returns the TestCallbackHelperContainer associated with this ContentView,
33 * or creates it lazily.
35 protected TestCallbackHelperContainer getTestCallbackHelperContainer() {
36 if (mCallbackHelper == null) {
37 mCallbackHelper = new TestCallbackHelperContainer(getContentView());
39 return mCallbackHelper;
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.
47 protected String urlForContent(String content) {
48 return Uri.encode(content).replaceAll("%20", "+");
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.
56 protected boolean isCurrentTestUrl(String testUrl) {
57 return UrlUtils.getTestFileUrl(testUrl).equals(getContentView().getUrl());
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.
65 protected String scrollAndTapExpectingIntent(String id) throws Throwable {
66 TestCallbackHelperContainer callbackHelperContainer = getTestCallbackHelperContainer();
67 OnStartContentIntentHelper onStartContentIntentHelper =
68 callbackHelperContainer.getOnStartContentIntentHelper();
69 int currentCallCount = onStartContentIntentHelper.getCallCount();
71 DOMUtils.scrollNodeIntoView(getContentView(), callbackHelperContainer, id);
72 DOMUtils.clickNode(this, getContentView(), callbackHelperContainer, id);
74 onStartContentIntentHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS,
76 getInstrumentation().waitForIdleSync();
77 return onStartContentIntentHelper.getIntentUrl();
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.
86 protected void scrollAndTapNavigatingOut(String id) throws Throwable {
87 TestCallbackHelperContainer callbackHelperContainer = getTestCallbackHelperContainer();
88 OnPageFinishedHelper onPageFinishedHelper =
89 callbackHelperContainer.getOnPageFinishedHelper();
90 int currentCallCount = onPageFinishedHelper.getCallCount();
92 DOMUtils.scrollNodeIntoView(getContentView(), callbackHelperContainer, id);
93 DOMUtils.clickNode(this, getContentView(), callbackHelperContainer, id);
95 onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS,
97 getInstrumentation().waitForIdleSync();