Elim cr-checkbox
[chromium-blink-merge.git] / chrome / browser / chromeos / login / users / avatar / user_image_manager_browsertest.cc
blobb3ceb68823e0975284a0bf2f1057b41b1ad5d0b6
1 // Copyright 2014 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 <map>
6 #include <string>
7 #include <vector>
9 #include "base/basictypes.h"
10 #include "base/compiler_specific.h"
11 #include "base/files/file_path.h"
12 #include "base/files/file_util.h"
13 #include "base/json/json_writer.h"
14 #include "base/memory/linked_ptr.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/ref_counted_memory.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/path_service.h"
19 #include "base/prefs/pref_change_registrar.h"
20 #include "base/prefs/pref_service.h"
21 #include "base/prefs/scoped_user_pref_update.h"
22 #include "base/run_loop.h"
23 #include "base/thread_task_runner_handle.h"
24 #include "base/time/time.h"
25 #include "base/values.h"
26 #include "chrome/browser/browser_process.h"
27 #include "chrome/browser/chrome_notification_types.h"
28 #include "chrome/browser/chromeos/login/login_manager_test.h"
29 #include "chrome/browser/chromeos/login/startup_utils.h"
30 #include "chrome/browser/chromeos/login/users/avatar/user_image_manager.h"
31 #include "chrome/browser/chromeos/login/users/avatar/user_image_manager_impl.h"
32 #include "chrome/browser/chromeos/login/users/avatar/user_image_manager_test_util.h"
33 #include "chrome/browser/chromeos/login/users/chrome_user_manager.h"
34 #include "chrome/browser/chromeos/login/users/mock_user_manager.h"
35 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
36 #include "chrome/browser/chromeos/policy/cloud_external_data_manager_base_test_util.h"
37 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h"
38 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chromeos.h"
39 #include "chrome/browser/chromeos/profiles/profile_helper.h"
40 #include "chrome/browser/profiles/profile.h"
41 #include "chrome/browser/profiles/profile_downloader.h"
42 #include "chrome/browser/signin/account_tracker_service_factory.h"
43 #include "chrome/common/chrome_paths.h"
44 #include "chrome/test/base/in_process_browser_test.h"
45 #include "chrome/test/base/testing_browser_process.h"
46 #include "chromeos/chromeos_paths.h"
47 #include "chromeos/dbus/cryptohome_client.h"
48 #include "chromeos/dbus/dbus_thread_manager.h"
49 #include "chromeos/dbus/fake_session_manager_client.h"
50 #include "chromeos/dbus/session_manager_client.h"
51 #include "components/policy/core/common/cloud/cloud_policy_core.h"
52 #include "components/policy/core/common/cloud/cloud_policy_store.h"
53 #include "components/policy/core/common/cloud/policy_builder.h"
54 #include "components/user_manager/user.h"
55 #include "components/user_manager/user_image/default_user_images.h"
56 #include "components/user_manager/user_image/user_image.h"
57 #include "components/user_manager/user_manager.h"
58 #include "content/public/browser/notification_service.h"
59 #include "content/public/browser/notification_source.h"
60 #include "content/public/test/test_utils.h"
61 #include "crypto/rsa_private_key.h"
62 #include "google_apis/gaia/gaia_oauth_client.h"
63 #include "google_apis/gaia/oauth2_token_service.h"
64 #include "net/test/embedded_test_server/embedded_test_server.h"
65 #include "net/url_request/test_url_fetcher_factory.h"
66 #include "net/url_request/url_fetcher_delegate.h"
67 #include "net/url_request/url_request_status.h"
68 #include "policy/proto/cloud_policy.pb.h"
69 #include "testing/gtest/include/gtest/gtest.h"
70 #include "third_party/skia/include/core/SkBitmap.h"
71 #include "ui/base/layout.h"
72 #include "ui/base/resource/resource_bundle.h"
73 #include "ui/gfx/image/image_skia.h"
74 #include "url/gurl.h"
76 namespace chromeos {
78 namespace {
80 // Because policy is not needed in some tests it is better to use e-mails that
81 // are definitely not enterprise. This lets us to avoid faking of policy fetch
82 // procedure.
83 const char kTestUser1[] = "test-user@gmail.com";
84 const char kTestUser2[] = "test-user2@gmail.com";
86 policy::CloudPolicyStore* GetStoreForUser(const user_manager::User* user) {
87 Profile* profile = ProfileHelper::Get()->GetProfileByUserUnsafe(user);
88 if (!profile) {
89 ADD_FAILURE();
90 return NULL;
92 policy::UserCloudPolicyManagerChromeOS* policy_manager =
93 policy::UserCloudPolicyManagerFactoryChromeOS::GetForProfile(profile);
94 if (!policy_manager) {
95 ADD_FAILURE();
96 return NULL;
98 return policy_manager->core()->store();
101 } // namespace
103 class UserImageManagerTest : public LoginManagerTest,
104 public user_manager::UserManager::Observer {
105 protected:
106 UserImageManagerTest() : LoginManagerTest(true) {
109 // LoginManagerTest overrides:
110 void SetUpInProcessBrowserTestFixture() override {
111 LoginManagerTest::SetUpInProcessBrowserTestFixture();
113 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_));
114 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir_));
117 void SetUpOnMainThread() override {
118 LoginManagerTest::SetUpOnMainThread();
119 local_state_ = g_browser_process->local_state();
120 user_manager::UserManager::Get()->AddObserver(this);
123 void TearDownOnMainThread() override {
124 user_manager::UserManager::Get()->RemoveObserver(this);
125 LoginManagerTest::TearDownOnMainThread();
128 // UserManager::Observer overrides:
129 void LocalStateChanged(user_manager::UserManager* user_manager) override {
130 if (run_loop_)
131 run_loop_->Quit();
134 // Logs in |username|.
135 void LogIn(const std::string& username) {
136 user_manager::UserManager::Get()->UserLoggedIn(username, username, false);
139 // Stores old (pre-migration) user image info.
140 void SetOldUserImageInfo(const std::string& username,
141 int image_index,
142 const base::FilePath& image_path) {
143 RegisterUser(username);
144 DictionaryPrefUpdate images_pref(local_state_, "UserImages");
145 base::DictionaryValue* image_properties = new base::DictionaryValue();
146 image_properties->Set("index", new base::FundamentalValue(image_index));
147 image_properties->Set(
148 "path" , new base::StringValue(image_path.value()));
149 images_pref->SetWithoutPathExpansion(username, image_properties);
152 // Verifies user image info in |images_pref| dictionary.
153 void ExpectUserImageInfo(const base::DictionaryValue* images_pref,
154 const std::string& username,
155 int image_index,
156 const base::FilePath& image_path) {
157 ASSERT_TRUE(images_pref);
158 const base::DictionaryValue* image_properties = NULL;
159 images_pref->GetDictionaryWithoutPathExpansion(username, &image_properties);
160 ASSERT_TRUE(image_properties);
161 int actual_image_index;
162 std::string actual_image_path;
163 ASSERT_TRUE(image_properties->GetInteger("index", &actual_image_index) &&
164 image_properties->GetString("path", &actual_image_path));
165 EXPECT_EQ(image_index, actual_image_index);
166 EXPECT_EQ(image_path.value(), actual_image_path);
169 // Verifies that there is no image info for |username| in dictionary
170 // |images_pref|.
171 void ExpectNoUserImageInfo(const base::DictionaryValue* images_pref,
172 const std::string& username) {
173 ASSERT_TRUE(images_pref);
174 const base::DictionaryValue* image_properties = NULL;
175 images_pref->GetDictionaryWithoutPathExpansion(username, &image_properties);
176 ASSERT_FALSE(image_properties);
179 // Verifies that old user image info matches |image_index| and |image_path|
180 // and that new user image info does not exist.
181 void ExpectOldUserImageInfo(const std::string& username,
182 int image_index,
183 const base::FilePath& image_path) {
184 ExpectUserImageInfo(local_state_->GetDictionary("UserImages"),
185 username, image_index, image_path);
186 ExpectNoUserImageInfo(local_state_->GetDictionary("user_image_info"),
187 username);
190 // Verifies that new user image info matches |image_index| and |image_path|
191 // and that old user image info does not exist.
192 void ExpectNewUserImageInfo(const std::string& username,
193 int image_index,
194 const base::FilePath& image_path) {
195 ExpectUserImageInfo(local_state_->GetDictionary("user_image_info"),
196 username, image_index, image_path);
197 ExpectNoUserImageInfo(local_state_->GetDictionary("UserImages"),
198 username);
201 // Sets bitmap |resource_id| as image for |username| and saves it to disk.
202 void SaveUserImagePNG(const std::string& username,
203 int resource_id) {
204 base::FilePath image_path = GetUserImagePath(username, "png");
205 scoped_refptr<base::RefCountedStaticMemory> image_data(
206 ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale(
207 resource_id, ui::SCALE_FACTOR_100P));
208 int written = base::WriteFile(
209 image_path,
210 reinterpret_cast<const char*>(image_data->front()),
211 image_data->size());
212 EXPECT_EQ(static_cast<int>(image_data->size()), written);
213 SetOldUserImageInfo(
214 username, user_manager::User::USER_IMAGE_EXTERNAL, image_path);
217 // Returns the image path for user |username| with specified |extension|.
218 base::FilePath GetUserImagePath(const std::string& username,
219 const std::string& extension) {
220 return user_data_dir_.Append(username).AddExtension(extension);
223 // Seeds the AccountTrackerService with test data so the ProfileDownloader can
224 // retrieve the picture URL and fetch the image.
225 void SeedAccountTrackerService(const std::string& username,
226 Profile* profile) {
227 AccountInfo info;
228 info.account_id = std::string();
229 info.gaia = username;
230 info.email = username;
231 info.full_name = username;
232 info.given_name = username;
233 info.hosted_domain = std::string();
234 info.locale = username;
235 info.picture_url = "http://localhost/avatar.jpg";
236 info.is_child_account = false;
238 AccountTrackerServiceFactory::GetForProfile(profile)->SeedAccountInfo(info);
241 // Completes the download of all non-image profile data for the user
242 // |username|. This method must only be called after a profile data
243 // download has been started. |url_fetcher_factory| will capture
244 // the net::TestURLFetcher created by the ProfileDownloader to
245 // download the profile image.
246 void CompleteProfileMetadataDownload(
247 const std::string& username,
248 net::TestURLFetcherFactory* url_fetcher_factory) {
249 ProfileDownloader* profile_downloader =
250 reinterpret_cast<UserImageManagerImpl*>(
251 ChromeUserManager::Get()->GetUserImageManager(username))
252 ->profile_downloader_.get();
253 ASSERT_TRUE(profile_downloader);
255 static_cast<OAuth2TokenService::Consumer*>(profile_downloader)->
256 OnGetTokenSuccess(NULL,
257 std::string(),
258 base::Time::Now() + base::TimeDelta::FromDays(1));
261 // Completes the download of the currently logged-in user's profile image.
262 // This method must only be called after a profile data download including
263 // the profile image has been started, the download of all non-image data has
264 // been completed by calling CompleteProfileMetadataDownload() and the
265 // net::TestURLFetcher created by the ProfileDownloader to download the
266 // profile image has been captured by |url_fetcher_factory|.
267 void CompleteProfileImageDownload(
268 net::TestURLFetcherFactory* url_fetcher_factory) {
269 std::string profile_image_data;
270 base::FilePath test_data_dir;
271 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir));
272 EXPECT_TRUE(ReadFileToString(
273 test_data_dir.Append("chromeos").Append("avatar1.jpg"),
274 &profile_image_data));
276 base::RunLoop run_loop;
277 PrefChangeRegistrar pref_change_registrar;
278 pref_change_registrar.Init(local_state_);
279 pref_change_registrar.Add("UserDisplayName", run_loop.QuitClosure());
280 net::TestURLFetcher* fetcher = url_fetcher_factory->GetFetcherByID(0);
281 ASSERT_TRUE(fetcher);
282 fetcher->SetResponseString(profile_image_data);
283 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
284 net::OK));
285 fetcher->set_response_code(200);
286 fetcher->delegate()->OnURLFetchComplete(fetcher);
287 run_loop.Run();
289 const user_manager::User* user =
290 user_manager::UserManager::Get()->GetLoggedInUser();
291 ASSERT_TRUE(user);
292 UserImageManagerImpl* uim = reinterpret_cast<UserImageManagerImpl*>(
293 ChromeUserManager::Get()->GetUserImageManager(user->email()));
294 if (uim->job_.get()) {
295 run_loop_.reset(new base::RunLoop);
296 run_loop_->Run();
300 base::FilePath test_data_dir_;
301 base::FilePath user_data_dir_;
303 PrefService* local_state_;
305 scoped_ptr<gfx::ImageSkia> decoded_image_;
307 scoped_ptr<base::RunLoop> run_loop_;
309 private:
310 DISALLOW_COPY_AND_ASSIGN(UserImageManagerTest);
313 IN_PROC_BROWSER_TEST_F(UserImageManagerTest, PRE_DefaultUserImagePreserved) {
314 // Setup an old default (stock) user image.
315 ScopedUserManagerEnabler(new MockUserManager);
316 SetOldUserImageInfo(
317 kTestUser1, user_manager::kFirstDefaultImageIndex, base::FilePath());
320 IN_PROC_BROWSER_TEST_F(UserImageManagerTest, DefaultUserImagePreserved) {
321 user_manager::UserManager::Get()->GetUsers(); // Load users.
322 // Old info preserved.
323 ExpectOldUserImageInfo(
324 kTestUser1, user_manager::kFirstDefaultImageIndex, base::FilePath());
325 LogIn(kTestUser1);
326 // Image info is migrated now.
327 ExpectNewUserImageInfo(
328 kTestUser1, user_manager::kFirstDefaultImageIndex, base::FilePath());
331 IN_PROC_BROWSER_TEST_F(UserImageManagerTest, PRE_OtherUsersUnaffected) {
332 // Setup two users with stock images.
333 ScopedUserManagerEnabler(new MockUserManager);
334 SetOldUserImageInfo(
335 kTestUser1, user_manager::kFirstDefaultImageIndex, base::FilePath());
336 SetOldUserImageInfo(
337 kTestUser2, user_manager::kFirstDefaultImageIndex + 1, base::FilePath());
340 IN_PROC_BROWSER_TEST_F(UserImageManagerTest, OtherUsersUnaffected) {
341 user_manager::UserManager::Get()->GetUsers(); // Load users.
342 // Old info preserved.
343 ExpectOldUserImageInfo(
344 kTestUser1, user_manager::kFirstDefaultImageIndex, base::FilePath());
345 ExpectOldUserImageInfo(
346 kTestUser2, user_manager::kFirstDefaultImageIndex + 1, base::FilePath());
347 LogIn(kTestUser1);
348 // Image info is migrated for the first user and unaffected for the rest.
349 ExpectNewUserImageInfo(
350 kTestUser1, user_manager::kFirstDefaultImageIndex, base::FilePath());
351 ExpectOldUserImageInfo(
352 kTestUser2, user_manager::kFirstDefaultImageIndex + 1, base::FilePath());
355 IN_PROC_BROWSER_TEST_F(UserImageManagerTest, PRE_PRE_NonJPEGImageFromFile) {
356 // Setup a user with non-JPEG image.
357 ScopedUserManagerEnabler(new MockUserManager);
358 SaveUserImagePNG(kTestUser1,
359 user_manager::kDefaultImageResourceIDs
360 [user_manager::kFirstDefaultImageIndex]);
363 IN_PROC_BROWSER_TEST_F(UserImageManagerTest, PRE_NonJPEGImageFromFile) {
364 user_manager::UserManager::Get()->GetUsers(); // Load users.
365 // Old info preserved.
366 ExpectOldUserImageInfo(kTestUser1,
367 user_manager::User::USER_IMAGE_EXTERNAL,
368 GetUserImagePath(kTestUser1, "png"));
369 const user_manager::User* user =
370 user_manager::UserManager::Get()->FindUser(kTestUser1);
371 EXPECT_TRUE(user->image_is_stub());
373 base::RunLoop run_loop;
374 PrefChangeRegistrar pref_change_registrar_;
375 pref_change_registrar_.Init(local_state_);
376 pref_change_registrar_.Add("UserImages", run_loop.QuitClosure());
377 LogIn(kTestUser1);
379 // Wait for migration.
380 run_loop.Run();
382 // Image info is migrated and the image is converted to JPG.
383 ExpectNewUserImageInfo(kTestUser1,
384 user_manager::User::USER_IMAGE_EXTERNAL,
385 GetUserImagePath(kTestUser1, "jpg"));
386 user = user_manager::UserManager::Get()->GetLoggedInUser();
387 ASSERT_TRUE(user);
388 EXPECT_FALSE(user->image_is_safe_format());
389 // Check image dimensions.
390 const gfx::ImageSkia& saved_image =
391 user_manager::GetDefaultImage(user_manager::kFirstDefaultImageIndex);
392 EXPECT_EQ(saved_image.width(), user->GetImage().width());
393 EXPECT_EQ(saved_image.height(), user->GetImage().height());
396 IN_PROC_BROWSER_TEST_F(UserImageManagerTest, NonJPEGImageFromFile) {
397 user_manager::UserManager::Get()->GetUsers(); // Load users.
398 const user_manager::User* user =
399 user_manager::UserManager::Get()->FindUser(kTestUser1);
400 ASSERT_TRUE(user);
401 // Wait for image load.
402 if (user->image_index() == user_manager::User::USER_IMAGE_INVALID) {
403 content::WindowedNotificationObserver(
404 chrome::NOTIFICATION_LOGIN_USER_IMAGE_CHANGED,
405 content::NotificationService::AllSources()).Wait();
407 // Now the migrated image is used.
408 EXPECT_TRUE(user->image_is_safe_format());
409 // Check image dimensions. Images can't be compared since JPEG is lossy.
410 const gfx::ImageSkia& saved_image =
411 user_manager::GetDefaultImage(user_manager::kFirstDefaultImageIndex);
412 EXPECT_EQ(saved_image.width(), user->GetImage().width());
413 EXPECT_EQ(saved_image.height(), user->GetImage().height());
416 IN_PROC_BROWSER_TEST_F(UserImageManagerTest, PRE_SaveUserDefaultImageIndex) {
417 RegisterUser(kTestUser1);
420 // Verifies that SaveUserDefaultImageIndex() correctly sets and persists the
421 // chosen user image.
422 IN_PROC_BROWSER_TEST_F(UserImageManagerTest, SaveUserDefaultImageIndex) {
423 const user_manager::User* user =
424 user_manager::UserManager::Get()->FindUser(kTestUser1);
425 ASSERT_TRUE(user);
427 const gfx::ImageSkia& default_image =
428 user_manager::GetDefaultImage(user_manager::kFirstDefaultImageIndex);
430 UserImageManager* user_image_manager =
431 ChromeUserManager::Get()->GetUserImageManager(kTestUser1);
432 user_image_manager->SaveUserDefaultImageIndex(
433 user_manager::kFirstDefaultImageIndex);
435 EXPECT_TRUE(user->HasDefaultImage());
436 EXPECT_EQ(user_manager::kFirstDefaultImageIndex, user->image_index());
437 EXPECT_TRUE(test::AreImagesEqual(default_image, user->GetImage()));
438 ExpectNewUserImageInfo(
439 kTestUser1, user_manager::kFirstDefaultImageIndex, base::FilePath());
442 IN_PROC_BROWSER_TEST_F(UserImageManagerTest, PRE_SaveUserImage) {
443 RegisterUser(kTestUser1);
446 // Verifies that SaveUserImage() correctly sets and persists the chosen user
447 // image.
448 IN_PROC_BROWSER_TEST_F(UserImageManagerTest, SaveUserImage) {
449 const user_manager::User* user =
450 user_manager::UserManager::Get()->FindUser(kTestUser1);
451 ASSERT_TRUE(user);
453 SkBitmap custom_image_bitmap;
454 custom_image_bitmap.allocN32Pixels(10, 10);
455 custom_image_bitmap.eraseColor(SK_ColorWHITE);
456 custom_image_bitmap.setImmutable();
457 const gfx::ImageSkia custom_image =
458 gfx::ImageSkia::CreateFrom1xBitmap(custom_image_bitmap);
460 run_loop_.reset(new base::RunLoop);
461 UserImageManager* user_image_manager =
462 ChromeUserManager::Get()->GetUserImageManager(kTestUser1);
463 user_image_manager->SaveUserImage(
464 user_manager::UserImage::CreateAndEncode(custom_image));
465 run_loop_->Run();
467 EXPECT_FALSE(user->HasDefaultImage());
468 EXPECT_EQ(user_manager::User::USER_IMAGE_EXTERNAL, user->image_index());
469 EXPECT_TRUE(test::AreImagesEqual(custom_image, user->GetImage()));
470 ExpectNewUserImageInfo(kTestUser1,
471 user_manager::User::USER_IMAGE_EXTERNAL,
472 GetUserImagePath(kTestUser1, "jpg"));
474 const scoped_ptr<gfx::ImageSkia> saved_image =
475 test::ImageLoader(GetUserImagePath(kTestUser1, "jpg")).Load();
476 ASSERT_TRUE(saved_image);
478 // Check image dimensions. Images can't be compared since JPEG is lossy.
479 EXPECT_EQ(custom_image.width(), saved_image->width());
480 EXPECT_EQ(custom_image.height(), saved_image->height());
483 IN_PROC_BROWSER_TEST_F(UserImageManagerTest, PRE_SaveUserImageFromFile) {
484 RegisterUser(kTestUser1);
487 // Verifies that SaveUserImageFromFile() correctly sets and persists the chosen
488 // user image.
489 IN_PROC_BROWSER_TEST_F(UserImageManagerTest, SaveUserImageFromFile) {
490 const user_manager::User* user =
491 user_manager::UserManager::Get()->FindUser(kTestUser1);
492 ASSERT_TRUE(user);
494 const base::FilePath custom_image_path =
495 test_data_dir_.Append(test::kUserAvatarImage1RelativePath);
496 const scoped_ptr<gfx::ImageSkia> custom_image =
497 test::ImageLoader(custom_image_path).Load();
498 ASSERT_TRUE(custom_image);
500 run_loop_.reset(new base::RunLoop);
501 UserImageManager* user_image_manager =
502 ChromeUserManager::Get()->GetUserImageManager(kTestUser1);
503 user_image_manager->SaveUserImageFromFile(custom_image_path);
504 run_loop_->Run();
506 EXPECT_FALSE(user->HasDefaultImage());
507 EXPECT_EQ(user_manager::User::USER_IMAGE_EXTERNAL, user->image_index());
508 EXPECT_TRUE(test::AreImagesEqual(*custom_image, user->GetImage()));
509 ExpectNewUserImageInfo(kTestUser1,
510 user_manager::User::USER_IMAGE_EXTERNAL,
511 GetUserImagePath(kTestUser1, "jpg"));
513 const scoped_ptr<gfx::ImageSkia> saved_image =
514 test::ImageLoader(GetUserImagePath(kTestUser1, "jpg")).Load();
515 ASSERT_TRUE(saved_image);
517 // Check image dimensions. Images can't be compared since JPEG is lossy.
518 EXPECT_EQ(custom_image->width(), saved_image->width());
519 EXPECT_EQ(custom_image->height(), saved_image->height());
522 IN_PROC_BROWSER_TEST_F(UserImageManagerTest,
523 PRE_SaveUserImageFromProfileImage) {
524 RegisterUser(kTestUser1);
525 chromeos::StartupUtils::MarkOobeCompleted();
528 // Verifies that SaveUserImageFromProfileImage() correctly downloads, sets and
529 // persists the chosen user image.
530 IN_PROC_BROWSER_TEST_F(UserImageManagerTest, SaveUserImageFromProfileImage) {
531 const user_manager::User* user =
532 user_manager::UserManager::Get()->FindUser(kTestUser1);
533 ASSERT_TRUE(user);
535 UserImageManagerImpl::IgnoreProfileDataDownloadDelayForTesting();
536 LoginUser(kTestUser1);
537 Profile* profile = ProfileHelper::Get()->GetProfileByUserUnsafe(user);
538 SeedAccountTrackerService(kTestUser1, profile);
540 run_loop_.reset(new base::RunLoop);
541 UserImageManager* user_image_manager =
542 ChromeUserManager::Get()->GetUserImageManager(kTestUser1);
543 user_image_manager->SaveUserImageFromProfileImage();
544 run_loop_->Run();
546 net::TestURLFetcherFactory url_fetcher_factory;
547 CompleteProfileMetadataDownload(kTestUser1, &url_fetcher_factory);
548 CompleteProfileImageDownload(&url_fetcher_factory);
550 const gfx::ImageSkia& profile_image =
551 user_image_manager->DownloadedProfileImage();
553 EXPECT_FALSE(user->HasDefaultImage());
554 EXPECT_EQ(user_manager::User::USER_IMAGE_PROFILE, user->image_index());
555 EXPECT_TRUE(test::AreImagesEqual(profile_image, user->GetImage()));
556 ExpectNewUserImageInfo(kTestUser1,
557 user_manager::User::USER_IMAGE_PROFILE,
558 GetUserImagePath(kTestUser1, "jpg"));
560 const scoped_ptr<gfx::ImageSkia> saved_image =
561 test::ImageLoader(GetUserImagePath(kTestUser1, "jpg")).Load();
562 ASSERT_TRUE(saved_image);
564 // Check image dimensions. Images can't be compared since JPEG is lossy.
565 EXPECT_EQ(profile_image.width(), saved_image->width());
566 EXPECT_EQ(profile_image.height(), saved_image->height());
569 IN_PROC_BROWSER_TEST_F(UserImageManagerTest,
570 PRE_ProfileImageDownloadDoesNotClobber) {
571 RegisterUser(kTestUser1);
572 chromeos::StartupUtils::MarkOobeCompleted();
575 // Sets the user image to the profile image, then sets it to one of the default
576 // images while the profile image download is still in progress. Verifies that
577 // when the download completes, the profile image is ignored and does not
578 // clobber the default image chosen in the meantime.
579 IN_PROC_BROWSER_TEST_F(UserImageManagerTest,
580 ProfileImageDownloadDoesNotClobber) {
581 const user_manager::User* user =
582 user_manager::UserManager::Get()->FindUser(kTestUser1);
583 ASSERT_TRUE(user);
585 const gfx::ImageSkia& default_image =
586 user_manager::GetDefaultImage(user_manager::kFirstDefaultImageIndex);
588 UserImageManagerImpl::IgnoreProfileDataDownloadDelayForTesting();
589 LoginUser(kTestUser1);
590 Profile* profile = ProfileHelper::Get()->GetProfileByUserUnsafe(user);
591 SeedAccountTrackerService(kTestUser1, profile);
593 run_loop_.reset(new base::RunLoop);
594 UserImageManager* user_image_manager =
595 ChromeUserManager::Get()->GetUserImageManager(kTestUser1);
596 user_image_manager->SaveUserImageFromProfileImage();
597 run_loop_->Run();
599 net::TestURLFetcherFactory url_fetcher_factory;
600 CompleteProfileMetadataDownload(kTestUser1, &url_fetcher_factory);
602 user_image_manager->SaveUserDefaultImageIndex(
603 user_manager::kFirstDefaultImageIndex);
605 CompleteProfileImageDownload(&url_fetcher_factory);
607 EXPECT_TRUE(user->HasDefaultImage());
608 EXPECT_EQ(user_manager::kFirstDefaultImageIndex, user->image_index());
609 EXPECT_TRUE(test::AreImagesEqual(default_image, user->GetImage()));
610 ExpectNewUserImageInfo(
611 kTestUser1, user_manager::kFirstDefaultImageIndex, base::FilePath());
614 class UserImageManagerPolicyTest : public UserImageManagerTest,
615 public policy::CloudPolicyStore::Observer {
616 protected:
617 UserImageManagerPolicyTest()
618 : fake_session_manager_client_(new chromeos::FakeSessionManagerClient) {
621 // UserImageManagerTest overrides:
622 void SetUpInProcessBrowserTestFixture() override {
623 DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient(
624 scoped_ptr<SessionManagerClient>(fake_session_manager_client_));
625 UserImageManagerTest::SetUpInProcessBrowserTestFixture();
628 void SetUpOnMainThread() override {
629 UserImageManagerTest::SetUpOnMainThread();
631 base::FilePath user_keys_dir;
632 ASSERT_TRUE(PathService::Get(chromeos::DIR_USER_POLICY_KEYS,
633 &user_keys_dir));
634 const std::string sanitized_username =
635 chromeos::CryptohomeClient::GetStubSanitizedUsername(kEnterpriseUser1);
636 const base::FilePath user_key_file =
637 user_keys_dir.AppendASCII(sanitized_username)
638 .AppendASCII("policy.pub");
639 std::vector<uint8> user_key_bits;
640 ASSERT_TRUE(user_policy_.GetSigningKey()->ExportPublicKey(&user_key_bits));
641 ASSERT_TRUE(base::CreateDirectory(user_key_file.DirName()));
642 ASSERT_EQ(base::WriteFile(
643 user_key_file,
644 reinterpret_cast<const char*>(user_key_bits.data()),
645 user_key_bits.size()),
646 static_cast<int>(user_key_bits.size()));
647 user_policy_.policy_data().set_username(kEnterpriseUser1);
649 policy_image_ = test::ImageLoader(test_data_dir_.Append(
650 test::kUserAvatarImage2RelativePath)).Load();
651 ASSERT_TRUE(policy_image_);
654 // policy::CloudPolicyStore::Observer overrides:
655 void OnStoreLoaded(policy::CloudPolicyStore* store) override {
656 if (run_loop_)
657 run_loop_->Quit();
660 void OnStoreError(policy::CloudPolicyStore* store) override {
661 if (run_loop_)
662 run_loop_->Quit();
665 std::string ConstructPolicy(const std::string& relative_path) {
666 std::string image_data;
667 if (!base::ReadFileToString(test_data_dir_.Append(relative_path),
668 &image_data)) {
669 ADD_FAILURE();
671 std::string policy;
672 base::JSONWriter::Write(*policy::test::ConstructExternalDataReference(
673 embedded_test_server()
674 ->GetURL(std::string("/") + relative_path)
675 .spec(),
676 image_data),
677 &policy);
678 return policy;
681 policy::UserPolicyBuilder user_policy_;
682 FakeSessionManagerClient* fake_session_manager_client_;
684 scoped_ptr<gfx::ImageSkia> policy_image_;
686 private:
687 DISALLOW_COPY_AND_ASSIGN(UserImageManagerPolicyTest);
690 IN_PROC_BROWSER_TEST_F(UserImageManagerPolicyTest, PRE_SetAndClear) {
691 RegisterUser(kEnterpriseUser1);
692 chromeos::StartupUtils::MarkOobeCompleted();
695 // Verifies that the user image can be set through policy. Also verifies that
696 // after the policy has been cleared, the user is able to choose a different
697 // image.
698 // http://crbug.com/396352
699 IN_PROC_BROWSER_TEST_F(UserImageManagerPolicyTest, DISABLED_SetAndClear) {
700 const user_manager::User* user =
701 user_manager::UserManager::Get()->FindUser(kEnterpriseUser1);
702 ASSERT_TRUE(user);
704 LoginUser(kEnterpriseUser1);
705 base::RunLoop().RunUntilIdle();
707 policy::CloudPolicyStore* store = GetStoreForUser(user);
708 ASSERT_TRUE(store);
710 // Set policy. Verify that the policy-provided user image is downloaded, set
711 // and persisted.
712 user_policy_.payload().mutable_useravatarimage()->set_value(
713 ConstructPolicy(test::kUserAvatarImage2RelativePath));
714 user_policy_.Build();
715 fake_session_manager_client_->set_user_policy(kEnterpriseUser1,
716 user_policy_.GetBlob());
717 run_loop_.reset(new base::RunLoop);
718 store->Load();
719 run_loop_->Run();
721 EXPECT_FALSE(user->HasDefaultImage());
722 EXPECT_EQ(user_manager::User::USER_IMAGE_EXTERNAL, user->image_index());
723 EXPECT_TRUE(test::AreImagesEqual(*policy_image_, user->GetImage()));
724 ExpectNewUserImageInfo(kEnterpriseUser1,
725 user_manager::User::USER_IMAGE_EXTERNAL,
726 GetUserImagePath(kEnterpriseUser1, "jpg"));
728 scoped_ptr<gfx::ImageSkia> saved_image =
729 test::ImageLoader(GetUserImagePath(kEnterpriseUser1, "jpg")).Load();
730 ASSERT_TRUE(saved_image);
732 // Check image dimensions. Images can't be compared since JPEG is lossy.
733 EXPECT_EQ(policy_image_->width(), saved_image->width());
734 EXPECT_EQ(policy_image_->height(), saved_image->height());
736 // Clear policy. Verify that the user image switches to a random default
737 // image.
738 user_policy_.payload().Clear();
739 user_policy_.Build();
740 fake_session_manager_client_->set_user_policy(kEnterpriseUser1,
741 user_policy_.GetBlob());
742 run_loop_.reset(new base::RunLoop);
743 store->AddObserver(this);
744 store->Load();
745 run_loop_->Run();
746 store->RemoveObserver(this);
747 base::RunLoop().RunUntilIdle();
749 const int default_image_index = user->image_index();
750 EXPECT_TRUE(user->HasDefaultImage());
751 ASSERT_LE(user_manager::kFirstDefaultImageIndex, default_image_index);
752 ASSERT_GT(
753 user_manager::kFirstDefaultImageIndex + user_manager::kDefaultImagesCount,
754 default_image_index);
755 const gfx::ImageSkia& default_image =
756 user_manager::GetDefaultImage(default_image_index);
757 EXPECT_TRUE(test::AreImagesEqual(default_image, user->GetImage()));
758 ExpectNewUserImageInfo(kEnterpriseUser1, default_image_index,
759 base::FilePath());
761 // Choose a different user image. Verify that the chosen user image is set and
762 // persisted.
763 const int user_image_index =
764 user_manager::kFirstDefaultImageIndex +
765 (default_image_index - user_manager::kFirstDefaultImageIndex + 1) %
766 user_manager::kDefaultImagesCount;
767 const gfx::ImageSkia& user_image =
768 user_manager::GetDefaultImage(user_image_index);
770 UserImageManager* user_image_manager =
771 ChromeUserManager::Get()->GetUserImageManager(kEnterpriseUser1);
772 user_image_manager->SaveUserDefaultImageIndex(user_image_index);
774 EXPECT_TRUE(user->HasDefaultImage());
775 EXPECT_EQ(user_image_index, user->image_index());
776 EXPECT_TRUE(test::AreImagesEqual(user_image, user->GetImage()));
777 ExpectNewUserImageInfo(kEnterpriseUser1, user_image_index, base::FilePath());
780 IN_PROC_BROWSER_TEST_F(UserImageManagerPolicyTest, PRE_PolicyOverridesUser) {
781 RegisterUser(kEnterpriseUser1);
782 chromeos::StartupUtils::MarkOobeCompleted();
785 // Verifies that when the user chooses a user image and a different image is
786 // then set through policy, the policy takes precedence, overriding the
787 // previously chosen image.
788 IN_PROC_BROWSER_TEST_F(UserImageManagerPolicyTest, PolicyOverridesUser) {
789 const user_manager::User* user =
790 user_manager::UserManager::Get()->FindUser(kEnterpriseUser1);
791 ASSERT_TRUE(user);
793 LoginUser(kEnterpriseUser1);
794 base::RunLoop().RunUntilIdle();
796 policy::CloudPolicyStore* store = GetStoreForUser(user);
797 ASSERT_TRUE(store);
799 // Choose a user image. Verify that the chosen user image is set and
800 // persisted.
801 const gfx::ImageSkia& default_image =
802 user_manager::GetDefaultImage(user_manager::kFirstDefaultImageIndex);
804 UserImageManager* user_image_manager =
805 ChromeUserManager::Get()->GetUserImageManager(kEnterpriseUser1);
806 user_image_manager->SaveUserDefaultImageIndex(
807 user_manager::kFirstDefaultImageIndex);
809 EXPECT_TRUE(user->HasDefaultImage());
810 EXPECT_EQ(user_manager::kFirstDefaultImageIndex, user->image_index());
811 EXPECT_TRUE(test::AreImagesEqual(default_image, user->GetImage()));
812 ExpectNewUserImageInfo(kEnterpriseUser1,
813 user_manager::kFirstDefaultImageIndex,
814 base::FilePath());
816 // Set policy. Verify that the policy-provided user image is downloaded, set
817 // and persisted, overriding the previously set image.
818 user_policy_.payload().mutable_useravatarimage()->set_value(
819 ConstructPolicy(test::kUserAvatarImage2RelativePath));
820 user_policy_.Build();
821 fake_session_manager_client_->set_user_policy(kEnterpriseUser1,
822 user_policy_.GetBlob());
823 run_loop_.reset(new base::RunLoop);
824 store->Load();
825 run_loop_->Run();
827 EXPECT_FALSE(user->HasDefaultImage());
828 EXPECT_EQ(user_manager::User::USER_IMAGE_EXTERNAL, user->image_index());
829 EXPECT_TRUE(test::AreImagesEqual(*policy_image_, user->GetImage()));
830 ExpectNewUserImageInfo(kEnterpriseUser1,
831 user_manager::User::USER_IMAGE_EXTERNAL,
832 GetUserImagePath(kEnterpriseUser1, "jpg"));
834 scoped_ptr<gfx::ImageSkia> saved_image =
835 test::ImageLoader(GetUserImagePath(kEnterpriseUser1, "jpg")).Load();
836 ASSERT_TRUE(saved_image);
838 // Check image dimensions. Images can't be compared since JPEG is lossy.
839 EXPECT_EQ(policy_image_->width(), saved_image->width());
840 EXPECT_EQ(policy_image_->height(), saved_image->height());
843 IN_PROC_BROWSER_TEST_F(UserImageManagerPolicyTest,
844 PRE_UserDoesNotOverridePolicy) {
845 RegisterUser(kEnterpriseUser1);
846 chromeos::StartupUtils::MarkOobeCompleted();
849 // Verifies that when the user image has been set through policy and the user
850 // chooses a different image, the policy takes precedence, preventing the user
851 // from overriding the previously chosen image.
852 IN_PROC_BROWSER_TEST_F(UserImageManagerPolicyTest, UserDoesNotOverridePolicy) {
853 const user_manager::User* user =
854 user_manager::UserManager::Get()->FindUser(kEnterpriseUser1);
855 ASSERT_TRUE(user);
857 LoginUser(kEnterpriseUser1);
858 base::RunLoop().RunUntilIdle();
860 policy::CloudPolicyStore* store = GetStoreForUser(user);
861 ASSERT_TRUE(store);
863 // Set policy. Verify that the policy-provided user image is downloaded, set
864 // and persisted.
865 user_policy_.payload().mutable_useravatarimage()->set_value(
866 ConstructPolicy(test::kUserAvatarImage2RelativePath));
867 user_policy_.Build();
868 fake_session_manager_client_->set_user_policy(kEnterpriseUser1,
869 user_policy_.GetBlob());
870 run_loop_.reset(new base::RunLoop);
871 store->Load();
872 run_loop_->Run();
874 EXPECT_FALSE(user->HasDefaultImage());
875 EXPECT_EQ(user_manager::User::USER_IMAGE_EXTERNAL, user->image_index());
876 EXPECT_TRUE(test::AreImagesEqual(*policy_image_, user->GetImage()));
877 ExpectNewUserImageInfo(kEnterpriseUser1,
878 user_manager::User::USER_IMAGE_EXTERNAL,
879 GetUserImagePath(kEnterpriseUser1, "jpg"));
881 scoped_ptr<gfx::ImageSkia> saved_image =
882 test::ImageLoader(GetUserImagePath(kEnterpriseUser1, "jpg")).Load();
883 ASSERT_TRUE(saved_image);
885 // Check image dimensions. Images can't be compared since JPEG is lossy.
886 EXPECT_EQ(policy_image_->width(), saved_image->width());
887 EXPECT_EQ(policy_image_->height(), saved_image->height());
889 // Choose a different user image. Verify that the user image does not change
890 // as policy takes precedence.
891 UserImageManager* user_image_manager =
892 ChromeUserManager::Get()->GetUserImageManager(kEnterpriseUser1);
893 user_image_manager->SaveUserDefaultImageIndex(
894 user_manager::kFirstDefaultImageIndex);
896 EXPECT_FALSE(user->HasDefaultImage());
897 EXPECT_EQ(user_manager::User::USER_IMAGE_EXTERNAL, user->image_index());
898 EXPECT_TRUE(test::AreImagesEqual(*policy_image_, user->GetImage()));
899 ExpectNewUserImageInfo(kEnterpriseUser1,
900 user_manager::User::USER_IMAGE_EXTERNAL,
901 GetUserImagePath(kEnterpriseUser1, "jpg"));
903 saved_image =
904 test::ImageLoader(GetUserImagePath(kEnterpriseUser1, "jpg")).Load();
905 ASSERT_TRUE(saved_image);
907 // Check image dimensions. Images can't be compared since JPEG is lossy.
908 EXPECT_EQ(policy_image_->width(), saved_image->width());
909 EXPECT_EQ(policy_image_->height(), saved_image->height());
912 } // namespace chromeos