Update broken references to image assets
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / cloud_external_data_policy_observer_unittest.cc
blobaa26956dc80e1f076dd0a9065b8fd4cffc6fc7c3
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"
7 #include <utility>
8 #include <vector>
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/path_service.h"
15 #include "base/run_loop.h"
16 #include "base/thread_task_runner_handle.h"
17 #include "base/values.h"
18 #include "chrome/browser/chrome_notification_types.h"
19 #include "chrome/browser/chromeos/policy/cloud_external_data_manager_base_test_util.h"
20 #include "chrome/browser/chromeos/policy/device_local_account.h"
21 #include "chrome/browser/chromeos/policy/device_local_account_external_data_manager.h"
22 #include "chrome/browser/chromeos/policy/device_local_account_policy_provider.h"
23 #include "chrome/browser/chromeos/policy/device_local_account_policy_service.h"
24 #include "chrome/browser/chromeos/policy/fake_affiliated_invalidation_service_provider.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"
55 #include "url/gurl.h"
57 namespace em = enterprise_management;
59 using ::testing::Mock;
60 using ::testing::Return;
61 using ::testing::SaveArg;
62 using ::testing::_;
64 namespace policy {
66 namespace {
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),
83 policy_data));
84 base::JSONWriter::Write(
85 *test::ConstructExternalDataReference(url, *policy_data), policy);
88 } // namespace
90 class CloudExternalDataPolicyObserverTest
91 : public chromeos::DeviceSettingsTestBase,
92 public CloudExternalDataPolicyObserver::Delegate {
93 public:
94 typedef std::pair<std::string, std::string> FetchedCall;
96 CloudExternalDataPolicyObserverTest();
97 ~CloudExternalDataPolicyObserverTest() override;
99 // chromeos::DeviceSettingsTestBase:
100 void SetUp() override;
101 void TearDown() override;
103 // CloudExternalDataPolicyObserver::Delegate:
104 void OnExternalDataSet(const std::string& policy,
105 const std::string& user_id) override;
106 void OnExternalDataCleared(const std::string& policy,
107 const std::string& user_id) override;
108 void OnExternalDataFetched(const std::string& policy,
109 const std::string& user_id,
110 scoped_ptr<std::string> data) override;
112 void CreateObserver();
114 void ClearObservations();
116 void SetDeviceLocalAccountAvatarPolicy(const std::string& account_id,
117 const std::string& value);
119 void AddDeviceLocalAccount(const std::string& account_id);
120 void RemoveDeviceLocalAccount(const std::string& account_id);
122 DeviceLocalAccountPolicyBroker* GetBrokerForDeviceLocalAccountUser();
124 void RefreshDeviceLocalAccountPolicy(DeviceLocalAccountPolicyBroker* broker);
126 void LogInAsDeviceLocalAccount(const std::string& user_id);
128 void SetRegularUserAvatarPolicy(const std::string& value);
130 void LogInAsRegularUser();
132 const std::string device_local_account_user_id_;
134 std::string avatar_policy_1_data_;
135 std::string avatar_policy_2_data_;
136 std::string avatar_policy_1_;
137 std::string avatar_policy_2_;
139 chromeos::CrosSettings cros_settings_;
140 scoped_ptr<DeviceLocalAccountPolicyService>
141 device_local_account_policy_service_;
142 FakeAffiliatedInvalidationServiceProvider
143 affiliated_invalidation_service_provider_;
144 net::TestURLFetcherFactory url_fetcher_factory_;
146 scoped_ptr<DeviceLocalAccountPolicyProvider>
147 device_local_account_policy_provider_;
149 MockCloudExternalDataManager external_data_manager_;
150 MockConfigurationPolicyProvider user_policy_provider_;
152 scoped_ptr<TestingProfile> profile_;
154 scoped_ptr<CloudExternalDataPolicyObserver> observer_;
156 std::vector<std::string> set_calls_;
157 std::vector<std::string> cleared_calls_;
158 std::vector<FetchedCall> fetched_calls_;
160 ExternalDataFetcher::FetchCallback fetch_callback_;
162 TestingProfileManager profile_manager_;
164 private:
165 DISALLOW_COPY_AND_ASSIGN(CloudExternalDataPolicyObserverTest);
168 CloudExternalDataPolicyObserverTest::CloudExternalDataPolicyObserverTest()
169 : device_local_account_user_id_(GenerateDeviceLocalAccountUserId(
170 kDeviceLocalAccount,
171 DeviceLocalAccount::TYPE_PUBLIC_SESSION)),
172 cros_settings_(&device_settings_service_),
173 profile_manager_(TestingBrowserProcess::GetGlobal()) {
176 CloudExternalDataPolicyObserverTest::~CloudExternalDataPolicyObserverTest() {
179 void CloudExternalDataPolicyObserverTest::SetUp() {
180 chromeos::DeviceSettingsTestBase::SetUp();
182 ASSERT_TRUE(profile_manager_.SetUp());
184 device_local_account_policy_service_.reset(
185 new DeviceLocalAccountPolicyService(
186 &device_settings_test_helper_, &device_settings_service_,
187 &cros_settings_, &affiliated_invalidation_service_provider_,
188 base::ThreadTaskRunnerHandle::Get(),
189 base::ThreadTaskRunnerHandle::Get(),
190 base::ThreadTaskRunnerHandle::Get(),
191 base::ThreadTaskRunnerHandle::Get(), nullptr));
192 url_fetcher_factory_.set_remove_fetcher_on_delete(true);
194 EXPECT_CALL(user_policy_provider_, IsInitializationComplete(_))
195 .WillRepeatedly(Return(true));
196 user_policy_provider_.Init();
198 ConstructAvatarPolicy("avatar1.jpg",
199 kAvatar1URL,
200 &avatar_policy_1_data_,
201 &avatar_policy_1_);
202 ConstructAvatarPolicy("avatar2.jpg",
203 kAvatar2URL,
204 &avatar_policy_2_data_,
205 &avatar_policy_2_);
208 void CloudExternalDataPolicyObserverTest::TearDown() {
209 observer_.reset();
210 user_policy_provider_.Shutdown();
211 profile_.reset();
212 if (device_local_account_policy_provider_) {
213 device_local_account_policy_provider_->Shutdown();
214 device_local_account_policy_provider_.reset();
216 device_local_account_policy_service_->Shutdown();
217 device_local_account_policy_service_.reset();
218 chromeos::DeviceSettingsTestBase::TearDown();
222 void CloudExternalDataPolicyObserverTest::OnExternalDataSet(
223 const std::string& policy,
224 const std::string& user_id) {
225 EXPECT_EQ(key::kUserAvatarImage, policy);
226 set_calls_.push_back(user_id);
229 void CloudExternalDataPolicyObserverTest::OnExternalDataCleared(
230 const std::string& policy,
231 const std::string& user_id) {
232 EXPECT_EQ(key::kUserAvatarImage, policy);
233 cleared_calls_.push_back(user_id);
236 void CloudExternalDataPolicyObserverTest::OnExternalDataFetched(
237 const std::string& policy,
238 const std::string& user_id,
239 scoped_ptr<std::string> data) {
240 EXPECT_EQ(key::kUserAvatarImage, policy);
241 fetched_calls_.push_back(make_pair(user_id, std::string()));
242 fetched_calls_.back().second.swap(*data);
245 void CloudExternalDataPolicyObserverTest::CreateObserver() {
246 observer_.reset(new CloudExternalDataPolicyObserver(
247 &cros_settings_,
248 device_local_account_policy_service_.get(),
249 key::kUserAvatarImage,
250 this));
251 observer_->Init();
254 void CloudExternalDataPolicyObserverTest::ClearObservations() {
255 set_calls_.clear();
256 cleared_calls_.clear();
257 fetched_calls_.clear();
260 void CloudExternalDataPolicyObserverTest::SetDeviceLocalAccountAvatarPolicy(
261 const std::string& account_id,
262 const std::string& value) {
263 UserPolicyBuilder builder;
264 builder.policy_data().set_policy_type(
265 dm_protocol::kChromePublicAccountPolicyType);
266 builder.policy_data().set_settings_entity_id(account_id);
267 builder.policy_data().set_username(account_id);
268 if (!value.empty())
269 builder.payload().mutable_useravatarimage()->set_value(value);
270 builder.Build();
271 device_settings_test_helper_.set_device_local_account_policy_blob(
272 account_id,
273 builder.GetBlob());
276 void CloudExternalDataPolicyObserverTest::AddDeviceLocalAccount(
277 const std::string& account_id) {
278 em::DeviceLocalAccountInfoProto* account =
279 device_policy_.payload().mutable_device_local_accounts()->add_account();
280 account->set_account_id(account_id);
281 account->set_type(
282 em::DeviceLocalAccountInfoProto::ACCOUNT_TYPE_PUBLIC_SESSION);
283 device_policy_.Build();
284 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
285 ReloadDeviceSettings();
288 void CloudExternalDataPolicyObserverTest::RemoveDeviceLocalAccount(
289 const std::string& account_id) {
290 em::DeviceLocalAccountsProto* accounts =
291 device_policy_.payload().mutable_device_local_accounts();
292 std::vector<std::string> account_ids;
293 for (int i = 0; i < accounts->account_size(); ++i) {
294 if (accounts->account(i).account_id() != account_id)
295 account_ids.push_back(accounts->account(i).account_id());
297 accounts->clear_account();
298 for (std::vector<std::string>::const_iterator it = account_ids.begin();
299 it != account_ids.end(); ++it) {
300 em::DeviceLocalAccountInfoProto* account = accounts->add_account();
301 account->set_account_id(*it);
302 account->set_type(
303 em::DeviceLocalAccountInfoProto::ACCOUNT_TYPE_PUBLIC_SESSION);
305 device_policy_.Build();
306 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
307 ReloadDeviceSettings();
310 DeviceLocalAccountPolicyBroker*
311 CloudExternalDataPolicyObserverTest::GetBrokerForDeviceLocalAccountUser() {
312 return device_local_account_policy_service_->GetBrokerForUser(
313 device_local_account_user_id_);
316 void CloudExternalDataPolicyObserverTest::RefreshDeviceLocalAccountPolicy(
317 DeviceLocalAccountPolicyBroker* broker) {
318 broker->core()->store()->Load();
319 device_settings_test_helper_.Flush();
322 void CloudExternalDataPolicyObserverTest::LogInAsDeviceLocalAccount(
323 const std::string& user_id) {
324 user_manager_->AddUser(user_id);
326 device_local_account_policy_provider_.reset(
327 new DeviceLocalAccountPolicyProvider(
328 user_id,
329 device_local_account_policy_service_.get(),
330 scoped_ptr<PolicyMap>()));
332 PolicyServiceImpl::Providers providers;
333 providers.push_back(device_local_account_policy_provider_.get());
334 TestingProfile::Builder builder;
335 builder.SetPolicyService(
336 scoped_ptr<PolicyService>(new PolicyServiceImpl(providers)));
337 builder.SetPath(chromeos::ProfileHelper::Get()->GetProfilePathByUserIdHash(
338 chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting(user_id)));
340 profile_ = builder.Build();
341 profile_->set_profile_name(user_id);
343 content::NotificationService::current()->Notify(
344 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
345 content::NotificationService::AllSources(),
346 content::Details<Profile>(profile_.get()));
349 void CloudExternalDataPolicyObserverTest::SetRegularUserAvatarPolicy(
350 const std::string& value) {
351 PolicyMap policy_map;
352 if (!value.empty()) {
353 policy_map.Set(
354 key::kUserAvatarImage,
355 POLICY_LEVEL_MANDATORY,
356 POLICY_SCOPE_USER,
357 new base::StringValue(value),
358 external_data_manager_.CreateExternalDataFetcher(
359 key::kUserAvatarImage).release());
361 user_policy_provider_.UpdateChromePolicy(policy_map);
364 void CloudExternalDataPolicyObserverTest::LogInAsRegularUser() {
365 user_manager_->AddUser(kRegularUserID);
367 PolicyServiceImpl::Providers providers;
368 providers.push_back(&user_policy_provider_);
369 TestingProfile::Builder builder;
370 builder.SetPolicyService(
371 scoped_ptr<PolicyService>(new PolicyServiceImpl(providers)));
372 builder.SetPath(chromeos::ProfileHelper::Get()->GetProfilePathByUserIdHash(
373 chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting(
374 kRegularUserID)));
376 profile_ = builder.Build();
377 profile_->set_profile_name(kRegularUserID);
379 content::NotificationService::current()->Notify(
380 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
381 content::NotificationService::AllSources(),
382 content::Details<Profile>(profile_.get()));
385 // Verifies that when an external data reference is set for a device-local
386 // account, a corresponding notification is emitted and a fetch is started.
387 // Further verifies that when the fetch succeeds, a notification containing the
388 // external data is emitted.
389 TEST_F(CloudExternalDataPolicyObserverTest,
390 ExistingDeviceLocalAccountFetchSuccess) {
391 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
392 AddDeviceLocalAccount(kDeviceLocalAccount);
394 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
395 ASSERT_TRUE(broker);
396 broker->external_data_manager()->Connect(NULL);
397 base::RunLoop().RunUntilIdle();
399 CreateObserver();
401 EXPECT_TRUE(cleared_calls_.empty());
402 EXPECT_TRUE(fetched_calls_.empty());
403 ASSERT_EQ(1u, set_calls_.size());
404 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
405 ClearObservations();
407 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
408 ASSERT_TRUE(fetcher);
409 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
411 fetcher->SetResponseString(avatar_policy_1_data_);
412 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
413 net::OK));
414 fetcher->set_response_code(200);
415 fetcher->delegate()->OnURLFetchComplete(fetcher);
416 base::RunLoop().RunUntilIdle();
418 EXPECT_TRUE(set_calls_.empty());
419 EXPECT_TRUE(cleared_calls_.empty());
420 ASSERT_EQ(1u, fetched_calls_.size());
421 EXPECT_EQ(device_local_account_user_id_, fetched_calls_.front().first);
422 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
423 ClearObservations();
425 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(1));
428 // Verifies that when an external data reference is set for a device-local
429 // account, a corresponding notification is emitted and a fetch is started.
430 // Further verifies that when the fetch fails, no notification is emitted.
431 TEST_F(CloudExternalDataPolicyObserverTest,
432 ExistingDeviceLocalAccountFetchFailure) {
433 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
434 AddDeviceLocalAccount(kDeviceLocalAccount);
436 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
437 ASSERT_TRUE(broker);
438 broker->external_data_manager()->Connect(NULL);
439 base::RunLoop().RunUntilIdle();
441 CreateObserver();
443 EXPECT_TRUE(cleared_calls_.empty());
444 EXPECT_TRUE(fetched_calls_.empty());
445 ASSERT_EQ(1u, set_calls_.size());
446 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
447 ClearObservations();
449 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
450 ASSERT_TRUE(fetcher);
451 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
453 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
454 net::OK));
455 fetcher->set_response_code(400);
456 fetcher->delegate()->OnURLFetchComplete(fetcher);
457 base::RunLoop().RunUntilIdle();
459 EXPECT_TRUE(set_calls_.empty());
460 EXPECT_TRUE(cleared_calls_.empty());
461 EXPECT_TRUE(fetched_calls_.empty());
462 ClearObservations();
464 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(1));
467 // Verifies that when the external data reference for a device-local account is
468 // initially not set, no notifications are emitted. Further verifies that when
469 // the external data reference is then cleared (which is a no-op), again, no
470 // notifications are emitted.
471 TEST_F(CloudExternalDataPolicyObserverTest,
472 ExistingDeviceLocalAccountClearUnset) {
473 AddDeviceLocalAccount(kDeviceLocalAccount);
475 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
476 ASSERT_TRUE(broker);
477 broker->external_data_manager()->Connect(NULL);
478 base::RunLoop().RunUntilIdle();
480 CreateObserver();
482 EXPECT_TRUE(set_calls_.empty());
483 EXPECT_TRUE(cleared_calls_.empty());
484 EXPECT_TRUE(fetched_calls_.empty());
485 ClearObservations();
487 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
489 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, "");
490 RefreshDeviceLocalAccountPolicy(broker);
492 EXPECT_TRUE(set_calls_.empty());
493 EXPECT_TRUE(cleared_calls_.empty());
494 EXPECT_TRUE(fetched_calls_.empty());
495 ClearObservations();
497 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
500 // Verifies that when the external data reference for a device-local account is
501 // initially set, a corresponding notification is emitted and a fetch is
502 // started. Further verifies that when the external data reference is then
503 // cleared, a corresponding notification is emitted and the fetch is canceled.
504 TEST_F(CloudExternalDataPolicyObserverTest,
505 ExistingDeviceLocalAccountClearSet) {
506 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
507 AddDeviceLocalAccount(kDeviceLocalAccount);
509 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
510 ASSERT_TRUE(broker);
511 broker->external_data_manager()->Connect(NULL);
512 base::RunLoop().RunUntilIdle();
514 CreateObserver();
516 EXPECT_TRUE(cleared_calls_.empty());
517 EXPECT_TRUE(fetched_calls_.empty());
518 ASSERT_EQ(1u, set_calls_.size());
519 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
520 ClearObservations();
522 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
523 ASSERT_TRUE(fetcher);
524 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
526 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, "");
527 RefreshDeviceLocalAccountPolicy(broker);
529 EXPECT_TRUE(set_calls_.empty());
530 EXPECT_TRUE(fetched_calls_.empty());
531 ASSERT_EQ(1u, cleared_calls_.size());
532 EXPECT_EQ(device_local_account_user_id_, cleared_calls_.front());
533 ClearObservations();
535 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
538 // Verifies that when the external data reference for a device-local account is
539 // initially not set, no notifications are emitted. Further verifies that when
540 // the external data reference is then set, a corresponding notification is
541 // emitted and a fetch is started. Also verifies that when the fetch eventually
542 // succeeds, a notification containing the external data is emitted.
543 TEST_F(CloudExternalDataPolicyObserverTest,
544 ExistingDeviceLocalAccountSetUnset) {
545 AddDeviceLocalAccount(kDeviceLocalAccount);
547 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
548 ASSERT_TRUE(broker);
549 broker->external_data_manager()->Connect(NULL);
550 base::RunLoop().RunUntilIdle();
552 CreateObserver();
554 EXPECT_TRUE(set_calls_.empty());
555 EXPECT_TRUE(cleared_calls_.empty());
556 EXPECT_TRUE(fetched_calls_.empty());
557 ClearObservations();
559 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
560 RefreshDeviceLocalAccountPolicy(broker);
562 EXPECT_TRUE(cleared_calls_.empty());
563 EXPECT_TRUE(fetched_calls_.empty());
564 ASSERT_EQ(1u, set_calls_.size());
565 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
566 ClearObservations();
568 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
569 ASSERT_TRUE(fetcher);
570 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
572 fetcher->SetResponseString(avatar_policy_1_data_);
573 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
574 net::OK));
575 fetcher->set_response_code(200);
576 fetcher->delegate()->OnURLFetchComplete(fetcher);
577 base::RunLoop().RunUntilIdle();
579 EXPECT_TRUE(set_calls_.empty());
580 EXPECT_TRUE(cleared_calls_.empty());
581 ASSERT_EQ(1u, fetched_calls_.size());
582 EXPECT_EQ(device_local_account_user_id_, fetched_calls_.front().first);
583 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
584 ClearObservations();
586 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(1));
589 // Verifies that when the external data reference for a device-local account is
590 // initially set, a corresponding notification is emitted and a fetch is
591 // started. Further verifies that when the external data reference is then
592 // updated, a corresponding notification is emitted and the fetch is restarted.
593 // Also verifies that when the fetch eventually succeeds, a notification
594 // containing the external data is emitted.
595 TEST_F(CloudExternalDataPolicyObserverTest, ExistingDeviceLocalAccountSetSet) {
596 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
597 AddDeviceLocalAccount(kDeviceLocalAccount);
599 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
600 ASSERT_TRUE(broker);
601 broker->external_data_manager()->Connect(NULL);
602 base::RunLoop().RunUntilIdle();
604 CreateObserver();
606 EXPECT_TRUE(cleared_calls_.empty());
607 EXPECT_TRUE(fetched_calls_.empty());
608 ASSERT_EQ(1u, set_calls_.size());
609 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
610 ClearObservations();
612 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
613 ASSERT_TRUE(fetcher);
614 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
616 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_2_);
617 RefreshDeviceLocalAccountPolicy(broker);
619 EXPECT_TRUE(cleared_calls_.empty());
620 EXPECT_TRUE(fetched_calls_.empty());
621 ASSERT_EQ(1u, set_calls_.size());
622 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
623 ClearObservations();
625 fetcher = url_fetcher_factory_.GetFetcherByID(1);
626 ASSERT_TRUE(fetcher);
627 EXPECT_EQ(GURL(kAvatar2URL), fetcher->GetOriginalURL());
629 fetcher->SetResponseString(avatar_policy_2_data_);
630 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
631 net::OK));
632 fetcher->set_response_code(200);
633 fetcher->delegate()->OnURLFetchComplete(fetcher);
634 base::RunLoop().RunUntilIdle();
636 EXPECT_TRUE(set_calls_.empty());
637 EXPECT_TRUE(cleared_calls_.empty());
638 ASSERT_EQ(1u, fetched_calls_.size());
639 EXPECT_EQ(device_local_account_user_id_, fetched_calls_.front().first);
640 EXPECT_EQ(avatar_policy_2_data_, fetched_calls_.front().second);
641 ClearObservations();
643 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(2));
646 // Verifies that when the external data reference for a device-local account is
647 // initially not set, no notifications are emitted during login into the
648 // account. Further verifies that when the external data reference is then set,
649 // a corresponding notification is emitted only once and a fetch is started.
650 // Also verifies that when the fetch eventually succeeds, a notification
651 // containing the external data is emitted, again, only once.
652 TEST_F(CloudExternalDataPolicyObserverTest,
653 ExistingDeviceLocalAccountSetAfterLogin) {
654 AddDeviceLocalAccount(kDeviceLocalAccount);
656 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
657 ASSERT_TRUE(broker);
658 broker->external_data_manager()->Connect(NULL);
659 base::RunLoop().RunUntilIdle();
661 CreateObserver();
663 LogInAsDeviceLocalAccount(kDeviceLocalAccount);
665 EXPECT_TRUE(set_calls_.empty());
666 EXPECT_TRUE(cleared_calls_.empty());
667 EXPECT_TRUE(fetched_calls_.empty());
668 ClearObservations();
670 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
671 RefreshDeviceLocalAccountPolicy(broker);
673 EXPECT_TRUE(cleared_calls_.empty());
674 EXPECT_TRUE(fetched_calls_.empty());
675 ASSERT_EQ(1u, set_calls_.size());
676 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
677 ClearObservations();
679 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
680 ASSERT_TRUE(fetcher);
681 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
683 fetcher->SetResponseString(avatar_policy_1_data_);
684 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
685 net::OK));
686 fetcher->set_response_code(200);
687 fetcher->delegate()->OnURLFetchComplete(fetcher);
688 base::RunLoop().RunUntilIdle();
690 EXPECT_TRUE(set_calls_.empty());
691 EXPECT_TRUE(cleared_calls_.empty());
692 ASSERT_EQ(1u, fetched_calls_.size());
693 EXPECT_EQ(device_local_account_user_id_, fetched_calls_.front().first);
694 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
695 ClearObservations();
697 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(1));
700 // Verifies that when the external data reference for a device-local account is
701 // initially not set, no notifications are emitted. Further verifies that when
702 // the device-local account is then removed, again, no notifications are sent.
703 TEST_F(CloudExternalDataPolicyObserverTest,
704 ExistingDeviceLocalAccountRemoveAccountUnset) {
705 AddDeviceLocalAccount(kDeviceLocalAccount);
707 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
708 ASSERT_TRUE(broker);
709 broker->external_data_manager()->Connect(NULL);
710 base::RunLoop().RunUntilIdle();
712 CreateObserver();
714 EXPECT_TRUE(set_calls_.empty());
715 EXPECT_TRUE(cleared_calls_.empty());
716 EXPECT_TRUE(fetched_calls_.empty());
717 ClearObservations();
719 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
721 RemoveDeviceLocalAccount(kDeviceLocalAccount);
723 EXPECT_TRUE(set_calls_.empty());
724 EXPECT_TRUE(cleared_calls_.empty());
725 EXPECT_TRUE(fetched_calls_.empty());
726 ClearObservations();
728 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
731 // Verifies that when the external data reference for a device-local account is
732 // initially set, a corresponding notification is emitted and a fetch is
733 // started. Further verifies that when the device-local account is then removed,
734 // a notification indicating that the external data reference has been cleared
735 // is emitted and the fetch is canceled.
736 TEST_F(CloudExternalDataPolicyObserverTest,
737 ExistingDeviceLocalAccountRemoveAccountSet) {
738 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
739 AddDeviceLocalAccount(kDeviceLocalAccount);
741 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
742 ASSERT_TRUE(broker);
743 broker->external_data_manager()->Connect(NULL);
744 base::RunLoop().RunUntilIdle();
746 CreateObserver();
748 EXPECT_TRUE(cleared_calls_.empty());
749 EXPECT_TRUE(fetched_calls_.empty());
750 ASSERT_EQ(1u, set_calls_.size());
751 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
752 ClearObservations();
754 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
755 ASSERT_TRUE(fetcher);
756 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
758 RemoveDeviceLocalAccount(kDeviceLocalAccount);
760 EXPECT_TRUE(set_calls_.empty());
761 EXPECT_TRUE(fetched_calls_.empty());
762 ASSERT_EQ(1u, cleared_calls_.size());
763 EXPECT_EQ(device_local_account_user_id_, cleared_calls_.front());
764 ClearObservations();
766 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
769 // Verifies that when an external data reference is set for a regular user and
770 // the user logs in, a corresponding notification is emitted and a fetch is
771 // started. Further verifies that when the fetch succeeds, a notification
772 // containing the external data is emitted.
773 TEST_F(CloudExternalDataPolicyObserverTest, RegularUserFetchSuccess) {
774 SetRegularUserAvatarPolicy(avatar_policy_1_);
776 CreateObserver();
778 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
779 .Times(1)
780 .WillOnce(SaveArg<1>(&fetch_callback_));
782 LogInAsRegularUser();
784 EXPECT_TRUE(cleared_calls_.empty());
785 EXPECT_TRUE(fetched_calls_.empty());
786 ASSERT_EQ(1u, set_calls_.size());
787 EXPECT_EQ(kRegularUserID, set_calls_.front());
788 ClearObservations();
790 Mock::VerifyAndClear(&external_data_manager_);
791 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
793 fetch_callback_.Run(make_scoped_ptr(new std::string(avatar_policy_1_data_)));
795 EXPECT_TRUE(set_calls_.empty());
796 EXPECT_TRUE(cleared_calls_.empty());
797 ASSERT_EQ(1u, fetched_calls_.size());
798 EXPECT_EQ(kRegularUserID, fetched_calls_.front().first);
799 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
800 ClearObservations();
803 // Verifies that when the external data reference for a regular user is not set
804 // while the user is logging in, no notifications are emitted. Further verifies
805 // that when the external data reference is then cleared (which is a no-op),
806 // again, no notifications are emitted.
807 TEST_F(CloudExternalDataPolicyObserverTest, RegularUserClearUnset) {
808 CreateObserver();
810 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
812 LogInAsRegularUser();
814 EXPECT_TRUE(set_calls_.empty());
815 EXPECT_TRUE(cleared_calls_.empty());
816 EXPECT_TRUE(fetched_calls_.empty());
817 ClearObservations();
819 Mock::VerifyAndClear(&external_data_manager_);
820 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
822 SetRegularUserAvatarPolicy("");
824 EXPECT_TRUE(set_calls_.empty());
825 EXPECT_TRUE(cleared_calls_.empty());
826 EXPECT_TRUE(fetched_calls_.empty());
827 ClearObservations();
830 // Verifies that when the external data reference for a regular user is set
831 // while the user is logging in, a corresponding notification is emitted and a
832 // fetch is started. Further verifies that when the external data reference is
833 // then cleared, a corresponding notification is emitted and no new fetch is
834 // started.
835 TEST_F(CloudExternalDataPolicyObserverTest, RegularUserClearSet) {
836 SetRegularUserAvatarPolicy(avatar_policy_1_);
838 CreateObserver();
840 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
841 .Times(1)
842 .WillOnce(SaveArg<1>(&fetch_callback_));
844 LogInAsRegularUser();
846 EXPECT_TRUE(cleared_calls_.empty());
847 EXPECT_TRUE(fetched_calls_.empty());
848 ASSERT_EQ(1u, set_calls_.size());
849 EXPECT_EQ(kRegularUserID, set_calls_.front());
850 ClearObservations();
852 Mock::VerifyAndClear(&external_data_manager_);
853 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
855 SetRegularUserAvatarPolicy("");
857 EXPECT_TRUE(set_calls_.empty());
858 EXPECT_TRUE(fetched_calls_.empty());
859 ASSERT_EQ(1u, cleared_calls_.size());
860 EXPECT_EQ(kRegularUserID, cleared_calls_.front());
861 ClearObservations();
865 // Verifies that when the external data reference for a regular user is not set
866 // while the user is logging in, no notifications are emitted. Further verifies
867 // that when the external data reference is then set, a corresponding
868 // notification is emitted and a fetch is started. Also verifies that when the
869 // fetch eventually succeeds, a notification containing the external data is
870 // emitted.
871 TEST_F(CloudExternalDataPolicyObserverTest, RegularUserSetUnset) {
872 CreateObserver();
874 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
876 LogInAsRegularUser();
878 EXPECT_TRUE(set_calls_.empty());
879 EXPECT_TRUE(cleared_calls_.empty());
880 EXPECT_TRUE(fetched_calls_.empty());
881 ClearObservations();
883 Mock::VerifyAndClear(&external_data_manager_);
884 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
885 .Times(1)
886 .WillOnce(SaveArg<1>(&fetch_callback_));
888 SetRegularUserAvatarPolicy(avatar_policy_1_);
890 EXPECT_TRUE(cleared_calls_.empty());
891 EXPECT_TRUE(fetched_calls_.empty());
892 ASSERT_EQ(1u, set_calls_.size());
893 EXPECT_EQ(kRegularUserID, set_calls_.front());
894 ClearObservations();
896 Mock::VerifyAndClear(&external_data_manager_);
897 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
899 fetch_callback_.Run(make_scoped_ptr(new std::string(avatar_policy_1_data_)));
901 EXPECT_TRUE(set_calls_.empty());
902 EXPECT_TRUE(cleared_calls_.empty());
903 ASSERT_EQ(1u, fetched_calls_.size());
904 EXPECT_EQ(kRegularUserID, fetched_calls_.front().first);
905 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
906 ClearObservations();
909 // Verifies that when the external data reference for a regular user is set
910 // while the user is logging in, a corresponding notification is emitted and a
911 // fetch is started. Further verifies that when the external data reference is
912 // then updated, a corresponding notification is emitted and the fetch is
913 // restarted. Also verifies that when the fetch eventually succeeds, a
914 // notification containing the external data is emitted.
915 TEST_F(CloudExternalDataPolicyObserverTest, RegularUserSetSet) {
916 SetRegularUserAvatarPolicy(avatar_policy_1_);
918 CreateObserver();
920 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
921 .Times(1)
922 .WillOnce(SaveArg<1>(&fetch_callback_));
924 LogInAsRegularUser();
926 EXPECT_TRUE(cleared_calls_.empty());
927 EXPECT_TRUE(fetched_calls_.empty());
928 ASSERT_EQ(1u, set_calls_.size());
929 EXPECT_EQ(kRegularUserID, set_calls_.front());
930 ClearObservations();
932 Mock::VerifyAndClear(&external_data_manager_);
933 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
934 .Times(1)
935 .WillOnce(SaveArg<1>(&fetch_callback_));
937 SetRegularUserAvatarPolicy(avatar_policy_2_);
939 EXPECT_TRUE(cleared_calls_.empty());
940 EXPECT_TRUE(fetched_calls_.empty());
941 ASSERT_EQ(1u, set_calls_.size());
942 EXPECT_EQ(kRegularUserID, set_calls_.front());
943 ClearObservations();
945 Mock::VerifyAndClear(&external_data_manager_);
946 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
948 fetch_callback_.Run(make_scoped_ptr(new std::string(avatar_policy_2_data_)));
950 EXPECT_TRUE(set_calls_.empty());
951 EXPECT_TRUE(cleared_calls_.empty());
952 ASSERT_EQ(1u, fetched_calls_.size());
953 EXPECT_EQ(kRegularUserID, fetched_calls_.front().first);
954 EXPECT_EQ(avatar_policy_2_data_, fetched_calls_.front().second);
955 ClearObservations();
958 } // namespace policy