Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / content / shell / browser / shell_mojo_test_utils_android.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 "content/shell/browser/shell_mojo_test_utils_android.h"
6
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"
13
14 namespace {
15
16 struct TestEnvironment {
17   base::MessageLoop message_loop;
18   ScopedVector<content::ServiceRegistryImpl> registries;
19   ScopedVector<content::ServiceRegistryAndroid> wrappers;
20 };
21
22 }  // namespace
23
24 namespace content {
25
26 static jlong SetupTestEnvironment(JNIEnv* env, jclass jcaller) {
27   return reinterpret_cast<intptr_t>(new TestEnvironment());
28 }
29
30 static void TearDownTestEnvironment(JNIEnv* env,
31                                     jclass jcaller,
32                                     jlong test_environment) {
33   delete reinterpret_cast<TestEnvironment*>(test_environment);
34 }
35
36 static jobject CreateServiceRegistryPair(JNIEnv* env,
37                                          jclass jcaller,
38                                          jlong native_test_environment) {
39   TestEnvironment* test_environment =
40       reinterpret_cast<TestEnvironment*>(native_test_environment);
41
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);
46
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());
52
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);
59
60   return Java_ShellMojoTestUtils_makePair(env,
61                                           wrapper_a->GetObjForTesting().obj(),
62                                           wrapper_b->GetObjForTesting().obj())
63       .Release();
64 }
65
66 static void RunLoop(JNIEnv* env, jclass jcaller, jlong timeout_ms) {
67   base::MessageLoop::current()->PostDelayedTask(
68       FROM_HERE,
69       base::MessageLoop::QuitClosure(),
70       base::TimeDelta::FromMilliseconds(timeout_ms));
71   base::RunLoop run_loop;
72   run_loop.Run();
73 }
74
75 bool RegisterShellMojoTestUtils(JNIEnv* env) {
76   return RegisterNativesImpl(env);
77 }
78
79 }  // namespace content