Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 #include "mojo/common/weak_interface_ptr_set.h" | 5 #include "mojo/common/interface_ptr_set.h" |
| 6 | 6 |
| 7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "mojo/common/message_pump_mojo.h" | 8 #include "mojo/common/message_pump_mojo.h" |
| 9 #include "mojo/common/test_interfaces.mojom.h" | 9 #include "mojo/common/test_interfaces.mojom.h" |
| 10 #include "mojo/public/cpp/bindings/binding.h" | 10 #include "mojo/public/cpp/bindings/binding.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 12 |
| 13 namespace mojo { | 13 namespace mojo { |
| 14 namespace common { | 14 namespace common { |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 class DummyImpl : public tests::Dummy { | 17 class DummyImpl : public tests::Dummy { |
| 18 public: | 18 public: |
| 19 explicit DummyImpl(InterfaceRequest<tests::Dummy> request) | 19 explicit DummyImpl(InterfaceRequest<tests::Dummy> request) |
| 20 : binding_(this, request.Pass()) {} | 20 : binding_(this, request.Pass()) {} |
| 21 | 21 |
| 22 void Foo() override { call_count_++; } | 22 void Foo() override { call_count_++; } |
| 23 | 23 |
| 24 void CloseMessagePipe() { binding_.Close(); } | 24 void CloseMessagePipe() { binding_.Close(); } |
| 25 | 25 |
| 26 int call_count() { return call_count_; } | 26 int call_count() { return call_count_; } |
| 27 | 27 |
| 28 private: | 28 private: |
| 29 Binding<tests::Dummy> binding_; | 29 Binding<tests::Dummy> binding_; |
| 30 int call_count_ = 0; | 30 int call_count_ = 0; |
| 31 | 31 |
| 32 DISALLOW_COPY_AND_ASSIGN(DummyImpl); | 32 DISALLOW_COPY_AND_ASSIGN(DummyImpl); |
| 33 }; | 33 }; |
| 34 | 34 |
| 35 // Tests all of the functionality of WeakInterfacePtrSet. | 35 // Tests all of the functionality of InterfacePtrSet. |
| 36 TEST(WeakInterfacePtr, FullLifeCycleTest) { | 36 TEST(InterfacePtrSet, FullLifeCycleTest) { |
|
viettrungluu
2015/07/20 16:48:55
nit (which I should have caught previously): For s
rudominer
2015/07/20 16:56:37
DONE (in ps3)
| |
| 37 base::MessageLoop loop(MessagePumpMojo::Create()); | 37 base::MessageLoop loop(MessagePumpMojo::Create()); |
| 38 | 38 |
| 39 // Create 10 InterfacePtrs. | 39 // Create 10 InterfacePtrs. |
| 40 const size_t kNumObjects = 10; | 40 const size_t kNumObjects = 10; |
| 41 InterfacePtr<tests::Dummy> intrfc_ptrs[kNumObjects]; | 41 InterfacePtr<tests::Dummy> intrfc_ptrs[kNumObjects]; |
| 42 | 42 |
| 43 // Create 10 DummyImpls and 10 message pipes and bind them all together. | 43 // Create 10 DummyImpls and 10 message pipes and bind them all together. |
| 44 std::unique_ptr<DummyImpl> impls[kNumObjects]; | 44 std::unique_ptr<DummyImpl> impls[kNumObjects]; |
| 45 for (size_t i = 0; i < kNumObjects; i++) { | 45 for (size_t i = 0; i < kNumObjects; i++) { |
| 46 impls[i].reset(new DummyImpl(GetProxy(&intrfc_ptrs[i]))); | 46 impls[i].reset(new DummyImpl(GetProxy(&intrfc_ptrs[i]))); |
| 47 } | 47 } |
| 48 | 48 |
| 49 // Move all 10 InterfacePtrs into the set. | 49 // Move all 10 InterfacePtrs into the set. |
| 50 WeakInterfacePtrSet<tests::Dummy> intrfc_ptr_set; | 50 InterfacePtrSet<tests::Dummy> intrfc_ptr_set; |
| 51 EXPECT_EQ(0u, intrfc_ptr_set.size()); | 51 EXPECT_EQ(0u, intrfc_ptr_set.size()); |
| 52 for (InterfacePtr<tests::Dummy>& ptr : intrfc_ptrs) { | 52 for (InterfacePtr<tests::Dummy>& ptr : intrfc_ptrs) { |
| 53 intrfc_ptr_set.AddInterfacePtr(ptr.Pass()); | 53 intrfc_ptr_set.AddInterfacePtr(ptr.Pass()); |
| 54 } | 54 } |
| 55 EXPECT_EQ(kNumObjects, intrfc_ptr_set.size()); | 55 EXPECT_EQ(kNumObjects, intrfc_ptr_set.size()); |
| 56 | 56 |
| 57 // Check that initially all call counts are zero. | 57 // Check that initially all call counts are zero. |
| 58 for (const std::unique_ptr<DummyImpl>& impl : impls) { | 58 for (const std::unique_ptr<DummyImpl>& impl : impls) { |
| 59 EXPECT_EQ(0, impl->call_count()); | 59 EXPECT_EQ(0, impl->call_count()); |
| 60 } | 60 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 105 for (size_t i = 0; i < kNumObjects; i++) { | 105 for (size_t i = 0; i < kNumObjects; i++) { |
| 106 int expected = (i < kNumObjects / 2 ? 1 : 2); | 106 int expected = (i < kNumObjects / 2 ? 1 : 2); |
| 107 EXPECT_EQ(expected, impls[i]->call_count()); | 107 EXPECT_EQ(expected, impls[i]->call_count()); |
| 108 } | 108 } |
| 109 EXPECT_EQ(0u, intrfc_ptr_set.size()); | 109 EXPECT_EQ(0u, intrfc_ptr_set.size()); |
| 110 } | 110 } |
| 111 | 111 |
| 112 } // namespace | 112 } // namespace |
| 113 } // namespace common | 113 } // namespace common |
| 114 } // namespace mojo | 114 } // namespace mojo |
| OLD | NEW |