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.
4
5 #include "ipc/ipc_test_base.h"
6
7 #include <utility>
8
9 #include "base/memory/ptr_util.h"
10 #include "base/run_loop.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/threading/thread_task_runner_handle.h"
13 #include "build/build_config.h"
14 #include "ipc/ipc_channel_mojo.h"
15
16 IPCChannelMojoTestBase::IPCChannelMojoTestBase() = default;
17 IPCChannelMojoTestBase::~IPCChannelMojoTestBase() = default;
18
Init(const std::string & test_client_name)19 void IPCChannelMojoTestBase::Init(const std::string& test_client_name) {
20 InitWithCustomMessageLoop(test_client_name,
21 std::make_unique<base::MessageLoop>());
22 }
23
InitWithCustomMessageLoop(const std::string & test_client_name,std::unique_ptr<base::MessageLoop> message_loop)24 void IPCChannelMojoTestBase::InitWithCustomMessageLoop(
25 const std::string& test_client_name,
26 std::unique_ptr<base::MessageLoop> message_loop) {
27 handle_ = helper_.StartChild(test_client_name);
28 message_loop_ = std::move(message_loop);
29 }
30
WaitForClientShutdown()31 bool IPCChannelMojoTestBase::WaitForClientShutdown() {
32 return helper_.WaitForChildTestShutdown();
33 }
34
TearDown()35 void IPCChannelMojoTestBase::TearDown() {
36 if (message_loop_)
37 base::RunLoop().RunUntilIdle();
38 }
39
CreateChannel(IPC::Listener * listener)40 void IPCChannelMojoTestBase::CreateChannel(IPC::Listener* listener) {
41 channel_ = IPC::ChannelMojo::Create(
42 TakeHandle(), IPC::Channel::MODE_SERVER, listener,
43 base::ThreadTaskRunnerHandle::Get(), base::ThreadTaskRunnerHandle::Get());
44 }
45
ConnectChannel()46 bool IPCChannelMojoTestBase::ConnectChannel() {
47 return channel_->Connect();
48 }
49
DestroyChannel()50 void IPCChannelMojoTestBase::DestroyChannel() {
51 channel_.reset();
52 }
53
TakeHandle()54 mojo::ScopedMessagePipeHandle IPCChannelMojoTestBase::TakeHandle() {
55 return std::move(handle_);
56 }
57
58 IpcChannelMojoTestClient::IpcChannelMojoTestClient() = default;
59
60 IpcChannelMojoTestClient::~IpcChannelMojoTestClient() = default;
61
Init(mojo::ScopedMessagePipeHandle handle)62 void IpcChannelMojoTestClient::Init(mojo::ScopedMessagePipeHandle handle) {
63 handle_ = std::move(handle);
64 }
65
Connect(IPC::Listener * listener)66 void IpcChannelMojoTestClient::Connect(IPC::Listener* listener) {
67 channel_ = IPC::ChannelMojo::Create(
68 std::move(handle_), IPC::Channel::MODE_CLIENT, listener,
69 base::ThreadTaskRunnerHandle::Get(), base::ThreadTaskRunnerHandle::Get());
70 CHECK(channel_->Connect());
71 }
72
Close()73 void IpcChannelMojoTestClient::Close() {
74 channel_->Close();
75
76 base::RunLoop run_loop;
77 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
78 run_loop.QuitClosure());
79 run_loop.Run();
80 }
81