avi@chromium.org | 362c8a8 | 2011-11-18 01:09:44 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
viettrungluu@chromium.org | 7d86af2 | 2013-01-12 00:13:37 +0900 | [diff] [blame] | 5 | #ifndef IPC_IPC_TEST_BASE_H_ |
| 6 | #define IPC_IPC_TEST_BASE_H_ |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 7 | |
danakj | c3fb6c5 | 2016-04-23 13:21:09 +0900 | [diff] [blame] | 8 | #include <memory> |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 9 | #include <string> |
| 10 | |
tfarina | 2d565d3 | 2015-09-16 18:56:21 +0900 | [diff] [blame] | 11 | #include "base/macros.h" |
sammc | 959d1d9 | 2017-01-31 07:07:33 +0900 | [diff] [blame] | 12 | #include "base/message_loop/message_loop.h" |
rsesek@chromium.org | 1931971 | 2013-07-24 14:15:24 +0900 | [diff] [blame] | 13 | #include "base/process/process.h" |
viettrungluu@chromium.org | 7d86af2 | 2013-01-12 00:13:37 +0900 | [diff] [blame] | 14 | #include "base/test/multiprocess_test.h" |
avi | 42ebda4 | 2015-12-22 11:39:04 +0900 | [diff] [blame] | 15 | #include "build/build_config.h" |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 16 | #include "ipc/ipc_channel.h" |
morrita | ab77970 | 2014-09-10 04:35:24 +0900 | [diff] [blame] | 17 | #include "ipc/ipc_channel_factory.h" |
viettrungluu@chromium.org | 0015594 | 2013-01-26 06:51:35 +0900 | [diff] [blame] | 18 | #include "ipc/ipc_channel_proxy.h" |
sammc | e3cae21 | 2016-10-27 19:13:59 +0900 | [diff] [blame] | 19 | #include "mojo/edk/test/mojo_test_base.h" |
| 20 | #include "mojo/edk/test/multiprocess_test_helper.h" |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 21 | |
brettw@chromium.org | 8799428 | 2013-03-31 11:29:20 +0900 | [diff] [blame] | 22 | namespace base { |
morrita | a422094 | 2014-08-29 09:20:59 +0900 | [diff] [blame] | 23 | class MessageLoop; |
brettw@chromium.org | 8799428 | 2013-03-31 11:29:20 +0900 | [diff] [blame] | 24 | } |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 25 | |
sammc | e3cae21 | 2016-10-27 19:13:59 +0900 | [diff] [blame] | 26 | class IPCChannelMojoTestBase : public testing::Test { |
| 27 | public: |
| 28 | IPCChannelMojoTestBase(); |
| 29 | ~IPCChannelMojoTestBase() override; |
| 30 | |
| 31 | void Init(const std::string& test_client_name); |
sammc | c54b4e9 | 2016-11-09 08:24:35 +0900 | [diff] [blame] | 32 | void InitWithCustomMessageLoop( |
| 33 | const std::string& test_client_name, |
| 34 | std::unique_ptr<base::MessageLoop> message_loop); |
sammc | e3cae21 | 2016-10-27 19:13:59 +0900 | [diff] [blame] | 35 | |
| 36 | bool WaitForClientShutdown(); |
| 37 | |
| 38 | void TearDown() override; |
| 39 | |
| 40 | void CreateChannel(IPC::Listener* listener); |
| 41 | |
| 42 | bool ConnectChannel(); |
| 43 | |
| 44 | void DestroyChannel(); |
| 45 | |
| 46 | IPC::Sender* sender() { return channel(); } |
| 47 | IPC::Channel* channel() { return channel_.get(); } |
| 48 | const base::Process& client_process() const { return helper_.test_child(); } |
| 49 | |
| 50 | protected: |
| 51 | mojo::ScopedMessagePipeHandle TakeHandle(); |
| 52 | |
| 53 | private: |
sammc | c54b4e9 | 2016-11-09 08:24:35 +0900 | [diff] [blame] | 54 | std::unique_ptr<base::MessageLoop> message_loop_; |
sammc | e3cae21 | 2016-10-27 19:13:59 +0900 | [diff] [blame] | 55 | |
| 56 | mojo::ScopedMessagePipeHandle handle_; |
| 57 | mojo::edk::test::MultiprocessTestHelper helper_; |
| 58 | |
| 59 | std::unique_ptr<IPC::Channel> channel_; |
| 60 | |
| 61 | DISALLOW_COPY_AND_ASSIGN(IPCChannelMojoTestBase); |
| 62 | }; |
| 63 | |
| 64 | class IpcChannelMojoTestClient { |
| 65 | public: |
| 66 | IpcChannelMojoTestClient(); |
| 67 | ~IpcChannelMojoTestClient(); |
| 68 | |
| 69 | void Init(mojo::ScopedMessagePipeHandle handle); |
| 70 | |
| 71 | void Connect(IPC::Listener* listener); |
| 72 | |
| 73 | void Close(); |
| 74 | |
| 75 | IPC::Channel* channel() const { return channel_.get(); } |
| 76 | |
| 77 | private: |
| 78 | base::MessageLoopForIO main_message_loop_; |
| 79 | mojo::ScopedMessagePipeHandle handle_; |
| 80 | std::unique_ptr<IPC::Channel> channel_; |
| 81 | }; |
| 82 | |
sammc | e3cae21 | 2016-10-27 19:13:59 +0900 | [diff] [blame] | 83 | // Use this to declare the client side for tests using IPCChannelMojoTestBase |
| 84 | // when a custom test fixture class is required in the client. |test_base| must |
| 85 | // be derived from IpcChannelMojoTestClient. |
| 86 | #define DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE(client_name, \ |
| 87 | test_base) \ |
| 88 | class client_name##_MainFixture : public test_base { \ |
| 89 | public: \ |
| 90 | void Main(); \ |
| 91 | }; \ |
| 92 | MULTIPROCESS_TEST_MAIN_WITH_SETUP( \ |
| 93 | client_name##TestChildMain, \ |
| 94 | ::mojo::edk::test::MultiprocessTestHelper::ChildSetup) { \ |
| 95 | client_name##_MainFixture test; \ |
| 96 | test.Init( \ |
| 97 | std::move(mojo::edk::test::MultiprocessTestHelper::primordial_pipe)); \ |
| 98 | test.Main(); \ |
| 99 | return (::testing::Test::HasFatalFailure() || \ |
| 100 | ::testing::Test::HasNonfatalFailure()) \ |
| 101 | ? 1 \ |
| 102 | : 0; \ |
| 103 | } \ |
| 104 | void client_name##_MainFixture::Main() |
| 105 | |
| 106 | // Use this to declare the client side for tests using IPCChannelMojoTestBase. |
| 107 | #define DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(client_name) \ |
| 108 | DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE( \ |
| 109 | client_name, IpcChannelMojoTestClient) |
| 110 | |
viettrungluu@chromium.org | 7d86af2 | 2013-01-12 00:13:37 +0900 | [diff] [blame] | 111 | #endif // IPC_IPC_TEST_BASE_H_ |