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/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 { | |
16 | |
17 class MinimalInterfaceImpl : public test::MinimalInterface { | |
18 public: | |
19 MinimalInterfaceImpl() {} | |
20 | |
21 void Message() override { call_count_++; } | |
22 | |
23 int call_count() const { return call_count_; } | |
24 | |
25 private: | |
26 int call_count_ = 0; | |
27 | |
28 MOJO_DISALLOW_COPY_AND_ASSIGN(MinimalInterfaceImpl); | |
29 }; | |
30 | |
31 // Tests all of the functionality of BindingSet. | |
32 TEST(BindingSetTest, FullLifeCycle) { | |
33 RunLoop loop; | |
34 | |
35 // Create 10 InterfacePtrs and MinimalInterfaceImpls. | |
36 const size_t kNumObjects = 10; | |
37 InterfacePtr<test::MinimalInterface> intrfc_ptrs[kNumObjects]; | |
38 MinimalInterfaceImpl impls[kNumObjects]; | |
39 | |
40 // Create 10 message pipes, bind everything together, and add the | |
41 // bindings to binding_set. | |
42 BindingSet<test::MinimalInterface> binding_set; | |
43 EXPECT_EQ(0u, binding_set.size()); | |
44 for (size_t i = 0; i < kNumObjects; i++) { | |
45 binding_set.AddBinding(&impls[i], GetProxy(&intrfc_ptrs[i])); | |
46 } | |
47 EXPECT_EQ(kNumObjects, binding_set.size()); | |
48 | |
49 // Check that initially all call counts are zero. | |
50 for (const auto& impl : impls) { | |
51 EXPECT_EQ(0, impl.call_count()); | |
52 } | |
53 | |
54 // Invoke method foo() on all 10 InterfacePointers. | |
55 for (InterfacePtr<test::MinimalInterface>& ptr : intrfc_ptrs) { | |
56 ptr->Message(); | |
57 } | |
58 | |
59 // Check that now all call counts are one. | |
60 loop.RunUntilIdle(); | |
61 for (const auto& impl : impls) { | |
62 EXPECT_EQ(1, impl.call_count()); | |
63 } | |
64 | |
65 // Close the first 5 message pipes and destroy the first five | |
66 // InterfacePtrs. | |
67 for (size_t i = 0; i < kNumObjects / 2; i++) { | |
68 intrfc_ptrs[i].reset(); | |
69 } | |
70 | |
71 // Check that the set contains only five elements now. | |
72 loop.RunUntilIdle(); | |
73 EXPECT_EQ(kNumObjects / 2, binding_set.size()); | |
74 | |
75 // Invoke method foo() on the second five InterfacePointers. | |
76 for (size_t i = kNumObjects / 2; i < kNumObjects; i++) { | |
77 intrfc_ptrs[i]->Message(); | |
78 } | |
79 loop.RunUntilIdle(); | |
80 | |
81 // Check that now the first five counts are still 1 but the second five | |
82 // counts are two. | |
83 for (size_t i = 0; i < kNumObjects; i++) { | |
84 int expected = (i < kNumObjects / 2 ? 1 : 2); | |
85 EXPECT_EQ(expected, impls[i].call_count()); | |
86 } | |
87 | |
88 // Invoke CloseAllBindings | |
89 binding_set.CloseAllBindings(); | |
90 EXPECT_EQ(0u, binding_set.size()); | |
91 | |
92 // Invoke method foo() on the second five InterfacePointers. | |
93 for (size_t i = kNumObjects / 2; i < kNumObjects; i++) { | |
94 intrfc_ptrs[i]->Message(); | |
95 } | |
96 loop.RunUntilIdle(); | |
97 | |
98 // Check that the call counts are the same as before. | |
99 for (size_t i = 0; i < kNumObjects; i++) { | |
100 int expected = (i < kNumObjects / 2 ? 1 : 2); | |
101 EXPECT_EQ(expected, impls[i].call_count()); | |
102 } | |
103 } | |
104 | |
105 } // namespace | |
106 } // namespace mojo | |
OLD | NEW |