Commit fd44a8b4 authored by Michael Thiessen's avatar Michael Thiessen Committed by Chromium LUCI CQ

Migrate android_webview to use BaseActivityTestRule

BaseActivityTestRule is meant as a chromium replacement to
ActivityTestRule, fixing a number of issues with the class and providing
more flexibility.

Bug: 1146574
Change-Id: I4a722700c88fe1881d5dcacd098a30bab3adb774
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2555901
Commit-Queue: Michael Thiessen <mthiesse@chromium.org>
Reviewed-by: default avatarBo <boliu@chromium.org>
Reviewed-by: default avatarYaron Friedman <yfriedman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#832525}
parent 1055d9bd
......@@ -9,7 +9,7 @@ import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
import android.content.Context;
import android.content.Intent;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.lifecycle.Stage;
import android.util.AndroidRuntimeException;
import android.util.Base64;
import android.view.ViewGroup;
......@@ -29,6 +29,8 @@ import org.chromium.android_webview.AwSettings;
import org.chromium.android_webview.test.util.GraphicsTestUtils;
import org.chromium.android_webview.test.util.JSUtils;
import org.chromium.base.Log;
import org.chromium.base.test.BaseActivityTestRule;
import org.chromium.base.test.util.ApplicationTestUtils;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.base.test.util.CriteriaHelper;
import org.chromium.base.test.util.InMemorySharedPreferences;
......@@ -52,7 +54,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** Custom ActivityTestRunner for WebView instrumentation tests */
public class AwActivityTestRule extends ActivityTestRule<AwTestRunnerActivity> {
public class AwActivityTestRule extends BaseActivityTestRule<AwTestRunnerActivity> {
public static final long WAIT_TIMEOUT_MS = scaleTimeout(15000L);
public static final int CHECK_INTERVAL = 100;
......@@ -79,7 +81,7 @@ public class AwActivityTestRule extends ActivityTestRule<AwTestRunnerActivity> {
private List<WeakReference<AwContents>> mAwContentsDestroyedInTearDown = new ArrayList<>();
public AwActivityTestRule() {
super(AwTestRunnerActivity.class, /* initialTouchMode */ false, /* launchActivity */ false);
super(AwTestRunnerActivity.class);
}
@Override
......@@ -134,10 +136,15 @@ public class AwActivityTestRule extends ActivityTestRule<AwTestRunnerActivity> {
return null;
}
@Override
public void launchActivity(Intent intent) {
if (getActivity() != null) return;
super.launchActivity(intent);
ApplicationTestUtils.waitForActivityState(getActivity(), Stage.RESUMED);
}
public AwTestRunnerActivity launchActivity() {
if (getActivity() == null) {
return launchActivity(getLaunchIntent());
}
launchActivity(getLaunchIntent());
return getActivity();
}
......@@ -201,6 +208,10 @@ public class AwActivityTestRule extends ActivityTestRule<AwTestRunnerActivity> {
}
}
public void runOnUiThread(Runnable r) {
TestThreadUtils.runOnUiThreadBlocking(r);
}
public static void enableJavaScriptOnUiThread(final AwContents awContents) {
TestThreadUtils.runOnUiThreadBlocking(
() -> awContents.getSettings().setJavaScriptEnabled(true));
......
......@@ -7,6 +7,8 @@ package org.chromium.android_webview.test;
import static org.chromium.android_webview.test.AwActivityTestRule.WAIT_TIMEOUT_MS;
import android.os.Looper;
import android.support.test.runner.lifecycle.ActivityLifecycleMonitor;
import android.support.test.runner.lifecycle.ActivityLifecycleMonitorRegistry;
import androidx.test.filters.MediumTest;
......@@ -22,6 +24,7 @@ import org.chromium.android_webview.common.crash.AwCrashReporterClient;
import org.chromium.base.ThreadUtils;
import org.chromium.base.test.util.Feature;
import java.lang.reflect.Field;
import java.util.concurrent.CountDownLatch;
/**
......@@ -68,11 +71,6 @@ public class AwUncaughtExceptionTest {
ThreadUtils.setUiThread(mLooper);
notifyAll();
}
try {
mActivityTestRule.createAwBrowserContext();
mActivityTestRule.startBrowserProcess();
} catch (Exception e) {
}
try {
Looper.loop();
} finally {
......@@ -99,14 +97,26 @@ public class AwUncaughtExceptionTest {
private AwContents mAwContents;
private Thread.UncaughtExceptionHandler mDefaultUncaughtExceptionHandler;
// Since this test overrides the UI thread, Android's ActivityLifecycleMonitor assertions fail
// as our UI thread isn't the Main Looper thread, so we have to disable them.
private void disableLifecycleThreadAssertion() throws Exception {
ActivityLifecycleMonitor monitor = ActivityLifecycleMonitorRegistry.getInstance();
Field declawThreadCheck = monitor.getClass().getDeclaredField("mDeclawThreadCheck");
declawThreadCheck.setAccessible(true);
declawThreadCheck.set(monitor, true);
}
@Before
public void setUp() {
public void setUp() throws Exception {
disableLifecycleThreadAssertion();
mDefaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
mBackgroundThread = new BackgroundThread("background");
mBackgroundThread.start();
// Once the background thread looper exists, it has been
// designated as the main thread.
mBackgroundThread.getLooper();
mActivityTestRule.createAwBrowserContext();
mActivityTestRule.startBrowserProcess();
}
@After
......@@ -116,6 +126,7 @@ public class AwUncaughtExceptionTest {
backgroundThreadLooper.quitSafely();
}
mBackgroundThread.join();
ThreadUtils.setUiThread(null);
Thread.setDefaultUncaughtExceptionHandler(mDefaultUncaughtExceptionHandler);
}
......
......@@ -31,7 +31,6 @@ import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.SystemClock;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
......@@ -60,6 +59,8 @@ import org.chromium.android_webview.devui.MainActivity;
import org.chromium.android_webview.devui.R;
import org.chromium.android_webview.services.DeveloperUiService;
import org.chromium.android_webview.test.AwJUnit4ClassRunner;
import org.chromium.base.ContextUtils;
import org.chromium.base.test.BaseActivityTestRule;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.base.test.util.DisabledTest;
import org.chromium.base.test.util.Feature;
......@@ -76,12 +77,11 @@ import java.util.Map;
@RunWith(AwJUnit4ClassRunner.class)
public class FlagsFragmentTest {
@Rule
public ActivityTestRule mRule =
new ActivityTestRule<MainActivity>(MainActivity.class, false, false);
public BaseActivityTestRule mRule = new BaseActivityTestRule<MainActivity>(MainActivity.class);
@Before
public void setUp() throws Exception {
Intent intent = new Intent();
Intent intent = new Intent(ContextUtils.getApplicationContext(), MainActivity.class);
intent.putExtra(MainActivity.FRAGMENT_ID_INTENT_EXTRA, MainActivity.FRAGMENT_ID_FLAGS);
mRule.launchActivity(intent);
}
......
......@@ -19,7 +19,6 @@ import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
import android.content.Intent;
import android.os.Build;
import android.support.test.rule.ActivityTestRule;
import android.webkit.WebView;
import androidx.test.espresso.BaseLayerComponent;
......@@ -28,6 +27,8 @@ import androidx.test.espresso.DaggerBaseLayerComponent;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.chromium.base.ThreadUtils;
import org.chromium.base.test.BaseActivityTestRule;
import org.chromium.webview_ui_test.R;
import org.chromium.webview_ui_test.WebViewUiTestActivity;
......@@ -39,7 +40,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
* Note that this must be run on test thread.
*
*/
public class WebViewUiTestRule extends ActivityTestRule<WebViewUiTestActivity> {
public class WebViewUiTestRule extends BaseActivityTestRule<WebViewUiTestActivity> {
private static final long ACTION_BAR_POPUP_TIMEOUT = scaleTimeout(5000L);
private static final long ACTION_BAR_CHECK_INTERVAL = 200L;
......@@ -51,12 +52,6 @@ public class WebViewUiTestRule extends ActivityTestRule<WebViewUiTestActivity> {
super(activityClass);
}
@Override
protected void afterActivityLaunched() {
mSyncWrapper = new WebViewSyncWrapper((WebView) getActivity().findViewById(R.id.webview));
super.afterActivityLaunched();
}
@Override
public Statement apply(Statement base, Description desc) {
UseLayout a = desc.getAnnotation(UseLayout.class);
......@@ -67,15 +62,16 @@ public class WebViewUiTestRule extends ActivityTestRule<WebViewUiTestActivity> {
}
@Override
public WebViewUiTestActivity launchActivity(Intent i) {
public void launchActivity(Intent i) {
if (mLayout != null && !mLayout.isEmpty()) {
i.putExtra(WebViewUiTestActivity.EXTRA_TEST_LAYOUT_FILE, mLayout);
}
return super.launchActivity(i);
super.launchActivity(i);
mSyncWrapper = new WebViewSyncWrapper((WebView) getActivity().findViewById(R.id.webview));
}
public WebViewUiTestActivity launchActivity() {
return launchActivity(new Intent());
public void launchActivity() {
launchActivity(null);
}
public void loadDataSync(
......@@ -115,12 +111,7 @@ public class WebViewUiTestRule extends ActivityTestRule<WebViewUiTestActivity> {
public boolean isActionBarDisplayed() {
final AtomicBoolean isDisplayed = new AtomicBoolean(false);
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
isDisplayed.set(isActionBarDisplayedFunc());
}
});
ThreadUtils.runOnUiThreadBlocking(() -> isDisplayed.set(isActionBarDisplayedFunc()));
} catch (Throwable e) {
throw new RuntimeException("Exception while checking action bar", e);
}
......
......@@ -8,7 +8,6 @@ import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
import android.os.Handler;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;
import android.webkit.WebView;
import androidx.test.filters.MediumTest;
......@@ -19,6 +18,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.test.BaseActivityTestRule;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.FlakyTest;
import org.chromium.webview_shell.WebPlatformTestsActivity;
......@@ -64,11 +64,12 @@ public class WebPlatformTestsActivityTest {
private WebPlatformTestsActivity mTestActivity;
@Rule
public ActivityTestRule<WebPlatformTestsActivity> mActivityTestRule =
new ActivityTestRule<>(WebPlatformTestsActivity.class, false, true);
public BaseActivityTestRule<WebPlatformTestsActivity> mActivityTestRule =
new BaseActivityTestRule<>(WebPlatformTestsActivity.class);
@Before
public void setUp() {
mActivityTestRule.launchActivity(null);
mTestActivity = mActivityTestRule.getActivity();
}
......@@ -164,4 +165,4 @@ public class WebPlatformTestsActivityTest {
if (element == null) throw new TimeoutException("Timeout while asserting: " + msg);
Assert.assertEquals(msg, Integer.valueOf(expected), element);
}
}
\ No newline at end of file
}
......@@ -4,10 +4,8 @@
package org.chromium.webview_shell.test;
import android.content.Intent;
import android.os.Bundle;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;
import androidx.test.filters.MediumTest;
......@@ -21,6 +19,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.Log;
import org.chromium.base.test.BaseActivityTestRule;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.DisabledTest;
......@@ -79,12 +78,13 @@ public class WebViewLayoutTest {
private boolean mRebaseLine;
@Rule
public ActivityTestRule<WebViewLayoutTestActivity> mActivityTestRule =
new ActivityTestRule<>(WebViewLayoutTestActivity.class, false, false);
public BaseActivityTestRule<WebViewLayoutTestActivity> mActivityTestRule =
new BaseActivityTestRule<>(WebViewLayoutTestActivity.class);
@Before
public void setUp() {
mTestActivity = mActivityTestRule.launchActivity(new Intent());
mActivityTestRule.launchActivity(null);
mTestActivity = mActivityTestRule.getActivity();
Bundle arguments = InstrumentationRegistry.getArguments();
if (arguments != null) {
String modeArgument = arguments.getString("mode");
......
......@@ -6,8 +6,6 @@ package org.chromium.webview_shell.test;
import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
import android.content.Intent;
import android.support.test.rule.ActivityTestRule;
import android.webkit.CookieManager;
import android.webkit.GeolocationPermissions;
import android.webkit.WebStorage;
......@@ -21,6 +19,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.test.BaseActivityTestRule;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.webview_shell.WebViewThreadTestActivity;
......@@ -36,12 +35,13 @@ public class WebViewThreadTest {
private WebViewThreadTestActivity mActivity;
@Rule
public ActivityTestRule<WebViewThreadTestActivity> mActivityTestRule =
new ActivityTestRule<>(WebViewThreadTestActivity.class, false, false);
public BaseActivityTestRule<WebViewThreadTestActivity> mActivityTestRule =
new BaseActivityTestRule<>(WebViewThreadTestActivity.class);
@Before
public void setUp() {
mActivity = mActivityTestRule.launchActivity(new Intent());
mActivityTestRule.launchActivity(null);
mActivity = mActivityTestRule.getActivity();
}
@After
......
......@@ -5,7 +5,6 @@
package org.chromium.webview_shell.page_cycler;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
......@@ -18,6 +17,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.test.BaseActivityTestRule;
import org.chromium.base.test.params.BaseJUnit4RunnerDelegate;
import org.chromium.base.test.params.ParameterAnnotations.UseMethodParameter;
import org.chromium.base.test.params.ParameterAnnotations.UseRunnerDelegate;
......@@ -59,8 +59,8 @@ public class PageCyclerTest {
}
@Rule
public ActivityTestRule<PageCyclerTestActivity> mRule =
new ActivityTestRule<>(PageCyclerTestActivity.class);
public BaseActivityTestRule<PageCyclerTestActivity> mRule =
new BaseActivityTestRule<>(PageCyclerTestActivity.class);
@Before
public void setUp() {
......
......@@ -9,7 +9,8 @@ import android.content.Intent;
import android.support.test.runner.lifecycle.Stage;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.annotation.CallSuper;
import androidx.annotation.Nullable;
import org.junit.Assert;
import org.junit.rules.TestRule;
......@@ -41,6 +42,7 @@ public class BaseActivityTestRule<T extends Activity> implements TestRule {
}
@Override
@CallSuper
public Statement apply(final Statement base, final Description desc) {
return new Statement() {
@Override
......@@ -76,19 +78,31 @@ public class BaseActivityTestRule<T extends Activity> implements TestRule {
mActivity = activity;
}
protected Intent getActivityIntent() {
return new Intent(ContextUtils.getApplicationContext(), mActivityClass);
}
/**
* Launches the Activity under test using the provided intent.
* Launches the Activity under test using the provided intent. If the provided intent is null,
* an explicit intent targeting the Activity is created and used.
*/
public void launchActivity(@NonNull Intent startIntent) {
String packageName = ContextUtils.getApplicationContext().getPackageName();
Assert.assertTrue(TextUtils.equals(startIntent.getPackage(), packageName)
|| TextUtils.equals(startIntent.getComponent().getPackageName(), packageName));
public void launchActivity(@Nullable Intent startIntent) {
if (startIntent == null) {
startIntent = getActivityIntent();
} else {
String packageName = ContextUtils.getApplicationContext().getPackageName();
Assert.assertTrue(TextUtils.equals(startIntent.getPackage(), packageName)
|| (startIntent.getComponent() != null
&& TextUtils.equals(
startIntent.getComponent().getPackageName(), packageName)));
}
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Log.d(TAG, String.format("Launching activity %s", mActivityClass.getName()));
final Intent intent = startIntent;
mActivity = ApplicationTestUtils.waitForActivityWithClass(mActivityClass, Stage.CREATED,
() -> ContextUtils.getApplicationContext().startActivity(startIntent));
() -> ContextUtils.getApplicationContext().startActivity(intent));
}
}
......@@ -99,6 +99,7 @@ public class ApplicationTestUtils {
*/
public static <T extends Activity> T waitForActivityWithClass(
Class<? extends Activity> activityClass, Stage stage, Runnable trigger) {
ThreadUtils.assertOnBackgroundThread();
final CallbackHelper activityCallback = new CallbackHelper();
final AtomicReference<T> activityRef = new AtomicReference<>();
ActivityLifecycleCallback stateListener = (Activity newActivity, Stage newStage) -> {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment