blob: 419419483c5b162253cec2416498d872d64e9931 [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) {
Hajime Hoshife3ecdc2017-11-28 03:34:1341 channel_ = IPC::ChannelMojo::Create(
42 TakeHandle(), IPC::Channel::MODE_SERVER, listener,
43 base::ThreadTaskRunnerHandle::Get(), base::ThreadTaskRunnerHandle::Get());
sammc4bcc4ed62016-10-27 10:13:5944}
45
46bool IPCChannelMojoTestBase::ConnectChannel() {
47 return channel_->Connect();
48}
49
50void IPCChannelMojoTestBase::DestroyChannel() {
51 channel_.reset();
52}
53
54mojo::ScopedMessagePipeHandle IPCChannelMojoTestBase::TakeHandle() {
55 return std::move(handle_);
56}
57
58IpcChannelMojoTestClient::IpcChannelMojoTestClient() = default;
59
60IpcChannelMojoTestClient::~IpcChannelMojoTestClient() = default;
61
62void IpcChannelMojoTestClient::Init(mojo::ScopedMessagePipeHandle handle) {
63 handle_ = std::move(handle);
64}
65
66void IpcChannelMojoTestClient::Connect(IPC::Listener* listener) {
Hajime Hoshife3ecdc2017-11-28 03:34:1367 channel_ = IPC::ChannelMojo::Create(
68 std::move(handle_), IPC::Channel::MODE_CLIENT, listener,
69 base::ThreadTaskRunnerHandle::Get(), base::ThreadTaskRunnerHandle::Get());
sammc4bcc4ed62016-10-27 10:13:5970 CHECK(channel_->Connect());
71}
72
73void 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}