Chromium Code Reviews| Index: content/shell/android/javatests/src/org/chromium/content_shell_apk/ContentShellTestBaseDelegate.java |
| diff --git a/content/shell/android/javatests/src/org/chromium/content_shell_apk/ContentShellTestBaseDelegate.java b/content/shell/android/javatests/src/org/chromium/content_shell_apk/ContentShellTestBaseDelegate.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3cc33eb5f7df73eb82a21de0b98a3e5349e886d3 |
| --- /dev/null |
| +++ b/content/shell/android/javatests/src/org/chromium/content_shell_apk/ContentShellTestBaseDelegate.java |
| @@ -0,0 +1,148 @@ |
| +// 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.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.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_public.browser.WebContents; |
| +import org.chromium.content_shell.Shell; |
| + |
| +import java.util.concurrent.Callable; |
| +import java.util.concurrent.ExecutionException; |
| +import java.util.concurrent.TimeUnit; |
| + |
| +/** |
| + * Base test delegate class created for Instrumentation JUnit3 to JUnit4 migration |
| + */ |
| +public final class ContentShellTestBaseDelegate { |
| + /** 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); |
| + |
| + @TargetApi(Build.VERSION_CODES.KITKAT_WATCH) |
| + @SuppressWarnings("deprecation") |
| + void assertScreenIsOn(Instrumentation instrumentation) { |
| + PowerManager pm = |
| + (PowerManager) instrumentation.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()); |
| + } |
| + } |
| + |
| + Intent getActivityLaunchIntent(Context targetContext, 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(targetContext, ContentShellActivity.class)); |
| + return intent; |
| + } |
| + |
| + void waitForActiveShellToBeDoneLoading(final ContentShellActivity activity) { |
| + // Wait for the Content Shell to be initialized. |
| + CriteriaHelper.pollUiThread(new Criteria() { |
| + @Override |
| + public boolean isSatisfied() { |
| + Shell shell = activity.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(ContentShellActivity activity) { |
| + return activity.getActiveShell().getContentViewCore(); |
| + } |
| + |
| + WebContents getWebContents(ContentShellActivity activity) { |
| + return activity.getActiveShell().getWebContents(); |
| + } |
| + |
| + Shell loadNewShell(final ContentShellActivity activity, final String url) |
| + throws ExecutionException { |
| + Shell shell = ThreadUtils.runOnUiThreadBlocking(new Callable<Shell>() { |
| + @Override |
| + public Shell call() { |
| + activity.getShellManager().launchShell(url); |
| + return activity.getActiveShell(); |
| + } |
| + }); |
| + Assert.assertNotNull("Unable to create shell.", shell); |
| + Assert.assertEquals("Active shell unexpected.", shell, activity.getActiveShell()); |
| + waitForActiveShellToBeDoneLoading(activity); |
| + |
| + return shell; |
| + } |
| + |
| + void handleBlockingCallbackAction(CallbackHelper callbackHelper, Runnable uiThreadAction) |
| + throws Throwable { |
| + int currentCallCount = callbackHelper.getCallCount(); |
| + uiThreadAction.run(); |
|
boliu
2017/03/01 23:01:23
something is inconsistent here, the contract here
the real yoland
2017/03/02 20:16:48
Done
|
| + callbackHelper.waitForCallback( |
| + currentCallCount, 1, WAIT_PAGE_LOADING_TIMEOUT_SECONDS, TimeUnit.SECONDS); |
| + } |
| + |
| + void assertWaitForPageScaleFactorMatch(final ContentViewCore viewCore, float expectedScale) { |
| + CriteriaHelper.pollInstrumentationThread( |
| + Criteria.equals(expectedScale, new Callable<Float>() { |
| + @Override |
| + public Float call() { |
| + return viewCore.getScale(); |
| + } |
| + })); |
| + } |
| + |
| + void replaceContainerView(final ContentShellActivity activity) throws Throwable { |
| + ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| + @Override |
| + public void run() { |
| + ContentView cv = |
| + ContentView.createContentView(activity, getContentViewCore(activity)); |
| + ((ViewGroup) getContentViewCore(activity).getContainerView().getParent()) |
| + .addView(cv); |
| + getContentViewCore(activity).setContainerView(cv); |
| + getContentViewCore(activity).setContainerViewInternals(cv); |
| + cv.requestFocus(); |
| + } |
| + }); |
| + } |
| +} |