• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
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 "base/test/multiprocess_test.h"
6 
7 #include <string.h>
8 
9 #include <vector>
10 
11 #include "base/android/binder_box.h"
12 #include "base/android/jni_android.h"
13 #include "base/android/jni_array.h"
14 #include "base/android/scoped_java_ref.h"
15 #include "base/base_switches.h"
16 #include "base/check.h"
17 #include "base/command_line.h"
18 
19 // Must come after all headers that specialize FromJniType() / ToJniType().
20 #include "base/test/test_support_jni_headers/MainReturnCodeResult_jni.h"
21 #include "base/test/test_support_jni_headers/MultiprocessTestClientLauncher_jni.h"
22 
23 namespace base {
24 
25 // A very basic implementation for Android. On Android tests can run in an APK
26 // and we don't have an executable to exec*. This implementation does the bare
27 // minimum to execute the method specified by procname (in the child process).
28 //  - All options except |fds_to_remap| are ignored.
29 //
30 // NOTE: This MUST NOT run on the main thread of the NativeTest application.
SpawnMultiProcessTestChild(const std::string & procname,const CommandLine & base_command_line,const LaunchOptions & options)31 Process SpawnMultiProcessTestChild(const std::string& procname,
32                                    const CommandLine& base_command_line,
33                                    const LaunchOptions& options) {
34   JNIEnv* env = android::AttachCurrentThread();
35   DCHECK(env);
36 
37   std::vector<int> fd_keys;
38   std::vector<int> fd_fds;
39   for (auto& iter : options.fds_to_remap) {
40     fd_keys.push_back(iter.second);
41     fd_fds.push_back(iter.first);
42   }
43 
44   android::ScopedJavaLocalRef<jobjectArray> fds =
45       android::Java_MultiprocessTestClientLauncher_makeFdInfoArray(
46           env, android::ToJavaIntArray(env, fd_keys),
47           android::ToJavaIntArray(env, fd_fds));
48 
49   CommandLine command_line(base_command_line);
50   if (!command_line.HasSwitch(switches::kTestChildProcess)) {
51     command_line.AppendSwitchASCII(switches::kTestChildProcess, procname);
52   }
53 
54   android::ScopedJavaLocalRef<jobjectArray> j_argv =
55       android::ToJavaArrayOfStrings(env, command_line.argv());
56 
57   jint pid = android::Java_MultiprocessTestClientLauncher_launchClient(
58       env, j_argv, fds, base::android::PackBinderBox(env, options.binders));
59   return Process(pid);
60 }
61 
WaitForMultiprocessTestChildExit(const Process & process,TimeDelta timeout,int * exit_code)62 bool WaitForMultiprocessTestChildExit(const Process& process,
63                                       TimeDelta timeout,
64                                       int* exit_code) {
65   JNIEnv* env = android::AttachCurrentThread();
66   DCHECK(env);
67 
68   android::ScopedJavaLocalRef<jobject> result_code =
69       android::Java_MultiprocessTestClientLauncher_waitForMainToReturn(
70           env, process.Pid(), static_cast<int32_t>(timeout.InMilliseconds()));
71   if (result_code.is_null() ||
72       android::Java_MainReturnCodeResult_hasTimedOut(env, result_code)) {
73     return false;
74   }
75   if (exit_code) {
76     *exit_code =
77         android::Java_MainReturnCodeResult_getReturnCode(env, result_code);
78   }
79   return true;
80 }
81 
TerminateMultiProcessTestChild(const Process & process,int exit_code,bool wait)82 bool TerminateMultiProcessTestChild(const Process& process,
83                                     int exit_code,
84                                     bool wait) {
85   JNIEnv* env = android::AttachCurrentThread();
86   DCHECK(env);
87 
88   return android::Java_MultiprocessTestClientLauncher_terminate(
89       env, process.Pid(), exit_code, wait);
90 }
91 
MultiProcessTestChildHasCleanExit(const Process & process)92 bool MultiProcessTestChildHasCleanExit(const Process& process) {
93   JNIEnv* env = android::AttachCurrentThread();
94   DCHECK(env);
95 
96   return android::Java_MultiprocessTestClientLauncher_hasCleanExit(
97       env, process.Pid());
98 }
99 
100 }  // namespace base
101