OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "build/build_config.h" | 5 #include "build/build_config.h" |
6 | 6 |
7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
8 #include <windows.h> | 8 #include <windows.h> |
9 #endif | 9 #endif |
10 | 10 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 EXPECT_TRUE(iter.ReadString(&big_string)); | 58 EXPECT_TRUE(iter.ReadString(&big_string)); |
59 EXPECT_EQ(kLongMessageStringNumBytes - 1, big_string.length()); | 59 EXPECT_EQ(kLongMessageStringNumBytes - 1, big_string.length()); |
60 | 60 |
61 SendNextMessage(); | 61 SendNextMessage(); |
62 return true; | 62 return true; |
63 } | 63 } |
64 | 64 |
65 virtual void OnChannelError() OVERRIDE { | 65 virtual void OnChannelError() OVERRIDE { |
66 // There is a race when closing the channel so the last message may be lost. | 66 // There is a race when closing the channel so the last message may be lost. |
67 EXPECT_LE(messages_left_, 1); | 67 EXPECT_LE(messages_left_, 1); |
68 MessageLoop::current()->Quit(); | 68 base::MessageLoop::current()->Quit(); |
69 } | 69 } |
70 | 70 |
71 void Init(IPC::Sender* s) { | 71 void Init(IPC::Sender* s) { |
72 sender_ = s; | 72 sender_ = s; |
73 } | 73 } |
74 | 74 |
75 protected: | 75 protected: |
76 void SendNextMessage() { | 76 void SendNextMessage() { |
77 if (--messages_left_ <= 0) | 77 if (--messages_left_ <= 0) |
78 MessageLoop::current()->Quit(); | 78 base::MessageLoop::current()->Quit(); |
79 else | 79 else |
80 Send(sender_, "Foo"); | 80 Send(sender_, "Foo"); |
81 } | 81 } |
82 | 82 |
83 private: | 83 private: |
84 IPC::Sender* sender_; | 84 IPC::Sender* sender_; |
85 int messages_left_; | 85 int messages_left_; |
86 }; | 86 }; |
87 | 87 |
88 class IPCChannelTest : public IPCTestBase { | 88 class IPCChannelTest : public IPCTestBase { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 // Set up IPC channel and start client. | 126 // Set up IPC channel and start client. |
127 GenericChannelListener listener; | 127 GenericChannelListener listener; |
128 CreateChannel(&listener); | 128 CreateChannel(&listener); |
129 listener.Init(sender()); | 129 listener.Init(sender()); |
130 ASSERT_TRUE(ConnectChannel()); | 130 ASSERT_TRUE(ConnectChannel()); |
131 ASSERT_TRUE(StartClient()); | 131 ASSERT_TRUE(StartClient()); |
132 | 132 |
133 Send(sender(), "hello from parent"); | 133 Send(sender(), "hello from parent"); |
134 | 134 |
135 // Run message loop. | 135 // Run message loop. |
136 MessageLoop::current()->Run(); | 136 base::MessageLoop::current()->Run(); |
137 | 137 |
138 // Close the channel so the client's OnChannelError() gets fired. | 138 // Close the channel so the client's OnChannelError() gets fired. |
139 channel()->Close(); | 139 channel()->Close(); |
140 | 140 |
141 EXPECT_TRUE(WaitForClientShutdown()); | 141 EXPECT_TRUE(WaitForClientShutdown()); |
142 DestroyChannel(); | 142 DestroyChannel(); |
143 } | 143 } |
144 | 144 |
145 // TODO(viettrungluu): Move to a separate IPCChannelWinTest. | 145 // TODO(viettrungluu): Move to a separate IPCChannelWinTest. |
146 #if defined(OS_WIN) | 146 #if defined(OS_WIN) |
(...skipping 18 matching lines...) Expand all Loading... |
165 CloseHandle(pipe); // The channel duplicates the handle. | 165 CloseHandle(pipe); // The channel duplicates the handle. |
166 listener.Init(sender()); | 166 listener.Init(sender()); |
167 | 167 |
168 // Connect to channel and start client. | 168 // Connect to channel and start client. |
169 ASSERT_TRUE(ConnectChannel()); | 169 ASSERT_TRUE(ConnectChannel()); |
170 ASSERT_TRUE(StartClient()); | 170 ASSERT_TRUE(StartClient()); |
171 | 171 |
172 Send(sender(), "hello from parent"); | 172 Send(sender(), "hello from parent"); |
173 | 173 |
174 // Run message loop. | 174 // Run message loop. |
175 MessageLoop::current()->Run(); | 175 base::MessageLoop::current()->Run(); |
176 | 176 |
177 // Close the channel so the client's OnChannelError() gets fired. | 177 // Close the channel so the client's OnChannelError() gets fired. |
178 channel()->Close(); | 178 channel()->Close(); |
179 | 179 |
180 EXPECT_TRUE(WaitForClientShutdown()); | 180 EXPECT_TRUE(WaitForClientShutdown()); |
181 DestroyChannel(); | 181 DestroyChannel(); |
182 } | 182 } |
183 #endif // defined (OS_WIN) | 183 #endif // defined (OS_WIN) |
184 | 184 |
185 TEST_F(IPCChannelTest, ChannelProxyTest) { | 185 TEST_F(IPCChannelTest, ChannelProxyTest) { |
186 Init("GenericClient"); | 186 Init("GenericClient"); |
187 | 187 |
188 base::Thread thread("ChannelProxyTestServer"); | 188 base::Thread thread("ChannelProxyTestServer"); |
189 base::Thread::Options options; | 189 base::Thread::Options options; |
190 options.message_loop_type = MessageLoop::TYPE_IO; | 190 options.message_loop_type = base::MessageLoop::TYPE_IO; |
191 thread.StartWithOptions(options); | 191 thread.StartWithOptions(options); |
192 | 192 |
193 // Set up IPC channel proxy. | 193 // Set up IPC channel proxy. |
194 GenericChannelListener listener; | 194 GenericChannelListener listener; |
195 CreateChannelProxy(&listener, thread.message_loop_proxy()); | 195 CreateChannelProxy(&listener, thread.message_loop_proxy()); |
196 listener.Init(sender()); | 196 listener.Init(sender()); |
197 | 197 |
198 ASSERT_TRUE(StartClient()); | 198 ASSERT_TRUE(StartClient()); |
199 | 199 |
200 Send(sender(), "hello from parent"); | 200 Send(sender(), "hello from parent"); |
201 | 201 |
202 // Run message loop. | 202 // Run message loop. |
203 MessageLoop::current()->Run(); | 203 base::MessageLoop::current()->Run(); |
204 | 204 |
205 EXPECT_TRUE(WaitForClientShutdown()); | 205 EXPECT_TRUE(WaitForClientShutdown()); |
206 | 206 |
207 // Destroy the channel proxy before shutting down the thread. | 207 // Destroy the channel proxy before shutting down the thread. |
208 DestroyChannelProxy(); | 208 DestroyChannelProxy(); |
209 thread.Stop(); | 209 thread.Stop(); |
210 } | 210 } |
211 | 211 |
212 class ChannelListenerWithOnConnectedSend : public GenericChannelListener { | 212 class ChannelListenerWithOnConnectedSend : public GenericChannelListener { |
213 public: | 213 public: |
(...skipping 19 matching lines...) Expand all Loading... |
233 // Set up IPC channel and start client. | 233 // Set up IPC channel and start client. |
234 ChannelListenerWithOnConnectedSend listener; | 234 ChannelListenerWithOnConnectedSend listener; |
235 CreateChannel(&listener); | 235 CreateChannel(&listener); |
236 listener.Init(sender()); | 236 listener.Init(sender()); |
237 ASSERT_TRUE(ConnectChannel()); | 237 ASSERT_TRUE(ConnectChannel()); |
238 ASSERT_TRUE(StartClient()); | 238 ASSERT_TRUE(StartClient()); |
239 | 239 |
240 Send(sender(), "hello from parent"); | 240 Send(sender(), "hello from parent"); |
241 | 241 |
242 // Run message loop. | 242 // Run message loop. |
243 MessageLoop::current()->Run(); | 243 base::MessageLoop::current()->Run(); |
244 | 244 |
245 // Close the channel so the client's OnChannelError() gets fired. | 245 // Close the channel so the client's OnChannelError() gets fired. |
246 channel()->Close(); | 246 channel()->Close(); |
247 | 247 |
248 EXPECT_TRUE(WaitForClientShutdown()); | 248 EXPECT_TRUE(WaitForClientShutdown()); |
249 DestroyChannel(); | 249 DestroyChannel(); |
250 } | 250 } |
251 | 251 |
252 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(GenericClient) { | 252 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(GenericClient) { |
253 MessageLoopForIO main_message_loop; | 253 base::MessageLoopForIO main_message_loop; |
254 GenericChannelListener listener; | 254 GenericChannelListener listener; |
255 | 255 |
256 // Set up IPC channel. | 256 // Set up IPC channel. |
257 IPC::Channel channel(IPCTestBase::GetChannelName("GenericClient"), | 257 IPC::Channel channel(IPCTestBase::GetChannelName("GenericClient"), |
258 IPC::Channel::MODE_CLIENT, | 258 IPC::Channel::MODE_CLIENT, |
259 &listener); | 259 &listener); |
260 CHECK(channel.Connect()); | 260 CHECK(channel.Connect()); |
261 listener.Init(&channel); | 261 listener.Init(&channel); |
262 Send(&channel, "hello from child"); | 262 Send(&channel, "hello from child"); |
263 | 263 |
264 MessageLoop::current()->Run(); | 264 base::MessageLoop::current()->Run(); |
265 return 0; | 265 return 0; |
266 } | 266 } |
267 | 267 |
268 } // namespace | 268 } // namespace |
OLD | NEW |