Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / mojo / android / javatests / mojo_test_case.cc
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/common/message_pump_mojo.h"
17
18 #include "mojo/public/cpp/environment/environment.h"
19
20 namespace {
21
22 struct TestEnvironment {
23   TestEnvironment() : message_loop(mojo::common::MessagePumpMojo::Create()) {}
24
25   base::ShadowingAtExitManager at_exit;
26   base::MessageLoop message_loop;
27 };
28
29 }  // namespace
30
31 namespace mojo {
32 namespace android {
33
34 static void InitApplicationContext(JNIEnv* env,
35                                    jobject jcaller,
36                                    jobject context) {
37   base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context);
38   base::android::InitApplicationContext(env, scoped_context);
39   base::InitAndroidTestMessageLoop();
40 }
41
42 static jlong SetupTestEnvironment(JNIEnv* env, jobject jcaller) {
43   return reinterpret_cast<intptr_t>(new TestEnvironment());
44 }
45
46 static void TearDownTestEnvironment(JNIEnv* env,
47                                     jobject jcaller,
48                                     jlong test_environment) {
49   delete reinterpret_cast<TestEnvironment*>(test_environment);
50 }
51
52 static void RunLoop(JNIEnv* env, jobject jcaller, jlong timeout_ms) {
53   base::RunLoop run_loop;
54   if (timeout_ms) {
55     base::MessageLoop::current()->PostDelayedTask(
56         FROM_HERE,
57         base::MessageLoop::QuitClosure(),
58         base::TimeDelta::FromMilliseconds(timeout_ms));
59     run_loop.Run();
60   } else {
61     run_loop.RunUntilIdle();
62   }
63 }
64
65 bool RegisterMojoTestCase(JNIEnv* env) {
66   return RegisterNativesImpl(env);
67 }
68
69 }  // namespace android
70 }  // namespace mojo