blob: ae69f104daccaefe220dc5f7bf10a3764f5792f9 [file] [log] [blame]
avi@chromium.org362c8a82011-11-18 01:09:44 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
agl@chromium.org1c6dcf22009-07-23 08:57:21 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
viettrungluu@chromium.org7d86af22013-01-12 00:13:37 +09005#ifndef IPC_IPC_TEST_BASE_H_
6#define IPC_IPC_TEST_BASE_H_
agl@chromium.org1c6dcf22009-07-23 08:57:21 +09007
danakjc3fb6c52016-04-23 13:21:09 +09008#include <memory>
viettrungluu@chromium.org00155942013-01-26 06:51:35 +09009#include <string>
10
tfarina2d565d32015-09-16 18:56:21 +090011#include "base/macros.h"
sammc959d1d92017-01-31 07:07:33 +090012#include "base/message_loop/message_loop.h"
rsesek@chromium.org19319712013-07-24 14:15:24 +090013#include "base/process/process.h"
viettrungluu@chromium.org7d86af22013-01-12 00:13:37 +090014#include "base/test/multiprocess_test.h"
avi42ebda42015-12-22 11:39:04 +090015#include "build/build_config.h"
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090016#include "ipc/ipc_channel.h"
morritaab779702014-09-10 04:35:24 +090017#include "ipc/ipc_channel_factory.h"
viettrungluu@chromium.org00155942013-01-26 06:51:35 +090018#include "ipc/ipc_channel_proxy.h"
sammce3cae212016-10-27 19:13:59 +090019#include "mojo/edk/test/mojo_test_base.h"
20#include "mojo/edk/test/multiprocess_test_helper.h"
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090021
brettw@chromium.org87994282013-03-31 11:29:20 +090022namespace base {
morritaa4220942014-08-29 09:20:59 +090023class MessageLoop;
brettw@chromium.org87994282013-03-31 11:29:20 +090024}
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090025
sammce3cae212016-10-27 19:13:59 +090026class IPCChannelMojoTestBase : public testing::Test {
27 public:
28 IPCChannelMojoTestBase();
29 ~IPCChannelMojoTestBase() override;
30
31 void Init(const std::string& test_client_name);
sammcc54b4e92016-11-09 08:24:35 +090032 void InitWithCustomMessageLoop(
33 const std::string& test_client_name,
34 std::unique_ptr<base::MessageLoop> message_loop);
sammce3cae212016-10-27 19:13:59 +090035
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:
sammcc54b4e92016-11-09 08:24:35 +090054 std::unique_ptr<base::MessageLoop> message_loop_;
sammce3cae212016-10-27 19:13:59 +090055
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
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
sammce3cae212016-10-27 19:13:59 +090083// 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.org7d86af22013-01-12 00:13:37 +0900111#endif // IPC_IPC_TEST_BASE_H_