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