• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "build/build_config.h"
12 
13 namespace base {
14 
15 #if !defined(OS_ANDROID) && !defined(__ANDROID__) && !defined(__ANDROID_HOST__)
SpawnMultiProcessTestChild(const std::string & procname,const CommandLine & base_command_line,const LaunchOptions & options)16 SpawnChildResult SpawnMultiProcessTestChild(
17     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   SpawnChildResult result;
28   result.process = LaunchProcess(command_line, options);
29   return result;
30 }
31 
WaitForMultiprocessTestChildExit(const Process & process,TimeDelta timeout,int * exit_code)32 bool WaitForMultiprocessTestChildExit(const Process& process,
33                                       TimeDelta timeout,
34                                       int* exit_code) {
35   return process.WaitForExitWithTimeout(timeout, exit_code);
36 }
37 
TerminateMultiProcessTestChild(const Process & process,int exit_code,bool wait)38 bool TerminateMultiProcessTestChild(const Process& process,
39                                     int exit_code,
40                                     bool wait) {
41   return process.Terminate(exit_code, wait);
42 }
43 
44 #endif  // !OS_ANDROID && !__ANDROID__ && !__ANDROID_HOST__
45 
GetMultiProcessTestChildBaseCommandLine()46 CommandLine GetMultiProcessTestChildBaseCommandLine() {
47   CommandLine cmd_line = *CommandLine::ForCurrentProcess();
48   cmd_line.SetProgram(MakeAbsoluteFilePath(cmd_line.GetProgram()));
49   return cmd_line;
50 }
51 
52 // MultiProcessTest ------------------------------------------------------------
53 
MultiProcessTest()54 MultiProcessTest::MultiProcessTest() {
55 }
56 
57 // Don't compile on Arc++.
58 #if 0
59 SpawnChildResult MultiProcessTest::SpawnChild(const std::string& procname) {
60   LaunchOptions options;
61 #if defined(OS_WIN)
62   options.start_hidden = true;
63 #endif
64   return SpawnChildWithOptions(procname, options);
65 }
66 
67 SpawnChildResult MultiProcessTest::SpawnChildWithOptions(
68     const std::string& procname,
69     const LaunchOptions& options) {
70   return SpawnMultiProcessTestChild(procname, MakeCmdLine(procname), options);
71 }
72 #endif
73 
MakeCmdLine(const std::string & procname)74 CommandLine MultiProcessTest::MakeCmdLine(const std::string& procname) {
75   CommandLine command_line = GetMultiProcessTestChildBaseCommandLine();
76   command_line.AppendSwitchASCII(switches::kTestChildProcess, procname);
77   return command_line;
78 }
79 
80 }  // namespace base
81