blob: 2030cb72fe09ac0ec89e4a591031c5ca46f57c13 [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
dchenge48600452015-12-28 02:24:507#include <utility>
8
danakj03de39b22016-04-23 04:21:099#include "base/memory/ptr_util.h"
sammc4bcc4ed62016-10-27 10:13:5910#include "base/run_loop.h"
dchenge48600452015-12-28 02:24:5011#include "base/single_thread_task_runner.h"
fdoraya19b7702016-12-23 14:19:3112#include "base/threading/thread_task_runner_handle.h"
dchenge48600452015-12-28 02:24:5013#include "build/build_config.h"
sammc4bcc4ed62016-10-27 10:13:5914#include "ipc/ipc_channel_mojo.h"
[email protected]0cb7d8c82013-01-11 15:13:3715
sammc4bcc4ed62016-10-27 10:13:5916IPCChannelMojoTestBase::IPCChannelMojoTestBase() = default;
17IPCChannelMojoTestBase::~IPCChannelMojoTestBase() = default;
18
19void IPCChannelMojoTestBase::Init(const std::string& test_client_name) {
sammcbeeed3682016-11-08 23:24:3520 InitWithCustomMessageLoop(test_client_name,
Jeremy Roman160eb922017-08-29 17:43:4321 std::make_unique<base::MessageLoop>());
sammcbeeed3682016-11-08 23:24:3522}
23
24void IPCChannelMojoTestBase::InitWithCustomMessageLoop(
25 const std::string& test_client_name,
26 std::unique_ptr<base::MessageLoop> message_loop) {
sammc4bcc4ed62016-10-27 10:13:5927 handle_ = helper_.StartChild(test_client_name);
sammcbeeed3682016-11-08 23:24:3528 message_loop_ = std::move(message_loop);
sammc4bcc4ed62016-10-27 10:13:5929}
30
31bool IPCChannelMojoTestBase::WaitForClientShutdown() {
32 return helper_.WaitForChildTestShutdown();
33}
34
35void IPCChannelMojoTestBase::TearDown() {
sammcbeeed3682016-11-08 23:24:3536 if (message_loop_)
37 base::RunLoop().RunUntilIdle();
sammc4bcc4ed62016-10-27 10:13:5938}
39
40void IPCChannelMojoTestBase::CreateChannel(IPC::Listener* listener) {
Sigurdur Asgeirssond655dd65f2019-11-12 19:32:2041 channel_ =
42 IPC::ChannelMojo::Create(TakeHandle(), IPC::Channel::MODE_SERVER,
43 listener, base::ThreadTaskRunnerHandle::Get(),
44 base::ThreadTaskRunnerHandle::Get(), nullptr);
sammc4bcc4ed62016-10-27 10:13:5945}
46
47bool IPCChannelMojoTestBase::ConnectChannel() {
48 return channel_->Connect();
49}
50
51void IPCChannelMojoTestBase::DestroyChannel() {
52 channel_.reset();
53}
54
55mojo::ScopedMessagePipeHandle IPCChannelMojoTestBase::TakeHandle() {
56 return std::move(handle_);
57}
58
59IpcChannelMojoTestClient::IpcChannelMojoTestClient() = default;
60
61IpcChannelMojoTestClient::~IpcChannelMojoTestClient() = default;
62
63void IpcChannelMojoTestClient::Init(mojo::ScopedMessagePipeHandle handle) {
64 handle_ = std::move(handle);
65}
66
67void IpcChannelMojoTestClient::Connect(IPC::Listener* listener) {
Sigurdur Asgeirssond655dd65f2019-11-12 19:32:2068 channel_ =
69 IPC::ChannelMojo::Create(std::move(handle_), IPC::Channel::MODE_CLIENT,
70 listener, base::ThreadTaskRunnerHandle::Get(),
71 base::ThreadTaskRunnerHandle::Get(), nullptr);
sammc4bcc4ed62016-10-27 10:13:5972 CHECK(channel_->Connect());
73}
74
75void IpcChannelMojoTestClient::Close() {
76 channel_->Close();
77
78 base::RunLoop run_loop;
79 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
80 run_loop.QuitClosure());
81 run_loop.Run();
82}