Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / policy / cloud_external_data_policy_observer_unittest.cc
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.
4
5 #include "chrome/browser/chromeos/policy/cloud_external_data_policy_observer.h"
6
7 #include <utility>
8 #include <vector>
9
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"
53 #include "url/gurl.h"
54
55 namespace em = enterprise_management;
56
57 using ::testing::Mock;
58 using ::testing::Return;
59 using ::testing::SaveArg;
60 using ::testing::_;
61
62 namespace policy {
63
64 namespace {
65
66 const char kDeviceLocalAccount[] = "device_local_account@localhost";
67
68 const char kRegularUserID[] = "user@example.com";
69
70 const char kAvatar1URL[] = "http://localhost/avatar1.jpg";
71 const char kAvatar2URL[] = "http://localhost/avatar2.jpg";
72
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),
81       policy_data));
82   base::JSONWriter::Write(
83       test::ConstructExternalDataReference(url, *policy_data).get(),
84       policy);
85 }
86
87 }  // namespace
88
89 class CloudExternalDataPolicyObserverTest
90     : public chromeos::DeviceSettingsTestBase,
91       public CloudExternalDataPolicyObserver::Delegate {
92  public:
93   typedef std::pair<std::string, std::string> FetchedCall;
94
95   CloudExternalDataPolicyObserverTest();
96   virtual ~CloudExternalDataPolicyObserverTest();
97
98   // chromeos::DeviceSettingsTestBase:
99   virtual void SetUp() OVERRIDE;
100   virtual void TearDown() OVERRIDE;
101
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;
110
111   void CreateObserver();
112
113   void ClearObservations();
114
115   void SetDeviceLocalAccountAvatarPolicy(const std::string& account_id,
116                                          const std::string& value);
117
118   void AddDeviceLocalAccount(const std::string& account_id);
119   void RemoveDeviceLocalAccount(const std::string& account_id);
120
121   DeviceLocalAccountPolicyBroker* GetBrokerForDeviceLocalAccountUser();
122
123   void RefreshDeviceLocalAccountPolicy(DeviceLocalAccountPolicyBroker* broker);
124
125   void LogInAsDeviceLocalAccount(const std::string& user_id);
126
127   void SetRegularUserAvatarPolicy(const std::string& value);
128
129   void LogInAsRegularUser();
130
131   const std::string device_local_account_user_id_;
132
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_;
137
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_;
143
144   scoped_ptr<DeviceLocalAccountPolicyProvider>
145       device_local_account_policy_provider_;
146
147   MockCloudExternalDataManager external_data_manager_;
148   MockConfigurationPolicyProvider user_policy_provider_;
149
150   scoped_ptr<TestingProfile> profile_;
151
152   scoped_ptr<CloudExternalDataPolicyObserver> observer_;
153
154   std::vector<std::string> set_calls_;
155   std::vector<std::string> cleared_calls_;
156   std::vector<FetchedCall> fetched_calls_;
157
158   ExternalDataFetcher::FetchCallback fetch_callback_;
159
160  private:
161   DISALLOW_COPY_AND_ASSIGN(CloudExternalDataPolicyObserverTest);
162 };
163
164 CloudExternalDataPolicyObserverTest::CloudExternalDataPolicyObserverTest()
165     : device_local_account_user_id_(GenerateDeviceLocalAccountUserId(
166           kDeviceLocalAccount,
167           DeviceLocalAccount::TYPE_PUBLIC_SESSION)),
168       cros_settings_(&device_settings_service_) {
169 }
170
171 CloudExternalDataPolicyObserverTest::~CloudExternalDataPolicyObserverTest() {
172 }
173
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_,
179                                           &cros_settings_,
180                                           loop_.message_loop_proxy(),
181                                           loop_.message_loop_proxy(),
182                                           loop_.message_loop_proxy(),
183                                           loop_.message_loop_proxy(),
184                                           NULL));
185   url_fetcher_factory_.set_remove_fetcher_on_delete(true);
186
187   EXPECT_CALL(user_policy_provider_, IsInitializationComplete(_))
188       .WillRepeatedly(Return(true));
189   user_policy_provider_.Init();
190
191   ConstructAvatarPolicy("avatar1.jpg",
192                         kAvatar1URL,
193                         &avatar_policy_1_data_,
194                         &avatar_policy_1_);
195   ConstructAvatarPolicy("avatar2.jpg",
196                         kAvatar2URL,
197                         &avatar_policy_2_data_,
198                         &avatar_policy_2_);
199 }
200
201 void CloudExternalDataPolicyObserverTest::TearDown() {
202   observer_.reset();
203   user_policy_provider_.Shutdown();
204   profile_.reset();
205   if (device_local_account_policy_provider_) {
206     device_local_account_policy_provider_->Shutdown();
207     device_local_account_policy_provider_.reset();
208   }
209   device_local_account_policy_service_->Shutdown();
210   device_local_account_policy_service_.reset();
211   chromeos::DeviceSettingsTestBase::TearDown();
212 }
213
214
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);
220 }
221
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);
227 }
228
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);
236 }
237
238 void CloudExternalDataPolicyObserverTest::CreateObserver() {
239   observer_.reset(new CloudExternalDataPolicyObserver(
240       &cros_settings_,
241       &user_manager_,
242       device_local_account_policy_service_.get(),
243       key::kUserAvatarImage,
244       this));
245   observer_->Init();
246 }
247
248 void CloudExternalDataPolicyObserverTest::ClearObservations() {
249   set_calls_.clear();
250   cleared_calls_.clear();
251   fetched_calls_.clear();
252 }
253
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);
262   if (!value.empty())
263     builder.payload().mutable_useravatarimage()->set_value(value);
264   builder.Build();
265   device_settings_test_helper_.set_device_local_account_policy_blob(
266       account_id,
267       builder.GetBlob());
268 }
269
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);
275   account->set_type(
276       em::DeviceLocalAccountInfoProto::ACCOUNT_TYPE_PUBLIC_SESSION);
277   device_policy_.Build();
278   device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
279   ReloadDeviceSettings();
280 }
281
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());
290   }
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);
296     account->set_type(
297         em::DeviceLocalAccountInfoProto::ACCOUNT_TYPE_PUBLIC_SESSION);
298   }
299   device_policy_.Build();
300   device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
301   ReloadDeviceSettings();
302 }
303
304 DeviceLocalAccountPolicyBroker*
305     CloudExternalDataPolicyObserverTest::GetBrokerForDeviceLocalAccountUser() {
306   return device_local_account_policy_service_->GetBrokerForUser(
307       device_local_account_user_id_);
308 }
309
310 void CloudExternalDataPolicyObserverTest::RefreshDeviceLocalAccountPolicy(
311     DeviceLocalAccountPolicyBroker* broker) {
312   broker->core()->store()->Load();
313   device_settings_test_helper_.Flush();
314 }
315
316 void CloudExternalDataPolicyObserverTest::LogInAsDeviceLocalAccount(
317     const std::string& user_id) {
318   device_local_account_policy_provider_.reset(
319       new DeviceLocalAccountPolicyProvider(
320           user_id,
321           device_local_account_policy_service_.get()));
322
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)));
328
329   profile_ = builder.Build();
330   profile_->set_profile_name(user_id);
331
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()));
337 }
338
339 void CloudExternalDataPolicyObserverTest::SetRegularUserAvatarPolicy(
340     const std::string& value) {
341   PolicyMap policy_map;
342   if (!value.empty()) {
343     policy_map.Set(
344         key::kUserAvatarImage,
345         POLICY_LEVEL_MANDATORY,
346         POLICY_SCOPE_USER,
347         new base::StringValue(value),
348         external_data_manager_.CreateExternalDataFetcher(
349             key::kUserAvatarImage).release());
350   }
351   user_policy_provider_.UpdateChromePolicy(policy_map);
352 }
353
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)));
360
361   profile_ = builder.Build();
362   profile_->set_profile_name(kRegularUserID);
363
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()));
369 }
370
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);
379
380   DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
381   ASSERT_TRUE(broker);
382   broker->external_data_manager()->Connect(NULL);
383   base::RunLoop().RunUntilIdle();
384
385   CreateObserver();
386
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());
391   ClearObservations();
392
393   net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
394   ASSERT_TRUE(fetcher);
395   EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
396
397   fetcher->SetResponseString(avatar_policy_1_data_);
398   fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
399                                             net::OK));
400   fetcher->set_response_code(200);
401   fetcher->delegate()->OnURLFetchComplete(fetcher);
402   base::RunLoop().RunUntilIdle();
403
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);
409   ClearObservations();
410
411   EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(1));
412 }
413
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);
421
422   DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
423   ASSERT_TRUE(broker);
424   broker->external_data_manager()->Connect(NULL);
425   base::RunLoop().RunUntilIdle();
426
427   CreateObserver();
428
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());
433   ClearObservations();
434
435   net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
436   ASSERT_TRUE(fetcher);
437   EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
438
439   fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
440                                             net::OK));
441   fetcher->set_response_code(400);
442   fetcher->delegate()->OnURLFetchComplete(fetcher);
443   base::RunLoop().RunUntilIdle();
444
445   EXPECT_TRUE(set_calls_.empty());
446   EXPECT_TRUE(cleared_calls_.empty());
447   EXPECT_TRUE(fetched_calls_.empty());
448   ClearObservations();
449
450   EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(1));
451 }
452
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);
460
461   DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
462   ASSERT_TRUE(broker);
463   broker->external_data_manager()->Connect(NULL);
464   base::RunLoop().RunUntilIdle();
465
466   CreateObserver();
467
468   EXPECT_TRUE(set_calls_.empty());
469   EXPECT_TRUE(cleared_calls_.empty());
470   EXPECT_TRUE(fetched_calls_.empty());
471   ClearObservations();
472
473   EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
474
475   SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, "");
476   RefreshDeviceLocalAccountPolicy(broker);
477
478   EXPECT_TRUE(set_calls_.empty());
479   EXPECT_TRUE(cleared_calls_.empty());
480   EXPECT_TRUE(fetched_calls_.empty());
481   ClearObservations();
482
483   EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
484 }
485
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);
494
495   DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
496   ASSERT_TRUE(broker);
497   broker->external_data_manager()->Connect(NULL);
498   base::RunLoop().RunUntilIdle();
499
500   CreateObserver();
501
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());
506   ClearObservations();
507
508   net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
509   ASSERT_TRUE(fetcher);
510   EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
511
512   SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, "");
513   RefreshDeviceLocalAccountPolicy(broker);
514
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());
519   ClearObservations();
520
521   EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
522 }
523
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);
532
533   DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
534   ASSERT_TRUE(broker);
535   broker->external_data_manager()->Connect(NULL);
536   base::RunLoop().RunUntilIdle();
537
538   CreateObserver();
539
540   EXPECT_TRUE(set_calls_.empty());
541   EXPECT_TRUE(cleared_calls_.empty());
542   EXPECT_TRUE(fetched_calls_.empty());
543   ClearObservations();
544
545   SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
546   RefreshDeviceLocalAccountPolicy(broker);
547
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());
552   ClearObservations();
553
554   net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
555   ASSERT_TRUE(fetcher);
556   EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
557
558   fetcher->SetResponseString(avatar_policy_1_data_);
559   fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
560                                             net::OK));
561   fetcher->set_response_code(200);
562   fetcher->delegate()->OnURLFetchComplete(fetcher);
563   base::RunLoop().RunUntilIdle();
564
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);
570   ClearObservations();
571
572   EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(1));
573 }
574
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);
584
585   DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
586   ASSERT_TRUE(broker);
587   broker->external_data_manager()->Connect(NULL);
588   base::RunLoop().RunUntilIdle();
589
590   CreateObserver();
591
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());
596   ClearObservations();
597
598   net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
599   ASSERT_TRUE(fetcher);
600   EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
601
602   SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_2_);
603   RefreshDeviceLocalAccountPolicy(broker);
604
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());
609   ClearObservations();
610
611   fetcher = url_fetcher_factory_.GetFetcherByID(1);
612   ASSERT_TRUE(fetcher);
613   EXPECT_EQ(GURL(kAvatar2URL), fetcher->GetOriginalURL());
614
615   fetcher->SetResponseString(avatar_policy_2_data_);
616   fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
617                                             net::OK));
618   fetcher->set_response_code(200);
619   fetcher->delegate()->OnURLFetchComplete(fetcher);
620   base::RunLoop().RunUntilIdle();
621
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);
627   ClearObservations();
628
629   EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(2));
630 }
631
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);
641
642   DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
643   ASSERT_TRUE(broker);
644   broker->external_data_manager()->Connect(NULL);
645   base::RunLoop().RunUntilIdle();
646
647   CreateObserver();
648
649   LogInAsDeviceLocalAccount(kDeviceLocalAccount);
650
651   EXPECT_TRUE(set_calls_.empty());
652   EXPECT_TRUE(cleared_calls_.empty());
653   EXPECT_TRUE(fetched_calls_.empty());
654   ClearObservations();
655
656   SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
657   RefreshDeviceLocalAccountPolicy(broker);
658
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());
663   ClearObservations();
664
665   net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
666   ASSERT_TRUE(fetcher);
667   EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
668
669   fetcher->SetResponseString(avatar_policy_1_data_);
670   fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
671                                             net::OK));
672   fetcher->set_response_code(200);
673   fetcher->delegate()->OnURLFetchComplete(fetcher);
674   base::RunLoop().RunUntilIdle();
675
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);
681   ClearObservations();
682
683   EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(1));
684 }
685
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);
692
693   DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
694   ASSERT_TRUE(broker);
695   broker->external_data_manager()->Connect(NULL);
696   base::RunLoop().RunUntilIdle();
697
698   CreateObserver();
699
700   EXPECT_TRUE(set_calls_.empty());
701   EXPECT_TRUE(cleared_calls_.empty());
702   EXPECT_TRUE(fetched_calls_.empty());
703   ClearObservations();
704
705   EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
706
707   RemoveDeviceLocalAccount(kDeviceLocalAccount);
708
709   EXPECT_TRUE(set_calls_.empty());
710   EXPECT_TRUE(cleared_calls_.empty());
711   EXPECT_TRUE(fetched_calls_.empty());
712   ClearObservations();
713
714   EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
715 }
716
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);
726
727   DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
728   ASSERT_TRUE(broker);
729   broker->external_data_manager()->Connect(NULL);
730   base::RunLoop().RunUntilIdle();
731
732   CreateObserver();
733
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());
738   ClearObservations();
739
740   net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
741   ASSERT_TRUE(fetcher);
742   EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
743
744   RemoveDeviceLocalAccount(kDeviceLocalAccount);
745
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());
750   ClearObservations();
751
752   EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
753 }
754
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_);
761
762   CreateObserver();
763
764   EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
765       .Times(1)
766       .WillOnce(SaveArg<1>(&fetch_callback_));
767
768   LogInAsRegularUser();
769
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());
774   ClearObservations();
775
776   Mock::VerifyAndClear(&external_data_manager_);
777   EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
778
779   fetch_callback_.Run(make_scoped_ptr(new std::string(avatar_policy_1_data_)));
780
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);
786   ClearObservations();
787 }
788
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) {
794   CreateObserver();
795
796   EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
797
798   LogInAsRegularUser();
799
800   EXPECT_TRUE(set_calls_.empty());
801   EXPECT_TRUE(cleared_calls_.empty());
802   EXPECT_TRUE(fetched_calls_.empty());
803   ClearObservations();
804
805   Mock::VerifyAndClear(&external_data_manager_);
806   EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
807
808   SetRegularUserAvatarPolicy("");
809
810   EXPECT_TRUE(set_calls_.empty());
811   EXPECT_TRUE(cleared_calls_.empty());
812   EXPECT_TRUE(fetched_calls_.empty());
813   ClearObservations();
814 }
815
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
820 // started.
821 TEST_F(CloudExternalDataPolicyObserverTest, RegularUserClearSet) {
822   SetRegularUserAvatarPolicy(avatar_policy_1_);
823
824   CreateObserver();
825
826   EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
827       .Times(1)
828       .WillOnce(SaveArg<1>(&fetch_callback_));
829
830   LogInAsRegularUser();
831
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());
836   ClearObservations();
837
838   Mock::VerifyAndClear(&external_data_manager_);
839   EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
840
841   SetRegularUserAvatarPolicy("");
842
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());
847   ClearObservations();
848 }
849
850
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
856 // emitted.
857 TEST_F(CloudExternalDataPolicyObserverTest, RegularUserSetUnset) {
858   CreateObserver();
859
860   EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
861
862   LogInAsRegularUser();
863
864   EXPECT_TRUE(set_calls_.empty());
865   EXPECT_TRUE(cleared_calls_.empty());
866   EXPECT_TRUE(fetched_calls_.empty());
867   ClearObservations();
868
869   Mock::VerifyAndClear(&external_data_manager_);
870   EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
871       .Times(1)
872       .WillOnce(SaveArg<1>(&fetch_callback_));
873
874   SetRegularUserAvatarPolicy(avatar_policy_1_);
875
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());
880   ClearObservations();
881
882   Mock::VerifyAndClear(&external_data_manager_);
883   EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
884
885   fetch_callback_.Run(make_scoped_ptr(new std::string(avatar_policy_1_data_)));
886
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);
892   ClearObservations();
893 }
894
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_);
903
904   CreateObserver();
905
906   EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
907       .Times(1)
908       .WillOnce(SaveArg<1>(&fetch_callback_));
909
910   LogInAsRegularUser();
911
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());
916   ClearObservations();
917
918   Mock::VerifyAndClear(&external_data_manager_);
919   EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
920       .Times(1)
921       .WillOnce(SaveArg<1>(&fetch_callback_));
922
923   SetRegularUserAvatarPolicy(avatar_policy_2_);
924
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());
929   ClearObservations();
930
931   Mock::VerifyAndClear(&external_data_manager_);
932   EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
933
934   fetch_callback_.Run(make_scoped_ptr(new std::string(avatar_policy_2_data_)));
935
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);
941   ClearObservations();
942 }
943
944 }  // namespace policy