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