Commit 91ea3f95 authored by aberent's avatar aberent Committed by Commit bot

Provide dummy Android UI thread with looper for C++ unit tests

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.

Review URL: https://codereview.chromium.org/1162763004

Cr-Commit-Position: refs/heads/master@{#333734}
parent e840735a
......@@ -992,6 +992,8 @@
'test/test_switches.h',
'test/test_timeouts.cc',
'test/test_timeouts.h',
'test/test_ui_thread_android.cc',
'test/test_ui_thread_android.h',
'test/thread_test_helper.cc',
'test/thread_test_helper.h',
'test/trace_event_analyzer.cc',
......@@ -1409,6 +1411,7 @@
'type': 'none',
'sources': [
'test/android/java/src/org/chromium/base/ContentUriTestUtils.java',
'test/android/java/src/org/chromium/base/TestUiThread.java',
],
'variables': {
'jni_gen_package': 'base',
......
......@@ -114,6 +114,8 @@ source_set("test_support") {
"test_support_android.h",
"test_support_ios.h",
"test_support_ios.mm",
"test_ui_thread_android.cc",
"test_ui_thread_android.h",
"thread_test_helper.cc",
"thread_test_helper.h",
"trace_event_analyzer.cc",
......@@ -205,6 +207,7 @@ if (is_android) {
generate_jni("base_unittests_jni_headers") {
sources = [
"android/java/src/org/chromium/base/ContentUriTestUtils.java",
"android/java/src/org/chromium/base/TestUiThread.java",
]
jni_package = "base"
}
......
// 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");
}
}
}
// 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.
#include "base/test/test_ui_thread_android.h"
#include "jni/TestUiThread_jni.h"
namespace base {
void StartTestUiThreadLooper() {
Java_TestUiThread_loop(base::android::AttachCurrentThread());
}
bool RegisterTestUiThreadAndroid(JNIEnv* env) {
return RegisterNativesImpl(env);
}
} // namespace base
// 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.
#ifndef BASE_TEST_TEST_UI_THREAD_ANDROID_
#define BASE_TEST_TEST_UI_THREAD_ANDROID_
#include <jni.h>
namespace base {
// 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.
void StartTestUiThreadLooper();
bool RegisterTestUiThreadAndroid(JNIEnv* env);
} // namespace base
#endif // BASE_TEST_TEST_UI_THREAD_ANDROID_
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