Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(87)

Side by Side Diff: mojo/public/cpp/bindings/tests/binding_unittest.cc

Issue 2280483002: Add FlushForTesting to InterfacePtr and Binding. (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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 binding.FlushForTesting();
405 binding.FlushForTesting();
406 EXPECT_TRUE(called);
407 }
408
409 TEST_F(BindingTest, FlushForTestingWithClosedPeer) {
410 bool called = false;
411 sample::ServicePtr ptr;
412 auto request = GetProxy(&ptr);
413 ServiceImpl impl;
414 Binding<sample::Service> binding(&impl, std::move(request));
415 binding.set_connection_error_handler(SetFlagAndRunClosure(&called));
416 ptr.reset();
417
418 EXPECT_FALSE(called);
419 binding.FlushForTesting();
420 EXPECT_TRUE(called);
421 binding.FlushForTesting();
422 }
423
388 // StrongBindingTest ----------------------------------------------------------- 424 // StrongBindingTest -----------------------------------------------------------
389 425
390 using StrongBindingTest = BindingTestBase; 426 using StrongBindingTest = BindingTestBase;
391 427
392 // Tests that destroying a mojo::StrongBinding closes the bound message pipe 428 // Tests that destroying a mojo::StrongBinding closes the bound message pipe
393 // handle but does *not* destroy the implementation object. 429 // handle but does *not* destroy the implementation object.
394 TEST_F(StrongBindingTest, DestroyClosesMessagePipe) { 430 TEST_F(StrongBindingTest, DestroyClosesMessagePipe) {
395 base::RunLoop run_loop; 431 base::RunLoop run_loop;
396 bool encountered_error = false; 432 bool encountered_error = false;
397 bool was_deleted = false; 433 bool was_deleted = false;
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
478 EXPECT_FALSE(ptr_error_handler_called); 514 EXPECT_FALSE(ptr_error_handler_called);
479 EXPECT_TRUE(was_deleted); 515 EXPECT_TRUE(was_deleted);
480 was_deleted = false; // It shouldn't be double-deleted! 516 was_deleted = false; // It shouldn't be double-deleted!
481 run_loop.Run(); 517 run_loop.Run();
482 EXPECT_TRUE(ptr_error_handler_called); 518 EXPECT_TRUE(ptr_error_handler_called);
483 EXPECT_FALSE(was_deleted); 519 EXPECT_FALSE(was_deleted);
484 520
485 EXPECT_FALSE(binding_error_handler_called); 521 EXPECT_FALSE(binding_error_handler_called);
486 } 522 }
487 523
524 TEST_F(StrongBindingTest, FlushForTesting) {
525 bool called = false;
526 bool was_deleted = false;
527 sample::ServicePtr ptr;
528 auto request = GetProxy(&ptr);
529 StrongBinding<sample::Service> binding(new ServiceImpl(&was_deleted),
530 std::move(request));
531 binding.set_connection_error_handler(base::Bind(&Fail));
532
533 ptr->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr,
534 SetFlagAndRunClosure<int32_t>(&called));
535 EXPECT_FALSE(called);
536 binding.FlushForTesting();
537 binding.FlushForTesting();
538 EXPECT_TRUE(called);
539 EXPECT_FALSE(was_deleted);
540 ptr.reset();
541 binding.set_connection_error_handler(base::Closure());
542 binding.FlushForTesting();
543 EXPECT_TRUE(was_deleted);
544 }
545
546 TEST_F(StrongBindingTest, FlushForTestingWithClosedPeer) {
547 bool called = false;
548 bool was_deleted = false;
549 sample::ServicePtr ptr;
550 auto request = GetProxy(&ptr);
551 StrongBinding<sample::Service> binding(new ServiceImpl(&was_deleted),
552 std::move(request));
553 binding.set_connection_error_handler(SetFlagAndRunClosure(&called));
554 ptr.reset();
555
556 EXPECT_FALSE(called);
557 EXPECT_FALSE(was_deleted);
558 binding.FlushForTesting();
559 EXPECT_TRUE(called);
560 EXPECT_TRUE(was_deleted);
561 binding.FlushForTesting();
562 EXPECT_TRUE(was_deleted);
563 }
564
488 } // namespace 565 } // namespace
489 } // mojo 566 } // mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698