Fix build break
[chromium-blink-merge.git] / ipc / ipc_test_base.cc
blob2f739f14e426aa6740c24585887a5dffab776e80
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.
5 #include "build/build_config.h"
7 #include "ipc/ipc_test_base.h"
9 #include "base/command_line.h"
10 #include "base/debug/debug_on_start_win.h"
11 #include "base/threading/thread.h"
12 #include "base/time.h"
13 #include "ipc/ipc_descriptors.h"
14 #include "ipc/ipc_switches.h"
16 // static
17 std::string IPCTestBase::GetChannelName(const std::string& test_client_name) {
18 DCHECK(!test_client_name.empty());
19 return test_client_name + "__Channel";
22 IPCTestBase::IPCTestBase()
23 : client_process_(base::kNullProcessHandle) {
26 IPCTestBase::~IPCTestBase() {
29 void IPCTestBase::SetUp() {
30 MultiProcessTest::SetUp();
32 // Construct a fresh IO Message loop for the duration of each test.
33 DCHECK(!message_loop_.get());
34 message_loop_.reset(new MessageLoopForIO());
37 void IPCTestBase::TearDown() {
38 DCHECK(message_loop_.get());
39 message_loop_.reset();
40 MultiProcessTest::TearDown();
43 void IPCTestBase::Init(const std::string& test_client_name) {
44 DCHECK(!test_client_name.empty());
45 DCHECK(test_client_name_.empty());
46 test_client_name_ = test_client_name;
49 void IPCTestBase::CreateChannel(IPC::Listener* listener) {
50 return CreateChannelFromChannelHandle(GetChannelName(test_client_name_),
51 listener);
54 bool IPCTestBase::ConnectChannel() {
55 CHECK(channel_.get());
56 return channel_->Connect();
59 void IPCTestBase::DestroyChannel() {
60 DCHECK(channel_.get());
61 channel_.reset();
64 void IPCTestBase::CreateChannelFromChannelHandle(
65 const IPC::ChannelHandle& channel_handle,
66 IPC::Listener* listener) {
67 CHECK(!channel_.get());
68 CHECK(!channel_proxy_.get());
69 channel_.reset(new IPC::Channel(channel_handle,
70 IPC::Channel::MODE_SERVER,
71 listener));
74 void IPCTestBase::CreateChannelProxy(
75 IPC::Listener* listener,
76 base::SingleThreadTaskRunner* ipc_task_runner) {
77 CHECK(!channel_.get());
78 CHECK(!channel_proxy_.get());
79 channel_proxy_.reset(new IPC::ChannelProxy(GetChannelName(test_client_name_),
80 IPC::Channel::MODE_SERVER,
81 listener,
82 ipc_task_runner));
85 void IPCTestBase::DestroyChannelProxy() {
86 CHECK(channel_proxy_.get());
87 channel_proxy_.reset();
90 bool IPCTestBase::StartClient() {
91 DCHECK(client_process_ == base::kNullProcessHandle);
93 std::string test_main = test_client_name_ + "TestClientMain";
94 bool debug_on_start =
95 CommandLine::ForCurrentProcess()->HasSwitch(switches::kDebugChildren);
97 #if defined(OS_WIN)
98 client_process_ = MultiProcessTest::SpawnChild(test_main, debug_on_start);
99 #elif defined(OS_POSIX)
100 base::FileHandleMappingVector fds_to_map;
101 const int ipcfd = channel_.get() ? channel_->GetClientFileDescriptor() :
102 channel_proxy_->GetClientFileDescriptor();
103 if (ipcfd > -1)
104 fds_to_map.push_back(std::pair<int, int>(ipcfd, kPrimaryIPCChannel + 3));
106 client_process_ = MultiProcessTest::SpawnChild(test_main,
107 fds_to_map,
108 debug_on_start);
109 #endif
111 return client_process_ != base::kNullProcessHandle;
114 bool IPCTestBase::WaitForClientShutdown() {
115 DCHECK(client_process_ != base::kNullProcessHandle);
117 bool rv = base::WaitForSingleProcess(client_process_,
118 base::TimeDelta::FromSeconds(5));
119 base::CloseProcessHandle(client_process_);
120 client_process_ = base::kNullProcessHandle;
121 return rv;