blob: 667cb061e7e3dcf630579b4e27cc746c7ffd668b [file] [log] [blame]
[email protected]0cb7d8c82013-01-11 15:13:371// 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]0cb7d8c82013-01-11 15:13:375#include "ipc/ipc_test_base.h"
6
Carlos Caballerof8599f02019-12-13 10:38:017#include <memory>
dchenge48600452015-12-28 02:24:508#include <utility>
9
danakj03de39b22016-04-23 04:21:0910#include "base/memory/ptr_util.h"
sammc4bcc4ed62016-10-27 10:13:5911#include "base/run_loop.h"
Patrick Monette643cdf62021-10-15 19:13:4212#include "base/task/single_thread_task_runner.h"
Carlos Caballerof8599f02019-12-13 10:38:0113#include "base/test/task_environment.h"
fdoraya19b7702016-12-23 14:19:3114#include "base/threading/thread_task_runner_handle.h"
dchenge48600452015-12-28 02:24:5015#include "build/build_config.h"
sammc4bcc4ed62016-10-27 10:13:5916#include "ipc/ipc_channel_mojo.h"
[email protected]0cb7d8c82013-01-11 15:13:3717
sammc4bcc4ed62016-10-27 10:13:5918IPCChannelMojoTestBase::IPCChannelMojoTestBase() = default;
19IPCChannelMojoTestBase::~IPCChannelMojoTestBase() = default;
20
21void IPCChannelMojoTestBase::Init(const std::string& test_client_name) {
22 handle_ = helper_.StartChild(test_client_name);
23}
24
25bool IPCChannelMojoTestBase::WaitForClientShutdown() {
26 return helper_.WaitForChildTestShutdown();
27}
28
29void IPCChannelMojoTestBase::TearDown() {
Greg Thompson9360c5d42021-12-01 17:55:3430 base::RunLoop().RunUntilIdle();
sammc4bcc4ed62016-10-27 10:13:5931}
32
33void IPCChannelMojoTestBase::CreateChannel(IPC::Listener* listener) {
Sigurdur Asgeirssond655dd65f2019-11-12 19:32:2034 channel_ =
35 IPC::ChannelMojo::Create(TakeHandle(), IPC::Channel::MODE_SERVER,
36 listener, base::ThreadTaskRunnerHandle::Get(),
37 base::ThreadTaskRunnerHandle::Get(), nullptr);
sammc4bcc4ed62016-10-27 10:13:5938}
39
40bool IPCChannelMojoTestBase::ConnectChannel() {
41 return channel_->Connect();
42}
43
44void IPCChannelMojoTestBase::DestroyChannel() {
45 channel_.reset();
46}
47
48mojo::ScopedMessagePipeHandle IPCChannelMojoTestBase::TakeHandle() {
49 return std::move(handle_);
50}
51
52IpcChannelMojoTestClient::IpcChannelMojoTestClient() = default;
53
54IpcChannelMojoTestClient::~IpcChannelMojoTestClient() = default;
55
56void IpcChannelMojoTestClient::Init(mojo::ScopedMessagePipeHandle handle) {
57 handle_ = std::move(handle);
58}
59
60void IpcChannelMojoTestClient::Connect(IPC::Listener* listener) {
Sigurdur Asgeirssond655dd65f2019-11-12 19:32:2061 channel_ =
62 IPC::ChannelMojo::Create(std::move(handle_), IPC::Channel::MODE_CLIENT,
63 listener, base::ThreadTaskRunnerHandle::Get(),
64 base::ThreadTaskRunnerHandle::Get(), nullptr);
sammc4bcc4ed62016-10-27 10:13:5965 CHECK(channel_->Connect());
66}
67
68void 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}