multiprocess_test_android.cc 3.3 KB

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