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.
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"
80 const char kTestUser1
[] = "test-user@example.com";
81 const char kTestUser2
[] = "test-user2@example.com";
83 policy::CloudPolicyStore
* GetStoreForUser(const user_manager::User
* user
) {
84 Profile
* profile
= ProfileHelper::Get()->GetProfileByUserUnsafe(user
);
89 policy::UserCloudPolicyManagerChromeOS
* policy_manager
=
90 policy::UserCloudPolicyManagerFactoryChromeOS::GetForProfile(profile
);
91 if (!policy_manager
) {
95 return policy_manager
->core()->store();
100 class UserImageManagerTest
: public LoginManagerTest
,
101 public user_manager::UserManager::Observer
{
103 UserImageManagerTest() : LoginManagerTest(true) {
106 // LoginManagerTest overrides:
107 void SetUpInProcessBrowserTestFixture() override
{
108 LoginManagerTest::SetUpInProcessBrowserTestFixture();
110 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir_
));
111 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA
, &user_data_dir_
));
114 void SetUpOnMainThread() override
{
115 LoginManagerTest::SetUpOnMainThread();
116 local_state_
= g_browser_process
->local_state();
117 user_manager::UserManager::Get()->AddObserver(this);
120 void TearDownOnMainThread() override
{
121 user_manager::UserManager::Get()->RemoveObserver(this);
122 LoginManagerTest::TearDownOnMainThread();
125 // UserManager::Observer overrides:
126 void LocalStateChanged(user_manager::UserManager
* user_manager
) override
{
131 // Logs in |username|.
132 void LogIn(const std::string
& username
) {
133 user_manager::UserManager::Get()->UserLoggedIn(username
, username
, false);
136 // Stores old (pre-migration) user image info.
137 void SetOldUserImageInfo(const std::string
& username
,
139 const base::FilePath
& image_path
) {
140 RegisterUser(username
);
141 DictionaryPrefUpdate
images_pref(local_state_
, "UserImages");
142 base::DictionaryValue
* image_properties
= new base::DictionaryValue();
143 image_properties
->Set("index", new base::FundamentalValue(image_index
));
144 image_properties
->Set(
145 "path" , new base::StringValue(image_path
.value()));
146 images_pref
->SetWithoutPathExpansion(username
, image_properties
);
149 // Verifies user image info in |images_pref| dictionary.
150 void ExpectUserImageInfo(const base::DictionaryValue
* images_pref
,
151 const std::string
& username
,
153 const base::FilePath
& image_path
) {
154 ASSERT_TRUE(images_pref
);
155 const base::DictionaryValue
* image_properties
= NULL
;
156 images_pref
->GetDictionaryWithoutPathExpansion(username
, &image_properties
);
157 ASSERT_TRUE(image_properties
);
158 int actual_image_index
;
159 std::string actual_image_path
;
160 ASSERT_TRUE(image_properties
->GetInteger("index", &actual_image_index
) &&
161 image_properties
->GetString("path", &actual_image_path
));
162 EXPECT_EQ(image_index
, actual_image_index
);
163 EXPECT_EQ(image_path
.value(), actual_image_path
);
166 // Verifies that there is no image info for |username| in dictionary
168 void ExpectNoUserImageInfo(const base::DictionaryValue
* images_pref
,
169 const std::string
& username
) {
170 ASSERT_TRUE(images_pref
);
171 const base::DictionaryValue
* image_properties
= NULL
;
172 images_pref
->GetDictionaryWithoutPathExpansion(username
, &image_properties
);
173 ASSERT_FALSE(image_properties
);
176 // Verifies that old user image info matches |image_index| and |image_path|
177 // and that new user image info does not exist.
178 void ExpectOldUserImageInfo(const std::string
& username
,
180 const base::FilePath
& image_path
) {
181 ExpectUserImageInfo(local_state_
->GetDictionary("UserImages"),
182 username
, image_index
, image_path
);
183 ExpectNoUserImageInfo(local_state_
->GetDictionary("user_image_info"),
187 // Verifies that new user image info matches |image_index| and |image_path|
188 // and that old user image info does not exist.
189 void ExpectNewUserImageInfo(const std::string
& username
,
191 const base::FilePath
& image_path
) {
192 ExpectUserImageInfo(local_state_
->GetDictionary("user_image_info"),
193 username
, image_index
, image_path
);
194 ExpectNoUserImageInfo(local_state_
->GetDictionary("UserImages"),
198 // Sets bitmap |resource_id| as image for |username| and saves it to disk.
199 void SaveUserImagePNG(const std::string
& username
,
201 base::FilePath image_path
= GetUserImagePath(username
, "png");
202 scoped_refptr
<base::RefCountedStaticMemory
> image_data(
203 ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale(
204 resource_id
, ui::SCALE_FACTOR_100P
));
205 int written
= base::WriteFile(
207 reinterpret_cast<const char*>(image_data
->front()),
209 EXPECT_EQ(static_cast<int>(image_data
->size()), written
);
211 username
, user_manager::User::USER_IMAGE_EXTERNAL
, image_path
);
214 // Returns the image path for user |username| with specified |extension|.
215 base::FilePath
GetUserImagePath(const std::string
& username
,
216 const std::string
& extension
) {
217 return user_data_dir_
.Append(username
).AddExtension(extension
);
220 // Seeds the AccountTrackerService with test data so the ProfileDownloader can
221 // retrieve the picture URL and fetch the image.
222 void SeedAccountTrackerService(const std::string
& username
,
224 AccountTrackerService::AccountInfo info
;
225 info
.account_id
= std::string();
226 info
.gaia
= username
;
227 info
.email
= username
;
228 info
.full_name
= username
;
229 info
.given_name
= username
;
230 info
.hosted_domain
= std::string();
231 info
.locale
= username
;
232 info
.picture_url
= "http://localhost/avatar.jpg";
233 info
.is_child_account
= false;
235 AccountTrackerServiceFactory::GetForProfile(profile
)->SeedAccountInfo(info
);
238 // Completes the download of all non-image profile data for the user
239 // |username|. This method must only be called after a profile data
240 // download has been started. |url_fetcher_factory| will capture
241 // the net::TestURLFetcher created by the ProfileDownloader to
242 // download the profile image.
243 void CompleteProfileMetadataDownload(
244 const std::string
& username
,
245 net::TestURLFetcherFactory
* url_fetcher_factory
) {
246 ProfileDownloader
* profile_downloader
=
247 reinterpret_cast<UserImageManagerImpl
*>(
248 ChromeUserManager::Get()->GetUserImageManager(username
))
249 ->profile_downloader_
.get();
250 ASSERT_TRUE(profile_downloader
);
252 static_cast<OAuth2TokenService::Consumer
*>(profile_downloader
)->
253 OnGetTokenSuccess(NULL
,
255 base::Time::Now() + base::TimeDelta::FromDays(1));
258 // Completes the download of the currently logged-in user's profile image.
259 // This method must only be called after a profile data download including
260 // the profile image has been started, the download of all non-image data has
261 // been completed by calling CompleteProfileMetadataDownload() and the
262 // net::TestURLFetcher created by the ProfileDownloader to download the
263 // profile image has been captured by |url_fetcher_factory|.
264 void CompleteProfileImageDownload(
265 net::TestURLFetcherFactory
* url_fetcher_factory
) {
266 std::string profile_image_data
;
267 base::FilePath test_data_dir
;
268 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
));
269 EXPECT_TRUE(ReadFileToString(
270 test_data_dir
.Append("chromeos").Append("avatar1.jpg"),
271 &profile_image_data
));
273 base::RunLoop run_loop
;
274 PrefChangeRegistrar pref_change_registrar
;
275 pref_change_registrar
.Init(local_state_
);
276 pref_change_registrar
.Add("UserDisplayName", run_loop
.QuitClosure());
277 net::TestURLFetcher
* fetcher
= url_fetcher_factory
->GetFetcherByID(0);
278 ASSERT_TRUE(fetcher
);
279 fetcher
->SetResponseString(profile_image_data
);
280 fetcher
->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
,
282 fetcher
->set_response_code(200);
283 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
286 const user_manager::User
* user
=
287 user_manager::UserManager::Get()->GetLoggedInUser();
289 UserImageManagerImpl
* uim
= reinterpret_cast<UserImageManagerImpl
*>(
290 ChromeUserManager::Get()->GetUserImageManager(user
->email()));
291 if (uim
->job_
.get()) {
292 run_loop_
.reset(new base::RunLoop
);
297 base::FilePath test_data_dir_
;
298 base::FilePath user_data_dir_
;
300 PrefService
* local_state_
;
302 scoped_ptr
<gfx::ImageSkia
> decoded_image_
;
304 scoped_ptr
<base::RunLoop
> run_loop_
;
307 DISALLOW_COPY_AND_ASSIGN(UserImageManagerTest
);
310 IN_PROC_BROWSER_TEST_F(UserImageManagerTest
, PRE_DefaultUserImagePreserved
) {
311 // Setup an old default (stock) user image.
312 ScopedUserManagerEnabler(new MockUserManager
);
314 kTestUser1
, user_manager::kFirstDefaultImageIndex
, base::FilePath());
317 IN_PROC_BROWSER_TEST_F(UserImageManagerTest
, DefaultUserImagePreserved
) {
318 user_manager::UserManager::Get()->GetUsers(); // Load users.
319 // Old info preserved.
320 ExpectOldUserImageInfo(
321 kTestUser1
, user_manager::kFirstDefaultImageIndex
, base::FilePath());
323 // Image info is migrated now.
324 ExpectNewUserImageInfo(
325 kTestUser1
, user_manager::kFirstDefaultImageIndex
, base::FilePath());
328 IN_PROC_BROWSER_TEST_F(UserImageManagerTest
, PRE_OtherUsersUnaffected
) {
329 // Setup two users with stock images.
330 ScopedUserManagerEnabler(new MockUserManager
);
332 kTestUser1
, user_manager::kFirstDefaultImageIndex
, base::FilePath());
334 kTestUser2
, user_manager::kFirstDefaultImageIndex
+ 1, base::FilePath());
337 IN_PROC_BROWSER_TEST_F(UserImageManagerTest
, OtherUsersUnaffected
) {
338 user_manager::UserManager::Get()->GetUsers(); // Load users.
339 // Old info preserved.
340 ExpectOldUserImageInfo(
341 kTestUser1
, user_manager::kFirstDefaultImageIndex
, base::FilePath());
342 ExpectOldUserImageInfo(
343 kTestUser2
, user_manager::kFirstDefaultImageIndex
+ 1, base::FilePath());
345 // Image info is migrated for the first user and unaffected for the rest.
346 ExpectNewUserImageInfo(
347 kTestUser1
, user_manager::kFirstDefaultImageIndex
, base::FilePath());
348 ExpectOldUserImageInfo(
349 kTestUser2
, user_manager::kFirstDefaultImageIndex
+ 1, base::FilePath());
352 IN_PROC_BROWSER_TEST_F(UserImageManagerTest
, PRE_PRE_NonJPEGImageFromFile
) {
353 // Setup a user with non-JPEG image.
354 ScopedUserManagerEnabler(new MockUserManager
);
355 SaveUserImagePNG(kTestUser1
,
356 user_manager::kDefaultImageResourceIDs
357 [user_manager::kFirstDefaultImageIndex
]);
360 IN_PROC_BROWSER_TEST_F(UserImageManagerTest
, PRE_NonJPEGImageFromFile
) {
361 user_manager::UserManager::Get()->GetUsers(); // Load users.
362 // Old info preserved.
363 ExpectOldUserImageInfo(kTestUser1
,
364 user_manager::User::USER_IMAGE_EXTERNAL
,
365 GetUserImagePath(kTestUser1
, "png"));
366 const user_manager::User
* user
=
367 user_manager::UserManager::Get()->FindUser(kTestUser1
);
368 EXPECT_TRUE(user
->image_is_stub());
370 base::RunLoop run_loop
;
371 PrefChangeRegistrar pref_change_registrar_
;
372 pref_change_registrar_
.Init(local_state_
);
373 pref_change_registrar_
.Add("UserImages", run_loop
.QuitClosure());
376 // Wait for migration.
379 // Image info is migrated and the image is converted to JPG.
380 ExpectNewUserImageInfo(kTestUser1
,
381 user_manager::User::USER_IMAGE_EXTERNAL
,
382 GetUserImagePath(kTestUser1
, "jpg"));
383 user
= user_manager::UserManager::Get()->GetLoggedInUser();
385 EXPECT_FALSE(user
->image_is_safe_format());
386 // Check image dimensions.
387 const gfx::ImageSkia
& saved_image
=
388 user_manager::GetDefaultImage(user_manager::kFirstDefaultImageIndex
);
389 EXPECT_EQ(saved_image
.width(), user
->GetImage().width());
390 EXPECT_EQ(saved_image
.height(), user
->GetImage().height());
393 IN_PROC_BROWSER_TEST_F(UserImageManagerTest
, NonJPEGImageFromFile
) {
394 user_manager::UserManager::Get()->GetUsers(); // Load users.
395 const user_manager::User
* user
=
396 user_manager::UserManager::Get()->FindUser(kTestUser1
);
398 // Wait for image load.
399 if (user
->image_index() == user_manager::User::USER_IMAGE_INVALID
) {
400 content::WindowedNotificationObserver(
401 chrome::NOTIFICATION_LOGIN_USER_IMAGE_CHANGED
,
402 content::NotificationService::AllSources()).Wait();
404 // Now the migrated image is used.
405 EXPECT_TRUE(user
->image_is_safe_format());
406 // Check image dimensions. Images can't be compared since JPEG is lossy.
407 const gfx::ImageSkia
& saved_image
=
408 user_manager::GetDefaultImage(user_manager::kFirstDefaultImageIndex
);
409 EXPECT_EQ(saved_image
.width(), user
->GetImage().width());
410 EXPECT_EQ(saved_image
.height(), user
->GetImage().height());
413 IN_PROC_BROWSER_TEST_F(UserImageManagerTest
, PRE_SaveUserDefaultImageIndex
) {
414 RegisterUser(kTestUser1
);
417 // Verifies that SaveUserDefaultImageIndex() correctly sets and persists the
418 // chosen user image.
419 IN_PROC_BROWSER_TEST_F(UserImageManagerTest
, SaveUserDefaultImageIndex
) {
420 const user_manager::User
* user
=
421 user_manager::UserManager::Get()->FindUser(kTestUser1
);
424 const gfx::ImageSkia
& default_image
=
425 user_manager::GetDefaultImage(user_manager::kFirstDefaultImageIndex
);
427 UserImageManager
* user_image_manager
=
428 ChromeUserManager::Get()->GetUserImageManager(kTestUser1
);
429 user_image_manager
->SaveUserDefaultImageIndex(
430 user_manager::kFirstDefaultImageIndex
);
432 EXPECT_TRUE(user
->HasDefaultImage());
433 EXPECT_EQ(user_manager::kFirstDefaultImageIndex
, user
->image_index());
434 EXPECT_TRUE(test::AreImagesEqual(default_image
, user
->GetImage()));
435 ExpectNewUserImageInfo(
436 kTestUser1
, user_manager::kFirstDefaultImageIndex
, base::FilePath());
439 IN_PROC_BROWSER_TEST_F(UserImageManagerTest
, PRE_SaveUserImage
) {
440 RegisterUser(kTestUser1
);
443 // Verifies that SaveUserImage() correctly sets and persists the chosen user
445 IN_PROC_BROWSER_TEST_F(UserImageManagerTest
, SaveUserImage
) {
446 const user_manager::User
* user
=
447 user_manager::UserManager::Get()->FindUser(kTestUser1
);
450 SkBitmap custom_image_bitmap
;
451 custom_image_bitmap
.allocN32Pixels(10, 10);
452 custom_image_bitmap
.eraseColor(SK_ColorWHITE
);
453 custom_image_bitmap
.setImmutable();
454 const gfx::ImageSkia custom_image
=
455 gfx::ImageSkia::CreateFrom1xBitmap(custom_image_bitmap
);
457 run_loop_
.reset(new base::RunLoop
);
458 UserImageManager
* user_image_manager
=
459 ChromeUserManager::Get()->GetUserImageManager(kTestUser1
);
460 user_image_manager
->SaveUserImage(
461 user_manager::UserImage::CreateAndEncode(custom_image
));
464 EXPECT_FALSE(user
->HasDefaultImage());
465 EXPECT_EQ(user_manager::User::USER_IMAGE_EXTERNAL
, user
->image_index());
466 EXPECT_TRUE(test::AreImagesEqual(custom_image
, user
->GetImage()));
467 ExpectNewUserImageInfo(kTestUser1
,
468 user_manager::User::USER_IMAGE_EXTERNAL
,
469 GetUserImagePath(kTestUser1
, "jpg"));
471 const scoped_ptr
<gfx::ImageSkia
> saved_image
=
472 test::ImageLoader(GetUserImagePath(kTestUser1
, "jpg")).Load();
473 ASSERT_TRUE(saved_image
);
475 // Check image dimensions. Images can't be compared since JPEG is lossy.
476 EXPECT_EQ(custom_image
.width(), saved_image
->width());
477 EXPECT_EQ(custom_image
.height(), saved_image
->height());
480 IN_PROC_BROWSER_TEST_F(UserImageManagerTest
, PRE_SaveUserImageFromFile
) {
481 RegisterUser(kTestUser1
);
484 // Verifies that SaveUserImageFromFile() correctly sets and persists the chosen
486 IN_PROC_BROWSER_TEST_F(UserImageManagerTest
, SaveUserImageFromFile
) {
487 const user_manager::User
* user
=
488 user_manager::UserManager::Get()->FindUser(kTestUser1
);
491 const base::FilePath custom_image_path
=
492 test_data_dir_
.Append(test::kUserAvatarImage1RelativePath
);
493 const scoped_ptr
<gfx::ImageSkia
> custom_image
=
494 test::ImageLoader(custom_image_path
).Load();
495 ASSERT_TRUE(custom_image
);
497 run_loop_
.reset(new base::RunLoop
);
498 UserImageManager
* user_image_manager
=
499 ChromeUserManager::Get()->GetUserImageManager(kTestUser1
);
500 user_image_manager
->SaveUserImageFromFile(custom_image_path
);
503 EXPECT_FALSE(user
->HasDefaultImage());
504 EXPECT_EQ(user_manager::User::USER_IMAGE_EXTERNAL
, user
->image_index());
505 EXPECT_TRUE(test::AreImagesEqual(*custom_image
, user
->GetImage()));
506 ExpectNewUserImageInfo(kTestUser1
,
507 user_manager::User::USER_IMAGE_EXTERNAL
,
508 GetUserImagePath(kTestUser1
, "jpg"));
510 const scoped_ptr
<gfx::ImageSkia
> saved_image
=
511 test::ImageLoader(GetUserImagePath(kTestUser1
, "jpg")).Load();
512 ASSERT_TRUE(saved_image
);
514 // Check image dimensions. Images can't be compared since JPEG is lossy.
515 EXPECT_EQ(custom_image
->width(), saved_image
->width());
516 EXPECT_EQ(custom_image
->height(), saved_image
->height());
519 IN_PROC_BROWSER_TEST_F(UserImageManagerTest
,
520 PRE_SaveUserImageFromProfileImage
) {
521 RegisterUser(kTestUser1
);
522 chromeos::StartupUtils::MarkOobeCompleted();
525 // Verifies that SaveUserImageFromProfileImage() correctly downloads, sets and
526 // persists the chosen user image.
527 IN_PROC_BROWSER_TEST_F(UserImageManagerTest
, SaveUserImageFromProfileImage
) {
528 const user_manager::User
* user
=
529 user_manager::UserManager::Get()->FindUser(kTestUser1
);
532 UserImageManagerImpl::IgnoreProfileDataDownloadDelayForTesting();
533 LoginUser(kTestUser1
);
534 Profile
* profile
= ProfileHelper::Get()->GetProfileByUserUnsafe(user
);
535 SeedAccountTrackerService(kTestUser1
, profile
);
537 run_loop_
.reset(new base::RunLoop
);
538 UserImageManager
* user_image_manager
=
539 ChromeUserManager::Get()->GetUserImageManager(kTestUser1
);
540 user_image_manager
->SaveUserImageFromProfileImage();
543 net::TestURLFetcherFactory url_fetcher_factory
;
544 CompleteProfileMetadataDownload(kTestUser1
, &url_fetcher_factory
);
545 CompleteProfileImageDownload(&url_fetcher_factory
);
547 const gfx::ImageSkia
& profile_image
=
548 user_image_manager
->DownloadedProfileImage();
550 EXPECT_FALSE(user
->HasDefaultImage());
551 EXPECT_EQ(user_manager::User::USER_IMAGE_PROFILE
, user
->image_index());
552 EXPECT_TRUE(test::AreImagesEqual(profile_image
, user
->GetImage()));
553 ExpectNewUserImageInfo(kTestUser1
,
554 user_manager::User::USER_IMAGE_PROFILE
,
555 GetUserImagePath(kTestUser1
, "jpg"));
557 const scoped_ptr
<gfx::ImageSkia
> saved_image
=
558 test::ImageLoader(GetUserImagePath(kTestUser1
, "jpg")).Load();
559 ASSERT_TRUE(saved_image
);
561 // Check image dimensions. Images can't be compared since JPEG is lossy.
562 EXPECT_EQ(profile_image
.width(), saved_image
->width());
563 EXPECT_EQ(profile_image
.height(), saved_image
->height());
566 IN_PROC_BROWSER_TEST_F(UserImageManagerTest
,
567 PRE_ProfileImageDownloadDoesNotClobber
) {
568 RegisterUser(kTestUser1
);
569 chromeos::StartupUtils::MarkOobeCompleted();
572 // Sets the user image to the profile image, then sets it to one of the default
573 // images while the profile image download is still in progress. Verifies that
574 // when the download completes, the profile image is ignored and does not
575 // clobber the default image chosen in the meantime.
576 IN_PROC_BROWSER_TEST_F(UserImageManagerTest
,
577 ProfileImageDownloadDoesNotClobber
) {
578 const user_manager::User
* user
=
579 user_manager::UserManager::Get()->FindUser(kTestUser1
);
582 const gfx::ImageSkia
& default_image
=
583 user_manager::GetDefaultImage(user_manager::kFirstDefaultImageIndex
);
585 UserImageManagerImpl::IgnoreProfileDataDownloadDelayForTesting();
586 LoginUser(kTestUser1
);
587 Profile
* profile
= ProfileHelper::Get()->GetProfileByUserUnsafe(user
);
588 SeedAccountTrackerService(kTestUser1
, profile
);
590 run_loop_
.reset(new base::RunLoop
);
591 UserImageManager
* user_image_manager
=
592 ChromeUserManager::Get()->GetUserImageManager(kTestUser1
);
593 user_image_manager
->SaveUserImageFromProfileImage();
596 net::TestURLFetcherFactory url_fetcher_factory
;
597 CompleteProfileMetadataDownload(kTestUser1
, &url_fetcher_factory
);
599 user_image_manager
->SaveUserDefaultImageIndex(
600 user_manager::kFirstDefaultImageIndex
);
602 CompleteProfileImageDownload(&url_fetcher_factory
);
604 EXPECT_TRUE(user
->HasDefaultImage());
605 EXPECT_EQ(user_manager::kFirstDefaultImageIndex
, user
->image_index());
606 EXPECT_TRUE(test::AreImagesEqual(default_image
, user
->GetImage()));
607 ExpectNewUserImageInfo(
608 kTestUser1
, user_manager::kFirstDefaultImageIndex
, base::FilePath());
611 class UserImageManagerPolicyTest
: public UserImageManagerTest
,
612 public policy::CloudPolicyStore::Observer
{
614 UserImageManagerPolicyTest()
615 : fake_session_manager_client_(new chromeos::FakeSessionManagerClient
) {
618 // UserImageManagerTest overrides:
619 void SetUpInProcessBrowserTestFixture() override
{
620 DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient(
621 scoped_ptr
<SessionManagerClient
>(fake_session_manager_client_
));
622 UserImageManagerTest::SetUpInProcessBrowserTestFixture();
625 void SetUpOnMainThread() override
{
626 UserImageManagerTest::SetUpOnMainThread();
628 base::FilePath user_keys_dir
;
629 ASSERT_TRUE(PathService::Get(chromeos::DIR_USER_POLICY_KEYS
,
631 const std::string sanitized_username
=
632 chromeos::CryptohomeClient::GetStubSanitizedUsername(kTestUser1
);
633 const base::FilePath user_key_file
=
634 user_keys_dir
.AppendASCII(sanitized_username
)
635 .AppendASCII("policy.pub");
636 std::vector
<uint8
> user_key_bits
;
637 ASSERT_TRUE(user_policy_
.GetSigningKey()->ExportPublicKey(&user_key_bits
));
638 ASSERT_TRUE(base::CreateDirectory(user_key_file
.DirName()));
639 ASSERT_EQ(base::WriteFile(
641 reinterpret_cast<const char*>(user_key_bits
.data()),
642 user_key_bits
.size()),
643 static_cast<int>(user_key_bits
.size()));
644 user_policy_
.policy_data().set_username(kTestUser1
);
646 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
648 policy_image_
= test::ImageLoader(test_data_dir_
.Append(
649 test::kUserAvatarImage2RelativePath
)).Load();
650 ASSERT_TRUE(policy_image_
);
653 // policy::CloudPolicyStore::Observer overrides:
654 void OnStoreLoaded(policy::CloudPolicyStore
* store
) override
{
659 void OnStoreError(policy::CloudPolicyStore
* store
) override
{
664 std::string
ConstructPolicy(const std::string
& relative_path
) {
665 std::string image_data
;
666 if (!base::ReadFileToString(test_data_dir_
.Append(relative_path
),
671 base::JSONWriter::Write(*policy::test::ConstructExternalDataReference(
672 embedded_test_server()
673 ->GetURL(std::string("/") + relative_path
)
680 policy::UserPolicyBuilder user_policy_
;
681 FakeSessionManagerClient
* fake_session_manager_client_
;
683 scoped_ptr
<gfx::ImageSkia
> policy_image_
;
686 DISALLOW_COPY_AND_ASSIGN(UserImageManagerPolicyTest
);
689 IN_PROC_BROWSER_TEST_F(UserImageManagerPolicyTest
, PRE_SetAndClear
) {
690 RegisterUser(kTestUser1
);
691 chromeos::StartupUtils::MarkOobeCompleted();
694 // Verifies that the user image can be set through policy. Also verifies that
695 // after the policy has been cleared, the user is able to choose a different
697 // http://crbug.com/396352
698 IN_PROC_BROWSER_TEST_F(UserImageManagerPolicyTest
, DISABLED_SetAndClear
) {
699 const user_manager::User
* user
=
700 user_manager::UserManager::Get()->FindUser(kTestUser1
);
703 LoginUser(kTestUser1
);
704 base::RunLoop().RunUntilIdle();
706 policy::CloudPolicyStore
* store
= GetStoreForUser(user
);
709 // Set policy. Verify that the policy-provided user image is downloaded, set
711 user_policy_
.payload().mutable_useravatarimage()->set_value(
712 ConstructPolicy(test::kUserAvatarImage2RelativePath
));
713 user_policy_
.Build();
714 fake_session_manager_client_
->set_user_policy(kTestUser1
,
715 user_policy_
.GetBlob());
716 run_loop_
.reset(new base::RunLoop
);
720 EXPECT_FALSE(user
->HasDefaultImage());
721 EXPECT_EQ(user_manager::User::USER_IMAGE_EXTERNAL
, user
->image_index());
722 EXPECT_TRUE(test::AreImagesEqual(*policy_image_
, user
->GetImage()));
723 ExpectNewUserImageInfo(kTestUser1
,
724 user_manager::User::USER_IMAGE_EXTERNAL
,
725 GetUserImagePath(kTestUser1
, "jpg"));
727 scoped_ptr
<gfx::ImageSkia
> saved_image
=
728 test::ImageLoader(GetUserImagePath(kTestUser1
, "jpg")).Load();
729 ASSERT_TRUE(saved_image
);
731 // Check image dimensions. Images can't be compared since JPEG is lossy.
732 EXPECT_EQ(policy_image_
->width(), saved_image
->width());
733 EXPECT_EQ(policy_image_
->height(), saved_image
->height());
735 // Clear policy. Verify that the user image switches to a random default
737 user_policy_
.payload().Clear();
738 user_policy_
.Build();
739 fake_session_manager_client_
->set_user_policy(kTestUser1
,
740 user_policy_
.GetBlob());
741 run_loop_
.reset(new base::RunLoop
);
742 store
->AddObserver(this);
745 store
->RemoveObserver(this);
746 base::RunLoop().RunUntilIdle();
748 const int default_image_index
= user
->image_index();
749 EXPECT_TRUE(user
->HasDefaultImage());
750 ASSERT_LE(user_manager::kFirstDefaultImageIndex
, default_image_index
);
752 user_manager::kFirstDefaultImageIndex
+ user_manager::kDefaultImagesCount
,
753 default_image_index
);
754 const gfx::ImageSkia
& default_image
=
755 user_manager::GetDefaultImage(default_image_index
);
756 EXPECT_TRUE(test::AreImagesEqual(default_image
, user
->GetImage()));
757 ExpectNewUserImageInfo(kTestUser1
, default_image_index
, base::FilePath());
759 // Choose a different user image. Verify that the chosen user image is set and
761 const int user_image_index
=
762 user_manager::kFirstDefaultImageIndex
+
763 (default_image_index
- user_manager::kFirstDefaultImageIndex
+ 1) %
764 user_manager::kDefaultImagesCount
;
765 const gfx::ImageSkia
& user_image
=
766 user_manager::GetDefaultImage(user_image_index
);
768 UserImageManager
* user_image_manager
=
769 ChromeUserManager::Get()->GetUserImageManager(kTestUser1
);
770 user_image_manager
->SaveUserDefaultImageIndex(user_image_index
);
772 EXPECT_TRUE(user
->HasDefaultImage());
773 EXPECT_EQ(user_image_index
, user
->image_index());
774 EXPECT_TRUE(test::AreImagesEqual(user_image
, user
->GetImage()));
775 ExpectNewUserImageInfo(kTestUser1
, user_image_index
, base::FilePath());
778 IN_PROC_BROWSER_TEST_F(UserImageManagerPolicyTest
, PRE_PolicyOverridesUser
) {
779 RegisterUser(kTestUser1
);
780 chromeos::StartupUtils::MarkOobeCompleted();
783 // Verifies that when the user chooses a user image and a different image is
784 // then set through policy, the policy takes precedence, overriding the
785 // previously chosen image.
786 IN_PROC_BROWSER_TEST_F(UserImageManagerPolicyTest
, PolicyOverridesUser
) {
787 const user_manager::User
* user
=
788 user_manager::UserManager::Get()->FindUser(kTestUser1
);
791 LoginUser(kTestUser1
);
792 base::RunLoop().RunUntilIdle();
794 policy::CloudPolicyStore
* store
= GetStoreForUser(user
);
797 // Choose a user image. Verify that the chosen user image is set and
799 const gfx::ImageSkia
& default_image
=
800 user_manager::GetDefaultImage(user_manager::kFirstDefaultImageIndex
);
802 UserImageManager
* user_image_manager
=
803 ChromeUserManager::Get()->GetUserImageManager(kTestUser1
);
804 user_image_manager
->SaveUserDefaultImageIndex(
805 user_manager::kFirstDefaultImageIndex
);
807 EXPECT_TRUE(user
->HasDefaultImage());
808 EXPECT_EQ(user_manager::kFirstDefaultImageIndex
, user
->image_index());
809 EXPECT_TRUE(test::AreImagesEqual(default_image
, user
->GetImage()));
810 ExpectNewUserImageInfo(
811 kTestUser1
, user_manager::kFirstDefaultImageIndex
, base::FilePath());
813 // Set policy. Verify that the policy-provided user image is downloaded, set
814 // and persisted, overriding the previously set image.
815 user_policy_
.payload().mutable_useravatarimage()->set_value(
816 ConstructPolicy(test::kUserAvatarImage2RelativePath
));
817 user_policy_
.Build();
818 fake_session_manager_client_
->set_user_policy(kTestUser1
,
819 user_policy_
.GetBlob());
820 run_loop_
.reset(new base::RunLoop
);
824 EXPECT_FALSE(user
->HasDefaultImage());
825 EXPECT_EQ(user_manager::User::USER_IMAGE_EXTERNAL
, user
->image_index());
826 EXPECT_TRUE(test::AreImagesEqual(*policy_image_
, user
->GetImage()));
827 ExpectNewUserImageInfo(kTestUser1
,
828 user_manager::User::USER_IMAGE_EXTERNAL
,
829 GetUserImagePath(kTestUser1
, "jpg"));
831 scoped_ptr
<gfx::ImageSkia
> saved_image
=
832 test::ImageLoader(GetUserImagePath(kTestUser1
, "jpg")).Load();
833 ASSERT_TRUE(saved_image
);
835 // Check image dimensions. Images can't be compared since JPEG is lossy.
836 EXPECT_EQ(policy_image_
->width(), saved_image
->width());
837 EXPECT_EQ(policy_image_
->height(), saved_image
->height());
840 IN_PROC_BROWSER_TEST_F(UserImageManagerPolicyTest
,
841 PRE_UserDoesNotOverridePolicy
) {
842 RegisterUser(kTestUser1
);
843 chromeos::StartupUtils::MarkOobeCompleted();
846 // Verifies that when the user image has been set through policy and the user
847 // chooses a different image, the policy takes precedence, preventing the user
848 // from overriding the previously chosen image.
849 IN_PROC_BROWSER_TEST_F(UserImageManagerPolicyTest
, UserDoesNotOverridePolicy
) {
850 const user_manager::User
* user
=
851 user_manager::UserManager::Get()->FindUser(kTestUser1
);
854 LoginUser(kTestUser1
);
855 base::RunLoop().RunUntilIdle();
857 policy::CloudPolicyStore
* store
= GetStoreForUser(user
);
860 // Set policy. Verify that the policy-provided user image is downloaded, set
862 user_policy_
.payload().mutable_useravatarimage()->set_value(
863 ConstructPolicy(test::kUserAvatarImage2RelativePath
));
864 user_policy_
.Build();
865 fake_session_manager_client_
->set_user_policy(kTestUser1
,
866 user_policy_
.GetBlob());
867 run_loop_
.reset(new base::RunLoop
);
871 EXPECT_FALSE(user
->HasDefaultImage());
872 EXPECT_EQ(user_manager::User::USER_IMAGE_EXTERNAL
, user
->image_index());
873 EXPECT_TRUE(test::AreImagesEqual(*policy_image_
, user
->GetImage()));
874 ExpectNewUserImageInfo(kTestUser1
,
875 user_manager::User::USER_IMAGE_EXTERNAL
,
876 GetUserImagePath(kTestUser1
, "jpg"));
878 scoped_ptr
<gfx::ImageSkia
> saved_image
=
879 test::ImageLoader(GetUserImagePath(kTestUser1
, "jpg")).Load();
880 ASSERT_TRUE(saved_image
);
882 // Check image dimensions. Images can't be compared since JPEG is lossy.
883 EXPECT_EQ(policy_image_
->width(), saved_image
->width());
884 EXPECT_EQ(policy_image_
->height(), saved_image
->height());
886 // Choose a different user image. Verify that the user image does not change
887 // as policy takes precedence.
888 UserImageManager
* user_image_manager
=
889 ChromeUserManager::Get()->GetUserImageManager(kTestUser1
);
890 user_image_manager
->SaveUserDefaultImageIndex(
891 user_manager::kFirstDefaultImageIndex
);
893 EXPECT_FALSE(user
->HasDefaultImage());
894 EXPECT_EQ(user_manager::User::USER_IMAGE_EXTERNAL
, user
->image_index());
895 EXPECT_TRUE(test::AreImagesEqual(*policy_image_
, user
->GetImage()));
896 ExpectNewUserImageInfo(kTestUser1
,
897 user_manager::User::USER_IMAGE_EXTERNAL
,
898 GetUserImagePath(kTestUser1
, "jpg"));
900 saved_image
= test::ImageLoader(GetUserImagePath(kTestUser1
, "jpg")).Load();
901 ASSERT_TRUE(saved_image
);
903 // Check image dimensions. Images can't be compared since JPEG is lossy.
904 EXPECT_EQ(policy_image_
->width(), saved_image
->width());
905 EXPECT_EQ(policy_image_
->height(), saved_image
->height());
908 } // namespace chromeos