Chromium Code Reviews| Index: content/shell/android/javatests/src/org/chromium/content_shell_apk/ContentShellTestDelegate.java |
| diff --git a/content/shell/android/javatests/src/org/chromium/content_shell_apk/ContentShellTestDelegate.java b/content/shell/android/javatests/src/org/chromium/content_shell_apk/ContentShellTestDelegate.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5094d0017d56bcb18e8fd6e6a294a0e67f736c4b |
| --- /dev/null |
| +++ b/content/shell/android/javatests/src/org/chromium/content_shell_apk/ContentShellTestDelegate.java |
| @@ -0,0 +1,196 @@ |
| +// 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_shell_apk; |
| + |
| +import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; |
| + |
| +import android.annotation.TargetApi; |
| +import android.app.Activity; |
| +import android.app.Instrumentation; |
| +import android.content.ComponentName; |
| +import android.content.Context; |
| +import android.content.Intent; |
| +import android.net.Uri; |
| +import android.os.Build; |
| +import android.os.PowerManager; |
| +import android.text.TextUtils; |
| +import android.view.ViewGroup; |
| + |
| +import org.junit.Assert; |
| + |
| +import org.chromium.base.ThreadUtils; |
| +import org.chromium.base.test.util.CallbackHelper; |
| +import org.chromium.base.test.util.UrlUtils; |
| +import org.chromium.content.browser.ContentView; |
| +import org.chromium.content.browser.ContentViewCore; |
| +import org.chromium.content.browser.test.util.Criteria; |
| +import org.chromium.content.browser.test.util.CriteriaHelper; |
| +import org.chromium.content.browser.test.util.TestCallbackHelperContainer; |
| +import org.chromium.content_public.browser.LoadUrlParams; |
| +import org.chromium.content_public.browser.NavigationController; |
| +import org.chromium.content_public.browser.WebContents; |
| +import org.chromium.content_shell.Shell; |
| + |
| +import java.util.concurrent.Callable; |
| +import java.util.concurrent.ExecutionException; |
| +import java.util.concurrent.TimeUnit; |
| + |
| +/** |
| + * Utility class for ContentShellTestBase and ContentShellActivityTestRule during |
| + * instrumentation test JUnit3 to JUnit4 migration |
| + * |
| + * Please do not use this class' methods in places other than {@link ContentShellTestBase} |
| + * and {@link ContentShellActivityTestRule} |
| + */ |
| +public final class ContentShellTestDelegate { |
|
boliu
2017/03/02 21:12:02
Ok, let's bikeshed the name a bit..
Delegate is t
the real yoland
2017/03/02 21:26:56
I see, changed to ContentShellTestCommon, since Co
|
| + /** The maximum time the waitForActiveShellToBeDoneLoading method will wait. */ |
| + private static final long WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT = scaleTimeout(10000); |
| + static final long WAIT_PAGE_LOADING_TIMEOUT_SECONDS = scaleTimeout(15); |
| + |
| + private final TestDelegateCallback<ContentShellActivity> mCallback; |
| + |
| + ContentShellTestDelegate(TestDelegateCallback<ContentShellActivity> callback) { |
| + mCallback = callback; |
| + } |
| + |
| + @TargetApi(Build.VERSION_CODES.KITKAT_WATCH) |
| + @SuppressWarnings("deprecation") |
| + void assertScreenIsOn() { |
| + PowerManager pm = (PowerManager) mCallback.getInstrumentationForDelegate() |
| + .getContext() |
| + .getSystemService(Context.POWER_SERVICE); |
| + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) { |
| + Assert.assertTrue("Many tests will fail if the screen is not on.", pm.isInteractive()); |
| + } else { |
| + Assert.assertTrue("Many tests will fail if the screen is not on.", pm.isScreenOn()); |
| + } |
| + } |
| + |
| + ContentShellActivity launchContentShellWithUrl(String url) { |
| + Intent intent = new Intent(Intent.ACTION_MAIN); |
| + intent.addCategory(Intent.CATEGORY_LAUNCHER); |
| + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| + if (url != null) intent.setData(Uri.parse(url)); |
| + intent.setComponent( |
| + new ComponentName(mCallback.getInstrumentationForDelegate().getTargetContext(), |
| + ContentShellActivity.class)); |
| + return mCallback.launchActivityWithIntentForDelegate(intent); |
| + } |
| + |
| + // TODO(yolandyan): This should use the url exactly without the getIsolatedTestFileUrl call. |
| + void launchContentShellWithUrlSync(String url) { |
| + String isolatedTestFileUrl = UrlUtils.getIsolatedTestFileUrl(url); |
| + launchContentShellWithUrl(isolatedTestFileUrl); |
| + Assert.assertNotNull(mCallback.getActivityForDelegate()); |
| + waitForActiveShellToBeDoneLoading(); |
| + Assert.assertEquals(UrlUtils.getIsolatedTestFileUrl(isolatedTestFileUrl), |
| + getContentViewCore().getWebContents().getUrl()); |
| + } |
| + |
| + void waitForActiveShellToBeDoneLoading() { |
| + // Wait for the Content Shell to be initialized. |
| + CriteriaHelper.pollUiThread(new Criteria() { |
| + @Override |
| + public boolean isSatisfied() { |
| + Shell shell = mCallback.getActivityForDelegate().getActiveShell(); |
| + // There are two cases here that need to be accounted for. |
| + // The first is that we've just created a Shell and it isn't |
| + // loading because it has no URL set yet. The second is that |
| + // we've set a URL and it actually is loading. |
| + if (shell == null) { |
| + updateFailureReason("Shell is null."); |
| + return false; |
| + } |
| + if (shell.isLoading()) { |
| + updateFailureReason("Shell is still loading."); |
| + return false; |
| + } |
| + if (TextUtils.isEmpty(shell.getContentViewCore().getWebContents().getUrl())) { |
| + updateFailureReason("Shell's URL is empty or null."); |
| + return false; |
| + } |
| + return true; |
| + } |
| + }, WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT, CriteriaHelper.DEFAULT_POLLING_INTERVAL); |
| + } |
| + |
| + ContentViewCore getContentViewCore() { |
| + return mCallback.getActivityForDelegate().getActiveShell().getContentViewCore(); |
| + } |
| + |
| + WebContents getWebContents() { |
| + return mCallback.getActivityForDelegate().getActiveShell().getWebContents(); |
| + } |
| + |
| + void loadUrl(final NavigationController navigationController, |
| + TestCallbackHelperContainer callbackHelperContainer, final LoadUrlParams params) |
| + throws Throwable { |
| + handleBlockingCallbackAction( |
| + callbackHelperContainer.getOnPageFinishedHelper(), new Runnable() { |
| + @Override |
| + public void run() { |
| + navigationController.loadUrl(params); |
| + } |
| + }); |
| + } |
| + |
| + Shell loadNewShell(final String url) throws ExecutionException { |
| + Shell shell = ThreadUtils.runOnUiThreadBlocking(new Callable<Shell>() { |
| + @Override |
| + public Shell call() { |
| + mCallback.getActivityForDelegate().getShellManager().launchShell(url); |
| + return mCallback.getActivityForDelegate().getActiveShell(); |
| + } |
| + }); |
| + Assert.assertNotNull("Unable to create shell.", shell); |
| + Assert.assertEquals("Active shell unexpected.", shell, |
| + mCallback.getActivityForDelegate().getActiveShell()); |
| + waitForActiveShellToBeDoneLoading(); |
| + return shell; |
| + } |
| + |
| + void handleBlockingCallbackAction(CallbackHelper callbackHelper, Runnable uiThreadAction) |
| + throws Throwable { |
| + int currentCallCount = callbackHelper.getCallCount(); |
| + mCallback.runOnUiThreadForDelegate(uiThreadAction); |
| + callbackHelper.waitForCallback( |
| + currentCallCount, 1, WAIT_PAGE_LOADING_TIMEOUT_SECONDS, TimeUnit.SECONDS); |
| + } |
| + |
| + void assertWaitForPageScaleFactorMatch(float expectedScale) { |
| + CriteriaHelper.pollInstrumentationThread( |
| + Criteria.equals(expectedScale, new Callable<Float>() { |
| + @Override |
| + public Float call() { |
| + return getContentViewCore().getScale(); |
| + } |
| + })); |
| + } |
| + |
| + void replaceContainerView() throws Throwable { |
| + ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| + @Override |
| + public void run() { |
| + ContentView cv = ContentView.createContentView( |
| + mCallback.getActivityForDelegate(), getContentViewCore()); |
| + ((ViewGroup) getContentViewCore().getContainerView().getParent()).addView(cv); |
| + getContentViewCore().setContainerView(cv); |
| + getContentViewCore().setContainerViewInternals(cv); |
| + cv.requestFocus(); |
| + } |
| + }); |
| + } |
| + |
| + /** |
| + * Interface used by TestRule and TestBase class to implement methods for TestDelegate class |
| + * to use. |
| + */ |
| + public static interface TestDelegateCallback<T extends Activity> { |
| + Instrumentation getInstrumentationForDelegate(); |
| + T launchActivityWithIntentForDelegate(Intent t); |
| + T getActivityForDelegate(); |
| + void runOnUiThreadForDelegate(Runnable runnable) throws Throwable; |
| + } |
| +} |