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/strong_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 explicit DummyImpl(bool* deleted_flag) : deleted_flag_(deleted_flag) {} |
| 21 ~DummyImpl() override { *deleted_flag_ = true; } |
| 22 |
| 23 void Foo() override { call_count_++; } |
| 24 |
| 25 int call_count() const { return call_count_; } |
| 26 |
| 27 private: |
| 28 bool* deleted_flag_; |
| 29 int call_count_ = 0; |
| 30 |
| 31 DISALLOW_COPY_AND_ASSIGN(DummyImpl); |
| 32 }; |
| 33 |
| 34 // Tests all of the functionality of StrongBindingSet. |
| 35 TEST(StrongBindingSetTest, FullLifeCycle) { |
| 36 base::MessageLoop loop(MessagePumpMojo::Create()); |
| 37 |
| 38 // Create 10 InterfacePtrs and DummyImpls. |
| 39 const size_t kNumObjects = 10; |
| 40 InterfacePtr<tests::Dummy> intrfc_ptrs[kNumObjects]; |
| 41 DummyImpl* impls[kNumObjects]; |
| 42 bool deleted_flags[kNumObjects] = {}; |
| 43 |
| 44 // Create 10 message pipes, bind everything together, and add the |
| 45 // bindings to binding_set. |
| 46 StrongBindingSet<tests::Dummy> binding_set; |
| 47 EXPECT_EQ(0u, binding_set.size()); |
| 48 for (size_t i = 0; i < kNumObjects; i++) { |
| 49 impls[i] = new DummyImpl(&deleted_flags[i]); |
| 50 binding_set.AddBinding(impls[i], GetProxy(&intrfc_ptrs[i])); |
| 51 } |
| 52 EXPECT_EQ(kNumObjects, binding_set.size()); |
| 53 |
| 54 // Check that initially all call counts are zero. |
| 55 for (const auto& impl : impls) { |
| 56 EXPECT_EQ(0, impl->call_count()); |
| 57 } |
| 58 |
| 59 // Invoke method foo() on all 10 InterfacePointers. |
| 60 for (InterfacePtr<tests::Dummy>& ptr : intrfc_ptrs) { |
| 61 ptr->Foo(); |
| 62 } |
| 63 |
| 64 // Check that now all call counts are one. |
| 65 loop.RunUntilIdle(); |
| 66 for (const auto& impl : impls) { |
| 67 EXPECT_EQ(1, impl->call_count()); |
| 68 } |
| 69 |
| 70 // Close the first 5 message pipes and destroy the first five |
| 71 // InterfacePtrs. |
| 72 for (size_t i = 0; i < kNumObjects / 2; i++) { |
| 73 intrfc_ptrs[i].reset(); |
| 74 } |
| 75 |
| 76 // Check that the set contains only five elements now. |
| 77 loop.RunUntilIdle(); |
| 78 EXPECT_EQ(kNumObjects / 2, binding_set.size()); |
| 79 |
| 80 // Check that the first 5 interfaces have all been deleted. |
| 81 for (size_t i = 0; i < kNumObjects; i++) { |
| 82 bool expected = (i < kNumObjects / 2); |
| 83 EXPECT_EQ(expected, deleted_flags[i]); |
| 84 } |
| 85 |
| 86 // Invoke method foo() on the second five InterfacePointers. |
| 87 for (size_t i = kNumObjects / 2; i < kNumObjects; i++) { |
| 88 intrfc_ptrs[i]->Foo(); |
| 89 } |
| 90 loop.RunUntilIdle(); |
| 91 |
| 92 // Check that now the second five counts are two. |
| 93 for (size_t i = kNumObjects / 2; i < kNumObjects; i++) { |
| 94 EXPECT_EQ(2, impls[i]->call_count()); |
| 95 } |
| 96 |
| 97 // Invoke CloseAllBindings |
| 98 binding_set.CloseAllBindings(); |
| 99 EXPECT_EQ(0u, binding_set.size()); |
| 100 |
| 101 // Invoke method foo() on the second five InterfacePointers. |
| 102 for (size_t i = kNumObjects / 2; i < kNumObjects; i++) { |
| 103 intrfc_ptrs[i]->Foo(); |
| 104 } |
| 105 loop.RunUntilIdle(); |
| 106 |
| 107 // Check that all interfaces have all been deleted. |
| 108 for (size_t i = 0; i < kNumObjects; i++) { |
| 109 EXPECT_TRUE(deleted_flags[i]); |
| 110 } |
| 111 } |
| 112 |
| 113 } // namespace |
| 114 } // namespace common |
| 115 } // namespace mojo |
OLD | NEW |