Chromium Code Reviews| 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 <string> | |
| 6 #include <utility> | |
| 7 | |
| 8 #include "base/memory/ptr_util.h" | |
| 9 #include "base/process/process_handle.h" | |
| 10 #include "base/run_loop.h" | |
| 11 #include "services/resource_coordinator/coordination_unit/coordination_unit_fact ory.h" | |
| 12 #include "services/resource_coordinator/coordination_unit/coordination_unit_grap h_observer.h" | |
| 13 #include "services/resource_coordinator/coordination_unit/coordination_unit_impl .h" | |
| 14 #include "services/resource_coordinator/coordination_unit/coordination_unit_impl _unittest_util.h" | |
| 15 #include "services/resource_coordinator/coordination_unit/coordination_unit_mana ger.h" | |
| 16 #include "services/resource_coordinator/public/cpp/coordination_unit_id.h" | |
| 17 #include "services/resource_coordinator/public/cpp/coordination_unit_types.h" | |
| 18 #include "services/resource_coordinator/public/interfaces/coordination_unit.mojo m.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 | |
| 21 namespace resource_coordinator { | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 class CoordinationUnitGraphObserverTest : public CoordinationUnitImplTestBase { | |
| 26 }; | |
| 27 | |
| 28 class TestBasicAPICoordinationUnitGraphObserver | |
| 29 : public CoordinationUnitGraphObserver { | |
| 30 public: | |
| 31 TestBasicAPICoordinationUnitGraphObserver() | |
| 32 : TestBasicAPICoordinationUnitGraphObserver( | |
| 33 CoordinationUnitType::kInvalidType) {} | |
| 34 | |
| 35 void OnCoordinationUnitCreatedEvent( | |
| 36 CoordinationUnitImpl* coordination_unit) override { | |
| 37 ++on_coordination_unit_created_invocations_; | |
| 38 | |
| 39 coordination_unit->ObserveOnChildAddedEvent(this); | |
| 40 coordination_unit->ObserveOnParentAddedEvent(this); | |
| 41 coordination_unit->ObserveOnPropertyChangedEvent(this); | |
| 42 coordination_unit->ObserveOnChildRemovedEvent(this); | |
| 43 coordination_unit->ObserveOnParentRemovedEvent(this); | |
| 44 coordination_unit->ObserveOnWillBeDestroyedEvent(this); | |
| 45 } | |
| 46 | |
| 47 size_t on_child_added_listener_invocations() { | |
| 48 return on_child_added_listener_invocations_; | |
| 49 } | |
| 50 size_t on_parent_added_listener_invocations() { | |
| 51 return on_parent_added_listener_invocations_; | |
| 52 } | |
| 53 size_t on_coordination_unit_created_invocations() { | |
| 54 return on_coordination_unit_created_invocations_; | |
| 55 } | |
| 56 size_t on_property_changed_listener_invocations() { | |
| 57 return on_property_changed_listener_invocations_; | |
| 58 } | |
| 59 size_t on_child_removed_listener_invocations() { | |
| 60 return on_child_removed_listener_invocations_; | |
| 61 } | |
| 62 size_t on_parent_removed_listener_invocations() { | |
| 63 return on_parent_removed_listener_invocations_; | |
| 64 } | |
| 65 size_t on_will_be_destroyed_listener_invocations() { | |
| 66 return on_will_be_destroyed_listener_invocations_; | |
| 67 } | |
| 68 | |
| 69 void OnChildAddedEvent( | |
| 70 const CoordinationUnitImpl* coordination_unit, | |
| 71 const CoordinationUnitImpl* child_coordination_unit) override { | |
| 72 ++on_child_added_listener_invocations_; | |
| 73 } | |
| 74 void OnParentAddedEvent( | |
| 75 const CoordinationUnitImpl* coordination_unit, | |
| 76 const CoordinationUnitImpl* parent_coordination_unit) override { | |
| 77 ++on_parent_added_listener_invocations_; | |
| 78 } | |
| 79 void OnPropertyChangedEvent(const CoordinationUnitImpl* coordination_unit, | |
| 80 mojom::PropertyType property) override { | |
| 81 ++on_property_changed_listener_invocations_; | |
| 82 } | |
| 83 void OnChildRemovedEvent( | |
| 84 const CoordinationUnitImpl* coordination_unit, | |
| 85 const CoordinationUnitImpl* former_child_coordination_unit) override { | |
| 86 ++on_child_removed_listener_invocations_; | |
| 87 } | |
| 88 void OnParentRemovedEvent( | |
| 89 const CoordinationUnitImpl* coordination_unit, | |
| 90 const CoordinationUnitImpl* former_parent_coordination_unit) override { | |
| 91 ++on_parent_removed_listener_invocations_; | |
| 92 } | |
| 93 void OnWillBeDestroyedEvent( | |
| 94 const CoordinationUnitImpl* coordination_unit) override { | |
| 95 ++on_will_be_destroyed_listener_invocations_; | |
| 96 } | |
| 97 | |
| 98 protected: | |
| 99 explicit TestBasicAPICoordinationUnitGraphObserver( | |
| 100 CoordinationUnitType filter) | |
| 101 : CoordinationUnitGraphObserver(filter), | |
| 102 on_child_added_listener_invocations_(0u), | |
| 103 on_parent_added_listener_invocations_(0u), | |
| 104 on_coordination_unit_created_invocations_(0u), | |
| 105 on_property_changed_listener_invocations_(0u), | |
| 106 on_child_removed_listener_invocations_(0u), | |
| 107 on_parent_removed_listener_invocations_(0u), | |
| 108 on_will_be_destroyed_listener_invocations_(0u) {} | |
| 109 | |
| 110 size_t on_child_added_listener_invocations_; | |
| 111 size_t on_parent_added_listener_invocations_; | |
| 112 size_t on_coordination_unit_created_invocations_; | |
| 113 size_t on_property_changed_listener_invocations_; | |
| 114 size_t on_child_removed_listener_invocations_; | |
| 115 size_t on_parent_removed_listener_invocations_; | |
| 116 size_t on_will_be_destroyed_listener_invocations_; | |
| 117 }; | |
| 118 | |
| 119 class TestFilterAPICoordinationUnitGraphObserver | |
| 120 : public TestBasicAPICoordinationUnitGraphObserver { | |
| 121 public: | |
| 122 explicit TestFilterAPICoordinationUnitGraphObserver( | |
| 123 CoordinationUnitType filter) | |
| 124 : TestBasicAPICoordinationUnitGraphObserver(filter) {} | |
| 125 | |
| 126 void OnCoordinationUnitCreatedEvent( | |
| 127 CoordinationUnitImpl* coordination_unit) override { | |
| 128 ++on_coordination_unit_created_invocations_; | |
| 129 | |
| 130 coordination_unit->ObserveOnChildAddedEvent(this, | |
| 131 CoordinationUnitType::kFrame); | |
| 132 coordination_unit->ObserveOnParentAddedEvent( | |
| 133 this, CoordinationUnitType::kProcess); | |
| 134 // TODO(matthalp) Use property mojom::PropertyType enum once it is created. | |
| 135 // Currently the only enum is mojom::PropertyType::kTest which is mean to | |
| 136 // be filtered out in the test this observer class is used in. | |
| 137 coordination_unit->ObserveOnPropertyChangedEvent( | |
| 138 this, static_cast<mojom::PropertyType>(1)); | |
| 139 coordination_unit->ObserveOnChildRemovedEvent( | |
| 140 this, CoordinationUnitType::kNavigation); | |
| 141 coordination_unit->ObserveOnParentRemovedEvent( | |
| 142 this, CoordinationUnitType::kWebContents); | |
| 143 coordination_unit->ObserveOnWillBeDestroyedEvent(this); | |
| 144 } | |
| 145 }; | |
| 146 | |
| 147 } // namespace | |
| 148 | |
| 149 TEST_F(CoordinationUnitGraphObserverTest, CallbacksInvokedNoFilters) { | |
| 150 TestBasicAPICoordinationUnitGraphObserver* observer = | |
| 151 new TestBasicAPICoordinationUnitGraphObserver(); | |
| 152 | |
| 153 // The observer will be deleted after this test executes when the | |
| 154 // CoordinationUnitManager object goes out of scope and destructs. | |
| 155 coordination_unit_manager()->RegisterObserver( | |
| 156 std::unique_ptr<TestBasicAPICoordinationUnitGraphObserver>(observer)); | |
|
Zhen Wang
2017/06/19 22:54:25
This is for line 150-156.
In general using raw po
matthalp
2017/06/20 19:02:51
Done.
| |
| 157 | |
| 158 // The CoordinationUnit types are intentionally different to make | |
| 159 // sure filtering, which is not disabled, does not occur. | |
| 160 CoordinationUnitID parent_cu_id(CoordinationUnitType::kWebContents, | |
| 161 std::string()); | |
| 162 CoordinationUnitID child_cu_id(CoordinationUnitType::kFrame, std::string()); | |
| 163 | |
| 164 std::unique_ptr<CoordinationUnitImpl> parent_coordination_unit = | |
| 165 coordination_unit_factory::CreateCoordinationUnit( | |
| 166 parent_cu_id, service_context_ref_factory()->CreateRef()); | |
| 167 std::unique_ptr<CoordinationUnitImpl> child_coordination_unit = | |
| 168 coordination_unit_factory::CreateCoordinationUnit( | |
| 169 child_cu_id, service_context_ref_factory()->CreateRef()); | |
| 170 | |
| 171 coordination_unit_manager()->NotifyObserversCoordinationUnitCreated( | |
| 172 parent_coordination_unit.get()); | |
| 173 coordination_unit_manager()->NotifyObserversCoordinationUnitCreated( | |
| 174 child_coordination_unit.get()); | |
| 175 | |
| 176 parent_coordination_unit->AddChild(child_coordination_unit->id()); | |
| 177 parent_coordination_unit->RemoveChild(child_coordination_unit->id()); | |
| 178 | |
| 179 parent_coordination_unit->SetProperty(mojom::PropertyType::kTest, | |
| 180 base::Value(42)); | |
| 181 | |
| 182 child_coordination_unit->WillBeDestroyed(); | |
| 183 parent_coordination_unit->WillBeDestroyed(); | |
| 184 | |
| 185 EXPECT_EQ(1u, observer->on_child_added_listener_invocations()); | |
| 186 EXPECT_EQ(1u, observer->on_child_removed_listener_invocations()); | |
| 187 EXPECT_EQ(2u, observer->on_coordination_unit_created_invocations()); | |
|
Zhen Wang
2017/06/19 22:54:24
nit: it probably helps readability to do this chec
matthalp
2017/06/20 19:02:51
Good suggestion. I moved the EXPECT_EQ directly af
| |
| 188 EXPECT_EQ(1u, observer->on_parent_added_listener_invocations()); | |
| 189 EXPECT_EQ(1u, observer->on_parent_removed_listener_invocations()); | |
| 190 EXPECT_EQ(1u, observer->on_property_changed_listener_invocations()); | |
| 191 EXPECT_EQ(2u, observer->on_will_be_destroyed_listener_invocations()); | |
| 192 } | |
| 193 | |
| 194 TEST_F(CoordinationUnitGraphObserverTest, CallbacksInvokedWithoutFilters) { | |
|
Zhen Wang
2017/06/19 22:54:25
Do you mean "With" filters here? And "WithoutFilte
matthalp
2017/06/20 19:02:52
Filtering has been removed -- this is no longer ap
| |
| 195 TestFilterAPICoordinationUnitGraphObserver* observer = | |
|
Zhen Wang
2017/06/19 22:54:24
ditto. Do not use raw pointers here.
matthalp
2017/06/20 19:02:52
Filtering has been removed -- this is no longer ap
| |
| 196 new TestFilterAPICoordinationUnitGraphObserver( | |
| 197 CoordinationUnitType::kFrame); | |
| 198 coordination_unit_manager()->RegisterObserver( | |
| 199 std::unique_ptr<TestFilterAPICoordinationUnitGraphObserver>(observer)); | |
| 200 | |
| 201 // The CoordinationUnit types are intentionally different to test | |
| 202 // that filtering is working correctly. | |
| 203 CoordinationUnitID process_cu_id(CoordinationUnitType::kProcess, | |
| 204 std::string()); | |
| 205 CoordinationUnitID tab_cu_id(CoordinationUnitType::kWebContents, | |
| 206 std::string()); | |
| 207 CoordinationUnitID root_frame_cu_id(CoordinationUnitType::kFrame, | |
| 208 std::string()); | |
| 209 CoordinationUnitID frame_cu_id(CoordinationUnitType::kFrame, std::string()); | |
| 210 CoordinationUnitID navigation_cu_id(CoordinationUnitType::kNavigation, | |
| 211 std::string()); | |
| 212 | |
| 213 std::unique_ptr<CoordinationUnitImpl> process_coordination_unit = | |
| 214 coordination_unit_factory::CreateCoordinationUnit( | |
| 215 process_cu_id, service_context_ref_factory()->CreateRef()); | |
| 216 std::unique_ptr<CoordinationUnitImpl> tab_coordination_unit = | |
| 217 coordination_unit_factory::CreateCoordinationUnit( | |
| 218 tab_cu_id, service_context_ref_factory()->CreateRef()); | |
| 219 std::unique_ptr<CoordinationUnitImpl> root_frame_coordination_unit = | |
| 220 coordination_unit_factory::CreateCoordinationUnit( | |
| 221 root_frame_cu_id, service_context_ref_factory()->CreateRef()); | |
| 222 std::unique_ptr<CoordinationUnitImpl> frame_coordination_unit = | |
| 223 coordination_unit_factory::CreateCoordinationUnit( | |
| 224 frame_cu_id, service_context_ref_factory()->CreateRef()); | |
| 225 std::unique_ptr<CoordinationUnitImpl> navigation_coordination_unit = | |
| 226 coordination_unit_factory::CreateCoordinationUnit( | |
| 227 navigation_cu_id, service_context_ref_factory()->CreateRef()); | |
| 228 | |
| 229 // Should only invoke the OnCoordinationUnitCreated listener for | |
| 230 // the root_frame and frame CoordinationUnits because the observer | |
| 231 // filter is set to CoordinationUnitType::kFrame. | |
| 232 coordination_unit_manager()->NotifyObserversCoordinationUnitCreated( | |
| 233 process_coordination_unit.get()); | |
| 234 coordination_unit_manager()->NotifyObserversCoordinationUnitCreated( | |
| 235 tab_coordination_unit.get()); | |
| 236 coordination_unit_manager()->NotifyObserversCoordinationUnitCreated( | |
| 237 root_frame_coordination_unit.get()); | |
| 238 coordination_unit_manager()->NotifyObserversCoordinationUnitCreated( | |
| 239 frame_coordination_unit.get()); | |
| 240 coordination_unit_manager()->NotifyObserversCoordinationUnitCreated( | |
| 241 navigation_coordination_unit.get()); | |
| 242 | |
| 243 // Test AddChild filtering. The AddChild filter for the frame | |
| 244 // CoordinationUnit has been set to only execute when the child | |
| 245 // is a CoordinationUnitType::kFrame so the callback will execute. | |
| 246 root_frame_coordination_unit->AddChild(frame_coordination_unit->id()); | |
| 247 // The child is CoordinationUnitType::kNavigation so the callback | |
| 248 // should not be executed. The navigation Coordination Unit is not | |
| 249 // actually a child of a frame in practice, but is being used here to | |
| 250 // check filtering on OnChildAdded. | |
| 251 root_frame_coordination_unit->AddChild(navigation_coordination_unit->id()); | |
| 252 | |
| 253 // Test AddParent filtering. The AddParent filter for the frame | |
| 254 // CoordinationUnit has been set to only execute when the parent | |
| 255 // is a CoordinationUnitType::kProcess so the callback will execute. | |
| 256 // Note that AddParent is called within Add child. | |
|
Zhen Wang
2017/06/19 22:54:24
s/within Add child/as a consequence of AddChild.
matthalp
2017/06/20 19:02:52
Filtering has been removed -- this is no longer ap
| |
| 257 process_coordination_unit->AddChild(root_frame_coordination_unit->id()); | |
| 258 // The parent is CoordinationUnitType::kFrame so the callback | |
| 259 // should not be executed. | |
| 260 tab_coordination_unit->AddChild(root_frame_coordination_unit->id()); | |
| 261 | |
| 262 // Test RemoveChild filtering. The RemoveChild filter for the frame | |
| 263 // CoordinationUnit has been set to only execute when the child | |
| 264 // is a CoordinationUnitType::kFrame so the callback will execute. | |
| 265 root_frame_coordination_unit->RemoveChild(frame_coordination_unit->id()); | |
| 266 // The child is CoordinationUnitType::kNavigation so the callback | |
| 267 // should not be executed. The navigation Coordination Unit is not | |
| 268 // actually a child of a frame in practice, but is being used here to | |
| 269 // check filtering on OnRemoveChild. | |
| 270 root_frame_coordination_unit->RemoveChild(navigation_coordination_unit->id()); | |
| 271 | |
| 272 // Test RemoveParent filtering. The RemoveParent filter for the frame | |
| 273 // CoordinationUnit has been set to only execute when the parent | |
| 274 // is a CoordinationUnitType::kProcess so the callback will execute. | |
| 275 // Note that RemoveParent is called within Remove child. | |
| 276 process_coordination_unit->RemoveChild(root_frame_coordination_unit->id()); | |
| 277 // The parent is CoordinationUnitType::kFrame so the callback | |
| 278 // should not be executed. | |
| 279 tab_coordination_unit->RemoveChild(root_frame_coordination_unit->id()); | |
| 280 | |
| 281 // TODO(matthalp) Implement this another SetProperty once an additional | |
| 282 // mojom::PropertyType has been implemented so that the SetProperty | |
| 283 // filtering API can be tested. | |
| 284 root_frame_coordination_unit->SetProperty(mojom::PropertyType::kTest, | |
| 285 base::Value(42)); | |
| 286 | |
| 287 frame_coordination_unit->WillBeDestroyed(); | |
| 288 navigation_coordination_unit->WillBeDestroyed(); | |
| 289 process_coordination_unit->WillBeDestroyed(); | |
| 290 root_frame_coordination_unit->WillBeDestroyed(); | |
| 291 tab_coordination_unit->WillBeDestroyed(); | |
| 292 | |
| 293 EXPECT_EQ(1u, observer->on_child_added_listener_invocations()); | |
| 294 EXPECT_EQ(1u, observer->on_child_removed_listener_invocations()); | |
| 295 EXPECT_EQ(2u, observer->on_coordination_unit_created_invocations()); | |
|
Zhen Wang
2017/06/19 22:54:24
nit: it probably helps readability to do this chec
matthalp
2017/06/20 19:02:52
Filtering has been removed -- this is no longer ap
| |
| 296 EXPECT_EQ(1u, observer->on_parent_added_listener_invocations()); | |
| 297 EXPECT_EQ(1u, observer->on_parent_removed_listener_invocations()); | |
| 298 EXPECT_EQ(0u, observer->on_property_changed_listener_invocations()); | |
| 299 EXPECT_EQ(2u, observer->on_will_be_destroyed_listener_invocations()); | |
| 300 } | |
| 301 | |
| 302 } // namespace resource_coordinator | |
| OLD | NEW |