Home | History | Annotate | Download | only in javatests
      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/logging.h"
     12 #include "base/message_loop/message_loop.h"
     13 #include "base/run_loop.h"
     14 #include "base/test/test_support_android.h"
     15 #include "jni/MojoTestCase_jni.h"
     16 #include "mojo/public/cpp/environment/environment.h"
     17 
     18 namespace {
     19 
     20 struct TestEnvironment {
     21   base::ShadowingAtExitManager at_exit;
     22   base::MessageLoopForUI message_loop;
     23 };
     24 
     25 }  // namespace
     26 
     27 namespace mojo {
     28 namespace android {
     29 
     30 static void InitApplicationContext(JNIEnv* env,
     31                                    jobject jcaller,
     32                                    jobject context) {
     33   base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context);
     34   base::android::InitApplicationContext(env, scoped_context);
     35   base::InitAndroidTestMessageLoop();
     36 }
     37 
     38 static jlong SetupTestEnvironment(JNIEnv* env, jobject jcaller) {
     39   return reinterpret_cast<intptr_t>(new TestEnvironment());
     40 }
     41 
     42 static void TearDownTestEnvironment(JNIEnv* env,
     43                                     jobject jcaller,
     44                                     jlong test_environment) {
     45   delete reinterpret_cast<TestEnvironment*>(test_environment);
     46 }
     47 
     48 static void RunLoop(JNIEnv* env, jobject jcaller, jlong timeout_ms) {
     49   base::MessageLoop::current()->PostDelayedTask(
     50       FROM_HERE,
     51       base::MessageLoop::QuitClosure(),
     52       base::TimeDelta::FromMilliseconds(timeout_ms));
     53   base::RunLoop run_loop;
     54   run_loop.Run();
     55 }
     56 
     57 bool RegisterMojoTestCase(JNIEnv* env) {
     58   return RegisterNativesImpl(env);
     59 }
     60 
     61 }  // namespace android
     62 }  // namespace mojo
     63