Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / cloud_external_data_policy_observer_unittest.cc
blob95982a3d9b588144754bf7ce1e2231c7247f46f5
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 POLICY_SOURCE_CLOUD,
358 new base::StringValue(value),
359 external_data_manager_.CreateExternalDataFetcher(
360 key::kUserAvatarImage).release());
362 user_policy_provider_.UpdateChromePolicy(policy_map);
365 void CloudExternalDataPolicyObserverTest::LogInAsRegularUser() {
366 user_manager_->AddUser(kRegularUserID);
368 PolicyServiceImpl::Providers providers;
369 providers.push_back(&user_policy_provider_);
370 TestingProfile::Builder builder;
371 builder.SetPolicyService(
372 scoped_ptr<PolicyService>(new PolicyServiceImpl(providers)));
373 builder.SetPath(chromeos::ProfileHelper::Get()->GetProfilePathByUserIdHash(
374 chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting(
375 kRegularUserID)));
377 profile_ = builder.Build();
378 profile_->set_profile_name(kRegularUserID);
380 content::NotificationService::current()->Notify(
381 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
382 content::NotificationService::AllSources(),
383 content::Details<Profile>(profile_.get()));
386 // Verifies that when an external data reference is set for a device-local
387 // account, a corresponding notification is emitted and a fetch is started.
388 // Further verifies that when the fetch succeeds, a notification containing the
389 // external data is emitted.
390 TEST_F(CloudExternalDataPolicyObserverTest,
391 ExistingDeviceLocalAccountFetchSuccess) {
392 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
393 AddDeviceLocalAccount(kDeviceLocalAccount);
395 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
396 ASSERT_TRUE(broker);
397 broker->external_data_manager()->Connect(NULL);
398 base::RunLoop().RunUntilIdle();
400 CreateObserver();
402 EXPECT_TRUE(cleared_calls_.empty());
403 EXPECT_TRUE(fetched_calls_.empty());
404 ASSERT_EQ(1u, set_calls_.size());
405 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
406 ClearObservations();
408 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
409 ASSERT_TRUE(fetcher);
410 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
412 fetcher->SetResponseString(avatar_policy_1_data_);
413 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
414 net::OK));
415 fetcher->set_response_code(200);
416 fetcher->delegate()->OnURLFetchComplete(fetcher);
417 base::RunLoop().RunUntilIdle();
419 EXPECT_TRUE(set_calls_.empty());
420 EXPECT_TRUE(cleared_calls_.empty());
421 ASSERT_EQ(1u, fetched_calls_.size());
422 EXPECT_EQ(device_local_account_user_id_, fetched_calls_.front().first);
423 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
424 ClearObservations();
426 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(1));
429 // Verifies that when an external data reference is set for a device-local
430 // account, a corresponding notification is emitted and a fetch is started.
431 // Further verifies that when the fetch fails, no notification is emitted.
432 TEST_F(CloudExternalDataPolicyObserverTest,
433 ExistingDeviceLocalAccountFetchFailure) {
434 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
435 AddDeviceLocalAccount(kDeviceLocalAccount);
437 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
438 ASSERT_TRUE(broker);
439 broker->external_data_manager()->Connect(NULL);
440 base::RunLoop().RunUntilIdle();
442 CreateObserver();
444 EXPECT_TRUE(cleared_calls_.empty());
445 EXPECT_TRUE(fetched_calls_.empty());
446 ASSERT_EQ(1u, set_calls_.size());
447 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
448 ClearObservations();
450 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
451 ASSERT_TRUE(fetcher);
452 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
454 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
455 net::OK));
456 fetcher->set_response_code(400);
457 fetcher->delegate()->OnURLFetchComplete(fetcher);
458 base::RunLoop().RunUntilIdle();
460 EXPECT_TRUE(set_calls_.empty());
461 EXPECT_TRUE(cleared_calls_.empty());
462 EXPECT_TRUE(fetched_calls_.empty());
463 ClearObservations();
465 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(1));
468 // Verifies that when the external data reference for a device-local account is
469 // initially not set, no notifications are emitted. Further verifies that when
470 // the external data reference is then cleared (which is a no-op), again, no
471 // notifications are emitted.
472 TEST_F(CloudExternalDataPolicyObserverTest,
473 ExistingDeviceLocalAccountClearUnset) {
474 AddDeviceLocalAccount(kDeviceLocalAccount);
476 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
477 ASSERT_TRUE(broker);
478 broker->external_data_manager()->Connect(NULL);
479 base::RunLoop().RunUntilIdle();
481 CreateObserver();
483 EXPECT_TRUE(set_calls_.empty());
484 EXPECT_TRUE(cleared_calls_.empty());
485 EXPECT_TRUE(fetched_calls_.empty());
486 ClearObservations();
488 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
490 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, "");
491 RefreshDeviceLocalAccountPolicy(broker);
493 EXPECT_TRUE(set_calls_.empty());
494 EXPECT_TRUE(cleared_calls_.empty());
495 EXPECT_TRUE(fetched_calls_.empty());
496 ClearObservations();
498 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
501 // Verifies that when the external data reference for a device-local account is
502 // initially set, a corresponding notification is emitted and a fetch is
503 // started. Further verifies that when the external data reference is then
504 // cleared, a corresponding notification is emitted and the fetch is canceled.
505 TEST_F(CloudExternalDataPolicyObserverTest,
506 ExistingDeviceLocalAccountClearSet) {
507 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
508 AddDeviceLocalAccount(kDeviceLocalAccount);
510 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
511 ASSERT_TRUE(broker);
512 broker->external_data_manager()->Connect(NULL);
513 base::RunLoop().RunUntilIdle();
515 CreateObserver();
517 EXPECT_TRUE(cleared_calls_.empty());
518 EXPECT_TRUE(fetched_calls_.empty());
519 ASSERT_EQ(1u, set_calls_.size());
520 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
521 ClearObservations();
523 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
524 ASSERT_TRUE(fetcher);
525 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
527 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, "");
528 RefreshDeviceLocalAccountPolicy(broker);
530 EXPECT_TRUE(set_calls_.empty());
531 EXPECT_TRUE(fetched_calls_.empty());
532 ASSERT_EQ(1u, cleared_calls_.size());
533 EXPECT_EQ(device_local_account_user_id_, cleared_calls_.front());
534 ClearObservations();
536 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
539 // Verifies that when the external data reference for a device-local account is
540 // initially not set, no notifications are emitted. Further verifies that when
541 // the external data reference is then set, a corresponding notification is
542 // emitted and a fetch is started. Also verifies that when the fetch eventually
543 // succeeds, a notification containing the external data is emitted.
544 TEST_F(CloudExternalDataPolicyObserverTest,
545 ExistingDeviceLocalAccountSetUnset) {
546 AddDeviceLocalAccount(kDeviceLocalAccount);
548 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
549 ASSERT_TRUE(broker);
550 broker->external_data_manager()->Connect(NULL);
551 base::RunLoop().RunUntilIdle();
553 CreateObserver();
555 EXPECT_TRUE(set_calls_.empty());
556 EXPECT_TRUE(cleared_calls_.empty());
557 EXPECT_TRUE(fetched_calls_.empty());
558 ClearObservations();
560 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
561 RefreshDeviceLocalAccountPolicy(broker);
563 EXPECT_TRUE(cleared_calls_.empty());
564 EXPECT_TRUE(fetched_calls_.empty());
565 ASSERT_EQ(1u, set_calls_.size());
566 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
567 ClearObservations();
569 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
570 ASSERT_TRUE(fetcher);
571 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
573 fetcher->SetResponseString(avatar_policy_1_data_);
574 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
575 net::OK));
576 fetcher->set_response_code(200);
577 fetcher->delegate()->OnURLFetchComplete(fetcher);
578 base::RunLoop().RunUntilIdle();
580 EXPECT_TRUE(set_calls_.empty());
581 EXPECT_TRUE(cleared_calls_.empty());
582 ASSERT_EQ(1u, fetched_calls_.size());
583 EXPECT_EQ(device_local_account_user_id_, fetched_calls_.front().first);
584 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
585 ClearObservations();
587 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(1));
590 // Verifies that when the external data reference for a device-local account is
591 // initially set, a corresponding notification is emitted and a fetch is
592 // started. Further verifies that when the external data reference is then
593 // updated, a corresponding notification is emitted and the fetch is restarted.
594 // Also verifies that when the fetch eventually succeeds, a notification
595 // containing the external data is emitted.
596 TEST_F(CloudExternalDataPolicyObserverTest, ExistingDeviceLocalAccountSetSet) {
597 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
598 AddDeviceLocalAccount(kDeviceLocalAccount);
600 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
601 ASSERT_TRUE(broker);
602 broker->external_data_manager()->Connect(NULL);
603 base::RunLoop().RunUntilIdle();
605 CreateObserver();
607 EXPECT_TRUE(cleared_calls_.empty());
608 EXPECT_TRUE(fetched_calls_.empty());
609 ASSERT_EQ(1u, set_calls_.size());
610 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
611 ClearObservations();
613 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
614 ASSERT_TRUE(fetcher);
615 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
617 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_2_);
618 RefreshDeviceLocalAccountPolicy(broker);
620 EXPECT_TRUE(cleared_calls_.empty());
621 EXPECT_TRUE(fetched_calls_.empty());
622 ASSERT_EQ(1u, set_calls_.size());
623 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
624 ClearObservations();
626 fetcher = url_fetcher_factory_.GetFetcherByID(1);
627 ASSERT_TRUE(fetcher);
628 EXPECT_EQ(GURL(kAvatar2URL), fetcher->GetOriginalURL());
630 fetcher->SetResponseString(avatar_policy_2_data_);
631 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
632 net::OK));
633 fetcher->set_response_code(200);
634 fetcher->delegate()->OnURLFetchComplete(fetcher);
635 base::RunLoop().RunUntilIdle();
637 EXPECT_TRUE(set_calls_.empty());
638 EXPECT_TRUE(cleared_calls_.empty());
639 ASSERT_EQ(1u, fetched_calls_.size());
640 EXPECT_EQ(device_local_account_user_id_, fetched_calls_.front().first);
641 EXPECT_EQ(avatar_policy_2_data_, fetched_calls_.front().second);
642 ClearObservations();
644 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(2));
647 // Verifies that when the external data reference for a device-local account is
648 // initially not set, no notifications are emitted during login into the
649 // account. Further verifies that when the external data reference is then set,
650 // a corresponding notification is emitted only once and a fetch is started.
651 // Also verifies that when the fetch eventually succeeds, a notification
652 // containing the external data is emitted, again, only once.
653 TEST_F(CloudExternalDataPolicyObserverTest,
654 ExistingDeviceLocalAccountSetAfterLogin) {
655 AddDeviceLocalAccount(kDeviceLocalAccount);
657 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
658 ASSERT_TRUE(broker);
659 broker->external_data_manager()->Connect(NULL);
660 base::RunLoop().RunUntilIdle();
662 CreateObserver();
664 LogInAsDeviceLocalAccount(kDeviceLocalAccount);
666 EXPECT_TRUE(set_calls_.empty());
667 EXPECT_TRUE(cleared_calls_.empty());
668 EXPECT_TRUE(fetched_calls_.empty());
669 ClearObservations();
671 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
672 RefreshDeviceLocalAccountPolicy(broker);
674 EXPECT_TRUE(cleared_calls_.empty());
675 EXPECT_TRUE(fetched_calls_.empty());
676 ASSERT_EQ(1u, set_calls_.size());
677 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
678 ClearObservations();
680 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
681 ASSERT_TRUE(fetcher);
682 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
684 fetcher->SetResponseString(avatar_policy_1_data_);
685 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
686 net::OK));
687 fetcher->set_response_code(200);
688 fetcher->delegate()->OnURLFetchComplete(fetcher);
689 base::RunLoop().RunUntilIdle();
691 EXPECT_TRUE(set_calls_.empty());
692 EXPECT_TRUE(cleared_calls_.empty());
693 ASSERT_EQ(1u, fetched_calls_.size());
694 EXPECT_EQ(device_local_account_user_id_, fetched_calls_.front().first);
695 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
696 ClearObservations();
698 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(1));
701 // Verifies that when the external data reference for a device-local account is
702 // initially not set, no notifications are emitted. Further verifies that when
703 // the device-local account is then removed, again, no notifications are sent.
704 TEST_F(CloudExternalDataPolicyObserverTest,
705 ExistingDeviceLocalAccountRemoveAccountUnset) {
706 AddDeviceLocalAccount(kDeviceLocalAccount);
708 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
709 ASSERT_TRUE(broker);
710 broker->external_data_manager()->Connect(NULL);
711 base::RunLoop().RunUntilIdle();
713 CreateObserver();
715 EXPECT_TRUE(set_calls_.empty());
716 EXPECT_TRUE(cleared_calls_.empty());
717 EXPECT_TRUE(fetched_calls_.empty());
718 ClearObservations();
720 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
722 RemoveDeviceLocalAccount(kDeviceLocalAccount);
724 EXPECT_TRUE(set_calls_.empty());
725 EXPECT_TRUE(cleared_calls_.empty());
726 EXPECT_TRUE(fetched_calls_.empty());
727 ClearObservations();
729 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
732 // Verifies that when the external data reference for a device-local account is
733 // initially set, a corresponding notification is emitted and a fetch is
734 // started. Further verifies that when the device-local account is then removed,
735 // a notification indicating that the external data reference has been cleared
736 // is emitted and the fetch is canceled.
737 TEST_F(CloudExternalDataPolicyObserverTest,
738 ExistingDeviceLocalAccountRemoveAccountSet) {
739 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
740 AddDeviceLocalAccount(kDeviceLocalAccount);
742 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
743 ASSERT_TRUE(broker);
744 broker->external_data_manager()->Connect(NULL);
745 base::RunLoop().RunUntilIdle();
747 CreateObserver();
749 EXPECT_TRUE(cleared_calls_.empty());
750 EXPECT_TRUE(fetched_calls_.empty());
751 ASSERT_EQ(1u, set_calls_.size());
752 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
753 ClearObservations();
755 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
756 ASSERT_TRUE(fetcher);
757 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
759 RemoveDeviceLocalAccount(kDeviceLocalAccount);
761 EXPECT_TRUE(set_calls_.empty());
762 EXPECT_TRUE(fetched_calls_.empty());
763 ASSERT_EQ(1u, cleared_calls_.size());
764 EXPECT_EQ(device_local_account_user_id_, cleared_calls_.front());
765 ClearObservations();
767 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
770 // Verifies that when an external data reference is set for a regular user and
771 // the user logs in, a corresponding notification is emitted and a fetch is
772 // started. Further verifies that when the fetch succeeds, a notification
773 // containing the external data is emitted.
774 TEST_F(CloudExternalDataPolicyObserverTest, RegularUserFetchSuccess) {
775 SetRegularUserAvatarPolicy(avatar_policy_1_);
777 CreateObserver();
779 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
780 .Times(1)
781 .WillOnce(SaveArg<1>(&fetch_callback_));
783 LogInAsRegularUser();
785 EXPECT_TRUE(cleared_calls_.empty());
786 EXPECT_TRUE(fetched_calls_.empty());
787 ASSERT_EQ(1u, set_calls_.size());
788 EXPECT_EQ(kRegularUserID, set_calls_.front());
789 ClearObservations();
791 Mock::VerifyAndClear(&external_data_manager_);
792 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
794 fetch_callback_.Run(make_scoped_ptr(new std::string(avatar_policy_1_data_)));
796 EXPECT_TRUE(set_calls_.empty());
797 EXPECT_TRUE(cleared_calls_.empty());
798 ASSERT_EQ(1u, fetched_calls_.size());
799 EXPECT_EQ(kRegularUserID, fetched_calls_.front().first);
800 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
801 ClearObservations();
804 // Verifies that when the external data reference for a regular user is not set
805 // while the user is logging in, no notifications are emitted. Further verifies
806 // that when the external data reference is then cleared (which is a no-op),
807 // again, no notifications are emitted.
808 TEST_F(CloudExternalDataPolicyObserverTest, RegularUserClearUnset) {
809 CreateObserver();
811 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
813 LogInAsRegularUser();
815 EXPECT_TRUE(set_calls_.empty());
816 EXPECT_TRUE(cleared_calls_.empty());
817 EXPECT_TRUE(fetched_calls_.empty());
818 ClearObservations();
820 Mock::VerifyAndClear(&external_data_manager_);
821 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
823 SetRegularUserAvatarPolicy("");
825 EXPECT_TRUE(set_calls_.empty());
826 EXPECT_TRUE(cleared_calls_.empty());
827 EXPECT_TRUE(fetched_calls_.empty());
828 ClearObservations();
831 // Verifies that when the external data reference for a regular user is set
832 // while the user is logging in, a corresponding notification is emitted and a
833 // fetch is started. Further verifies that when the external data reference is
834 // then cleared, a corresponding notification is emitted and no new fetch is
835 // started.
836 TEST_F(CloudExternalDataPolicyObserverTest, RegularUserClearSet) {
837 SetRegularUserAvatarPolicy(avatar_policy_1_);
839 CreateObserver();
841 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
842 .Times(1)
843 .WillOnce(SaveArg<1>(&fetch_callback_));
845 LogInAsRegularUser();
847 EXPECT_TRUE(cleared_calls_.empty());
848 EXPECT_TRUE(fetched_calls_.empty());
849 ASSERT_EQ(1u, set_calls_.size());
850 EXPECT_EQ(kRegularUserID, set_calls_.front());
851 ClearObservations();
853 Mock::VerifyAndClear(&external_data_manager_);
854 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
856 SetRegularUserAvatarPolicy("");
858 EXPECT_TRUE(set_calls_.empty());
859 EXPECT_TRUE(fetched_calls_.empty());
860 ASSERT_EQ(1u, cleared_calls_.size());
861 EXPECT_EQ(kRegularUserID, cleared_calls_.front());
862 ClearObservations();
866 // Verifies that when the external data reference for a regular user is not set
867 // while the user is logging in, no notifications are emitted. Further verifies
868 // that when the external data reference is then set, a corresponding
869 // notification is emitted and a fetch is started. Also verifies that when the
870 // fetch eventually succeeds, a notification containing the external data is
871 // emitted.
872 TEST_F(CloudExternalDataPolicyObserverTest, RegularUserSetUnset) {
873 CreateObserver();
875 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
877 LogInAsRegularUser();
879 EXPECT_TRUE(set_calls_.empty());
880 EXPECT_TRUE(cleared_calls_.empty());
881 EXPECT_TRUE(fetched_calls_.empty());
882 ClearObservations();
884 Mock::VerifyAndClear(&external_data_manager_);
885 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
886 .Times(1)
887 .WillOnce(SaveArg<1>(&fetch_callback_));
889 SetRegularUserAvatarPolicy(avatar_policy_1_);
891 EXPECT_TRUE(cleared_calls_.empty());
892 EXPECT_TRUE(fetched_calls_.empty());
893 ASSERT_EQ(1u, set_calls_.size());
894 EXPECT_EQ(kRegularUserID, set_calls_.front());
895 ClearObservations();
897 Mock::VerifyAndClear(&external_data_manager_);
898 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
900 fetch_callback_.Run(make_scoped_ptr(new std::string(avatar_policy_1_data_)));
902 EXPECT_TRUE(set_calls_.empty());
903 EXPECT_TRUE(cleared_calls_.empty());
904 ASSERT_EQ(1u, fetched_calls_.size());
905 EXPECT_EQ(kRegularUserID, fetched_calls_.front().first);
906 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
907 ClearObservations();
910 // Verifies that when the external data reference for a regular user is set
911 // while the user is logging in, a corresponding notification is emitted and a
912 // fetch is started. Further verifies that when the external data reference is
913 // then updated, a corresponding notification is emitted and the fetch is
914 // restarted. Also verifies that when the fetch eventually succeeds, a
915 // notification containing the external data is emitted.
916 TEST_F(CloudExternalDataPolicyObserverTest, RegularUserSetSet) {
917 SetRegularUserAvatarPolicy(avatar_policy_1_);
919 CreateObserver();
921 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
922 .Times(1)
923 .WillOnce(SaveArg<1>(&fetch_callback_));
925 LogInAsRegularUser();
927 EXPECT_TRUE(cleared_calls_.empty());
928 EXPECT_TRUE(fetched_calls_.empty());
929 ASSERT_EQ(1u, set_calls_.size());
930 EXPECT_EQ(kRegularUserID, set_calls_.front());
931 ClearObservations();
933 Mock::VerifyAndClear(&external_data_manager_);
934 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
935 .Times(1)
936 .WillOnce(SaveArg<1>(&fetch_callback_));
938 SetRegularUserAvatarPolicy(avatar_policy_2_);
940 EXPECT_TRUE(cleared_calls_.empty());
941 EXPECT_TRUE(fetched_calls_.empty());
942 ASSERT_EQ(1u, set_calls_.size());
943 EXPECT_EQ(kRegularUserID, set_calls_.front());
944 ClearObservations();
946 Mock::VerifyAndClear(&external_data_manager_);
947 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
949 fetch_callback_.Run(make_scoped_ptr(new std::string(avatar_policy_2_data_)));
951 EXPECT_TRUE(set_calls_.empty());
952 EXPECT_TRUE(cleared_calls_.empty());
953 ASSERT_EQ(1u, fetched_calls_.size());
954 EXPECT_EQ(kRegularUserID, fetched_calls_.front().first);
955 EXPECT_EQ(avatar_policy_2_data_, fetched_calls_.front().second);
956 ClearObservations();
959 } // namespace policy