OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 // Note: This file tests both binding.h (mojo::Binding) and strong_binding.h | 5 // Note: This file tests both binding.h (mojo::Binding) and strong_binding.h |
6 // (mojo::StrongBinding). | 6 // (mojo::StrongBinding). |
7 | 7 |
8 #include "mojo/public/cpp/bindings/binding.h" | 8 #include "mojo/public/cpp/bindings/binding.h" |
9 | 9 |
10 #include <stdint.h> | 10 #include <stdint.h> |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 return base::Bind(&DoSetFlagAndRunClosure<Args...>, flag, callback); | 76 return base::Bind(&DoSetFlagAndRunClosure<Args...>, flag, callback); |
77 } | 77 } |
78 | 78 |
79 // BindingTest ----------------------------------------------------------------- | 79 // BindingTest ----------------------------------------------------------------- |
80 | 80 |
81 using BindingTest = BindingTestBase; | 81 using BindingTest = BindingTestBase; |
82 | 82 |
83 TEST_F(BindingTest, Close) { | 83 TEST_F(BindingTest, Close) { |
84 bool called = false; | 84 bool called = false; |
85 sample::ServicePtr ptr; | 85 sample::ServicePtr ptr; |
86 auto request = GetProxy(&ptr); | 86 auto request = MakeRequest(&ptr); |
87 base::RunLoop run_loop; | 87 base::RunLoop run_loop; |
88 ptr.set_connection_error_handler( | 88 ptr.set_connection_error_handler( |
89 SetFlagAndRunClosure(&called, run_loop.QuitClosure())); | 89 SetFlagAndRunClosure(&called, run_loop.QuitClosure())); |
90 ServiceImpl impl; | 90 ServiceImpl impl; |
91 Binding<sample::Service> binding(&impl, std::move(request)); | 91 Binding<sample::Service> binding(&impl, std::move(request)); |
92 | 92 |
93 binding.Close(); | 93 binding.Close(); |
94 EXPECT_FALSE(called); | 94 EXPECT_FALSE(called); |
95 run_loop.Run(); | 95 run_loop.Run(); |
96 EXPECT_TRUE(called); | 96 EXPECT_TRUE(called); |
97 } | 97 } |
98 | 98 |
99 // Tests that destroying a mojo::Binding closes the bound message pipe handle. | 99 // Tests that destroying a mojo::Binding closes the bound message pipe handle. |
100 TEST_F(BindingTest, DestroyClosesMessagePipe) { | 100 TEST_F(BindingTest, DestroyClosesMessagePipe) { |
101 bool encountered_error = false; | 101 bool encountered_error = false; |
102 ServiceImpl impl; | 102 ServiceImpl impl; |
103 sample::ServicePtr ptr; | 103 sample::ServicePtr ptr; |
104 auto request = GetProxy(&ptr); | 104 auto request = MakeRequest(&ptr); |
105 base::RunLoop run_loop; | 105 base::RunLoop run_loop; |
106 ptr.set_connection_error_handler( | 106 ptr.set_connection_error_handler( |
107 SetFlagAndRunClosure(&encountered_error, run_loop.QuitClosure())); | 107 SetFlagAndRunClosure(&encountered_error, run_loop.QuitClosure())); |
108 bool called = false; | 108 bool called = false; |
109 base::RunLoop run_loop2; | 109 base::RunLoop run_loop2; |
110 { | 110 { |
111 Binding<sample::Service> binding(&impl, std::move(request)); | 111 Binding<sample::Service> binding(&impl, std::move(request)); |
112 ptr->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr, | 112 ptr->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr, |
113 SetFlagAndRunClosure<int32_t>(&called, | 113 SetFlagAndRunClosure<int32_t>(&called, |
114 run_loop2.QuitClosure())); | 114 run_loop2.QuitClosure())); |
(...skipping 15 matching lines...) Expand all Loading... |
130 EXPECT_FALSE(called); | 130 EXPECT_FALSE(called); |
131 } | 131 } |
132 | 132 |
133 // Tests that the binding's connection error handler gets called when the other | 133 // Tests that the binding's connection error handler gets called when the other |
134 // end is closed. | 134 // end is closed. |
135 TEST_F(BindingTest, ConnectionError) { | 135 TEST_F(BindingTest, ConnectionError) { |
136 bool called = false; | 136 bool called = false; |
137 { | 137 { |
138 ServiceImpl impl; | 138 ServiceImpl impl; |
139 sample::ServicePtr ptr; | 139 sample::ServicePtr ptr; |
140 Binding<sample::Service> binding(&impl, GetProxy(&ptr)); | 140 Binding<sample::Service> binding(&impl, MakeRequest(&ptr)); |
141 base::RunLoop run_loop; | 141 base::RunLoop run_loop; |
142 binding.set_connection_error_handler( | 142 binding.set_connection_error_handler( |
143 SetFlagAndRunClosure(&called, run_loop.QuitClosure())); | 143 SetFlagAndRunClosure(&called, run_loop.QuitClosure())); |
144 ptr.reset(); | 144 ptr.reset(); |
145 EXPECT_FALSE(called); | 145 EXPECT_FALSE(called); |
146 run_loop.Run(); | 146 run_loop.Run(); |
147 EXPECT_TRUE(called); | 147 EXPECT_TRUE(called); |
148 // We want to make sure that it isn't called again during destruction. | 148 // We want to make sure that it isn't called again during destruction. |
149 called = false; | 149 called = false; |
150 } | 150 } |
151 EXPECT_FALSE(called); | 151 EXPECT_FALSE(called); |
152 } | 152 } |
153 | 153 |
154 // Tests that calling Close doesn't result in the connection error handler being | 154 // Tests that calling Close doesn't result in the connection error handler being |
155 // called. | 155 // called. |
156 TEST_F(BindingTest, CloseDoesntCallConnectionErrorHandler) { | 156 TEST_F(BindingTest, CloseDoesntCallConnectionErrorHandler) { |
157 ServiceImpl impl; | 157 ServiceImpl impl; |
158 sample::ServicePtr ptr; | 158 sample::ServicePtr ptr; |
159 Binding<sample::Service> binding(&impl, GetProxy(&ptr)); | 159 Binding<sample::Service> binding(&impl, MakeRequest(&ptr)); |
160 bool called = false; | 160 bool called = false; |
161 binding.set_connection_error_handler(SetFlagAndRunClosure(&called)); | 161 binding.set_connection_error_handler(SetFlagAndRunClosure(&called)); |
162 binding.Close(); | 162 binding.Close(); |
163 base::RunLoop().RunUntilIdle(); | 163 base::RunLoop().RunUntilIdle(); |
164 EXPECT_FALSE(called); | 164 EXPECT_FALSE(called); |
165 | 165 |
166 // We can also close the other end, and the error handler still won't be | 166 // We can also close the other end, and the error handler still won't be |
167 // called. | 167 // called. |
168 ptr.reset(); | 168 ptr.reset(); |
169 base::RunLoop().RunUntilIdle(); | 169 base::RunLoop().RunUntilIdle(); |
(...skipping 26 matching lines...) Expand all Loading... |
196 DISALLOW_COPY_AND_ASSIGN(ServiceImplWithBinding); | 196 DISALLOW_COPY_AND_ASSIGN(ServiceImplWithBinding); |
197 }; | 197 }; |
198 | 198 |
199 // Tests that the binding may be deleted in the connection error handler. | 199 // Tests that the binding may be deleted in the connection error handler. |
200 TEST_F(BindingTest, SelfDeleteOnConnectionError) { | 200 TEST_F(BindingTest, SelfDeleteOnConnectionError) { |
201 bool was_deleted = false; | 201 bool was_deleted = false; |
202 sample::ServicePtr ptr; | 202 sample::ServicePtr ptr; |
203 // This should delete itself on connection error. | 203 // This should delete itself on connection error. |
204 base::RunLoop run_loop; | 204 base::RunLoop run_loop; |
205 new ServiceImplWithBinding(&was_deleted, run_loop.QuitClosure(), | 205 new ServiceImplWithBinding(&was_deleted, run_loop.QuitClosure(), |
206 GetProxy(&ptr)); | 206 MakeRequest(&ptr)); |
207 ptr.reset(); | 207 ptr.reset(); |
208 EXPECT_FALSE(was_deleted); | 208 EXPECT_FALSE(was_deleted); |
209 run_loop.Run(); | 209 run_loop.Run(); |
210 EXPECT_TRUE(was_deleted); | 210 EXPECT_TRUE(was_deleted); |
211 } | 211 } |
212 | 212 |
213 // Tests that explicitly calling Unbind followed by rebinding works. | 213 // Tests that explicitly calling Unbind followed by rebinding works. |
214 TEST_F(BindingTest, Unbind) { | 214 TEST_F(BindingTest, Unbind) { |
215 ServiceImpl impl; | 215 ServiceImpl impl; |
216 sample::ServicePtr ptr; | 216 sample::ServicePtr ptr; |
217 Binding<sample::Service> binding(&impl, GetProxy(&ptr)); | 217 Binding<sample::Service> binding(&impl, MakeRequest(&ptr)); |
218 | 218 |
219 bool called = false; | 219 bool called = false; |
220 base::RunLoop run_loop; | 220 base::RunLoop run_loop; |
221 ptr->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr, | 221 ptr->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr, |
222 SetFlagAndRunClosure<int32_t>(&called, | 222 SetFlagAndRunClosure<int32_t>(&called, |
223 run_loop.QuitClosure())); | 223 run_loop.QuitClosure())); |
224 run_loop.Run(); | 224 run_loop.Run(); |
225 EXPECT_TRUE(called); | 225 EXPECT_TRUE(called); |
226 | 226 |
227 called = false; | 227 called = false; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 IntegerAccessorImpl impl; | 265 IntegerAccessorImpl impl; |
266 sample::IntegerAccessorPtr ptr; | 266 sample::IntegerAccessorPtr ptr; |
267 Binding<sample::IntegerAccessor> binding(&impl, &ptr); | 267 Binding<sample::IntegerAccessor> binding(&impl, &ptr); |
268 EXPECT_EQ(3u, ptr.version()); | 268 EXPECT_EQ(3u, ptr.version()); |
269 } | 269 } |
270 | 270 |
271 TEST_F(BindingTest, PauseResume) { | 271 TEST_F(BindingTest, PauseResume) { |
272 bool called = false; | 272 bool called = false; |
273 base::RunLoop run_loop; | 273 base::RunLoop run_loop; |
274 sample::ServicePtr ptr; | 274 sample::ServicePtr ptr; |
275 auto request = GetProxy(&ptr); | 275 auto request = MakeRequest(&ptr); |
276 ServiceImpl impl; | 276 ServiceImpl impl; |
277 Binding<sample::Service> binding(&impl, std::move(request)); | 277 Binding<sample::Service> binding(&impl, std::move(request)); |
278 binding.PauseIncomingMethodCallProcessing(); | 278 binding.PauseIncomingMethodCallProcessing(); |
279 ptr->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr, | 279 ptr->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr, |
280 SetFlagAndRunClosure<int32_t>(&called, | 280 SetFlagAndRunClosure<int32_t>(&called, |
281 run_loop.QuitClosure())); | 281 run_loop.QuitClosure())); |
282 EXPECT_FALSE(called); | 282 EXPECT_FALSE(called); |
283 base::RunLoop().RunUntilIdle(); | 283 base::RunLoop().RunUntilIdle(); |
284 // Frobinate() should not be called as the binding is paused. | 284 // Frobinate() should not be called as the binding is paused. |
285 EXPECT_FALSE(called); | 285 EXPECT_FALSE(called); |
286 | 286 |
287 // Resume the binding, which should trigger processing. | 287 // Resume the binding, which should trigger processing. |
288 binding.ResumeIncomingMethodCallProcessing(); | 288 binding.ResumeIncomingMethodCallProcessing(); |
289 run_loop.Run(); | 289 run_loop.Run(); |
290 EXPECT_TRUE(called); | 290 EXPECT_TRUE(called); |
291 } | 291 } |
292 | 292 |
293 // Verifies the connection error handler is not run while a binding is paused. | 293 // Verifies the connection error handler is not run while a binding is paused. |
294 TEST_F(BindingTest, ErrorHandleNotRunWhilePaused) { | 294 TEST_F(BindingTest, ErrorHandleNotRunWhilePaused) { |
295 bool called = false; | 295 bool called = false; |
296 base::RunLoop run_loop; | 296 base::RunLoop run_loop; |
297 sample::ServicePtr ptr; | 297 sample::ServicePtr ptr; |
298 auto request = GetProxy(&ptr); | 298 auto request = MakeRequest(&ptr); |
299 ServiceImpl impl; | 299 ServiceImpl impl; |
300 Binding<sample::Service> binding(&impl, std::move(request)); | 300 Binding<sample::Service> binding(&impl, std::move(request)); |
301 binding.set_connection_error_handler( | 301 binding.set_connection_error_handler( |
302 SetFlagAndRunClosure(&called, run_loop.QuitClosure())); | 302 SetFlagAndRunClosure(&called, run_loop.QuitClosure())); |
303 binding.PauseIncomingMethodCallProcessing(); | 303 binding.PauseIncomingMethodCallProcessing(); |
304 | 304 |
305 ptr.reset(); | 305 ptr.reset(); |
306 base::RunLoop().RunUntilIdle(); | 306 base::RunLoop().RunUntilIdle(); |
307 // The connection error handle should not be called as the binding is paused. | 307 // The connection error handle should not be called as the binding is paused. |
308 EXPECT_FALSE(called); | 308 EXPECT_FALSE(called); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
353 | 353 |
354 private: | 354 private: |
355 const base::Closure callback_; | 355 const base::Closure callback_; |
356 }; | 356 }; |
357 | 357 |
358 // Verifies that message filters are notified in the order they were added and | 358 // Verifies that message filters are notified in the order they were added and |
359 // are always notified before a message is dispatched. | 359 // are always notified before a message is dispatched. |
360 TEST_F(BindingTest, MessageFilter) { | 360 TEST_F(BindingTest, MessageFilter) { |
361 test::PingServicePtr ptr; | 361 test::PingServicePtr ptr; |
362 PingServiceImpl impl; | 362 PingServiceImpl impl; |
363 mojo::Binding<test::PingService> binding(&impl, GetProxy(&ptr)); | 363 mojo::Binding<test::PingService> binding(&impl, MakeRequest(&ptr)); |
364 | 364 |
365 int status = 0; | 365 int status = 0; |
366 auto handler_helper = [] (int* status, int expected_status, int new_status) { | 366 auto handler_helper = [] (int* status, int expected_status, int new_status) { |
367 EXPECT_EQ(expected_status, *status); | 367 EXPECT_EQ(expected_status, *status); |
368 *status = new_status; | 368 *status = new_status; |
369 }; | 369 }; |
370 auto create_handler = [&] (int expected_status, int new_status) { | 370 auto create_handler = [&] (int expected_status, int new_status) { |
371 return base::Bind(handler_helper, &status, expected_status, new_status); | 371 return base::Bind(handler_helper, &status, expected_status, new_status); |
372 }; | 372 }; |
373 | 373 |
(...skipping 10 matching lines...) Expand all Loading... |
384 } | 384 } |
385 } | 385 } |
386 | 386 |
387 void Fail() { | 387 void Fail() { |
388 FAIL() << "Unexpected connection error"; | 388 FAIL() << "Unexpected connection error"; |
389 } | 389 } |
390 | 390 |
391 TEST_F(BindingTest, FlushForTesting) { | 391 TEST_F(BindingTest, FlushForTesting) { |
392 bool called = false; | 392 bool called = false; |
393 sample::ServicePtr ptr; | 393 sample::ServicePtr ptr; |
394 auto request = GetProxy(&ptr); | 394 auto request = MakeRequest(&ptr); |
395 ServiceImpl impl; | 395 ServiceImpl impl; |
396 Binding<sample::Service> binding(&impl, std::move(request)); | 396 Binding<sample::Service> binding(&impl, std::move(request)); |
397 binding.set_connection_error_handler(base::Bind(&Fail)); | 397 binding.set_connection_error_handler(base::Bind(&Fail)); |
398 | 398 |
399 ptr->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr, | 399 ptr->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr, |
400 SetFlagAndRunClosure<int32_t>(&called)); | 400 SetFlagAndRunClosure<int32_t>(&called)); |
401 EXPECT_FALSE(called); | 401 EXPECT_FALSE(called); |
402 // Because the flush is sent from the binding, it only guarantees that the | 402 // Because the flush is sent from the binding, it only guarantees that the |
403 // request has been received, not the response. The second flush waits for the | 403 // request has been received, not the response. The second flush waits for the |
404 // response to be received. | 404 // response to be received. |
405 binding.FlushForTesting(); | 405 binding.FlushForTesting(); |
406 binding.FlushForTesting(); | 406 binding.FlushForTesting(); |
407 EXPECT_TRUE(called); | 407 EXPECT_TRUE(called); |
408 } | 408 } |
409 | 409 |
410 TEST_F(BindingTest, FlushForTestingWithClosedPeer) { | 410 TEST_F(BindingTest, FlushForTestingWithClosedPeer) { |
411 bool called = false; | 411 bool called = false; |
412 sample::ServicePtr ptr; | 412 sample::ServicePtr ptr; |
413 auto request = GetProxy(&ptr); | 413 auto request = MakeRequest(&ptr); |
414 ServiceImpl impl; | 414 ServiceImpl impl; |
415 Binding<sample::Service> binding(&impl, std::move(request)); | 415 Binding<sample::Service> binding(&impl, std::move(request)); |
416 binding.set_connection_error_handler(SetFlagAndRunClosure(&called)); | 416 binding.set_connection_error_handler(SetFlagAndRunClosure(&called)); |
417 ptr.reset(); | 417 ptr.reset(); |
418 | 418 |
419 EXPECT_FALSE(called); | 419 EXPECT_FALSE(called); |
420 binding.FlushForTesting(); | 420 binding.FlushForTesting(); |
421 EXPECT_TRUE(called); | 421 EXPECT_TRUE(called); |
422 binding.FlushForTesting(); | 422 binding.FlushForTesting(); |
423 } | 423 } |
424 | 424 |
425 TEST_F(BindingTest, ConnectionErrorWithReason) { | 425 TEST_F(BindingTest, ConnectionErrorWithReason) { |
426 sample::ServicePtr ptr; | 426 sample::ServicePtr ptr; |
427 auto request = GetProxy(&ptr); | 427 auto request = MakeRequest(&ptr); |
428 ServiceImpl impl; | 428 ServiceImpl impl; |
429 Binding<sample::Service> binding(&impl, std::move(request)); | 429 Binding<sample::Service> binding(&impl, std::move(request)); |
430 | 430 |
431 base::RunLoop run_loop; | 431 base::RunLoop run_loop; |
432 binding.set_connection_error_with_reason_handler(base::Bind( | 432 binding.set_connection_error_with_reason_handler(base::Bind( |
433 [](const base::Closure& quit_closure, uint32_t custom_reason, | 433 [](const base::Closure& quit_closure, uint32_t custom_reason, |
434 const std::string& description) { | 434 const std::string& description) { |
435 EXPECT_EQ(1234u, custom_reason); | 435 EXPECT_EQ(1234u, custom_reason); |
436 EXPECT_EQ("hello", description); | 436 EXPECT_EQ("hello", description); |
437 quit_closure.Run(); | 437 quit_closure.Run(); |
(...skipping 15 matching lines...) Expand all Loading... |
453 | 453 |
454 template <typename T> | 454 template <typename T> |
455 using WeakBinding = Binding<T, WeakPtrImplRefTraits<T>>; | 455 using WeakBinding = Binding<T, WeakPtrImplRefTraits<T>>; |
456 | 456 |
457 TEST_F(BindingTest, CustomImplPointerType) { | 457 TEST_F(BindingTest, CustomImplPointerType) { |
458 PingServiceImpl impl; | 458 PingServiceImpl impl; |
459 base::WeakPtrFactory<test::PingService> weak_factory(&impl); | 459 base::WeakPtrFactory<test::PingService> weak_factory(&impl); |
460 | 460 |
461 test::PingServicePtr proxy; | 461 test::PingServicePtr proxy; |
462 WeakBinding<test::PingService> binding(weak_factory.GetWeakPtr(), | 462 WeakBinding<test::PingService> binding(weak_factory.GetWeakPtr(), |
463 GetProxy(&proxy)); | 463 MakeRequest(&proxy)); |
464 | 464 |
465 { | 465 { |
466 // Ensure the binding is functioning. | 466 // Ensure the binding is functioning. |
467 base::RunLoop run_loop; | 467 base::RunLoop run_loop; |
468 proxy->Ping(run_loop.QuitClosure()); | 468 proxy->Ping(run_loop.QuitClosure()); |
469 run_loop.Run(); | 469 run_loop.Run(); |
470 } | 470 } |
471 | 471 |
472 { | 472 { |
473 // Attempt to dispatch another message after the WeakPtr is invalidated. | 473 // Attempt to dispatch another message after the WeakPtr is invalidated. |
(...skipping 14 matching lines...) Expand all Loading... |
488 | 488 |
489 using StrongBindingTest = BindingTestBase; | 489 using StrongBindingTest = BindingTestBase; |
490 | 490 |
491 // Tests that destroying a mojo::StrongBinding closes the bound message pipe | 491 // Tests that destroying a mojo::StrongBinding closes the bound message pipe |
492 // handle but does *not* destroy the implementation object. | 492 // handle but does *not* destroy the implementation object. |
493 TEST_F(StrongBindingTest, DestroyClosesMessagePipe) { | 493 TEST_F(StrongBindingTest, DestroyClosesMessagePipe) { |
494 base::RunLoop run_loop; | 494 base::RunLoop run_loop; |
495 bool encountered_error = false; | 495 bool encountered_error = false; |
496 bool was_deleted = false; | 496 bool was_deleted = false; |
497 sample::ServicePtr ptr; | 497 sample::ServicePtr ptr; |
498 auto request = GetProxy(&ptr); | 498 auto request = MakeRequest(&ptr); |
499 ptr.set_connection_error_handler( | 499 ptr.set_connection_error_handler( |
500 SetFlagAndRunClosure(&encountered_error, run_loop.QuitClosure())); | 500 SetFlagAndRunClosure(&encountered_error, run_loop.QuitClosure())); |
501 bool called = false; | 501 bool called = false; |
502 base::RunLoop run_loop2; | 502 base::RunLoop run_loop2; |
503 | 503 |
504 auto binding = MakeStrongBinding(base::MakeUnique<ServiceImpl>(&was_deleted), | 504 auto binding = MakeStrongBinding(base::MakeUnique<ServiceImpl>(&was_deleted), |
505 std::move(request)); | 505 std::move(request)); |
506 ptr->Frobinate( | 506 ptr->Frobinate( |
507 nullptr, sample::Service::BazOptions::REGULAR, nullptr, | 507 nullptr, sample::Service::BazOptions::REGULAR, nullptr, |
508 SetFlagAndRunClosure<int32_t>(&called, run_loop2.QuitClosure())); | 508 SetFlagAndRunClosure<int32_t>(&called, run_loop2.QuitClosure())); |
(...skipping 12 matching lines...) Expand all Loading... |
521 } | 521 } |
522 | 522 |
523 // Tests the typical case, where the implementation object owns the | 523 // Tests the typical case, where the implementation object owns the |
524 // StrongBinding (and should be destroyed on connection error). | 524 // StrongBinding (and should be destroyed on connection error). |
525 TEST_F(StrongBindingTest, ConnectionErrorDestroysImpl) { | 525 TEST_F(StrongBindingTest, ConnectionErrorDestroysImpl) { |
526 sample::ServicePtr ptr; | 526 sample::ServicePtr ptr; |
527 bool was_deleted = false; | 527 bool was_deleted = false; |
528 // Will delete itself. | 528 // Will delete itself. |
529 base::RunLoop run_loop; | 529 base::RunLoop run_loop; |
530 new ServiceImplWithBinding(&was_deleted, run_loop.QuitClosure(), | 530 new ServiceImplWithBinding(&was_deleted, run_loop.QuitClosure(), |
531 GetProxy(&ptr)); | 531 MakeRequest(&ptr)); |
532 | 532 |
533 base::RunLoop().RunUntilIdle(); | 533 base::RunLoop().RunUntilIdle(); |
534 EXPECT_FALSE(was_deleted); | 534 EXPECT_FALSE(was_deleted); |
535 | 535 |
536 ptr.reset(); | 536 ptr.reset(); |
537 EXPECT_FALSE(was_deleted); | 537 EXPECT_FALSE(was_deleted); |
538 run_loop.Run(); | 538 run_loop.Run(); |
539 EXPECT_TRUE(was_deleted); | 539 EXPECT_TRUE(was_deleted); |
540 } | 540 } |
541 | 541 |
542 TEST_F(StrongBindingTest, FlushForTesting) { | 542 TEST_F(StrongBindingTest, FlushForTesting) { |
543 bool called = false; | 543 bool called = false; |
544 bool was_deleted = false; | 544 bool was_deleted = false; |
545 sample::ServicePtr ptr; | 545 sample::ServicePtr ptr; |
546 auto request = GetProxy(&ptr); | 546 auto request = MakeRequest(&ptr); |
547 auto binding = MakeStrongBinding(base::MakeUnique<ServiceImpl>(&was_deleted), | 547 auto binding = MakeStrongBinding(base::MakeUnique<ServiceImpl>(&was_deleted), |
548 std::move(request)); | 548 std::move(request)); |
549 binding->set_connection_error_handler(base::Bind(&Fail)); | 549 binding->set_connection_error_handler(base::Bind(&Fail)); |
550 | 550 |
551 ptr->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr, | 551 ptr->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr, |
552 SetFlagAndRunClosure<int32_t>(&called)); | 552 SetFlagAndRunClosure<int32_t>(&called)); |
553 EXPECT_FALSE(called); | 553 EXPECT_FALSE(called); |
554 // Because the flush is sent from the binding, it only guarantees that the | 554 // Because the flush is sent from the binding, it only guarantees that the |
555 // request has been received, not the response. The second flush waits for the | 555 // request has been received, not the response. The second flush waits for the |
556 // response to be received. | 556 // response to be received. |
557 ASSERT_TRUE(binding); | 557 ASSERT_TRUE(binding); |
558 binding->FlushForTesting(); | 558 binding->FlushForTesting(); |
559 ASSERT_TRUE(binding); | 559 ASSERT_TRUE(binding); |
560 binding->FlushForTesting(); | 560 binding->FlushForTesting(); |
561 EXPECT_TRUE(called); | 561 EXPECT_TRUE(called); |
562 EXPECT_FALSE(was_deleted); | 562 EXPECT_FALSE(was_deleted); |
563 ptr.reset(); | 563 ptr.reset(); |
564 ASSERT_TRUE(binding); | 564 ASSERT_TRUE(binding); |
565 binding->set_connection_error_handler(base::Closure()); | 565 binding->set_connection_error_handler(base::Closure()); |
566 binding->FlushForTesting(); | 566 binding->FlushForTesting(); |
567 EXPECT_TRUE(was_deleted); | 567 EXPECT_TRUE(was_deleted); |
568 } | 568 } |
569 | 569 |
570 TEST_F(StrongBindingTest, FlushForTestingWithClosedPeer) { | 570 TEST_F(StrongBindingTest, FlushForTestingWithClosedPeer) { |
571 bool called = false; | 571 bool called = false; |
572 bool was_deleted = false; | 572 bool was_deleted = false; |
573 sample::ServicePtr ptr; | 573 sample::ServicePtr ptr; |
574 auto request = GetProxy(&ptr); | 574 auto request = MakeRequest(&ptr); |
575 auto binding = MakeStrongBinding(base::MakeUnique<ServiceImpl>(&was_deleted), | 575 auto binding = MakeStrongBinding(base::MakeUnique<ServiceImpl>(&was_deleted), |
576 std::move(request)); | 576 std::move(request)); |
577 binding->set_connection_error_handler(SetFlagAndRunClosure(&called)); | 577 binding->set_connection_error_handler(SetFlagAndRunClosure(&called)); |
578 ptr.reset(); | 578 ptr.reset(); |
579 | 579 |
580 EXPECT_FALSE(called); | 580 EXPECT_FALSE(called); |
581 EXPECT_FALSE(was_deleted); | 581 EXPECT_FALSE(was_deleted); |
582 ASSERT_TRUE(binding); | 582 ASSERT_TRUE(binding); |
583 binding->FlushForTesting(); | 583 binding->FlushForTesting(); |
584 EXPECT_TRUE(called); | 584 EXPECT_TRUE(called); |
585 EXPECT_TRUE(was_deleted); | 585 EXPECT_TRUE(was_deleted); |
586 ASSERT_FALSE(binding); | 586 ASSERT_FALSE(binding); |
587 } | 587 } |
588 | 588 |
589 TEST_F(StrongBindingTest, ConnectionErrorWithReason) { | 589 TEST_F(StrongBindingTest, ConnectionErrorWithReason) { |
590 sample::ServicePtr ptr; | 590 sample::ServicePtr ptr; |
591 auto request = GetProxy(&ptr); | 591 auto request = MakeRequest(&ptr); |
592 auto binding = | 592 auto binding = |
593 MakeStrongBinding(base::MakeUnique<ServiceImpl>(), std::move(request)); | 593 MakeStrongBinding(base::MakeUnique<ServiceImpl>(), std::move(request)); |
594 base::RunLoop run_loop; | 594 base::RunLoop run_loop; |
595 binding->set_connection_error_with_reason_handler(base::Bind( | 595 binding->set_connection_error_with_reason_handler(base::Bind( |
596 [](const base::Closure& quit_closure, uint32_t custom_reason, | 596 [](const base::Closure& quit_closure, uint32_t custom_reason, |
597 const std::string& description) { | 597 const std::string& description) { |
598 EXPECT_EQ(5678u, custom_reason); | 598 EXPECT_EQ(5678u, custom_reason); |
599 EXPECT_EQ("hello", description); | 599 EXPECT_EQ("hello", description); |
600 quit_closure.Run(); | 600 quit_closure.Run(); |
601 }, | 601 }, |
602 run_loop.QuitClosure())); | 602 run_loop.QuitClosure())); |
603 | 603 |
604 ptr.ResetWithReason(5678u, "hello"); | 604 ptr.ResetWithReason(5678u, "hello"); |
605 | 605 |
606 run_loop.Run(); | 606 run_loop.Run(); |
607 } | 607 } |
608 | 608 |
609 } // namespace | 609 } // namespace |
610 } // mojo | 610 } // mojo |
OLD | NEW |