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