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