1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2011 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 #ifndef IPC_IPC_TEST_BASE_H_ 6*635a8641SAndroid Build Coastguard Worker #define IPC_IPC_TEST_BASE_H_ 7*635a8641SAndroid Build Coastguard Worker 8*635a8641SAndroid Build Coastguard Worker #include <memory> 9*635a8641SAndroid Build Coastguard Worker #include <string> 10*635a8641SAndroid Build Coastguard Worker 11*635a8641SAndroid Build Coastguard Worker #include "base/macros.h" 12*635a8641SAndroid Build Coastguard Worker #include "base/message_loop/message_loop.h" 13*635a8641SAndroid Build Coastguard Worker #include "base/process/process.h" 14*635a8641SAndroid Build Coastguard Worker #include "base/test/multiprocess_test.h" 15*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h" 16*635a8641SAndroid Build Coastguard Worker #include "ipc/ipc_channel.h" 17*635a8641SAndroid Build Coastguard Worker #include "ipc/ipc_channel_factory.h" 18*635a8641SAndroid Build Coastguard Worker #include "ipc/ipc_channel_proxy.h" 19*635a8641SAndroid Build Coastguard Worker #include "mojo/core/test/mojo_test_base.h" 20*635a8641SAndroid Build Coastguard Worker #include "mojo/core/test/multiprocess_test_helper.h" 21*635a8641SAndroid Build Coastguard Worker 22*635a8641SAndroid Build Coastguard Worker namespace base { 23*635a8641SAndroid Build Coastguard Worker class MessageLoop; 24*635a8641SAndroid Build Coastguard Worker } 25*635a8641SAndroid Build Coastguard Worker 26*635a8641SAndroid Build Coastguard Worker class IPCChannelMojoTestBase : public testing::Test { 27*635a8641SAndroid Build Coastguard Worker public: 28*635a8641SAndroid Build Coastguard Worker IPCChannelMojoTestBase(); 29*635a8641SAndroid Build Coastguard Worker ~IPCChannelMojoTestBase() override; 30*635a8641SAndroid Build Coastguard Worker 31*635a8641SAndroid Build Coastguard Worker void Init(const std::string& test_client_name); 32*635a8641SAndroid Build Coastguard Worker void InitWithCustomMessageLoop( 33*635a8641SAndroid Build Coastguard Worker const std::string& test_client_name, 34*635a8641SAndroid Build Coastguard Worker std::unique_ptr<base::MessageLoop> message_loop); 35*635a8641SAndroid Build Coastguard Worker 36*635a8641SAndroid Build Coastguard Worker bool WaitForClientShutdown(); 37*635a8641SAndroid Build Coastguard Worker 38*635a8641SAndroid Build Coastguard Worker void TearDown() override; 39*635a8641SAndroid Build Coastguard Worker 40*635a8641SAndroid Build Coastguard Worker void CreateChannel(IPC::Listener* listener); 41*635a8641SAndroid Build Coastguard Worker 42*635a8641SAndroid Build Coastguard Worker bool ConnectChannel(); 43*635a8641SAndroid Build Coastguard Worker 44*635a8641SAndroid Build Coastguard Worker void DestroyChannel(); 45*635a8641SAndroid Build Coastguard Worker sender()46*635a8641SAndroid Build Coastguard Worker IPC::Sender* sender() { return channel(); } channel()47*635a8641SAndroid Build Coastguard Worker IPC::Channel* channel() { return channel_.get(); } client_process()48*635a8641SAndroid Build Coastguard Worker const base::Process& client_process() const { return helper_.test_child(); } 49*635a8641SAndroid Build Coastguard Worker 50*635a8641SAndroid Build Coastguard Worker protected: 51*635a8641SAndroid Build Coastguard Worker mojo::ScopedMessagePipeHandle TakeHandle(); 52*635a8641SAndroid Build Coastguard Worker 53*635a8641SAndroid Build Coastguard Worker private: 54*635a8641SAndroid Build Coastguard Worker std::unique_ptr<base::MessageLoop> message_loop_; 55*635a8641SAndroid Build Coastguard Worker 56*635a8641SAndroid Build Coastguard Worker mojo::ScopedMessagePipeHandle handle_; 57*635a8641SAndroid Build Coastguard Worker mojo::core::test::MultiprocessTestHelper helper_; 58*635a8641SAndroid Build Coastguard Worker 59*635a8641SAndroid Build Coastguard Worker std::unique_ptr<IPC::Channel> channel_; 60*635a8641SAndroid Build Coastguard Worker 61*635a8641SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(IPCChannelMojoTestBase); 62*635a8641SAndroid Build Coastguard Worker }; 63*635a8641SAndroid Build Coastguard Worker 64*635a8641SAndroid Build Coastguard Worker class IpcChannelMojoTestClient { 65*635a8641SAndroid Build Coastguard Worker public: 66*635a8641SAndroid Build Coastguard Worker IpcChannelMojoTestClient(); 67*635a8641SAndroid Build Coastguard Worker ~IpcChannelMojoTestClient(); 68*635a8641SAndroid Build Coastguard Worker 69*635a8641SAndroid Build Coastguard Worker void Init(mojo::ScopedMessagePipeHandle handle); 70*635a8641SAndroid Build Coastguard Worker 71*635a8641SAndroid Build Coastguard Worker void Connect(IPC::Listener* listener); 72*635a8641SAndroid Build Coastguard Worker 73*635a8641SAndroid Build Coastguard Worker void Close(); 74*635a8641SAndroid Build Coastguard Worker channel()75*635a8641SAndroid Build Coastguard Worker IPC::Channel* channel() const { return channel_.get(); } 76*635a8641SAndroid Build Coastguard Worker 77*635a8641SAndroid Build Coastguard Worker private: 78*635a8641SAndroid Build Coastguard Worker base::MessageLoopForIO main_message_loop_; 79*635a8641SAndroid Build Coastguard Worker mojo::ScopedMessagePipeHandle handle_; 80*635a8641SAndroid Build Coastguard Worker std::unique_ptr<IPC::Channel> channel_; 81*635a8641SAndroid Build Coastguard Worker }; 82*635a8641SAndroid Build Coastguard Worker 83*635a8641SAndroid Build Coastguard Worker // Use this to declare the client side for tests using IPCChannelMojoTestBase 84*635a8641SAndroid Build Coastguard Worker // when a custom test fixture class is required in the client. |test_base| must 85*635a8641SAndroid Build Coastguard Worker // be derived from IpcChannelMojoTestClient. 86*635a8641SAndroid Build Coastguard Worker #define DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE(client_name, \ 87*635a8641SAndroid Build Coastguard Worker test_base) \ 88*635a8641SAndroid Build Coastguard Worker class client_name##_MainFixture : public test_base { \ 89*635a8641SAndroid Build Coastguard Worker public: \ 90*635a8641SAndroid Build Coastguard Worker void Main(); \ 91*635a8641SAndroid Build Coastguard Worker }; \ 92*635a8641SAndroid Build Coastguard Worker MULTIPROCESS_TEST_MAIN_WITH_SETUP( \ 93*635a8641SAndroid Build Coastguard Worker client_name##TestChildMain, \ 94*635a8641SAndroid Build Coastguard Worker ::mojo::core::test::MultiprocessTestHelper::ChildSetup) { \ 95*635a8641SAndroid Build Coastguard Worker client_name##_MainFixture test; \ 96*635a8641SAndroid Build Coastguard Worker test.Init( \ 97*635a8641SAndroid Build Coastguard Worker std::move(mojo::core::test::MultiprocessTestHelper::primordial_pipe)); \ 98*635a8641SAndroid Build Coastguard Worker test.Main(); \ 99*635a8641SAndroid Build Coastguard Worker return (::testing::Test::HasFatalFailure() || \ 100*635a8641SAndroid Build Coastguard Worker ::testing::Test::HasNonfatalFailure()) \ 101*635a8641SAndroid Build Coastguard Worker ? 1 \ 102*635a8641SAndroid Build Coastguard Worker : 0; \ 103*635a8641SAndroid Build Coastguard Worker } \ 104*635a8641SAndroid Build Coastguard Worker void client_name##_MainFixture::Main() 105*635a8641SAndroid Build Coastguard Worker 106*635a8641SAndroid Build Coastguard Worker // Use this to declare the client side for tests using IPCChannelMojoTestBase. 107*635a8641SAndroid Build Coastguard Worker #define DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(client_name) \ 108*635a8641SAndroid Build Coastguard Worker DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE( \ 109*635a8641SAndroid Build Coastguard Worker client_name, IpcChannelMojoTestClient) 110*635a8641SAndroid Build Coastguard Worker 111*635a8641SAndroid Build Coastguard Worker #endif // IPC_IPC_TEST_BASE_H_ 112