Remove NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED from c/b/e/api
[chromium-blink-merge.git] / ipc / ipc_test_base.cc
blobb609e8cb2c23fecd6c9b338834b8fafbfb54f8b8
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/process/kill.h"
11 #include "base/threading/thread.h"
12 #include "base/time/time.h"
13 #include "ipc/ipc_descriptors.h"
15 #if defined(OS_POSIX)
16 #include "base/posix/global_descriptors.h"
17 #endif
19 // static
20 std::string IPCTestBase::GetChannelName(const std::string& test_client_name) {
21 DCHECK(!test_client_name.empty());
22 return test_client_name + "__Channel";
25 IPCTestBase::IPCTestBase()
26 : client_process_(base::kNullProcessHandle) {
29 IPCTestBase::~IPCTestBase() {
32 void IPCTestBase::SetUp() {
33 MultiProcessTest::SetUp();
35 // Construct a fresh IO Message loop for the duration of each test.
36 DCHECK(!message_loop_.get());
37 message_loop_.reset(new base::MessageLoopForIO());
40 void IPCTestBase::TearDown() {
41 DCHECK(message_loop_.get());
42 message_loop_.reset();
43 MultiProcessTest::TearDown();
46 void IPCTestBase::Init(const std::string& test_client_name) {
47 DCHECK(!test_client_name.empty());
48 DCHECK(test_client_name_.empty());
49 test_client_name_ = test_client_name;
52 void IPCTestBase::CreateChannel(IPC::Listener* listener) {
53 return CreateChannelFromChannelHandle(GetChannelName(test_client_name_),
54 listener);
57 bool IPCTestBase::ConnectChannel() {
58 CHECK(channel_.get());
59 return channel_->Connect();
62 void IPCTestBase::DestroyChannel() {
63 DCHECK(channel_.get());
64 channel_.reset();
67 void IPCTestBase::CreateChannelFromChannelHandle(
68 const IPC::ChannelHandle& channel_handle,
69 IPC::Listener* listener) {
70 CHECK(!channel_.get());
71 CHECK(!channel_proxy_.get());
72 channel_.reset(new IPC::Channel(channel_handle,
73 IPC::Channel::MODE_SERVER,
74 listener));
77 void IPCTestBase::CreateChannelProxy(
78 IPC::Listener* listener,
79 base::SingleThreadTaskRunner* ipc_task_runner) {
80 CHECK(!channel_.get());
81 CHECK(!channel_proxy_.get());
82 channel_proxy_.reset(new IPC::ChannelProxy(GetChannelName(test_client_name_),
83 IPC::Channel::MODE_SERVER,
84 listener,
85 ipc_task_runner));
88 void IPCTestBase::DestroyChannelProxy() {
89 CHECK(channel_proxy_.get());
90 channel_proxy_.reset();
93 bool IPCTestBase::StartClient() {
94 DCHECK(client_process_ == base::kNullProcessHandle);
96 std::string test_main = test_client_name_ + "TestClientMain";
98 #if defined(OS_WIN)
99 client_process_ = SpawnChild(test_main);
100 #elif defined(OS_POSIX)
101 base::FileHandleMappingVector fds_to_map;
102 const int ipcfd = channel_.get() ? channel_->GetClientFileDescriptor() :
103 channel_proxy_->GetClientFileDescriptor();
104 if (ipcfd > -1)
105 fds_to_map.push_back(std::pair<int, int>(ipcfd,
106 kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor));
107 base::LaunchOptions options;
108 options.fds_to_remap = &fds_to_map;
109 client_process_ = SpawnChildWithOptions(test_main, options);
110 #endif
112 return client_process_ != base::kNullProcessHandle;
115 bool IPCTestBase::WaitForClientShutdown() {
116 DCHECK(client_process_ != base::kNullProcessHandle);
118 bool rv = base::WaitForSingleProcess(client_process_,
119 base::TimeDelta::FromSeconds(5));
120 base::CloseProcessHandle(client_process_);
121 client_process_ = base::kNullProcessHandle;
122 return rv;