1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/policy/cloud_external_data_policy_observer.h"
10 #include "base/file_util.h"
11 #include "base/files/file_path.h"
12 #include "base/json/json_writer.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/message_loop/message_loop_proxy.h"
16 #include "base/path_service.h"
17 #include "base/run_loop.h"
18 #include "base/values.h"
19 #include "chrome/browser/chrome_notification_types.h"
20 #include "chrome/browser/chromeos/login/fake_user_manager.h"
21 #include "chrome/browser/chromeos/policy/cloud_external_data_manager_base_test_util.h"
22 #include "chrome/browser/chromeos/policy/device_local_account.h"
23 #include "chrome/browser/chromeos/policy/device_local_account_external_data_manager.h"
24 #include "chrome/browser/chromeos/policy/device_local_account_policy_provider.h"
25 #include "chrome/browser/chromeos/policy/device_local_account_policy_service.h"
26 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.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_profile.h"
32 #include "components/policy/core/common/cloud/cloud_policy_core.h"
33 #include "components/policy/core/common/cloud/cloud_policy_store.h"
34 #include "components/policy/core/common/cloud/mock_cloud_external_data_manager.h"
35 #include "components/policy/core/common/cloud/policy_builder.h"
36 #include "components/policy/core/common/external_data_fetcher.h"
37 #include "components/policy/core/common/mock_configuration_policy_provider.h"
38 #include "components/policy/core/common/policy_map.h"
39 #include "components/policy/core/common/policy_service.h"
40 #include "components/policy/core/common/policy_service_impl.h"
41 #include "components/policy/core/common/policy_types.h"
42 #include "content/public/browser/notification_details.h"
43 #include "content/public/browser/notification_service.h"
44 #include "content/public/browser/notification_source.h"
45 #include "net/url_request/test_url_fetcher_factory.h"
46 #include "net/url_request/url_fetcher_delegate.h"
47 #include "net/url_request/url_request_context_getter.h"
48 #include "net/url_request/url_request_status.h"
49 #include "policy/policy_constants.h"
50 #include "policy/proto/cloud_policy.pb.h"
51 #include "testing/gmock/include/gmock/gmock.h"
52 #include "testing/gtest/include/gtest/gtest.h"
55 namespace em
= enterprise_management
;
57 using ::testing::Mock
;
58 using ::testing::Return
;
59 using ::testing::SaveArg
;
66 const char kDeviceLocalAccount
[] = "device_local_account@localhost";
68 const char kRegularUserID
[] = "user@example.com";
70 const char kAvatar1URL
[] = "http://localhost/avatar1.jpg";
71 const char kAvatar2URL
[] = "http://localhost/avatar2.jpg";
73 void ConstructAvatarPolicy(const std::string
& file_name
,
74 const std::string
& url
,
75 std::string
* policy_data
,
76 std::string
* policy
) {
77 base::FilePath test_data_dir
;
78 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
));
79 ASSERT_TRUE(base::ReadFileToString(
80 test_data_dir
.Append("chromeos").Append(file_name
),
82 base::JSONWriter::Write(
83 test::ConstructExternalDataReference(url
, *policy_data
).get(),
89 class CloudExternalDataPolicyObserverTest
90 : public chromeos::DeviceSettingsTestBase
,
91 public CloudExternalDataPolicyObserver::Delegate
{
93 typedef std::pair
<std::string
, std::string
> FetchedCall
;
95 CloudExternalDataPolicyObserverTest();
96 virtual ~CloudExternalDataPolicyObserverTest();
98 // chromeos::DeviceSettingsTestBase:
99 virtual void SetUp() OVERRIDE
;
100 virtual void TearDown() OVERRIDE
;
102 // CloudExternalDataPolicyObserver::Delegate:
103 virtual void OnExternalDataSet(const std::string
& policy
,
104 const std::string
& user_id
) OVERRIDE
;
105 virtual void OnExternalDataCleared(const std::string
& policy
,
106 const std::string
& user_id
) OVERRIDE
;
107 virtual void OnExternalDataFetched(const std::string
& policy
,
108 const std::string
& user_id
,
109 scoped_ptr
<std::string
> data
) OVERRIDE
;
111 void CreateObserver();
113 void ClearObservations();
115 void SetDeviceLocalAccountAvatarPolicy(const std::string
& account_id
,
116 const std::string
& value
);
118 void AddDeviceLocalAccount(const std::string
& account_id
);
119 void RemoveDeviceLocalAccount(const std::string
& account_id
);
121 DeviceLocalAccountPolicyBroker
* GetBrokerForDeviceLocalAccountUser();
123 void RefreshDeviceLocalAccountPolicy(DeviceLocalAccountPolicyBroker
* broker
);
125 void LogInAsDeviceLocalAccount(const std::string
& user_id
);
127 void SetRegularUserAvatarPolicy(const std::string
& value
);
129 void LogInAsRegularUser();
131 const std::string device_local_account_user_id_
;
133 std::string avatar_policy_1_data_
;
134 std::string avatar_policy_2_data_
;
135 std::string avatar_policy_1_
;
136 std::string avatar_policy_2_
;
138 chromeos::CrosSettings cros_settings_
;
139 chromeos::FakeUserManager user_manager_
;
140 scoped_ptr
<DeviceLocalAccountPolicyService
>
141 device_local_account_policy_service_
;
142 net::TestURLFetcherFactory url_fetcher_factory_
;
144 scoped_ptr
<DeviceLocalAccountPolicyProvider
>
145 device_local_account_policy_provider_
;
147 MockCloudExternalDataManager external_data_manager_
;
148 MockConfigurationPolicyProvider user_policy_provider_
;
150 scoped_ptr
<TestingProfile
> profile_
;
152 scoped_ptr
<CloudExternalDataPolicyObserver
> observer_
;
154 std::vector
<std::string
> set_calls_
;
155 std::vector
<std::string
> cleared_calls_
;
156 std::vector
<FetchedCall
> fetched_calls_
;
158 ExternalDataFetcher::FetchCallback fetch_callback_
;
161 DISALLOW_COPY_AND_ASSIGN(CloudExternalDataPolicyObserverTest
);
164 CloudExternalDataPolicyObserverTest::CloudExternalDataPolicyObserverTest()
165 : device_local_account_user_id_(GenerateDeviceLocalAccountUserId(
167 DeviceLocalAccount::TYPE_PUBLIC_SESSION
)),
168 cros_settings_(&device_settings_service_
) {
171 CloudExternalDataPolicyObserverTest::~CloudExternalDataPolicyObserverTest() {
174 void CloudExternalDataPolicyObserverTest::SetUp() {
175 chromeos::DeviceSettingsTestBase::SetUp();
176 device_local_account_policy_service_
.reset(
177 new DeviceLocalAccountPolicyService(&device_settings_test_helper_
,
178 &device_settings_service_
,
180 loop_
.message_loop_proxy(),
181 loop_
.message_loop_proxy(),
182 loop_
.message_loop_proxy(),
183 loop_
.message_loop_proxy(),
185 url_fetcher_factory_
.set_remove_fetcher_on_delete(true);
187 EXPECT_CALL(user_policy_provider_
, IsInitializationComplete(_
))
188 .WillRepeatedly(Return(true));
189 user_policy_provider_
.Init();
191 ConstructAvatarPolicy("avatar1.jpg",
193 &avatar_policy_1_data_
,
195 ConstructAvatarPolicy("avatar2.jpg",
197 &avatar_policy_2_data_
,
201 void CloudExternalDataPolicyObserverTest::TearDown() {
203 user_policy_provider_
.Shutdown();
205 if (device_local_account_policy_provider_
) {
206 device_local_account_policy_provider_
->Shutdown();
207 device_local_account_policy_provider_
.reset();
209 device_local_account_policy_service_
->Shutdown();
210 device_local_account_policy_service_
.reset();
211 chromeos::DeviceSettingsTestBase::TearDown();
215 void CloudExternalDataPolicyObserverTest::OnExternalDataSet(
216 const std::string
& policy
,
217 const std::string
& user_id
) {
218 EXPECT_EQ(key::kUserAvatarImage
, policy
);
219 set_calls_
.push_back(user_id
);
222 void CloudExternalDataPolicyObserverTest::OnExternalDataCleared(
223 const std::string
& policy
,
224 const std::string
& user_id
) {
225 EXPECT_EQ(key::kUserAvatarImage
, policy
);
226 cleared_calls_
.push_back(user_id
);
229 void CloudExternalDataPolicyObserverTest::OnExternalDataFetched(
230 const std::string
& policy
,
231 const std::string
& user_id
,
232 scoped_ptr
<std::string
> data
) {
233 EXPECT_EQ(key::kUserAvatarImage
, policy
);
234 fetched_calls_
.push_back(make_pair(user_id
, std::string()));
235 fetched_calls_
.back().second
.swap(*data
);
238 void CloudExternalDataPolicyObserverTest::CreateObserver() {
239 observer_
.reset(new CloudExternalDataPolicyObserver(
242 device_local_account_policy_service_
.get(),
243 key::kUserAvatarImage
,
248 void CloudExternalDataPolicyObserverTest::ClearObservations() {
250 cleared_calls_
.clear();
251 fetched_calls_
.clear();
254 void CloudExternalDataPolicyObserverTest::SetDeviceLocalAccountAvatarPolicy(
255 const std::string
& account_id
,
256 const std::string
& value
) {
257 UserPolicyBuilder builder
;
258 builder
.policy_data().set_policy_type(
259 dm_protocol::kChromePublicAccountPolicyType
);
260 builder
.policy_data().set_settings_entity_id(account_id
);
261 builder
.policy_data().set_username(account_id
);
263 builder
.payload().mutable_useravatarimage()->set_value(value
);
265 device_settings_test_helper_
.set_device_local_account_policy_blob(
270 void CloudExternalDataPolicyObserverTest::AddDeviceLocalAccount(
271 const std::string
& account_id
) {
272 em::DeviceLocalAccountInfoProto
* account
=
273 device_policy_
.payload().mutable_device_local_accounts()->add_account();
274 account
->set_account_id(account_id
);
276 em::DeviceLocalAccountInfoProto::ACCOUNT_TYPE_PUBLIC_SESSION
);
277 device_policy_
.Build();
278 device_settings_test_helper_
.set_policy_blob(device_policy_
.GetBlob());
279 ReloadDeviceSettings();
282 void CloudExternalDataPolicyObserverTest::RemoveDeviceLocalAccount(
283 const std::string
& account_id
) {
284 em::DeviceLocalAccountsProto
* accounts
=
285 device_policy_
.payload().mutable_device_local_accounts();
286 std::vector
<std::string
> account_ids
;
287 for (int i
= 0; i
< accounts
->account_size(); ++i
) {
288 if (accounts
->account(i
).account_id() != account_id
)
289 account_ids
.push_back(accounts
->account(i
).account_id());
291 accounts
->clear_account();
292 for (std::vector
<std::string
>::const_iterator it
= account_ids
.begin();
293 it
!= account_ids
.end(); ++it
) {
294 em::DeviceLocalAccountInfoProto
* account
= accounts
->add_account();
295 account
->set_account_id(*it
);
297 em::DeviceLocalAccountInfoProto::ACCOUNT_TYPE_PUBLIC_SESSION
);
299 device_policy_
.Build();
300 device_settings_test_helper_
.set_policy_blob(device_policy_
.GetBlob());
301 ReloadDeviceSettings();
304 DeviceLocalAccountPolicyBroker
*
305 CloudExternalDataPolicyObserverTest::GetBrokerForDeviceLocalAccountUser() {
306 return device_local_account_policy_service_
->GetBrokerForUser(
307 device_local_account_user_id_
);
310 void CloudExternalDataPolicyObserverTest::RefreshDeviceLocalAccountPolicy(
311 DeviceLocalAccountPolicyBroker
* broker
) {
312 broker
->core()->store()->Load();
313 device_settings_test_helper_
.Flush();
316 void CloudExternalDataPolicyObserverTest::LogInAsDeviceLocalAccount(
317 const std::string
& user_id
) {
318 device_local_account_policy_provider_
.reset(
319 new DeviceLocalAccountPolicyProvider(
321 device_local_account_policy_service_
.get()));
323 PolicyServiceImpl::Providers providers
;
324 providers
.push_back(device_local_account_policy_provider_
.get());
325 TestingProfile::Builder builder
;
326 builder
.SetPolicyService(
327 scoped_ptr
<PolicyService
>(new PolicyServiceImpl(providers
)));
329 profile_
= builder
.Build();
330 profile_
->set_profile_name(user_id
);
332 user_manager_
.AddUser(user_id
);
333 content::NotificationService::current()->Notify(
334 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED
,
335 content::NotificationService::AllSources(),
336 content::Details
<Profile
>(profile_
.get()));
339 void CloudExternalDataPolicyObserverTest::SetRegularUserAvatarPolicy(
340 const std::string
& value
) {
341 PolicyMap policy_map
;
342 if (!value
.empty()) {
344 key::kUserAvatarImage
,
345 POLICY_LEVEL_MANDATORY
,
347 new base::StringValue(value
),
348 external_data_manager_
.CreateExternalDataFetcher(
349 key::kUserAvatarImage
).release());
351 user_policy_provider_
.UpdateChromePolicy(policy_map
);
354 void CloudExternalDataPolicyObserverTest::LogInAsRegularUser() {
355 PolicyServiceImpl::Providers providers
;
356 providers
.push_back(&user_policy_provider_
);
357 TestingProfile::Builder builder
;
358 builder
.SetPolicyService(
359 scoped_ptr
<PolicyService
>(new PolicyServiceImpl(providers
)));
361 profile_
= builder
.Build();
362 profile_
->set_profile_name(kRegularUserID
);
364 user_manager_
.AddUser(kRegularUserID
);
365 content::NotificationService::current()->Notify(
366 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED
,
367 content::NotificationService::AllSources(),
368 content::Details
<Profile
>(profile_
.get()));
371 // Verifies that when an external data reference is set for a device-local
372 // account, a corresponding notification is emitted and a fetch is started.
373 // Further verifies that when the fetch succeeds, a notification containing the
374 // external data is emitted.
375 TEST_F(CloudExternalDataPolicyObserverTest
,
376 ExistingDeviceLocalAccountFetchSuccess
) {
377 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount
, avatar_policy_1_
);
378 AddDeviceLocalAccount(kDeviceLocalAccount
);
380 DeviceLocalAccountPolicyBroker
* broker
= GetBrokerForDeviceLocalAccountUser();
382 broker
->external_data_manager()->Connect(NULL
);
383 base::RunLoop().RunUntilIdle();
387 EXPECT_TRUE(cleared_calls_
.empty());
388 EXPECT_TRUE(fetched_calls_
.empty());
389 ASSERT_EQ(1u, set_calls_
.size());
390 EXPECT_EQ(device_local_account_user_id_
, set_calls_
.front());
393 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
394 ASSERT_TRUE(fetcher
);
395 EXPECT_EQ(GURL(kAvatar1URL
), fetcher
->GetOriginalURL());
397 fetcher
->SetResponseString(avatar_policy_1_data_
);
398 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
400 fetcher
->set_response_code(200);
401 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
402 base::RunLoop().RunUntilIdle();
404 EXPECT_TRUE(set_calls_
.empty());
405 EXPECT_TRUE(cleared_calls_
.empty());
406 ASSERT_EQ(1u, fetched_calls_
.size());
407 EXPECT_EQ(device_local_account_user_id_
, fetched_calls_
.front().first
);
408 EXPECT_EQ(avatar_policy_1_data_
, fetched_calls_
.front().second
);
411 EXPECT_FALSE(url_fetcher_factory_
.GetFetcherByID(1));
414 // Verifies that when an external data reference is set for a device-local
415 // account, a corresponding notification is emitted and a fetch is started.
416 // Further verifies that when the fetch fails, no notification is emitted.
417 TEST_F(CloudExternalDataPolicyObserverTest
,
418 ExistingDeviceLocalAccountFetchFailure
) {
419 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount
, avatar_policy_1_
);
420 AddDeviceLocalAccount(kDeviceLocalAccount
);
422 DeviceLocalAccountPolicyBroker
* broker
= GetBrokerForDeviceLocalAccountUser();
424 broker
->external_data_manager()->Connect(NULL
);
425 base::RunLoop().RunUntilIdle();
429 EXPECT_TRUE(cleared_calls_
.empty());
430 EXPECT_TRUE(fetched_calls_
.empty());
431 ASSERT_EQ(1u, set_calls_
.size());
432 EXPECT_EQ(device_local_account_user_id_
, set_calls_
.front());
435 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
436 ASSERT_TRUE(fetcher
);
437 EXPECT_EQ(GURL(kAvatar1URL
), fetcher
->GetOriginalURL());
439 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
441 fetcher
->set_response_code(400);
442 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
443 base::RunLoop().RunUntilIdle();
445 EXPECT_TRUE(set_calls_
.empty());
446 EXPECT_TRUE(cleared_calls_
.empty());
447 EXPECT_TRUE(fetched_calls_
.empty());
450 EXPECT_FALSE(url_fetcher_factory_
.GetFetcherByID(1));
453 // Verifies that when the external data reference for a device-local account is
454 // initially not set, no notifications are emitted. Further verifies that when
455 // the external data reference is then cleared (which is a no-op), again, no
456 // notifications are emitted.
457 TEST_F(CloudExternalDataPolicyObserverTest
,
458 ExistingDeviceLocalAccountClearUnset
) {
459 AddDeviceLocalAccount(kDeviceLocalAccount
);
461 DeviceLocalAccountPolicyBroker
* broker
= GetBrokerForDeviceLocalAccountUser();
463 broker
->external_data_manager()->Connect(NULL
);
464 base::RunLoop().RunUntilIdle();
468 EXPECT_TRUE(set_calls_
.empty());
469 EXPECT_TRUE(cleared_calls_
.empty());
470 EXPECT_TRUE(fetched_calls_
.empty());
473 EXPECT_FALSE(url_fetcher_factory_
.GetFetcherByID(0));
475 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount
, "");
476 RefreshDeviceLocalAccountPolicy(broker
);
478 EXPECT_TRUE(set_calls_
.empty());
479 EXPECT_TRUE(cleared_calls_
.empty());
480 EXPECT_TRUE(fetched_calls_
.empty());
483 EXPECT_FALSE(url_fetcher_factory_
.GetFetcherByID(0));
486 // Verifies that when the external data reference for a device-local account is
487 // initially set, a corresponding notification is emitted and a fetch is
488 // started. Further verifies that when the external data reference is then
489 // cleared, a corresponding notification is emitted and the fetch is canceled.
490 TEST_F(CloudExternalDataPolicyObserverTest
,
491 ExistingDeviceLocalAccountClearSet
) {
492 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount
, avatar_policy_1_
);
493 AddDeviceLocalAccount(kDeviceLocalAccount
);
495 DeviceLocalAccountPolicyBroker
* broker
= GetBrokerForDeviceLocalAccountUser();
497 broker
->external_data_manager()->Connect(NULL
);
498 base::RunLoop().RunUntilIdle();
502 EXPECT_TRUE(cleared_calls_
.empty());
503 EXPECT_TRUE(fetched_calls_
.empty());
504 ASSERT_EQ(1u, set_calls_
.size());
505 EXPECT_EQ(device_local_account_user_id_
, set_calls_
.front());
508 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
509 ASSERT_TRUE(fetcher
);
510 EXPECT_EQ(GURL(kAvatar1URL
), fetcher
->GetOriginalURL());
512 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount
, "");
513 RefreshDeviceLocalAccountPolicy(broker
);
515 EXPECT_TRUE(set_calls_
.empty());
516 EXPECT_TRUE(fetched_calls_
.empty());
517 ASSERT_EQ(1u, cleared_calls_
.size());
518 EXPECT_EQ(device_local_account_user_id_
, cleared_calls_
.front());
521 EXPECT_FALSE(url_fetcher_factory_
.GetFetcherByID(0));
524 // Verifies that when the external data reference for a device-local account is
525 // initially not set, no notifications are emitted. Further verifies that when
526 // the external data reference is then set, a corresponding notification is
527 // emitted and a fetch is started. Also verifies that when the fetch eventually
528 // succeeds, a notification containing the external data is emitted.
529 TEST_F(CloudExternalDataPolicyObserverTest
,
530 ExistingDeviceLocalAccountSetUnset
) {
531 AddDeviceLocalAccount(kDeviceLocalAccount
);
533 DeviceLocalAccountPolicyBroker
* broker
= GetBrokerForDeviceLocalAccountUser();
535 broker
->external_data_manager()->Connect(NULL
);
536 base::RunLoop().RunUntilIdle();
540 EXPECT_TRUE(set_calls_
.empty());
541 EXPECT_TRUE(cleared_calls_
.empty());
542 EXPECT_TRUE(fetched_calls_
.empty());
545 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount
, avatar_policy_1_
);
546 RefreshDeviceLocalAccountPolicy(broker
);
548 EXPECT_TRUE(cleared_calls_
.empty());
549 EXPECT_TRUE(fetched_calls_
.empty());
550 ASSERT_EQ(1u, set_calls_
.size());
551 EXPECT_EQ(device_local_account_user_id_
, set_calls_
.front());
554 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
555 ASSERT_TRUE(fetcher
);
556 EXPECT_EQ(GURL(kAvatar1URL
), fetcher
->GetOriginalURL());
558 fetcher
->SetResponseString(avatar_policy_1_data_
);
559 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
561 fetcher
->set_response_code(200);
562 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
563 base::RunLoop().RunUntilIdle();
565 EXPECT_TRUE(set_calls_
.empty());
566 EXPECT_TRUE(cleared_calls_
.empty());
567 ASSERT_EQ(1u, fetched_calls_
.size());
568 EXPECT_EQ(device_local_account_user_id_
, fetched_calls_
.front().first
);
569 EXPECT_EQ(avatar_policy_1_data_
, fetched_calls_
.front().second
);
572 EXPECT_FALSE(url_fetcher_factory_
.GetFetcherByID(1));
575 // Verifies that when the external data reference for a device-local account is
576 // initially set, a corresponding notification is emitted and a fetch is
577 // started. Further verifies that when the external data reference is then
578 // updated, a corresponding notification is emitted and the fetch is restarted.
579 // Also verifies that when the fetch eventually succeeds, a notification
580 // containing the external data is emitted.
581 TEST_F(CloudExternalDataPolicyObserverTest
, ExistingDeviceLocalAccountSetSet
) {
582 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount
, avatar_policy_1_
);
583 AddDeviceLocalAccount(kDeviceLocalAccount
);
585 DeviceLocalAccountPolicyBroker
* broker
= GetBrokerForDeviceLocalAccountUser();
587 broker
->external_data_manager()->Connect(NULL
);
588 base::RunLoop().RunUntilIdle();
592 EXPECT_TRUE(cleared_calls_
.empty());
593 EXPECT_TRUE(fetched_calls_
.empty());
594 ASSERT_EQ(1u, set_calls_
.size());
595 EXPECT_EQ(device_local_account_user_id_
, set_calls_
.front());
598 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
599 ASSERT_TRUE(fetcher
);
600 EXPECT_EQ(GURL(kAvatar1URL
), fetcher
->GetOriginalURL());
602 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount
, avatar_policy_2_
);
603 RefreshDeviceLocalAccountPolicy(broker
);
605 EXPECT_TRUE(cleared_calls_
.empty());
606 EXPECT_TRUE(fetched_calls_
.empty());
607 ASSERT_EQ(1u, set_calls_
.size());
608 EXPECT_EQ(device_local_account_user_id_
, set_calls_
.front());
611 fetcher
= url_fetcher_factory_
.GetFetcherByID(1);
612 ASSERT_TRUE(fetcher
);
613 EXPECT_EQ(GURL(kAvatar2URL
), fetcher
->GetOriginalURL());
615 fetcher
->SetResponseString(avatar_policy_2_data_
);
616 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
618 fetcher
->set_response_code(200);
619 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
620 base::RunLoop().RunUntilIdle();
622 EXPECT_TRUE(set_calls_
.empty());
623 EXPECT_TRUE(cleared_calls_
.empty());
624 ASSERT_EQ(1u, fetched_calls_
.size());
625 EXPECT_EQ(device_local_account_user_id_
, fetched_calls_
.front().first
);
626 EXPECT_EQ(avatar_policy_2_data_
, fetched_calls_
.front().second
);
629 EXPECT_FALSE(url_fetcher_factory_
.GetFetcherByID(2));
632 // Verifies that when the external data reference for a device-local account is
633 // initially not set, no notifications are emitted during login into the
634 // account. Further verifies that when the external data reference is then set,
635 // a corresponding notification is emitted only once and a fetch is started.
636 // Also verifies that when the fetch eventually succeeds, a notification
637 // containing the external data is emitted, again, only once.
638 TEST_F(CloudExternalDataPolicyObserverTest
,
639 ExistingDeviceLocalAccountSetAfterLogin
) {
640 AddDeviceLocalAccount(kDeviceLocalAccount
);
642 DeviceLocalAccountPolicyBroker
* broker
= GetBrokerForDeviceLocalAccountUser();
644 broker
->external_data_manager()->Connect(NULL
);
645 base::RunLoop().RunUntilIdle();
649 LogInAsDeviceLocalAccount(kDeviceLocalAccount
);
651 EXPECT_TRUE(set_calls_
.empty());
652 EXPECT_TRUE(cleared_calls_
.empty());
653 EXPECT_TRUE(fetched_calls_
.empty());
656 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount
, avatar_policy_1_
);
657 RefreshDeviceLocalAccountPolicy(broker
);
659 EXPECT_TRUE(cleared_calls_
.empty());
660 EXPECT_TRUE(fetched_calls_
.empty());
661 ASSERT_EQ(1u, set_calls_
.size());
662 EXPECT_EQ(device_local_account_user_id_
, set_calls_
.front());
665 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
666 ASSERT_TRUE(fetcher
);
667 EXPECT_EQ(GURL(kAvatar1URL
), fetcher
->GetOriginalURL());
669 fetcher
->SetResponseString(avatar_policy_1_data_
);
670 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
672 fetcher
->set_response_code(200);
673 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
674 base::RunLoop().RunUntilIdle();
676 EXPECT_TRUE(set_calls_
.empty());
677 EXPECT_TRUE(cleared_calls_
.empty());
678 ASSERT_EQ(1u, fetched_calls_
.size());
679 EXPECT_EQ(device_local_account_user_id_
, fetched_calls_
.front().first
);
680 EXPECT_EQ(avatar_policy_1_data_
, fetched_calls_
.front().second
);
683 EXPECT_FALSE(url_fetcher_factory_
.GetFetcherByID(1));
686 // Verifies that when the external data reference for a device-local account is
687 // initially not set, no notifications are emitted. Further verifies that when
688 // the device-local account is then removed, again, no notifications are sent.
689 TEST_F(CloudExternalDataPolicyObserverTest
,
690 ExistingDeviceLocalAccountRemoveAccountUnset
) {
691 AddDeviceLocalAccount(kDeviceLocalAccount
);
693 DeviceLocalAccountPolicyBroker
* broker
= GetBrokerForDeviceLocalAccountUser();
695 broker
->external_data_manager()->Connect(NULL
);
696 base::RunLoop().RunUntilIdle();
700 EXPECT_TRUE(set_calls_
.empty());
701 EXPECT_TRUE(cleared_calls_
.empty());
702 EXPECT_TRUE(fetched_calls_
.empty());
705 EXPECT_FALSE(url_fetcher_factory_
.GetFetcherByID(0));
707 RemoveDeviceLocalAccount(kDeviceLocalAccount
);
709 EXPECT_TRUE(set_calls_
.empty());
710 EXPECT_TRUE(cleared_calls_
.empty());
711 EXPECT_TRUE(fetched_calls_
.empty());
714 EXPECT_FALSE(url_fetcher_factory_
.GetFetcherByID(0));
717 // Verifies that when the external data reference for a device-local account is
718 // initially set, a corresponding notification is emitted and a fetch is
719 // started. Further verifies that when the device-local account is then removed,
720 // a notification indicating that the external data reference has been cleared
721 // is emitted and the fetch is canceled.
722 TEST_F(CloudExternalDataPolicyObserverTest
,
723 ExistingDeviceLocalAccountRemoveAccountSet
) {
724 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount
, avatar_policy_1_
);
725 AddDeviceLocalAccount(kDeviceLocalAccount
);
727 DeviceLocalAccountPolicyBroker
* broker
= GetBrokerForDeviceLocalAccountUser();
729 broker
->external_data_manager()->Connect(NULL
);
730 base::RunLoop().RunUntilIdle();
734 EXPECT_TRUE(cleared_calls_
.empty());
735 EXPECT_TRUE(fetched_calls_
.empty());
736 ASSERT_EQ(1u, set_calls_
.size());
737 EXPECT_EQ(device_local_account_user_id_
, set_calls_
.front());
740 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
741 ASSERT_TRUE(fetcher
);
742 EXPECT_EQ(GURL(kAvatar1URL
), fetcher
->GetOriginalURL());
744 RemoveDeviceLocalAccount(kDeviceLocalAccount
);
746 EXPECT_TRUE(set_calls_
.empty());
747 EXPECT_TRUE(fetched_calls_
.empty());
748 ASSERT_EQ(1u, cleared_calls_
.size());
749 EXPECT_EQ(device_local_account_user_id_
, cleared_calls_
.front());
752 EXPECT_FALSE(url_fetcher_factory_
.GetFetcherByID(0));
755 // Verifies that when an external data reference is set for a regular user and
756 // the user logs in, a corresponding notification is emitted and a fetch is
757 // started. Further verifies that when the fetch succeeds, a notification
758 // containing the external data is emitted.
759 TEST_F(CloudExternalDataPolicyObserverTest
, RegularUserFetchSuccess
) {
760 SetRegularUserAvatarPolicy(avatar_policy_1_
);
764 EXPECT_CALL(external_data_manager_
, Fetch(key::kUserAvatarImage
, _
))
766 .WillOnce(SaveArg
<1>(&fetch_callback_
));
768 LogInAsRegularUser();
770 EXPECT_TRUE(cleared_calls_
.empty());
771 EXPECT_TRUE(fetched_calls_
.empty());
772 ASSERT_EQ(1u, set_calls_
.size());
773 EXPECT_EQ(kRegularUserID
, set_calls_
.front());
776 Mock::VerifyAndClear(&external_data_manager_
);
777 EXPECT_CALL(external_data_manager_
, Fetch(key::kUserAvatarImage
, _
)).Times(0);
779 fetch_callback_
.Run(make_scoped_ptr(new std::string(avatar_policy_1_data_
)));
781 EXPECT_TRUE(set_calls_
.empty());
782 EXPECT_TRUE(cleared_calls_
.empty());
783 ASSERT_EQ(1u, fetched_calls_
.size());
784 EXPECT_EQ(kRegularUserID
, fetched_calls_
.front().first
);
785 EXPECT_EQ(avatar_policy_1_data_
, fetched_calls_
.front().second
);
789 // Verifies that when the external data reference for a regular user is not set
790 // while the user is logging in, no notifications are emitted. Further verifies
791 // that when the external data reference is then cleared (which is a no-op),
792 // again, no notifications are emitted.
793 TEST_F(CloudExternalDataPolicyObserverTest
, RegularUserClearUnset
) {
796 EXPECT_CALL(external_data_manager_
, Fetch(key::kUserAvatarImage
, _
)).Times(0);
798 LogInAsRegularUser();
800 EXPECT_TRUE(set_calls_
.empty());
801 EXPECT_TRUE(cleared_calls_
.empty());
802 EXPECT_TRUE(fetched_calls_
.empty());
805 Mock::VerifyAndClear(&external_data_manager_
);
806 EXPECT_CALL(external_data_manager_
, Fetch(key::kUserAvatarImage
, _
)).Times(0);
808 SetRegularUserAvatarPolicy("");
810 EXPECT_TRUE(set_calls_
.empty());
811 EXPECT_TRUE(cleared_calls_
.empty());
812 EXPECT_TRUE(fetched_calls_
.empty());
816 // Verifies that when the external data reference for a regular user is set
817 // while the user is logging in, a corresponding notification is emitted and a
818 // fetch is started. Further verifies that when the external data reference is
819 // then cleared, a corresponding notification is emitted and no new fetch is
821 TEST_F(CloudExternalDataPolicyObserverTest
, RegularUserClearSet
) {
822 SetRegularUserAvatarPolicy(avatar_policy_1_
);
826 EXPECT_CALL(external_data_manager_
, Fetch(key::kUserAvatarImage
, _
))
828 .WillOnce(SaveArg
<1>(&fetch_callback_
));
830 LogInAsRegularUser();
832 EXPECT_TRUE(cleared_calls_
.empty());
833 EXPECT_TRUE(fetched_calls_
.empty());
834 ASSERT_EQ(1u, set_calls_
.size());
835 EXPECT_EQ(kRegularUserID
, set_calls_
.front());
838 Mock::VerifyAndClear(&external_data_manager_
);
839 EXPECT_CALL(external_data_manager_
, Fetch(key::kUserAvatarImage
, _
)).Times(0);
841 SetRegularUserAvatarPolicy("");
843 EXPECT_TRUE(set_calls_
.empty());
844 EXPECT_TRUE(fetched_calls_
.empty());
845 ASSERT_EQ(1u, cleared_calls_
.size());
846 EXPECT_EQ(kRegularUserID
, cleared_calls_
.front());
851 // Verifies that when the external data reference for a regular user is not set
852 // while the user is logging in, no notifications are emitted. Further verifies
853 // that when the external data reference is then set, a corresponding
854 // notification is emitted and a fetch is started. Also verifies that when the
855 // fetch eventually succeeds, a notification containing the external data is
857 TEST_F(CloudExternalDataPolicyObserverTest
, RegularUserSetUnset
) {
860 EXPECT_CALL(external_data_manager_
, Fetch(key::kUserAvatarImage
, _
)).Times(0);
862 LogInAsRegularUser();
864 EXPECT_TRUE(set_calls_
.empty());
865 EXPECT_TRUE(cleared_calls_
.empty());
866 EXPECT_TRUE(fetched_calls_
.empty());
869 Mock::VerifyAndClear(&external_data_manager_
);
870 EXPECT_CALL(external_data_manager_
, Fetch(key::kUserAvatarImage
, _
))
872 .WillOnce(SaveArg
<1>(&fetch_callback_
));
874 SetRegularUserAvatarPolicy(avatar_policy_1_
);
876 EXPECT_TRUE(cleared_calls_
.empty());
877 EXPECT_TRUE(fetched_calls_
.empty());
878 ASSERT_EQ(1u, set_calls_
.size());
879 EXPECT_EQ(kRegularUserID
, set_calls_
.front());
882 Mock::VerifyAndClear(&external_data_manager_
);
883 EXPECT_CALL(external_data_manager_
, Fetch(key::kUserAvatarImage
, _
)).Times(0);
885 fetch_callback_
.Run(make_scoped_ptr(new std::string(avatar_policy_1_data_
)));
887 EXPECT_TRUE(set_calls_
.empty());
888 EXPECT_TRUE(cleared_calls_
.empty());
889 ASSERT_EQ(1u, fetched_calls_
.size());
890 EXPECT_EQ(kRegularUserID
, fetched_calls_
.front().first
);
891 EXPECT_EQ(avatar_policy_1_data_
, fetched_calls_
.front().second
);
895 // Verifies that when the external data reference for a regular user is set
896 // while the user is logging in, a corresponding notification is emitted and a
897 // fetch is started. Further verifies that when the external data reference is
898 // then updated, a corresponding notification is emitted and the fetch is
899 // restarted. Also verifies that when the fetch eventually succeeds, a
900 // notification containing the external data is emitted.
901 TEST_F(CloudExternalDataPolicyObserverTest
, RegularUserSetSet
) {
902 SetRegularUserAvatarPolicy(avatar_policy_1_
);
906 EXPECT_CALL(external_data_manager_
, Fetch(key::kUserAvatarImage
, _
))
908 .WillOnce(SaveArg
<1>(&fetch_callback_
));
910 LogInAsRegularUser();
912 EXPECT_TRUE(cleared_calls_
.empty());
913 EXPECT_TRUE(fetched_calls_
.empty());
914 ASSERT_EQ(1u, set_calls_
.size());
915 EXPECT_EQ(kRegularUserID
, set_calls_
.front());
918 Mock::VerifyAndClear(&external_data_manager_
);
919 EXPECT_CALL(external_data_manager_
, Fetch(key::kUserAvatarImage
, _
))
921 .WillOnce(SaveArg
<1>(&fetch_callback_
));
923 SetRegularUserAvatarPolicy(avatar_policy_2_
);
925 EXPECT_TRUE(cleared_calls_
.empty());
926 EXPECT_TRUE(fetched_calls_
.empty());
927 ASSERT_EQ(1u, set_calls_
.size());
928 EXPECT_EQ(kRegularUserID
, set_calls_
.front());
931 Mock::VerifyAndClear(&external_data_manager_
);
932 EXPECT_CALL(external_data_manager_
, Fetch(key::kUserAvatarImage
, _
)).Times(0);
934 fetch_callback_
.Run(make_scoped_ptr(new std::string(avatar_policy_2_data_
)));
936 EXPECT_TRUE(set_calls_
.empty());
937 EXPECT_TRUE(cleared_calls_
.empty());
938 ASSERT_EQ(1u, fetched_calls_
.size());
939 EXPECT_EQ(kRegularUserID
, fetched_calls_
.front().first
);
940 EXPECT_EQ(avatar_policy_2_data_
, fetched_calls_
.front().second
);
944 } // namespace policy