[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 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 | |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 5 | #include "ipc/ipc_test_base.h" |
| 6 | |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 9 | #include "base/memory/ptr_util.h" |
sammc | 4bcc4ed6 | 2016-10-27 10:13:59 | [diff] [blame] | 10 | #include "base/run_loop.h" |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 11 | #include "base/single_thread_task_runner.h" |
fdoray | a19b770 | 2016-12-23 14:19:31 | [diff] [blame] | 12 | #include "base/threading/thread_task_runner_handle.h" |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 13 | #include "build/build_config.h" |
sammc | 4bcc4ed6 | 2016-10-27 10:13:59 | [diff] [blame] | 14 | #include "ipc/ipc_channel_mojo.h" |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 15 | |
sammc | 4bcc4ed6 | 2016-10-27 10:13:59 | [diff] [blame] | 16 | IPCChannelMojoTestBase::IPCChannelMojoTestBase() = default; |
| 17 | IPCChannelMojoTestBase::~IPCChannelMojoTestBase() = default; |
| 18 | |
| 19 | void IPCChannelMojoTestBase::Init(const std::string& test_client_name) { |
sammc | beeed368 | 2016-11-08 23:24:35 | [diff] [blame] | 20 | InitWithCustomMessageLoop(test_client_name, |
Jeremy Roman | 160eb92 | 2017-08-29 17:43:43 | [diff] [blame] | 21 | std::make_unique<base::MessageLoop>()); |
sammc | beeed368 | 2016-11-08 23:24:35 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | void IPCChannelMojoTestBase::InitWithCustomMessageLoop( |
| 25 | const std::string& test_client_name, |
| 26 | std::unique_ptr<base::MessageLoop> message_loop) { |
sammc | 4bcc4ed6 | 2016-10-27 10:13:59 | [diff] [blame] | 27 | handle_ = helper_.StartChild(test_client_name); |
sammc | beeed368 | 2016-11-08 23:24:35 | [diff] [blame] | 28 | message_loop_ = std::move(message_loop); |
sammc | 4bcc4ed6 | 2016-10-27 10:13:59 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | bool IPCChannelMojoTestBase::WaitForClientShutdown() { |
| 32 | return helper_.WaitForChildTestShutdown(); |
| 33 | } |
| 34 | |
| 35 | void IPCChannelMojoTestBase::TearDown() { |
sammc | beeed368 | 2016-11-08 23:24:35 | [diff] [blame] | 36 | if (message_loop_) |
| 37 | base::RunLoop().RunUntilIdle(); |
sammc | 4bcc4ed6 | 2016-10-27 10:13:59 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void IPCChannelMojoTestBase::CreateChannel(IPC::Listener* listener) { |
Hajime Hoshi | fe3ecdc | 2017-11-28 03:34:13 | [diff] [blame] | 41 | channel_ = IPC::ChannelMojo::Create( |
| 42 | TakeHandle(), IPC::Channel::MODE_SERVER, listener, |
| 43 | base::ThreadTaskRunnerHandle::Get(), base::ThreadTaskRunnerHandle::Get()); |
sammc | 4bcc4ed6 | 2016-10-27 10:13:59 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | bool IPCChannelMojoTestBase::ConnectChannel() { |
| 47 | return channel_->Connect(); |
| 48 | } |
| 49 | |
| 50 | void IPCChannelMojoTestBase::DestroyChannel() { |
| 51 | channel_.reset(); |
| 52 | } |
| 53 | |
| 54 | mojo::ScopedMessagePipeHandle IPCChannelMojoTestBase::TakeHandle() { |
| 55 | return std::move(handle_); |
| 56 | } |
| 57 | |
| 58 | IpcChannelMojoTestClient::IpcChannelMojoTestClient() = default; |
| 59 | |
| 60 | IpcChannelMojoTestClient::~IpcChannelMojoTestClient() = default; |
| 61 | |
| 62 | void IpcChannelMojoTestClient::Init(mojo::ScopedMessagePipeHandle handle) { |
| 63 | handle_ = std::move(handle); |
| 64 | } |
| 65 | |
| 66 | void IpcChannelMojoTestClient::Connect(IPC::Listener* listener) { |
Hajime Hoshi | fe3ecdc | 2017-11-28 03:34:13 | [diff] [blame] | 67 | channel_ = IPC::ChannelMojo::Create( |
| 68 | std::move(handle_), IPC::Channel::MODE_CLIENT, listener, |
| 69 | base::ThreadTaskRunnerHandle::Get(), base::ThreadTaskRunnerHandle::Get()); |
sammc | 4bcc4ed6 | 2016-10-27 10:13:59 | [diff] [blame] | 70 | CHECK(channel_->Connect()); |
| 71 | } |
| 72 | |
| 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 | } |