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_
10 #include "base/basictypes.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_proxy.h"
16 #include "ipc/ipc_multiprocess_test.h"
19 class MessageLoopForIO
;
22 // A test fixture for multiprocess IPC tests. Such tests include a "client" side
23 // (running in a separate process). The same client may be shared between
24 // several different tests.
25 class IPCTestBase
: public base::MultiProcessTest
{
27 // The channel name is based on the client's name. This is a public static
28 // helper to be used by the client-side code; server-side test code should
29 // usually not use this (directly).
30 static std::string
GetChannelName(const std::string
& test_client_name
);
34 virtual ~IPCTestBase();
36 virtual void SetUp() OVERRIDE
;
37 virtual void TearDown() OVERRIDE
;
39 // Initializes the test to use the given client.
40 void Init(const std::string
& test_client_name
);
42 // Creates a channel with the given listener and connects to the channel
43 // (returning true if successful), respectively. Use these to use a channel
44 // directly. Since the listener must outlive the channel, you must destroy the
45 // channel before the listener gets destroyed.
46 void CreateChannel(IPC::Listener
* listener
);
47 bool ConnectChannel();
48 void DestroyChannel();
50 // Releases or replaces existing channel.
51 // These are useful for testing specific types of channel subclasses.
52 scoped_ptr
<IPC::Channel
> ReleaseChannel();
53 void SetChannel(scoped_ptr
<IPC::Channel
> channel
);
55 // Use this instead of CreateChannel() if you want to use some different
56 // channel specification (then use ConnectChannel() as usual).
57 void CreateChannelFromChannelHandle(const IPC::ChannelHandle
& channel_handle
,
58 IPC::Listener
* listener
);
60 // Creates a channel proxy with the given listener and task runner. (The
61 // channel proxy will automatically create and connect a channel.) You must
62 // (manually) destroy the channel proxy before the task runner's thread is
64 void CreateChannelProxy(IPC::Listener
* listener
,
65 base::SingleThreadTaskRunner
* ipc_task_runner
);
66 void DestroyChannelProxy();
68 // Starts the client process, returning true if successful; this should be
69 // done after connecting to the channel.
72 // Waits for the client to shut down, returning true if successful. Note that
73 // this does not initiate client shutdown; that must be done by the test
74 // (somehow). This must be called before the end of the test whenever
75 // StartClient() was called successfully.
76 bool WaitForClientShutdown();
78 // Use this to send IPC messages (when you don't care if you're using a
79 // channel or a proxy).
80 IPC::Sender
* sender() {
81 return channel_
.get() ? static_cast<IPC::Sender
*>(channel_
.get()) :
82 static_cast<IPC::Sender
*>(channel_proxy_
.get());
85 IPC::Channel
* channel() { return channel_
.get(); }
86 IPC::ChannelProxy
* channel_proxy() { return channel_proxy_
.get(); }
88 const base::ProcessHandle
& client_process() const { return client_process_
; }
89 scoped_refptr
<base::TaskRunner
> io_thread_task_runner();
92 std::string test_client_name_
;
93 scoped_ptr
<base::MessageLoopForIO
> message_loop_
;
95 scoped_ptr
<IPC::Channel
> channel_
;
96 scoped_ptr
<IPC::ChannelProxy
> channel_proxy_
;
98 base::ProcessHandle client_process_
;
100 DISALLOW_COPY_AND_ASSIGN(IPCTestBase
);
103 // Use this to declare the client side for tests using IPCTestBase.
104 #define MULTIPROCESS_IPC_TEST_CLIENT_MAIN(test_client_name) \
105 MULTIPROCESS_IPC_TEST_MAIN(test_client_name ## TestClientMain)
107 #endif // IPC_IPC_TEST_BASE_H_