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

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
424 TEST_F(BindingTest, FlushForTestingWhenClosed) {
425 ServiceImpl impl;
426 Binding<sample::Service> binding(&impl);
427 binding.FlushForTesting();
428 }
429
388 // StrongBindingTest ----------------------------------------------------------- 430 // StrongBindingTest -----------------------------------------------------------
389 431
390 using StrongBindingTest = BindingTestBase; 432 using StrongBindingTest = BindingTestBase;
391 433
392 // Tests that destroying a mojo::StrongBinding closes the bound message pipe 434 // Tests that destroying a mojo::StrongBinding closes the bound message pipe
393 // handle but does *not* destroy the implementation object. 435 // handle but does *not* destroy the implementation object.
394 TEST_F(StrongBindingTest, DestroyClosesMessagePipe) { 436 TEST_F(StrongBindingTest, DestroyClosesMessagePipe) {
395 base::RunLoop run_loop; 437 base::RunLoop run_loop;
396 bool encountered_error = false; 438 bool encountered_error = false;
397 bool was_deleted = false; 439 bool was_deleted = false;
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
478 EXPECT_FALSE(ptr_error_handler_called); 520 EXPECT_FALSE(ptr_error_handler_called);
479 EXPECT_TRUE(was_deleted); 521 EXPECT_TRUE(was_deleted);
480 was_deleted = false; // It shouldn't be double-deleted! 522 was_deleted = false; // It shouldn't be double-deleted!
481 run_loop.Run(); 523 run_loop.Run();
482 EXPECT_TRUE(ptr_error_handler_called); 524 EXPECT_TRUE(ptr_error_handler_called);
483 EXPECT_FALSE(was_deleted); 525 EXPECT_FALSE(was_deleted);
484 526
485 EXPECT_FALSE(binding_error_handler_called); 527 EXPECT_FALSE(binding_error_handler_called);
486 } 528 }
487 529
530 TEST_F(StrongBindingTest, FlushForTesting) {
531 bool called = false;
532 bool was_deleted = false;
533 sample::ServicePtr ptr;
534 auto request = GetProxy(&ptr);
535 StrongBinding<sample::Service> binding(new ServiceImpl(&was_deleted),
536 std::move(request));
537 binding.set_connection_error_handler(base::Bind(&Fail));
538
539 ptr->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr,
540 SetFlagAndRunClosure<int32_t>(&called));
541 EXPECT_FALSE(called);
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
571 TEST_F(StrongBindingTest, FlushForTestingWhenClosed) {
572 bool was_deleted = false;
573 ServiceImpl impl(&was_deleted);
574 StrongBinding<sample::Service> binding(&impl);
575 binding.FlushForTesting();
576 EXPECT_FALSE(was_deleted);
577 }
578
488 } // namespace 579 } // namespace
489 } // mojo 580 } // mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698