Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / cloud_external_data_policy_observer_unittest.cc
blob29f5eded72f0000aab65bd56e40e40182ba933e5
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/policy/cloud_external_data_policy_observer.h"
7 #include <utility>
8 #include <vector>
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/json/json_writer.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/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/policy/cloud_external_data_manager_base_test_util.h"
21 #include "chrome/browser/chromeos/policy/device_local_account.h"
22 #include "chrome/browser/chromeos/policy/device_local_account_external_data_manager.h"
23 #include "chrome/browser/chromeos/policy/device_local_account_policy_provider.h"
24 #include "chrome/browser/chromeos/policy/device_local_account_policy_service.h"
25 #include "chrome/browser/chromeos/policy/fake_affiliated_invalidation_service_provider.h"
26 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h"
27 #include "chrome/browser/chromeos/profiles/profile_helper.h"
28 #include "chrome/browser/chromeos/settings/device_settings_service.h"
29 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h"
30 #include "chrome/browser/profiles/profile.h"
31 #include "chrome/common/chrome_paths.h"
32 #include "chrome/test/base/testing_browser_process.h"
33 #include "chrome/test/base/testing_profile.h"
34 #include "chrome/test/base/testing_profile_manager.h"
35 #include "components/policy/core/common/cloud/cloud_policy_core.h"
36 #include "components/policy/core/common/cloud/cloud_policy_store.h"
37 #include "components/policy/core/common/cloud/mock_cloud_external_data_manager.h"
38 #include "components/policy/core/common/cloud/policy_builder.h"
39 #include "components/policy/core/common/external_data_fetcher.h"
40 #include "components/policy/core/common/mock_configuration_policy_provider.h"
41 #include "components/policy/core/common/policy_map.h"
42 #include "components/policy/core/common/policy_service.h"
43 #include "components/policy/core/common/policy_service_impl.h"
44 #include "components/policy/core/common/policy_types.h"
45 #include "content/public/browser/notification_details.h"
46 #include "content/public/browser/notification_service.h"
47 #include "content/public/browser/notification_source.h"
48 #include "net/url_request/test_url_fetcher_factory.h"
49 #include "net/url_request/url_fetcher_delegate.h"
50 #include "net/url_request/url_request_context_getter.h"
51 #include "net/url_request/url_request_status.h"
52 #include "policy/policy_constants.h"
53 #include "policy/proto/cloud_policy.pb.h"
54 #include "testing/gmock/include/gmock/gmock.h"
55 #include "testing/gtest/include/gtest/gtest.h"
56 #include "url/gurl.h"
58 namespace em = enterprise_management;
60 using ::testing::Mock;
61 using ::testing::Return;
62 using ::testing::SaveArg;
63 using ::testing::_;
65 namespace policy {
67 namespace {
69 const char kDeviceLocalAccount[] = "device_local_account@localhost";
71 const char kRegularUserID[] = "user@example.com";
73 const char kAvatar1URL[] = "http://localhost/avatar1.jpg";
74 const char kAvatar2URL[] = "http://localhost/avatar2.jpg";
76 void ConstructAvatarPolicy(const std::string& file_name,
77 const std::string& url,
78 std::string* policy_data,
79 std::string* policy) {
80 base::FilePath test_data_dir;
81 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir));
82 ASSERT_TRUE(base::ReadFileToString(
83 test_data_dir.Append("chromeos").Append(file_name),
84 policy_data));
85 base::JSONWriter::Write(
86 test::ConstructExternalDataReference(url, *policy_data).get(),
87 policy);
90 } // namespace
92 class CloudExternalDataPolicyObserverTest
93 : public chromeos::DeviceSettingsTestBase,
94 public CloudExternalDataPolicyObserver::Delegate {
95 public:
96 typedef std::pair<std::string, std::string> FetchedCall;
98 CloudExternalDataPolicyObserverTest();
99 ~CloudExternalDataPolicyObserverTest() override;
101 // chromeos::DeviceSettingsTestBase:
102 void SetUp() override;
103 void TearDown() override;
105 // CloudExternalDataPolicyObserver::Delegate:
106 void OnExternalDataSet(const std::string& policy,
107 const std::string& user_id) override;
108 void OnExternalDataCleared(const std::string& policy,
109 const std::string& user_id) override;
110 void OnExternalDataFetched(const std::string& policy,
111 const std::string& user_id,
112 scoped_ptr<std::string> data) override;
114 void CreateObserver();
116 void ClearObservations();
118 void SetDeviceLocalAccountAvatarPolicy(const std::string& account_id,
119 const std::string& value);
121 void AddDeviceLocalAccount(const std::string& account_id);
122 void RemoveDeviceLocalAccount(const std::string& account_id);
124 DeviceLocalAccountPolicyBroker* GetBrokerForDeviceLocalAccountUser();
126 void RefreshDeviceLocalAccountPolicy(DeviceLocalAccountPolicyBroker* broker);
128 void LogInAsDeviceLocalAccount(const std::string& user_id);
130 void SetRegularUserAvatarPolicy(const std::string& value);
132 void LogInAsRegularUser();
134 const std::string device_local_account_user_id_;
136 std::string avatar_policy_1_data_;
137 std::string avatar_policy_2_data_;
138 std::string avatar_policy_1_;
139 std::string avatar_policy_2_;
141 chromeos::CrosSettings cros_settings_;
142 scoped_ptr<DeviceLocalAccountPolicyService>
143 device_local_account_policy_service_;
144 FakeAffiliatedInvalidationServiceProvider
145 affiliated_invalidation_service_provider_;
146 net::TestURLFetcherFactory url_fetcher_factory_;
148 scoped_ptr<DeviceLocalAccountPolicyProvider>
149 device_local_account_policy_provider_;
151 MockCloudExternalDataManager external_data_manager_;
152 MockConfigurationPolicyProvider user_policy_provider_;
154 scoped_ptr<TestingProfile> profile_;
156 scoped_ptr<CloudExternalDataPolicyObserver> observer_;
158 std::vector<std::string> set_calls_;
159 std::vector<std::string> cleared_calls_;
160 std::vector<FetchedCall> fetched_calls_;
162 ExternalDataFetcher::FetchCallback fetch_callback_;
164 TestingProfileManager profile_manager_;
166 private:
167 DISALLOW_COPY_AND_ASSIGN(CloudExternalDataPolicyObserverTest);
170 CloudExternalDataPolicyObserverTest::CloudExternalDataPolicyObserverTest()
171 : device_local_account_user_id_(GenerateDeviceLocalAccountUserId(
172 kDeviceLocalAccount,
173 DeviceLocalAccount::TYPE_PUBLIC_SESSION)),
174 cros_settings_(&device_settings_service_),
175 profile_manager_(TestingBrowserProcess::GetGlobal()) {
178 CloudExternalDataPolicyObserverTest::~CloudExternalDataPolicyObserverTest() {
181 void CloudExternalDataPolicyObserverTest::SetUp() {
182 chromeos::DeviceSettingsTestBase::SetUp();
184 ASSERT_TRUE(profile_manager_.SetUp());
186 device_local_account_policy_service_.reset(
187 new DeviceLocalAccountPolicyService(
188 &device_settings_test_helper_,
189 &device_settings_service_,
190 &cros_settings_,
191 &affiliated_invalidation_service_provider_,
192 base::MessageLoopProxy::current(),
193 base::MessageLoopProxy::current(),
194 base::MessageLoopProxy::current(),
195 base::MessageLoopProxy::current(),
196 nullptr));
197 url_fetcher_factory_.set_remove_fetcher_on_delete(true);
199 EXPECT_CALL(user_policy_provider_, IsInitializationComplete(_))
200 .WillRepeatedly(Return(true));
201 user_policy_provider_.Init();
203 ConstructAvatarPolicy("avatar1.jpg",
204 kAvatar1URL,
205 &avatar_policy_1_data_,
206 &avatar_policy_1_);
207 ConstructAvatarPolicy("avatar2.jpg",
208 kAvatar2URL,
209 &avatar_policy_2_data_,
210 &avatar_policy_2_);
213 void CloudExternalDataPolicyObserverTest::TearDown() {
214 observer_.reset();
215 user_policy_provider_.Shutdown();
216 profile_.reset();
217 if (device_local_account_policy_provider_) {
218 device_local_account_policy_provider_->Shutdown();
219 device_local_account_policy_provider_.reset();
221 device_local_account_policy_service_->Shutdown();
222 device_local_account_policy_service_.reset();
223 chromeos::DeviceSettingsTestBase::TearDown();
227 void CloudExternalDataPolicyObserverTest::OnExternalDataSet(
228 const std::string& policy,
229 const std::string& user_id) {
230 EXPECT_EQ(key::kUserAvatarImage, policy);
231 set_calls_.push_back(user_id);
234 void CloudExternalDataPolicyObserverTest::OnExternalDataCleared(
235 const std::string& policy,
236 const std::string& user_id) {
237 EXPECT_EQ(key::kUserAvatarImage, policy);
238 cleared_calls_.push_back(user_id);
241 void CloudExternalDataPolicyObserverTest::OnExternalDataFetched(
242 const std::string& policy,
243 const std::string& user_id,
244 scoped_ptr<std::string> data) {
245 EXPECT_EQ(key::kUserAvatarImage, policy);
246 fetched_calls_.push_back(make_pair(user_id, std::string()));
247 fetched_calls_.back().second.swap(*data);
250 void CloudExternalDataPolicyObserverTest::CreateObserver() {
251 observer_.reset(new CloudExternalDataPolicyObserver(
252 &cros_settings_,
253 device_local_account_policy_service_.get(),
254 key::kUserAvatarImage,
255 this));
256 observer_->Init();
259 void CloudExternalDataPolicyObserverTest::ClearObservations() {
260 set_calls_.clear();
261 cleared_calls_.clear();
262 fetched_calls_.clear();
265 void CloudExternalDataPolicyObserverTest::SetDeviceLocalAccountAvatarPolicy(
266 const std::string& account_id,
267 const std::string& value) {
268 UserPolicyBuilder builder;
269 builder.policy_data().set_policy_type(
270 dm_protocol::kChromePublicAccountPolicyType);
271 builder.policy_data().set_settings_entity_id(account_id);
272 builder.policy_data().set_username(account_id);
273 if (!value.empty())
274 builder.payload().mutable_useravatarimage()->set_value(value);
275 builder.Build();
276 device_settings_test_helper_.set_device_local_account_policy_blob(
277 account_id,
278 builder.GetBlob());
281 void CloudExternalDataPolicyObserverTest::AddDeviceLocalAccount(
282 const std::string& account_id) {
283 em::DeviceLocalAccountInfoProto* account =
284 device_policy_.payload().mutable_device_local_accounts()->add_account();
285 account->set_account_id(account_id);
286 account->set_type(
287 em::DeviceLocalAccountInfoProto::ACCOUNT_TYPE_PUBLIC_SESSION);
288 device_policy_.Build();
289 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
290 ReloadDeviceSettings();
293 void CloudExternalDataPolicyObserverTest::RemoveDeviceLocalAccount(
294 const std::string& account_id) {
295 em::DeviceLocalAccountsProto* accounts =
296 device_policy_.payload().mutable_device_local_accounts();
297 std::vector<std::string> account_ids;
298 for (int i = 0; i < accounts->account_size(); ++i) {
299 if (accounts->account(i).account_id() != account_id)
300 account_ids.push_back(accounts->account(i).account_id());
302 accounts->clear_account();
303 for (std::vector<std::string>::const_iterator it = account_ids.begin();
304 it != account_ids.end(); ++it) {
305 em::DeviceLocalAccountInfoProto* account = accounts->add_account();
306 account->set_account_id(*it);
307 account->set_type(
308 em::DeviceLocalAccountInfoProto::ACCOUNT_TYPE_PUBLIC_SESSION);
310 device_policy_.Build();
311 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
312 ReloadDeviceSettings();
315 DeviceLocalAccountPolicyBroker*
316 CloudExternalDataPolicyObserverTest::GetBrokerForDeviceLocalAccountUser() {
317 return device_local_account_policy_service_->GetBrokerForUser(
318 device_local_account_user_id_);
321 void CloudExternalDataPolicyObserverTest::RefreshDeviceLocalAccountPolicy(
322 DeviceLocalAccountPolicyBroker* broker) {
323 broker->core()->store()->Load();
324 device_settings_test_helper_.Flush();
327 void CloudExternalDataPolicyObserverTest::LogInAsDeviceLocalAccount(
328 const std::string& user_id) {
329 user_manager_->AddUser(user_id);
331 device_local_account_policy_provider_.reset(
332 new DeviceLocalAccountPolicyProvider(
333 user_id,
334 device_local_account_policy_service_.get(),
335 scoped_ptr<PolicyMap>()));
337 PolicyServiceImpl::Providers providers;
338 providers.push_back(device_local_account_policy_provider_.get());
339 TestingProfile::Builder builder;
340 builder.SetPolicyService(
341 scoped_ptr<PolicyService>(new PolicyServiceImpl(providers)));
342 builder.SetPath(chromeos::ProfileHelper::Get()->GetProfilePathByUserIdHash(
343 chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting(user_id)));
345 profile_ = builder.Build();
346 profile_->set_profile_name(user_id);
348 content::NotificationService::current()->Notify(
349 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
350 content::NotificationService::AllSources(),
351 content::Details<Profile>(profile_.get()));
354 void CloudExternalDataPolicyObserverTest::SetRegularUserAvatarPolicy(
355 const std::string& value) {
356 PolicyMap policy_map;
357 if (!value.empty()) {
358 policy_map.Set(
359 key::kUserAvatarImage,
360 POLICY_LEVEL_MANDATORY,
361 POLICY_SCOPE_USER,
362 new base::StringValue(value),
363 external_data_manager_.CreateExternalDataFetcher(
364 key::kUserAvatarImage).release());
366 user_policy_provider_.UpdateChromePolicy(policy_map);
369 void CloudExternalDataPolicyObserverTest::LogInAsRegularUser() {
370 user_manager_->AddUser(kRegularUserID);
372 PolicyServiceImpl::Providers providers;
373 providers.push_back(&user_policy_provider_);
374 TestingProfile::Builder builder;
375 builder.SetPolicyService(
376 scoped_ptr<PolicyService>(new PolicyServiceImpl(providers)));
377 builder.SetPath(chromeos::ProfileHelper::Get()->GetProfilePathByUserIdHash(
378 chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting(
379 kRegularUserID)));
381 profile_ = builder.Build();
382 profile_->set_profile_name(kRegularUserID);
384 content::NotificationService::current()->Notify(
385 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
386 content::NotificationService::AllSources(),
387 content::Details<Profile>(profile_.get()));
390 // Verifies that when an external data reference is set for a device-local
391 // account, a corresponding notification is emitted and a fetch is started.
392 // Further verifies that when the fetch succeeds, a notification containing the
393 // external data is emitted.
394 TEST_F(CloudExternalDataPolicyObserverTest,
395 ExistingDeviceLocalAccountFetchSuccess) {
396 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
397 AddDeviceLocalAccount(kDeviceLocalAccount);
399 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
400 ASSERT_TRUE(broker);
401 broker->external_data_manager()->Connect(NULL);
402 base::RunLoop().RunUntilIdle();
404 CreateObserver();
406 EXPECT_TRUE(cleared_calls_.empty());
407 EXPECT_TRUE(fetched_calls_.empty());
408 ASSERT_EQ(1u, set_calls_.size());
409 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
410 ClearObservations();
412 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
413 ASSERT_TRUE(fetcher);
414 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
416 fetcher->SetResponseString(avatar_policy_1_data_);
417 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
418 net::OK));
419 fetcher->set_response_code(200);
420 fetcher->delegate()->OnURLFetchComplete(fetcher);
421 base::RunLoop().RunUntilIdle();
423 EXPECT_TRUE(set_calls_.empty());
424 EXPECT_TRUE(cleared_calls_.empty());
425 ASSERT_EQ(1u, fetched_calls_.size());
426 EXPECT_EQ(device_local_account_user_id_, fetched_calls_.front().first);
427 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
428 ClearObservations();
430 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(1));
433 // Verifies that when an external data reference is set for a device-local
434 // account, a corresponding notification is emitted and a fetch is started.
435 // Further verifies that when the fetch fails, no notification is emitted.
436 TEST_F(CloudExternalDataPolicyObserverTest,
437 ExistingDeviceLocalAccountFetchFailure) {
438 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
439 AddDeviceLocalAccount(kDeviceLocalAccount);
441 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
442 ASSERT_TRUE(broker);
443 broker->external_data_manager()->Connect(NULL);
444 base::RunLoop().RunUntilIdle();
446 CreateObserver();
448 EXPECT_TRUE(cleared_calls_.empty());
449 EXPECT_TRUE(fetched_calls_.empty());
450 ASSERT_EQ(1u, set_calls_.size());
451 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
452 ClearObservations();
454 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
455 ASSERT_TRUE(fetcher);
456 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
458 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
459 net::OK));
460 fetcher->set_response_code(400);
461 fetcher->delegate()->OnURLFetchComplete(fetcher);
462 base::RunLoop().RunUntilIdle();
464 EXPECT_TRUE(set_calls_.empty());
465 EXPECT_TRUE(cleared_calls_.empty());
466 EXPECT_TRUE(fetched_calls_.empty());
467 ClearObservations();
469 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(1));
472 // Verifies that when the external data reference for a device-local account is
473 // initially not set, no notifications are emitted. Further verifies that when
474 // the external data reference is then cleared (which is a no-op), again, no
475 // notifications are emitted.
476 TEST_F(CloudExternalDataPolicyObserverTest,
477 ExistingDeviceLocalAccountClearUnset) {
478 AddDeviceLocalAccount(kDeviceLocalAccount);
480 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
481 ASSERT_TRUE(broker);
482 broker->external_data_manager()->Connect(NULL);
483 base::RunLoop().RunUntilIdle();
485 CreateObserver();
487 EXPECT_TRUE(set_calls_.empty());
488 EXPECT_TRUE(cleared_calls_.empty());
489 EXPECT_TRUE(fetched_calls_.empty());
490 ClearObservations();
492 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
494 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, "");
495 RefreshDeviceLocalAccountPolicy(broker);
497 EXPECT_TRUE(set_calls_.empty());
498 EXPECT_TRUE(cleared_calls_.empty());
499 EXPECT_TRUE(fetched_calls_.empty());
500 ClearObservations();
502 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
505 // Verifies that when the external data reference for a device-local account is
506 // initially set, a corresponding notification is emitted and a fetch is
507 // started. Further verifies that when the external data reference is then
508 // cleared, a corresponding notification is emitted and the fetch is canceled.
509 TEST_F(CloudExternalDataPolicyObserverTest,
510 ExistingDeviceLocalAccountClearSet) {
511 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
512 AddDeviceLocalAccount(kDeviceLocalAccount);
514 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
515 ASSERT_TRUE(broker);
516 broker->external_data_manager()->Connect(NULL);
517 base::RunLoop().RunUntilIdle();
519 CreateObserver();
521 EXPECT_TRUE(cleared_calls_.empty());
522 EXPECT_TRUE(fetched_calls_.empty());
523 ASSERT_EQ(1u, set_calls_.size());
524 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
525 ClearObservations();
527 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
528 ASSERT_TRUE(fetcher);
529 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
531 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, "");
532 RefreshDeviceLocalAccountPolicy(broker);
534 EXPECT_TRUE(set_calls_.empty());
535 EXPECT_TRUE(fetched_calls_.empty());
536 ASSERT_EQ(1u, cleared_calls_.size());
537 EXPECT_EQ(device_local_account_user_id_, cleared_calls_.front());
538 ClearObservations();
540 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
543 // Verifies that when the external data reference for a device-local account is
544 // initially not set, no notifications are emitted. Further verifies that when
545 // the external data reference is then set, a corresponding notification is
546 // emitted and a fetch is started. Also verifies that when the fetch eventually
547 // succeeds, a notification containing the external data is emitted.
548 TEST_F(CloudExternalDataPolicyObserverTest,
549 ExistingDeviceLocalAccountSetUnset) {
550 AddDeviceLocalAccount(kDeviceLocalAccount);
552 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
553 ASSERT_TRUE(broker);
554 broker->external_data_manager()->Connect(NULL);
555 base::RunLoop().RunUntilIdle();
557 CreateObserver();
559 EXPECT_TRUE(set_calls_.empty());
560 EXPECT_TRUE(cleared_calls_.empty());
561 EXPECT_TRUE(fetched_calls_.empty());
562 ClearObservations();
564 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
565 RefreshDeviceLocalAccountPolicy(broker);
567 EXPECT_TRUE(cleared_calls_.empty());
568 EXPECT_TRUE(fetched_calls_.empty());
569 ASSERT_EQ(1u, set_calls_.size());
570 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
571 ClearObservations();
573 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
574 ASSERT_TRUE(fetcher);
575 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
577 fetcher->SetResponseString(avatar_policy_1_data_);
578 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
579 net::OK));
580 fetcher->set_response_code(200);
581 fetcher->delegate()->OnURLFetchComplete(fetcher);
582 base::RunLoop().RunUntilIdle();
584 EXPECT_TRUE(set_calls_.empty());
585 EXPECT_TRUE(cleared_calls_.empty());
586 ASSERT_EQ(1u, fetched_calls_.size());
587 EXPECT_EQ(device_local_account_user_id_, fetched_calls_.front().first);
588 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
589 ClearObservations();
591 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(1));
594 // Verifies that when the external data reference for a device-local account is
595 // initially set, a corresponding notification is emitted and a fetch is
596 // started. Further verifies that when the external data reference is then
597 // updated, a corresponding notification is emitted and the fetch is restarted.
598 // Also verifies that when the fetch eventually succeeds, a notification
599 // containing the external data is emitted.
600 TEST_F(CloudExternalDataPolicyObserverTest, ExistingDeviceLocalAccountSetSet) {
601 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
602 AddDeviceLocalAccount(kDeviceLocalAccount);
604 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
605 ASSERT_TRUE(broker);
606 broker->external_data_manager()->Connect(NULL);
607 base::RunLoop().RunUntilIdle();
609 CreateObserver();
611 EXPECT_TRUE(cleared_calls_.empty());
612 EXPECT_TRUE(fetched_calls_.empty());
613 ASSERT_EQ(1u, set_calls_.size());
614 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
615 ClearObservations();
617 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
618 ASSERT_TRUE(fetcher);
619 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
621 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_2_);
622 RefreshDeviceLocalAccountPolicy(broker);
624 EXPECT_TRUE(cleared_calls_.empty());
625 EXPECT_TRUE(fetched_calls_.empty());
626 ASSERT_EQ(1u, set_calls_.size());
627 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
628 ClearObservations();
630 fetcher = url_fetcher_factory_.GetFetcherByID(1);
631 ASSERT_TRUE(fetcher);
632 EXPECT_EQ(GURL(kAvatar2URL), fetcher->GetOriginalURL());
634 fetcher->SetResponseString(avatar_policy_2_data_);
635 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
636 net::OK));
637 fetcher->set_response_code(200);
638 fetcher->delegate()->OnURLFetchComplete(fetcher);
639 base::RunLoop().RunUntilIdle();
641 EXPECT_TRUE(set_calls_.empty());
642 EXPECT_TRUE(cleared_calls_.empty());
643 ASSERT_EQ(1u, fetched_calls_.size());
644 EXPECT_EQ(device_local_account_user_id_, fetched_calls_.front().first);
645 EXPECT_EQ(avatar_policy_2_data_, fetched_calls_.front().second);
646 ClearObservations();
648 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(2));
651 // Verifies that when the external data reference for a device-local account is
652 // initially not set, no notifications are emitted during login into the
653 // account. Further verifies that when the external data reference is then set,
654 // a corresponding notification is emitted only once and a fetch is started.
655 // Also verifies that when the fetch eventually succeeds, a notification
656 // containing the external data is emitted, again, only once.
657 TEST_F(CloudExternalDataPolicyObserverTest,
658 ExistingDeviceLocalAccountSetAfterLogin) {
659 AddDeviceLocalAccount(kDeviceLocalAccount);
661 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
662 ASSERT_TRUE(broker);
663 broker->external_data_manager()->Connect(NULL);
664 base::RunLoop().RunUntilIdle();
666 CreateObserver();
668 LogInAsDeviceLocalAccount(kDeviceLocalAccount);
670 EXPECT_TRUE(set_calls_.empty());
671 EXPECT_TRUE(cleared_calls_.empty());
672 EXPECT_TRUE(fetched_calls_.empty());
673 ClearObservations();
675 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
676 RefreshDeviceLocalAccountPolicy(broker);
678 EXPECT_TRUE(cleared_calls_.empty());
679 EXPECT_TRUE(fetched_calls_.empty());
680 ASSERT_EQ(1u, set_calls_.size());
681 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
682 ClearObservations();
684 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
685 ASSERT_TRUE(fetcher);
686 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
688 fetcher->SetResponseString(avatar_policy_1_data_);
689 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
690 net::OK));
691 fetcher->set_response_code(200);
692 fetcher->delegate()->OnURLFetchComplete(fetcher);
693 base::RunLoop().RunUntilIdle();
695 EXPECT_TRUE(set_calls_.empty());
696 EXPECT_TRUE(cleared_calls_.empty());
697 ASSERT_EQ(1u, fetched_calls_.size());
698 EXPECT_EQ(device_local_account_user_id_, fetched_calls_.front().first);
699 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
700 ClearObservations();
702 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(1));
705 // Verifies that when the external data reference for a device-local account is
706 // initially not set, no notifications are emitted. Further verifies that when
707 // the device-local account is then removed, again, no notifications are sent.
708 TEST_F(CloudExternalDataPolicyObserverTest,
709 ExistingDeviceLocalAccountRemoveAccountUnset) {
710 AddDeviceLocalAccount(kDeviceLocalAccount);
712 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
713 ASSERT_TRUE(broker);
714 broker->external_data_manager()->Connect(NULL);
715 base::RunLoop().RunUntilIdle();
717 CreateObserver();
719 EXPECT_TRUE(set_calls_.empty());
720 EXPECT_TRUE(cleared_calls_.empty());
721 EXPECT_TRUE(fetched_calls_.empty());
722 ClearObservations();
724 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
726 RemoveDeviceLocalAccount(kDeviceLocalAccount);
728 EXPECT_TRUE(set_calls_.empty());
729 EXPECT_TRUE(cleared_calls_.empty());
730 EXPECT_TRUE(fetched_calls_.empty());
731 ClearObservations();
733 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
736 // Verifies that when the external data reference for a device-local account is
737 // initially set, a corresponding notification is emitted and a fetch is
738 // started. Further verifies that when the device-local account is then removed,
739 // a notification indicating that the external data reference has been cleared
740 // is emitted and the fetch is canceled.
741 TEST_F(CloudExternalDataPolicyObserverTest,
742 ExistingDeviceLocalAccountRemoveAccountSet) {
743 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_);
744 AddDeviceLocalAccount(kDeviceLocalAccount);
746 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser();
747 ASSERT_TRUE(broker);
748 broker->external_data_manager()->Connect(NULL);
749 base::RunLoop().RunUntilIdle();
751 CreateObserver();
753 EXPECT_TRUE(cleared_calls_.empty());
754 EXPECT_TRUE(fetched_calls_.empty());
755 ASSERT_EQ(1u, set_calls_.size());
756 EXPECT_EQ(device_local_account_user_id_, set_calls_.front());
757 ClearObservations();
759 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
760 ASSERT_TRUE(fetcher);
761 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL());
763 RemoveDeviceLocalAccount(kDeviceLocalAccount);
765 EXPECT_TRUE(set_calls_.empty());
766 EXPECT_TRUE(fetched_calls_.empty());
767 ASSERT_EQ(1u, cleared_calls_.size());
768 EXPECT_EQ(device_local_account_user_id_, cleared_calls_.front());
769 ClearObservations();
771 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0));
774 // Verifies that when an external data reference is set for a regular user and
775 // the user logs in, a corresponding notification is emitted and a fetch is
776 // started. Further verifies that when the fetch succeeds, a notification
777 // containing the external data is emitted.
778 TEST_F(CloudExternalDataPolicyObserverTest, RegularUserFetchSuccess) {
779 SetRegularUserAvatarPolicy(avatar_policy_1_);
781 CreateObserver();
783 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
784 .Times(1)
785 .WillOnce(SaveArg<1>(&fetch_callback_));
787 LogInAsRegularUser();
789 EXPECT_TRUE(cleared_calls_.empty());
790 EXPECT_TRUE(fetched_calls_.empty());
791 ASSERT_EQ(1u, set_calls_.size());
792 EXPECT_EQ(kRegularUserID, set_calls_.front());
793 ClearObservations();
795 Mock::VerifyAndClear(&external_data_manager_);
796 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
798 fetch_callback_.Run(make_scoped_ptr(new std::string(avatar_policy_1_data_)));
800 EXPECT_TRUE(set_calls_.empty());
801 EXPECT_TRUE(cleared_calls_.empty());
802 ASSERT_EQ(1u, fetched_calls_.size());
803 EXPECT_EQ(kRegularUserID, fetched_calls_.front().first);
804 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
805 ClearObservations();
808 // Verifies that when the external data reference for a regular user is not set
809 // while the user is logging in, no notifications are emitted. Further verifies
810 // that when the external data reference is then cleared (which is a no-op),
811 // again, no notifications are emitted.
812 TEST_F(CloudExternalDataPolicyObserverTest, RegularUserClearUnset) {
813 CreateObserver();
815 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
817 LogInAsRegularUser();
819 EXPECT_TRUE(set_calls_.empty());
820 EXPECT_TRUE(cleared_calls_.empty());
821 EXPECT_TRUE(fetched_calls_.empty());
822 ClearObservations();
824 Mock::VerifyAndClear(&external_data_manager_);
825 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
827 SetRegularUserAvatarPolicy("");
829 EXPECT_TRUE(set_calls_.empty());
830 EXPECT_TRUE(cleared_calls_.empty());
831 EXPECT_TRUE(fetched_calls_.empty());
832 ClearObservations();
835 // Verifies that when the external data reference for a regular user is set
836 // while the user is logging in, a corresponding notification is emitted and a
837 // fetch is started. Further verifies that when the external data reference is
838 // then cleared, a corresponding notification is emitted and no new fetch is
839 // started.
840 TEST_F(CloudExternalDataPolicyObserverTest, RegularUserClearSet) {
841 SetRegularUserAvatarPolicy(avatar_policy_1_);
843 CreateObserver();
845 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
846 .Times(1)
847 .WillOnce(SaveArg<1>(&fetch_callback_));
849 LogInAsRegularUser();
851 EXPECT_TRUE(cleared_calls_.empty());
852 EXPECT_TRUE(fetched_calls_.empty());
853 ASSERT_EQ(1u, set_calls_.size());
854 EXPECT_EQ(kRegularUserID, set_calls_.front());
855 ClearObservations();
857 Mock::VerifyAndClear(&external_data_manager_);
858 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
860 SetRegularUserAvatarPolicy("");
862 EXPECT_TRUE(set_calls_.empty());
863 EXPECT_TRUE(fetched_calls_.empty());
864 ASSERT_EQ(1u, cleared_calls_.size());
865 EXPECT_EQ(kRegularUserID, cleared_calls_.front());
866 ClearObservations();
870 // Verifies that when the external data reference for a regular user is not set
871 // while the user is logging in, no notifications are emitted. Further verifies
872 // that when the external data reference is then set, a corresponding
873 // notification is emitted and a fetch is started. Also verifies that when the
874 // fetch eventually succeeds, a notification containing the external data is
875 // emitted.
876 TEST_F(CloudExternalDataPolicyObserverTest, RegularUserSetUnset) {
877 CreateObserver();
879 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
881 LogInAsRegularUser();
883 EXPECT_TRUE(set_calls_.empty());
884 EXPECT_TRUE(cleared_calls_.empty());
885 EXPECT_TRUE(fetched_calls_.empty());
886 ClearObservations();
888 Mock::VerifyAndClear(&external_data_manager_);
889 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
890 .Times(1)
891 .WillOnce(SaveArg<1>(&fetch_callback_));
893 SetRegularUserAvatarPolicy(avatar_policy_1_);
895 EXPECT_TRUE(cleared_calls_.empty());
896 EXPECT_TRUE(fetched_calls_.empty());
897 ASSERT_EQ(1u, set_calls_.size());
898 EXPECT_EQ(kRegularUserID, set_calls_.front());
899 ClearObservations();
901 Mock::VerifyAndClear(&external_data_manager_);
902 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
904 fetch_callback_.Run(make_scoped_ptr(new std::string(avatar_policy_1_data_)));
906 EXPECT_TRUE(set_calls_.empty());
907 EXPECT_TRUE(cleared_calls_.empty());
908 ASSERT_EQ(1u, fetched_calls_.size());
909 EXPECT_EQ(kRegularUserID, fetched_calls_.front().first);
910 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second);
911 ClearObservations();
914 // Verifies that when the external data reference for a regular user is set
915 // while the user is logging in, a corresponding notification is emitted and a
916 // fetch is started. Further verifies that when the external data reference is
917 // then updated, a corresponding notification is emitted and the fetch is
918 // restarted. Also verifies that when the fetch eventually succeeds, a
919 // notification containing the external data is emitted.
920 TEST_F(CloudExternalDataPolicyObserverTest, RegularUserSetSet) {
921 SetRegularUserAvatarPolicy(avatar_policy_1_);
923 CreateObserver();
925 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
926 .Times(1)
927 .WillOnce(SaveArg<1>(&fetch_callback_));
929 LogInAsRegularUser();
931 EXPECT_TRUE(cleared_calls_.empty());
932 EXPECT_TRUE(fetched_calls_.empty());
933 ASSERT_EQ(1u, set_calls_.size());
934 EXPECT_EQ(kRegularUserID, set_calls_.front());
935 ClearObservations();
937 Mock::VerifyAndClear(&external_data_manager_);
938 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _))
939 .Times(1)
940 .WillOnce(SaveArg<1>(&fetch_callback_));
942 SetRegularUserAvatarPolicy(avatar_policy_2_);
944 EXPECT_TRUE(cleared_calls_.empty());
945 EXPECT_TRUE(fetched_calls_.empty());
946 ASSERT_EQ(1u, set_calls_.size());
947 EXPECT_EQ(kRegularUserID, set_calls_.front());
948 ClearObservations();
950 Mock::VerifyAndClear(&external_data_manager_);
951 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0);
953 fetch_callback_.Run(make_scoped_ptr(new std::string(avatar_policy_2_data_)));
955 EXPECT_TRUE(set_calls_.empty());
956 EXPECT_TRUE(cleared_calls_.empty());
957 ASSERT_EQ(1u, fetched_calls_.size());
958 EXPECT_EQ(kRegularUserID, fetched_calls_.front().first);
959 EXPECT_EQ(avatar_policy_2_data_, fetched_calls_.front().second);
960 ClearObservations();
963 } // namespace policy