Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(211)

Side by Side Diff: services/resource_coordinator/coordination_unit/coordination_unit_impl_unittest.cc

Issue 2798713002: Global Resource Coordinator: Basic service internals (Closed)
Patch Set: Wrap EventType directly in resource_coordinator namespace when using helper Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "testing/gtest/include/gtest/gtest.h"
13
14 namespace resource_coordinator {
15
16 namespace {
17
18 class CoordinationUnitImplTest : public testing::Test {
19 public:
20 CoordinationUnitImplTest() : provider_(nullptr) {}
21 ~CoordinationUnitImplTest() override {}
22
23 // testing::Test:
24 void TearDown() override { base::RunLoop().RunUntilIdle(); }
25
26 protected:
27 CoordinationUnitProviderImpl* provider() { return &provider_; }
28
29 private:
30 base::MessageLoop message_loop_;
31
32 CoordinationUnitProviderImpl provider_;
33 };
34
35 class TestCoordinationUnit : public mojom::PolicyCallback {
36 public:
37 TestCoordinationUnit(CoordinationUnitProviderImpl* provider,
38 const CoordinationUnitType& type,
39 const std::string& id)
40 : binding_(this) {
41 CHECK(provider);
42
43 CoordinationUnitID new_cu_id(type, id);
44 mojom::CoordinationUnitPtr coordination_unit;
45 provider->CreateCoordinationUnit(mojo::MakeRequest(&coordination_unit_),
46 new_cu_id);
47
48 base::RunLoop callback;
49 SetGetIDClosure(callback.QuitClosure());
50 coordination_unit_->SetPolicyCallback(GetPolicyCallback());
51 // Forces us to wait for the creation of the CUID to finish.
52 coordination_unit_->GetID(base::Bind(&TestCoordinationUnit::GetIDCallback,
53 base::Unretained(this)));
54
55 callback.Run();
56 }
57
58 void GetIDCallback(const CoordinationUnitID& cu_id) {
59 id_ = cu_id;
60 get_id_closure_.Run();
61 }
62
63 void SetGetIDClosure(const base::Closure& get_id_closure) {
64 get_id_closure_ = get_id_closure;
65 }
66
67 void SetPolicyClosure(const base::Closure& policy_closure) {
68 policy_update_closure_ = policy_closure;
69 }
70
71 mojom::PolicyCallbackPtr GetPolicyCallback() {
72 return binding_.CreateInterfacePtrAndBind();
73 }
74
75 // The CU will always send policy updates on events (including parent events)
76 void ForcePolicyUpdates() {
77 base::RunLoop callback;
78 SetPolicyClosure(callback.QuitClosure());
79 mojom::EventPtr event = mojom::Event::New();
80 event->type = mojom::EventType::TEST_EVENT;
81 coordination_unit_->SendEvent(std::move(event));
82 callback.Run();
83 }
84
85 const mojom::CoordinationUnitPtr& interface() const {
86 return coordination_unit_;
87 }
88
89 const CoordinationUnitID& id() const { return id_; }
90
91 // mojom::PolicyCallback:
92 void SetPolicy(resource_coordinator::mojom::PolicyPtr policy) override {
93 if (policy_update_closure_)
94 policy_update_closure_.Run();
95 }
96
97 private:
98 base::Closure policy_update_closure_;
99 base::Closure get_id_closure_;
100
101 mojo::Binding<mojom::PolicyCallback> binding_;
102 mojom::CoordinationUnitPtr coordination_unit_;
103 CoordinationUnitID id_;
104 };
105
106 } // namespace
107
108 TEST_F(CoordinationUnitImplTest, BasicPolicyCallback) {
109 TestCoordinationUnit test_coordination_unit(
110 provider(), CoordinationUnitType::WEBCONTENTS, "test_id");
111 test_coordination_unit.ForcePolicyUpdates();
112 }
113
114 TEST_F(CoordinationUnitImplTest, AddChild) {
115 TestCoordinationUnit parent_unit(
116 provider(), CoordinationUnitType::WEBCONTENTS, "parent_unit");
117
118 TestCoordinationUnit child_unit(provider(), CoordinationUnitType::WEBCONTENTS,
119 "child_unit");
120
121 child_unit.ForcePolicyUpdates();
122 parent_unit.ForcePolicyUpdates();
123
124 {
125 base::RunLoop callback;
126 child_unit.SetPolicyClosure(callback.QuitClosure());
127 parent_unit.interface()->AddChild(child_unit.id());
128 callback.Run();
129 }
130
131 {
132 base::RunLoop parent_callback;
133 base::RunLoop child_callback;
134 parent_unit.SetPolicyClosure(parent_callback.QuitClosure());
135 child_unit.SetPolicyClosure(child_callback.QuitClosure());
136
137 // This event should force the policy to recalculated for all children.
138 mojom::EventPtr event = mojom::Event::New();
139 event->type = mojom::EventType::TEST_EVENT;
140 parent_unit.interface()->SendEvent(std::move(event));
141
142 parent_callback.Run();
143 child_callback.Run();
144 }
145 }
146
147 TEST_F(CoordinationUnitImplTest, CyclicGraphUnits) {
148 TestCoordinationUnit parent_unit(
149 provider(), CoordinationUnitType::WEBCONTENTS, std::string());
150
151 TestCoordinationUnit child_unit(provider(), CoordinationUnitType::WEBCONTENTS,
152 std::string());
153
154 child_unit.ForcePolicyUpdates();
155 parent_unit.ForcePolicyUpdates();
156
157 {
158 base::RunLoop callback;
159 child_unit.SetPolicyClosure(callback.QuitClosure());
160 parent_unit.interface()->AddChild(child_unit.id());
161 callback.Run();
162 }
163
164 // This should fail, due to the existing child-parent relationship.
165 // Otherwise we end up with infinite recursion and crash when recalculating
166 // policies below.
167 child_unit.interface()->AddChild(parent_unit.id());
168
169 {
170 base::RunLoop parent_callback;
171 base::RunLoop child_callback;
172 parent_unit.SetPolicyClosure(parent_callback.QuitClosure());
173 child_unit.SetPolicyClosure(child_callback.QuitClosure());
174
175 // This event should force the policy to recalculated for all children.
176 mojom::EventPtr event = mojom::Event::New();
177 event->type = mojom::EventType::TEST_EVENT;
178 parent_unit.interface()->SendEvent(std::move(event));
179
180 parent_callback.Run();
181 child_callback.Run();
182 }
183 }
184
185 } // namespace resource_coordinator
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698