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 "base/android/jni_android.h"
6 #include "base/android/scoped_java_ref.h"
7 #include "base/at_exit.h"
8 #include "base/bind.h"
9 #include "base/location.h"
10 #include "base/logging.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/test/test_support_android.h"
15 #include "base/threading/thread_task_runner_handle.h"
16 #include "jni/MojoTestRule_jni.h"
17 #include "mojo/core/embedder/embedder.h"
18
19 using base::android::JavaParamRef;
20
21 namespace {
22
23 struct TestEnvironment {
TestEnvironment__anonddd1e2f60111::TestEnvironment24 TestEnvironment() {}
25
26 base::ShadowingAtExitManager at_exit;
27 base::MessageLoop message_loop;
28 };
29
30 } // namespace
31
32 namespace mojo {
33 namespace android {
34
JNI_MojoTestRule_InitCore(JNIEnv * env,const JavaParamRef<jclass> & clazz)35 static void JNI_MojoTestRule_InitCore(JNIEnv* env,
36 const JavaParamRef<jclass>& clazz) {
37 mojo::core::Init();
38 }
39
JNI_MojoTestRule_Init(JNIEnv * env,const JavaParamRef<jobject> & jcaller)40 static void JNI_MojoTestRule_Init(JNIEnv* env,
41 const JavaParamRef<jobject>& jcaller) {
42 base::InitAndroidTestMessageLoop();
43 }
44
JNI_MojoTestRule_SetupTestEnvironment(JNIEnv * env,const JavaParamRef<jobject> & jcaller)45 static jlong JNI_MojoTestRule_SetupTestEnvironment(
46 JNIEnv* env,
47 const JavaParamRef<jobject>& jcaller) {
48 return reinterpret_cast<intptr_t>(new TestEnvironment());
49 }
50
JNI_MojoTestRule_TearDownTestEnvironment(JNIEnv * env,const JavaParamRef<jobject> & jcaller,jlong test_environment)51 static void JNI_MojoTestRule_TearDownTestEnvironment(
52 JNIEnv* env,
53 const JavaParamRef<jobject>& jcaller,
54 jlong test_environment) {
55 delete reinterpret_cast<TestEnvironment*>(test_environment);
56 }
57
JNI_MojoTestRule_RunLoop(JNIEnv * env,const JavaParamRef<jobject> & jcaller,jlong timeout_ms)58 static void JNI_MojoTestRule_RunLoop(JNIEnv* env,
59 const JavaParamRef<jobject>& jcaller,
60 jlong timeout_ms) {
61 base::RunLoop run_loop;
62 if (timeout_ms) {
63 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
64 FROM_HERE, run_loop.QuitWhenIdleClosure(),
65 base::TimeDelta::FromMilliseconds(timeout_ms));
66 run_loop.Run();
67 } else {
68 run_loop.RunUntilIdle();
69 }
70 }
71
72 } // namespace android
73 } // namespace mojo
74