OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef IPC_IPC_TEST_BASE_H_ | 5 #ifndef IPC_IPC_TEST_BASE_H_ |
6 #define IPC_IPC_TEST_BASE_H_ | 6 #define IPC_IPC_TEST_BASE_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/process/process.h" | 12 #include "base/process/process.h" |
13 #include "base/test/multiprocess_test.h" | 13 #include "base/test/multiprocess_test.h" |
14 #include "build/build_config.h" | 14 #include "build/build_config.h" |
15 #include "ipc/ipc_channel.h" | 15 #include "ipc/ipc_channel.h" |
16 #include "ipc/ipc_channel_factory.h" | 16 #include "ipc/ipc_channel_factory.h" |
17 #include "ipc/ipc_channel_proxy.h" | 17 #include "ipc/ipc_channel_proxy.h" |
18 #include "ipc/ipc_multiprocess_test.h" | 18 #include "ipc/ipc_multiprocess_test.h" |
| 19 #include "mojo/edk/test/mojo_test_base.h" |
| 20 #include "mojo/edk/test/multiprocess_test_helper.h" |
19 | 21 |
20 namespace base { | 22 namespace base { |
21 class MessageLoop; | 23 class MessageLoop; |
22 } | 24 } |
23 | 25 |
24 namespace IPC { | 26 namespace IPC { |
25 class AttachmentBroker; | 27 class AttachmentBroker; |
26 } | 28 } |
27 | 29 |
28 // A test fixture for multiprocess IPC tests. Such tests include a "client" side | 30 // A test fixture for multiprocess IPC tests. Such tests include a "client" side |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 std::unique_ptr<base::MessageLoop> message_loop_; | 128 std::unique_ptr<base::MessageLoop> message_loop_; |
127 | 129 |
128 std::unique_ptr<IPC::Channel> channel_; | 130 std::unique_ptr<IPC::Channel> channel_; |
129 std::unique_ptr<IPC::ChannelProxy> channel_proxy_; | 131 std::unique_ptr<IPC::ChannelProxy> channel_proxy_; |
130 | 132 |
131 base::Process client_process_; | 133 base::Process client_process_; |
132 | 134 |
133 DISALLOW_COPY_AND_ASSIGN(IPCTestBase); | 135 DISALLOW_COPY_AND_ASSIGN(IPCTestBase); |
134 }; | 136 }; |
135 | 137 |
| 138 class IPCChannelMojoTestBase : public testing::Test { |
| 139 public: |
| 140 IPCChannelMojoTestBase(); |
| 141 ~IPCChannelMojoTestBase() override; |
| 142 |
| 143 void Init(const std::string& test_client_name); |
| 144 |
| 145 bool WaitForClientShutdown(); |
| 146 |
| 147 void TearDown() override; |
| 148 |
| 149 void CreateChannel(IPC::Listener* listener); |
| 150 |
| 151 bool ConnectChannel(); |
| 152 |
| 153 void DestroyChannel(); |
| 154 |
| 155 IPC::Sender* sender() { return channel(); } |
| 156 IPC::Channel* channel() { return channel_.get(); } |
| 157 const base::Process& client_process() const { return helper_.test_child(); } |
| 158 |
| 159 protected: |
| 160 mojo::ScopedMessagePipeHandle TakeHandle(); |
| 161 |
| 162 private: |
| 163 base::MessageLoop message_loop_; |
| 164 |
| 165 mojo::ScopedMessagePipeHandle handle_; |
| 166 mojo::edk::test::MultiprocessTestHelper helper_; |
| 167 |
| 168 std::unique_ptr<IPC::Channel> channel_; |
| 169 |
| 170 DISALLOW_COPY_AND_ASSIGN(IPCChannelMojoTestBase); |
| 171 }; |
| 172 |
| 173 class IpcChannelMojoTestClient { |
| 174 public: |
| 175 IpcChannelMojoTestClient(); |
| 176 ~IpcChannelMojoTestClient(); |
| 177 |
| 178 void Init(mojo::ScopedMessagePipeHandle handle); |
| 179 |
| 180 void Connect(IPC::Listener* listener); |
| 181 |
| 182 void Close(); |
| 183 |
| 184 IPC::Channel* channel() const { return channel_.get(); } |
| 185 |
| 186 private: |
| 187 base::MessageLoopForIO main_message_loop_; |
| 188 mojo::ScopedMessagePipeHandle handle_; |
| 189 std::unique_ptr<IPC::Channel> channel_; |
| 190 }; |
| 191 |
136 // Use this to declare the client side for tests using IPCTestBase. | 192 // Use this to declare the client side for tests using IPCTestBase. |
137 #define MULTIPROCESS_IPC_TEST_CLIENT_MAIN(test_client_name) \ | 193 #define MULTIPROCESS_IPC_TEST_CLIENT_MAIN(test_client_name) \ |
138 MULTIPROCESS_IPC_TEST_MAIN(test_client_name ## TestClientMain) | 194 MULTIPROCESS_IPC_TEST_MAIN(test_client_name ## TestClientMain) |
139 | 195 |
| 196 // Use this to declare the client side for tests using IPCChannelMojoTestBase |
| 197 // when a custom test fixture class is required in the client. |test_base| must |
| 198 // be derived from IpcChannelMojoTestClient. |
| 199 #define DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE(client_name, \ |
| 200 test_base) \ |
| 201 class client_name##_MainFixture : public test_base { \ |
| 202 public: \ |
| 203 void Main(); \ |
| 204 }; \ |
| 205 MULTIPROCESS_TEST_MAIN_WITH_SETUP( \ |
| 206 client_name##TestChildMain, \ |
| 207 ::mojo::edk::test::MultiprocessTestHelper::ChildSetup) { \ |
| 208 client_name##_MainFixture test; \ |
| 209 test.Init( \ |
| 210 std::move(mojo::edk::test::MultiprocessTestHelper::primordial_pipe)); \ |
| 211 test.Main(); \ |
| 212 return (::testing::Test::HasFatalFailure() || \ |
| 213 ::testing::Test::HasNonfatalFailure()) \ |
| 214 ? 1 \ |
| 215 : 0; \ |
| 216 } \ |
| 217 void client_name##_MainFixture::Main() |
| 218 |
| 219 // Use this to declare the client side for tests using IPCChannelMojoTestBase. |
| 220 #define DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(client_name) \ |
| 221 DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE( \ |
| 222 client_name, IpcChannelMojoTestClient) |
| 223 |
140 #endif // IPC_IPC_TEST_BASE_H_ | 224 #endif // IPC_IPC_TEST_BASE_H_ |
OLD | NEW |