| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.base; | 5 package org.chromium.base; |
| 6 | 6 |
| 7 import android.os.Looper; | 7 import android.os.Looper; |
| 8 | 8 |
| 9 import java.util.concurrent.CountDownLatch; | 9 import java.util.concurrent.CountDownLatch; |
| 10 import java.util.concurrent.atomic.AtomicBoolean; | 10 import java.util.concurrent.atomic.AtomicBoolean; |
| 11 | 11 |
| 12 import javax.annotation.concurrent.ThreadSafe; | 12 import javax.annotation.concurrent.ThreadSafe; |
| 13 | 13 |
| 14 /** | 14 /** |
| 15 * Set up a thread as the Chromium UI Thread, and run its looper. This is is int
ended for C++ unit | 15 * Set up a thread as the Chromium UI Thread, and run its looper. This is is int
ended for C++ unit |
| 16 * tests (e.g. the net unit tests) that don't run with the UI thread as their ma
in looper, but test | 16 * tests (e.g. the net unit tests) that don't run with the UI thread as their ma
in looper, but test |
| 17 * code that, on Android, uses UI thread events, so need a running UI thread. | 17 * code that, on Android, uses UI thread events, so need a running UI thread. |
| 18 */ | 18 */ |
| 19 @ThreadSafe | 19 @ThreadSafe |
| 20 public class TestUiThread { | 20 public class TestUiThread { |
| 21 private static final AtomicBoolean sStarted = new AtomicBoolean(false); | 21 private static final AtomicBoolean sStarted = new AtomicBoolean(false); |
| 22 private static final String TAG = Log.makeTag("TestUiThread"); | 22 private static final String TAG = "cr.TestUiThread"; |
| 23 | 23 |
| 24 @CalledByNative | 24 @CalledByNative |
| 25 private static void loop() { | 25 private static void loop() { |
| 26 // @{link ThreadUtils#setUiThread(Looper)} can only be called once in a
test run, so do this | 26 // @{link ThreadUtils#setUiThread(Looper)} can only be called once in a
test run, so do this |
| 27 // once, and leave it running. | 27 // once, and leave it running. |
| 28 if (sStarted.getAndSet(true)) return; | 28 if (sStarted.getAndSet(true)) return; |
| 29 | 29 |
| 30 final CountDownLatch startLatch = new CountDownLatch(1); | 30 final CountDownLatch startLatch = new CountDownLatch(1); |
| 31 new Thread(new Runnable() { | 31 new Thread(new Runnable() { |
| 32 | 32 |
| 33 @Override | 33 @Override |
| 34 public void run() { | 34 public void run() { |
| 35 Looper.prepare(); | 35 Looper.prepare(); |
| 36 ThreadUtils.setUiThread(Looper.myLooper()); | 36 ThreadUtils.setUiThread(Looper.myLooper()); |
| 37 startLatch.countDown(); | 37 startLatch.countDown(); |
| 38 Looper.loop(); | 38 Looper.loop(); |
| 39 } | 39 } |
| 40 | 40 |
| 41 }).start(); | 41 }).start(); |
| 42 | 42 |
| 43 try { | 43 try { |
| 44 startLatch.await(); | 44 startLatch.await(); |
| 45 } catch (InterruptedException e) { | 45 } catch (InterruptedException e) { |
| 46 Log.e(TAG, "Failed to set UI Thread"); | 46 Log.e(TAG, "Failed to set UI Thread"); |
| 47 } | 47 } |
| 48 } | 48 } |
| 49 } | 49 } |
| OLD | NEW |