multiprocess_test.cc 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 "base/base_switches.h"
  6. #include "base/command_line.h"
  7. #include "base/files/file_path.h"
  8. #include "base/files/file_util.h"
  9. #include "base/threading/thread_restrictions.h"
  10. #include "build/build_config.h"
  11. namespace base {
  12. #if !BUILDFLAG(IS_ANDROID)
  13. Process SpawnMultiProcessTestChild(const std::string& procname,
  14. const CommandLine& base_command_line,
  15. const LaunchOptions& options) {
  16. CommandLine command_line(base_command_line);
  17. // TODO(viettrungluu): See comment above |MakeCmdLine()| in the header file.
  18. // This is a temporary hack, since |MakeCmdLine()| has to provide a full
  19. // command line.
  20. if (!command_line.HasSwitch(switches::kTestChildProcess))
  21. command_line.AppendSwitchASCII(switches::kTestChildProcess, procname);
  22. return LaunchProcess(command_line, options);
  23. }
  24. bool WaitForMultiprocessTestChildExit(const Process& process,
  25. TimeDelta timeout,
  26. int* exit_code) {
  27. return process.WaitForExitWithTimeout(timeout, exit_code);
  28. }
  29. bool TerminateMultiProcessTestChild(const Process& process,
  30. int exit_code,
  31. bool wait) {
  32. return process.Terminate(exit_code, wait);
  33. }
  34. #endif // !BUILDFLAG(IS_ANDROID)
  35. CommandLine GetMultiProcessTestChildBaseCommandLine() {
  36. base::ScopedAllowBlockingForTesting allow_blocking;
  37. CommandLine cmd_line = *CommandLine::ForCurrentProcess();
  38. cmd_line.SetProgram(MakeAbsoluteFilePath(cmd_line.GetProgram()));
  39. return cmd_line;
  40. }
  41. // MultiProcessTest ------------------------------------------------------------
  42. MultiProcessTest::MultiProcessTest() = default;
  43. Process MultiProcessTest::SpawnChild(const std::string& procname) {
  44. LaunchOptions options;
  45. #if BUILDFLAG(IS_WIN)
  46. options.start_hidden = true;
  47. #endif
  48. return SpawnChildWithOptions(procname, options);
  49. }
  50. Process MultiProcessTest::SpawnChildWithOptions(const std::string& procname,
  51. const LaunchOptions& options) {
  52. return SpawnMultiProcessTestChild(procname, MakeCmdLine(procname), options);
  53. }
  54. CommandLine MultiProcessTest::MakeCmdLine(const std::string& procname) {
  55. CommandLine command_line = GetMultiProcessTestChildBaseCommandLine();
  56. command_line.AppendSwitchASCII(switches::kTestChildProcess, procname);
  57. return command_line;
  58. }
  59. } // namespace base