blob: f2a73c43f905c5349455fc2ac70c132d95a64c44 [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);
Carlos Caballerof8599f02019-12-13 10:38:0123 task_environment_ =
24 std::make_unique<base::test::SingleThreadTaskEnvironment>();
sammc4bcc4ed62016-10-27 10:13:5925}
26
27bool IPCChannelMojoTestBase::WaitForClientShutdown() {
28 return helper_.WaitForChildTestShutdown();
29}
30
31void IPCChannelMojoTestBase::TearDown() {
Carlos Caballerof8599f02019-12-13 10:38:0132 if (task_environment_)
sammcbeeed3682016-11-08 23:24:3533 base::RunLoop().RunUntilIdle();
sammc4bcc4ed62016-10-27 10:13:5934}
35
36void IPCChannelMojoTestBase::CreateChannel(IPC::Listener* listener) {
Sigurdur Asgeirssond655dd65f2019-11-12 19:32:2037 channel_ =
38 IPC::ChannelMojo::Create(TakeHandle(), IPC::Channel::MODE_SERVER,
39 listener, base::ThreadTaskRunnerHandle::Get(),
40 base::ThreadTaskRunnerHandle::Get(), nullptr);
sammc4bcc4ed62016-10-27 10:13:5941}
42
43bool IPCChannelMojoTestBase::ConnectChannel() {
44 return channel_->Connect();
45}
46
47void IPCChannelMojoTestBase::DestroyChannel() {
48 channel_.reset();
49}
50
51mojo::ScopedMessagePipeHandle IPCChannelMojoTestBase::TakeHandle() {
52 return std::move(handle_);
53}
54
55IpcChannelMojoTestClient::IpcChannelMojoTestClient() = default;
56
57IpcChannelMojoTestClient::~IpcChannelMojoTestClient() = default;
58
59void IpcChannelMojoTestClient::Init(mojo::ScopedMessagePipeHandle handle) {
60 handle_ = std::move(handle);
61}
62
63void IpcChannelMojoTestClient::Connect(IPC::Listener* listener) {
Sigurdur Asgeirssond655dd65f2019-11-12 19:32:2064 channel_ =
65 IPC::ChannelMojo::Create(std::move(handle_), IPC::Channel::MODE_CLIENT,
66 listener, base::ThreadTaskRunnerHandle::Get(),
67 base::ThreadTaskRunnerHandle::Get(), nullptr);
sammc4bcc4ed62016-10-27 10:13:5968 CHECK(channel_->Connect());
69}
70
71void IpcChannelMojoTestClient::Close() {
72 channel_->Close();
73
74 base::RunLoop run_loop;
75 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
76 run_loop.QuitClosure());
77 run_loop.Run();
78}