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 <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 public: | 26 public: |
27 // The channel name is based on the client's name. This is a public static | 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 | 28 // helper to be used by the client-side code; server-side test code should |
29 // usually not use this (directly). | 29 // usually not use this (directly). |
30 static std::string GetChannelName(const std::string& test_client_name); | 30 static std::string GetChannelName(const std::string& test_client_name); |
31 | 31 |
32 protected: | 32 protected: |
33 IPCTestBase(); | 33 IPCTestBase(); |
34 virtual ~IPCTestBase(); | 34 virtual ~IPCTestBase(); |
35 | 35 |
36 virtual void SetUp() OVERRIDE; | |
37 virtual void TearDown() OVERRIDE; | 36 virtual void TearDown() OVERRIDE; |
38 | 37 |
39 // Initializes the test to use the given client. | 38 // Initializes the test to use the given client and creates an IO message loop |
| 39 // on the current thread. |
40 void Init(const std::string& test_client_name); | 40 void Init(const std::string& test_client_name); |
| 41 // Some tests create separate thread for IO message loop and run non-IO |
| 42 // message loop on the main thread. As IPCTestBase creates IO message loop by |
| 43 // default, such tests need to provide a custom message loop for the main |
| 44 // thread. |
| 45 void InitWithCustomMessageLoop(const std::string& test_client_name, |
| 46 scoped_ptr<base::MessageLoop> message_loop); |
41 | 47 |
42 // Creates a channel with the given listener and connects to the channel | 48 // Creates a channel with the given listener and connects to the channel |
43 // (returning true if successful), respectively. Use these to use a channel | 49 // (returning true if successful), respectively. Use these to use a channel |
44 // directly. Since the listener must outlive the channel, you must destroy the | 50 // directly. Since the listener must outlive the channel, you must destroy the |
45 // channel before the listener gets destroyed. | 51 // channel before the listener gets destroyed. |
46 void CreateChannel(IPC::Listener* listener); | 52 void CreateChannel(IPC::Listener* listener); |
47 bool ConnectChannel(); | 53 bool ConnectChannel(); |
48 void DestroyChannel(); | 54 void DestroyChannel(); |
49 | 55 |
50 // Releases or replaces existing channel. | 56 // Releases or replaces existing channel. |
(...skipping 30 matching lines...) Expand all Loading... |
81 return channel_.get() ? static_cast<IPC::Sender*>(channel_.get()) : | 87 return channel_.get() ? static_cast<IPC::Sender*>(channel_.get()) : |
82 static_cast<IPC::Sender*>(channel_proxy_.get()); | 88 static_cast<IPC::Sender*>(channel_proxy_.get()); |
83 } | 89 } |
84 | 90 |
85 IPC::Channel* channel() { return channel_.get(); } | 91 IPC::Channel* channel() { return channel_.get(); } |
86 IPC::ChannelProxy* channel_proxy() { return channel_proxy_.get(); } | 92 IPC::ChannelProxy* channel_proxy() { return channel_proxy_.get(); } |
87 | 93 |
88 const base::ProcessHandle& client_process() const { return client_process_; } | 94 const base::ProcessHandle& client_process() const { return client_process_; } |
89 scoped_refptr<base::TaskRunner> task_runner(); | 95 scoped_refptr<base::TaskRunner> task_runner(); |
90 | 96 |
91 // Some tests creates separate thread for IO message loop and Run | |
92 // non-IO message loop on the main thread. As IPCTestBase creates IO | |
93 // message loop by default, such tests might want to replace it with | |
94 // non-IO one using set_message_loop(). | |
95 // | |
96 // The replacement should be done at the beginning of the test. | |
97 // You should be careful not to replace "live" message loop that has | |
98 // started running. | |
99 void set_message_loop(scoped_ptr<base::MessageLoop> loop); | |
100 | |
101 private: | 97 private: |
102 std::string test_client_name_; | 98 std::string test_client_name_; |
103 scoped_ptr<base::MessageLoop> message_loop_; | 99 scoped_ptr<base::MessageLoop> message_loop_; |
104 | 100 |
105 scoped_ptr<IPC::Channel> channel_; | 101 scoped_ptr<IPC::Channel> channel_; |
106 scoped_ptr<IPC::ChannelProxy> channel_proxy_; | 102 scoped_ptr<IPC::ChannelProxy> channel_proxy_; |
107 | 103 |
108 base::ProcessHandle client_process_; | 104 base::ProcessHandle client_process_; |
109 | 105 |
110 DISALLOW_COPY_AND_ASSIGN(IPCTestBase); | 106 DISALLOW_COPY_AND_ASSIGN(IPCTestBase); |
111 }; | 107 }; |
112 | 108 |
113 // Use this to declare the client side for tests using IPCTestBase. | 109 // Use this to declare the client side for tests using IPCTestBase. |
114 #define MULTIPROCESS_IPC_TEST_CLIENT_MAIN(test_client_name) \ | 110 #define MULTIPROCESS_IPC_TEST_CLIENT_MAIN(test_client_name) \ |
115 MULTIPROCESS_IPC_TEST_MAIN(test_client_name ## TestClientMain) | 111 MULTIPROCESS_IPC_TEST_MAIN(test_client_name ## TestClientMain) |
116 | 112 |
117 #endif // IPC_IPC_TEST_BASE_H_ | 113 #endif // IPC_IPC_TEST_BASE_H_ |
OLD | NEW |