Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / profiles / profile_info_cache_unittest.cc
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.
4
5 #include "chrome/browser/profiles/profile_info_cache_unittest.h"
6
7 #include <vector>
8
9 #include "base/prefs/testing_pref_service.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/time/time.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/chrome_notification_types.h"
15 #include "chrome/browser/prefs/pref_service_syncable.h"
16 #include "chrome/browser/profiles/profile_info_cache.h"
17 #include "chrome/browser/profiles/profile_manager.h"
18 #include "chrome/test/base/testing_browser_process.h"
19 #include "content/public/browser/notification_observer.h"
20 #include "content/public/browser/notification_registrar.h"
21 #include "content/public/browser/notification_service.h"
22 #include "content/public/test/test_browser_thread_bundle.h"
23 #include "content/public/test/test_utils.h"
24 #include "third_party/skia/include/core/SkBitmap.h"
25 #include "ui/base/resource/resource_bundle.h"
26 #include "ui/gfx/image/image.h"
27 #include "ui/gfx/image/image_unittest_util.h"
28
29 using base::ASCIIToUTF16;
30 using base::UTF8ToUTF16;
31 using content::BrowserThread;
32
33 ProfileNameVerifierObserver::ProfileNameVerifierObserver(
34     TestingProfileManager* testing_profile_manager)
35     : testing_profile_manager_(testing_profile_manager) {
36   DCHECK(testing_profile_manager_);
37 }
38
39 ProfileNameVerifierObserver::~ProfileNameVerifierObserver() {
40 }
41
42 void ProfileNameVerifierObserver::OnProfileAdded(
43     const base::FilePath& profile_path) {
44   base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
45       GetCache()->GetIndexOfProfileWithPath(profile_path));
46   EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end());
47   profile_names_.insert(profile_name);
48 }
49
50 void ProfileNameVerifierObserver::OnProfileWillBeRemoved(
51     const base::FilePath& profile_path) {
52   base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
53       GetCache()->GetIndexOfProfileWithPath(profile_path));
54   EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end());
55   profile_names_.erase(profile_name);
56 }
57
58 void ProfileNameVerifierObserver::OnProfileWasRemoved(
59     const base::FilePath& profile_path,
60     const base::string16& profile_name) {
61   EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end());
62 }
63
64 void ProfileNameVerifierObserver::OnProfileNameChanged(
65     const base::FilePath& profile_path,
66     const base::string16& old_profile_name) {
67   base::string16 new_profile_name = GetCache()->GetNameOfProfileAtIndex(
68       GetCache()->GetIndexOfProfileWithPath(profile_path));
69   EXPECT_TRUE(profile_names_.find(old_profile_name) != profile_names_.end());
70   EXPECT_TRUE(profile_names_.find(new_profile_name) == profile_names_.end());
71   profile_names_.erase(old_profile_name);
72   profile_names_.insert(new_profile_name);
73 }
74
75 void ProfileNameVerifierObserver::OnProfileAvatarChanged(
76     const base::FilePath& profile_path) {
77   base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
78       GetCache()->GetIndexOfProfileWithPath(profile_path));
79   EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end());
80 }
81
82 ProfileInfoCache* ProfileNameVerifierObserver::GetCache() {
83   return testing_profile_manager_->profile_info_cache();
84 }
85
86 ProfileInfoCacheTest::ProfileInfoCacheTest()
87     : testing_profile_manager_(TestingBrowserProcess::GetGlobal()),
88       name_observer_(&testing_profile_manager_) {
89 }
90
91 ProfileInfoCacheTest::~ProfileInfoCacheTest() {
92 }
93
94 void ProfileInfoCacheTest::SetUp() {
95   ASSERT_TRUE(testing_profile_manager_.SetUp());
96   testing_profile_manager_.profile_info_cache()->AddObserver(&name_observer_);
97 }
98
99 void ProfileInfoCacheTest::TearDown() {
100   // Drain the UI thread to make sure all tasks are completed. This prevents
101   // memory leaks.
102   base::RunLoop().RunUntilIdle();
103 }
104
105 ProfileInfoCache* ProfileInfoCacheTest::GetCache() {
106   return testing_profile_manager_.profile_info_cache();
107 }
108
109 base::FilePath ProfileInfoCacheTest::GetProfilePath(
110     const std::string& base_name) {
111   return testing_profile_manager_.profile_manager()->user_data_dir().
112       AppendASCII(base_name);
113 }
114
115 void ProfileInfoCacheTest::ResetCache() {
116   testing_profile_manager_.DeleteProfileInfoCache();
117 }
118
119 namespace {
120
121 TEST_F(ProfileInfoCacheTest, AddProfiles) {
122   EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
123
124   ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
125   for (uint32 i = 0; i < 4; ++i) {
126     base::FilePath profile_path =
127         GetProfilePath(base::StringPrintf("path_%ud", i));
128     base::string16 profile_name =
129         ASCIIToUTF16(base::StringPrintf("name_%ud", i));
130     const SkBitmap* icon = rb.GetImageNamed(
131         ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(
132             i)).ToSkBitmap();
133     std::string managed_user_id = i == 3 ? "TEST_ID" : "";
134
135     GetCache()->AddProfileToCache(profile_path, profile_name, base::string16(),
136                                   i, managed_user_id);
137     GetCache()->SetBackgroundStatusOfProfileAtIndex(i, true);
138     base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
139     GetCache()->SetGAIANameOfProfileAtIndex(i, gaia_name);
140
141     EXPECT_EQ(i + 1, GetCache()->GetNumberOfProfiles());
142     EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
143     EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i));
144     const SkBitmap* actual_icon =
145         GetCache()->GetAvatarIconOfProfileAtIndex(i).ToSkBitmap();
146     EXPECT_EQ(icon->width(), actual_icon->width());
147     EXPECT_EQ(icon->height(), actual_icon->height());
148     EXPECT_EQ(i == 3, GetCache()->ProfileIsManagedAtIndex(i));
149     EXPECT_EQ(i == 3, GetCache()->IsOmittedProfileAtIndex(i));
150     EXPECT_EQ(managed_user_id, GetCache()->GetManagedUserIdOfProfileAtIndex(i));
151   }
152
153   // Reset the cache and test the it reloads correctly.
154   ResetCache();
155
156   EXPECT_EQ(4u, GetCache()->GetNumberOfProfiles());
157   for (uint32 i = 0; i < 4; ++i) {
158     base::FilePath profile_path =
159           GetProfilePath(base::StringPrintf("path_%ud", i));
160     EXPECT_EQ(i, GetCache()->GetIndexOfProfileWithPath(profile_path));
161     base::string16 profile_name =
162         ASCIIToUTF16(base::StringPrintf("name_%ud", i));
163     EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
164     EXPECT_EQ(i, GetCache()->GetAvatarIconIndexOfProfileAtIndex(i));
165     EXPECT_EQ(true, GetCache()->GetBackgroundStatusOfProfileAtIndex(i));
166     base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
167     EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(i));
168   }
169 }
170
171 TEST_F(ProfileInfoCacheTest, DeleteProfile) {
172   EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
173
174   base::FilePath path_1 = GetProfilePath("path_1");
175   GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("name_1"),
176                                 base::string16(), 0, std::string());
177   EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
178
179   base::FilePath path_2 = GetProfilePath("path_2");
180   base::string16 name_2 = ASCIIToUTF16("name_2");
181   GetCache()->AddProfileToCache(path_2, name_2, base::string16(), 0,
182                                 std::string());
183   EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles());
184
185   GetCache()->DeleteProfileFromCache(path_1);
186   EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
187   EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(0));
188
189   GetCache()->DeleteProfileFromCache(path_2);
190   EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
191 }
192
193 TEST_F(ProfileInfoCacheTest, MutateProfile) {
194   GetCache()->AddProfileToCache(
195       GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
196       base::string16(), 0, std::string());
197   GetCache()->AddProfileToCache(
198       GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
199       base::string16(), 0, std::string());
200
201   base::string16 new_name = ASCIIToUTF16("new_name");
202   GetCache()->SetNameOfProfileAtIndex(1, new_name);
203   EXPECT_EQ(new_name, GetCache()->GetNameOfProfileAtIndex(1));
204   EXPECT_NE(new_name, GetCache()->GetNameOfProfileAtIndex(0));
205
206   base::string16 new_user_name = ASCIIToUTF16("user_name");
207   GetCache()->SetUserNameOfProfileAtIndex(1, new_user_name);
208   EXPECT_EQ(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(1));
209   EXPECT_NE(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(0));
210
211   size_t new_icon_index = 3;
212   GetCache()->SetAvatarIconOfProfileAtIndex(1, new_icon_index);
213   // Not much to test.
214   GetCache()->GetAvatarIconOfProfileAtIndex(1);
215 }
216
217 TEST_F(ProfileInfoCacheTest, Sort) {
218   base::string16 name_a = ASCIIToUTF16("apple");
219   GetCache()->AddProfileToCache(
220       GetProfilePath("path_a"), name_a, base::string16(), 0, std::string());
221
222   base::string16 name_c = ASCIIToUTF16("cat");
223   GetCache()->AddProfileToCache(
224       GetProfilePath("path_c"), name_c, base::string16(), 0, std::string());
225
226   // Sanity check the initial order.
227   EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
228   EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
229
230   // Add a new profile (start with a capital to test case insensitive sorting.
231   base::string16 name_b = ASCIIToUTF16("Banana");
232   GetCache()->AddProfileToCache(
233       GetProfilePath("path_b"), name_b, base::string16(), 0, std::string());
234
235   // Verify the new order.
236   EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
237   EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(1));
238   EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(2));
239
240   // Change the name of an existing profile.
241   name_a = UTF8ToUTF16("dog");
242   GetCache()->SetNameOfProfileAtIndex(0, name_a);
243
244   // Verify the new order.
245   EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
246   EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
247   EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(2));
248
249   // Delete a profile.
250   GetCache()->DeleteProfileFromCache(GetProfilePath("path_c"));
251
252   // Verify the new order.
253   EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
254   EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(1));
255 }
256
257 TEST_F(ProfileInfoCacheTest, BackgroundModeStatus) {
258   GetCache()->AddProfileToCache(
259       GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
260       base::string16(), 0, std::string());
261   GetCache()->AddProfileToCache(
262       GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
263       base::string16(), 0, std::string());
264
265   EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
266   EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
267
268   GetCache()->SetBackgroundStatusOfProfileAtIndex(1, true);
269
270   EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
271   EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
272
273   GetCache()->SetBackgroundStatusOfProfileAtIndex(0, true);
274
275   EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
276   EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
277
278   GetCache()->SetBackgroundStatusOfProfileAtIndex(1, false);
279
280   EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
281   EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
282 }
283
284 TEST_F(ProfileInfoCacheTest, ProfileActiveTime) {
285   GetCache()->AddProfileToCache(
286       GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
287       base::string16(), 0, std::string());
288   EXPECT_EQ(base::Time(), GetCache()->GetProfileActiveTimeAtIndex(0));
289   // Before & After times are artificially shifted because just relying upon
290   // the system time can yield problems due to inaccuracies in the
291   // underlying storage system (which uses a double with only 52 bits of
292   // precision to store the 64-bit "time" number).  http://crbug.com/346827
293   base::Time before = base::Time::Now();
294   before -= base::TimeDelta::FromSeconds(1);
295   GetCache()->SetProfileActiveTimeAtIndex(0);
296   base::Time after = base::Time::Now();
297   after += base::TimeDelta::FromSeconds(1);
298   EXPECT_LE(before, GetCache()->GetProfileActiveTimeAtIndex(0));
299   EXPECT_GE(after, GetCache()->GetProfileActiveTimeAtIndex(0));
300 }
301
302 TEST_F(ProfileInfoCacheTest, GAIAName) {
303   GetCache()->AddProfileToCache(
304       GetProfilePath("path_1"), ASCIIToUTF16("Person 1"),
305       base::string16(), 0, std::string());
306   base::string16 profile_name(ASCIIToUTF16("Person 2"));
307   GetCache()->AddProfileToCache(
308       GetProfilePath("path_2"), profile_name, base::string16(), 0,
309       std::string());
310
311   int index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
312   int index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
313
314   // Sanity check.
315   EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty());
316   EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index2).empty());
317
318   // Set GAIA name. This re-sorts the cache.
319   base::string16 gaia_name(ASCIIToUTF16("Pat Smith"));
320   GetCache()->SetGAIANameOfProfileAtIndex(index2, gaia_name);
321   index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
322   index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
323
324   // Since there is a GAIA name, we use that as a display name.
325   EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty());
326   EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2));
327   EXPECT_EQ(gaia_name, GetCache()->GetNameOfProfileAtIndex(index2));
328
329   // Don't use GAIA name as profile name. This re-sorts the cache.
330   base::string16 custom_name(ASCIIToUTF16("Custom name"));
331   GetCache()->SetNameOfProfileAtIndex(index2, custom_name);
332   index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
333   index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
334
335   EXPECT_EQ(custom_name, GetCache()->GetNameOfProfileAtIndex(index2));
336   EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2));
337 }
338
339 TEST_F(ProfileInfoCacheTest, GAIAPicture) {
340   GetCache()->AddProfileToCache(
341       GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
342       base::string16(), 0, std::string());
343   GetCache()->AddProfileToCache(
344       GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
345       base::string16(), 0, std::string());
346
347   // Sanity check.
348   EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
349   EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(1));
350   EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0));
351   EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
352
353   // The profile icon should be the default one.
354   int id = ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(0);
355   const gfx::Image& profile_image(
356       ResourceBundle::GetSharedInstance().GetImageNamed(id));
357   EXPECT_TRUE(gfx::test::IsEqual(
358       profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
359
360   // Set GAIA picture.
361   gfx::Image gaia_image(gfx::test::CreateImage());
362   GetCache()->SetGAIAPictureOfProfileAtIndex(1, &gaia_image);
363   EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
364   EXPECT_TRUE(gfx::test::IsEqual(
365       gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
366   EXPECT_TRUE(gfx::test::IsEqual(
367       profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
368
369   // Use GAIA picture as profile picture.
370   GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, true);
371   EXPECT_TRUE(gfx::test::IsEqual(
372       gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
373   EXPECT_TRUE(gfx::test::IsEqual(
374       gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
375
376   // Don't use GAIA picture as profile picture.
377   GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, false);
378   EXPECT_TRUE(gfx::test::IsEqual(
379       gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
380   EXPECT_TRUE(gfx::test::IsEqual(
381       profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
382 }
383
384 TEST_F(ProfileInfoCacheTest, PersistGAIAPicture) {
385   GetCache()->AddProfileToCache(
386       GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
387       base::string16(), 0, std::string());
388   gfx::Image gaia_image(gfx::test::CreateImage());
389
390   content::WindowedNotificationObserver save_observer(
391       chrome::NOTIFICATION_PROFILE_CACHE_PICTURE_SAVED,
392       content::NotificationService::AllSources());
393   GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_image);
394   EXPECT_TRUE(gfx::test::IsEqual(
395       gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
396
397   // Wait for the file to be written to disk then reset the cache.
398   save_observer.Wait();
399   ResetCache();
400
401   // Try to get the GAIA picture. This should return NULL until the read from
402   // disk is done.
403   content::WindowedNotificationObserver read_observer(
404       chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED,
405       content::NotificationService::AllSources());
406   EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
407   read_observer.Wait();
408   EXPECT_TRUE(gfx::test::IsEqual(
409     gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
410 }
411
412 TEST_F(ProfileInfoCacheTest, SetManagedUserId) {
413   GetCache()->AddProfileToCache(
414       GetProfilePath("test"), ASCIIToUTF16("Test"),
415       base::string16(), 0, std::string());
416   EXPECT_FALSE(GetCache()->ProfileIsManagedAtIndex(0));
417
418   GetCache()->SetManagedUserIdOfProfileAtIndex(0, "TEST_ID");
419   EXPECT_TRUE(GetCache()->ProfileIsManagedAtIndex(0));
420   EXPECT_EQ("TEST_ID", GetCache()->GetManagedUserIdOfProfileAtIndex(0));
421
422   ResetCache();
423   EXPECT_TRUE(GetCache()->ProfileIsManagedAtIndex(0));
424
425   GetCache()->SetManagedUserIdOfProfileAtIndex(0, std::string());
426   EXPECT_FALSE(GetCache()->ProfileIsManagedAtIndex(0));
427   EXPECT_EQ("", GetCache()->GetManagedUserIdOfProfileAtIndex(0));
428 }
429
430 TEST_F(ProfileInfoCacheTest, EmptyGAIAInfo) {
431   base::string16 profile_name = ASCIIToUTF16("name_1");
432   int id = ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(0);
433   const gfx::Image& profile_image(
434       ResourceBundle::GetSharedInstance().GetImageNamed(id));
435
436   GetCache()->AddProfileToCache(
437       GetProfilePath("path_1"), profile_name, base::string16(), 0,
438                      std::string());
439
440   // Set empty GAIA info.
441   GetCache()->SetGAIANameOfProfileAtIndex(0, base::string16());
442   GetCache()->SetGAIAPictureOfProfileAtIndex(0, NULL);
443   GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(0, true);
444
445   // Verify that the profile name and picture are not empty.
446   EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(0));
447   EXPECT_TRUE(gfx::test::IsEqual(
448       profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(0)));
449 }
450
451 TEST_F(ProfileInfoCacheTest, CreateManagedTestingProfile) {
452   testing_profile_manager_.CreateTestingProfile("default");
453   base::string16 managed_user_name = ASCIIToUTF16("Supervised User");
454   testing_profile_manager_.CreateTestingProfile(
455       "test1", scoped_ptr<PrefServiceSyncable>(),
456       managed_user_name, 0, "TEST_ID", TestingProfile::TestingFactories());
457   for (size_t i = 0; i < GetCache()->GetNumberOfProfiles(); i++) {
458     bool is_managed =
459         GetCache()->GetNameOfProfileAtIndex(i) == managed_user_name;
460     EXPECT_EQ(is_managed, GetCache()->ProfileIsManagedAtIndex(i));
461     std::string managed_user_id = is_managed ? "TEST_ID" : "";
462     EXPECT_EQ(managed_user_id, GetCache()->GetManagedUserIdOfProfileAtIndex(i));
463   }
464
465   // Managed profiles have a custom theme, which needs to be deleted on the FILE
466   // thread. Reset the profile manager now so everything is deleted while we
467   // still have a FILE thread.
468   TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
469 }
470
471 TEST_F(ProfileInfoCacheTest, AddStubProfile) {
472   EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
473
474   // Add some profiles with and without a '.' in their paths.
475   const struct {
476     const char* profile_path;
477     const char* profile_name;
478   } kTestCases[] = {
479     { "path.test0", "name_0" },
480     { "path_test1", "name_1" },
481     { "path.test2", "name_2" },
482     { "path_test3", "name_3" },
483   };
484
485   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) {
486     base::FilePath profile_path = GetProfilePath(kTestCases[i].profile_path);
487     base::string16 profile_name = ASCIIToUTF16(kTestCases[i].profile_name);
488
489     GetCache()->AddProfileToCache(profile_path, profile_name, base::string16(),
490                                   i, "");
491
492     EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i));
493     EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
494   }
495
496   ASSERT_EQ(4U, GetCache()->GetNumberOfProfiles());
497
498   // Check that the profiles can be extracted from the local state.
499   std::vector<base::string16> names = ProfileInfoCache::GetProfileNames();
500   for (size_t i = 0; i < 4; i++)
501     ASSERT_FALSE(names[i].empty());
502 }
503
504 }  // namespace