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_shell_apk; |
| 6 |
| 7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; |
| 8 |
| 9 import android.app.Instrumentation; |
| 10 import android.content.Intent; |
| 11 import android.support.test.InstrumentationRegistry; |
| 12 import android.support.test.rule.ActivityTestRule; |
| 13 |
| 14 import org.junit.Assert; |
| 15 |
| 16 import org.chromium.base.test.util.CallbackHelper; |
| 17 import org.chromium.content.browser.ContentView; |
| 18 import org.chromium.content.browser.ContentViewCore; |
| 19 import org.chromium.content.browser.test.util.TestCallbackHelperContainer; |
| 20 import org.chromium.content_public.browser.LoadUrlParams; |
| 21 import org.chromium.content_public.browser.NavigationController; |
| 22 import org.chromium.content_public.browser.WebContents; |
| 23 import org.chromium.content_shell.Shell; |
| 24 import org.chromium.content_shell_apk.ContentShellTestCommon.TestCommonCallback; |
| 25 |
| 26 import java.lang.annotation.ElementType; |
| 27 import java.lang.annotation.Retention; |
| 28 import java.lang.annotation.RetentionPolicy; |
| 29 import java.lang.annotation.Target; |
| 30 import java.util.concurrent.ExecutionException; |
| 31 |
| 32 /** |
| 33 * ActivityTestRule for ContentShellActivity. |
| 34 * |
| 35 * Test can use this ActivityTestRule to launch or get ContentShellActivity. |
| 36 */ |
| 37 public class ContentShellActivityTestRule extends ActivityTestRule<ContentShellA
ctivity> |
| 38 implements TestCommonCallback<ContentShellActivity> { |
| 39 private final ContentShellTestCommon mDelegate; |
| 40 private final boolean mLaunchActivity; |
| 41 |
| 42 protected static final long WAIT_PAGE_LOADING_TIMEOUT_SECONDS = scaleTimeout
(15); |
| 43 |
| 44 public ContentShellActivityTestRule() { |
| 45 this(false, false); |
| 46 } |
| 47 |
| 48 public ContentShellActivityTestRule(boolean initialTouchMode, boolean launch
Activity) { |
| 49 super(ContentShellActivity.class, initialTouchMode, launchActivity); |
| 50 mLaunchActivity = launchActivity; |
| 51 mDelegate = new ContentShellTestCommon(this); |
| 52 } |
| 53 |
| 54 @Override |
| 55 public ContentShellActivity getActivityForTestCommon() { |
| 56 return getActivity(); |
| 57 } |
| 58 |
| 59 @Override |
| 60 public Instrumentation getInstrumentationForTestCommon() { |
| 61 return InstrumentationRegistry.getInstrumentation(); |
| 62 } |
| 63 |
| 64 @Override |
| 65 public ContentShellActivity launchActivityWithIntentForTestCommon(Intent t)
{ |
| 66 return launchActivity(t); |
| 67 } |
| 68 |
| 69 @Override |
| 70 public void runOnUiThreadForTestCommon(Runnable runnable) throws Throwable { |
| 71 runOnUiThread(runnable); |
| 72 } |
| 73 |
| 74 @Override |
| 75 protected void beforeActivityLaunched() { |
| 76 mDelegate.assertScreenIsOn(); |
| 77 } |
| 78 |
| 79 /** |
| 80 * Starts the ContentShell activity and loads the given URL. |
| 81 * The URL can be null, in which case will default to ContentShellActivity.D
EFAULT_SHELL_URL. |
| 82 */ |
| 83 public ContentShellActivity launchContentShellWithUrl(String url) { |
| 84 Assert.assertFalse(mLaunchActivity); |
| 85 return mDelegate.launchContentShellWithUrl(url); |
| 86 } |
| 87 |
| 88 /** |
| 89 * Starts the content shell activity with the provided test url. |
| 90 * The url is synchronously loaded. |
| 91 * @param url Test url to load. |
| 92 */ |
| 93 public void launchContentShellWithUrlSync(String url) { |
| 94 Assert.assertFalse(mLaunchActivity); |
| 95 mDelegate.launchContentShellWithUrlSync(url); |
| 96 } |
| 97 |
| 98 /** |
| 99 * Returns the current ContentViewCore or null if there is no ContentView. |
| 100 */ |
| 101 public ContentViewCore getContentViewCore() { |
| 102 return mDelegate.getContentViewCore(); |
| 103 } |
| 104 |
| 105 /** |
| 106 * Returns the WebContents of this Shell. |
| 107 */ |
| 108 public WebContents getWebContents() { |
| 109 return mDelegate.getWebContents(); |
| 110 } |
| 111 |
| 112 /** |
| 113 * Waits for the Active shell to finish loading. This times out after |
| 114 * WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT milliseconds and it shouldn't be us
ed for long |
| 115 * loading pages. Instead it should be used more for test initialization. Th
e proper way |
| 116 * to wait is to use a TestCallbackHelperContainer after the initial load is
completed. |
| 117 */ |
| 118 public void waitForActiveShellToBeDoneLoading() { |
| 119 mDelegate.waitForActiveShellToBeDoneLoading(); |
| 120 } |
| 121 |
| 122 /** |
| 123 * Creates a new {@link Shell} and waits for it to finish loading. |
| 124 * @param url The URL to create the new {@link Shell} with. |
| 125 * @return A new instance of a {@link Shell}. |
| 126 * @throws ExecutionException |
| 127 */ |
| 128 public Shell loadNewShell(String url) throws ExecutionException { |
| 129 return mDelegate.loadNewShell(url); |
| 130 } |
| 131 |
| 132 /** |
| 133 * Loads a URL in the specified content view. |
| 134 * |
| 135 * @param navigationController The navigation controller to load the URL in. |
| 136 * @param callbackHelperContainer The callback helper container used to moni
tor progress. |
| 137 * @param params The URL params to use. |
| 138 */ |
| 139 public void loadUrl(NavigationController navigationController, |
| 140 TestCallbackHelperContainer callbackHelperContainer, LoadUrlParams p
arams) |
| 141 throws Throwable { |
| 142 mDelegate.loadUrl(navigationController, callbackHelperContainer, params)
; |
| 143 } |
| 144 |
| 145 /** |
| 146 * Handles performing an action on the UI thread that will return when the s
pecified callback |
| 147 * is incremented. |
| 148 * |
| 149 * @param callbackHelper The callback helper that will be blocked on. |
| 150 * @param action The action to be performed on the UI thread. |
| 151 */ |
| 152 public void handleBlockingCallbackAction(CallbackHelper callbackHelper, Runn
able action) |
| 153 throws Throwable { |
| 154 mDelegate.handleBlockingCallbackAction(callbackHelper, action); |
| 155 } |
| 156 |
| 157 // TODO(aelias): This method needs to be removed once http://crbug.com/17951
1 is fixed. |
| 158 // Meanwhile, we have to wait if the page has the <meta viewport> tag. |
| 159 /** |
| 160 * Waits till the ContentViewCore receives the expected page scale factor |
| 161 * from the compositor and asserts that this happens. |
| 162 */ |
| 163 public void assertWaitForPageScaleFactorMatch(float expectedScale) { |
| 164 mDelegate.assertWaitForPageScaleFactorMatch(expectedScale); |
| 165 } |
| 166 |
| 167 /** |
| 168 * Replaces the {@link ContentViewCore#mContainerView} with a newly created |
| 169 * {@link ContentView}. |
| 170 */ |
| 171 @SuppressWarnings("javadoc") |
| 172 public void replaceContainerView() throws Throwable { |
| 173 mDelegate.replaceContainerView(); |
| 174 } |
| 175 |
| 176 /** |
| 177 * Annotation for tests that should be executed a second time after replacin
g |
| 178 * the ContentViewCore's container view. |
| 179 * <p>Please note that activity launch is only invoked once before both runs
, |
| 180 * and that any state changes produced by the first run are visible to the s
econd run. |
| 181 */ |
| 182 @Target(ElementType.METHOD) |
| 183 @Retention(RetentionPolicy.RUNTIME) |
| 184 public @interface RerunWithUpdatedContainerView {} |
| 185 } |
OLD | NEW |