| Index: content/public/android/javatests/src/org/chromium/content/browser/ContentDetectionActivityTestRule.java
|
| diff --git a/content/public/android/javatests/src/org/chromium/content/browser/ContentDetectionActivityTestRule.java b/content/public/android/javatests/src/org/chromium/content/browser/ContentDetectionActivityTestRule.java
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..9cf717aed0f045c69ccb049c30e872eac38f6b7f
|
| --- /dev/null
|
| +++ b/content/public/android/javatests/src/org/chromium/content/browser/ContentDetectionActivityTestRule.java
|
| @@ -0,0 +1,96 @@
|
| +// Copyright 2017 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +package org.chromium.content.browser;
|
| +
|
| +import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
|
| +
|
| +import android.net.Uri;
|
| +import android.support.test.InstrumentationRegistry;
|
| +
|
| +import org.chromium.base.test.util.UrlUtils;
|
| +import org.chromium.content.browser.test.util.DOMUtils;
|
| +import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
|
| +import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPageFinishedHelper;
|
| +import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnStartContentIntentHelper;
|
| +import org.chromium.content_shell_apk.ContentShellActivityTestRule;
|
| +
|
| +import java.util.concurrent.TimeUnit;
|
| +
|
| +/**
|
| + * ActivityTestRule for content detection test suites.
|
| + */
|
| +public class ContentDetectionActivityTestRule extends ContentShellActivityTestRule {
|
| + private static final long WAIT_TIMEOUT_SECONDS = scaleTimeout(10);
|
| +
|
| + private TestCallbackHelperContainer mCallbackHelper;
|
| +
|
| + /**
|
| + * Returns the TestCallbackHelperContainer associated with this ContentView,
|
| + * or creates it lazily.
|
| + */
|
| + public TestCallbackHelperContainer getTestCallbackHelperContainer() {
|
| + if (mCallbackHelper == null) {
|
| + mCallbackHelper = new TestCallbackHelperContainer(getContentViewCore());
|
| + }
|
| + return mCallbackHelper;
|
| + }
|
| +
|
| + /**
|
| + * Encodes the provided content string into an escaped url as intents do.
|
| + * @param content Content to escape into a url.
|
| + * @return Escaped url.
|
| + */
|
| + public String urlForContent(String content) {
|
| + return Uri.encode(content).replaceAll("%20", "+");
|
| + }
|
| +
|
| + /**
|
| + * Checks if the provided test url is the current url in the content view.
|
| + * @param testUrl Test url to check.
|
| + * @return true if the test url is the current one, false otherwise.
|
| + */
|
| + public boolean isCurrentTestUrl(String testUrl) {
|
| + return UrlUtils.getIsolatedTestFileUrl(testUrl).equals(
|
| + getContentViewCore().getWebContents().getUrl());
|
| + }
|
| +
|
| + /**
|
| + * Scrolls to the node with the provided id, taps on it and waits for an intent to come.
|
| + * @param id Id of the node to scroll and tap.
|
| + * @return The content url of the received intent or null if none.
|
| + */
|
| + public String scrollAndTapExpectingIntent(String id) throws Throwable {
|
| + TestCallbackHelperContainer callbackHelperContainer = getTestCallbackHelperContainer();
|
| + OnStartContentIntentHelper onStartContentIntentHelper =
|
| + callbackHelperContainer.getOnStartContentIntentHelper();
|
| + int currentCallCount = onStartContentIntentHelper.getCallCount();
|
| +
|
| + DOMUtils.clickNode(getContentViewCore(), id);
|
| +
|
| + onStartContentIntentHelper.waitForCallback(
|
| + currentCallCount, 1, WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
|
| + InstrumentationRegistry.getInstrumentation().waitForIdleSync();
|
| + return onStartContentIntentHelper.getIntentUrl();
|
| + }
|
| +
|
| + /**
|
| + * Scrolls to the node with the provided id, taps on it and waits for a new page load to finish.
|
| + * Useful when tapping on links that take to other pages.
|
| + * @param id Id of the node to scroll and tap.
|
| + * @return The content url of the received intent or null if none.
|
| + */
|
| + public void scrollAndTapNavigatingOut(String id) throws Throwable {
|
| + TestCallbackHelperContainer callbackHelperContainer = getTestCallbackHelperContainer();
|
| + OnPageFinishedHelper onPageFinishedHelper =
|
| + callbackHelperContainer.getOnPageFinishedHelper();
|
| + int currentCallCount = onPageFinishedHelper.getCallCount();
|
| +
|
| + DOMUtils.clickNode(getContentViewCore(), id);
|
| +
|
| + onPageFinishedHelper.waitForCallback(
|
| + currentCallCount, 1, WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
|
| + InstrumentationRegistry.getInstrumentation().waitForIdleSync();
|
| + }
|
| +}
|
|
|