1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "mojo/android/javatests/mojo_test_case.h"
6
7 #include "base/android/jni_android.h"
8 #include "base/android/scoped_java_ref.h"
9 #include "base/at_exit.h"
10 #include "base/bind.h"
11 #include "base/location.h"
12 #include "base/logging.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/run_loop.h"
15 #include "base/single_thread_task_runner.h"
16 #include "base/test/test_support_android.h"
17 #include "base/threading/thread_task_runner_handle.h"
18 #include "jni/MojoTestCase_jni.h"
19
20 using base::android::JavaParamRef;
21
22 namespace {
23
24 struct TestEnvironment {
TestEnvironment__anon7a0c66640111::TestEnvironment25 TestEnvironment() {}
26
27 base::ShadowingAtExitManager at_exit;
28 base::MessageLoop message_loop;
29 };
30
31 } // namespace
32
33 namespace mojo {
34 namespace android {
35
Init(JNIEnv * env,const JavaParamRef<jobject> & jcaller)36 static void Init(JNIEnv* env, const JavaParamRef<jobject>& jcaller) {
37 base::InitAndroidTestMessageLoop();
38 }
39
SetupTestEnvironment(JNIEnv * env,const JavaParamRef<jobject> & jcaller)40 static jlong SetupTestEnvironment(JNIEnv* env,
41 const JavaParamRef<jobject>& jcaller) {
42 return reinterpret_cast<intptr_t>(new TestEnvironment());
43 }
44
TearDownTestEnvironment(JNIEnv * env,const JavaParamRef<jobject> & jcaller,jlong test_environment)45 static void TearDownTestEnvironment(JNIEnv* env,
46 const JavaParamRef<jobject>& jcaller,
47 jlong test_environment) {
48 delete reinterpret_cast<TestEnvironment*>(test_environment);
49 }
50
RunLoop(JNIEnv * env,const JavaParamRef<jobject> & jcaller,jlong timeout_ms)51 static void RunLoop(JNIEnv* env,
52 const JavaParamRef<jobject>& jcaller,
53 jlong timeout_ms) {
54 base::RunLoop run_loop;
55 if (timeout_ms) {
56 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
57 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(),
58 base::TimeDelta::FromMilliseconds(timeout_ms));
59 run_loop.Run();
60 } else {
61 run_loop.RunUntilIdle();
62 }
63 }
64
RegisterMojoTestCase(JNIEnv * env)65 bool RegisterMojoTestCase(JNIEnv* env) {
66 return RegisterNativesImpl(env);
67 }
68
69 } // namespace android
70 } // namespace mojo
71