Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(200)

Side by Side Diff: chrome/browser/chromeos/policy/cloud_external_data_policy_observer_chromeos_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698