Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(153)

Unified Diff: android_webview/tools/automated_ui_tests/apk/javatests/src/org/chromium/webview_ui_test/test/WebViewUiInstrumentationTestRunner.java

Issue 2146713003: Refactor and add special test runner to support Espresso tests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: android_webview/tools/automated_ui_tests/apk/javatests/src/org/chromium/webview_ui_test/test/WebViewUiInstrumentationTestRunner.java
diff --git a/android_webview/tools/automated_ui_tests/apk/javatests/src/org/chromium/webview_ui_test/test/WebViewUiInstrumentationTestRunner.java b/android_webview/tools/automated_ui_tests/apk/javatests/src/org/chromium/webview_ui_test/test/WebViewUiInstrumentationTestRunner.java
new file mode 100644
index 0000000000000000000000000000000000000000..9385f59bf77cc362a633172adebb06a8291d4cb6
--- /dev/null
+++ b/android_webview/tools/automated_ui_tests/apk/javatests/src/org/chromium/webview_ui_test/test/WebViewUiInstrumentationTestRunner.java
@@ -0,0 +1,163 @@
+// Copyright 2016 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.webview_ui_test.test;
+
+import android.app.Activity;
+import android.app.Application;
+import android.os.Bundle;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.internal.runner.lifecycle.ActivityLifecycleMonitorImpl;
+import android.support.test.internal.runner.lifecycle.ApplicationLifecycleMonitorImpl;
+import android.support.test.runner.lifecycle.ActivityLifecycleMonitorRegistry;
+import android.support.test.runner.lifecycle.ApplicationLifecycleMonitorRegistry;
+import android.support.test.runner.lifecycle.ApplicationStage;
+import android.support.test.runner.lifecycle.Stage;
+
+import org.chromium.base.Log;
+import org.chromium.base.test.BaseInstrumentationTestRunner;
+
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * Instrumentation Test Runner that keeps track of activity state to support Espresso tests
+ */
+public class WebViewUiInstrumentationTestRunner extends BaseInstrumentationTestRunner{
+
+ private static final long MILLIS_TO_WAIT_FOR_ACTIVITY_TO_STOP = TimeUnit.SECONDS.toMillis(2);
jbudorick 2016/07/13 18:39:20 nit: ACTIVITY_STOP_WAIT_INTERVAL_MS
the real yoland 2016/07/14 03:10:00 Done
+ private static final long MILLIS_TO_POLL_FOR_ACTIVITY_STOP =
jbudorick 2016/07/13 18:39:19 nit: ACTIVITY_STOP_POLL_INTERVAL_MS
the real yoland 2016/07/14 03:09:59 Done
+ MILLIS_TO_WAIT_FOR_ACTIVITY_TO_STOP / 40;
+
+ private static final String TAG = "WebViewUiITR";
+
+ private ActivityLifecycleMonitorImpl mLifecycleMonitor = new ActivityLifecycleMonitorImpl();
+ private ApplicationLifecycleMonitorImpl mApplicationMonitor =
+ new ApplicationLifecycleMonitorImpl();
+ private AtomicInteger mStartedActivityCounter = new AtomicInteger(0);
+
+ @Override
+ public void onCreate(Bundle arguments) {
+ Log.i(TAG, "Instrumentation Started");
jbudorick 2016/07/13 18:39:19 What's the point of this log? (also, it's wrong;
the real yoland 2016/07/14 03:10:00 Done
+ logUncaughtExceptions();
+
+ InstrumentationRegistry.registerInstance(this, arguments);
+ ActivityLifecycleMonitorRegistry.registerInstance(mLifecycleMonitor);
+ ApplicationLifecycleMonitorRegistry.registerInstance(mApplicationMonitor);
+
+ super.onCreate(arguments);
+ }
+
+ private void logUncaughtExceptions() {
+ final Thread.UncaughtExceptionHandler standardHandler =
+ Thread.currentThread().getUncaughtExceptionHandler();
+ Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
+ @Override
+ public void uncaughtException(Thread t, Throwable e) {
+ onException(t, e);
jbudorick 2016/07/13 18:39:19 What is this? I don't see it defined anywhere, but
the real yoland 2016/07/14 03:10:00 Done
+ if (null != standardHandler) {
+ standardHandler.uncaughtException(t, e);
+ }
+ }
+ });
+ }
+
+
mikecase (-- gone --) 2016/07/13 19:38:20 nit: extra line
the real yoland 2016/07/14 03:10:00 Done
+ @Override
+ public void onStart() {
+ super.onStart();
+ waitForIdleSync();
+ }
+
+ @Override
+ public void finish(int resultCode, Bundle results) {
+ long startTime = System.currentTimeMillis();
+ waitForActivitiesToComplete();
+ long endTime = System.currentTimeMillis();
+ Log.i(TAG, String.format("waitForActivitiesToComplete() took: %sms", endTime - startTime));
jbudorick 2016/07/13 18:39:20 Is this here intentionally, or was it just here fo
the real yoland 2016/07/14 03:10:00 I think the idea of all these logging is to show t
+ ActivityLifecycleMonitorRegistry.registerInstance(null);
+ super.finish(resultCode, results);
+ }
+
+ protected void waitForActivitiesToComplete() {
+ long endTime = System.currentTimeMillis() + MILLIS_TO_WAIT_FOR_ACTIVITY_TO_STOP;
+ int currentActivityCount = mStartedActivityCounter.get();
+
+ while (currentActivityCount > 0 && System.currentTimeMillis() < endTime) {
jbudorick 2016/07/13 18:39:19 Can this use Criteria/CriteriaHelper? https://code
the real yoland 2016/07/14 03:10:00 Hmm, true, but pollInstrumentationThread from Crit
+ try {
+ Log.i(TAG, "Unstopped activity count: " + currentActivityCount);
+ Thread.sleep(MILLIS_TO_POLL_FOR_ACTIVITY_STOP);
+ currentActivityCount = mStartedActivityCounter.get();
+ } catch (InterruptedException ie) {
+ Log.i(TAG, "Abandoning activity wait due to interruption.", ie);
+ break;
+ }
+ }
+
+ if (currentActivityCount > 0) {
+ Log.w(TAG, String.format("Still %s activities active after waiting %s ms.",
+ currentActivityCount, MILLIS_TO_WAIT_FOR_ACTIVITY_TO_STOP));
+ }
+ }
+
+ @Override
+ public void callApplicationOnCreate(Application app) {
+ mApplicationMonitor.signalLifecycleChange(app, ApplicationStage.PRE_ON_CREATE);
+ super.callApplicationOnCreate(app);
+ mApplicationMonitor.signalLifecycleChange(app, ApplicationStage.CREATED);
+ }
+
+ @Override
+ public void callActivityOnDestroy(Activity activity) {
+ super.callActivityOnDestroy(activity);
+ mLifecycleMonitor.signalLifecycleChange(Stage.DESTROYED, activity);
+ }
+
+ @Override
+ public void callActivityOnRestart(Activity activity) {
+ super.callActivityOnRestart(activity);
+ mLifecycleMonitor.signalLifecycleChange(Stage.RESTARTED, activity);
+ }
+
+ @Override
+ public void callActivityOnCreate(Activity activity, Bundle bundle) {
+ mLifecycleMonitor.signalLifecycleChange(Stage.PRE_ON_CREATE, activity);
+ super.callActivityOnCreate(activity, bundle);
+ mLifecycleMonitor.signalLifecycleChange(Stage.CREATED, activity);
+ }
+
+ @Override
+ public void callActivityOnStart(Activity activity) {
+ mStartedActivityCounter.incrementAndGet();
+ try {
+ super.callActivityOnStart(activity);
+ mLifecycleMonitor.signalLifecycleChange(Stage.STARTED, activity);
+ } catch (RuntimeException re) {
+ mStartedActivityCounter.decrementAndGet();
+ throw re;
+ }
+ }
+
+ @Override
+ public void callActivityOnStop(Activity activity) {
+ try {
+ super.callActivityOnStop(activity);
+ mLifecycleMonitor.signalLifecycleChange(Stage.STOPPED, activity);
+ } finally {
+ mStartedActivityCounter.decrementAndGet();
+ }
+ }
+
+ @Override
+ public void callActivityOnResume(Activity activity) {
+ super.callActivityOnResume(activity);
+ mLifecycleMonitor.signalLifecycleChange(Stage.RESUMED, activity);
+ }
+
+ @Override
+ public void callActivityOnPause(Activity activity) {
+ super.callActivityOnPause(activity);
+ mLifecycleMonitor.signalLifecycleChange(Stage.PAUSED, activity);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698