1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/policy/cloud_external_data_policy_observer.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/json/json_writer.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/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"
57 namespace em
= enterprise_management
;
59 using ::testing::Mock
;
60 using ::testing::Return
;
61 using ::testing::SaveArg
;
68 const char kDeviceLocalAccount
[] = "device_local_account@localhost";
70 const char kRegularUserID
[] = "user@example.com";
72 const char kAvatar1URL
[] = "http://localhost/avatar1.jpg";
73 const char kAvatar2URL
[] = "http://localhost/avatar2.jpg";
75 void ConstructAvatarPolicy(const std::string
& file_name
,
76 const std::string
& url
,
77 std::string
* policy_data
,
78 std::string
* policy
) {
79 base::FilePath test_data_dir
;
80 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
));
81 ASSERT_TRUE(base::ReadFileToString(
82 test_data_dir
.Append("chromeos").Append(file_name
),
84 base::JSONWriter::Write(
85 *test::ConstructExternalDataReference(url
, *policy_data
), policy
);
90 class CloudExternalDataPolicyObserverTest
91 : public chromeos::DeviceSettingsTestBase
,
92 public CloudExternalDataPolicyObserver::Delegate
{
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_
;
165 DISALLOW_COPY_AND_ASSIGN(CloudExternalDataPolicyObserverTest
);
168 CloudExternalDataPolicyObserverTest::CloudExternalDataPolicyObserverTest()
169 : device_local_account_user_id_(GenerateDeviceLocalAccountUserId(
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",
200 &avatar_policy_1_data_
,
202 ConstructAvatarPolicy("avatar2.jpg",
204 &avatar_policy_2_data_
,
208 void CloudExternalDataPolicyObserverTest::TearDown() {
210 user_policy_provider_
.Shutdown();
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(
248 device_local_account_policy_service_
.get(),
249 key::kUserAvatarImage
,
254 void CloudExternalDataPolicyObserverTest::ClearObservations() {
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
);
269 builder
.payload().mutable_useravatarimage()->set_value(value
);
271 device_settings_test_helper_
.set_device_local_account_policy_blob(
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
);
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
);
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(
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()) {
354 key::kUserAvatarImage
,
355 POLICY_LEVEL_MANDATORY
,
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(
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();
397 broker
->external_data_manager()->Connect(NULL
);
398 base::RunLoop().RunUntilIdle();
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());
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
,
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
);
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();
439 broker
->external_data_manager()->Connect(NULL
);
440 base::RunLoop().RunUntilIdle();
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());
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
,
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());
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();
478 broker
->external_data_manager()->Connect(NULL
);
479 base::RunLoop().RunUntilIdle();
483 EXPECT_TRUE(set_calls_
.empty());
484 EXPECT_TRUE(cleared_calls_
.empty());
485 EXPECT_TRUE(fetched_calls_
.empty());
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());
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();
512 broker
->external_data_manager()->Connect(NULL
);
513 base::RunLoop().RunUntilIdle();
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());
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());
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();
550 broker
->external_data_manager()->Connect(NULL
);
551 base::RunLoop().RunUntilIdle();
555 EXPECT_TRUE(set_calls_
.empty());
556 EXPECT_TRUE(cleared_calls_
.empty());
557 EXPECT_TRUE(fetched_calls_
.empty());
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());
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
,
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
);
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();
602 broker
->external_data_manager()->Connect(NULL
);
603 base::RunLoop().RunUntilIdle();
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());
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());
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
,
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
);
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();
659 broker
->external_data_manager()->Connect(NULL
);
660 base::RunLoop().RunUntilIdle();
664 LogInAsDeviceLocalAccount(kDeviceLocalAccount
);
666 EXPECT_TRUE(set_calls_
.empty());
667 EXPECT_TRUE(cleared_calls_
.empty());
668 EXPECT_TRUE(fetched_calls_
.empty());
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());
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
,
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
);
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();
710 broker
->external_data_manager()->Connect(NULL
);
711 base::RunLoop().RunUntilIdle();
715 EXPECT_TRUE(set_calls_
.empty());
716 EXPECT_TRUE(cleared_calls_
.empty());
717 EXPECT_TRUE(fetched_calls_
.empty());
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());
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();
744 broker
->external_data_manager()->Connect(NULL
);
745 base::RunLoop().RunUntilIdle();
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());
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());
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_
);
779 EXPECT_CALL(external_data_manager_
, Fetch(key::kUserAvatarImage
, _
))
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());
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
);
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
) {
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());
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());
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
836 TEST_F(CloudExternalDataPolicyObserverTest
, RegularUserClearSet
) {
837 SetRegularUserAvatarPolicy(avatar_policy_1_
);
841 EXPECT_CALL(external_data_manager_
, Fetch(key::kUserAvatarImage
, _
))
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());
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());
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
872 TEST_F(CloudExternalDataPolicyObserverTest
, RegularUserSetUnset
) {
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());
884 Mock::VerifyAndClear(&external_data_manager_
);
885 EXPECT_CALL(external_data_manager_
, Fetch(key::kUserAvatarImage
, _
))
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());
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
);
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_
);
921 EXPECT_CALL(external_data_manager_
, Fetch(key::kUserAvatarImage
, _
))
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());
933 Mock::VerifyAndClear(&external_data_manager_
);
934 EXPECT_CALL(external_data_manager_
, Fetch(key::kUserAvatarImage
, _
))
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());
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
);
959 } // namespace policy