1 // Copyright (c) 2012 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 "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/command_line.h"
15 #include "base/logging.h"
16 #include "jni/MainReturnCodeResult_jni.h"
17 #include "jni/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, base::android::ToJavaIntArray(env, fd_keys),
43 base::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 base::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 Java_MainReturnCodeResult_hasTimedOut(env, result_code)) {
68 return false;
69 }
70 if (exit_code) {
71 *exit_code = Java_MainReturnCodeResult_getReturnCode(env, result_code);
72 }
73 return true;
74 }
75
TerminateMultiProcessTestChild(const Process & process,int exit_code,bool wait)76 bool TerminateMultiProcessTestChild(const Process& process,
77 int exit_code,
78 bool wait) {
79 JNIEnv* env = android::AttachCurrentThread();
80 DCHECK(env);
81
82 return android::Java_MultiprocessTestClientLauncher_terminate(
83 env, process.Pid(), exit_code, wait);
84 }
85
86 } // namespace base
87