| OLD | NEW |
| (Empty) |
| 1 // Copyright 2017 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 | |
| 5 package org.chromium.content.browser; | |
| 6 | |
| 7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; | |
| 8 | |
| 9 import android.net.Uri; | |
| 10 | |
| 11 import org.chromium.base.test.util.CallbackHelper; | |
| 12 import org.chromium.base.test.util.UrlUtils; | |
| 13 import org.chromium.content.browser.test.util.DOMUtils; | |
| 14 import org.chromium.content.browser.test.util.TestCallbackHelperContainer; | |
| 15 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPage
FinishedHelper; | |
| 16 import org.chromium.content_shell.ShellViewAndroidDelegate.ContentIntentHandler; | |
| 17 import org.chromium.content_shell_apk.ContentShellActivity; | |
| 18 import org.chromium.content_shell_apk.ContentShellTestCommon.TestCommonCallback; | |
| 19 | |
| 20 import java.util.concurrent.TimeUnit; | |
| 21 import java.util.concurrent.TimeoutException; | |
| 22 | |
| 23 public class ContentDetectionTestCommon { | |
| 24 private static final long WAIT_TIMEOUT_SECONDS = scaleTimeout(10); | |
| 25 | |
| 26 private final TestCommonCallback<ContentShellActivity> mCallback; | |
| 27 | |
| 28 private TestCallbackHelperContainer mCallbackHelper; | |
| 29 private TestContentIntentHandler mContentIntentHandler; | |
| 30 | |
| 31 public ContentDetectionTestCommon(TestCommonCallback<ContentShellActivity> c
allback) { | |
| 32 mCallback = callback; | |
| 33 } | |
| 34 | |
| 35 /** | |
| 36 * CallbackHelper for OnStartContentIntent. | |
| 37 */ | |
| 38 private static class OnStartContentIntentHelper extends CallbackHelper { | |
| 39 private String mIntentUrl; | |
| 40 public void notifyCalled(String intentUrl) { | |
| 41 mIntentUrl = intentUrl; | |
| 42 notifyCalled(); | |
| 43 } | |
| 44 public String getIntentUrl() { | |
| 45 assert getCallCount() > 0; | |
| 46 return mIntentUrl; | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 /** | |
| 51 * ContentIntentHandler impl to test content detection. | |
| 52 */ | |
| 53 public static class TestContentIntentHandler implements ContentIntentHandler
{ | |
| 54 private OnStartContentIntentHelper mOnStartContentIntentHelper; | |
| 55 | |
| 56 public OnStartContentIntentHelper getOnStartContentIntentHelper() { | |
| 57 if (mOnStartContentIntentHelper == null) { | |
| 58 mOnStartContentIntentHelper = new OnStartContentIntentHelper(); | |
| 59 } | |
| 60 return mOnStartContentIntentHelper; | |
| 61 } | |
| 62 | |
| 63 @Override | |
| 64 public void onIntentUrlReceived(String intentUrl) { | |
| 65 mOnStartContentIntentHelper.notifyCalled(intentUrl); | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 static String urlForContent(String content) { | |
| 70 return Uri.encode(content).replaceAll("%20", "+"); | |
| 71 } | |
| 72 | |
| 73 TestCallbackHelperContainer getTestCallbackHelperContainer() { | |
| 74 if (mCallbackHelper == null) { | |
| 75 mCallbackHelper = | |
| 76 new TestCallbackHelperContainer(mCallback.getContentViewCore
ForTestCommon()); | |
| 77 } | |
| 78 return mCallbackHelper; | |
| 79 } | |
| 80 | |
| 81 void createTestContentIntentHandler() { | |
| 82 mContentIntentHandler = new TestContentIntentHandler(); | |
| 83 } | |
| 84 | |
| 85 void setContentHandler() { | |
| 86 mCallback.getActivityForTestCommon() | |
| 87 .getShellManager() | |
| 88 .getActiveShell() | |
| 89 .getViewAndroidDelegate() | |
| 90 .setContentIntentHandler(mContentIntentHandler); | |
| 91 } | |
| 92 | |
| 93 boolean isCurrentTestUrl(String testUrl) { | |
| 94 return UrlUtils.getIsolatedTestFileUrl(testUrl).equals( | |
| 95 mCallback.getContentViewCoreForTestCommon().getWebContents().get
Url()); | |
| 96 } | |
| 97 | |
| 98 String scrollAndTapExpectingIntent(String id) throws InterruptedException, T
imeoutException { | |
| 99 OnStartContentIntentHelper onStartContentIntentHelper = | |
| 100 mContentIntentHandler.getOnStartContentIntentHelper(); | |
| 101 int currentCallCount = onStartContentIntentHelper.getCallCount(); | |
| 102 | |
| 103 DOMUtils.clickNode(mCallback.getContentViewCoreForTestCommon(), id); | |
| 104 | |
| 105 onStartContentIntentHelper.waitForCallback( | |
| 106 currentCallCount, 1, WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS); | |
| 107 mCallback.getInstrumentationForTestCommon().waitForIdleSync(); | |
| 108 return onStartContentIntentHelper.getIntentUrl(); | |
| 109 } | |
| 110 | |
| 111 void scrollAndTapNavigatingOut(String id) throws InterruptedException, Timeo
utException { | |
| 112 TestCallbackHelperContainer callbackHelperContainer = getTestCallbackHel
perContainer(); | |
| 113 OnPageFinishedHelper onPageFinishedHelper = | |
| 114 callbackHelperContainer.getOnPageFinishedHelper(); | |
| 115 int currentCallCount = onPageFinishedHelper.getCallCount(); | |
| 116 | |
| 117 DOMUtils.clickNode(mCallback.getContentViewCoreForTestCommon(), id); | |
| 118 | |
| 119 onPageFinishedHelper.waitForCallback( | |
| 120 currentCallCount, 1, WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS); | |
| 121 mCallback.getInstrumentationForTestCommon().waitForIdleSync(); | |
| 122 } | |
| 123 } | |
| OLD | NEW |