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 "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), id_(type, id) { |
| 41 CHECK(provider); |
| 42 |
| 43 mojom::CoordinationUnitPtr coordination_unit; |
| 44 provider->CreateCoordinationUnit(mojo::MakeRequest(&coordination_unit_), |
| 45 id_); |
| 46 |
| 47 base::RunLoop policy_callback; |
| 48 SetPolicyClosure(policy_callback.QuitClosure()); |
| 49 coordination_unit_->SetPolicyCallback(GetPolicyCallback()); |
| 50 // Forces us to wait for the creation of the CUID to finish. |
| 51 policy_callback.Run(); |
| 52 } |
| 53 |
| 54 void SetPolicyClosure(const base::Closure& policy_closure) { |
| 55 policy_update_closure_ = policy_closure; |
| 56 } |
| 57 |
| 58 mojom::PolicyCallbackPtr GetPolicyCallback() { |
| 59 return binding_.CreateInterfacePtrAndBind(); |
| 60 } |
| 61 |
| 62 // The CU will always send policy updates on events (including parent events) |
| 63 void ForcePolicyUpdates() { |
| 64 base::RunLoop callback; |
| 65 SetPolicyClosure(callback.QuitClosure()); |
| 66 mojom::EventPtr event = mojom::Event::New(); |
| 67 event->type = TEST_EVENT; |
| 68 coordination_unit_->SendEvent(std::move(event)); |
| 69 callback.Run(); |
| 70 } |
| 71 |
| 72 const mojom::CoordinationUnitPtr& interface() const { |
| 73 return coordination_unit_; |
| 74 } |
| 75 |
| 76 const CoordinationUnitID& id() const { return id_; } |
| 77 |
| 78 // mojom::PolicyCallback: |
| 79 void SetPolicy(resource_coordinator::mojom::PolicyPtr policy) override { |
| 80 policy_update_closure_.Run(); |
| 81 } |
| 82 |
| 83 private: |
| 84 base::Closure policy_update_closure_; |
| 85 |
| 86 mojo::Binding<mojom::PolicyCallback> binding_; |
| 87 mojom::CoordinationUnitPtr coordination_unit_; |
| 88 CoordinationUnitID id_; |
| 89 }; |
| 90 |
| 91 } // namespace |
| 92 |
| 93 TEST_F(CoordinationUnitImplTest, BasicPolicyCallback) { |
| 94 TestCoordinationUnit test_coordination_unit(provider(), WEBCONTENTS, |
| 95 "test_id"); |
| 96 test_coordination_unit.ForcePolicyUpdates(); |
| 97 } |
| 98 |
| 99 TEST_F(CoordinationUnitImplTest, AddChild) { |
| 100 TestCoordinationUnit parent_unit(provider(), WEBCONTENTS, "parent_unit"); |
| 101 |
| 102 TestCoordinationUnit child_unit(provider(), WEBCONTENTS, "child_unit"); |
| 103 |
| 104 child_unit.ForcePolicyUpdates(); |
| 105 parent_unit.ForcePolicyUpdates(); |
| 106 |
| 107 { |
| 108 base::RunLoop callback; |
| 109 child_unit.SetPolicyClosure(callback.QuitClosure()); |
| 110 parent_unit.interface()->AddChild(child_unit.id()); |
| 111 callback.Run(); |
| 112 } |
| 113 |
| 114 { |
| 115 base::RunLoop parent_callback; |
| 116 base::RunLoop child_callback; |
| 117 parent_unit.SetPolicyClosure(parent_callback.QuitClosure()); |
| 118 child_unit.SetPolicyClosure(child_callback.QuitClosure()); |
| 119 |
| 120 // This event should force the policy to recalculated for all children. |
| 121 mojom::EventPtr event = mojom::Event::New(); |
| 122 event->type = TEST_EVENT; |
| 123 parent_unit.interface()->SendEvent(std::move(event)); |
| 124 |
| 125 parent_callback.Run(); |
| 126 child_callback.Run(); |
| 127 } |
| 128 } |
| 129 |
| 130 TEST_F(CoordinationUnitImplTest, CyclicGraphUnits) { |
| 131 TestCoordinationUnit parent_unit(provider(), WEBCONTENTS, "parent_unit"); |
| 132 |
| 133 TestCoordinationUnit child_unit(provider(), WEBCONTENTS, "child_unit"); |
| 134 |
| 135 child_unit.ForcePolicyUpdates(); |
| 136 parent_unit.ForcePolicyUpdates(); |
| 137 |
| 138 { |
| 139 base::RunLoop callback; |
| 140 child_unit.SetPolicyClosure(callback.QuitClosure()); |
| 141 parent_unit.interface()->AddChild(child_unit.id()); |
| 142 callback.Run(); |
| 143 } |
| 144 |
| 145 // This should fail, due to the existing child-parent relationship. |
| 146 // Otherwise we end up with infinite recursion and crash when recalculating |
| 147 // policies below. |
| 148 child_unit.interface()->AddChild(parent_unit.id()); |
| 149 |
| 150 { |
| 151 base::RunLoop parent_callback; |
| 152 base::RunLoop child_callback; |
| 153 parent_unit.SetPolicyClosure(parent_callback.QuitClosure()); |
| 154 child_unit.SetPolicyClosure(child_callback.QuitClosure()); |
| 155 |
| 156 // This event should force the policy to recalculated for all children. |
| 157 mojom::EventPtr event = mojom::Event::New(); |
| 158 event->type = TEST_EVENT; |
| 159 parent_unit.interface()->SendEvent(std::move(event)); |
| 160 |
| 161 parent_callback.Run(); |
| 162 child_callback.Run(); |
| 163 } |
| 164 } |
| 165 |
| 166 } // namespace resource_coordinator |
OLD | NEW |