| 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/public/cpp/bindings/interface_ptr_set.h" | |
| 6 | |
| 7 #include "gtest/gtest.h" | |
| 8 #include "mojo/public/cpp/bindings/binding.h" | |
| 9 #include "mojo/public/cpp/system/macros.h" | |
| 10 #include "mojo/public/cpp/utility/run_loop.h" | |
| 11 #include "mojo/public/interfaces/bindings/tests/minimal_interface.mojom.h" | |
| 12 | |
| 13 namespace mojo { | |
| 14 namespace common { | |
| 15 namespace { | |
| 16 | |
| 17 class MinimalInterfaceImpl : public test::MinimalInterface { | |
| 18 public: | |
| 19 explicit MinimalInterfaceImpl( | |
| 20 InterfaceRequest<test::MinimalInterface> request) | |
| 21 : binding_(this, request.Pass()) {} | |
| 22 | |
| 23 void Message() override { call_count_++; } | |
| 24 | |
| 25 void CloseMessagePipe() { binding_.Close(); } | |
| 26 | |
| 27 int call_count() { return call_count_; } | |
| 28 | |
| 29 private: | |
| 30 Binding<test::MinimalInterface> binding_; | |
| 31 int call_count_ = 0; | |
| 32 | |
| 33 MOJO_DISALLOW_COPY_AND_ASSIGN(MinimalInterfaceImpl); | |
| 34 }; | |
| 35 | |
| 36 // Tests all of the functionality of InterfacePtrSet. | |
| 37 TEST(InterfacePtrSetTest, FullLifeCycle) { | |
| 38 RunLoop loop; | |
| 39 | |
| 40 // Create 10 InterfacePtrs. | |
| 41 const size_t kNumObjects = 10; | |
| 42 InterfacePtr<test::MinimalInterface> intrfc_ptrs[kNumObjects]; | |
| 43 | |
| 44 // Create 10 MinimalInterfaceImpls and 10 message pipes and bind them all | |
| 45 // together. | |
| 46 std::unique_ptr<MinimalInterfaceImpl> impls[kNumObjects]; | |
| 47 for (size_t i = 0; i < kNumObjects; i++) { | |
| 48 impls[i].reset(new MinimalInterfaceImpl(GetProxy(&intrfc_ptrs[i]))); | |
| 49 } | |
| 50 | |
| 51 // Move all 10 InterfacePtrs into the set. | |
| 52 InterfacePtrSet<test::MinimalInterface> intrfc_ptr_set; | |
| 53 EXPECT_EQ(0u, intrfc_ptr_set.size()); | |
| 54 for (InterfacePtr<test::MinimalInterface>& ptr : intrfc_ptrs) { | |
| 55 intrfc_ptr_set.AddInterfacePtr(ptr.Pass()); | |
| 56 } | |
| 57 EXPECT_EQ(kNumObjects, intrfc_ptr_set.size()); | |
| 58 | |
| 59 // Check that initially all call counts are zero. | |
| 60 for (const std::unique_ptr<MinimalInterfaceImpl>& impl : impls) { | |
| 61 EXPECT_EQ(0, impl->call_count()); | |
| 62 } | |
| 63 | |
| 64 // Invoke ForAllPtrs(). | |
| 65 size_t num_invocations = 0; | |
| 66 intrfc_ptr_set.ForAllPtrs([&num_invocations](test::MinimalInterface* dummy) { | |
| 67 dummy->Message(); | |
| 68 num_invocations++; | |
| 69 }); | |
| 70 EXPECT_EQ(kNumObjects, num_invocations); | |
| 71 | |
| 72 // Check that now all call counts are one. | |
| 73 loop.RunUntilIdle(); | |
| 74 for (const std::unique_ptr<MinimalInterfaceImpl>& impl : impls) { | |
| 75 EXPECT_EQ(1, impl->call_count()); | |
| 76 } | |
| 77 | |
| 78 // Close the first 5 message pipes. This will (after RunUntilIdle) cause | |
| 79 // connection errors on the closed pipes which will cause the first five | |
| 80 // objects to be removed. | |
| 81 for (size_t i = 0; i < kNumObjects / 2; i++) { | |
| 82 impls[i]->CloseMessagePipe(); | |
| 83 } | |
| 84 EXPECT_EQ(kNumObjects, intrfc_ptr_set.size()); | |
| 85 loop.RunUntilIdle(); | |
| 86 EXPECT_EQ(kNumObjects / 2, intrfc_ptr_set.size()); | |
| 87 | |
| 88 // Invoke ForAllPtrs again on the remaining five pointers | |
| 89 intrfc_ptr_set.ForAllPtrs( | |
| 90 [](test::MinimalInterface* dummy) { dummy->Message(); }); | |
| 91 loop.RunUntilIdle(); | |
| 92 | |
| 93 // Check that now the first five counts are still 1 but the second five | |
| 94 // counts are two. | |
| 95 for (size_t i = 0; i < kNumObjects; i++) { | |
| 96 int expected = (i < kNumObjects / 2 ? 1 : 2); | |
| 97 EXPECT_EQ(expected, impls[i]->call_count()); | |
| 98 } | |
| 99 | |
| 100 // Close all of the MessagePipes and clear the set. | |
| 101 intrfc_ptr_set.CloseAll(); | |
| 102 | |
| 103 // Invoke ForAllPtrs() again. | |
| 104 intrfc_ptr_set.ForAllPtrs( | |
| 105 [](test::MinimalInterface* dummy) { dummy->Message(); }); | |
| 106 loop.RunUntilIdle(); | |
| 107 | |
| 108 // Check that the counts are the same as last time. | |
| 109 for (size_t i = 0; i < kNumObjects; i++) { | |
| 110 int expected = (i < kNumObjects / 2 ? 1 : 2); | |
| 111 EXPECT_EQ(expected, impls[i]->call_count()); | |
| 112 } | |
| 113 EXPECT_EQ(0u, intrfc_ptr_set.size()); | |
| 114 } | |
| 115 | |
| 116 } // namespace | |
| 117 } // namespace common | |
| 118 } // namespace mojo | |
| OLD | NEW |