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