OLD | NEW |
| (Empty) |
1 // Copyright 2017 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 <memory> | |
6 #include <vector> | |
7 | |
8 #include "base/bind.h" | |
9 #include "base/message_loop/message_loop.h" | |
10 #include "base/run_loop.h" | |
11 #include "services/resource_coordinator/coordination_unit/coordination_unit_prov
ider_impl.h" | |
12 #include "services/service_manager/public/cpp/service_context_ref.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 namespace resource_coordinator { | |
16 | |
17 namespace { | |
18 | |
19 void OnLastServiceRefDestroyed() { | |
20 // No-op. This is required by service_manager::ServiceContextRefFactory | |
21 // construction but not needed for the tests. | |
22 } | |
23 | |
24 class CoordinationUnitImplTest : public testing::Test { | |
25 public: | |
26 CoordinationUnitImplTest() | |
27 : service_ref_factory_(base::Bind(&OnLastServiceRefDestroyed)), | |
28 provider_(&service_ref_factory_) {} | |
29 ~CoordinationUnitImplTest() override {} | |
30 | |
31 // testing::Test: | |
32 void TearDown() override { base::RunLoop().RunUntilIdle(); } | |
33 | |
34 protected: | |
35 CoordinationUnitProviderImpl* provider() { return &provider_; } | |
36 | |
37 private: | |
38 base::MessageLoop message_loop_; | |
39 | |
40 service_manager::ServiceContextRefFactory service_ref_factory_; | |
41 CoordinationUnitProviderImpl provider_; | |
42 }; | |
43 | |
44 class TestCoordinationUnit : public mojom::CoordinationPolicyCallback { | |
45 public: | |
46 TestCoordinationUnit(CoordinationUnitProviderImpl* provider, | |
47 const CoordinationUnitType& type, | |
48 const std::string& id) | |
49 : binding_(this) { | |
50 CHECK(provider); | |
51 | |
52 CoordinationUnitID new_cu_id(type, id); | |
53 mojom::CoordinationUnitPtr coordination_unit; | |
54 provider->CreateCoordinationUnit(mojo::MakeRequest(&coordination_unit_), | |
55 new_cu_id); | |
56 | |
57 base::RunLoop callback; | |
58 SetGetIDClosure(callback.QuitClosure()); | |
59 coordination_unit_->SetCoordinationPolicyCallback(GetPolicyCallback()); | |
60 // Forces us to wait for the creation of the CUID to finish. | |
61 coordination_unit_->GetID(base::Bind(&TestCoordinationUnit::GetIDCallback, | |
62 base::Unretained(this))); | |
63 | |
64 callback.Run(); | |
65 } | |
66 | |
67 void GetIDCallback(const CoordinationUnitID& cu_id) { | |
68 id_ = cu_id; | |
69 get_id_closure_.Run(); | |
70 } | |
71 | |
72 void SetGetIDClosure(const base::Closure& get_id_closure) { | |
73 get_id_closure_ = get_id_closure; | |
74 } | |
75 | |
76 void SetPolicyClosure(const base::Closure& policy_closure) { | |
77 policy_update_closure_ = policy_closure; | |
78 } | |
79 | |
80 mojom::CoordinationPolicyCallbackPtr GetPolicyCallback() { | |
81 return binding_.CreateInterfacePtrAndBind(); | |
82 } | |
83 | |
84 // The CU will always send policy updates on events (including parent events) | |
85 void ForcePolicyUpdates() { | |
86 base::RunLoop callback; | |
87 SetPolicyClosure(callback.QuitClosure()); | |
88 mojom::EventPtr event = mojom::Event::New(); | |
89 event->type = mojom::EventType::kTestEvent; | |
90 coordination_unit_->SendEvent(std::move(event)); | |
91 callback.Run(); | |
92 } | |
93 | |
94 const mojom::CoordinationUnitPtr& interface() const { | |
95 return coordination_unit_; | |
96 } | |
97 | |
98 const CoordinationUnitID& id() const { return id_; } | |
99 | |
100 // mojom::CoordinationPolicyCallback: | |
101 void SetCoordinationPolicy( | |
102 resource_coordinator::mojom::CoordinationPolicyPtr policy) override { | |
103 if (policy_update_closure_) { | |
104 policy_update_closure_.Run(); | |
105 } | |
106 } | |
107 | |
108 private: | |
109 base::Closure policy_update_closure_; | |
110 base::Closure get_id_closure_; | |
111 | |
112 mojo::Binding<mojom::CoordinationPolicyCallback> binding_; | |
113 mojom::CoordinationUnitPtr coordination_unit_; | |
114 CoordinationUnitID id_; | |
115 }; | |
116 | |
117 } // namespace | |
118 | |
119 TEST_F(CoordinationUnitImplTest, BasicPolicyCallback) { | |
120 TestCoordinationUnit test_coordination_unit( | |
121 provider(), CoordinationUnitType::kWebContents, "test_id"); | |
122 test_coordination_unit.ForcePolicyUpdates(); | |
123 } | |
124 | |
125 TEST_F(CoordinationUnitImplTest, AddChild) { | |
126 TestCoordinationUnit parent_unit( | |
127 provider(), CoordinationUnitType::kWebContents, "parent_unit"); | |
128 | |
129 TestCoordinationUnit child_unit( | |
130 provider(), CoordinationUnitType::kWebContents, "child_unit"); | |
131 | |
132 child_unit.ForcePolicyUpdates(); | |
133 parent_unit.ForcePolicyUpdates(); | |
134 | |
135 { | |
136 base::RunLoop callback; | |
137 child_unit.SetPolicyClosure(callback.QuitClosure()); | |
138 parent_unit.interface()->AddChild(child_unit.id()); | |
139 callback.Run(); | |
140 } | |
141 | |
142 { | |
143 base::RunLoop parent_callback; | |
144 base::RunLoop child_callback; | |
145 parent_unit.SetPolicyClosure(parent_callback.QuitClosure()); | |
146 child_unit.SetPolicyClosure(child_callback.QuitClosure()); | |
147 | |
148 // This event should force the policy to recalculated for all children. | |
149 mojom::EventPtr event = mojom::Event::New(); | |
150 event->type = mojom::EventType::kTestEvent; | |
151 parent_unit.interface()->SendEvent(std::move(event)); | |
152 | |
153 parent_callback.Run(); | |
154 child_callback.Run(); | |
155 } | |
156 } | |
157 | |
158 TEST_F(CoordinationUnitImplTest, CyclicGraphUnits) { | |
159 TestCoordinationUnit parent_unit( | |
160 provider(), CoordinationUnitType::kWebContents, std::string()); | |
161 | |
162 TestCoordinationUnit child_unit( | |
163 provider(), CoordinationUnitType::kWebContents, std::string()); | |
164 | |
165 child_unit.ForcePolicyUpdates(); | |
166 parent_unit.ForcePolicyUpdates(); | |
167 | |
168 { | |
169 base::RunLoop callback; | |
170 child_unit.SetPolicyClosure(callback.QuitClosure()); | |
171 parent_unit.interface()->AddChild(child_unit.id()); | |
172 callback.Run(); | |
173 } | |
174 | |
175 // This should fail, due to the existing child-parent relationship. | |
176 // Otherwise we end up with infinite recursion and crash when recalculating | |
177 // policies below. | |
178 child_unit.interface()->AddChild(parent_unit.id()); | |
179 | |
180 { | |
181 base::RunLoop parent_callback; | |
182 base::RunLoop child_callback; | |
183 parent_unit.SetPolicyClosure(parent_callback.QuitClosure()); | |
184 child_unit.SetPolicyClosure(child_callback.QuitClosure()); | |
185 | |
186 // This event should force the policy to recalculated for all children. | |
187 mojom::EventPtr event = mojom::Event::New(); | |
188 event->type = mojom::EventType::kTestEvent; | |
189 parent_unit.interface()->SendEvent(std::move(event)); | |
190 | |
191 parent_callback.Run(); | |
192 child_callback.Run(); | |
193 } | |
194 } | |
195 | |
196 } // namespace resource_coordinator | |
OLD | NEW |