OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <stdint.h> | 5 #include <stdint.h> |
6 #include <utility> | 6 #include <utility> |
7 | 7 |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "mojo/public/cpp/bindings/binding.h" | 10 #include "mojo/public/cpp/bindings/binding.h" |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 std::string* got_text_reply, | 193 std::string* got_text_reply, |
194 const base::Closure& closure, | 194 const base::Closure& closure, |
195 const std::string& text_reply) { | 195 const std::string& text_reply) { |
196 *got_response = true; | 196 *got_response = true; |
197 *got_text_reply = text_reply; | 197 *got_text_reply = text_reply; |
198 closure.Run(); | 198 closure.Run(); |
199 } | 199 } |
200 | 200 |
201 TEST_F(HandlePassingTest, Basic) { | 201 TEST_F(HandlePassingTest, Basic) { |
202 sample::FactoryPtr factory; | 202 sample::FactoryPtr factory; |
203 SampleFactoryImpl factory_impl(GetProxy(&factory)); | 203 SampleFactoryImpl factory_impl(MakeRequest(&factory)); |
204 | 204 |
205 MessagePipe pipe0; | 205 MessagePipe pipe0; |
206 EXPECT_TRUE(WriteTextMessage(pipe0.handle1.get(), kText1)); | 206 EXPECT_TRUE(WriteTextMessage(pipe0.handle1.get(), kText1)); |
207 | 207 |
208 MessagePipe pipe1; | 208 MessagePipe pipe1; |
209 EXPECT_TRUE(WriteTextMessage(pipe1.handle1.get(), kText2)); | 209 EXPECT_TRUE(WriteTextMessage(pipe1.handle1.get(), kText2)); |
210 | 210 |
211 imported::ImportedInterfacePtr imported; | 211 imported::ImportedInterfacePtr imported; |
212 base::RunLoop run_loop; | 212 base::RunLoop run_loop; |
213 ImportedInterfaceImpl imported_impl(GetProxy(&imported), | 213 ImportedInterfaceImpl imported_impl(MakeRequest(&imported), |
214 run_loop.QuitClosure()); | 214 run_loop.QuitClosure()); |
215 | 215 |
216 sample::RequestPtr request(sample::Request::New()); | 216 sample::RequestPtr request(sample::Request::New()); |
217 request->x = 1; | 217 request->x = 1; |
218 request->pipe = std::move(pipe1.handle0); | 218 request->pipe = std::move(pipe1.handle0); |
219 request->obj = std::move(imported); | 219 request->obj = std::move(imported); |
220 bool got_response = false; | 220 bool got_response = false; |
221 std::string got_text_reply; | 221 std::string got_text_reply; |
222 base::RunLoop run_loop2; | 222 base::RunLoop run_loop2; |
223 factory->DoStuff(std::move(request), std::move(pipe0.handle0), | 223 factory->DoStuff(std::move(request), std::move(pipe0.handle0), |
224 base::Bind(&DoStuff, &got_response, &got_text_reply, | 224 base::Bind(&DoStuff, &got_response, &got_text_reply, |
225 run_loop2.QuitClosure())); | 225 run_loop2.QuitClosure())); |
226 | 226 |
227 EXPECT_FALSE(got_response); | 227 EXPECT_FALSE(got_response); |
228 int count_before = ImportedInterfaceImpl::do_something_count(); | 228 int count_before = ImportedInterfaceImpl::do_something_count(); |
229 | 229 |
230 run_loop.Run(); | 230 run_loop.Run(); |
231 run_loop2.Run(); | 231 run_loop2.Run(); |
232 | 232 |
233 EXPECT_TRUE(got_response); | 233 EXPECT_TRUE(got_response); |
234 EXPECT_EQ(kText1, got_text_reply); | 234 EXPECT_EQ(kText1, got_text_reply); |
235 EXPECT_EQ(1, ImportedInterfaceImpl::do_something_count() - count_before); | 235 EXPECT_EQ(1, ImportedInterfaceImpl::do_something_count() - count_before); |
236 } | 236 } |
237 | 237 |
238 TEST_F(HandlePassingTest, PassInvalid) { | 238 TEST_F(HandlePassingTest, PassInvalid) { |
239 sample::FactoryPtr factory; | 239 sample::FactoryPtr factory; |
240 SampleFactoryImpl factory_impl(GetProxy(&factory)); | 240 SampleFactoryImpl factory_impl(MakeRequest(&factory)); |
241 | 241 |
242 sample::RequestPtr request(sample::Request::New()); | 242 sample::RequestPtr request(sample::Request::New()); |
243 request->x = 1; | 243 request->x = 1; |
244 bool got_response = false; | 244 bool got_response = false; |
245 std::string got_text_reply; | 245 std::string got_text_reply; |
246 base::RunLoop run_loop; | 246 base::RunLoop run_loop; |
247 factory->DoStuff(std::move(request), ScopedMessagePipeHandle(), | 247 factory->DoStuff(std::move(request), ScopedMessagePipeHandle(), |
248 base::Bind(&DoStuff, &got_response, &got_text_reply, | 248 base::Bind(&DoStuff, &got_response, &got_text_reply, |
249 run_loop.QuitClosure())); | 249 run_loop.QuitClosure())); |
250 | 250 |
251 EXPECT_FALSE(got_response); | 251 EXPECT_FALSE(got_response); |
252 | 252 |
253 run_loop.Run(); | 253 run_loop.Run(); |
254 | 254 |
255 EXPECT_TRUE(got_response); | 255 EXPECT_TRUE(got_response); |
256 } | 256 } |
257 | 257 |
258 // Verifies DataPipeConsumer can be passed and read from. | 258 // Verifies DataPipeConsumer can be passed and read from. |
259 TEST_F(HandlePassingTest, DataPipe) { | 259 TEST_F(HandlePassingTest, DataPipe) { |
260 sample::FactoryPtr factory; | 260 sample::FactoryPtr factory; |
261 SampleFactoryImpl factory_impl(GetProxy(&factory)); | 261 SampleFactoryImpl factory_impl(MakeRequest(&factory)); |
262 | 262 |
263 // Writes a string to a data pipe and passes the data pipe (consumer) to the | 263 // Writes a string to a data pipe and passes the data pipe (consumer) to the |
264 // factory. | 264 // factory. |
265 ScopedDataPipeProducerHandle producer_handle; | 265 ScopedDataPipeProducerHandle producer_handle; |
266 ScopedDataPipeConsumerHandle consumer_handle; | 266 ScopedDataPipeConsumerHandle consumer_handle; |
267 MojoCreateDataPipeOptions options = {sizeof(MojoCreateDataPipeOptions), | 267 MojoCreateDataPipeOptions options = {sizeof(MojoCreateDataPipeOptions), |
268 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, | 268 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, |
269 1, | 269 1, |
270 1024}; | 270 1024}; |
271 ASSERT_EQ(MOJO_RESULT_OK, | 271 ASSERT_EQ(MOJO_RESULT_OK, |
(...skipping 17 matching lines...) Expand all Loading... |
289 EXPECT_FALSE(got_response); | 289 EXPECT_FALSE(got_response); |
290 | 290 |
291 run_loop.Run(); | 291 run_loop.Run(); |
292 | 292 |
293 EXPECT_TRUE(got_response); | 293 EXPECT_TRUE(got_response); |
294 EXPECT_EQ(expected_text_reply, got_text_reply); | 294 EXPECT_EQ(expected_text_reply, got_text_reply); |
295 } | 295 } |
296 | 296 |
297 TEST_F(HandlePassingTest, PipesAreClosed) { | 297 TEST_F(HandlePassingTest, PipesAreClosed) { |
298 sample::FactoryPtr factory; | 298 sample::FactoryPtr factory; |
299 SampleFactoryImpl factory_impl(GetProxy(&factory)); | 299 SampleFactoryImpl factory_impl(MakeRequest(&factory)); |
300 | 300 |
301 MessagePipe extra_pipe; | 301 MessagePipe extra_pipe; |
302 | 302 |
303 MojoHandle handle0_value = extra_pipe.handle0.get().value(); | 303 MojoHandle handle0_value = extra_pipe.handle0.get().value(); |
304 MojoHandle handle1_value = extra_pipe.handle1.get().value(); | 304 MojoHandle handle1_value = extra_pipe.handle1.get().value(); |
305 | 305 |
306 { | 306 { |
307 std::vector<ScopedMessagePipeHandle> pipes(2); | 307 std::vector<ScopedMessagePipeHandle> pipes(2); |
308 pipes[0] = std::move(extra_pipe.handle0); | 308 pipes[0] = std::move(extra_pipe.handle0); |
309 pipes[1] = std::move(extra_pipe.handle1); | 309 pipes[1] = std::move(extra_pipe.handle1); |
310 | 310 |
311 sample::RequestPtr request(sample::Request::New()); | 311 sample::RequestPtr request(sample::Request::New()); |
312 request->more_pipes = std::move(pipes); | 312 request->more_pipes = std::move(pipes); |
313 | 313 |
314 factory->DoStuff(std::move(request), ScopedMessagePipeHandle(), | 314 factory->DoStuff(std::move(request), ScopedMessagePipeHandle(), |
315 sample::Factory::DoStuffCallback()); | 315 sample::Factory::DoStuffCallback()); |
316 } | 316 } |
317 | 317 |
318 // We expect the pipes to have been closed. | 318 // We expect the pipes to have been closed. |
319 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(handle0_value)); | 319 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(handle0_value)); |
320 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(handle1_value)); | 320 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(handle1_value)); |
321 } | 321 } |
322 | 322 |
323 TEST_F(HandlePassingTest, CreateNamedObject) { | 323 TEST_F(HandlePassingTest, CreateNamedObject) { |
324 sample::FactoryPtr factory; | 324 sample::FactoryPtr factory; |
325 SampleFactoryImpl factory_impl(GetProxy(&factory)); | 325 SampleFactoryImpl factory_impl(MakeRequest(&factory)); |
326 | 326 |
327 sample::NamedObjectPtr object1; | 327 sample::NamedObjectPtr object1; |
328 EXPECT_FALSE(object1); | 328 EXPECT_FALSE(object1); |
329 | 329 |
330 InterfaceRequest<sample::NamedObject> object1_request = GetProxy(&object1); | 330 InterfaceRequest<sample::NamedObject> object1_request = MakeRequest(&object1); |
331 EXPECT_TRUE(object1_request.is_pending()); | 331 EXPECT_TRUE(object1_request.is_pending()); |
332 factory->CreateNamedObject(std::move(object1_request)); | 332 factory->CreateNamedObject(std::move(object1_request)); |
333 EXPECT_FALSE(object1_request.is_pending()); // We've passed the request. | 333 EXPECT_FALSE(object1_request.is_pending()); // We've passed the request. |
334 | 334 |
335 ASSERT_TRUE(object1); | 335 ASSERT_TRUE(object1); |
336 object1->SetName("object1"); | 336 object1->SetName("object1"); |
337 | 337 |
338 sample::NamedObjectPtr object2; | 338 sample::NamedObjectPtr object2; |
339 factory->CreateNamedObject(GetProxy(&object2)); | 339 factory->CreateNamedObject(MakeRequest(&object2)); |
340 object2->SetName("object2"); | 340 object2->SetName("object2"); |
341 | 341 |
342 base::RunLoop run_loop, run_loop2; | 342 base::RunLoop run_loop, run_loop2; |
343 std::string name1; | 343 std::string name1; |
344 object1->GetName(MakeStringRecorder(&name1, run_loop.QuitClosure())); | 344 object1->GetName(MakeStringRecorder(&name1, run_loop.QuitClosure())); |
345 | 345 |
346 std::string name2; | 346 std::string name2; |
347 object2->GetName(MakeStringRecorder(&name2, run_loop2.QuitClosure())); | 347 object2->GetName(MakeStringRecorder(&name2, run_loop2.QuitClosure())); |
348 | 348 |
349 run_loop.Run(); | 349 run_loop.Run(); |
350 run_loop2.Run(); | 350 run_loop2.Run(); |
351 | 351 |
352 EXPECT_EQ(std::string("object1"), name1); | 352 EXPECT_EQ(std::string("object1"), name1); |
353 EXPECT_EQ(std::string("object2"), name2); | 353 EXPECT_EQ(std::string("object2"), name2); |
354 } | 354 } |
355 | 355 |
356 } // namespace | 356 } // namespace |
357 } // namespace test | 357 } // namespace test |
358 } // namespace mojo | 358 } // namespace mojo |
OLD | NEW |