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

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

Issue 1162763004: Provide dummy Android UI thread with looper for C++ unit tests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add BUILD.gn changes 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
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..fc84de410e91526beb46be4bb3fea558b6f20763
--- /dev/null
+++ b/base/test/android/java/src/org/chromium/base/TestUiThread.java
@@ -0,0 +1,46 @@
+// 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;
+
+/**
+ * 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.
nyquist 2015/06/08 23:38:19 Nit: UI*
aberent 2015/06/09 10:33:12 Done.
+ */
+public class TestUiThread {
+ static boolean sStarted = false;
nyquist 2015/06/08 23:38:19 Nit: private
aberent 2015/06/09 10:33:13 Done.
+
+ @CalledByNative
+ private static void loop() {
+ // setUiThread can only be called once in a test run, so do this once, and leave it running.
nyquist 2015/06/08 23:38:19 Nit: @{link ThreadUtils#setUiThread(Looper)}
+ if (sStarted) return;
nyquist 2015/06/08 23:38:19 It's not clear to me that we are guaranteed to get
aberent 2015/06/09 10:33:12 Done.
+
+ 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();
+
+ sStarted = true;
+
+ try {
+ startLatch.await();
+ } catch (InterruptedException e) {
+ // TODO(aberent): Auto-generated catch block
nyquist 2015/06/08 23:38:19 Nit: Could we log an error message here instead of
aberent 2015/06/09 10:33:12 Done.
+ e.printStackTrace();
+ }
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698