cloud_external_data_policy_observer_unittest.cc revision 116680a4aac90f2aa7413d9095a592090648e557
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/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/proto/chrome_device_policy.pb.h" 26#include "chrome/browser/chromeos/settings/device_settings_service.h" 27#include "chrome/browser/chromeos/settings/device_settings_test_helper.h" 28#include "chrome/browser/profiles/profile.h" 29#include "chrome/common/chrome_paths.h" 30#include "chrome/test/base/testing_profile.h" 31#include "components/policy/core/common/cloud/cloud_policy_core.h" 32#include "components/policy/core/common/cloud/cloud_policy_store.h" 33#include "components/policy/core/common/cloud/mock_cloud_external_data_manager.h" 34#include "components/policy/core/common/cloud/policy_builder.h" 35#include "components/policy/core/common/external_data_fetcher.h" 36#include "components/policy/core/common/mock_configuration_policy_provider.h" 37#include "components/policy/core/common/policy_map.h" 38#include "components/policy/core/common/policy_service.h" 39#include "components/policy/core/common/policy_service_impl.h" 40#include "components/policy/core/common/policy_types.h" 41#include "content/public/browser/notification_details.h" 42#include "content/public/browser/notification_service.h" 43#include "content/public/browser/notification_source.h" 44#include "net/url_request/test_url_fetcher_factory.h" 45#include "net/url_request/url_fetcher_delegate.h" 46#include "net/url_request/url_request_context_getter.h" 47#include "net/url_request/url_request_status.h" 48#include "policy/policy_constants.h" 49#include "policy/proto/cloud_policy.pb.h" 50#include "testing/gmock/include/gmock/gmock.h" 51#include "testing/gtest/include/gtest/gtest.h" 52#include "url/gurl.h" 53 54namespace em = enterprise_management; 55 56using ::testing::Mock; 57using ::testing::Return; 58using ::testing::SaveArg; 59using ::testing::_; 60 61namespace policy { 62 63namespace { 64 65const char kDeviceLocalAccount[] = "device_local_account@localhost"; 66 67const char kRegularUserID[] = "user@example.com"; 68 69const char kAvatar1URL[] = "http://localhost/avatar1.jpg"; 70const char kAvatar2URL[] = "http://localhost/avatar2.jpg"; 71 72void ConstructAvatarPolicy(const std::string& file_name, 73 const std::string& url, 74 std::string* policy_data, 75 std::string* policy) { 76 base::FilePath test_data_dir; 77 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir)); 78 ASSERT_TRUE(base::ReadFileToString( 79 test_data_dir.Append("chromeos").Append(file_name), 80 policy_data)); 81 base::JSONWriter::Write( 82 test::ConstructExternalDataReference(url, *policy_data).get(), 83 policy); 84} 85 86} // namespace 87 88class CloudExternalDataPolicyObserverTest 89 : public chromeos::DeviceSettingsTestBase, 90 public CloudExternalDataPolicyObserver::Delegate { 91 public: 92 typedef std::pair<std::string, std::string> FetchedCall; 93 94 CloudExternalDataPolicyObserverTest(); 95 virtual ~CloudExternalDataPolicyObserverTest(); 96 97 // chromeos::DeviceSettingsTestBase: 98 virtual void SetUp() OVERRIDE; 99 virtual void TearDown() OVERRIDE; 100 101 // CloudExternalDataPolicyObserver::Delegate: 102 virtual void OnExternalDataSet(const std::string& policy, 103 const std::string& user_id) OVERRIDE; 104 virtual void OnExternalDataCleared(const std::string& policy, 105 const std::string& user_id) OVERRIDE; 106 virtual void OnExternalDataFetched(const std::string& policy, 107 const std::string& user_id, 108 scoped_ptr<std::string> data) OVERRIDE; 109 110 void CreateObserver(); 111 112 void ClearObservations(); 113 114 void SetDeviceLocalAccountAvatarPolicy(const std::string& account_id, 115 const std::string& value); 116 117 void AddDeviceLocalAccount(const std::string& account_id); 118 void RemoveDeviceLocalAccount(const std::string& account_id); 119 120 DeviceLocalAccountPolicyBroker* GetBrokerForDeviceLocalAccountUser(); 121 122 void RefreshDeviceLocalAccountPolicy(DeviceLocalAccountPolicyBroker* broker); 123 124 void LogInAsDeviceLocalAccount(const std::string& user_id); 125 126 void SetRegularUserAvatarPolicy(const std::string& value); 127 128 void LogInAsRegularUser(); 129 130 const std::string device_local_account_user_id_; 131 132 std::string avatar_policy_1_data_; 133 std::string avatar_policy_2_data_; 134 std::string avatar_policy_1_; 135 std::string avatar_policy_2_; 136 137 chromeos::CrosSettings cros_settings_; 138 scoped_ptr<DeviceLocalAccountPolicyService> 139 device_local_account_policy_service_; 140 net::TestURLFetcherFactory url_fetcher_factory_; 141 142 scoped_ptr<DeviceLocalAccountPolicyProvider> 143 device_local_account_policy_provider_; 144 145 MockCloudExternalDataManager external_data_manager_; 146 MockConfigurationPolicyProvider user_policy_provider_; 147 148 scoped_ptr<TestingProfile> profile_; 149 150 scoped_ptr<CloudExternalDataPolicyObserver> observer_; 151 152 std::vector<std::string> set_calls_; 153 std::vector<std::string> cleared_calls_; 154 std::vector<FetchedCall> fetched_calls_; 155 156 ExternalDataFetcher::FetchCallback fetch_callback_; 157 158 private: 159 DISALLOW_COPY_AND_ASSIGN(CloudExternalDataPolicyObserverTest); 160}; 161 162CloudExternalDataPolicyObserverTest::CloudExternalDataPolicyObserverTest() 163 : device_local_account_user_id_(GenerateDeviceLocalAccountUserId( 164 kDeviceLocalAccount, 165 DeviceLocalAccount::TYPE_PUBLIC_SESSION)), 166 cros_settings_(&device_settings_service_) { 167} 168 169CloudExternalDataPolicyObserverTest::~CloudExternalDataPolicyObserverTest() { 170} 171 172void CloudExternalDataPolicyObserverTest::SetUp() { 173 chromeos::DeviceSettingsTestBase::SetUp(); 174 device_local_account_policy_service_.reset( 175 new DeviceLocalAccountPolicyService(&device_settings_test_helper_, 176 &device_settings_service_, 177 &cros_settings_, 178 base::MessageLoopProxy::current(), 179 base::MessageLoopProxy::current(), 180 base::MessageLoopProxy::current(), 181 base::MessageLoopProxy::current(), 182 NULL)); 183 url_fetcher_factory_.set_remove_fetcher_on_delete(true); 184 185 EXPECT_CALL(user_policy_provider_, IsInitializationComplete(_)) 186 .WillRepeatedly(Return(true)); 187 user_policy_provider_.Init(); 188 189 ConstructAvatarPolicy("avatar1.jpg", 190 kAvatar1URL, 191 &avatar_policy_1_data_, 192 &avatar_policy_1_); 193 ConstructAvatarPolicy("avatar2.jpg", 194 kAvatar2URL, 195 &avatar_policy_2_data_, 196 &avatar_policy_2_); 197} 198 199void CloudExternalDataPolicyObserverTest::TearDown() { 200 observer_.reset(); 201 user_policy_provider_.Shutdown(); 202 profile_.reset(); 203 if (device_local_account_policy_provider_) { 204 device_local_account_policy_provider_->Shutdown(); 205 device_local_account_policy_provider_.reset(); 206 } 207 device_local_account_policy_service_->Shutdown(); 208 device_local_account_policy_service_.reset(); 209 chromeos::DeviceSettingsTestBase::TearDown(); 210} 211 212 213void CloudExternalDataPolicyObserverTest::OnExternalDataSet( 214 const std::string& policy, 215 const std::string& user_id) { 216 EXPECT_EQ(key::kUserAvatarImage, policy); 217 set_calls_.push_back(user_id); 218} 219 220void CloudExternalDataPolicyObserverTest::OnExternalDataCleared( 221 const std::string& policy, 222 const std::string& user_id) { 223 EXPECT_EQ(key::kUserAvatarImage, policy); 224 cleared_calls_.push_back(user_id); 225} 226 227void CloudExternalDataPolicyObserverTest::OnExternalDataFetched( 228 const std::string& policy, 229 const std::string& user_id, 230 scoped_ptr<std::string> data) { 231 EXPECT_EQ(key::kUserAvatarImage, policy); 232 fetched_calls_.push_back(make_pair(user_id, std::string())); 233 fetched_calls_.back().second.swap(*data); 234} 235 236void CloudExternalDataPolicyObserverTest::CreateObserver() { 237 observer_.reset(new CloudExternalDataPolicyObserver( 238 &cros_settings_, 239 device_local_account_policy_service_.get(), 240 key::kUserAvatarImage, 241 this)); 242 observer_->Init(); 243} 244 245void CloudExternalDataPolicyObserverTest::ClearObservations() { 246 set_calls_.clear(); 247 cleared_calls_.clear(); 248 fetched_calls_.clear(); 249} 250 251void CloudExternalDataPolicyObserverTest::SetDeviceLocalAccountAvatarPolicy( 252 const std::string& account_id, 253 const std::string& value) { 254 UserPolicyBuilder builder; 255 builder.policy_data().set_policy_type( 256 dm_protocol::kChromePublicAccountPolicyType); 257 builder.policy_data().set_settings_entity_id(account_id); 258 builder.policy_data().set_username(account_id); 259 if (!value.empty()) 260 builder.payload().mutable_useravatarimage()->set_value(value); 261 builder.Build(); 262 device_settings_test_helper_.set_device_local_account_policy_blob( 263 account_id, 264 builder.GetBlob()); 265} 266 267void CloudExternalDataPolicyObserverTest::AddDeviceLocalAccount( 268 const std::string& account_id) { 269 em::DeviceLocalAccountInfoProto* account = 270 device_policy_.payload().mutable_device_local_accounts()->add_account(); 271 account->set_account_id(account_id); 272 account->set_type( 273 em::DeviceLocalAccountInfoProto::ACCOUNT_TYPE_PUBLIC_SESSION); 274 device_policy_.Build(); 275 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob()); 276 ReloadDeviceSettings(); 277} 278 279void CloudExternalDataPolicyObserverTest::RemoveDeviceLocalAccount( 280 const std::string& account_id) { 281 em::DeviceLocalAccountsProto* accounts = 282 device_policy_.payload().mutable_device_local_accounts(); 283 std::vector<std::string> account_ids; 284 for (int i = 0; i < accounts->account_size(); ++i) { 285 if (accounts->account(i).account_id() != account_id) 286 account_ids.push_back(accounts->account(i).account_id()); 287 } 288 accounts->clear_account(); 289 for (std::vector<std::string>::const_iterator it = account_ids.begin(); 290 it != account_ids.end(); ++it) { 291 em::DeviceLocalAccountInfoProto* account = accounts->add_account(); 292 account->set_account_id(*it); 293 account->set_type( 294 em::DeviceLocalAccountInfoProto::ACCOUNT_TYPE_PUBLIC_SESSION); 295 } 296 device_policy_.Build(); 297 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob()); 298 ReloadDeviceSettings(); 299} 300 301DeviceLocalAccountPolicyBroker* 302 CloudExternalDataPolicyObserverTest::GetBrokerForDeviceLocalAccountUser() { 303 return device_local_account_policy_service_->GetBrokerForUser( 304 device_local_account_user_id_); 305} 306 307void CloudExternalDataPolicyObserverTest::RefreshDeviceLocalAccountPolicy( 308 DeviceLocalAccountPolicyBroker* broker) { 309 broker->core()->store()->Load(); 310 device_settings_test_helper_.Flush(); 311} 312 313void CloudExternalDataPolicyObserverTest::LogInAsDeviceLocalAccount( 314 const std::string& user_id) { 315 device_local_account_policy_provider_.reset( 316 new DeviceLocalAccountPolicyProvider( 317 user_id, 318 device_local_account_policy_service_.get(), 319 scoped_ptr<PolicyMap>())); 320 321 PolicyServiceImpl::Providers providers; 322 providers.push_back(device_local_account_policy_provider_.get()); 323 TestingProfile::Builder builder; 324 builder.SetPolicyService( 325 scoped_ptr<PolicyService>(new PolicyServiceImpl(providers))); 326 327 profile_ = builder.Build(); 328 profile_->set_profile_name(user_id); 329 330 user_manager_->AddUser(user_id); 331 content::NotificationService::current()->Notify( 332 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED, 333 content::NotificationService::AllSources(), 334 content::Details<Profile>(profile_.get())); 335} 336 337void CloudExternalDataPolicyObserverTest::SetRegularUserAvatarPolicy( 338 const std::string& value) { 339 PolicyMap policy_map; 340 if (!value.empty()) { 341 policy_map.Set( 342 key::kUserAvatarImage, 343 POLICY_LEVEL_MANDATORY, 344 POLICY_SCOPE_USER, 345 new base::StringValue(value), 346 external_data_manager_.CreateExternalDataFetcher( 347 key::kUserAvatarImage).release()); 348 } 349 user_policy_provider_.UpdateChromePolicy(policy_map); 350} 351 352void CloudExternalDataPolicyObserverTest::LogInAsRegularUser() { 353 PolicyServiceImpl::Providers providers; 354 providers.push_back(&user_policy_provider_); 355 TestingProfile::Builder builder; 356 builder.SetPolicyService( 357 scoped_ptr<PolicyService>(new PolicyServiceImpl(providers))); 358 359 profile_ = builder.Build(); 360 profile_->set_profile_name(kRegularUserID); 361 362 user_manager_->AddUser(kRegularUserID); 363 content::NotificationService::current()->Notify( 364 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED, 365 content::NotificationService::AllSources(), 366 content::Details<Profile>(profile_.get())); 367} 368 369// Verifies that when an external data reference is set for a device-local 370// account, a corresponding notification is emitted and a fetch is started. 371// Further verifies that when the fetch succeeds, a notification containing the 372// external data is emitted. 373TEST_F(CloudExternalDataPolicyObserverTest, 374 ExistingDeviceLocalAccountFetchSuccess) { 375 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_); 376 AddDeviceLocalAccount(kDeviceLocalAccount); 377 378 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser(); 379 ASSERT_TRUE(broker); 380 broker->external_data_manager()->Connect(NULL); 381 base::RunLoop().RunUntilIdle(); 382 383 CreateObserver(); 384 385 EXPECT_TRUE(cleared_calls_.empty()); 386 EXPECT_TRUE(fetched_calls_.empty()); 387 ASSERT_EQ(1u, set_calls_.size()); 388 EXPECT_EQ(device_local_account_user_id_, set_calls_.front()); 389 ClearObservations(); 390 391 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 392 ASSERT_TRUE(fetcher); 393 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL()); 394 395 fetcher->SetResponseString(avatar_policy_1_data_); 396 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 397 net::OK)); 398 fetcher->set_response_code(200); 399 fetcher->delegate()->OnURLFetchComplete(fetcher); 400 base::RunLoop().RunUntilIdle(); 401 402 EXPECT_TRUE(set_calls_.empty()); 403 EXPECT_TRUE(cleared_calls_.empty()); 404 ASSERT_EQ(1u, fetched_calls_.size()); 405 EXPECT_EQ(device_local_account_user_id_, fetched_calls_.front().first); 406 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second); 407 ClearObservations(); 408 409 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(1)); 410} 411 412// Verifies that when an external data reference is set for a device-local 413// account, a corresponding notification is emitted and a fetch is started. 414// Further verifies that when the fetch fails, no notification is emitted. 415TEST_F(CloudExternalDataPolicyObserverTest, 416 ExistingDeviceLocalAccountFetchFailure) { 417 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_); 418 AddDeviceLocalAccount(kDeviceLocalAccount); 419 420 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser(); 421 ASSERT_TRUE(broker); 422 broker->external_data_manager()->Connect(NULL); 423 base::RunLoop().RunUntilIdle(); 424 425 CreateObserver(); 426 427 EXPECT_TRUE(cleared_calls_.empty()); 428 EXPECT_TRUE(fetched_calls_.empty()); 429 ASSERT_EQ(1u, set_calls_.size()); 430 EXPECT_EQ(device_local_account_user_id_, set_calls_.front()); 431 ClearObservations(); 432 433 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 434 ASSERT_TRUE(fetcher); 435 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL()); 436 437 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 438 net::OK)); 439 fetcher->set_response_code(400); 440 fetcher->delegate()->OnURLFetchComplete(fetcher); 441 base::RunLoop().RunUntilIdle(); 442 443 EXPECT_TRUE(set_calls_.empty()); 444 EXPECT_TRUE(cleared_calls_.empty()); 445 EXPECT_TRUE(fetched_calls_.empty()); 446 ClearObservations(); 447 448 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(1)); 449} 450 451// Verifies that when the external data reference for a device-local account is 452// initially not set, no notifications are emitted. Further verifies that when 453// the external data reference is then cleared (which is a no-op), again, no 454// notifications are emitted. 455TEST_F(CloudExternalDataPolicyObserverTest, 456 ExistingDeviceLocalAccountClearUnset) { 457 AddDeviceLocalAccount(kDeviceLocalAccount); 458 459 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser(); 460 ASSERT_TRUE(broker); 461 broker->external_data_manager()->Connect(NULL); 462 base::RunLoop().RunUntilIdle(); 463 464 CreateObserver(); 465 466 EXPECT_TRUE(set_calls_.empty()); 467 EXPECT_TRUE(cleared_calls_.empty()); 468 EXPECT_TRUE(fetched_calls_.empty()); 469 ClearObservations(); 470 471 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0)); 472 473 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, ""); 474 RefreshDeviceLocalAccountPolicy(broker); 475 476 EXPECT_TRUE(set_calls_.empty()); 477 EXPECT_TRUE(cleared_calls_.empty()); 478 EXPECT_TRUE(fetched_calls_.empty()); 479 ClearObservations(); 480 481 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0)); 482} 483 484// Verifies that when the external data reference for a device-local account is 485// initially set, a corresponding notification is emitted and a fetch is 486// started. Further verifies that when the external data reference is then 487// cleared, a corresponding notification is emitted and the fetch is canceled. 488TEST_F(CloudExternalDataPolicyObserverTest, 489 ExistingDeviceLocalAccountClearSet) { 490 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_); 491 AddDeviceLocalAccount(kDeviceLocalAccount); 492 493 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser(); 494 ASSERT_TRUE(broker); 495 broker->external_data_manager()->Connect(NULL); 496 base::RunLoop().RunUntilIdle(); 497 498 CreateObserver(); 499 500 EXPECT_TRUE(cleared_calls_.empty()); 501 EXPECT_TRUE(fetched_calls_.empty()); 502 ASSERT_EQ(1u, set_calls_.size()); 503 EXPECT_EQ(device_local_account_user_id_, set_calls_.front()); 504 ClearObservations(); 505 506 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 507 ASSERT_TRUE(fetcher); 508 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL()); 509 510 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, ""); 511 RefreshDeviceLocalAccountPolicy(broker); 512 513 EXPECT_TRUE(set_calls_.empty()); 514 EXPECT_TRUE(fetched_calls_.empty()); 515 ASSERT_EQ(1u, cleared_calls_.size()); 516 EXPECT_EQ(device_local_account_user_id_, cleared_calls_.front()); 517 ClearObservations(); 518 519 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0)); 520} 521 522// Verifies that when the external data reference for a device-local account is 523// initially not set, no notifications are emitted. Further verifies that when 524// the external data reference is then set, a corresponding notification is 525// emitted and a fetch is started. Also verifies that when the fetch eventually 526// succeeds, a notification containing the external data is emitted. 527TEST_F(CloudExternalDataPolicyObserverTest, 528 ExistingDeviceLocalAccountSetUnset) { 529 AddDeviceLocalAccount(kDeviceLocalAccount); 530 531 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser(); 532 ASSERT_TRUE(broker); 533 broker->external_data_manager()->Connect(NULL); 534 base::RunLoop().RunUntilIdle(); 535 536 CreateObserver(); 537 538 EXPECT_TRUE(set_calls_.empty()); 539 EXPECT_TRUE(cleared_calls_.empty()); 540 EXPECT_TRUE(fetched_calls_.empty()); 541 ClearObservations(); 542 543 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_); 544 RefreshDeviceLocalAccountPolicy(broker); 545 546 EXPECT_TRUE(cleared_calls_.empty()); 547 EXPECT_TRUE(fetched_calls_.empty()); 548 ASSERT_EQ(1u, set_calls_.size()); 549 EXPECT_EQ(device_local_account_user_id_, set_calls_.front()); 550 ClearObservations(); 551 552 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 553 ASSERT_TRUE(fetcher); 554 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL()); 555 556 fetcher->SetResponseString(avatar_policy_1_data_); 557 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 558 net::OK)); 559 fetcher->set_response_code(200); 560 fetcher->delegate()->OnURLFetchComplete(fetcher); 561 base::RunLoop().RunUntilIdle(); 562 563 EXPECT_TRUE(set_calls_.empty()); 564 EXPECT_TRUE(cleared_calls_.empty()); 565 ASSERT_EQ(1u, fetched_calls_.size()); 566 EXPECT_EQ(device_local_account_user_id_, fetched_calls_.front().first); 567 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second); 568 ClearObservations(); 569 570 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(1)); 571} 572 573// Verifies that when the external data reference for a device-local account is 574// initially set, a corresponding notification is emitted and a fetch is 575// started. Further verifies that when the external data reference is then 576// updated, a corresponding notification is emitted and the fetch is restarted. 577// Also verifies that when the fetch eventually succeeds, a notification 578// containing the external data is emitted. 579TEST_F(CloudExternalDataPolicyObserverTest, ExistingDeviceLocalAccountSetSet) { 580 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_); 581 AddDeviceLocalAccount(kDeviceLocalAccount); 582 583 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser(); 584 ASSERT_TRUE(broker); 585 broker->external_data_manager()->Connect(NULL); 586 base::RunLoop().RunUntilIdle(); 587 588 CreateObserver(); 589 590 EXPECT_TRUE(cleared_calls_.empty()); 591 EXPECT_TRUE(fetched_calls_.empty()); 592 ASSERT_EQ(1u, set_calls_.size()); 593 EXPECT_EQ(device_local_account_user_id_, set_calls_.front()); 594 ClearObservations(); 595 596 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 597 ASSERT_TRUE(fetcher); 598 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL()); 599 600 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_2_); 601 RefreshDeviceLocalAccountPolicy(broker); 602 603 EXPECT_TRUE(cleared_calls_.empty()); 604 EXPECT_TRUE(fetched_calls_.empty()); 605 ASSERT_EQ(1u, set_calls_.size()); 606 EXPECT_EQ(device_local_account_user_id_, set_calls_.front()); 607 ClearObservations(); 608 609 fetcher = url_fetcher_factory_.GetFetcherByID(1); 610 ASSERT_TRUE(fetcher); 611 EXPECT_EQ(GURL(kAvatar2URL), fetcher->GetOriginalURL()); 612 613 fetcher->SetResponseString(avatar_policy_2_data_); 614 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 615 net::OK)); 616 fetcher->set_response_code(200); 617 fetcher->delegate()->OnURLFetchComplete(fetcher); 618 base::RunLoop().RunUntilIdle(); 619 620 EXPECT_TRUE(set_calls_.empty()); 621 EXPECT_TRUE(cleared_calls_.empty()); 622 ASSERT_EQ(1u, fetched_calls_.size()); 623 EXPECT_EQ(device_local_account_user_id_, fetched_calls_.front().first); 624 EXPECT_EQ(avatar_policy_2_data_, fetched_calls_.front().second); 625 ClearObservations(); 626 627 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(2)); 628} 629 630// Verifies that when the external data reference for a device-local account is 631// initially not set, no notifications are emitted during login into the 632// account. Further verifies that when the external data reference is then set, 633// a corresponding notification is emitted only once and a fetch is started. 634// Also verifies that when the fetch eventually succeeds, a notification 635// containing the external data is emitted, again, only once. 636TEST_F(CloudExternalDataPolicyObserverTest, 637 ExistingDeviceLocalAccountSetAfterLogin) { 638 AddDeviceLocalAccount(kDeviceLocalAccount); 639 640 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser(); 641 ASSERT_TRUE(broker); 642 broker->external_data_manager()->Connect(NULL); 643 base::RunLoop().RunUntilIdle(); 644 645 CreateObserver(); 646 647 LogInAsDeviceLocalAccount(kDeviceLocalAccount); 648 649 EXPECT_TRUE(set_calls_.empty()); 650 EXPECT_TRUE(cleared_calls_.empty()); 651 EXPECT_TRUE(fetched_calls_.empty()); 652 ClearObservations(); 653 654 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_); 655 RefreshDeviceLocalAccountPolicy(broker); 656 657 EXPECT_TRUE(cleared_calls_.empty()); 658 EXPECT_TRUE(fetched_calls_.empty()); 659 ASSERT_EQ(1u, set_calls_.size()); 660 EXPECT_EQ(device_local_account_user_id_, set_calls_.front()); 661 ClearObservations(); 662 663 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 664 ASSERT_TRUE(fetcher); 665 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL()); 666 667 fetcher->SetResponseString(avatar_policy_1_data_); 668 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 669 net::OK)); 670 fetcher->set_response_code(200); 671 fetcher->delegate()->OnURLFetchComplete(fetcher); 672 base::RunLoop().RunUntilIdle(); 673 674 EXPECT_TRUE(set_calls_.empty()); 675 EXPECT_TRUE(cleared_calls_.empty()); 676 ASSERT_EQ(1u, fetched_calls_.size()); 677 EXPECT_EQ(device_local_account_user_id_, fetched_calls_.front().first); 678 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second); 679 ClearObservations(); 680 681 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(1)); 682} 683 684// Verifies that when the external data reference for a device-local account is 685// initially not set, no notifications are emitted. Further verifies that when 686// the device-local account is then removed, again, no notifications are sent. 687TEST_F(CloudExternalDataPolicyObserverTest, 688 ExistingDeviceLocalAccountRemoveAccountUnset) { 689 AddDeviceLocalAccount(kDeviceLocalAccount); 690 691 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser(); 692 ASSERT_TRUE(broker); 693 broker->external_data_manager()->Connect(NULL); 694 base::RunLoop().RunUntilIdle(); 695 696 CreateObserver(); 697 698 EXPECT_TRUE(set_calls_.empty()); 699 EXPECT_TRUE(cleared_calls_.empty()); 700 EXPECT_TRUE(fetched_calls_.empty()); 701 ClearObservations(); 702 703 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0)); 704 705 RemoveDeviceLocalAccount(kDeviceLocalAccount); 706 707 EXPECT_TRUE(set_calls_.empty()); 708 EXPECT_TRUE(cleared_calls_.empty()); 709 EXPECT_TRUE(fetched_calls_.empty()); 710 ClearObservations(); 711 712 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0)); 713} 714 715// Verifies that when the external data reference for a device-local account is 716// initially set, a corresponding notification is emitted and a fetch is 717// started. Further verifies that when the device-local account is then removed, 718// a notification indicating that the external data reference has been cleared 719// is emitted and the fetch is canceled. 720TEST_F(CloudExternalDataPolicyObserverTest, 721 ExistingDeviceLocalAccountRemoveAccountSet) { 722 SetDeviceLocalAccountAvatarPolicy(kDeviceLocalAccount, avatar_policy_1_); 723 AddDeviceLocalAccount(kDeviceLocalAccount); 724 725 DeviceLocalAccountPolicyBroker* broker = GetBrokerForDeviceLocalAccountUser(); 726 ASSERT_TRUE(broker); 727 broker->external_data_manager()->Connect(NULL); 728 base::RunLoop().RunUntilIdle(); 729 730 CreateObserver(); 731 732 EXPECT_TRUE(cleared_calls_.empty()); 733 EXPECT_TRUE(fetched_calls_.empty()); 734 ASSERT_EQ(1u, set_calls_.size()); 735 EXPECT_EQ(device_local_account_user_id_, set_calls_.front()); 736 ClearObservations(); 737 738 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 739 ASSERT_TRUE(fetcher); 740 EXPECT_EQ(GURL(kAvatar1URL), fetcher->GetOriginalURL()); 741 742 RemoveDeviceLocalAccount(kDeviceLocalAccount); 743 744 EXPECT_TRUE(set_calls_.empty()); 745 EXPECT_TRUE(fetched_calls_.empty()); 746 ASSERT_EQ(1u, cleared_calls_.size()); 747 EXPECT_EQ(device_local_account_user_id_, cleared_calls_.front()); 748 ClearObservations(); 749 750 EXPECT_FALSE(url_fetcher_factory_.GetFetcherByID(0)); 751} 752 753// Verifies that when an external data reference is set for a regular user and 754// the user logs in, a corresponding notification is emitted and a fetch is 755// started. Further verifies that when the fetch succeeds, a notification 756// containing the external data is emitted. 757TEST_F(CloudExternalDataPolicyObserverTest, RegularUserFetchSuccess) { 758 SetRegularUserAvatarPolicy(avatar_policy_1_); 759 760 CreateObserver(); 761 762 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)) 763 .Times(1) 764 .WillOnce(SaveArg<1>(&fetch_callback_)); 765 766 LogInAsRegularUser(); 767 768 EXPECT_TRUE(cleared_calls_.empty()); 769 EXPECT_TRUE(fetched_calls_.empty()); 770 ASSERT_EQ(1u, set_calls_.size()); 771 EXPECT_EQ(kRegularUserID, set_calls_.front()); 772 ClearObservations(); 773 774 Mock::VerifyAndClear(&external_data_manager_); 775 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0); 776 777 fetch_callback_.Run(make_scoped_ptr(new std::string(avatar_policy_1_data_))); 778 779 EXPECT_TRUE(set_calls_.empty()); 780 EXPECT_TRUE(cleared_calls_.empty()); 781 ASSERT_EQ(1u, fetched_calls_.size()); 782 EXPECT_EQ(kRegularUserID, fetched_calls_.front().first); 783 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second); 784 ClearObservations(); 785} 786 787// Verifies that when the external data reference for a regular user is not set 788// while the user is logging in, no notifications are emitted. Further verifies 789// that when the external data reference is then cleared (which is a no-op), 790// again, no notifications are emitted. 791TEST_F(CloudExternalDataPolicyObserverTest, RegularUserClearUnset) { 792 CreateObserver(); 793 794 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0); 795 796 LogInAsRegularUser(); 797 798 EXPECT_TRUE(set_calls_.empty()); 799 EXPECT_TRUE(cleared_calls_.empty()); 800 EXPECT_TRUE(fetched_calls_.empty()); 801 ClearObservations(); 802 803 Mock::VerifyAndClear(&external_data_manager_); 804 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0); 805 806 SetRegularUserAvatarPolicy(""); 807 808 EXPECT_TRUE(set_calls_.empty()); 809 EXPECT_TRUE(cleared_calls_.empty()); 810 EXPECT_TRUE(fetched_calls_.empty()); 811 ClearObservations(); 812} 813 814// Verifies that when the external data reference for a regular user is set 815// while the user is logging in, a corresponding notification is emitted and a 816// fetch is started. Further verifies that when the external data reference is 817// then cleared, a corresponding notification is emitted and no new fetch is 818// started. 819TEST_F(CloudExternalDataPolicyObserverTest, RegularUserClearSet) { 820 SetRegularUserAvatarPolicy(avatar_policy_1_); 821 822 CreateObserver(); 823 824 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)) 825 .Times(1) 826 .WillOnce(SaveArg<1>(&fetch_callback_)); 827 828 LogInAsRegularUser(); 829 830 EXPECT_TRUE(cleared_calls_.empty()); 831 EXPECT_TRUE(fetched_calls_.empty()); 832 ASSERT_EQ(1u, set_calls_.size()); 833 EXPECT_EQ(kRegularUserID, set_calls_.front()); 834 ClearObservations(); 835 836 Mock::VerifyAndClear(&external_data_manager_); 837 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0); 838 839 SetRegularUserAvatarPolicy(""); 840 841 EXPECT_TRUE(set_calls_.empty()); 842 EXPECT_TRUE(fetched_calls_.empty()); 843 ASSERT_EQ(1u, cleared_calls_.size()); 844 EXPECT_EQ(kRegularUserID, cleared_calls_.front()); 845 ClearObservations(); 846} 847 848 849// Verifies that when the external data reference for a regular user is not set 850// while the user is logging in, no notifications are emitted. Further verifies 851// that when the external data reference is then set, a corresponding 852// notification is emitted and a fetch is started. Also verifies that when the 853// fetch eventually succeeds, a notification containing the external data is 854// emitted. 855TEST_F(CloudExternalDataPolicyObserverTest, RegularUserSetUnset) { 856 CreateObserver(); 857 858 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0); 859 860 LogInAsRegularUser(); 861 862 EXPECT_TRUE(set_calls_.empty()); 863 EXPECT_TRUE(cleared_calls_.empty()); 864 EXPECT_TRUE(fetched_calls_.empty()); 865 ClearObservations(); 866 867 Mock::VerifyAndClear(&external_data_manager_); 868 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)) 869 .Times(1) 870 .WillOnce(SaveArg<1>(&fetch_callback_)); 871 872 SetRegularUserAvatarPolicy(avatar_policy_1_); 873 874 EXPECT_TRUE(cleared_calls_.empty()); 875 EXPECT_TRUE(fetched_calls_.empty()); 876 ASSERT_EQ(1u, set_calls_.size()); 877 EXPECT_EQ(kRegularUserID, set_calls_.front()); 878 ClearObservations(); 879 880 Mock::VerifyAndClear(&external_data_manager_); 881 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0); 882 883 fetch_callback_.Run(make_scoped_ptr(new std::string(avatar_policy_1_data_))); 884 885 EXPECT_TRUE(set_calls_.empty()); 886 EXPECT_TRUE(cleared_calls_.empty()); 887 ASSERT_EQ(1u, fetched_calls_.size()); 888 EXPECT_EQ(kRegularUserID, fetched_calls_.front().first); 889 EXPECT_EQ(avatar_policy_1_data_, fetched_calls_.front().second); 890 ClearObservations(); 891} 892 893// Verifies that when the external data reference for a regular user is set 894// while the user is logging in, a corresponding notification is emitted and a 895// fetch is started. Further verifies that when the external data reference is 896// then updated, a corresponding notification is emitted and the fetch is 897// restarted. Also verifies that when the fetch eventually succeeds, a 898// notification containing the external data is emitted. 899TEST_F(CloudExternalDataPolicyObserverTest, RegularUserSetSet) { 900 SetRegularUserAvatarPolicy(avatar_policy_1_); 901 902 CreateObserver(); 903 904 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)) 905 .Times(1) 906 .WillOnce(SaveArg<1>(&fetch_callback_)); 907 908 LogInAsRegularUser(); 909 910 EXPECT_TRUE(cleared_calls_.empty()); 911 EXPECT_TRUE(fetched_calls_.empty()); 912 ASSERT_EQ(1u, set_calls_.size()); 913 EXPECT_EQ(kRegularUserID, set_calls_.front()); 914 ClearObservations(); 915 916 Mock::VerifyAndClear(&external_data_manager_); 917 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)) 918 .Times(1) 919 .WillOnce(SaveArg<1>(&fetch_callback_)); 920 921 SetRegularUserAvatarPolicy(avatar_policy_2_); 922 923 EXPECT_TRUE(cleared_calls_.empty()); 924 EXPECT_TRUE(fetched_calls_.empty()); 925 ASSERT_EQ(1u, set_calls_.size()); 926 EXPECT_EQ(kRegularUserID, set_calls_.front()); 927 ClearObservations(); 928 929 Mock::VerifyAndClear(&external_data_manager_); 930 EXPECT_CALL(external_data_manager_, Fetch(key::kUserAvatarImage, _)).Times(0); 931 932 fetch_callback_.Run(make_scoped_ptr(new std::string(avatar_policy_2_data_))); 933 934 EXPECT_TRUE(set_calls_.empty()); 935 EXPECT_TRUE(cleared_calls_.empty()); 936 ASSERT_EQ(1u, fetched_calls_.size()); 937 EXPECT_EQ(kRegularUserID, fetched_calls_.front().first); 938 EXPECT_EQ(avatar_policy_2_data_, fetched_calls_.front().second); 939 ClearObservations(); 940} 941 942} // namespace policy 943