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/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/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/settings/device_settings_service.h"
27 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h"
28 #include "chrome/browser/profiles/profile.h"
29 #include "chrome/common/chrome_paths.h"
30 #include "chrome/test/base/testing_profile.h"
31 #include "components/policy/core/common/cloud/cloud_policy_core.h"
32 #include "components/policy/core/common/cloud/cloud_policy_store.h"
33 #include "components/policy/core/common/cloud/mock_cloud_external_data_manager.h"
34 #include "components/policy/core/common/cloud/policy_builder.h"
35 #include "components/policy/core/common/external_data_fetcher.h"
36 #include "components/policy/core/common/mock_configuration_policy_provider.h"
37 #include "components/policy/core/common/policy_map.h"
38 #include "components/policy/core/common/policy_service.h"
39 #include "components/policy/core/common/policy_service_impl.h"
40 #include "components/policy/core/common/policy_types.h"
41 #include "content/public/browser/notification_details.h"
42 #include "content/public/browser/notification_service.h"
43 #include "content/public/browser/notification_source.h"
44 #include "net/url_request/test_url_fetcher_factory.h"
45 #include "net/url_request/url_fetcher_delegate.h"
46 #include "net/url_request/url_request_context_getter.h"
47 #include "net/url_request/url_request_status.h"
48 #include "policy/policy_constants.h"
49 #include "policy/proto/cloud_policy.pb.h"
50 #include "testing/gmock/include/gmock/gmock.h"
51 #include "testing/gtest/include/gtest/gtest.h"
54 namespace em = enterprise_management;
56 using ::testing::Mock;
57 using ::testing::Return;
58 using ::testing::SaveArg;
65 const char kDeviceLocalAccount[] = "device_local_account@localhost";
67 const char kRegularUserID[] = "user@example.com";
69 const char kAvatar1URL[] = "http://localhost/avatar1.jpg";
70 const char kAvatar2URL[] = "http://localhost/avatar2.jpg";
72 void ConstructAvatarPolicy(const std::string& file_name,
73 const std::string& url,
74 std::string* policy_data,
75 std::string* policy) {
76 base::FilePath test_data_dir;
77 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir));
78 ASSERT_TRUE(base::ReadFileToString(
79 test_data_dir.Append("chromeos").Append(file_name),
81 base::JSONWriter::Write(
82 test::ConstructExternalDataReference(url, *policy_data).get(),
88 class CloudExternalDataPolicyObserverTest
89 : public chromeos::DeviceSettingsTestBase,
90 public CloudExternalDataPolicyObserver::Delegate {
92 typedef std::pair<std::string, std::string> FetchedCall;
94 CloudExternalDataPolicyObserverTest();
95 virtual ~CloudExternalDataPolicyObserverTest();
97 // chromeos::DeviceSettingsTestBase:
98 virtual void SetUp() OVERRIDE;
99 virtual void TearDown() OVERRIDE;
101 // CloudExternalDataPolicyObserver::Delegate:
102 virtual void OnExternalDataSet(const std::string& policy,
103 const std::string& user_id) OVERRIDE;
104 virtual void OnExternalDataCleared(const std::string& policy,
105 const std::string& user_id) OVERRIDE;
106 virtual void OnExternalDataFetched(const std::string& policy,
107 const std::string& user_id,
108 scoped_ptr<std::string> data) OVERRIDE;
110 void CreateObserver();
112 void ClearObservations();
114 void SetDeviceLocalAccountAvatarPolicy(const std::string& account_id,
115 const std::string& value);
117 void AddDeviceLocalAccount(const std::string& account_id);
118 void RemoveDeviceLocalAccount(const std::string& account_id);
120 DeviceLocalAccountPolicyBroker* GetBrokerForDeviceLocalAccountUser();
122 void RefreshDeviceLocalAccountPolicy(DeviceLocalAccountPolicyBroker* broker);
124 void LogInAsDeviceLocalAccount(const std::string& user_id);
126 void SetRegularUserAvatarPolicy(const std::string& value);
128 void LogInAsRegularUser();
130 const std::string device_local_account_user_id_;
132 std::string avatar_policy_1_data_;
133 std::string avatar_policy_2_data_;
134 std::string avatar_policy_1_;
135 std::string avatar_policy_2_;
137 chromeos::CrosSettings cros_settings_;
138 scoped_ptr<DeviceLocalAccountPolicyService>
139 device_local_account_policy_service_;
140 net::TestURLFetcherFactory url_fetcher_factory_;
142 scoped_ptr<DeviceLocalAccountPolicyProvider>
143 device_local_account_policy_provider_;
145 MockCloudExternalDataManager external_data_manager_;
146 MockConfigurationPolicyProvider user_policy_provider_;
148 scoped_ptr<TestingProfile> profile_;
150 scoped_ptr<CloudExternalDataPolicyObserver> observer_;
152 std::vector<std::string> set_calls_;
153 std::vector<std::string> cleared_calls_;
154 std::vector<FetchedCall> fetched_calls_;
156 ExternalDataFetcher::FetchCallback fetch_callback_;
159 DISALLOW_COPY_AND_ASSIGN(CloudExternalDataPolicyObserverTest);
162 CloudExternalDataPolicyObserverTest::CloudExternalDataPolicyObserverTest()
163 : device_local_account_user_id_(GenerateDeviceLocalAccountUserId(
165 DeviceLocalAccount::TYPE_PUBLIC_SESSION)),
166 cros_settings_(&device_settings_service_) {
169 CloudExternalDataPolicyObserverTest::~CloudExternalDataPolicyObserverTest() {
172 void CloudExternalDataPolicyObserverTest::SetUp() {
173 chromeos::DeviceSettingsTestBase::SetUp();
174 device_local_account_policy_service_.reset(
175 new DeviceLocalAccountPolicyService(&device_settings_test_helper_,
176 &device_settings_service_,
178 base::MessageLoopProxy::current(),
179 base::MessageLoopProxy::current(),
180 base::MessageLoopProxy::current(),
181 base::MessageLoopProxy::current(),
183 url_fetcher_factory_.set_remove_fetcher_on_delete(true);
185 EXPECT_CALL(user_policy_provider_, IsInitializationComplete(_))
186 .WillRepeatedly(Return(true));
187 user_policy_provider_.Init();
189 ConstructAvatarPolicy("avatar1.jpg",
191 &avatar_policy_1_data_,
193 ConstructAvatarPolicy("avatar2.jpg",
195 &avatar_policy_2_data_,
199 void CloudExternalDataPolicyObserverTest::TearDown() {
201 user_policy_provider_.Shutdown();
203 if (device_local_account_policy_provider_) {
204 device_local_account_policy_provider_->Shutdown();
205 device_local_account_policy_provider_.reset();
207 device_local_account_policy_service_->Shutdown();
208 device_local_account_policy_service_.reset();
209 chromeos::DeviceSettingsTestBase::TearDown();
213 void CloudExternalDataPolicyObserverTest::OnExternalDataSet(
214 const std::string& policy,
215 const std::string& user_id) {
216 EXPECT_EQ(key::kUserAvatarImage, policy);
217 set_calls_.push_back(user_id);
220 void CloudExternalDataPolicyObserverTest::OnExternalDataCleared(
221 const std::string& policy,
222 const std::string& user_id) {
223 EXPECT_EQ(key::kUserAvatarImage, policy);
224 cleared_calls_.push_back(user_id);
227 void CloudExternalDataPolicyObserverTest::OnExternalDataFetched(
228 const std::string& policy,
229 const std::string& user_id,
230 scoped_ptr<std::string> data) {
231 EXPECT_EQ(key::kUserAvatarImage, policy);
232 fetched_calls_.push_back(make_pair(user_id, std::string()));
233 fetched_calls_.back().second.swap(*data);
236 void CloudExternalDataPolicyObserverTest::CreateObserver() {
237 observer_.reset(new CloudExternalDataPolicyObserver(
239 device_local_account_policy_service_.get(),
240 key::kUserAvatarImage,
245 void CloudExternalDataPolicyObserverTest::ClearObservations() {
247 cleared_calls_.clear();
248 fetched_calls_.clear();
251 void CloudExternalDataPolicyObserverTest::SetDeviceLocalAccountAvatarPolicy(
252 const std::string& account_id,
253 const std::string& value) {
254 UserPolicyBuilder builder;
255 builder.policy_data().set_policy_type(
256 dm_protocol::kChromePublicAccountPolicyType);
257 builder.policy_data().set_settings_entity_id(account_id);
258 builder.policy_data().set_username(account_id);
260 builder.payload().mutable_useravatarimage()->set_value(value);
262 device_settings_test_helper_.set_device_local_account_policy_blob(
267 void CloudExternalDataPolicyObserverTest::AddDeviceLocalAccount(
268 const std::string& account_id) {
269 em::DeviceLocalAccountInfoProto* account =
270 device_policy_.payload().mutable_device_local_accounts()->add_account();
271 account->set_account_id(account_id);
273 em::DeviceLocalAccountInfoProto::ACCOUNT_TYPE_PUBLIC_SESSION);
274 device_policy_.Build();
275 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
276 ReloadDeviceSettings();
279 void CloudExternalDataPolicyObserverTest::RemoveDeviceLocalAccount(
280 const std::string& account_id) {
281 em::DeviceLocalAccountsProto* accounts =
282 device_policy_.payload().mutable_device_local_accounts();
283 std::vector<std::string> account_ids;
284 for (int i = 0; i < accounts->account_size(); ++i) {
285 if (accounts->account(i).account_id() != account_id)
286 account_ids.push_back(accounts->account(i).account_id());
288 accounts->clear_account();
289 for (std::vector<std::string>::const_iterator it = account_ids.begin();
290 it != account_ids.end(); ++it) {
291 em::DeviceLocalAccountInfoProto* account = accounts->add_account();
292 account->set_account_id(*it);
294 em::DeviceLocalAccountInfoProto::ACCOUNT_TYPE_PUBLIC_SESSION);
296 device_policy_.Build();
297 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
298 ReloadDeviceSettings();
301 DeviceLocalAccountPolicyBroker*
302 CloudExternalDataPolicyObserverTest::GetBrokerForDeviceLocalAccountUser() {
303 return device_local_account_policy_service_->GetBrokerForUser(
304 device_local_account_user_id_);
307 void CloudExternalDataPolicyObserverTest::RefreshDeviceLocalAccountPolicy(
308 DeviceLocalAccountPolicyBroker* broker) {
309 broker->core()->store()->Load();
310 device_settings_test_helper_.Flush();
313 void CloudExternalDataPolicyObserverTest::LogInAsDeviceLocalAccount(
314 const std::string& user_id) {
315 device_local_account_policy_provider_.reset(
316 new DeviceLocalAccountPolicyProvider(
318 device_local_account_policy_service_.get(),
319 scoped_ptr<PolicyMap>()));
321 PolicyServiceImpl::Providers providers;
322 providers.push_back(device_local_account_policy_provider_.get());
323 TestingProfile::Builder builder;
324 builder.SetPolicyService(
325 scoped_ptr<PolicyService>(new PolicyServiceImpl(providers)));
327 profile_ = builder.Build();
328 profile_->set_profile_name(user_id);
330 user_manager_->AddUser(user_id);
331 content::NotificationService::current()->Notify(
332 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
333 content::NotificationService::AllSources(),
334 content::Details<Profile>(profile_.get()));
337 void CloudExternalDataPolicyObserverTest::SetRegularUserAvatarPolicy(
338 const std::string& value) {
339 PolicyMap policy_map;
340 if (!value.empty()) {
342 key::kUserAvatarImage,
343 POLICY_LEVEL_MANDATORY,
345 new base::StringValue(value),
346 external_data_manager_.CreateExternalDataFetcher(
347 key::kUserAvatarImage).release());
349 user_policy_provider_.UpdateChromePolicy(policy_map);
352 void CloudExternalDataPolicyObserverTest::LogInAsRegularUser() {
353 PolicyServiceImpl::Providers providers;
354 providers.push_back(&user_policy_provider_);
355 TestingProfile::Builder builder;
356 builder.SetPolicyService(
357 scoped_ptr<PolicyService>(new PolicyServiceImpl(providers)));
359 profile_ = builder.Build();
360 profile_->set_profile_name(kRegularUserID);
362 user_manager_->AddUser(kRegularUserID);
363 content::NotificationService::current()->Notify(
364 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
365 content::NotificationService::AllSources(),
366 content::Details<Profile>(profile_.get()));
369 // Verifies that when an external data reference is set for a device-local
370 // account, a corresponding notification is emitted and a fetch is started.
371 // Further verifies that when the fetch succeeds, a notification containing the
372 // external data is emitted.
373 TEST_F(CloudExternalDataPolicyObserverTest,
374 ExistingDeviceLocalAccountFetchSuccess) {
375 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
376 AddDeviceLocalAccount(kDeviceLocalAccount);
378 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
380 broker->external_data_manager()->Connect(NULL);
381 base::RunLoop().RunUntilIdle();
385 EXPECT_TRUE(cleared_calls_.empty());
386 EXPECT_TRUE(fetched_calls_.empty());
387 ASSERT_EQ(1u, set_calls_.size());
388 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
391 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
392 ASSERT_TRUE(fetcher);
393 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
395 fetcher->SetResponseString(avatar_policy_1_data_);
396 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
398 fetcher->set_response_code(200);
399 fetcher->delegate()->OnURLFetchComplete(fetcher);
400 base::RunLoop().RunUntilIdle();
402 EXPECT_TRUE(set_calls_.empty());
403 EXPECT_TRUE(cleared_calls_.empty());
404 ASSERT_EQ(1u, fetched_calls_.size());
405 EXPECT_EQ(device_local_account_user_id_, fetched_calls_.front().first);
406 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
409 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(1));
412 // Verifies that when an external data reference is set for a device-local
413 // account, a corresponding notification is emitted and a fetch is started.
414 // Further verifies that when the fetch fails, no notification is emitted.
415 TEST_F(CloudExternalDataPolicyObserverTest,
416 ExistingDeviceLocalAccountFetchFailure) {
417 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
418 AddDeviceLocalAccount(kDeviceLocalAccount);
420 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
422 broker->external_data_manager()->Connect(NULL);
423 base::RunLoop().RunUntilIdle();
427 EXPECT_TRUE(cleared_calls_.empty());
428 EXPECT_TRUE(fetched_calls_.empty());
429 ASSERT_EQ(1u, set_calls_.size());
430 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
433 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
434 ASSERT_TRUE(fetcher);
435 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
437 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
439 fetcher->set_response_code(400);
440 fetcher->delegate()->OnURLFetchComplete(fetcher);
441 base::RunLoop().RunUntilIdle();
443 EXPECT_TRUE(set_calls_.empty());
444 EXPECT_TRUE(cleared_calls_.empty());
445 EXPECT_TRUE(fetched_calls_.empty());
448 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(1));
451 // Verifies that when the external data reference for a device-local account is
452 // initially not set, no notifications are emitted. Further verifies that when
453 // the external data reference is then cleared (which is a no-op), again, no
454 // notifications are emitted.
455 TEST_F(CloudExternalDataPolicyObserverTest,
456 ExistingDeviceLocalAccountClearUnset) {
457 AddDeviceLocalAccount(kDeviceLocalAccount);
459 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
461 broker->external_data_manager()->Connect(NULL);
462 base::RunLoop().RunUntilIdle();
466 EXPECT_TRUE(set_calls_.empty());
467 EXPECT_TRUE(cleared_calls_.empty());
468 EXPECT_TRUE(fetched_calls_.empty());
471 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
473 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, "");
474 RefreshDeviceLocalAccountPolicy(broker);
476 EXPECT_TRUE(set_calls_.empty());
477 EXPECT_TRUE(cleared_calls_.empty());
478 EXPECT_TRUE(fetched_calls_.empty());
481 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
484 // Verifies that when the external data reference for a device-local account is
485 // initially set, a corresponding notification is emitted and a fetch is
486 // started. Further verifies that when the external data reference is then
487 // cleared, a corresponding notification is emitted and the fetch is canceled.
488 TEST_F(CloudExternalDataPolicyObserverTest,
489 ExistingDeviceLocalAccountClearSet) {
490 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
491 AddDeviceLocalAccount(kDeviceLocalAccount);
493 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
495 broker->external_data_manager()->Connect(NULL);
496 base::RunLoop().RunUntilIdle();
500 EXPECT_TRUE(cleared_calls_.empty());
501 EXPECT_TRUE(fetched_calls_.empty());
502 ASSERT_EQ(1u, set_calls_.size());
503 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
506 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
507 ASSERT_TRUE(fetcher);
508 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
510 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, "");
511 RefreshDeviceLocalAccountPolicy(broker);
513 EXPECT_TRUE(set_calls_.empty());
514 EXPECT_TRUE(fetched_calls_.empty());
515 ASSERT_EQ(1u, cleared_calls_.size());
516 EXPECT_EQ(device_local_account_user_id_, cleared_calls_.front());
519 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
522 // Verifies that when the external data reference for a device-local account is
523 // initially not set, no notifications are emitted. Further verifies that when
524 // the external data reference is then set, a corresponding notification is
525 // emitted and a fetch is started. Also verifies that when the fetch eventually
526 // succeeds, a notification containing the external data is emitted.
527 TEST_F(CloudExternalDataPolicyObserverTest,
528 ExistingDeviceLocalAccountSetUnset) {
529 AddDeviceLocalAccount(kDeviceLocalAccount);
531 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
533 broker->external_data_manager()->Connect(NULL);
534 base::RunLoop().RunUntilIdle();
538 EXPECT_TRUE(set_calls_.empty());
539 EXPECT_TRUE(cleared_calls_.empty());
540 EXPECT_TRUE(fetched_calls_.empty());
543 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
544 RefreshDeviceLocalAccountPolicy(broker);
546 EXPECT_TRUE(cleared_calls_.empty());
547 EXPECT_TRUE(fetched_calls_.empty());
548 ASSERT_EQ(1u, set_calls_.size());
549 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
552 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
553 ASSERT_TRUE(fetcher);
554 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
556 fetcher->SetResponseString(avatar_policy_1_data_);
557 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
559 fetcher->set_response_code(200);
560 fetcher->delegate()->OnURLFetchComplete(fetcher);
561 base::RunLoop().RunUntilIdle();
563 EXPECT_TRUE(set_calls_.empty());
564 EXPECT_TRUE(cleared_calls_.empty());
565 ASSERT_EQ(1u, fetched_calls_.size());
566 EXPECT_EQ(device_local_account_user_id_, fetched_calls_.front().first);
567 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
570 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(1));
573 // Verifies that when the external data reference for a device-local account is
574 // initially set, a corresponding notification is emitted and a fetch is
575 // started. Further verifies that when the external data reference is then
576 // updated, a corresponding notification is emitted and the fetch is restarted.
577 // Also verifies that when the fetch eventually succeeds, a notification
578 // containing the external data is emitted.
579 TEST_F(CloudExternalDataPolicyObserverTest, ExistingDeviceLocalAccountSetSet) {
580 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
581 AddDeviceLocalAccount(kDeviceLocalAccount);
583 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
585 broker->external_data_manager()->Connect(NULL);
586 base::RunLoop().RunUntilIdle();
590 EXPECT_TRUE(cleared_calls_.empty());
591 EXPECT_TRUE(fetched_calls_.empty());
592 ASSERT_EQ(1u, set_calls_.size());
593 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
596 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
597 ASSERT_TRUE(fetcher);
598 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
600 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_2_);
601 RefreshDeviceLocalAccountPolicy(broker);
603 EXPECT_TRUE(cleared_calls_.empty());
604 EXPECT_TRUE(fetched_calls_.empty());
605 ASSERT_EQ(1u, set_calls_.size());
606 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
609 fetcher = url_fetcher_factory_.GetFetcherByID(1);
610 ASSERT_TRUE(fetcher);
611 EXPECT_EQ(GURL(kAvatar2URL), fetcher->GetOriginalURL());
613 fetcher->SetResponseString(avatar_policy_2_data_);
614 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
616 fetcher->set_response_code(200);
617 fetcher->delegate()->OnURLFetchComplete(fetcher);
618 base::RunLoop().RunUntilIdle();
620 EXPECT_TRUE(set_calls_.empty());
621 EXPECT_TRUE(cleared_calls_.empty());
622 ASSERT_EQ(1u, fetched_calls_.size());
623 EXPECT_EQ(device_local_account_user_id_, fetched_calls_.front().first);
624 EXPECT_EQ(avatar_policy_2_data_, fetched_calls_.front().second);
627 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(2));
630 // Verifies that when the external data reference for a device-local account is
631 // initially not set, no notifications are emitted during login into the
632 // account. Further verifies that when the external data reference is then set,
633 // a corresponding notification is emitted only once and a fetch is started.
634 // Also verifies that when the fetch eventually succeeds, a notification
635 // containing the external data is emitted, again, only once.
636 TEST_F(CloudExternalDataPolicyObserverTest,
637 ExistingDeviceLocalAccountSetAfterLogin) {
638 AddDeviceLocalAccount(kDeviceLocalAccount);
640 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
642 broker->external_data_manager()->Connect(NULL);
643 base::RunLoop().RunUntilIdle();
647 LogInAsDeviceLocalAccount(kDeviceLocalAccount);
649 EXPECT_TRUE(set_calls_.empty());
650 EXPECT_TRUE(cleared_calls_.empty());
651 EXPECT_TRUE(fetched_calls_.empty());
654 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
655 RefreshDeviceLocalAccountPolicy(broker);
657 EXPECT_TRUE(cleared_calls_.empty());
658 EXPECT_TRUE(fetched_calls_.empty());
659 ASSERT_EQ(1u, set_calls_.size());
660 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
663 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
664 ASSERT_TRUE(fetcher);
665 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
667 fetcher->SetResponseString(avatar_policy_1_data_);
668 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
670 fetcher->set_response_code(200);
671 fetcher->delegate()->OnURLFetchComplete(fetcher);
672 base::RunLoop().RunUntilIdle();
674 EXPECT_TRUE(set_calls_.empty());
675 EXPECT_TRUE(cleared_calls_.empty());
676 ASSERT_EQ(1u, fetched_calls_.size());
677 EXPECT_EQ(device_local_account_user_id_, fetched_calls_.front().first);
678 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
681 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(1));
684 // Verifies that when the external data reference for a device-local account is
685 // initially not set, no notifications are emitted. Further verifies that when
686 // the device-local account is then removed, again, no notifications are sent.
687 TEST_F(CloudExternalDataPolicyObserverTest,
688 ExistingDeviceLocalAccountRemoveAccountUnset) {
689 AddDeviceLocalAccount(kDeviceLocalAccount);
691 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
693 broker->external_data_manager()->Connect(NULL);
694 base::RunLoop().RunUntilIdle();
698 EXPECT_TRUE(set_calls_.empty());
699 EXPECT_TRUE(cleared_calls_.empty());
700 EXPECT_TRUE(fetched_calls_.empty());
703 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
705 RemoveDeviceLocalAccount(kDeviceLocalAccount);
707 EXPECT_TRUE(set_calls_.empty());
708 EXPECT_TRUE(cleared_calls_.empty());
709 EXPECT_TRUE(fetched_calls_.empty());
712 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
715 // Verifies that when the external data reference for a device-local account is
716 // initially set, a corresponding notification is emitted and a fetch is
717 // started. Further verifies that when the device-local account is then removed,
718 // a notification indicating that the external data reference has been cleared
719 // is emitted and the fetch is canceled.
720 TEST_F(CloudExternalDataPolicyObserverTest,
721 ExistingDeviceLocalAccountRemoveAccountSet) {
722 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
723 AddDeviceLocalAccount(kDeviceLocalAccount);
725 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
727 broker->external_data_manager()->Connect(NULL);
728 base::RunLoop().RunUntilIdle();
732 EXPECT_TRUE(cleared_calls_.empty());
733 EXPECT_TRUE(fetched_calls_.empty());
734 ASSERT_EQ(1u, set_calls_.size());
735 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
738 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
739 ASSERT_TRUE(fetcher);
740 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
742 RemoveDeviceLocalAccount(kDeviceLocalAccount);
744 EXPECT_TRUE(set_calls_.empty());
745 EXPECT_TRUE(fetched_calls_.empty());
746 ASSERT_EQ(1u, cleared_calls_.size());
747 EXPECT_EQ(device_local_account_user_id_, cleared_calls_.front());
750 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
753 // Verifies that when an external data reference is set for a regular user and
754 // the user logs in, a corresponding notification is emitted and a fetch is
755 // started. Further verifies that when the fetch succeeds, a notification
756 // containing the external data is emitted.
757 TEST_F(CloudExternalDataPolicyObserverTest, RegularUserFetchSuccess) {
758 SetRegularUserAvatarPolicy(avatar_policy_1_);
762 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
764 .WillOnce(SaveArg<1>(&fetch_callback_));
766 LogInAsRegularUser();
768 EXPECT_TRUE(cleared_calls_.empty());
769 EXPECT_TRUE(fetched_calls_.empty());
770 ASSERT_EQ(1u, set_calls_.size());
771 EXPECT_EQ(kRegularUserID, set_calls_.front());
774 Mock::VerifyAndClear(&external_data_manager_);
775 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
777 fetch_callback_.Run(make_scoped_ptr(new std::string(avatar_policy_1_data_)));
779 EXPECT_TRUE(set_calls_.empty());
780 EXPECT_TRUE(cleared_calls_.empty());
781 ASSERT_EQ(1u, fetched_calls_.size());
782 EXPECT_EQ(kRegularUserID, fetched_calls_.front().first);
783 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
787 // Verifies that when the external data reference for a regular user is not set
788 // while the user is logging in, no notifications are emitted. Further verifies
789 // that when the external data reference is then cleared (which is a no-op),
790 // again, no notifications are emitted.
791 TEST_F(CloudExternalDataPolicyObserverTest, RegularUserClearUnset) {
794 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
796 LogInAsRegularUser();
798 EXPECT_TRUE(set_calls_.empty());
799 EXPECT_TRUE(cleared_calls_.empty());
800 EXPECT_TRUE(fetched_calls_.empty());
803 Mock::VerifyAndClear(&external_data_manager_);
804 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
806 SetRegularUserAvatarPolicy("");
808 EXPECT_TRUE(set_calls_.empty());
809 EXPECT_TRUE(cleared_calls_.empty());
810 EXPECT_TRUE(fetched_calls_.empty());
814 // Verifies that when the external data reference for a regular user is set
815 // while the user is logging in, a corresponding notification is emitted and a
816 // fetch is started. Further verifies that when the external data reference is
817 // then cleared, a corresponding notification is emitted and no new fetch is
819 TEST_F(CloudExternalDataPolicyObserverTest, RegularUserClearSet) {
820 SetRegularUserAvatarPolicy(avatar_policy_1_);
824 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
826 .WillOnce(SaveArg<1>(&fetch_callback_));
828 LogInAsRegularUser();
830 EXPECT_TRUE(cleared_calls_.empty());
831 EXPECT_TRUE(fetched_calls_.empty());
832 ASSERT_EQ(1u, set_calls_.size());
833 EXPECT_EQ(kRegularUserID, set_calls_.front());
836 Mock::VerifyAndClear(&external_data_manager_);
837 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
839 SetRegularUserAvatarPolicy("");
841 EXPECT_TRUE(set_calls_.empty());
842 EXPECT_TRUE(fetched_calls_.empty());
843 ASSERT_EQ(1u, cleared_calls_.size());
844 EXPECT_EQ(kRegularUserID, cleared_calls_.front());
849 // Verifies that when the external data reference for a regular user is not set
850 // while the user is logging in, no notifications are emitted. Further verifies
851 // that when the external data reference is then set, a corresponding
852 // notification is emitted and a fetch is started. Also verifies that when the
853 // fetch eventually succeeds, a notification containing the external data is
855 TEST_F(CloudExternalDataPolicyObserverTest, RegularUserSetUnset) {
858 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
860 LogInAsRegularUser();
862 EXPECT_TRUE(set_calls_.empty());
863 EXPECT_TRUE(cleared_calls_.empty());
864 EXPECT_TRUE(fetched_calls_.empty());
867 Mock::VerifyAndClear(&external_data_manager_);
868 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
870 .WillOnce(SaveArg<1>(&fetch_callback_));
872 SetRegularUserAvatarPolicy(avatar_policy_1_);
874 EXPECT_TRUE(cleared_calls_.empty());
875 EXPECT_TRUE(fetched_calls_.empty());
876 ASSERT_EQ(1u, set_calls_.size());
877 EXPECT_EQ(kRegularUserID, set_calls_.front());
880 Mock::VerifyAndClear(&external_data_manager_);
881 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
883 fetch_callback_.Run(make_scoped_ptr(new std::string(avatar_policy_1_data_)));
885 EXPECT_TRUE(set_calls_.empty());
886 EXPECT_TRUE(cleared_calls_.empty());
887 ASSERT_EQ(1u, fetched_calls_.size());
888 EXPECT_EQ(kRegularUserID, fetched_calls_.front().first);
889 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
893 // Verifies that when the external data reference for a regular user is set
894 // while the user is logging in, a corresponding notification is emitted and a
895 // fetch is started. Further verifies that when the external data reference is
896 // then updated, a corresponding notification is emitted and the fetch is
897 // restarted. Also verifies that when the fetch eventually succeeds, a
898 // notification containing the external data is emitted.
899 TEST_F(CloudExternalDataPolicyObserverTest, RegularUserSetSet) {
900 SetRegularUserAvatarPolicy(avatar_policy_1_);
904 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
906 .WillOnce(SaveArg<1>(&fetch_callback_));
908 LogInAsRegularUser();
910 EXPECT_TRUE(cleared_calls_.empty());
911 EXPECT_TRUE(fetched_calls_.empty());
912 ASSERT_EQ(1u, set_calls_.size());
913 EXPECT_EQ(kRegularUserID, set_calls_.front());
916 Mock::VerifyAndClear(&external_data_manager_);
917 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
919 .WillOnce(SaveArg<1>(&fetch_callback_));
921 SetRegularUserAvatarPolicy(avatar_policy_2_);
923 EXPECT_TRUE(cleared_calls_.empty());
924 EXPECT_TRUE(fetched_calls_.empty());
925 ASSERT_EQ(1u, set_calls_.size());
926 EXPECT_EQ(kRegularUserID, set_calls_.front());
929 Mock::VerifyAndClear(&external_data_manager_);
930 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
932 fetch_callback_.Run(make_scoped_ptr(new std::string(avatar_policy_2_data_)));
934 EXPECT_TRUE(set_calls_.empty());
935 EXPECT_TRUE(cleared_calls_.empty());
936 ASSERT_EQ(1u, fetched_calls_.size());
937 EXPECT_EQ(kRegularUserID, fetched_calls_.front().first);
938 EXPECT_EQ(avatar_policy_2_data_, fetched_calls_.front().second);
942 } // namespace policy