OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "mojo/common/binding_set.h" |
| 6 |
| 7 #include "base/message_loop/message_loop.h" |
| 8 #include "mojo/common/test_interfaces.mojom.h" |
| 9 #include "mojo/message_pump/message_pump_mojo.h" |
| 10 #include "mojo/public/cpp/bindings/binding.h" |
| 11 #include "mojo/public/cpp/bindings/interface_request.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace mojo { |
| 15 namespace common { |
| 16 namespace { |
| 17 |
| 18 class DummyImpl : public tests::Dummy { |
| 19 public: |
| 20 DummyImpl() {} |
| 21 |
| 22 void Foo() override { call_count_++; } |
| 23 |
| 24 int call_count() const { return call_count_; } |
| 25 |
| 26 private: |
| 27 int call_count_ = 0; |
| 28 |
| 29 DISALLOW_COPY_AND_ASSIGN(DummyImpl); |
| 30 }; |
| 31 |
| 32 // Tests all of the functionality of BindingSet. |
| 33 TEST(BindingSetTest, FullLifeCycle) { |
| 34 base::MessageLoop loop(MessagePumpMojo::Create()); |
| 35 |
| 36 // Create 10 InterfacePtrs and DummyImpls. |
| 37 const size_t kNumObjects = 10; |
| 38 InterfacePtr<tests::Dummy> intrfc_ptrs[kNumObjects]; |
| 39 DummyImpl impls[kNumObjects]; |
| 40 |
| 41 // Create 10 message pipes, bind everything together, and add the |
| 42 // bindings to binding_set. |
| 43 BindingSet<tests::Dummy> binding_set; |
| 44 EXPECT_EQ(0u, binding_set.size()); |
| 45 for (size_t i = 0; i < kNumObjects; i++) { |
| 46 binding_set.AddBinding(&impls[i], GetProxy(&intrfc_ptrs[i])); |
| 47 } |
| 48 EXPECT_EQ(kNumObjects, binding_set.size()); |
| 49 |
| 50 // Check that initially all call counts are zero. |
| 51 for (const auto& impl : impls) { |
| 52 EXPECT_EQ(0, impl.call_count()); |
| 53 } |
| 54 |
| 55 // Invoke method foo() on all 10 InterfacePointers. |
| 56 for (InterfacePtr<tests::Dummy>& ptr : intrfc_ptrs) { |
| 57 ptr->Foo(); |
| 58 } |
| 59 |
| 60 // Check that now all call counts are one. |
| 61 loop.RunUntilIdle(); |
| 62 for (const auto& impl : impls) { |
| 63 EXPECT_EQ(1, impl.call_count()); |
| 64 } |
| 65 |
| 66 // Close the first 5 message pipes and destroy the first five |
| 67 // InterfacePtrs. |
| 68 for (size_t i = 0; i < kNumObjects / 2; i++) { |
| 69 intrfc_ptrs[i].reset(); |
| 70 } |
| 71 |
| 72 // Check that the set contains only five elements now. |
| 73 loop.RunUntilIdle(); |
| 74 EXPECT_EQ(kNumObjects / 2, binding_set.size()); |
| 75 |
| 76 // Invoke method foo() on the second five InterfacePointers. |
| 77 for (size_t i = kNumObjects / 2; i < kNumObjects; i++) { |
| 78 intrfc_ptrs[i]->Foo(); |
| 79 } |
| 80 loop.RunUntilIdle(); |
| 81 |
| 82 // Check that now the first five counts are still 1 but the second five |
| 83 // counts are two. |
| 84 for (size_t i = 0; i < kNumObjects; i++) { |
| 85 int expected = (i < kNumObjects / 2 ? 1 : 2); |
| 86 EXPECT_EQ(expected, impls[i].call_count()); |
| 87 } |
| 88 |
| 89 // Invoke CloseAllBindings |
| 90 binding_set.CloseAllBindings(); |
| 91 EXPECT_EQ(0u, binding_set.size()); |
| 92 |
| 93 // Invoke method foo() on the second five InterfacePointers. |
| 94 for (size_t i = kNumObjects / 2; i < kNumObjects; i++) { |
| 95 intrfc_ptrs[i]->Foo(); |
| 96 } |
| 97 loop.RunUntilIdle(); |
| 98 |
| 99 // Check that the call counts are the same as before. |
| 100 for (size_t i = 0; i < kNumObjects; i++) { |
| 101 int expected = (i < kNumObjects / 2 ? 1 : 2); |
| 102 EXPECT_EQ(expected, impls[i].call_count()); |
| 103 } |
| 104 } |
| 105 |
| 106 } // namespace |
| 107 } // namespace common |
| 108 } // namespace mojo |
OLD | NEW |