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.
5 #include "chrome/browser/chromeos/policy/cloud_external_data_policy_observer.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.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/values.h"
19 #include "chrome/browser/chrome_notification_types.h"
20 #include "chrome/browser/chromeos/policy/cloud_external_data_manager_base_test_util.h"
21 #include "chrome/browser/chromeos/policy/device_local_account.h"
22 #include "chrome/browser/chromeos/policy/device_local_account_external_data_manager.h"
23 #include "chrome/browser/chromeos/policy/device_local_account_policy_provider.h"
24 #include "chrome/browser/chromeos/policy/device_local_account_policy_service.h"
25 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h"
26 #include "chrome/browser/chromeos/profiles/profile_helper.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/profiles/profile.h"
30 #include "chrome/common/chrome_paths.h"
31 #include "chrome/test/base/testing_browser_process.h"
32 #include "chrome/test/base/testing_profile.h"
33 #include "chrome/test/base/testing_profile_manager.h"
34 #include "components/policy/core/common/cloud/cloud_policy_core.h"
35 #include "components/policy/core/common/cloud/cloud_policy_store.h"
36 #include "components/policy/core/common/cloud/mock_cloud_external_data_manager.h"
37 #include "components/policy/core/common/cloud/policy_builder.h"
38 #include "components/policy/core/common/external_data_fetcher.h"
39 #include "components/policy/core/common/mock_configuration_policy_provider.h"
40 #include "components/policy/core/common/policy_map.h"
41 #include "components/policy/core/common/policy_service.h"
42 #include "components/policy/core/common/policy_service_impl.h"
43 #include "components/policy/core/common/policy_types.h"
44 #include "content/public/browser/notification_details.h"
45 #include "content/public/browser/notification_service.h"
46 #include "content/public/browser/notification_source.h"
47 #include "net/url_request/test_url_fetcher_factory.h"
48 #include "net/url_request/url_fetcher_delegate.h"
49 #include "net/url_request/url_request_context_getter.h"
50 #include "net/url_request/url_request_status.h"
51 #include "policy/policy_constants.h"
52 #include "policy/proto/cloud_policy.pb.h"
53 #include "testing/gmock/include/gmock/gmock.h"
54 #include "testing/gtest/include/gtest/gtest.h"
57 namespace em = enterprise_management;
59 using ::testing::Mock;
60 using ::testing::Return;
61 using ::testing::SaveArg;
68 const char kDeviceLocalAccount[] = "device_local_account@localhost";
70 const char kRegularUserID[] = "user@example.com";
72 const char kAvatar1URL[] = "http://localhost/avatar1.jpg";
73 const char kAvatar2URL[] = "http://localhost/avatar2.jpg";
75 void ConstructAvatarPolicy(const std::string& file_name,
76 const std::string& url,
77 std::string* policy_data,
78 std::string* policy) {
79 base::FilePath test_data_dir;
80 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir));
81 ASSERT_TRUE(base::ReadFileToString(
82 test_data_dir.Append("chromeos").Append(file_name),
84 base::JSONWriter::Write(
85 test::ConstructExternalDataReference(url, *policy_data).get(),
91 class CloudExternalDataPolicyObserverTest
92 : public chromeos::DeviceSettingsTestBase,
93 public CloudExternalDataPolicyObserver::Delegate {
95 typedef std::pair<std::string, std::string> FetchedCall;
97 CloudExternalDataPolicyObserverTest();
98 virtual ~CloudExternalDataPolicyObserverTest();
100 // chromeos::DeviceSettingsTestBase:
101 virtual void SetUp() override;
102 virtual void TearDown() override;
104 // CloudExternalDataPolicyObserver::Delegate:
105 virtual void OnExternalDataSet(const std::string& policy,
106 const std::string& user_id) override;
107 virtual void OnExternalDataCleared(const std::string& policy,
108 const std::string& user_id) override;
109 virtual void OnExternalDataFetched(const std::string& policy,
110 const std::string& user_id,
111 scoped_ptr<std::string> data) override;
113 void CreateObserver();
115 void ClearObservations();
117 void SetDeviceLocalAccountAvatarPolicy(const std::string& account_id,
118 const std::string& value);
120 void AddDeviceLocalAccount(const std::string& account_id);
121 void RemoveDeviceLocalAccount(const std::string& account_id);
123 DeviceLocalAccountPolicyBroker* GetBrokerForDeviceLocalAccountUser();
125 void RefreshDeviceLocalAccountPolicy(DeviceLocalAccountPolicyBroker* broker);
127 void LogInAsDeviceLocalAccount(const std::string& user_id);
129 void SetRegularUserAvatarPolicy(const std::string& value);
131 void LogInAsRegularUser();
133 const std::string device_local_account_user_id_;
135 std::string avatar_policy_1_data_;
136 std::string avatar_policy_2_data_;
137 std::string avatar_policy_1_;
138 std::string avatar_policy_2_;
140 chromeos::CrosSettings cros_settings_;
141 scoped_ptr<DeviceLocalAccountPolicyService>
142 device_local_account_policy_service_;
143 net::TestURLFetcherFactory url_fetcher_factory_;
145 scoped_ptr<DeviceLocalAccountPolicyProvider>
146 device_local_account_policy_provider_;
148 MockCloudExternalDataManager external_data_manager_;
149 MockConfigurationPolicyProvider user_policy_provider_;
151 scoped_ptr<TestingProfile> profile_;
153 scoped_ptr<CloudExternalDataPolicyObserver> observer_;
155 std::vector<std::string> set_calls_;
156 std::vector<std::string> cleared_calls_;
157 std::vector<FetchedCall> fetched_calls_;
159 ExternalDataFetcher::FetchCallback fetch_callback_;
161 TestingProfileManager profile_manager_;
164 DISALLOW_COPY_AND_ASSIGN(CloudExternalDataPolicyObserverTest);
167 CloudExternalDataPolicyObserverTest::CloudExternalDataPolicyObserverTest()
168 : device_local_account_user_id_(GenerateDeviceLocalAccountUserId(
170 DeviceLocalAccount::TYPE_PUBLIC_SESSION)),
171 cros_settings_(&device_settings_service_),
172 profile_manager_(TestingBrowserProcess::GetGlobal()) {
175 CloudExternalDataPolicyObserverTest::~CloudExternalDataPolicyObserverTest() {
178 void CloudExternalDataPolicyObserverTest::SetUp() {
179 chromeos::DeviceSettingsTestBase::SetUp();
180 ASSERT_TRUE(profile_manager_.SetUp());
181 device_local_account_policy_service_.reset(
182 new DeviceLocalAccountPolicyService(&device_settings_test_helper_,
183 &device_settings_service_,
185 base::MessageLoopProxy::current(),
186 base::MessageLoopProxy::current(),
187 base::MessageLoopProxy::current(),
188 base::MessageLoopProxy::current(),
190 url_fetcher_factory_.set_remove_fetcher_on_delete(true);
192 EXPECT_CALL(user_policy_provider_, IsInitializationComplete(_))
193 .WillRepeatedly(Return(true));
194 user_policy_provider_.Init();
196 ConstructAvatarPolicy("avatar1.jpg",
198 &avatar_policy_1_data_,
200 ConstructAvatarPolicy("avatar2.jpg",
202 &avatar_policy_2_data_,
206 void CloudExternalDataPolicyObserverTest::TearDown() {
208 user_policy_provider_.Shutdown();
210 if (device_local_account_policy_provider_) {
211 device_local_account_policy_provider_->Shutdown();
212 device_local_account_policy_provider_.reset();
214 device_local_account_policy_service_->Shutdown();
215 device_local_account_policy_service_.reset();
216 chromeos::DeviceSettingsTestBase::TearDown();
220 void CloudExternalDataPolicyObserverTest::OnExternalDataSet(
221 const std::string& policy,
222 const std::string& user_id) {
223 EXPECT_EQ(key::kUserAvatarImage, policy);
224 set_calls_.push_back(user_id);
227 void CloudExternalDataPolicyObserverTest::OnExternalDataCleared(
228 const std::string& policy,
229 const std::string& user_id) {
230 EXPECT_EQ(key::kUserAvatarImage, policy);
231 cleared_calls_.push_back(user_id);
234 void CloudExternalDataPolicyObserverTest::OnExternalDataFetched(
235 const std::string& policy,
236 const std::string& user_id,
237 scoped_ptr<std::string> data) {
238 EXPECT_EQ(key::kUserAvatarImage, policy);
239 fetched_calls_.push_back(make_pair(user_id, std::string()));
240 fetched_calls_.back().second.swap(*data);
243 void CloudExternalDataPolicyObserverTest::CreateObserver() {
244 observer_.reset(new CloudExternalDataPolicyObserver(
246 device_local_account_policy_service_.get(),
247 key::kUserAvatarImage,
252 void CloudExternalDataPolicyObserverTest::ClearObservations() {
254 cleared_calls_.clear();
255 fetched_calls_.clear();
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);
267 builder.payload().mutable_useravatarimage()->set_value(value);
269 device_settings_test_helper_.set_device_local_account_policy_blob(
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);
280 em::DeviceLocalAccountInfoProto::ACCOUNT_TYPE_PUBLIC_SESSION);
281 device_policy_.Build();
282 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
283 ReloadDeviceSettings();
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());
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);
301 em::DeviceLocalAccountInfoProto::ACCOUNT_TYPE_PUBLIC_SESSION);
303 device_policy_.Build();
304 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
305 ReloadDeviceSettings();
308 DeviceLocalAccountPolicyBroker*
309 CloudExternalDataPolicyObserverTest::GetBrokerForDeviceLocalAccountUser() {
310 return device_local_account_policy_service_->GetBrokerForUser(
311 device_local_account_user_id_);
314 void CloudExternalDataPolicyObserverTest::RefreshDeviceLocalAccountPolicy(
315 DeviceLocalAccountPolicyBroker* broker) {
316 broker->core()->store()->Load();
317 device_settings_test_helper_.Flush();
320 void CloudExternalDataPolicyObserverTest::LogInAsDeviceLocalAccount(
321 const std::string& user_id) {
322 user_manager_->AddUser(user_id);
324 device_local_account_policy_provider_.reset(
325 new DeviceLocalAccountPolicyProvider(
327 device_local_account_policy_service_.get(),
328 scoped_ptr<PolicyMap>()));
330 PolicyServiceImpl::Providers providers;
331 providers.push_back(device_local_account_policy_provider_.get());
332 TestingProfile::Builder builder;
333 builder.SetPolicyService(
334 scoped_ptr<PolicyService>(new PolicyServiceImpl(providers)));
335 builder.SetPath(chromeos::ProfileHelper::Get()->GetProfilePathByUserIdHash(
336 chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting(user_id)));
338 profile_ = builder.Build();
339 profile_->set_profile_name(user_id);
341 content::NotificationService::current()->Notify(
342 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
343 content::NotificationService::AllSources(),
344 content::Details<Profile>(profile_.get()));
347 void CloudExternalDataPolicyObserverTest::SetRegularUserAvatarPolicy(
348 const std::string& value) {
349 PolicyMap policy_map;
350 if (!value.empty()) {
352 key::kUserAvatarImage,
353 POLICY_LEVEL_MANDATORY,
355 new base::StringValue(value),
356 external_data_manager_.CreateExternalDataFetcher(
357 key::kUserAvatarImage).release());
359 user_policy_provider_.UpdateChromePolicy(policy_map);
362 void CloudExternalDataPolicyObserverTest::LogInAsRegularUser() {
363 user_manager_->AddUser(kRegularUserID);
365 PolicyServiceImpl::Providers providers;
366 providers.push_back(&user_policy_provider_);
367 TestingProfile::Builder builder;
368 builder.SetPolicyService(
369 scoped_ptr<PolicyService>(new PolicyServiceImpl(providers)));
370 builder.SetPath(chromeos::ProfileHelper::Get()->GetProfilePathByUserIdHash(
371 chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting(
374 profile_ = builder.Build();
375 profile_->set_profile_name(kRegularUserID);
377 content::NotificationService::current()->Notify(
378 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
379 content::NotificationService::AllSources(),
380 content::Details<Profile>(profile_.get()));
383 // Verifies that when an external data reference is set for a device-local
384 // account, a corresponding notification is emitted and a fetch is started.
385 // Further verifies that when the fetch succeeds, a notification containing the
386 // external data is emitted.
387 TEST_F(CloudExternalDataPolicyObserverTest,
388 ExistingDeviceLocalAccountFetchSuccess) {
389 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
390 AddDeviceLocalAccount(kDeviceLocalAccount);
392 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
394 broker->external_data_manager()->Connect(NULL);
395 base::RunLoop().RunUntilIdle();
399 EXPECT_TRUE(cleared_calls_.empty());
400 EXPECT_TRUE(fetched_calls_.empty());
401 ASSERT_EQ(1u, set_calls_.size());
402 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
405 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
406 ASSERT_TRUE(fetcher);
407 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
409 fetcher->SetResponseString(avatar_policy_1_data_);
410 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
412 fetcher->set_response_code(200);
413 fetcher->delegate()->OnURLFetchComplete(fetcher);
414 base::RunLoop().RunUntilIdle();
416 EXPECT_TRUE(set_calls_.empty());
417 EXPECT_TRUE(cleared_calls_.empty());
418 ASSERT_EQ(1u, fetched_calls_.size());
419 EXPECT_EQ(device_local_account_user_id_, fetched_calls_.front().first);
420 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
423 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(1));
426 // Verifies that when an external data reference is set for a device-local
427 // account, a corresponding notification is emitted and a fetch is started.
428 // Further verifies that when the fetch fails, no notification is emitted.
429 TEST_F(CloudExternalDataPolicyObserverTest,
430 ExistingDeviceLocalAccountFetchFailure) {
431 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
432 AddDeviceLocalAccount(kDeviceLocalAccount);
434 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
436 broker->external_data_manager()->Connect(NULL);
437 base::RunLoop().RunUntilIdle();
441 EXPECT_TRUE(cleared_calls_.empty());
442 EXPECT_TRUE(fetched_calls_.empty());
443 ASSERT_EQ(1u, set_calls_.size());
444 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
447 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
448 ASSERT_TRUE(fetcher);
449 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
451 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
453 fetcher->set_response_code(400);
454 fetcher->delegate()->OnURLFetchComplete(fetcher);
455 base::RunLoop().RunUntilIdle();
457 EXPECT_TRUE(set_calls_.empty());
458 EXPECT_TRUE(cleared_calls_.empty());
459 EXPECT_TRUE(fetched_calls_.empty());
462 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(1));
465 // Verifies that when the external data reference for a device-local account is
466 // initially not set, no notifications are emitted. Further verifies that when
467 // the external data reference is then cleared (which is a no-op), again, no
468 // notifications are emitted.
469 TEST_F(CloudExternalDataPolicyObserverTest,
470 ExistingDeviceLocalAccountClearUnset) {
471 AddDeviceLocalAccount(kDeviceLocalAccount);
473 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
475 broker->external_data_manager()->Connect(NULL);
476 base::RunLoop().RunUntilIdle();
480 EXPECT_TRUE(set_calls_.empty());
481 EXPECT_TRUE(cleared_calls_.empty());
482 EXPECT_TRUE(fetched_calls_.empty());
485 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
487 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, "");
488 RefreshDeviceLocalAccountPolicy(broker);
490 EXPECT_TRUE(set_calls_.empty());
491 EXPECT_TRUE(cleared_calls_.empty());
492 EXPECT_TRUE(fetched_calls_.empty());
495 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
498 // Verifies that when the external data reference for a device-local account is
499 // initially set, a corresponding notification is emitted and a fetch is
500 // started. Further verifies that when the external data reference is then
501 // cleared, a corresponding notification is emitted and the fetch is canceled.
502 TEST_F(CloudExternalDataPolicyObserverTest,
503 ExistingDeviceLocalAccountClearSet) {
504 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
505 AddDeviceLocalAccount(kDeviceLocalAccount);
507 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
509 broker->external_data_manager()->Connect(NULL);
510 base::RunLoop().RunUntilIdle();
514 EXPECT_TRUE(cleared_calls_.empty());
515 EXPECT_TRUE(fetched_calls_.empty());
516 ASSERT_EQ(1u, set_calls_.size());
517 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
520 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
521 ASSERT_TRUE(fetcher);
522 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
524 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, "");
525 RefreshDeviceLocalAccountPolicy(broker);
527 EXPECT_TRUE(set_calls_.empty());
528 EXPECT_TRUE(fetched_calls_.empty());
529 ASSERT_EQ(1u, cleared_calls_.size());
530 EXPECT_EQ(device_local_account_user_id_, cleared_calls_.front());
533 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
536 // Verifies that when the external data reference for a device-local account is
537 // initially not set, no notifications are emitted. Further verifies that when
538 // the external data reference is then set, a corresponding notification is
539 // emitted and a fetch is started. Also verifies that when the fetch eventually
540 // succeeds, a notification containing the external data is emitted.
541 TEST_F(CloudExternalDataPolicyObserverTest,
542 ExistingDeviceLocalAccountSetUnset) {
543 AddDeviceLocalAccount(kDeviceLocalAccount);
545 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
547 broker->external_data_manager()->Connect(NULL);
548 base::RunLoop().RunUntilIdle();
552 EXPECT_TRUE(set_calls_.empty());
553 EXPECT_TRUE(cleared_calls_.empty());
554 EXPECT_TRUE(fetched_calls_.empty());
557 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
558 RefreshDeviceLocalAccountPolicy(broker);
560 EXPECT_TRUE(cleared_calls_.empty());
561 EXPECT_TRUE(fetched_calls_.empty());
562 ASSERT_EQ(1u, set_calls_.size());
563 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
566 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
567 ASSERT_TRUE(fetcher);
568 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
570 fetcher->SetResponseString(avatar_policy_1_data_);
571 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
573 fetcher->set_response_code(200);
574 fetcher->delegate()->OnURLFetchComplete(fetcher);
575 base::RunLoop().RunUntilIdle();
577 EXPECT_TRUE(set_calls_.empty());
578 EXPECT_TRUE(cleared_calls_.empty());
579 ASSERT_EQ(1u, fetched_calls_.size());
580 EXPECT_EQ(device_local_account_user_id_, fetched_calls_.front().first);
581 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
584 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(1));
587 // Verifies that when the external data reference for a device-local account is
588 // initially set, a corresponding notification is emitted and a fetch is
589 // started. Further verifies that when the external data reference is then
590 // updated, a corresponding notification is emitted and the fetch is restarted.
591 // Also verifies that when the fetch eventually succeeds, a notification
592 // containing the external data is emitted.
593 TEST_F(CloudExternalDataPolicyObserverTest, ExistingDeviceLocalAccountSetSet) {
594 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
595 AddDeviceLocalAccount(kDeviceLocalAccount);
597 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
599 broker->external_data_manager()->Connect(NULL);
600 base::RunLoop().RunUntilIdle();
604 EXPECT_TRUE(cleared_calls_.empty());
605 EXPECT_TRUE(fetched_calls_.empty());
606 ASSERT_EQ(1u, set_calls_.size());
607 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
610 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
611 ASSERT_TRUE(fetcher);
612 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
614 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_2_);
615 RefreshDeviceLocalAccountPolicy(broker);
617 EXPECT_TRUE(cleared_calls_.empty());
618 EXPECT_TRUE(fetched_calls_.empty());
619 ASSERT_EQ(1u, set_calls_.size());
620 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
623 fetcher = url_fetcher_factory_.GetFetcherByID(1);
624 ASSERT_TRUE(fetcher);
625 EXPECT_EQ(GURL(kAvatar2URL), fetcher->GetOriginalURL());
627 fetcher->SetResponseString(avatar_policy_2_data_);
628 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
630 fetcher->set_response_code(200);
631 fetcher->delegate()->OnURLFetchComplete(fetcher);
632 base::RunLoop().RunUntilIdle();
634 EXPECT_TRUE(set_calls_.empty());
635 EXPECT_TRUE(cleared_calls_.empty());
636 ASSERT_EQ(1u, fetched_calls_.size());
637 EXPECT_EQ(device_local_account_user_id_, fetched_calls_.front().first);
638 EXPECT_EQ(avatar_policy_2_data_, fetched_calls_.front().second);
641 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(2));
644 // Verifies that when the external data reference for a device-local account is
645 // initially not set, no notifications are emitted during login into the
646 // account. Further verifies that when the external data reference is then set,
647 // a corresponding notification is emitted only once and a fetch is started.
648 // Also verifies that when the fetch eventually succeeds, a notification
649 // containing the external data is emitted, again, only once.
650 TEST_F(CloudExternalDataPolicyObserverTest,
651 ExistingDeviceLocalAccountSetAfterLogin) {
652 AddDeviceLocalAccount(kDeviceLocalAccount);
654 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
656 broker->external_data_manager()->Connect(NULL);
657 base::RunLoop().RunUntilIdle();
661 LogInAsDeviceLocalAccount(kDeviceLocalAccount);
663 EXPECT_TRUE(set_calls_.empty());
664 EXPECT_TRUE(cleared_calls_.empty());
665 EXPECT_TRUE(fetched_calls_.empty());
668 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
669 RefreshDeviceLocalAccountPolicy(broker);
671 EXPECT_TRUE(cleared_calls_.empty());
672 EXPECT_TRUE(fetched_calls_.empty());
673 ASSERT_EQ(1u, set_calls_.size());
674 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
677 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
678 ASSERT_TRUE(fetcher);
679 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
681 fetcher->SetResponseString(avatar_policy_1_data_);
682 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
684 fetcher->set_response_code(200);
685 fetcher->delegate()->OnURLFetchComplete(fetcher);
686 base::RunLoop().RunUntilIdle();
688 EXPECT_TRUE(set_calls_.empty());
689 EXPECT_TRUE(cleared_calls_.empty());
690 ASSERT_EQ(1u, fetched_calls_.size());
691 EXPECT_EQ(device_local_account_user_id_, fetched_calls_.front().first);
692 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
695 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(1));
698 // Verifies that when the external data reference for a device-local account is
699 // initially not set, no notifications are emitted. Further verifies that when
700 // the device-local account is then removed, again, no notifications are sent.
701 TEST_F(CloudExternalDataPolicyObserverTest,
702 ExistingDeviceLocalAccountRemoveAccountUnset) {
703 AddDeviceLocalAccount(kDeviceLocalAccount);
705 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
707 broker->external_data_manager()->Connect(NULL);
708 base::RunLoop().RunUntilIdle();
712 EXPECT_TRUE(set_calls_.empty());
713 EXPECT_TRUE(cleared_calls_.empty());
714 EXPECT_TRUE(fetched_calls_.empty());
717 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
719 RemoveDeviceLocalAccount(kDeviceLocalAccount);
721 EXPECT_TRUE(set_calls_.empty());
722 EXPECT_TRUE(cleared_calls_.empty());
723 EXPECT_TRUE(fetched_calls_.empty());
726 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
729 // Verifies that when the external data reference for a device-local account is
730 // initially set, a corresponding notification is emitted and a fetch is
731 // started. Further verifies that when the device-local account is then removed,
732 // a notification indicating that the external data reference has been cleared
733 // is emitted and the fetch is canceled.
734 TEST_F(CloudExternalDataPolicyObserverTest,
735 ExistingDeviceLocalAccountRemoveAccountSet) {
736 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
737 AddDeviceLocalAccount(kDeviceLocalAccount);
739 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
741 broker->external_data_manager()->Connect(NULL);
742 base::RunLoop().RunUntilIdle();
746 EXPECT_TRUE(cleared_calls_.empty());
747 EXPECT_TRUE(fetched_calls_.empty());
748 ASSERT_EQ(1u, set_calls_.size());
749 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
752 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
753 ASSERT_TRUE(fetcher);
754 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
756 RemoveDeviceLocalAccount(kDeviceLocalAccount);
758 EXPECT_TRUE(set_calls_.empty());
759 EXPECT_TRUE(fetched_calls_.empty());
760 ASSERT_EQ(1u, cleared_calls_.size());
761 EXPECT_EQ(device_local_account_user_id_, cleared_calls_.front());
764 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
767 // Verifies that when an external data reference is set for a regular user and
768 // the user logs in, a corresponding notification is emitted and a fetch is
769 // started. Further verifies that when the fetch succeeds, a notification
770 // containing the external data is emitted.
771 TEST_F(CloudExternalDataPolicyObserverTest, RegularUserFetchSuccess) {
772 SetRegularUserAvatarPolicy(avatar_policy_1_);
776 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
778 .WillOnce(SaveArg<1>(&fetch_callback_));
780 LogInAsRegularUser();
782 EXPECT_TRUE(cleared_calls_.empty());
783 EXPECT_TRUE(fetched_calls_.empty());
784 ASSERT_EQ(1u, set_calls_.size());
785 EXPECT_EQ(kRegularUserID, set_calls_.front());
788 Mock::VerifyAndClear(&external_data_manager_);
789 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
791 fetch_callback_.Run(make_scoped_ptr(new std::string(avatar_policy_1_data_)));
793 EXPECT_TRUE(set_calls_.empty());
794 EXPECT_TRUE(cleared_calls_.empty());
795 ASSERT_EQ(1u, fetched_calls_.size());
796 EXPECT_EQ(kRegularUserID, fetched_calls_.front().first);
797 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
801 // Verifies that when the external data reference for a regular user is not set
802 // while the user is logging in, no notifications are emitted. Further verifies
803 // that when the external data reference is then cleared (which is a no-op),
804 // again, no notifications are emitted.
805 TEST_F(CloudExternalDataPolicyObserverTest, RegularUserClearUnset) {
808 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
810 LogInAsRegularUser();
812 EXPECT_TRUE(set_calls_.empty());
813 EXPECT_TRUE(cleared_calls_.empty());
814 EXPECT_TRUE(fetched_calls_.empty());
817 Mock::VerifyAndClear(&external_data_manager_);
818 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
820 SetRegularUserAvatarPolicy("");
822 EXPECT_TRUE(set_calls_.empty());
823 EXPECT_TRUE(cleared_calls_.empty());
824 EXPECT_TRUE(fetched_calls_.empty());
828 // Verifies that when the external data reference for a regular user is set
829 // while the user is logging in, a corresponding notification is emitted and a
830 // fetch is started. Further verifies that when the external data reference is
831 // then cleared, a corresponding notification is emitted and no new fetch is
833 TEST_F(CloudExternalDataPolicyObserverTest, RegularUserClearSet) {
834 SetRegularUserAvatarPolicy(avatar_policy_1_);
838 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
840 .WillOnce(SaveArg<1>(&fetch_callback_));
842 LogInAsRegularUser();
844 EXPECT_TRUE(cleared_calls_.empty());
845 EXPECT_TRUE(fetched_calls_.empty());
846 ASSERT_EQ(1u, set_calls_.size());
847 EXPECT_EQ(kRegularUserID, set_calls_.front());
850 Mock::VerifyAndClear(&external_data_manager_);
851 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
853 SetRegularUserAvatarPolicy("");
855 EXPECT_TRUE(set_calls_.empty());
856 EXPECT_TRUE(fetched_calls_.empty());
857 ASSERT_EQ(1u, cleared_calls_.size());
858 EXPECT_EQ(kRegularUserID, cleared_calls_.front());
863 // Verifies that when the external data reference for a regular user is not set
864 // while the user is logging in, no notifications are emitted. Further verifies
865 // that when the external data reference is then set, a corresponding
866 // notification is emitted and a fetch is started. Also verifies that when the
867 // fetch eventually succeeds, a notification containing the external data is
869 TEST_F(CloudExternalDataPolicyObserverTest, RegularUserSetUnset) {
872 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
874 LogInAsRegularUser();
876 EXPECT_TRUE(set_calls_.empty());
877 EXPECT_TRUE(cleared_calls_.empty());
878 EXPECT_TRUE(fetched_calls_.empty());
881 Mock::VerifyAndClear(&external_data_manager_);
882 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
884 .WillOnce(SaveArg<1>(&fetch_callback_));
886 SetRegularUserAvatarPolicy(avatar_policy_1_);
888 EXPECT_TRUE(cleared_calls_.empty());
889 EXPECT_TRUE(fetched_calls_.empty());
890 ASSERT_EQ(1u, set_calls_.size());
891 EXPECT_EQ(kRegularUserID, set_calls_.front());
894 Mock::VerifyAndClear(&external_data_manager_);
895 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
897 fetch_callback_.Run(make_scoped_ptr(new std::string(avatar_policy_1_data_)));
899 EXPECT_TRUE(set_calls_.empty());
900 EXPECT_TRUE(cleared_calls_.empty());
901 ASSERT_EQ(1u, fetched_calls_.size());
902 EXPECT_EQ(kRegularUserID, fetched_calls_.front().first);
903 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
907 // Verifies that when the external data reference for a regular user is set
908 // while the user is logging in, a corresponding notification is emitted and a
909 // fetch is started. Further verifies that when the external data reference is
910 // then updated, a corresponding notification is emitted and the fetch is
911 // restarted. Also verifies that when the fetch eventually succeeds, a
912 // notification containing the external data is emitted.
913 TEST_F(CloudExternalDataPolicyObserverTest, RegularUserSetSet) {
914 SetRegularUserAvatarPolicy(avatar_policy_1_);
918 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
920 .WillOnce(SaveArg<1>(&fetch_callback_));
922 LogInAsRegularUser();
924 EXPECT_TRUE(cleared_calls_.empty());
925 EXPECT_TRUE(fetched_calls_.empty());
926 ASSERT_EQ(1u, set_calls_.size());
927 EXPECT_EQ(kRegularUserID, set_calls_.front());
930 Mock::VerifyAndClear(&external_data_manager_);
931 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
933 .WillOnce(SaveArg<1>(&fetch_callback_));
935 SetRegularUserAvatarPolicy(avatar_policy_2_);
937 EXPECT_TRUE(cleared_calls_.empty());
938 EXPECT_TRUE(fetched_calls_.empty());
939 ASSERT_EQ(1u, set_calls_.size());
940 EXPECT_EQ(kRegularUserID, set_calls_.front());
943 Mock::VerifyAndClear(&external_data_manager_);
944 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
946 fetch_callback_.Run(make_scoped_ptr(new std::string(avatar_policy_2_data_)));
948 EXPECT_TRUE(set_calls_.empty());
949 EXPECT_TRUE(cleared_calls_.empty());
950 ASSERT_EQ(1u, fetched_calls_.size());
951 EXPECT_EQ(kRegularUserID, fetched_calls_.front().first);
952 EXPECT_EQ(avatar_policy_2_data_, fetched_calls_.front().second);
956 } // namespace policy