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.
5 #include "content/shell/browser/shell_mojo_test_utils_android.h"
7 #include "base/memory/scoped_vector.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "content/browser/mojo/service_registry_android.h"
11 #include "content/common/mojo/service_registry_impl.h"
12 #include "jni/ShellMojoTestUtils_jni.h"
16 struct TestEnvironment {
17 base::MessageLoop message_loop;
18 ScopedVector<content::ServiceRegistryImpl> registries;
19 ScopedVector<content::ServiceRegistryAndroid> wrappers;
26 static jlong SetupTestEnvironment(JNIEnv* env, jclass jcaller) {
27 return reinterpret_cast<intptr_t>(new TestEnvironment());
30 static void TearDownTestEnvironment(JNIEnv* env,
32 jlong test_environment) {
33 delete reinterpret_cast<TestEnvironment*>(test_environment);
36 static jobject CreateServiceRegistryPair(JNIEnv* env,
38 jlong native_test_environment) {
39 TestEnvironment* test_environment =
40 reinterpret_cast<TestEnvironment*>(native_test_environment);
42 content::ServiceRegistryImpl* registry_a = new ServiceRegistryImpl();
43 test_environment->registries.push_back(registry_a);
44 content::ServiceRegistryImpl* registry_b = new ServiceRegistryImpl();
45 test_environment->registries.push_back(registry_b);
47 mojo::ScopedMessagePipeHandle handle_a;
48 mojo::ScopedMessagePipeHandle handle_b;
49 mojo::CreateMessagePipe(NULL, &handle_a, &handle_b);
50 registry_a->BindRemoteServiceProvider(handle_a.Pass());
51 registry_b->BindRemoteServiceProvider(handle_b.Pass());
53 content::ServiceRegistryAndroid* wrapper_a =
54 new ServiceRegistryAndroid(registry_a);
55 test_environment->wrappers.push_back(wrapper_a);
56 content::ServiceRegistryAndroid* wrapper_b =
57 new ServiceRegistryAndroid(registry_b);
58 test_environment->wrappers.push_back(wrapper_b);
60 return Java_ShellMojoTestUtils_makePair(env,
61 wrapper_a->GetObjForTesting().obj(),
62 wrapper_b->GetObjForTesting().obj())
66 static void RunLoop(JNIEnv* env, jclass jcaller, jlong timeout_ms) {
67 base::MessageLoop::current()->PostDelayedTask(
69 base::MessageLoop::QuitClosure(),
70 base::TimeDelta::FromMilliseconds(timeout_ms));
71 base::RunLoop run_loop;
75 bool RegisterShellMojoTestUtils(JNIEnv* env) {
76 return RegisterNativesImpl(env);
79 } // namespace content