Roll src/third_party/WebKit a3b4a2e:7441784 (svn 202551:202552)
[chromium-blink-merge.git] / ipc / ipc_test_base.h
blob59f5b8654035b2e1d110501daae37fc810cd6865
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
3 // found in the LICENSE file.
5 #ifndef IPC_IPC_TEST_BASE_H_
6 #define IPC_IPC_TEST_BASE_H_
8 #include <string>
10 #include "base/macros.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/process/process.h"
13 #include "base/test/multiprocess_test.h"
14 #include "ipc/ipc_channel.h"
15 #include "ipc/ipc_channel_factory.h"
16 #include "ipc/ipc_channel_proxy.h"
17 #include "ipc/ipc_multiprocess_test.h"
19 namespace base {
20 class MessageLoop;
23 namespace IPC {
24 class AttachmentBroker;
27 // A test fixture for multiprocess IPC tests. Such tests include a "client" side
28 // (running in a separate process). The same client may be shared between
29 // several different tests.
30 class IPCTestBase : public base::MultiProcessTest {
31 public:
32 // The channel name is based on the client's name. This is a public static
33 // helper to be used by the client-side code; server-side test code should
34 // usually not use this (directly).
35 static std::string GetChannelName(const std::string& test_client_name);
37 protected:
38 IPCTestBase();
39 ~IPCTestBase() override;
41 void TearDown() override;
43 // Initializes the test to use the given client and creates an IO message loop
44 // on the current thread.
45 void Init(const std::string& test_client_name);
46 // Some tests create separate thread for IO message loop and run non-IO
47 // message loop on the main thread. As IPCTestBase creates IO message loop by
48 // default, such tests need to provide a custom message loop for the main
49 // thread.
50 void InitWithCustomMessageLoop(const std::string& test_client_name,
51 scoped_ptr<base::MessageLoop> message_loop);
53 // Creates a channel with the given listener and connects to the channel
54 // (returning true if successful), respectively. Use these to use a channel
55 // directly. Since the listener must outlive the channel, you must destroy the
56 // channel before the listener gets destroyed.
57 void CreateChannel(IPC::Listener* listener);
58 bool ConnectChannel();
59 void DestroyChannel();
61 // Releases or replaces existing channel.
62 // These are useful for testing specific types of channel subclasses.
63 scoped_ptr<IPC::Channel> ReleaseChannel();
64 void SetChannel(scoped_ptr<IPC::Channel> channel);
66 // Use this instead of CreateChannel() if you want to use some different
67 // channel specification (then use ConnectChannel() as usual).
68 void CreateChannelFromChannelHandle(const IPC::ChannelHandle& channel_handle,
69 IPC::Listener* listener);
71 // Creates a channel proxy with the given listener and task runner. (The
72 // channel proxy will automatically create and connect a channel.) You must
73 // (manually) destroy the channel proxy before the task runner's thread is
74 // destroyed.
75 void CreateChannelProxy(
76 IPC::Listener* listener,
77 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner);
78 void DestroyChannelProxy();
80 // Starts the client process, returning true if successful; this should be
81 // done after connecting to the channel.
82 bool StartClient();
84 #if defined(OS_POSIX)
85 // A StartClient() variant that allows caller to pass the FD of IPC pipe
86 bool StartClientWithFD(int ipcfd);
87 #endif
89 // Waits for the client to shut down, returning true if successful. Note that
90 // this does not initiate client shutdown; that must be done by the test
91 // (somehow). This must be called before the end of the test whenever
92 // StartClient() was called successfully.
93 bool WaitForClientShutdown();
95 IPC::ChannelHandle GetTestChannelHandle();
97 // Use this to send IPC messages (when you don't care if you're using a
98 // channel or a proxy).
99 IPC::Sender* sender() {
100 return channel_.get() ? static_cast<IPC::Sender*>(channel_.get()) :
101 static_cast<IPC::Sender*>(channel_proxy_.get());
104 IPC::Channel* channel() { return channel_.get(); }
105 IPC::ChannelProxy* channel_proxy() { return channel_proxy_.get(); }
106 void set_attachment_broker(IPC::AttachmentBroker* broker) {
107 attachment_broker_ = broker;
110 const base::Process& client_process() const { return client_process_; }
111 scoped_refptr<base::SequencedTaskRunner> task_runner();
113 virtual scoped_ptr<IPC::ChannelFactory> CreateChannelFactory(
114 const IPC::ChannelHandle& handle, base::SequencedTaskRunner* runner);
116 virtual bool DidStartClient();
118 private:
119 std::string GetTestMainName() const;
121 std::string test_client_name_;
122 scoped_ptr<base::MessageLoop> message_loop_;
124 scoped_ptr<IPC::Channel> channel_;
125 scoped_ptr<IPC::ChannelProxy> channel_proxy_;
127 // The AttachmentBroker that should be passed to |channel_| when it is
128 // created.
129 IPC::AttachmentBroker* attachment_broker_;
131 base::Process client_process_;
133 DISALLOW_COPY_AND_ASSIGN(IPCTestBase);
136 // Use this to declare the client side for tests using IPCTestBase.
137 #define MULTIPROCESS_IPC_TEST_CLIENT_MAIN(test_client_name) \
138 MULTIPROCESS_IPC_TEST_MAIN(test_client_name ## TestClientMain)
140 #endif // IPC_IPC_TEST_BASE_H_