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.
5 package org.chromium.content.browser;
7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
9 import android.net.Uri;
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;
18 import java.util.concurrent.TimeUnit;
21 * Base class for content detection test suites.
23 public class ContentDetectionTestBase extends ContentShellTestBase {
25 private static final long WAIT_TIMEOUT_SECONDS = scaleTimeout(10);
27 private TestCallbackHelperContainer mCallbackHelper;
30 * Returns the TestCallbackHelperContainer associated with this ContentView,
31 * or creates it lazily.
33 protected TestCallbackHelperContainer getTestCallbackHelperContainer() {
34 if (mCallbackHelper == null) {
35 mCallbackHelper = new TestCallbackHelperContainer(getContentViewCore());
37 return mCallbackHelper;
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.
45 protected String urlForContent(String content) {
46 return Uri.encode(content).replaceAll("%20", "+");
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.
54 protected boolean isCurrentTestUrl(String testUrl) {
55 return UrlUtils.getTestFileUrl(testUrl).equals(getContentViewCore().getUrl());
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.
63 protected String scrollAndTapExpectingIntent(String id) throws Throwable {
64 TestCallbackHelperContainer callbackHelperContainer = getTestCallbackHelperContainer();
65 OnStartContentIntentHelper onStartContentIntentHelper =
66 callbackHelperContainer.getOnStartContentIntentHelper();
67 int currentCallCount = onStartContentIntentHelper.getCallCount();
69 DOMUtils.scrollNodeIntoView(getContentViewCore(), id);
70 DOMUtils.clickNode(this, getContentViewCore(), id);
72 onStartContentIntentHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS,
74 getInstrumentation().waitForIdleSync();
75 return onStartContentIntentHelper.getIntentUrl();
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.
84 protected void scrollAndTapNavigatingOut(String id) throws Throwable {
85 TestCallbackHelperContainer callbackHelperContainer = getTestCallbackHelperContainer();
86 OnPageFinishedHelper onPageFinishedHelper =
87 callbackHelperContainer.getOnPageFinishedHelper();
88 int currentCallCount = onPageFinishedHelper.getCallCount();
90 DOMUtils.scrollNodeIntoView(getContentViewCore(), id);
91 DOMUtils.clickNode(this, getContentViewCore(), id);
93 onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS,
95 getInstrumentation().waitForIdleSync();