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

Unified Diff: base/test/android/java/src/org/chromium/base/TestUiThread.java

Issue 1180693002: Update from https://crrev.com/333737 (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: rebased Created 5 years, 6 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
« no previous file with comments | « base/test/BUILD.gn ('k') | base/test/gtest_xml_unittest_result_printer.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/test/android/java/src/org/chromium/base/TestUiThread.java
diff --git a/base/test/android/java/src/org/chromium/base/TestUiThread.java b/base/test/android/java/src/org/chromium/base/TestUiThread.java
new file mode 100644
index 0000000000000000000000000000000000000000..77f96606ad6dbddec88ef9e46cda7996e7934f2e
--- /dev/null
+++ b/base/test/android/java/src/org/chromium/base/TestUiThread.java
@@ -0,0 +1,49 @@
+// Copyright 2015 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.base;
+
+import android.os.Looper;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import javax.annotation.concurrent.ThreadSafe;
+
+/**
+ * Set up a thread as the Chromium UI Thread, and run its looper. This is is intended for C++ unit
+ * tests (e.g. the net unit tests) that don't run with the UI thread as their main looper, but test
+ * code that, on Android, uses UI thread events, so need a running UI thread.
+ */
+@ThreadSafe
+public class TestUiThread {
+ private static final AtomicBoolean sStarted = new AtomicBoolean(false);
+ private static final String TAG = Log.makeTag("TestUiThread");
+
+ @CalledByNative
+ private static void loop() {
+ // @{link ThreadUtils#setUiThread(Looper)} can only be called once in a test run, so do this
+ // once, and leave it running.
+ if (sStarted.getAndSet(true)) return;
+
+ final CountDownLatch startLatch = new CountDownLatch(1);
+ new Thread(new Runnable() {
+
+ @Override
+ public void run() {
+ Looper.prepare();
+ ThreadUtils.setUiThread(Looper.myLooper());
+ startLatch.countDown();
+ Looper.loop();
+ }
+
+ }).start();
+
+ try {
+ startLatch.await();
+ } catch (InterruptedException e) {
+ Log.e(TAG, "Failed to set UI Thread");
+ }
+ }
+}
« no previous file with comments | « base/test/BUILD.gn ('k') | base/test/gtest_xml_unittest_result_printer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698