1 // Copyright 2013 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.
5 #include "mojo/common/test/multiprocess_test_base.h"
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "base/process/kill.h"
10 #include "base/process/process_handle.h"
11 #include "build/build_config.h"
12 #include "mojo/system/embedder/platform_channel_pair.h"
17 MultiprocessTestBase::MultiprocessTestBase()
18 : test_child_handle_(base::kNullProcessHandle
) {
21 MultiprocessTestBase::~MultiprocessTestBase() {
22 CHECK_EQ(test_child_handle_
, base::kNullProcessHandle
);
25 void MultiprocessTestBase::SetUp() {
26 CHECK_EQ(test_child_handle_
, base::kNullProcessHandle
);
28 MultiProcessTest::SetUp();
30 platform_channel_pair_
.reset(new embedder::PlatformChannelPair());
31 server_platform_handle
= platform_channel_pair_
->PassServerHandle();
34 void MultiprocessTestBase::TearDown() {
35 CHECK_EQ(test_child_handle_
, base::kNullProcessHandle
);
37 server_platform_handle
.reset();
38 platform_channel_pair_
.reset();
40 MultiProcessTest::TearDown();
43 void MultiprocessTestBase::StartChild(const std::string
& test_child_name
) {
44 CHECK(platform_channel_pair_
.get());
45 CHECK(!test_child_name
.empty());
46 CHECK_EQ(test_child_handle_
, base::kNullProcessHandle
);
48 std::string test_child_main
= test_child_name
+ "TestChildMain";
50 CommandLine
unused(CommandLine::NO_PROGRAM
);
51 embedder::HandlePassingInformation handle_passing_info
;
52 platform_channel_pair_
->PrepareToPassClientHandleToChildProcess(
53 &unused
, &handle_passing_info
);
55 base::LaunchOptions options
;
57 options
.fds_to_remap
= &handle_passing_info
;
59 options
.start_hidden
= true;
60 options
.handles_to_inherit
= &handle_passing_info
;
62 #error "Not supported yet."
65 test_child_handle_
= SpawnChildWithOptions(test_child_main
, options
, false);
66 platform_channel_pair_
->ChildProcessLaunched();
68 CHECK_NE(test_child_handle_
, base::kNullProcessHandle
);
71 int MultiprocessTestBase::WaitForChildShutdown() {
72 CHECK_NE(test_child_handle_
, base::kNullProcessHandle
);
74 static const int kTimeoutSeconds
= 5;
76 CHECK(base::WaitForExitCodeWithTimeout(
77 test_child_handle_
, &rv
, base::TimeDelta::FromSeconds(kTimeoutSeconds
)));
78 base::CloseProcessHandle(test_child_handle_
);
79 test_child_handle_
= base::kNullProcessHandle
;
83 CommandLine
MultiprocessTestBase::MakeCmdLine(const std::string
& procname
,
84 bool debug_on_start
) {
85 CHECK(platform_channel_pair_
.get());
87 CommandLine command_line
=
88 base::MultiProcessTest::MakeCmdLine(procname
, debug_on_start
);
89 embedder::HandlePassingInformation unused
;
90 platform_channel_pair_
->PrepareToPassClientHandleToChildProcess(&command_line
,
97 void MultiprocessTestBase::ChildSetup() {
98 CHECK(CommandLine::InitializedForCurrentProcess());
99 client_platform_handle
=
100 embedder::PlatformChannelPair::PassClientHandleFromParentProcess(
101 *CommandLine::ForCurrentProcess());
105 embedder::ScopedPlatformHandle
MultiprocessTestBase::client_platform_handle
;