OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/policy/async_policy_provider.h" | 5 #include "chrome/browser/policy/async_policy_provider.h" |
6 | 6 |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "chrome/browser/policy/async_policy_loader.h" | 9 #include "chrome/browser/policy/async_policy_loader.h" |
10 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | 10 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 .WillRepeatedly(Return(base::Time())); | 100 .WillRepeatedly(Return(base::Time())); |
101 EXPECT_CALL(*loader_, InitOnFile()).Times(1); | 101 EXPECT_CALL(*loader_, InitOnFile()).Times(1); |
102 EXPECT_CALL(*loader_, MockLoad()).WillOnce(Return(&initial_bundle_)); | 102 EXPECT_CALL(*loader_, MockLoad()).WillOnce(Return(&initial_bundle_)); |
103 | 103 |
104 provider_.reset( | 104 provider_.reset( |
105 new AsyncPolicyProvider(scoped_ptr<AsyncPolicyLoader>(loader_))); | 105 new AsyncPolicyProvider(scoped_ptr<AsyncPolicyLoader>(loader_))); |
106 provider_->Init(); | 106 provider_->Init(); |
107 // Verify that the initial load is done synchronously: | 107 // Verify that the initial load is done synchronously: |
108 EXPECT_TRUE(provider_->policies().Equals(initial_bundle_)); | 108 EXPECT_TRUE(provider_->policies().Equals(initial_bundle_)); |
109 | 109 |
110 loop_.RunAllPending(); | 110 loop_.RunUntilIdle(); |
111 Mock::VerifyAndClearExpectations(loader_); | 111 Mock::VerifyAndClearExpectations(loader_); |
112 | 112 |
113 EXPECT_CALL(*loader_, LastModificationTime()) | 113 EXPECT_CALL(*loader_, LastModificationTime()) |
114 .WillRepeatedly(Return(base::Time())); | 114 .WillRepeatedly(Return(base::Time())); |
115 } | 115 } |
116 | 116 |
117 void AsyncPolicyProviderTest::TearDown() { | 117 void AsyncPolicyProviderTest::TearDown() { |
118 if (provider_) { | 118 if (provider_) { |
119 provider_->Shutdown(); | 119 provider_->Shutdown(); |
120 provider_.reset(); | 120 provider_.reset(); |
121 } | 121 } |
122 loop_.RunAllPending(); | 122 loop_.RunUntilIdle(); |
123 } | 123 } |
124 | 124 |
125 TEST_F(AsyncPolicyProviderTest, RefreshPolicies) { | 125 TEST_F(AsyncPolicyProviderTest, RefreshPolicies) { |
126 PolicyBundle refreshed_bundle; | 126 PolicyBundle refreshed_bundle; |
127 SetPolicy(&refreshed_bundle, "policy", "refreshed"); | 127 SetPolicy(&refreshed_bundle, "policy", "refreshed"); |
128 EXPECT_CALL(*loader_, MockLoad()).WillOnce(Return(&refreshed_bundle)); | 128 EXPECT_CALL(*loader_, MockLoad()).WillOnce(Return(&refreshed_bundle)); |
129 | 129 |
130 MockConfigurationPolicyObserver observer; | 130 MockConfigurationPolicyObserver observer; |
131 provider_->AddObserver(&observer); | 131 provider_->AddObserver(&observer); |
132 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1); | 132 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1); |
133 provider_->RefreshPolicies(); | 133 provider_->RefreshPolicies(); |
134 loop_.RunAllPending(); | 134 loop_.RunUntilIdle(); |
135 // The refreshed policies are now provided. | 135 // The refreshed policies are now provided. |
136 EXPECT_TRUE(provider_->policies().Equals(refreshed_bundle)); | 136 EXPECT_TRUE(provider_->policies().Equals(refreshed_bundle)); |
137 provider_->RemoveObserver(&observer); | 137 provider_->RemoveObserver(&observer); |
138 } | 138 } |
139 | 139 |
140 TEST_F(AsyncPolicyProviderTest, RefreshPoliciesTwice) { | 140 TEST_F(AsyncPolicyProviderTest, RefreshPoliciesTwice) { |
141 PolicyBundle refreshed_bundle; | 141 PolicyBundle refreshed_bundle; |
142 SetPolicy(&refreshed_bundle, "policy", "refreshed"); | 142 SetPolicy(&refreshed_bundle, "policy", "refreshed"); |
143 EXPECT_CALL(*loader_, MockLoad()).WillRepeatedly(Return(&refreshed_bundle)); | 143 EXPECT_CALL(*loader_, MockLoad()).WillRepeatedly(Return(&refreshed_bundle)); |
144 | 144 |
145 MockConfigurationPolicyObserver observer; | 145 MockConfigurationPolicyObserver observer; |
146 provider_->AddObserver(&observer); | 146 provider_->AddObserver(&observer); |
147 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0); | 147 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0); |
148 provider_->RefreshPolicies(); | 148 provider_->RefreshPolicies(); |
149 // Doesn't refresh before going through the FILE thread. | 149 // Doesn't refresh before going through the FILE thread. |
150 Mock::VerifyAndClearExpectations(&observer); | 150 Mock::VerifyAndClearExpectations(&observer); |
151 | 151 |
152 // Doesn't refresh if another RefreshPolicies request is made. | 152 // Doesn't refresh if another RefreshPolicies request is made. |
153 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0); | 153 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0); |
154 provider_->RefreshPolicies(); | 154 provider_->RefreshPolicies(); |
155 Mock::VerifyAndClearExpectations(&observer); | 155 Mock::VerifyAndClearExpectations(&observer); |
156 | 156 |
157 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1); | 157 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1); |
158 loop_.RunAllPending(); | 158 loop_.RunUntilIdle(); |
159 // The refreshed policies are now provided. | 159 // The refreshed policies are now provided. |
160 EXPECT_TRUE(provider_->policies().Equals(refreshed_bundle)); | 160 EXPECT_TRUE(provider_->policies().Equals(refreshed_bundle)); |
161 Mock::VerifyAndClearExpectations(&observer); | 161 Mock::VerifyAndClearExpectations(&observer); |
162 provider_->RemoveObserver(&observer); | 162 provider_->RemoveObserver(&observer); |
163 } | 163 } |
164 | 164 |
165 TEST_F(AsyncPolicyProviderTest, RefreshPoliciesDuringReload) { | 165 TEST_F(AsyncPolicyProviderTest, RefreshPoliciesDuringReload) { |
166 PolicyBundle reloaded_bundle; | 166 PolicyBundle reloaded_bundle; |
167 SetPolicy(&reloaded_bundle, "policy", "reloaded"); | 167 SetPolicy(&reloaded_bundle, "policy", "reloaded"); |
168 PolicyBundle refreshed_bundle; | 168 PolicyBundle refreshed_bundle; |
(...skipping 15 matching lines...) Expand all Loading... |
184 // notifications. | 184 // notifications. |
185 loader_->Reload(true); | 185 loader_->Reload(true); |
186 Mock::VerifyAndClearExpectations(&observer); | 186 Mock::VerifyAndClearExpectations(&observer); |
187 | 187 |
188 // Doesn't refresh before going through the FILE thread. | 188 // Doesn't refresh before going through the FILE thread. |
189 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0); | 189 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0); |
190 provider_->RefreshPolicies(); | 190 provider_->RefreshPolicies(); |
191 Mock::VerifyAndClearExpectations(&observer); | 191 Mock::VerifyAndClearExpectations(&observer); |
192 | 192 |
193 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1); | 193 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1); |
194 loop_.RunAllPending(); | 194 loop_.RunUntilIdle(); |
195 // The refreshed policies are now provided, and the |reloaded_bundle| was | 195 // The refreshed policies are now provided, and the |reloaded_bundle| was |
196 // dropped. | 196 // dropped. |
197 EXPECT_TRUE(provider_->policies().Equals(refreshed_bundle)); | 197 EXPECT_TRUE(provider_->policies().Equals(refreshed_bundle)); |
198 Mock::VerifyAndClearExpectations(&observer); | 198 Mock::VerifyAndClearExpectations(&observer); |
199 provider_->RemoveObserver(&observer); | 199 provider_->RemoveObserver(&observer); |
200 } | 200 } |
201 | 201 |
202 TEST_F(AsyncPolicyProviderTest, Shutdown) { | 202 TEST_F(AsyncPolicyProviderTest, Shutdown) { |
203 EXPECT_CALL(*loader_, MockLoad()).WillRepeatedly(Return(&initial_bundle_)); | 203 EXPECT_CALL(*loader_, MockLoad()).WillRepeatedly(Return(&initial_bundle_)); |
204 | 204 |
205 MockConfigurationPolicyObserver observer; | 205 MockConfigurationPolicyObserver observer; |
206 provider_->AddObserver(&observer); | 206 provider_->AddObserver(&observer); |
207 | 207 |
208 // Though there is a pending Reload, the provider and the loader can be | 208 // Though there is a pending Reload, the provider and the loader can be |
209 // deleted at any time. | 209 // deleted at any time. |
210 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0); | 210 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0); |
211 loader_->Reload(true); | 211 loader_->Reload(true); |
212 Mock::VerifyAndClearExpectations(&observer); | 212 Mock::VerifyAndClearExpectations(&observer); |
213 | 213 |
214 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0); | 214 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0); |
215 provider_->Shutdown(); | 215 provider_->Shutdown(); |
216 loop_.RunAllPending(); | 216 loop_.RunUntilIdle(); |
217 Mock::VerifyAndClearExpectations(&observer); | 217 Mock::VerifyAndClearExpectations(&observer); |
218 | 218 |
219 provider_->RemoveObserver(&observer); | 219 provider_->RemoveObserver(&observer); |
220 provider_.reset(); | 220 provider_.reset(); |
221 } | 221 } |
222 | 222 |
223 } // namespace policy | 223 } // namespace policy |
OLD | NEW |