[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 | |
Carlos Caballero | f8599f0 | 2019-12-13 10:38:01 | [diff] [blame] | 7 | #include <memory> |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 8 | #include <utility> |
| 9 | |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 10 | #include "base/memory/ptr_util.h" |
sammc | 4bcc4ed6 | 2016-10-27 10:13:59 | [diff] [blame] | 11 | #include "base/run_loop.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 12 | #include "base/task/single_thread_task_runner.h" |
Carlos Caballero | f8599f0 | 2019-12-13 10:38:01 | [diff] [blame] | 13 | #include "base/test/task_environment.h" |
fdoray | a19b770 | 2016-12-23 14:19:31 | [diff] [blame] | 14 | #include "base/threading/thread_task_runner_handle.h" |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 15 | #include "build/build_config.h" |
sammc | 4bcc4ed6 | 2016-10-27 10:13:59 | [diff] [blame] | 16 | #include "ipc/ipc_channel_mojo.h" |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 17 | |
sammc | 4bcc4ed6 | 2016-10-27 10:13:59 | [diff] [blame] | 18 | IPCChannelMojoTestBase::IPCChannelMojoTestBase() = default; |
| 19 | IPCChannelMojoTestBase::~IPCChannelMojoTestBase() = default; |
| 20 | |
| 21 | void IPCChannelMojoTestBase::Init(const std::string& test_client_name) { |
| 22 | handle_ = helper_.StartChild(test_client_name); |
| 23 | } |
| 24 | |
| 25 | bool IPCChannelMojoTestBase::WaitForClientShutdown() { |
| 26 | return helper_.WaitForChildTestShutdown(); |
| 27 | } |
| 28 | |
| 29 | void IPCChannelMojoTestBase::TearDown() { |
Greg Thompson | 9360c5d4 | 2021-12-01 17:55:34 | [diff] [blame] | 30 | base::RunLoop().RunUntilIdle(); |
sammc | 4bcc4ed6 | 2016-10-27 10:13:59 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | void IPCChannelMojoTestBase::CreateChannel(IPC::Listener* listener) { |
Sigurdur Asgeirsson | d655dd65f | 2019-11-12 19:32:20 | [diff] [blame] | 34 | channel_ = |
| 35 | IPC::ChannelMojo::Create(TakeHandle(), IPC::Channel::MODE_SERVER, |
| 36 | listener, base::ThreadTaskRunnerHandle::Get(), |
| 37 | base::ThreadTaskRunnerHandle::Get(), nullptr); |
sammc | 4bcc4ed6 | 2016-10-27 10:13:59 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | bool IPCChannelMojoTestBase::ConnectChannel() { |
| 41 | return channel_->Connect(); |
| 42 | } |
| 43 | |
| 44 | void IPCChannelMojoTestBase::DestroyChannel() { |
| 45 | channel_.reset(); |
| 46 | } |
| 47 | |
| 48 | mojo::ScopedMessagePipeHandle IPCChannelMojoTestBase::TakeHandle() { |
| 49 | return std::move(handle_); |
| 50 | } |
| 51 | |
| 52 | IpcChannelMojoTestClient::IpcChannelMojoTestClient() = default; |
| 53 | |
| 54 | IpcChannelMojoTestClient::~IpcChannelMojoTestClient() = default; |
| 55 | |
| 56 | void IpcChannelMojoTestClient::Init(mojo::ScopedMessagePipeHandle handle) { |
| 57 | handle_ = std::move(handle); |
| 58 | } |
| 59 | |
| 60 | void IpcChannelMojoTestClient::Connect(IPC::Listener* listener) { |
Sigurdur Asgeirsson | d655dd65f | 2019-11-12 19:32:20 | [diff] [blame] | 61 | channel_ = |
| 62 | IPC::ChannelMojo::Create(std::move(handle_), IPC::Channel::MODE_CLIENT, |
| 63 | listener, base::ThreadTaskRunnerHandle::Get(), |
| 64 | base::ThreadTaskRunnerHandle::Get(), nullptr); |
sammc | 4bcc4ed6 | 2016-10-27 10:13:59 | [diff] [blame] | 65 | CHECK(channel_->Connect()); |
| 66 | } |
| 67 | |
| 68 | void IpcChannelMojoTestClient::Close() { |
| 69 | channel_->Close(); |
| 70 | |
| 71 | base::RunLoop run_loop; |
| 72 | base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 73 | run_loop.QuitClosure()); |
| 74 | run_loop.Run(); |
| 75 | } |