xref: /aosp_15_r20/external/libchrome/ipc/ipc_test_base.cc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #include "ipc/ipc_test_base.h"
6*635a8641SAndroid Build Coastguard Worker 
7*635a8641SAndroid Build Coastguard Worker #include <utility>
8*635a8641SAndroid Build Coastguard Worker 
9*635a8641SAndroid Build Coastguard Worker #include "base/memory/ptr_util.h"
10*635a8641SAndroid Build Coastguard Worker #include "base/run_loop.h"
11*635a8641SAndroid Build Coastguard Worker #include "base/single_thread_task_runner.h"
12*635a8641SAndroid Build Coastguard Worker #include "base/threading/thread_task_runner_handle.h"
13*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
14*635a8641SAndroid Build Coastguard Worker #include "ipc/ipc_channel_mojo.h"
15*635a8641SAndroid Build Coastguard Worker 
16*635a8641SAndroid Build Coastguard Worker IPCChannelMojoTestBase::IPCChannelMojoTestBase() = default;
17*635a8641SAndroid Build Coastguard Worker IPCChannelMojoTestBase::~IPCChannelMojoTestBase() = default;
18*635a8641SAndroid Build Coastguard Worker 
Init(const std::string & test_client_name)19*635a8641SAndroid Build Coastguard Worker void IPCChannelMojoTestBase::Init(const std::string& test_client_name) {
20*635a8641SAndroid Build Coastguard Worker   InitWithCustomMessageLoop(test_client_name,
21*635a8641SAndroid Build Coastguard Worker                             std::make_unique<base::MessageLoop>());
22*635a8641SAndroid Build Coastguard Worker }
23*635a8641SAndroid Build Coastguard Worker 
InitWithCustomMessageLoop(const std::string & test_client_name,std::unique_ptr<base::MessageLoop> message_loop)24*635a8641SAndroid Build Coastguard Worker void IPCChannelMojoTestBase::InitWithCustomMessageLoop(
25*635a8641SAndroid Build Coastguard Worker     const std::string& test_client_name,
26*635a8641SAndroid Build Coastguard Worker     std::unique_ptr<base::MessageLoop> message_loop) {
27*635a8641SAndroid Build Coastguard Worker   handle_ = helper_.StartChild(test_client_name);
28*635a8641SAndroid Build Coastguard Worker   message_loop_ = std::move(message_loop);
29*635a8641SAndroid Build Coastguard Worker }
30*635a8641SAndroid Build Coastguard Worker 
WaitForClientShutdown()31*635a8641SAndroid Build Coastguard Worker bool IPCChannelMojoTestBase::WaitForClientShutdown() {
32*635a8641SAndroid Build Coastguard Worker   return helper_.WaitForChildTestShutdown();
33*635a8641SAndroid Build Coastguard Worker }
34*635a8641SAndroid Build Coastguard Worker 
TearDown()35*635a8641SAndroid Build Coastguard Worker void IPCChannelMojoTestBase::TearDown() {
36*635a8641SAndroid Build Coastguard Worker   if (message_loop_)
37*635a8641SAndroid Build Coastguard Worker     base::RunLoop().RunUntilIdle();
38*635a8641SAndroid Build Coastguard Worker }
39*635a8641SAndroid Build Coastguard Worker 
CreateChannel(IPC::Listener * listener)40*635a8641SAndroid Build Coastguard Worker void IPCChannelMojoTestBase::CreateChannel(IPC::Listener* listener) {
41*635a8641SAndroid Build Coastguard Worker   channel_ = IPC::ChannelMojo::Create(
42*635a8641SAndroid Build Coastguard Worker       TakeHandle(), IPC::Channel::MODE_SERVER, listener,
43*635a8641SAndroid Build Coastguard Worker       base::ThreadTaskRunnerHandle::Get(), base::ThreadTaskRunnerHandle::Get());
44*635a8641SAndroid Build Coastguard Worker }
45*635a8641SAndroid Build Coastguard Worker 
ConnectChannel()46*635a8641SAndroid Build Coastguard Worker bool IPCChannelMojoTestBase::ConnectChannel() {
47*635a8641SAndroid Build Coastguard Worker   return channel_->Connect();
48*635a8641SAndroid Build Coastguard Worker }
49*635a8641SAndroid Build Coastguard Worker 
DestroyChannel()50*635a8641SAndroid Build Coastguard Worker void IPCChannelMojoTestBase::DestroyChannel() {
51*635a8641SAndroid Build Coastguard Worker   channel_.reset();
52*635a8641SAndroid Build Coastguard Worker }
53*635a8641SAndroid Build Coastguard Worker 
TakeHandle()54*635a8641SAndroid Build Coastguard Worker mojo::ScopedMessagePipeHandle IPCChannelMojoTestBase::TakeHandle() {
55*635a8641SAndroid Build Coastguard Worker   return std::move(handle_);
56*635a8641SAndroid Build Coastguard Worker }
57*635a8641SAndroid Build Coastguard Worker 
58*635a8641SAndroid Build Coastguard Worker IpcChannelMojoTestClient::IpcChannelMojoTestClient() = default;
59*635a8641SAndroid Build Coastguard Worker 
60*635a8641SAndroid Build Coastguard Worker IpcChannelMojoTestClient::~IpcChannelMojoTestClient() = default;
61*635a8641SAndroid Build Coastguard Worker 
Init(mojo::ScopedMessagePipeHandle handle)62*635a8641SAndroid Build Coastguard Worker void IpcChannelMojoTestClient::Init(mojo::ScopedMessagePipeHandle handle) {
63*635a8641SAndroid Build Coastguard Worker   handle_ = std::move(handle);
64*635a8641SAndroid Build Coastguard Worker }
65*635a8641SAndroid Build Coastguard Worker 
Connect(IPC::Listener * listener)66*635a8641SAndroid Build Coastguard Worker void IpcChannelMojoTestClient::Connect(IPC::Listener* listener) {
67*635a8641SAndroid Build Coastguard Worker   channel_ = IPC::ChannelMojo::Create(
68*635a8641SAndroid Build Coastguard Worker       std::move(handle_), IPC::Channel::MODE_CLIENT, listener,
69*635a8641SAndroid Build Coastguard Worker       base::ThreadTaskRunnerHandle::Get(), base::ThreadTaskRunnerHandle::Get());
70*635a8641SAndroid Build Coastguard Worker   CHECK(channel_->Connect());
71*635a8641SAndroid Build Coastguard Worker }
72*635a8641SAndroid Build Coastguard Worker 
Close()73*635a8641SAndroid Build Coastguard Worker void IpcChannelMojoTestClient::Close() {
74*635a8641SAndroid Build Coastguard Worker   channel_->Close();
75*635a8641SAndroid Build Coastguard Worker 
76*635a8641SAndroid Build Coastguard Worker   base::RunLoop run_loop;
77*635a8641SAndroid Build Coastguard Worker   base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
78*635a8641SAndroid Build Coastguard Worker                                                 run_loop.QuitClosure());
79*635a8641SAndroid Build Coastguard Worker   run_loop.Run();
80*635a8641SAndroid Build Coastguard Worker }
81