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