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

Side by Side Diff: chrome/browser/chromeos/policy/cloud_external_data_policy_observer_unittest.cc

Issue 88423002: Add CloudExternalDataPolicyObserverChromeOS (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed suggestion. Created 7 years 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 | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/chromeos/policy/cloud_external_data_policy_observer.h"
6
7 #include <utility>
8 #include <vector>
9
10 #include "base/file_util.h"
11 #include "base/files/file_path.h"
12 #include "base/json/json_writer.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/message_loop/message_loop_proxy.h"
16 #include "base/path_service.h"
17 #include "base/run_loop.h"
18 #include "base/sha1.h"
19 #include "base/strings/string_number_conversions.h"
20 #include "base/values.h"
21 #include "chrome/browser/chrome_notification_types.h"
22 #include "chrome/browser/chromeos/login/fake_user_manager.h"
23 #include "chrome/browser/chromeos/policy/device_local_account.h"
24 #include "chrome/browser/chromeos/policy/device_local_account_external_data_mana ger.h"
25 #include "chrome/browser/chromeos/policy/device_local_account_policy_provider.h"
26 #include "chrome/browser/chromeos/policy/device_local_account_policy_service.h"
27 #include "chrome/browser/chromeos/settings/device_settings_service.h"
28 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h"
29 #include "chrome/browser/policy/cloud/cloud_policy_core.h"
30 #include "chrome/browser/policy/cloud/cloud_policy_store.h"
31 #include "chrome/browser/policy/cloud/mock_cloud_external_data_manager.h"
32 #include "chrome/browser/policy/cloud/policy_builder.h"
33 #include "chrome/browser/policy/mock_configuration_policy_provider.h"
34 #include "chrome/browser/policy/policy_service.h"
35 #include "chrome/browser/policy/policy_service_impl.h"
36 #include "chrome/browser/policy/proto/chromeos/chrome_device_policy.pb.h"
37 #include "chrome/browser/profiles/profile.h"
38 #include "chrome/common/chrome_paths.h"
39 #include "chrome/test/base/testing_profile.h"
40 #include "components/policy/core/common/external_data_fetcher.h"
41 #include "components/policy/core/common/policy_map.h"
42 #include "components/policy/core/common/policy_types.h"
43 #include "content/public/browser/notification_details.h"
44 #include "content/public/browser/notification_service.h"
45 #include "content/public/browser/notification_source.h"
46 #include "net/url_request/test_url_fetcher_factory.h"
47 #include "net/url_request/url_fetcher_delegate.h"
48 #include "net/url_request/url_request_context_getter.h"
49 #include "net/url_request/url_request_status.h"
50 #include "policy/policy_constants.h"
51 #include "policy/proto/cloud_policy.pb.h"
52 #include "testing/gmock/include/gmock/gmock.h"
53 #include "testing/gtest/include/gtest/gtest.h"
54 #include "url/gurl.h"
55
56 namespace em = enterprise_management;
57
58 using ::testing::Mock;
59 using ::testing::Return;
60 using ::testing::SaveArg;
61 using ::testing::_;
62
63 namespace policy {
64
65 namespace {
66
67 const char kDeviceLocalAccount[] = "device_local_account@localhost";
68
69 const char kRegularUserID[] = "user@example.com";
70
71 const char kAvatar1URL[] = "http://localhost/avatar1.jpg";
72 const char kAvatar2URL[] = "http://localhost/avatar2.jpg";
73
74 void ConstructAvatarPolicy(const std::string& file_name,
75 const std::string& url,
76 std::string* policy_data,
77 std::string* policy) {
78 base::FilePath test_data_dir;
79 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir));
80 ASSERT_TRUE(base::ReadFileToString(
81 test_data_dir.Append("policy").Append(file_name),
82 policy_data));
83 const std::string sha1 = base::SHA1HashString(*policy_data);
84
85 base::DictionaryValue dict;
86 dict.SetString("url", url);
87 dict.SetString("hash", base::HexEncode(sha1.data(), sha1.size()));
88 base::JSONWriter::Write(&dict, policy);
89 }
90
91 } // namespace
92
93 class CloudExternalDataPolicyObserverTest
94 : public chromeos::DeviceSettingsTestBase,
95 public CloudExternalDataPolicyObserver::Delegate {
96 public:
97 typedef std::pair<std::string, std::string> FetchedCall;
98
99 CloudExternalDataPolicyObserverTest();
100 virtual ~CloudExternalDataPolicyObserverTest();
101
102 // chromeos::DeviceSettingsTestBase:
103 virtual void SetUp() OVERRIDE;
104 virtual void TearDown() OVERRIDE;
105
106 // CloudExternalDataPolicyObserver::Delegate:
107 virtual void OnExternalDataSet(const std::string& policy,
108 const std::string& user_id) OVERRIDE;
109 virtual void OnExternalDataCleared(const std::string& policy,
110 const std::string& user_id) OVERRIDE;
111 virtual void OnExternalDataFetched(const std::string& policy,
112 const std::string& user_id,
113 scoped_ptr<std::string> data) OVERRIDE;
114
115 void CreateObserver();
116
117 void ClearObservations();
118
119 void SetDeviceLocalAccountAvatarPolicy(const std::string& account_id,
120 const std::string& value);
121
122 void AddDeviceLocalAccount(const std::string& account_id);
123 void RemoveDeviceLocalAccount(const std::string& account_id);
124
125 DeviceLocalAccountPolicyBroker* GetBrokerForDeviceLocalAccountUser();
126
127 void RefreshDeviceLocalAccountPolicy(DeviceLocalAccountPolicyBroker* broker);
128
129 void LogInAsDeviceLocalAccount(const std::string& user_id);
130
131 void SetRegularUserAvatarPolicy(const std::string& value);
132
133 void LogInAsRegularUser();
134
135 const std::string device_local_account_user_id_;
136
137 std::string avatar_policy_1_data_;
138 std::string avatar_policy_2_data_;
139 std::string avatar_policy_1_;
140 std::string avatar_policy_2_;
141
142 chromeos::CrosSettings cros_settings_;
143 chromeos::FakeUserManager user_manager_;
144 scoped_ptr<DeviceLocalAccountPolicyService>
145 device_local_account_policy_service_;
146 net::TestURLFetcherFactory url_fetcher_factory_;
147
148 scoped_ptr<DeviceLocalAccountPolicyProvider>
149 device_local_account_policy_provider_;
150
151 MockCloudExternalDataManager external_data_manager_;
152 MockConfigurationPolicyProvider user_policy_provider_;
153
154 scoped_ptr<TestingProfile> profile_;
155
156 scoped_ptr<CloudExternalDataPolicyObserver> observer_;
157
158 std::vector<std::string> set_calls_;
159 std::vector<std::string> cleared_calls_;
160 std::vector<FetchedCall> fetched_calls_;
161
162 ExternalDataFetcher::FetchCallback fetch_callback_;
163
164 private:
165 DISALLOW_COPY_AND_ASSIGN(CloudExternalDataPolicyObserverTest);
166 };
167
168 CloudExternalDataPolicyObserverTest::CloudExternalDataPolicyObserverTest()
169 : device_local_account_user_id_(GenerateDeviceLocalAccountUserId(
170 kDeviceLocalAccount,
171 DeviceLocalAccount::TYPE_PUBLIC_SESSION)),
172 cros_settings_(&device_settings_service_) {
173 }
174
175 CloudExternalDataPolicyObserverTest::~CloudExternalDataPolicyObserverTest() {
176 }
177
178 void CloudExternalDataPolicyObserverTest::SetUp() {
179 chromeos::DeviceSettingsTestBase::SetUp();
180 device_local_account_policy_service_.reset(
181 new DeviceLocalAccountPolicyService(&device_settings_test_helper_,
182 &device_settings_service_,
183 &cros_settings_,
184 loop_.message_loop_proxy(),
185 loop_.message_loop_proxy(),
186 loop_.message_loop_proxy(),
187 loop_.message_loop_proxy(),
188 NULL));
189 url_fetcher_factory_.set_remove_fetcher_on_delete(true);
190
191 EXPECT_CALL(user_policy_provider_, IsInitializationComplete(_))
192 .WillRepeatedly(Return(true));
193 user_policy_provider_.Init();
194
195 ConstructAvatarPolicy("avatar1.jpg",
196 kAvatar1URL,
197 &avatar_policy_1_data_,
198 &avatar_policy_1_);
199 ConstructAvatarPolicy("avatar2.jpg",
200 kAvatar2URL,
201 &avatar_policy_2_data_,
202 &avatar_policy_2_);
203 }
204
205 void CloudExternalDataPolicyObserverTest::TearDown() {
206 observer_.reset();
207 user_policy_provider_.Shutdown();
208 profile_.reset();
209 if (device_local_account_policy_provider_) {
210 device_local_account_policy_provider_->Shutdown();
211 device_local_account_policy_provider_.reset();
212 }
213 device_local_account_policy_service_->Shutdown();
214 device_local_account_policy_service_.reset();
215 chromeos::DeviceSettingsTestBase::TearDown();
216 }
217
218
219 void CloudExternalDataPolicyObserverTest::OnExternalDataSet(
220 const std::string& policy,
221 const std::string& user_id) {
222 EXPECT_EQ(key::kUserAvatarImage, policy);
223 set_calls_.push_back(user_id);
224 }
225
226 void CloudExternalDataPolicyObserverTest::OnExternalDataCleared(
227 const std::string& policy,
228 const std::string& user_id) {
229 EXPECT_EQ(key::kUserAvatarImage, policy);
230 cleared_calls_.push_back(user_id);
231 }
232
233 void CloudExternalDataPolicyObserverTest::OnExternalDataFetched(
234 const std::string& policy,
235 const std::string& user_id,
236 scoped_ptr<std::string> data) {
237 EXPECT_EQ(key::kUserAvatarImage, policy);
238 fetched_calls_.push_back(make_pair(user_id, std::string()));
239 fetched_calls_.back().second.swap(*data);
240 }
241
242 void CloudExternalDataPolicyObserverTest::CreateObserver() {
243 observer_.reset(new CloudExternalDataPolicyObserver(
244 &cros_settings_,
245 &user_manager_,
246 device_local_account_policy_service_.get(),
247 key::kUserAvatarImage,
248 this));
249 observer_->Init();
250 }
251
252 void CloudExternalDataPolicyObserverTest::ClearObservations() {
253 set_calls_.clear();
254 cleared_calls_.clear();
255 fetched_calls_.clear();
256 }
257
258 void CloudExternalDataPolicyObserverTest::SetDeviceLocalAccountAvatarPolicy(
259 const std::string& account_id,
260 const std::string& value) {
261 UserPolicyBuilder builder;
262 builder.policy_data().set_policy_type(
263 dm_protocol::kChromePublicAccountPolicyType);
264 builder.policy_data().set_settings_entity_id(account_id);
265 builder.policy_data().set_username(account_id);
266 if (!value.empty())
267 builder.payload().mutable_useravatarimage()->set_value(value);
268 builder.Build();
269 device_settings_test_helper_.set_device_local_account_policy_blob(
270 account_id,
271 builder.GetBlob());
272 }
273
274 void CloudExternalDataPolicyObserverTest::AddDeviceLocalAccount(
275 const std::string& account_id) {
276 em::DeviceLocalAccountInfoProto* account =
277 device_policy_.payload().mutable_device_local_accounts()->add_account();
278 account->set_account_id(account_id);
279 account->set_type(
280 em::DeviceLocalAccountInfoProto::ACCOUNT_TYPE_PUBLIC_SESSION);
281 device_policy_.Build();
282 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
283 ReloadDeviceSettings();
284 }
285
286 void CloudExternalDataPolicyObserverTest::RemoveDeviceLocalAccount(
287 const std::string& account_id) {
288 em::DeviceLocalAccountsProto* accounts =
289 device_policy_.payload().mutable_device_local_accounts();
290 std::vector<std::string> account_ids;
291 for (int i = 0; i < accounts->account_size(); ++i) {
292 if (accounts->account(i).account_id() != account_id)
293 account_ids.push_back(accounts->account(i).account_id());
294 }
295 accounts->clear_account();
296 for (std::vector<std::string>::const_iterator it = account_ids.begin();
297 it != account_ids.end(); ++it) {
298 em::DeviceLocalAccountInfoProto* account = accounts->add_account();
299 account->set_account_id(*it);
300 account->set_type(
301 em::DeviceLocalAccountInfoProto::ACCOUNT_TYPE_PUBLIC_SESSION);
302 }
303 device_policy_.Build();
304 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
305 ReloadDeviceSettings();
306 }
307
308 DeviceLocalAccountPolicyBroker*
309 CloudExternalDataPolicyObserverTest::GetBrokerForDeviceLocalAccountUser() {
310 return device_local_account_policy_service_->GetBrokerForUser(
311 device_local_account_user_id_);
312 }
313
314 void CloudExternalDataPolicyObserverTest::RefreshDeviceLocalAccountPolicy(
315 DeviceLocalAccountPolicyBroker* broker) {
316 broker->core()->store()->Load();
317 device_settings_test_helper_.Flush();
318 }
319
320 void CloudExternalDataPolicyObserverTest::LogInAsDeviceLocalAccount(
321 const std::string& user_id) {
322 device_local_account_policy_provider_.reset(
323 new DeviceLocalAccountPolicyProvider(
324 user_id,
325 device_local_account_policy_service_.get()));
326
327 PolicyServiceImpl::Providers providers;
328 providers.push_back(device_local_account_policy_provider_.get());
329 TestingProfile::Builder builder;
330 builder.SetPolicyService(scoped_ptr<PolicyService>(new PolicyServiceImpl(
331 providers,
332 PolicyServiceImpl::PreprocessCallback())));
333
334 profile_ = builder.Build();
335 profile_->set_profile_name(user_id);
336
337 user_manager_.AddUser(user_id);
338 content::NotificationService::current()->Notify(
339 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
340 content::NotificationService::AllSources(),
341 content::Details<Profile>(profile_.get()));
342 }
343
344 void CloudExternalDataPolicyObserverTest::SetRegularUserAvatarPolicy(
345 const std::string& value) {
346 PolicyMap policy_map;
347 if (!value.empty()) {
348 policy_map.Set(
349 key::kUserAvatarImage,
350 POLICY_LEVEL_MANDATORY,
351 POLICY_SCOPE_USER,
352 new base::StringValue(value),
353 external_data_manager_.CreateExternalDataFetcher(
354 key::kUserAvatarImage).release());
355 }
356 user_policy_provider_.UpdateChromePolicy(policy_map);
357 }
358
359 void CloudExternalDataPolicyObserverTest::LogInAsRegularUser() {
360 PolicyServiceImpl::Providers providers;
361 providers.push_back(&user_policy_provider_);
362 TestingProfile::Builder builder;
363 builder.SetPolicyService(scoped_ptr<PolicyService>(new PolicyServiceImpl(
364 providers,
365 PolicyServiceImpl::PreprocessCallback())));
366
367 profile_ = builder.Build();
368 profile_->set_profile_name(kRegularUserID);
369
370 user_manager_.AddUser(kRegularUserID);
371 content::NotificationService::current()->Notify(
372 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
373 content::NotificationService::AllSources(),
374 content::Details<Profile>(profile_.get()));
375 }
376
377 // Verifies that when an external data reference is set for a device-local
378 // account, a corresponding notification is emitted and a fetch is started.
379 // Further verifies that when the fetch succeeds, a notification containing the
380 // external data is emitted.
381 TEST_F(CloudExternalDataPolicyObserverTest,
382 ExistingDeviceLocalAccountFetchSuccess) {
383 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
384 AddDeviceLocalAccount(kDeviceLocalAccount);
385
386 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
387 ASSERT_TRUE(broker);
388 broker->external_data_manager()->Connect(NULL);
389 base::RunLoop().RunUntilIdle();
390
391 CreateObserver();
392
393 EXPECT_TRUE(cleared_calls_.empty());
394 EXPECT_TRUE(fetched_calls_.empty());
395 ASSERT_EQ(1u, set_calls_.size());
396 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
397 ClearObservations();
398
399 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
400 ASSERT_TRUE(fetcher);
401 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
402
403 fetcher->SetResponseString(avatar_policy_1_data_);
404 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
405 net::OK));
406 fetcher->set_response_code(200);
407 fetcher->delegate()->OnURLFetchComplete(fetcher);
408 base::RunLoop().RunUntilIdle();
409
410 EXPECT_TRUE(set_calls_.empty());
411 EXPECT_TRUE(cleared_calls_.empty());
412 ASSERT_EQ(1u, fetched_calls_.size());
413 EXPECT_EQ(device_local_account_user_id_, fetched_calls_.front().first);
414 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
415 ClearObservations();
416
417 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(1));
418 }
419
420 // Verifies that when an external data reference is set for a device-local
421 // account, a corresponding notification is emitted and a fetch is started.
422 // Further verifies that when the fetch fails, no notification is emitted.
423 TEST_F(CloudExternalDataPolicyObserverTest,
424 ExistingDeviceLocalAccountFetchFailure) {
425 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
426 AddDeviceLocalAccount(kDeviceLocalAccount);
427
428 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
429 ASSERT_TRUE(broker);
430 broker->external_data_manager()->Connect(NULL);
431 base::RunLoop().RunUntilIdle();
432
433 CreateObserver();
434
435 EXPECT_TRUE(cleared_calls_.empty());
436 EXPECT_TRUE(fetched_calls_.empty());
437 ASSERT_EQ(1u, set_calls_.size());
438 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
439 ClearObservations();
440
441 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
442 ASSERT_TRUE(fetcher);
443 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
444
445 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
446 net::OK));
447 fetcher->set_response_code(400);
448 fetcher->delegate()->OnURLFetchComplete(fetcher);
449 base::RunLoop().RunUntilIdle();
450
451 EXPECT_TRUE(set_calls_.empty());
452 EXPECT_TRUE(cleared_calls_.empty());
453 EXPECT_TRUE(fetched_calls_.empty());
454 ClearObservations();
455
456 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(1));
457 }
458
459 // Verifies that when the external data reference for a device-local account is
460 // initially not set, no notifications are emitted. Further verifies that when
461 // the external data reference is then cleared (which is a no-op), again, no
462 // notifications are emitted.
463 TEST_F(CloudExternalDataPolicyObserverTest,
464 ExistingDeviceLocalAccountClearUnset) {
465 AddDeviceLocalAccount(kDeviceLocalAccount);
466
467 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
468 ASSERT_TRUE(broker);
469 broker->external_data_manager()->Connect(NULL);
470 base::RunLoop().RunUntilIdle();
471
472 CreateObserver();
473
474 EXPECT_TRUE(set_calls_.empty());
475 EXPECT_TRUE(cleared_calls_.empty());
476 EXPECT_TRUE(fetched_calls_.empty());
477 ClearObservations();
478
479 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
480
481 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, "");
482 RefreshDeviceLocalAccountPolicy(broker);
483
484 EXPECT_TRUE(set_calls_.empty());
485 EXPECT_TRUE(cleared_calls_.empty());
486 EXPECT_TRUE(fetched_calls_.empty());
487 ClearObservations();
488
489 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
490 }
491
492 // Verifies that when the external data reference for a device-local account is
493 // initially set, a corresponding notification is emitted and a fetch is
494 // started. Further verifies that when the external data reference is then
495 // cleared, a corresponding notification is emitted and the fetch is canceled.
496 TEST_F(CloudExternalDataPolicyObserverTest,
497 ExistingDeviceLocalAccountClearSet) {
498 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
499 AddDeviceLocalAccount(kDeviceLocalAccount);
500
501 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
502 ASSERT_TRUE(broker);
503 broker->external_data_manager()->Connect(NULL);
504 base::RunLoop().RunUntilIdle();
505
506 CreateObserver();
507
508 EXPECT_TRUE(cleared_calls_.empty());
509 EXPECT_TRUE(fetched_calls_.empty());
510 ASSERT_EQ(1u, set_calls_.size());
511 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
512 ClearObservations();
513
514 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
515 ASSERT_TRUE(fetcher);
516 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
517
518 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, "");
519 RefreshDeviceLocalAccountPolicy(broker);
520
521 EXPECT_TRUE(set_calls_.empty());
522 EXPECT_TRUE(fetched_calls_.empty());
523 ASSERT_EQ(1u, cleared_calls_.size());
524 EXPECT_EQ(device_local_account_user_id_, cleared_calls_.front());
525 ClearObservations();
526
527 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
528 }
529
530 // Verifies that when the external data reference for a device-local account is
531 // initially not set, no notifications are emitted. Further verifies that when
532 // the external data reference is then set, a corresponding notification is
533 // emitted and a fetch is started. Also verifies that when the fetch eventually
534 // succeeds, a notification containing the external data is emitted.
535 TEST_F(CloudExternalDataPolicyObserverTest,
536 ExistingDeviceLocalAccountSetUnset) {
537 AddDeviceLocalAccount(kDeviceLocalAccount);
538
539 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
540 ASSERT_TRUE(broker);
541 broker->external_data_manager()->Connect(NULL);
542 base::RunLoop().RunUntilIdle();
543
544 CreateObserver();
545
546 EXPECT_TRUE(set_calls_.empty());
547 EXPECT_TRUE(cleared_calls_.empty());
548 EXPECT_TRUE(fetched_calls_.empty());
549 ClearObservations();
550
551 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
552 RefreshDeviceLocalAccountPolicy(broker);
553
554 EXPECT_TRUE(cleared_calls_.empty());
555 EXPECT_TRUE(fetched_calls_.empty());
556 ASSERT_EQ(1u, set_calls_.size());
557 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
558 ClearObservations();
559
560 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
561 ASSERT_TRUE(fetcher);
562 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
563
564 fetcher->SetResponseString(avatar_policy_1_data_);
565 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
566 net::OK));
567 fetcher->set_response_code(200);
568 fetcher->delegate()->OnURLFetchComplete(fetcher);
569 base::RunLoop().RunUntilIdle();
570
571 EXPECT_TRUE(set_calls_.empty());
572 EXPECT_TRUE(cleared_calls_.empty());
573 ASSERT_EQ(1u, fetched_calls_.size());
574 EXPECT_EQ(device_local_account_user_id_, fetched_calls_.front().first);
575 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
576 ClearObservations();
577
578 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(1));
579 }
580
581 // Verifies that when the external data reference for a device-local account is
582 // initially set, a corresponding notification is emitted and a fetch is
583 // started. Further verifies that when the external data reference is then
584 // updated, a corresponding notification is emitted and the fetch is restarted.
585 // Also verifies that when the fetch eventually succeeds, a notification
586 // containing the external data is emitted.
587 TEST_F(CloudExternalDataPolicyObserverTest, ExistingDeviceLocalAccountSetSet) {
588 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
589 AddDeviceLocalAccount(kDeviceLocalAccount);
590
591 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
592 ASSERT_TRUE(broker);
593 broker->external_data_manager()->Connect(NULL);
594 base::RunLoop().RunUntilIdle();
595
596 CreateObserver();
597
598 EXPECT_TRUE(cleared_calls_.empty());
599 EXPECT_TRUE(fetched_calls_.empty());
600 ASSERT_EQ(1u, set_calls_.size());
601 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
602 ClearObservations();
603
604 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
605 ASSERT_TRUE(fetcher);
606 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
607
608 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_2_);
609 RefreshDeviceLocalAccountPolicy(broker);
610
611 EXPECT_TRUE(cleared_calls_.empty());
612 EXPECT_TRUE(fetched_calls_.empty());
613 ASSERT_EQ(1u, set_calls_.size());
614 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
615 ClearObservations();
616
617 fetcher = url_fetcher_factory_.GetFetcherByID(1);
618 ASSERT_TRUE(fetcher);
619 EXPECT_EQ(GURL(kAvatar2URL), fetcher->GetOriginalURL());
620
621 fetcher->SetResponseString(avatar_policy_2_data_);
622 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
623 net::OK));
624 fetcher->set_response_code(200);
625 fetcher->delegate()->OnURLFetchComplete(fetcher);
626 base::RunLoop().RunUntilIdle();
627
628 EXPECT_TRUE(set_calls_.empty());
629 EXPECT_TRUE(cleared_calls_.empty());
630 ASSERT_EQ(1u, fetched_calls_.size());
631 EXPECT_EQ(device_local_account_user_id_, fetched_calls_.front().first);
632 EXPECT_EQ(avatar_policy_2_data_, fetched_calls_.front().second);
633 ClearObservations();
634
635 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(2));
636 }
637
638 // Verifies that when the external data reference for a device-local account is
639 // initially not set, no notifications are emitted during login into the
640 // account. Further verifies that when the external data reference is then set,
641 // a corresponding notification is emitted only once and a fetch is started.
642 // Also verifies that when the fetch eventually succeeds, a notification
643 // containing the external data is emitted, again, only once.
644 TEST_F(CloudExternalDataPolicyObserverTest,
645 ExistingDeviceLocalAccountSetAfterLogin) {
646 AddDeviceLocalAccount(kDeviceLocalAccount);
647
648 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
649 ASSERT_TRUE(broker);
650 broker->external_data_manager()->Connect(NULL);
651 base::RunLoop().RunUntilIdle();
652
653 CreateObserver();
654
655 LogInAsDeviceLocalAccount(kDeviceLocalAccount);
656
657 EXPECT_TRUE(set_calls_.empty());
658 EXPECT_TRUE(cleared_calls_.empty());
659 EXPECT_TRUE(fetched_calls_.empty());
660 ClearObservations();
661
662 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
663 RefreshDeviceLocalAccountPolicy(broker);
664
665 EXPECT_TRUE(cleared_calls_.empty());
666 EXPECT_TRUE(fetched_calls_.empty());
667 ASSERT_EQ(1u, set_calls_.size());
668 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
669 ClearObservations();
670
671 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
672 ASSERT_TRUE(fetcher);
673 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
674
675 fetcher->SetResponseString(avatar_policy_1_data_);
676 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
677 net::OK));
678 fetcher->set_response_code(200);
679 fetcher->delegate()->OnURLFetchComplete(fetcher);
680 base::RunLoop().RunUntilIdle();
681
682 EXPECT_TRUE(set_calls_.empty());
683 EXPECT_TRUE(cleared_calls_.empty());
684 ASSERT_EQ(1u, fetched_calls_.size());
685 EXPECT_EQ(device_local_account_user_id_, fetched_calls_.front().first);
686 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
687 ClearObservations();
688
689 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(1));
690 }
691
692 // Verifies that when the external data reference for a device-local account is
693 // initially not set, no notifications are emitted. Further verifies that when
694 // the device-local account is then removed, again, no notifications are sent.
695 TEST_F(CloudExternalDataPolicyObserverTest,
696 ExistingDeviceLocalAccountRemoveAccountUnset) {
697 AddDeviceLocalAccount(kDeviceLocalAccount);
698
699 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
700 ASSERT_TRUE(broker);
701 broker->external_data_manager()->Connect(NULL);
702 base::RunLoop().RunUntilIdle();
703
704 CreateObserver();
705
706 EXPECT_TRUE(set_calls_.empty());
707 EXPECT_TRUE(cleared_calls_.empty());
708 EXPECT_TRUE(fetched_calls_.empty());
709 ClearObservations();
710
711 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
712
713 RemoveDeviceLocalAccount(kDeviceLocalAccount);
714
715 EXPECT_TRUE(set_calls_.empty());
716 EXPECT_TRUE(cleared_calls_.empty());
717 EXPECT_TRUE(fetched_calls_.empty());
718 ClearObservations();
719
720 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
721 }
722
723 // Verifies that when the external data reference for a device-local account is
724 // initially set, a corresponding notification is emitted and a fetch is
725 // started. Further verifies that when the device-local account is then removed,
726 // a notification indicating that the external data reference has been cleared
727 // is emitted and the fetch is canceled.
728 TEST_F(CloudExternalDataPolicyObserverTest,
729 ExistingDeviceLocalAccountRemoveAccountSet) {
730 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
731 AddDeviceLocalAccount(kDeviceLocalAccount);
732
733 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
734 ASSERT_TRUE(broker);
735 broker->external_data_manager()->Connect(NULL);
736 base::RunLoop().RunUntilIdle();
737
738 CreateObserver();
739
740 EXPECT_TRUE(cleared_calls_.empty());
741 EXPECT_TRUE(fetched_calls_.empty());
742 ASSERT_EQ(1u, set_calls_.size());
743 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
744 ClearObservations();
745
746 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
747 ASSERT_TRUE(fetcher);
748 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
749
750 RemoveDeviceLocalAccount(kDeviceLocalAccount);
751
752 EXPECT_TRUE(set_calls_.empty());
753 EXPECT_TRUE(fetched_calls_.empty());
754 ASSERT_EQ(1u, cleared_calls_.size());
755 EXPECT_EQ(device_local_account_user_id_, cleared_calls_.front());
756 ClearObservations();
757
758 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
759 }
760
761 // Verifies that when an external data reference is set for a regular user and
762 // the user logs in, a corresponding notification is emitted and a fetch is
763 // started. Further verifies that when the fetch succeeds, a notification
764 // containing the external data is emitted.
765 TEST_F(CloudExternalDataPolicyObserverTest, RegularUserFetchSuccess) {
766 SetRegularUserAvatarPolicy(avatar_policy_1_);
767
768 CreateObserver();
769
770 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
771 .Times(1)
772 .WillOnce(SaveArg<1>(&fetch_callback_));
773
774 LogInAsRegularUser();
775
776 EXPECT_TRUE(cleared_calls_.empty());
777 EXPECT_TRUE(fetched_calls_.empty());
778 ASSERT_EQ(1u, set_calls_.size());
779 EXPECT_EQ(kRegularUserID, set_calls_.front());
780 ClearObservations();
781
782 Mock::VerifyAndClear(&external_data_manager_);
783 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
784
785 fetch_callback_.Run(make_scoped_ptr(new std::string(avatar_policy_1_data_)));
786
787 EXPECT_TRUE(set_calls_.empty());
788 EXPECT_TRUE(cleared_calls_.empty());
789 ASSERT_EQ(1u, fetched_calls_.size());
790 EXPECT_EQ(kRegularUserID, fetched_calls_.front().first);
791 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
792 ClearObservations();
793 }
794
795 // Verifies that when the external data reference for a regular user is not set
796 // while the user is logging in, no notifications are emitted. Further verifies
797 // that when the external data reference is then cleared (which is a no-op),
798 // again, no notifications are emitted.
799 TEST_F(CloudExternalDataPolicyObserverTest, RegularUserClearUnset) {
800 CreateObserver();
801
802 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
803
804 LogInAsRegularUser();
805
806 EXPECT_TRUE(set_calls_.empty());
807 EXPECT_TRUE(cleared_calls_.empty());
808 EXPECT_TRUE(fetched_calls_.empty());
809 ClearObservations();
810
811 Mock::VerifyAndClear(&external_data_manager_);
812 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
813
814 SetRegularUserAvatarPolicy("");
815
816 EXPECT_TRUE(set_calls_.empty());
817 EXPECT_TRUE(cleared_calls_.empty());
818 EXPECT_TRUE(fetched_calls_.empty());
819 ClearObservations();
820 }
821
822 // Verifies that when the external data reference for a regular user is set
823 // while the user is logging in, a corresponding notification is emitted and a
824 // fetch is started. Further verifies that when the external data reference is
825 // then cleared, a corresponding notification is emitted and no new fetch is
826 // started.
827 TEST_F(CloudExternalDataPolicyObserverTest, RegularUserClearSet) {
828 SetRegularUserAvatarPolicy(avatar_policy_1_);
829
830 CreateObserver();
831
832 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
833 .Times(1)
834 .WillOnce(SaveArg<1>(&fetch_callback_));
835
836 LogInAsRegularUser();
837
838 EXPECT_TRUE(cleared_calls_.empty());
839 EXPECT_TRUE(fetched_calls_.empty());
840 ASSERT_EQ(1u, set_calls_.size());
841 EXPECT_EQ(kRegularUserID, set_calls_.front());
842 ClearObservations();
843
844 Mock::VerifyAndClear(&external_data_manager_);
845 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
846
847 SetRegularUserAvatarPolicy("");
848
849 EXPECT_TRUE(set_calls_.empty());
850 EXPECT_TRUE(fetched_calls_.empty());
851 ASSERT_EQ(1u, cleared_calls_.size());
852 EXPECT_EQ(kRegularUserID, cleared_calls_.front());
853 ClearObservations();
854 }
855
856
857 // Verifies that when the external data reference for a regular user is not set
858 // while the user is logging in, no notifications are emitted. Further verifies
859 // that when the external data reference is then set, a corresponding
860 // notification is emitted and a fetch is started. Also verifies that when the
861 // fetch eventually succeeds, a notification containing the external data is
862 // emitted.
863 TEST_F(CloudExternalDataPolicyObserverTest, RegularUserSetUnset) {
864 CreateObserver();
865
866 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
867
868 LogInAsRegularUser();
869
870 EXPECT_TRUE(set_calls_.empty());
871 EXPECT_TRUE(cleared_calls_.empty());
872 EXPECT_TRUE(fetched_calls_.empty());
873 ClearObservations();
874
875 Mock::VerifyAndClear(&external_data_manager_);
876 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
877 .Times(1)
878 .WillOnce(SaveArg<1>(&fetch_callback_));
879
880 SetRegularUserAvatarPolicy(avatar_policy_1_);
881
882 EXPECT_TRUE(cleared_calls_.empty());
883 EXPECT_TRUE(fetched_calls_.empty());
884 ASSERT_EQ(1u, set_calls_.size());
885 EXPECT_EQ(kRegularUserID, set_calls_.front());
886 ClearObservations();
887
888 Mock::VerifyAndClear(&external_data_manager_);
889 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
890
891 fetch_callback_.Run(make_scoped_ptr(new std::string(avatar_policy_1_data_)));
892
893 EXPECT_TRUE(set_calls_.empty());
894 EXPECT_TRUE(cleared_calls_.empty());
895 ASSERT_EQ(1u, fetched_calls_.size());
896 EXPECT_EQ(kRegularUserID, fetched_calls_.front().first);
897 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
898 ClearObservations();
899 }
900
901 // Verifies that when the external data reference for a regular user is set
902 // while the user is logging in, a corresponding notification is emitted and a
903 // fetch is started. Further verifies that when the external data reference is
904 // then updated, a corresponding notification is emitted and the fetch is
905 // restarted. Also verifies that when the fetch eventually succeeds, a
906 // notification containing the external data is emitted.
907 TEST_F(CloudExternalDataPolicyObserverTest, RegularUserSetSet) {
908 SetRegularUserAvatarPolicy(avatar_policy_1_);
909
910 CreateObserver();
911
912 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
913 .Times(1)
914 .WillOnce(SaveArg<1>(&fetch_callback_));
915
916 LogInAsRegularUser();
917
918 EXPECT_TRUE(cleared_calls_.empty());
919 EXPECT_TRUE(fetched_calls_.empty());
920 ASSERT_EQ(1u, set_calls_.size());
921 EXPECT_EQ(kRegularUserID, set_calls_.front());
922 ClearObservations();
923
924 Mock::VerifyAndClear(&external_data_manager_);
925 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
926 .Times(1)
927 .WillOnce(SaveArg<1>(&fetch_callback_));
928
929 SetRegularUserAvatarPolicy(avatar_policy_2_);
930
931 EXPECT_TRUE(cleared_calls_.empty());
932 EXPECT_TRUE(fetched_calls_.empty());
933 ASSERT_EQ(1u, set_calls_.size());
934 EXPECT_EQ(kRegularUserID, set_calls_.front());
935 ClearObservations();
936
937 Mock::VerifyAndClear(&external_data_manager_);
938 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
939
940 fetch_callback_.Run(make_scoped_ptr(new std::string(avatar_policy_2_data_)));
941
942 EXPECT_TRUE(set_calls_.empty());
943 EXPECT_TRUE(cleared_calls_.empty());
944 ASSERT_EQ(1u, fetched_calls_.size());
945 EXPECT_EQ(kRegularUserID, fetched_calls_.front().first);
946 EXPECT_EQ(avatar_policy_2_data_, fetched_calls_.front().second);
947 ClearObservations();
948 }
949
950 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698