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