OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include <string> |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/compiler_specific.h" |
8 #include "base/file_util.h" | 9 #include "base/file_util.h" |
9 #include "base/memory/ref_counted_memory.h" | 10 #include "base/memory/ref_counted_memory.h" |
| 11 #include "base/memory/scoped_ptr.h" |
10 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 13 #include "base/prefs/pref_change_registrar.h" |
11 #include "base/prefs/pref_service.h" | 14 #include "base/prefs/pref_service.h" |
12 #include "base/prefs/scoped_user_pref_update.h" | 15 #include "base/prefs/scoped_user_pref_update.h" |
| 16 #include "base/run_loop.h" |
13 #include "base/values.h" | 17 #include "base/values.h" |
14 #include "chrome/browser/chrome_notification_types.h" | 18 #include "chrome/browser/chrome_notification_types.h" |
15 #include "chrome/browser/chromeos/login/default_user_images.h" | 19 #include "chrome/browser/chromeos/login/default_user_images.h" |
16 #include "chrome/browser/chromeos/login/mock_user_manager.h" | 20 #include "chrome/browser/chromeos/login/mock_user_manager.h" |
17 #include "chrome/browser/chromeos/login/user_image_manager_impl.h" | |
18 #include "chrome/browser/chromeos/login/user_manager.h" | 21 #include "chrome/browser/chromeos/login/user_manager.h" |
19 #include "chrome/common/chrome_paths.h" | 22 #include "chrome/common/chrome_paths.h" |
20 #include "chrome/common/chrome_switches.h" | 23 #include "chrome/common/chrome_switches.h" |
21 #include "chrome/test/base/in_process_browser_test.h" | 24 #include "chrome/test/base/in_process_browser_test.h" |
22 #include "chrome/test/base/testing_browser_process.h" | 25 #include "chrome/test/base/testing_browser_process.h" |
23 #include "chromeos/chromeos_switches.h" | 26 #include "chromeos/chromeos_switches.h" |
24 #include "content/public/browser/notification_observer.h" | |
25 #include "content/public/browser/notification_registrar.h" | |
26 #include "content/public/browser/notification_service.h" | 27 #include "content/public/browser/notification_service.h" |
27 #include "content/public/test/test_utils.h" | 28 #include "content/public/test/test_utils.h" |
28 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
29 #include "ui/base/layout.h" | 30 #include "ui/base/layout.h" |
30 #include "ui/base/resource/resource_bundle.h" | 31 #include "ui/base/resource/resource_bundle.h" |
31 | 32 |
32 namespace chromeos { | 33 namespace chromeos { |
33 | 34 |
34 const char kTestUser1[] = "test-user@example.com"; | 35 const char kTestUser1[] = "test-user@example.com"; |
35 const char kTestUser2[] = "test-user2@example.com"; | 36 const char kTestUser2[] = "test-user2@example.com"; |
36 | 37 |
37 class UserImageManagerTest : public InProcessBrowserTest, | 38 class UserImageManagerTest : public InProcessBrowserTest { |
38 public content::NotificationObserver, | |
39 public UserManager::Observer { | |
40 protected: | 39 protected: |
41 UserImageManagerTest() { | 40 UserImageManagerTest() { |
42 } | 41 } |
43 | 42 |
44 // InProcessBrowserTest overrides: | 43 // InProcessBrowserTest overrides: |
45 virtual void SetUpOnMainThread() OVERRIDE { | 44 virtual void SetUpOnMainThread() OVERRIDE { |
46 UserManager::Get()->AddObserver(this); | |
47 user_image_manager_ = UserManager::Get()->GetUserImageManager(); | |
48 local_state_ = g_browser_process->local_state(); | 45 local_state_ = g_browser_process->local_state(); |
49 // No migration delay for testing. | |
50 UserImageManagerImpl::user_image_migration_delay_sec = 0; | |
51 } | 46 } |
52 | 47 |
53 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 48 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
54 command_line->AppendSwitch(switches::kLoginManager); | 49 command_line->AppendSwitch(switches::kLoginManager); |
55 command_line->AppendSwitchASCII(switches::kLoginProfile, "user"); | 50 command_line->AppendSwitchASCII(switches::kLoginProfile, "user"); |
56 } | 51 } |
57 | 52 |
58 // content::NotificationObserver overrides: | |
59 virtual void Observe(int type, | |
60 const content::NotificationSource& source, | |
61 const content::NotificationDetails& details) OVERRIDE { | |
62 DCHECK(type == chrome::NOTIFICATION_LOGIN_USER_IMAGE_CHANGED); | |
63 registrar_.Remove(this, chrome::NOTIFICATION_LOGIN_USER_IMAGE_CHANGED, | |
64 content::NotificationService::AllSources()); | |
65 base::MessageLoopForUI::current()->Quit(); | |
66 } | |
67 | |
68 // UserManager::Observer overrides: | |
69 virtual void LocalStateChanged(UserManager* user_manager) OVERRIDE { | |
70 base::MessageLoopForUI::current()->Quit(); | |
71 } | |
72 | |
73 // Adds given user to Local State, if not there. | 53 // Adds given user to Local State, if not there. |
74 void AddUser(const std::string& username) { | 54 void AddUser(const std::string& username) { |
75 ListPrefUpdate users_pref(local_state_, "LoggedInUsers"); | 55 ListPrefUpdate users_pref(local_state_, "LoggedInUsers"); |
76 users_pref->AppendIfNotPresent(new base::StringValue(username)); | 56 users_pref->AppendIfNotPresent(new base::StringValue(username)); |
77 } | 57 } |
78 | 58 |
79 // Logs in |username|. | 59 // Logs in |username|. |
80 void LogIn(const std::string& username) { | 60 void LogIn(const std::string& username) { |
81 UserManager::Get()->UserLoggedIn(username, username, false); | 61 UserManager::Get()->UserLoggedIn(username, username, false); |
82 } | 62 } |
83 | 63 |
84 // Subscribes for image change notification. | |
85 void ExpectImageChange() { | |
86 registrar_.Add(this, chrome::NOTIFICATION_LOGIN_USER_IMAGE_CHANGED, | |
87 content::NotificationService::AllSources()); | |
88 } | |
89 | |
90 // Stores old (pre-migration) user image info. | 64 // Stores old (pre-migration) user image info. |
91 void SetOldUserImageInfo(const std::string& username, | 65 void SetOldUserImageInfo(const std::string& username, |
92 int image_index, | 66 int image_index, |
93 const base::FilePath& image_path) { | 67 const base::FilePath& image_path) { |
94 AddUser(username); | 68 AddUser(username); |
95 DictionaryPrefUpdate images_pref(local_state_, "UserImages"); | 69 DictionaryPrefUpdate images_pref(local_state_, "UserImages"); |
96 base::DictionaryValue* image_properties = new base::DictionaryValue(); | 70 base::DictionaryValue* image_properties = new base::DictionaryValue(); |
97 image_properties->Set( | 71 image_properties->Set( |
98 "index", base::Value::CreateIntegerValue(image_index)); | 72 "index", base::Value::CreateIntegerValue(image_index)); |
99 image_properties->Set( | 73 image_properties->Set( |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 } | 140 } |
167 | 141 |
168 // Returns the image path for user |username| with specified |extension|. | 142 // Returns the image path for user |username| with specified |extension|. |
169 base::FilePath GetUserImagePath(const std::string& username, | 143 base::FilePath GetUserImagePath(const std::string& username, |
170 const std::string& extension) { | 144 const std::string& extension) { |
171 base::FilePath user_data_dir; | 145 base::FilePath user_data_dir; |
172 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); | 146 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); |
173 return user_data_dir.Append(username).AddExtension(extension); | 147 return user_data_dir.Append(username).AddExtension(extension); |
174 } | 148 } |
175 | 149 |
176 UserImageManager* user_image_manager_; | |
177 PrefService* local_state_; | 150 PrefService* local_state_; |
178 content::NotificationRegistrar registrar_; | |
179 | 151 |
180 private: | 152 private: |
181 DISALLOW_COPY_AND_ASSIGN(UserImageManagerTest); | 153 DISALLOW_COPY_AND_ASSIGN(UserImageManagerTest); |
182 }; | 154 }; |
183 | 155 |
184 IN_PROC_BROWSER_TEST_F(UserImageManagerTest, PRE_DefaultUserImagePreserved) { | 156 IN_PROC_BROWSER_TEST_F(UserImageManagerTest, PRE_DefaultUserImagePreserved) { |
185 // Setup an old default (stock) user image. | 157 // Setup an old default (stock) user image. |
186 ScopedUserManagerEnabler(new MockUserManager); | 158 ScopedUserManagerEnabler(new MockUserManager); |
187 SetOldUserImageInfo(kTestUser1, kFirstDefaultImageIndex, base::FilePath()); | 159 SetOldUserImageInfo(kTestUser1, kFirstDefaultImageIndex, base::FilePath()); |
188 } | 160 } |
189 | 161 |
190 IN_PROC_BROWSER_TEST_F(UserImageManagerTest, DefaultUserImagePreserved) { | 162 IN_PROC_BROWSER_TEST_F(UserImageManagerTest, DefaultUserImagePreserved) { |
191 UserManager::Get()->GetUsers(); // Load users. | 163 UserManager::Get()->GetUsers(); // Load users. |
192 // Old info preserved. | 164 // Old info preserved. |
193 ExpectOldUserImageInfo(kTestUser1, kFirstDefaultImageIndex, base::FilePath()); | 165 ExpectOldUserImageInfo(kTestUser1, kFirstDefaultImageIndex, base::FilePath()); |
194 LogIn(kTestUser1); | 166 LogIn(kTestUser1); |
195 // Wait for migration. | |
196 content::RunMessageLoop(); | |
197 // Image info is migrated now. | 167 // Image info is migrated now. |
198 ExpectNewUserImageInfo(kTestUser1, kFirstDefaultImageIndex, base::FilePath()); | 168 ExpectNewUserImageInfo(kTestUser1, kFirstDefaultImageIndex, base::FilePath()); |
199 } | 169 } |
200 | 170 |
201 IN_PROC_BROWSER_TEST_F(UserImageManagerTest, PRE_OtherUsersUnaffected) { | 171 IN_PROC_BROWSER_TEST_F(UserImageManagerTest, PRE_OtherUsersUnaffected) { |
202 // Setup two users with stock images. | 172 // Setup two users with stock images. |
203 ScopedUserManagerEnabler(new MockUserManager); | 173 ScopedUserManagerEnabler(new MockUserManager); |
204 SetOldUserImageInfo(kTestUser1, kFirstDefaultImageIndex, base::FilePath()); | 174 SetOldUserImageInfo(kTestUser1, kFirstDefaultImageIndex, base::FilePath()); |
205 SetOldUserImageInfo(kTestUser2, kFirstDefaultImageIndex + 1, | 175 SetOldUserImageInfo(kTestUser2, kFirstDefaultImageIndex + 1, |
206 base::FilePath()); | 176 base::FilePath()); |
207 } | 177 } |
208 | 178 |
209 IN_PROC_BROWSER_TEST_F(UserImageManagerTest, OtherUsersUnaffected) { | 179 IN_PROC_BROWSER_TEST_F(UserImageManagerTest, OtherUsersUnaffected) { |
210 UserManager::Get()->GetUsers(); // Load users. | 180 UserManager::Get()->GetUsers(); // Load users. |
211 // Old info preserved. | 181 // Old info preserved. |
212 ExpectOldUserImageInfo(kTestUser1, kFirstDefaultImageIndex, base::FilePath()); | 182 ExpectOldUserImageInfo(kTestUser1, kFirstDefaultImageIndex, base::FilePath()); |
213 ExpectOldUserImageInfo(kTestUser2, kFirstDefaultImageIndex + 1, | 183 ExpectOldUserImageInfo(kTestUser2, kFirstDefaultImageIndex + 1, |
214 base::FilePath()); | 184 base::FilePath()); |
215 LogIn(kTestUser1); | 185 LogIn(kTestUser1); |
216 // Wait for migration. | |
217 content::RunMessageLoop(); | |
218 // Image info is migrated for the first user and unaffected for the rest. | 186 // Image info is migrated for the first user and unaffected for the rest. |
219 ExpectNewUserImageInfo(kTestUser1, kFirstDefaultImageIndex, base::FilePath()); | 187 ExpectNewUserImageInfo(kTestUser1, kFirstDefaultImageIndex, base::FilePath()); |
220 ExpectOldUserImageInfo(kTestUser2, kFirstDefaultImageIndex + 1, | 188 ExpectOldUserImageInfo(kTestUser2, kFirstDefaultImageIndex + 1, |
221 base::FilePath()); | 189 base::FilePath()); |
222 } | 190 } |
223 | 191 |
224 IN_PROC_BROWSER_TEST_F(UserImageManagerTest, PRE_PRE_NonJPEGImageFromFile) { | 192 IN_PROC_BROWSER_TEST_F(UserImageManagerTest, PRE_PRE_NonJPEGImageFromFile) { |
225 // Setup a user with non-JPEG image. | 193 // Setup a user with non-JPEG image. |
226 ScopedUserManagerEnabler(new MockUserManager); | 194 ScopedUserManagerEnabler(new MockUserManager); |
227 SaveUserImagePNG( | 195 SaveUserImagePNG( |
228 kTestUser1, kDefaultImageResourceIDs[kFirstDefaultImageIndex]); | 196 kTestUser1, kDefaultImageResourceIDs[kFirstDefaultImageIndex]); |
229 } | 197 } |
230 | 198 |
231 IN_PROC_BROWSER_TEST_F(UserImageManagerTest, PRE_NonJPEGImageFromFile) { | 199 IN_PROC_BROWSER_TEST_F(UserImageManagerTest, PRE_NonJPEGImageFromFile) { |
232 UserManager::Get()->GetUsers(); // Load users. | 200 UserManager::Get()->GetUsers(); // Load users. |
233 // Old info preserved. | 201 // Old info preserved. |
234 ExpectOldUserImageInfo(kTestUser1, User::kExternalImageIndex, | 202 ExpectOldUserImageInfo(kTestUser1, User::kExternalImageIndex, |
235 GetUserImagePath(kTestUser1, "png")); | 203 GetUserImagePath(kTestUser1, "png")); |
236 const User* user = UserManager::Get()->FindUser(kTestUser1); | 204 const User* user = UserManager::Get()->FindUser(kTestUser1); |
237 EXPECT_TRUE(user->image_is_stub()); | 205 EXPECT_TRUE(user->image_is_stub()); |
| 206 |
| 207 base::RunLoop run_loop; |
| 208 PrefChangeRegistrar pref_change_registrar_; |
| 209 pref_change_registrar_.Init(local_state_); |
| 210 pref_change_registrar_.Add("UserImages", run_loop.QuitClosure()); |
238 LogIn(kTestUser1); | 211 LogIn(kTestUser1); |
| 212 |
239 // Wait for migration. | 213 // Wait for migration. |
240 content::RunMessageLoop(); | 214 run_loop.Run(); |
| 215 |
241 // Image info is migrated and the image is converted to JPG. | 216 // Image info is migrated and the image is converted to JPG. |
242 ExpectNewUserImageInfo(kTestUser1, User::kExternalImageIndex, | 217 ExpectNewUserImageInfo(kTestUser1, User::kExternalImageIndex, |
243 GetUserImagePath(kTestUser1, "jpg")); | 218 GetUserImagePath(kTestUser1, "jpg")); |
244 user = UserManager::Get()->GetLoggedInUser(); | 219 user = UserManager::Get()->GetLoggedInUser(); |
245 ASSERT_TRUE(user); | 220 ASSERT_TRUE(user); |
246 EXPECT_FALSE(user->image_is_safe_format()); | 221 EXPECT_FALSE(user->image_is_safe_format()); |
247 // Check image dimensions. | 222 // Check image dimensions. |
248 const gfx::ImageSkia& saved_image = GetDefaultImage(kFirstDefaultImageIndex); | 223 const gfx::ImageSkia& saved_image = GetDefaultImage(kFirstDefaultImageIndex); |
249 EXPECT_EQ(saved_image.width(), user->image().width()); | 224 EXPECT_EQ(saved_image.width(), user->image().width()); |
250 EXPECT_EQ(saved_image.height(), user->image().height()); | 225 EXPECT_EQ(saved_image.height(), user->image().height()); |
251 } | 226 } |
252 | 227 |
253 // http://crbug.com/257009. | 228 IN_PROC_BROWSER_TEST_F(UserImageManagerTest, NonJPEGImageFromFile) { |
254 IN_PROC_BROWSER_TEST_F(UserImageManagerTest, DISABLED_NonJPEGImageFromFile) { | |
255 ExpectImageChange(); | |
256 UserManager::Get()->GetUsers(); // Load users. | 229 UserManager::Get()->GetUsers(); // Load users. |
257 // Wait for image load. | |
258 content::RunMessageLoop(); | |
259 // Now the migrated image is used. | |
260 const User* user = UserManager::Get()->FindUser(kTestUser1); | 230 const User* user = UserManager::Get()->FindUser(kTestUser1); |
261 ASSERT_TRUE(user); | 231 ASSERT_TRUE(user); |
| 232 // Wait for image load. |
| 233 if (user->image_index() == User::kInvalidImageIndex) { |
| 234 content::WindowedNotificationObserver( |
| 235 chrome::NOTIFICATION_LOGIN_USER_IMAGE_CHANGED, |
| 236 content::NotificationService::AllSources()).Wait(); |
| 237 } |
| 238 // Now the migrated image is used. |
262 EXPECT_TRUE(user->image_is_safe_format()); | 239 EXPECT_TRUE(user->image_is_safe_format()); |
263 // Check image dimensions. Images can't be compared since JPEG is lossy. | 240 // Check image dimensions. Images can't be compared since JPEG is lossy. |
264 const gfx::ImageSkia& saved_image = GetDefaultImage(kFirstDefaultImageIndex); | 241 const gfx::ImageSkia& saved_image = GetDefaultImage(kFirstDefaultImageIndex); |
265 EXPECT_EQ(saved_image.width(), user->image().width()); | 242 EXPECT_EQ(saved_image.width(), user->image().width()); |
266 EXPECT_EQ(saved_image.height(), user->image().height()); | 243 EXPECT_EQ(saved_image.height(), user->image().height()); |
267 } | 244 } |
268 | 245 |
269 } // namespace chromeos | 246 } // namespace chromeos |
OLD | NEW |