[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / profiles / profile_info_cache_unittest.cc
blob1907a22cc300bac80df0b36404e4f777e575ae9e
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
3 // found in the LICENSE file.
5 #include "chrome/browser/profiles/profile_info_cache_unittest.h"
7 #include <vector>
9 #include "base/file_util.h"
10 #include "base/prefs/testing_pref_service.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/time/time.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/prefs/pref_service_syncable.h"
17 #include "chrome/browser/profiles/profile_avatar_downloader.h"
18 #include "chrome/browser/profiles/profile_avatar_icon_util.h"
19 #include "chrome/browser/profiles/profile_info_cache.h"
20 #include "chrome/browser/profiles/profile_manager.h"
21 #include "chrome/test/base/testing_browser_process.h"
22 #include "content/public/browser/notification_observer.h"
23 #include "content/public/browser/notification_registrar.h"
24 #include "content/public/browser/notification_service.h"
25 #include "content/public/test/test_browser_thread_bundle.h"
26 #include "content/public/test/test_utils.h"
27 #include "third_party/skia/include/core/SkBitmap.h"
28 #include "ui/base/resource/resource_bundle.h"
29 #include "ui/gfx/image/image.h"
30 #include "ui/gfx/image/image_unittest_util.h"
32 using base::ASCIIToUTF16;
33 using base::UTF8ToUTF16;
34 using content::BrowserThread;
36 ProfileNameVerifierObserver::ProfileNameVerifierObserver(
37 TestingProfileManager* testing_profile_manager)
38 : testing_profile_manager_(testing_profile_manager) {
39 DCHECK(testing_profile_manager_);
42 ProfileNameVerifierObserver::~ProfileNameVerifierObserver() {
45 void ProfileNameVerifierObserver::OnProfileAdded(
46 const base::FilePath& profile_path) {
47 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
48 GetCache()->GetIndexOfProfileWithPath(profile_path));
49 EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end());
50 profile_names_.insert(profile_name);
53 void ProfileNameVerifierObserver::OnProfileWillBeRemoved(
54 const base::FilePath& profile_path) {
55 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
56 GetCache()->GetIndexOfProfileWithPath(profile_path));
57 EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end());
58 profile_names_.erase(profile_name);
61 void ProfileNameVerifierObserver::OnProfileWasRemoved(
62 const base::FilePath& profile_path,
63 const base::string16& profile_name) {
64 EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end());
67 void ProfileNameVerifierObserver::OnProfileNameChanged(
68 const base::FilePath& profile_path,
69 const base::string16& old_profile_name) {
70 base::string16 new_profile_name = GetCache()->GetNameOfProfileAtIndex(
71 GetCache()->GetIndexOfProfileWithPath(profile_path));
72 EXPECT_TRUE(profile_names_.find(old_profile_name) != profile_names_.end());
73 EXPECT_TRUE(profile_names_.find(new_profile_name) == profile_names_.end());
74 profile_names_.erase(old_profile_name);
75 profile_names_.insert(new_profile_name);
78 void ProfileNameVerifierObserver::OnProfileAvatarChanged(
79 const base::FilePath& profile_path) {
80 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
81 GetCache()->GetIndexOfProfileWithPath(profile_path));
82 EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end());
85 ProfileInfoCache* ProfileNameVerifierObserver::GetCache() {
86 return testing_profile_manager_->profile_info_cache();
89 ProfileInfoCacheTest::ProfileInfoCacheTest()
90 : testing_profile_manager_(TestingBrowserProcess::GetGlobal()),
91 name_observer_(&testing_profile_manager_) {
94 ProfileInfoCacheTest::~ProfileInfoCacheTest() {
97 void ProfileInfoCacheTest::SetUp() {
98 ASSERT_TRUE(testing_profile_manager_.SetUp());
99 testing_profile_manager_.profile_info_cache()->AddObserver(&name_observer_);
102 void ProfileInfoCacheTest::TearDown() {
103 // Drain the UI thread to make sure all tasks are completed. This prevents
104 // memory leaks.
105 base::RunLoop().RunUntilIdle();
108 ProfileInfoCache* ProfileInfoCacheTest::GetCache() {
109 return testing_profile_manager_.profile_info_cache();
112 base::FilePath ProfileInfoCacheTest::GetProfilePath(
113 const std::string& base_name) {
114 return testing_profile_manager_.profile_manager()->user_data_dir().
115 AppendASCII(base_name);
118 void ProfileInfoCacheTest::ResetCache() {
119 testing_profile_manager_.DeleteProfileInfoCache();
122 TEST_F(ProfileInfoCacheTest, AddProfiles) {
123 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
125 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
126 for (uint32 i = 0; i < 4; ++i) {
127 base::FilePath profile_path =
128 GetProfilePath(base::StringPrintf("path_%ud", i));
129 base::string16 profile_name =
130 ASCIIToUTF16(base::StringPrintf("name_%ud", i));
131 const SkBitmap* icon = rb.GetImageNamed(
132 profiles::GetDefaultAvatarIconResourceIDAtIndex(
133 i)).ToSkBitmap();
134 std::string managed_user_id = i == 3 ? "TEST_ID" : "";
136 GetCache()->AddProfileToCache(profile_path, profile_name, base::string16(),
137 i, managed_user_id);
138 GetCache()->SetBackgroundStatusOfProfileAtIndex(i, true);
139 base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
140 GetCache()->SetGAIANameOfProfileAtIndex(i, gaia_name);
142 EXPECT_EQ(i + 1, GetCache()->GetNumberOfProfiles());
143 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
144 EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i));
145 const SkBitmap* actual_icon =
146 GetCache()->GetAvatarIconOfProfileAtIndex(i).ToSkBitmap();
147 EXPECT_EQ(icon->width(), actual_icon->width());
148 EXPECT_EQ(icon->height(), actual_icon->height());
149 EXPECT_EQ(i == 3, GetCache()->ProfileIsManagedAtIndex(i));
150 EXPECT_EQ(i == 3, GetCache()->IsOmittedProfileAtIndex(i));
151 EXPECT_EQ(managed_user_id, GetCache()->GetManagedUserIdOfProfileAtIndex(i));
154 // Reset the cache and test the it reloads correctly.
155 ResetCache();
157 EXPECT_EQ(4u, GetCache()->GetNumberOfProfiles());
158 for (uint32 i = 0; i < 4; ++i) {
159 base::FilePath profile_path =
160 GetProfilePath(base::StringPrintf("path_%ud", i));
161 EXPECT_EQ(i, GetCache()->GetIndexOfProfileWithPath(profile_path));
162 base::string16 profile_name =
163 ASCIIToUTF16(base::StringPrintf("name_%ud", i));
164 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
165 EXPECT_EQ(i, GetCache()->GetAvatarIconIndexOfProfileAtIndex(i));
166 EXPECT_EQ(true, GetCache()->GetBackgroundStatusOfProfileAtIndex(i));
167 base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
168 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(i));
172 TEST_F(ProfileInfoCacheTest, DeleteProfile) {
173 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
175 base::FilePath path_1 = GetProfilePath("path_1");
176 GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("name_1"),
177 base::string16(), 0, std::string());
178 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
180 base::FilePath path_2 = GetProfilePath("path_2");
181 base::string16 name_2 = ASCIIToUTF16("name_2");
182 GetCache()->AddProfileToCache(path_2, name_2, base::string16(), 0,
183 std::string());
184 EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles());
186 GetCache()->DeleteProfileFromCache(path_1);
187 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
188 EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(0));
190 GetCache()->DeleteProfileFromCache(path_2);
191 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
194 TEST_F(ProfileInfoCacheTest, MutateProfile) {
195 GetCache()->AddProfileToCache(
196 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
197 base::string16(), 0, std::string());
198 GetCache()->AddProfileToCache(
199 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
200 base::string16(), 0, std::string());
202 base::string16 new_name = ASCIIToUTF16("new_name");
203 GetCache()->SetNameOfProfileAtIndex(1, new_name);
204 EXPECT_EQ(new_name, GetCache()->GetNameOfProfileAtIndex(1));
205 EXPECT_NE(new_name, GetCache()->GetNameOfProfileAtIndex(0));
207 base::string16 new_user_name = ASCIIToUTF16("user_name");
208 GetCache()->SetUserNameOfProfileAtIndex(1, new_user_name);
209 EXPECT_EQ(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(1));
210 EXPECT_NE(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(0));
212 size_t new_icon_index = 3;
213 GetCache()->SetAvatarIconOfProfileAtIndex(1, new_icon_index);
214 // Not much to test.
215 GetCache()->GetAvatarIconOfProfileAtIndex(1);
218 TEST_F(ProfileInfoCacheTest, Sort) {
219 base::string16 name_a = ASCIIToUTF16("apple");
220 GetCache()->AddProfileToCache(
221 GetProfilePath("path_a"), name_a, base::string16(), 0, std::string());
223 base::string16 name_c = ASCIIToUTF16("cat");
224 GetCache()->AddProfileToCache(
225 GetProfilePath("path_c"), name_c, base::string16(), 0, std::string());
227 // Sanity check the initial order.
228 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
229 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
231 // Add a new profile (start with a capital to test case insensitive sorting.
232 base::string16 name_b = ASCIIToUTF16("Banana");
233 GetCache()->AddProfileToCache(
234 GetProfilePath("path_b"), name_b, base::string16(), 0, std::string());
236 // Verify the new order.
237 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
238 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(1));
239 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(2));
241 // Change the name of an existing profile.
242 name_a = UTF8ToUTF16("dog");
243 GetCache()->SetNameOfProfileAtIndex(0, name_a);
245 // Verify the new order.
246 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
247 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
248 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(2));
250 // Delete a profile.
251 GetCache()->DeleteProfileFromCache(GetProfilePath("path_c"));
253 // Verify the new order.
254 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
255 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(1));
258 TEST_F(ProfileInfoCacheTest, BackgroundModeStatus) {
259 GetCache()->AddProfileToCache(
260 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
261 base::string16(), 0, std::string());
262 GetCache()->AddProfileToCache(
263 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
264 base::string16(), 0, std::string());
266 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
267 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
269 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, true);
271 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
272 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
274 GetCache()->SetBackgroundStatusOfProfileAtIndex(0, true);
276 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
277 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
279 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, false);
281 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
282 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
285 TEST_F(ProfileInfoCacheTest, ProfileActiveTime) {
286 GetCache()->AddProfileToCache(
287 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
288 base::string16(), 0, std::string());
289 EXPECT_EQ(base::Time(), GetCache()->GetProfileActiveTimeAtIndex(0));
290 // Before & After times are artificially shifted because just relying upon
291 // the system time can yield problems due to inaccuracies in the
292 // underlying storage system (which uses a double with only 52 bits of
293 // precision to store the 64-bit "time" number). http://crbug.com/346827
294 base::Time before = base::Time::Now();
295 before -= base::TimeDelta::FromSeconds(1);
296 GetCache()->SetProfileActiveTimeAtIndex(0);
297 base::Time after = base::Time::Now();
298 after += base::TimeDelta::FromSeconds(1);
299 EXPECT_LE(before, GetCache()->GetProfileActiveTimeAtIndex(0));
300 EXPECT_GE(after, GetCache()->GetProfileActiveTimeAtIndex(0));
303 TEST_F(ProfileInfoCacheTest, GAIAName) {
304 GetCache()->AddProfileToCache(
305 GetProfilePath("path_1"), ASCIIToUTF16("Person 1"),
306 base::string16(), 0, std::string());
307 base::string16 profile_name(ASCIIToUTF16("Person 2"));
308 GetCache()->AddProfileToCache(
309 GetProfilePath("path_2"), profile_name, base::string16(), 0,
310 std::string());
312 int index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
313 int index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
315 // Sanity check.
316 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty());
317 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index2).empty());
319 // Set GAIA name. This re-sorts the cache.
320 base::string16 gaia_name(ASCIIToUTF16("Pat Smith"));
321 GetCache()->SetGAIANameOfProfileAtIndex(index2, gaia_name);
322 index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
323 index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
325 // Since there is a GAIA name, we use that as a display name.
326 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty());
327 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2));
328 EXPECT_EQ(gaia_name, GetCache()->GetNameOfProfileAtIndex(index2));
330 // Don't use GAIA name as profile name. This re-sorts the cache.
331 base::string16 custom_name(ASCIIToUTF16("Custom name"));
332 GetCache()->SetNameOfProfileAtIndex(index2, custom_name);
333 index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
334 index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
336 EXPECT_EQ(custom_name, GetCache()->GetNameOfProfileAtIndex(index2));
337 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2));
340 TEST_F(ProfileInfoCacheTest, GAIAPicture) {
341 GetCache()->AddProfileToCache(
342 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
343 base::string16(), 0, std::string());
344 GetCache()->AddProfileToCache(
345 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
346 base::string16(), 0, std::string());
348 // Sanity check.
349 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
350 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(1));
351 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0));
352 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
354 // The profile icon should be the default one.
355 int id = profiles::GetDefaultAvatarIconResourceIDAtIndex(0);
356 const gfx::Image& profile_image(
357 ResourceBundle::GetSharedInstance().GetImageNamed(id));
358 EXPECT_TRUE(gfx::test::IsEqual(
359 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
361 // Set GAIA picture.
362 gfx::Image gaia_image(gfx::test::CreateImage());
363 GetCache()->SetGAIAPictureOfProfileAtIndex(1, &gaia_image);
364 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
365 EXPECT_TRUE(gfx::test::IsEqual(
366 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
367 EXPECT_TRUE(gfx::test::IsEqual(
368 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
370 // Use GAIA picture as profile picture.
371 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, true);
372 EXPECT_TRUE(gfx::test::IsEqual(
373 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
374 EXPECT_TRUE(gfx::test::IsEqual(
375 gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
377 // Don't use GAIA picture as profile picture.
378 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, false);
379 EXPECT_TRUE(gfx::test::IsEqual(
380 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
381 EXPECT_TRUE(gfx::test::IsEqual(
382 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
385 TEST_F(ProfileInfoCacheTest, PersistGAIAPicture) {
386 GetCache()->AddProfileToCache(
387 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
388 base::string16(), 0, std::string());
389 gfx::Image gaia_image(gfx::test::CreateImage());
391 content::WindowedNotificationObserver save_observer(
392 chrome::NOTIFICATION_PROFILE_CACHE_PICTURE_SAVED,
393 content::NotificationService::AllSources());
394 GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_image);
395 EXPECT_TRUE(gfx::test::IsEqual(
396 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
398 // Wait for the file to be written to disk then reset the cache.
399 save_observer.Wait();
400 ResetCache();
402 // Try to get the GAIA picture. This should return NULL until the read from
403 // disk is done.
404 content::WindowedNotificationObserver read_observer(
405 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED,
406 content::NotificationService::AllSources());
407 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
408 read_observer.Wait();
409 EXPECT_TRUE(gfx::test::IsEqual(
410 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
413 TEST_F(ProfileInfoCacheTest, SetManagedUserId) {
414 GetCache()->AddProfileToCache(
415 GetProfilePath("test"), ASCIIToUTF16("Test"),
416 base::string16(), 0, std::string());
417 EXPECT_FALSE(GetCache()->ProfileIsManagedAtIndex(0));
419 GetCache()->SetManagedUserIdOfProfileAtIndex(0, "TEST_ID");
420 EXPECT_TRUE(GetCache()->ProfileIsManagedAtIndex(0));
421 EXPECT_EQ("TEST_ID", GetCache()->GetManagedUserIdOfProfileAtIndex(0));
423 ResetCache();
424 EXPECT_TRUE(GetCache()->ProfileIsManagedAtIndex(0));
426 GetCache()->SetManagedUserIdOfProfileAtIndex(0, std::string());
427 EXPECT_FALSE(GetCache()->ProfileIsManagedAtIndex(0));
428 EXPECT_EQ("", GetCache()->GetManagedUserIdOfProfileAtIndex(0));
431 TEST_F(ProfileInfoCacheTest, EmptyGAIAInfo) {
432 base::string16 profile_name = ASCIIToUTF16("name_1");
433 int id = profiles::GetDefaultAvatarIconResourceIDAtIndex(0);
434 const gfx::Image& profile_image(
435 ResourceBundle::GetSharedInstance().GetImageNamed(id));
437 GetCache()->AddProfileToCache(
438 GetProfilePath("path_1"), profile_name, base::string16(), 0,
439 std::string());
441 // Set empty GAIA info.
442 GetCache()->SetGAIANameOfProfileAtIndex(0, base::string16());
443 GetCache()->SetGAIAPictureOfProfileAtIndex(0, NULL);
444 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(0, true);
446 // Verify that the profile name and picture are not empty.
447 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(0));
448 EXPECT_TRUE(gfx::test::IsEqual(
449 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(0)));
452 TEST_F(ProfileInfoCacheTest, CreateManagedTestingProfile) {
453 testing_profile_manager_.CreateTestingProfile("default");
454 base::string16 managed_user_name = ASCIIToUTF16("Supervised User");
455 testing_profile_manager_.CreateTestingProfile(
456 "test1", scoped_ptr<PrefServiceSyncable>(),
457 managed_user_name, 0, "TEST_ID", TestingProfile::TestingFactories());
458 for (size_t i = 0; i < GetCache()->GetNumberOfProfiles(); i++) {
459 bool is_managed =
460 GetCache()->GetNameOfProfileAtIndex(i) == managed_user_name;
461 EXPECT_EQ(is_managed, GetCache()->ProfileIsManagedAtIndex(i));
462 std::string managed_user_id = is_managed ? "TEST_ID" : "";
463 EXPECT_EQ(managed_user_id, GetCache()->GetManagedUserIdOfProfileAtIndex(i));
466 // Managed profiles have a custom theme, which needs to be deleted on the FILE
467 // thread. Reset the profile manager now so everything is deleted while we
468 // still have a FILE thread.
469 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
472 TEST_F(ProfileInfoCacheTest, AddStubProfile) {
473 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
475 // Add some profiles with and without a '.' in their paths.
476 const struct {
477 const char* profile_path;
478 const char* profile_name;
479 } kTestCases[] = {
480 { "path.test0", "name_0" },
481 { "path_test1", "name_1" },
482 { "path.test2", "name_2" },
483 { "path_test3", "name_3" },
486 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) {
487 base::FilePath profile_path = GetProfilePath(kTestCases[i].profile_path);
488 base::string16 profile_name = ASCIIToUTF16(kTestCases[i].profile_name);
490 GetCache()->AddProfileToCache(profile_path, profile_name, base::string16(),
491 i, "");
493 EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i));
494 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
497 ASSERT_EQ(4U, GetCache()->GetNumberOfProfiles());
499 // Check that the profiles can be extracted from the local state.
500 std::vector<base::string16> names = ProfileInfoCache::GetProfileNames();
501 for (size_t i = 0; i < 4; i++)
502 ASSERT_FALSE(names[i].empty());
505 TEST_F(ProfileInfoCacheTest, DownloadHighResAvatarTest) {
506 EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles());
507 base::FilePath path_1 = GetProfilePath("path_1");
508 GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("name_1"),
509 base::string16(), 0, std::string());
510 EXPECT_EQ(1U, GetCache()->GetNumberOfProfiles());
512 // We haven't downloaded any high-res avatars yet.
513 EXPECT_EQ(0U, GetCache()->cached_avatar_images_.size());
514 EXPECT_EQ(0U, GetCache()->avatar_images_downloads_in_progress_.size());
515 EXPECT_FALSE(GetCache()->GetHighResAvatarOfProfileAtIndex(0));
517 // Simulate downloading a high-res avatar.
518 const size_t kIconIndex = 0;
519 ProfileAvatarDownloader avatar_downloader(
520 kIconIndex, GetCache()->GetPathOfProfileAtIndex(0), GetCache());
522 // Put a real bitmap into "bitmap". 2x2 bitmap of green 32 bit pixels.
523 SkBitmap bitmap;
524 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 2, 2);
525 bitmap.allocPixels();
526 bitmap.eraseColor(SK_ColorGREEN);
528 avatar_downloader.OnFetchComplete(
529 GURL("http://www.google.com/avatar.png"), &bitmap);
531 std::string file_name =
532 profiles::GetDefaultAvatarIconFileNameAtIndex(kIconIndex);
534 // The file should have been cached and saved.
535 EXPECT_EQ(0U, GetCache()->avatar_images_downloads_in_progress_.size());
536 EXPECT_EQ(1U, GetCache()->cached_avatar_images_.size());
537 EXPECT_TRUE(GetCache()->GetHighResAvatarOfProfileAtIndex(0));
538 EXPECT_EQ(GetCache()->cached_avatar_images_[file_name],
539 GetCache()->GetHighResAvatarOfProfileAtIndex(0));
541 // Make sure everything has completed, and the file has been written to disk.
542 base::RunLoop().RunUntilIdle();
544 // Clean up.
545 base::FilePath icon_path =
546 profiles::GetPathOfHighResAvatarAtIndex(kIconIndex);
547 EXPECT_NE(std::string::npos, icon_path.MaybeAsASCII().find(file_name));
548 EXPECT_TRUE(base::PathExists(icon_path));
549 EXPECT_TRUE(base::DeleteFile(icon_path, true));
550 EXPECT_FALSE(base::PathExists(icon_path));