tuple: update to make use of C++11
[chromium-blink-merge.git] / mojo / edk / test / multiprocess_test_helper.cc
blob758520a68157ce4640036848a12e68984a9dbe40
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/edk/test/multiprocess_test_helper.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/edk/embedder/platform_channel_pair.h"
14 namespace mojo {
15 namespace test {
17 MultiprocessTestHelper::MultiprocessTestHelper()
18 : test_child_handle_(base::kNullProcessHandle) {
19 platform_channel_pair_.reset(new embedder::PlatformChannelPair());
20 server_platform_handle = platform_channel_pair_->PassServerHandle();
23 MultiprocessTestHelper::~MultiprocessTestHelper() {
24 CHECK_EQ(test_child_handle_, base::kNullProcessHandle);
25 server_platform_handle.reset();
26 platform_channel_pair_.reset();
29 void MultiprocessTestHelper::StartChild(const std::string& test_child_name) {
30 CHECK(platform_channel_pair_);
31 CHECK(!test_child_name.empty());
32 CHECK_EQ(test_child_handle_, base::kNullProcessHandle);
34 std::string test_child_main = test_child_name + "TestChildMain";
36 base::CommandLine command_line(
37 base::GetMultiProcessTestChildBaseCommandLine());
38 embedder::HandlePassingInformation handle_passing_info;
39 platform_channel_pair_->PrepareToPassClientHandleToChildProcess(
40 &command_line, &handle_passing_info);
42 base::LaunchOptions options;
43 #if defined(OS_POSIX)
44 options.fds_to_remap = &handle_passing_info;
45 #elif defined(OS_WIN)
46 options.start_hidden = true;
47 options.handles_to_inherit = &handle_passing_info;
48 #else
49 #error "Not supported yet."
50 #endif
52 test_child_handle_ =
53 base::SpawnMultiProcessTestChild(test_child_main, command_line, options);
54 platform_channel_pair_->ChildProcessLaunched();
56 CHECK_NE(test_child_handle_, base::kNullProcessHandle);
59 int MultiprocessTestHelper::WaitForChildShutdown() {
60 CHECK_NE(test_child_handle_, base::kNullProcessHandle);
62 int rv = -1;
63 CHECK(base::WaitForExitCodeWithTimeout(test_child_handle_, &rv,
64 TestTimeouts::action_timeout()));
65 base::CloseProcessHandle(test_child_handle_);
66 test_child_handle_ = base::kNullProcessHandle;
67 return rv;
70 bool MultiprocessTestHelper::WaitForChildTestShutdown() {
71 return WaitForChildShutdown() == 0;
74 // static
75 void MultiprocessTestHelper::ChildSetup() {
76 CHECK(base::CommandLine::InitializedForCurrentProcess());
77 client_platform_handle =
78 embedder::PlatformChannelPair::PassClientHandleFromParentProcess(
79 *base::CommandLine::ForCurrentProcess());
82 // static
83 embedder::ScopedPlatformHandle MultiprocessTestHelper::client_platform_handle;
85 } // namespace test
86 } // namespace mojo