| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/policy/core/common/async_policy_provider.h" | 5 #include "components/policy/core/common/async_policy_provider.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/run_loop.h" |
| 12 #include "base/sequenced_task_runner.h" | 13 #include "base/sequenced_task_runner.h" |
| 13 #include "base/values.h" | 14 #include "base/values.h" |
| 14 #include "components/policy/core/common/async_policy_loader.h" | 15 #include "components/policy/core/common/async_policy_loader.h" |
| 15 #include "components/policy/core/common/external_data_fetcher.h" | 16 #include "components/policy/core/common/external_data_fetcher.h" |
| 16 #include "components/policy/core/common/mock_configuration_policy_provider.h" | 17 #include "components/policy/core/common/mock_configuration_policy_provider.h" |
| 17 #include "components/policy/core/common/policy_types.h" | 18 #include "components/policy/core/common/policy_types.h" |
| 18 #include "components/policy/core/common/schema_registry.h" | 19 #include "components/policy/core/common/schema_registry.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 22 |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 .WillRepeatedly(Return(base::Time())); | 105 .WillRepeatedly(Return(base::Time())); |
| 105 EXPECT_CALL(*loader_, InitOnBackgroundThread()).Times(1); | 106 EXPECT_CALL(*loader_, InitOnBackgroundThread()).Times(1); |
| 106 EXPECT_CALL(*loader_, MockLoad()).WillOnce(Return(&initial_bundle_)); | 107 EXPECT_CALL(*loader_, MockLoad()).WillOnce(Return(&initial_bundle_)); |
| 107 | 108 |
| 108 provider_.reset(new AsyncPolicyProvider( | 109 provider_.reset(new AsyncPolicyProvider( |
| 109 &schema_registry_, std::unique_ptr<AsyncPolicyLoader>(loader_))); | 110 &schema_registry_, std::unique_ptr<AsyncPolicyLoader>(loader_))); |
| 110 provider_->Init(&schema_registry_); | 111 provider_->Init(&schema_registry_); |
| 111 // Verify that the initial load is done synchronously: | 112 // Verify that the initial load is done synchronously: |
| 112 EXPECT_TRUE(provider_->policies().Equals(initial_bundle_)); | 113 EXPECT_TRUE(provider_->policies().Equals(initial_bundle_)); |
| 113 | 114 |
| 114 loop_.RunUntilIdle(); | 115 base::RunLoop().RunUntilIdle(); |
| 115 Mock::VerifyAndClearExpectations(loader_); | 116 Mock::VerifyAndClearExpectations(loader_); |
| 116 | 117 |
| 117 EXPECT_CALL(*loader_, LastModificationTime()) | 118 EXPECT_CALL(*loader_, LastModificationTime()) |
| 118 .WillRepeatedly(Return(base::Time())); | 119 .WillRepeatedly(Return(base::Time())); |
| 119 } | 120 } |
| 120 | 121 |
| 121 void AsyncPolicyProviderTest::TearDown() { | 122 void AsyncPolicyProviderTest::TearDown() { |
| 122 if (provider_) { | 123 if (provider_) { |
| 123 provider_->Shutdown(); | 124 provider_->Shutdown(); |
| 124 provider_.reset(); | 125 provider_.reset(); |
| 125 } | 126 } |
| 126 loop_.RunUntilIdle(); | 127 base::RunLoop().RunUntilIdle(); |
| 127 } | 128 } |
| 128 | 129 |
| 129 TEST_F(AsyncPolicyProviderTest, RefreshPolicies) { | 130 TEST_F(AsyncPolicyProviderTest, RefreshPolicies) { |
| 130 PolicyBundle refreshed_bundle; | 131 PolicyBundle refreshed_bundle; |
| 131 SetPolicy(&refreshed_bundle, "policy", "refreshed"); | 132 SetPolicy(&refreshed_bundle, "policy", "refreshed"); |
| 132 EXPECT_CALL(*loader_, MockLoad()).WillOnce(Return(&refreshed_bundle)); | 133 EXPECT_CALL(*loader_, MockLoad()).WillOnce(Return(&refreshed_bundle)); |
| 133 | 134 |
| 134 MockConfigurationPolicyObserver observer; | 135 MockConfigurationPolicyObserver observer; |
| 135 provider_->AddObserver(&observer); | 136 provider_->AddObserver(&observer); |
| 136 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1); | 137 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1); |
| 137 provider_->RefreshPolicies(); | 138 provider_->RefreshPolicies(); |
| 138 loop_.RunUntilIdle(); | 139 base::RunLoop().RunUntilIdle(); |
| 139 // The refreshed policies are now provided. | 140 // The refreshed policies are now provided. |
| 140 EXPECT_TRUE(provider_->policies().Equals(refreshed_bundle)); | 141 EXPECT_TRUE(provider_->policies().Equals(refreshed_bundle)); |
| 141 provider_->RemoveObserver(&observer); | 142 provider_->RemoveObserver(&observer); |
| 142 } | 143 } |
| 143 | 144 |
| 144 TEST_F(AsyncPolicyProviderTest, RefreshPoliciesTwice) { | 145 TEST_F(AsyncPolicyProviderTest, RefreshPoliciesTwice) { |
| 145 PolicyBundle refreshed_bundle; | 146 PolicyBundle refreshed_bundle; |
| 146 SetPolicy(&refreshed_bundle, "policy", "refreshed"); | 147 SetPolicy(&refreshed_bundle, "policy", "refreshed"); |
| 147 EXPECT_CALL(*loader_, MockLoad()).WillRepeatedly(Return(&refreshed_bundle)); | 148 EXPECT_CALL(*loader_, MockLoad()).WillRepeatedly(Return(&refreshed_bundle)); |
| 148 | 149 |
| 149 MockConfigurationPolicyObserver observer; | 150 MockConfigurationPolicyObserver observer; |
| 150 provider_->AddObserver(&observer); | 151 provider_->AddObserver(&observer); |
| 151 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0); | 152 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0); |
| 152 provider_->RefreshPolicies(); | 153 provider_->RefreshPolicies(); |
| 153 // Doesn't refresh before going through the background thread. | 154 // Doesn't refresh before going through the background thread. |
| 154 Mock::VerifyAndClearExpectations(&observer); | 155 Mock::VerifyAndClearExpectations(&observer); |
| 155 | 156 |
| 156 // Doesn't refresh if another RefreshPolicies request is made. | 157 // Doesn't refresh if another RefreshPolicies request is made. |
| 157 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0); | 158 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0); |
| 158 provider_->RefreshPolicies(); | 159 provider_->RefreshPolicies(); |
| 159 Mock::VerifyAndClearExpectations(&observer); | 160 Mock::VerifyAndClearExpectations(&observer); |
| 160 | 161 |
| 161 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1); | 162 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1); |
| 162 loop_.RunUntilIdle(); | 163 base::RunLoop().RunUntilIdle(); |
| 163 // The refreshed policies are now provided. | 164 // The refreshed policies are now provided. |
| 164 EXPECT_TRUE(provider_->policies().Equals(refreshed_bundle)); | 165 EXPECT_TRUE(provider_->policies().Equals(refreshed_bundle)); |
| 165 Mock::VerifyAndClearExpectations(&observer); | 166 Mock::VerifyAndClearExpectations(&observer); |
| 166 provider_->RemoveObserver(&observer); | 167 provider_->RemoveObserver(&observer); |
| 167 } | 168 } |
| 168 | 169 |
| 169 TEST_F(AsyncPolicyProviderTest, RefreshPoliciesDuringReload) { | 170 TEST_F(AsyncPolicyProviderTest, RefreshPoliciesDuringReload) { |
| 170 PolicyBundle reloaded_bundle; | 171 PolicyBundle reloaded_bundle; |
| 171 SetPolicy(&reloaded_bundle, "policy", "reloaded"); | 172 SetPolicy(&reloaded_bundle, "policy", "reloaded"); |
| 172 PolicyBundle refreshed_bundle; | 173 PolicyBundle refreshed_bundle; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 188 // notifications. | 189 // notifications. |
| 189 loader_->Reload(true); | 190 loader_->Reload(true); |
| 190 Mock::VerifyAndClearExpectations(&observer); | 191 Mock::VerifyAndClearExpectations(&observer); |
| 191 | 192 |
| 192 // Doesn't refresh before going through the background thread. | 193 // Doesn't refresh before going through the background thread. |
| 193 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0); | 194 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0); |
| 194 provider_->RefreshPolicies(); | 195 provider_->RefreshPolicies(); |
| 195 Mock::VerifyAndClearExpectations(&observer); | 196 Mock::VerifyAndClearExpectations(&observer); |
| 196 | 197 |
| 197 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1); | 198 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1); |
| 198 loop_.RunUntilIdle(); | 199 base::RunLoop().RunUntilIdle(); |
| 199 // The refreshed policies are now provided, and the |reloaded_bundle| was | 200 // The refreshed policies are now provided, and the |reloaded_bundle| was |
| 200 // dropped. | 201 // dropped. |
| 201 EXPECT_TRUE(provider_->policies().Equals(refreshed_bundle)); | 202 EXPECT_TRUE(provider_->policies().Equals(refreshed_bundle)); |
| 202 Mock::VerifyAndClearExpectations(&observer); | 203 Mock::VerifyAndClearExpectations(&observer); |
| 203 provider_->RemoveObserver(&observer); | 204 provider_->RemoveObserver(&observer); |
| 204 } | 205 } |
| 205 | 206 |
| 206 TEST_F(AsyncPolicyProviderTest, Shutdown) { | 207 TEST_F(AsyncPolicyProviderTest, Shutdown) { |
| 207 EXPECT_CALL(*loader_, MockLoad()).WillRepeatedly(Return(&initial_bundle_)); | 208 EXPECT_CALL(*loader_, MockLoad()).WillRepeatedly(Return(&initial_bundle_)); |
| 208 | 209 |
| 209 MockConfigurationPolicyObserver observer; | 210 MockConfigurationPolicyObserver observer; |
| 210 provider_->AddObserver(&observer); | 211 provider_->AddObserver(&observer); |
| 211 | 212 |
| 212 // Though there is a pending Reload, the provider and the loader can be | 213 // Though there is a pending Reload, the provider and the loader can be |
| 213 // deleted at any time. | 214 // deleted at any time. |
| 214 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0); | 215 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0); |
| 215 loader_->Reload(true); | 216 loader_->Reload(true); |
| 216 Mock::VerifyAndClearExpectations(&observer); | 217 Mock::VerifyAndClearExpectations(&observer); |
| 217 | 218 |
| 218 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0); | 219 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0); |
| 219 provider_->Shutdown(); | 220 provider_->Shutdown(); |
| 220 loop_.RunUntilIdle(); | 221 base::RunLoop().RunUntilIdle(); |
| 221 Mock::VerifyAndClearExpectations(&observer); | 222 Mock::VerifyAndClearExpectations(&observer); |
| 222 | 223 |
| 223 provider_->RemoveObserver(&observer); | 224 provider_->RemoveObserver(&observer); |
| 224 provider_.reset(); | 225 provider_.reset(); |
| 225 } | 226 } |
| 226 | 227 |
| 227 } // namespace policy | 228 } // namespace policy |
| OLD | NEW |