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 #include "mojo/message_pump/message_pump_mojo.h" 20 21 namespace { 22 23 struct TestEnvironment { 24 TestEnvironment() : message_loop(mojo::common::MessagePumpMojo::Create()) {} 25 26 base::ShadowingAtExitManager at_exit; 27 base::MessageLoop message_loop; 28 }; 29 30 } // namespace 31 32 namespace mojo { 33 namespace android { 34 35 static void Init(JNIEnv* env, const JavaParamRef<jobject>& jcaller) { 36 base::InitAndroidTestMessageLoop(); 37 } 38 39 static jlong SetupTestEnvironment(JNIEnv* env, 40 const JavaParamRef<jobject>& jcaller) { 41 return reinterpret_cast<intptr_t>(new TestEnvironment()); 42 } 43 44 static void TearDownTestEnvironment(JNIEnv* env, 45 const JavaParamRef<jobject>& jcaller, 46 jlong test_environment) { 47 delete reinterpret_cast<TestEnvironment*>(test_environment); 48 } 49 50 static void RunLoop(JNIEnv* env, 51 const JavaParamRef<jobject>& jcaller, 52 jlong timeout_ms) { 53 base::RunLoop run_loop; 54 if (timeout_ms) { 55 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( 56 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(), 57 base::TimeDelta::FromMilliseconds(timeout_ms)); 58 run_loop.Run(); 59 } else { 60 run_loop.RunUntilIdle(); 61 } 62 } 63 64 bool RegisterMojoTestCase(JNIEnv* env) { 65 return RegisterNativesImpl(env); 66 } 67 68 } // namespace android 69 } // namespace mojo 70