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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 bool* const was_deleted_; | 57 bool* const was_deleted_; |
58 | 58 |
59 DISALLOW_COPY_AND_ASSIGN(ServiceImpl); | 59 DISALLOW_COPY_AND_ASSIGN(ServiceImpl); |
60 }; | 60 }; |
61 | 61 |
62 template <typename... Args> | 62 template <typename... Args> |
63 void DoSetFlagAndRunClosure(bool* flag, | 63 void DoSetFlagAndRunClosure(bool* flag, |
64 const base::Closure& closure, | 64 const base::Closure& closure, |
65 Args... args) { | 65 Args... args) { |
66 *flag = true; | 66 *flag = true; |
67 closure.Run(); | 67 if (!closure.is_null()) |
| 68 closure.Run(); |
68 } | 69 } |
69 | 70 |
70 template <typename... Args> | 71 template <typename... Args> |
71 base::Callback<void(Args...)> SetFlagAndRunClosure( | 72 base::Callback<void(Args...)> SetFlagAndRunClosure( |
72 bool* flag, | 73 bool* flag, |
73 const base::Closure& callback = base::Closure()) { | 74 const base::Closure& callback = base::Closure()) { |
74 return base::Bind(&DoSetFlagAndRunClosure<Args...>, flag, callback); | 75 return base::Bind(&DoSetFlagAndRunClosure<Args...>, flag, callback); |
75 } | 76 } |
76 | 77 |
77 // BindingTest ----------------------------------------------------------------- | 78 // BindingTest ----------------------------------------------------------------- |
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 | 379 |
379 for (int i = 0; i < 10; ++i) { | 380 for (int i = 0; i < 10; ++i) { |
380 status = 0; | 381 status = 0; |
381 base::RunLoop loop; | 382 base::RunLoop loop; |
382 ptr->Ping(loop.QuitClosure()); | 383 ptr->Ping(loop.QuitClosure()); |
383 loop.Run(); | 384 loop.Run(); |
384 EXPECT_EQ(3, status); | 385 EXPECT_EQ(3, status); |
385 } | 386 } |
386 } | 387 } |
387 | 388 |
| 389 void Fail() { |
| 390 FAIL() << "Unexpected connection error"; |
| 391 } |
| 392 |
| 393 TEST_F(BindingTest, FlushForTesting) { |
| 394 bool called = false; |
| 395 sample::ServicePtr ptr; |
| 396 auto request = GetProxy(&ptr); |
| 397 ServiceImpl impl; |
| 398 Binding<sample::Service> binding(&impl, std::move(request)); |
| 399 binding.set_connection_error_handler(base::Bind(&Fail)); |
| 400 |
| 401 ptr->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr, |
| 402 SetFlagAndRunClosure<int32_t>(&called)); |
| 403 EXPECT_FALSE(called); |
| 404 // Because the flush is sent from the binding, it only guarantees that the |
| 405 // request has been received, not the response. The second flush waits for the |
| 406 // response to be received. |
| 407 binding.FlushForTesting(); |
| 408 binding.FlushForTesting(); |
| 409 EXPECT_TRUE(called); |
| 410 } |
| 411 |
| 412 TEST_F(BindingTest, FlushForTestingWithClosedPeer) { |
| 413 bool called = false; |
| 414 sample::ServicePtr ptr; |
| 415 auto request = GetProxy(&ptr); |
| 416 ServiceImpl impl; |
| 417 Binding<sample::Service> binding(&impl, std::move(request)); |
| 418 binding.set_connection_error_handler(SetFlagAndRunClosure(&called)); |
| 419 ptr.reset(); |
| 420 |
| 421 EXPECT_FALSE(called); |
| 422 binding.FlushForTesting(); |
| 423 EXPECT_TRUE(called); |
| 424 binding.FlushForTesting(); |
| 425 } |
| 426 |
388 // StrongBindingTest ----------------------------------------------------------- | 427 // StrongBindingTest ----------------------------------------------------------- |
389 | 428 |
390 using StrongBindingTest = BindingTestBase; | 429 using StrongBindingTest = BindingTestBase; |
391 | 430 |
392 // Tests that destroying a mojo::StrongBinding closes the bound message pipe | 431 // Tests that destroying a mojo::StrongBinding closes the bound message pipe |
393 // handle but does *not* destroy the implementation object. | 432 // handle but does *not* destroy the implementation object. |
394 TEST_F(StrongBindingTest, DestroyClosesMessagePipe) { | 433 TEST_F(StrongBindingTest, DestroyClosesMessagePipe) { |
395 base::RunLoop run_loop; | 434 base::RunLoop run_loop; |
396 bool encountered_error = false; | 435 bool encountered_error = false; |
397 bool was_deleted = false; | 436 bool was_deleted = false; |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
478 EXPECT_FALSE(ptr_error_handler_called); | 517 EXPECT_FALSE(ptr_error_handler_called); |
479 EXPECT_TRUE(was_deleted); | 518 EXPECT_TRUE(was_deleted); |
480 was_deleted = false; // It shouldn't be double-deleted! | 519 was_deleted = false; // It shouldn't be double-deleted! |
481 run_loop.Run(); | 520 run_loop.Run(); |
482 EXPECT_TRUE(ptr_error_handler_called); | 521 EXPECT_TRUE(ptr_error_handler_called); |
483 EXPECT_FALSE(was_deleted); | 522 EXPECT_FALSE(was_deleted); |
484 | 523 |
485 EXPECT_FALSE(binding_error_handler_called); | 524 EXPECT_FALSE(binding_error_handler_called); |
486 } | 525 } |
487 | 526 |
| 527 TEST_F(StrongBindingTest, FlushForTesting) { |
| 528 bool called = false; |
| 529 bool was_deleted = false; |
| 530 sample::ServicePtr ptr; |
| 531 auto request = GetProxy(&ptr); |
| 532 StrongBinding<sample::Service> binding(new ServiceImpl(&was_deleted), |
| 533 std::move(request)); |
| 534 binding.set_connection_error_handler(base::Bind(&Fail)); |
| 535 |
| 536 ptr->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr, |
| 537 SetFlagAndRunClosure<int32_t>(&called)); |
| 538 EXPECT_FALSE(called); |
| 539 // Because the flush is sent from the binding, it only guarantees that the |
| 540 // request has been received, not the response. The second flush waits for the |
| 541 // response to be received. |
| 542 binding.FlushForTesting(); |
| 543 binding.FlushForTesting(); |
| 544 EXPECT_TRUE(called); |
| 545 EXPECT_FALSE(was_deleted); |
| 546 ptr.reset(); |
| 547 binding.set_connection_error_handler(base::Closure()); |
| 548 binding.FlushForTesting(); |
| 549 EXPECT_TRUE(was_deleted); |
| 550 } |
| 551 |
| 552 TEST_F(StrongBindingTest, FlushForTestingWithClosedPeer) { |
| 553 bool called = false; |
| 554 bool was_deleted = false; |
| 555 sample::ServicePtr ptr; |
| 556 auto request = GetProxy(&ptr); |
| 557 StrongBinding<sample::Service> binding(new ServiceImpl(&was_deleted), |
| 558 std::move(request)); |
| 559 binding.set_connection_error_handler(SetFlagAndRunClosure(&called)); |
| 560 ptr.reset(); |
| 561 |
| 562 EXPECT_FALSE(called); |
| 563 EXPECT_FALSE(was_deleted); |
| 564 binding.FlushForTesting(); |
| 565 EXPECT_TRUE(called); |
| 566 EXPECT_TRUE(was_deleted); |
| 567 binding.FlushForTesting(); |
| 568 EXPECT_TRUE(was_deleted); |
| 569 } |
| 570 |
488 } // namespace | 571 } // namespace |
489 } // mojo | 572 } // mojo |
OLD | NEW |