blob: 2555611c959698a08940b0abb2c62f16005bbf79 [file] [log] [blame]
avi@chromium.org2a026e52011-11-17 16:09:441// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
viettrungluu@chromium.org0cb7d8c82013-01-11 15:13:375#ifndef IPC_IPC_TEST_BASE_H_
6#define IPC_IPC_TEST_BASE_H_
initial.commit09911bf2008-07-26 23:55:297
danakj03de39b22016-04-23 04:21:098#include <memory>
viettrungluu@chromium.org3c788582013-01-25 21:51:359#include <string>
10
tfarina4da8275d2015-09-16 09:56:2111#include "base/macros.h"
Alexander Timin4f9c35c2018-11-01 20:15:2012#include "base/message_loop/message_loop.h"
rsesek@chromium.orge66ef602013-07-24 05:15:2413#include "base/process/process.h"
viettrungluu@chromium.org0cb7d8c82013-01-11 15:13:3714#include "base/test/multiprocess_test.h"
avi246998d82015-12-22 02:39:0415#include "build/build_config.h"
viettrungluu@chromium.org3c788582013-01-25 21:51:3516#include "ipc/ipc_channel.h"
morrita373af03b2014-09-09 19:35:2417#include "ipc/ipc_channel_factory.h"
viettrungluu@chromium.org3c788582013-01-25 21:51:3518#include "ipc/ipc_channel_proxy.h"
Ken Rockot8a7f35f2018-07-04 19:40:5619#include "mojo/core/test/mojo_test_base.h"
20#include "mojo/core/test/multiprocess_test_helper.h"
initial.commit09911bf2008-07-26 23:55:2921
brettw@chromium.org5e9e96a2013-03-31 02:29:2022namespace base {
morritad36736c2014-08-29 00:20:5923class MessageLoop;
brettw@chromium.org5e9e96a2013-03-31 02:29:2024}
initial.commit09911bf2008-07-26 23:55:2925
sammc4bcc4ed62016-10-27 10:13:5926class IPCChannelMojoTestBase : public testing::Test {
27 public:
28 IPCChannelMojoTestBase();
29 ~IPCChannelMojoTestBase() override;
30
31 void Init(const std::string& test_client_name);
sammcbeeed3682016-11-08 23:24:3532 void InitWithCustomMessageLoop(
33 const std::string& test_client_name,
34 std::unique_ptr<base::MessageLoop> message_loop);
sammc4bcc4ed62016-10-27 10:13:5935
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:
sammcbeeed3682016-11-08 23:24:3554 std::unique_ptr<base::MessageLoop> message_loop_;
sammc4bcc4ed62016-10-27 10:13:5955
56 mojo::ScopedMessagePipeHandle handle_;
Ken Rockot8a7f35f2018-07-04 19:40:5657 mojo::core::test::MultiprocessTestHelper helper_;
sammc4bcc4ed62016-10-27 10:13:5958
59 std::unique_ptr<IPC::Channel> channel_;
60
61 DISALLOW_COPY_AND_ASSIGN(IPCChannelMojoTestBase);
62};
63
64class 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
sammc4bcc4ed62016-10-27 10:13:5983// 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.
Ken Rockot8a7f35f2018-07-04 19:40:5686#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::core::test::MultiprocessTestHelper::ChildSetup) { \
95 client_name##_MainFixture test; \
96 test.Init( \
97 std::move(mojo::core::test::MultiprocessTestHelper::primordial_pipe)); \
98 test.Main(); \
99 return (::testing::Test::HasFatalFailure() || \
100 ::testing::Test::HasNonfatalFailure()) \
101 ? 1 \
102 : 0; \
103 } \
sammc4bcc4ed62016-10-27 10:13:59104 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.org0cb7d8c82013-01-11 15:13:37111#endif // IPC_IPC_TEST_BASE_H_