Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / mojo / common / test / multiprocess_test_base.cc
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.
4
5 #include "mojo/common/test/multiprocess_test_base.h"
6
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"
13
14 namespace mojo {
15 namespace test {
16
17 MultiprocessTestBase::MultiprocessTestBase()
18     : test_child_handle_(base::kNullProcessHandle) {
19 }
20
21 MultiprocessTestBase::~MultiprocessTestBase() {
22   CHECK_EQ(test_child_handle_, base::kNullProcessHandle);
23 }
24
25 void MultiprocessTestBase::SetUp() {
26   CHECK_EQ(test_child_handle_, base::kNullProcessHandle);
27
28   MultiProcessTest::SetUp();
29
30   platform_channel_pair_.reset(new embedder::PlatformChannelPair());
31   server_platform_handle = platform_channel_pair_->PassServerHandle();
32 }
33
34 void MultiprocessTestBase::TearDown() {
35   CHECK_EQ(test_child_handle_, base::kNullProcessHandle);
36
37   server_platform_handle.reset();
38   platform_channel_pair_.reset();
39
40   MultiProcessTest::TearDown();
41 }
42
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);
47
48   std::string test_child_main = test_child_name + "TestChildMain";
49
50   CommandLine unused(CommandLine::NO_PROGRAM);
51   embedder::HandlePassingInformation handle_passing_info;
52   platform_channel_pair_->PrepareToPassClientHandleToChildProcess(
53       &unused, &handle_passing_info);
54
55   base::LaunchOptions options;
56 #if defined(OS_POSIX)
57   options.fds_to_remap = &handle_passing_info;
58 #elif defined(OS_WIN)
59   options.start_hidden = true;
60   options.handles_to_inherit = &handle_passing_info;
61 #else
62 #error "Not supported yet."
63 #endif
64
65   test_child_handle_ = SpawnChildWithOptions(test_child_main, options, false);
66   platform_channel_pair_->ChildProcessLaunched();
67
68   CHECK_NE(test_child_handle_, base::kNullProcessHandle);
69 }
70
71 int MultiprocessTestBase::WaitForChildShutdown() {
72   CHECK_NE(test_child_handle_, base::kNullProcessHandle);
73
74   static const int kTimeoutSeconds = 5;
75   int rv = -1;
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;
80   return rv;
81 }
82
83 CommandLine MultiprocessTestBase::MakeCmdLine(const std::string& procname,
84                                               bool debug_on_start) {
85   CHECK(platform_channel_pair_.get());
86
87   CommandLine command_line =
88       base::MultiProcessTest::MakeCmdLine(procname, debug_on_start);
89   embedder::HandlePassingInformation unused;
90   platform_channel_pair_->PrepareToPassClientHandleToChildProcess(&command_line,
91                                                                   &unused);
92
93   return command_line;
94 }
95
96 // static
97 void MultiprocessTestBase::ChildSetup() {
98   CHECK(CommandLine::InitializedForCurrentProcess());
99   client_platform_handle =
100       embedder::PlatformChannelPair::PassClientHandleFromParentProcess(
101           *CommandLine::ForCurrentProcess());
102 }
103
104 // static
105 embedder::ScopedPlatformHandle MultiprocessTestBase::client_platform_handle;
106
107 }  // namespace test
108 }  // namespace mojo