blob: 67055fbe8d687d2119cb9b7b0ce3c12e9f99cdee [file] [log] [blame]
[email protected]ca591072012-03-27 01:54:441// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]dee810e2011-06-27 19:43:392// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]d4f5d1162011-11-30 01:41:525#include "chrome/browser/profiles/profile_info_cache_unittest.h"
[email protected]dee810e2011-06-27 19:43:396
[email protected]047481802013-09-16 22:26:387#include <vector>
8
[email protected]5cf8bcd92014-07-17 20:27:409#include "base/command_line.h"
thestig18dfb7a52014-08-26 10:44:0410#include "base/files/file_util.h"
[email protected]e5ba874f2013-02-14 17:20:1911#include "base/prefs/testing_pref_service.h"
[email protected]76fb05c2013-06-11 04:38:0512#include "base/strings/stringprintf.h"
[email protected]e309f312013-06-07 21:50:0813#include "base/strings/utf_string_conversions.h"
[email protected]85503a02014-01-16 17:27:2214#include "base/time/time.h"
[email protected]583844c2011-08-27 00:38:3515#include "chrome/browser/browser_process.h"
[email protected]0aa018a2013-07-31 15:08:5416#include "chrome/browser/prefs/pref_service_syncable.h"
[email protected]9de44302014-05-01 16:32:2717#include "chrome/browser/profiles/profile_avatar_downloader.h"
[email protected]c3e559772014-04-09 04:02:5418#include "chrome/browser/profiles/profile_avatar_icon_util.h"
[email protected]d4f5d1162011-11-30 01:41:5219#include "chrome/browser/profiles/profile_info_cache.h"
[email protected]6730b1e2011-09-29 05:23:5220#include "chrome/browser/profiles/profile_manager.h"
[email protected]5cf8bcd92014-07-17 20:27:4021#include "chrome/common/chrome_switches.h"
noms693f35aa2015-02-05 16:03:1622#include "chrome/common/pref_names.h"
[email protected]583844c2011-08-27 00:38:3523#include "chrome/test/base/testing_browser_process.h"
[email protected]5cf8bcd92014-07-17 20:27:4024#include "components/signin/core/common/profile_management_switches.h"
[email protected]371662e372013-10-17 22:05:2225#include "content/public/test/test_browser_thread_bundle.h"
[email protected]a7fe9112012-07-20 02:34:4526#include "content/public/test/test_utils.h"
[email protected]dee810e2011-06-27 19:43:3927#include "third_party/skia/include/core/SkBitmap.h"
28#include "ui/base/resource/resource_bundle.h"
29#include "ui/gfx/image/image.h"
[email protected]d4f5d1162011-11-30 01:41:5230#include "ui/gfx/image/image_unittest_util.h"
[email protected]dee810e2011-06-27 19:43:3931
[email protected]6778fed2013-12-24 20:09:3732using base::ASCIIToUTF16;
[email protected]f729d7a2013-12-26 07:07:5633using base::UTF8ToUTF16;
[email protected]cb114f142011-11-23 20:18:0434using content::BrowserThread;
35
[email protected]6a460662011-12-22 22:05:1636ProfileNameVerifierObserver::ProfileNameVerifierObserver(
37 TestingProfileManager* testing_profile_manager)
38 : testing_profile_manager_(testing_profile_manager) {
39 DCHECK(testing_profile_manager_);
[email protected]590e189b2011-12-13 22:07:0340}
41
42ProfileNameVerifierObserver::~ProfileNameVerifierObserver() {
43}
44
45void ProfileNameVerifierObserver::OnProfileAdded(
[email protected]650b2d52013-02-10 03:41:4546 const base::FilePath& profile_path) {
[email protected]a04db822013-12-11 19:14:4047 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1648 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0349 EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end());
50 profile_names_.insert(profile_name);
51}
52
[email protected]7b0af152011-12-16 17:02:0653void ProfileNameVerifierObserver::OnProfileWillBeRemoved(
[email protected]650b2d52013-02-10 03:41:4554 const base::FilePath& profile_path) {
[email protected]a04db822013-12-11 19:14:4055 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1656 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0357 EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end());
58 profile_names_.erase(profile_name);
59}
60
[email protected]7b0af152011-12-16 17:02:0661void ProfileNameVerifierObserver::OnProfileWasRemoved(
[email protected]650b2d52013-02-10 03:41:4562 const base::FilePath& profile_path,
[email protected]a04db822013-12-11 19:14:4063 const base::string16& profile_name) {
[email protected]7b0af152011-12-16 17:02:0664 EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end());
65}
66
[email protected]590e189b2011-12-13 22:07:0367void ProfileNameVerifierObserver::OnProfileNameChanged(
[email protected]650b2d52013-02-10 03:41:4568 const base::FilePath& profile_path,
[email protected]a04db822013-12-11 19:14:4069 const base::string16& old_profile_name) {
70 base::string16 new_profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1671 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0372 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);
76}
77
78void ProfileNameVerifierObserver::OnProfileAvatarChanged(
[email protected]650b2d52013-02-10 03:41:4579 const base::FilePath& profile_path) {
[email protected]a04db822013-12-11 19:14:4080 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1681 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0382 EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end());
83}
84
[email protected]6a460662011-12-22 22:05:1685ProfileInfoCache* ProfileNameVerifierObserver::GetCache() {
86 return testing_profile_manager_->profile_info_cache();
87}
88
[email protected]d4f5d1162011-11-30 01:41:5289ProfileInfoCacheTest::ProfileInfoCacheTest()
[email protected]0aa018a2013-07-31 15:08:5490 : testing_profile_manager_(TestingBrowserProcess::GetGlobal()),
[email protected]6a460662011-12-22 22:05:1691 name_observer_(&testing_profile_manager_) {
[email protected]d4f5d1162011-11-30 01:41:5292}
93
94ProfileInfoCacheTest::~ProfileInfoCacheTest() {
95}
96
97void ProfileInfoCacheTest::SetUp() {
98 ASSERT_TRUE(testing_profile_manager_.SetUp());
[email protected]590e189b2011-12-13 22:07:0399 testing_profile_manager_.profile_info_cache()->AddObserver(&name_observer_);
[email protected]d4f5d1162011-11-30 01:41:52100}
101
[email protected]1302dcf2011-11-30 21:47:05102void ProfileInfoCacheTest::TearDown() {
103 // Drain the UI thread to make sure all tasks are completed. This prevents
104 // memory leaks.
[email protected]0aa018a2013-07-31 15:08:54105 base::RunLoop().RunUntilIdle();
[email protected]1302dcf2011-11-30 21:47:05106}
107
[email protected]d4f5d1162011-11-30 01:41:52108ProfileInfoCache* ProfileInfoCacheTest::GetCache() {
109 return testing_profile_manager_.profile_info_cache();
110}
111
[email protected]650b2d52013-02-10 03:41:45112base::FilePath ProfileInfoCacheTest::GetProfilePath(
[email protected]d4f5d1162011-11-30 01:41:52113 const std::string& base_name) {
114 return testing_profile_manager_.profile_manager()->user_data_dir().
115 AppendASCII(base_name);
116}
117
118void ProfileInfoCacheTest::ResetCache() {
119 testing_profile_manager_.DeleteProfileInfoCache();
120}
121
[email protected]d4f5d1162011-11-30 01:41:52122TEST_F(ProfileInfoCacheTest, AddProfiles) {
[email protected]6730b1e2011-09-29 05:23:52123 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39124
[email protected]ca591072012-03-27 01:54:44125 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
[email protected]dee810e2011-06-27 19:43:39126 for (uint32 i = 0; i < 4; ++i) {
[email protected]7d3cbc92013-03-18 22:33:04127 base::FilePath profile_path =
128 GetProfilePath(base::StringPrintf("path_%ud", i));
[email protected]a04db822013-12-11 19:14:40129 base::string16 profile_name =
130 ASCIIToUTF16(base::StringPrintf("name_%ud", i));
[email protected]ca591072012-03-27 01:54:44131 const SkBitmap* icon = rb.GetImageNamed(
[email protected]c3e559772014-04-09 04:02:54132 profiles::GetDefaultAvatarIconResourceIDAtIndex(
[email protected]ca591072012-03-27 01:54:44133 i)).ToSkBitmap();
[email protected]d20d0432014-06-12 17:14:05134 std::string supervised_user_id = i == 3 ? "TEST_ID" : "";
[email protected]dee810e2011-06-27 19:43:39135
rogertae22659602015-05-11 21:58:21136 GetCache()->AddProfileToCache(profile_path, profile_name, std::string(),
137 base::string16(), i, supervised_user_id);
[email protected]cb114f142011-11-23 20:18:04138 GetCache()->SetBackgroundStatusOfProfileAtIndex(i, true);
[email protected]a04db822013-12-11 19:14:40139 base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
[email protected]cb114f142011-11-23 20:18:04140 GetCache()->SetGAIANameOfProfileAtIndex(i, gaia_name);
[email protected]dee810e2011-06-27 19:43:39141
[email protected]6730b1e2011-09-29 05:23:52142 EXPECT_EQ(i + 1, GetCache()->GetNumberOfProfiles());
143 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
144 EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i));
[email protected]0aa018a2013-07-31 15:08:54145 const SkBitmap* actual_icon =
146 GetCache()->GetAvatarIconOfProfileAtIndex(i).ToSkBitmap();
[email protected]ca591072012-03-27 01:54:44147 EXPECT_EQ(icon->width(), actual_icon->width());
148 EXPECT_EQ(icon->height(), actual_icon->height());
[email protected]d20d0432014-06-12 17:14:05149 EXPECT_EQ(i == 3, GetCache()->ProfileIsSupervisedAtIndex(i));
[email protected]5ddfade2014-02-03 10:24:53150 EXPECT_EQ(i == 3, GetCache()->IsOmittedProfileAtIndex(i));
[email protected]d20d0432014-06-12 17:14:05151 EXPECT_EQ(supervised_user_id,
152 GetCache()->GetSupervisedUserIdOfProfileAtIndex(i));
[email protected]dee810e2011-06-27 19:43:39153 }
[email protected]cb114f142011-11-23 20:18:04154
155 // Reset the cache and test the it reloads correctly.
156 ResetCache();
157
158 EXPECT_EQ(4u, GetCache()->GetNumberOfProfiles());
159 for (uint32 i = 0; i < 4; ++i) {
[email protected]7d3cbc92013-03-18 22:33:04160 base::FilePath profile_path =
161 GetProfilePath(base::StringPrintf("path_%ud", i));
[email protected]cb114f142011-11-23 20:18:04162 EXPECT_EQ(i, GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]a04db822013-12-11 19:14:40163 base::string16 profile_name =
164 ASCIIToUTF16(base::StringPrintf("name_%ud", i));
[email protected]cb114f142011-11-23 20:18:04165 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
166 EXPECT_EQ(i, GetCache()->GetAvatarIconIndexOfProfileAtIndex(i));
167 EXPECT_EQ(true, GetCache()->GetBackgroundStatusOfProfileAtIndex(i));
[email protected]a04db822013-12-11 19:14:40168 base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
[email protected]cb114f142011-11-23 20:18:04169 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(i));
170 }
[email protected]dee810e2011-06-27 19:43:39171}
172
[email protected]d4f5d1162011-11-30 01:41:52173TEST_F(ProfileInfoCacheTest, DeleteProfile) {
[email protected]6730b1e2011-09-29 05:23:52174 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39175
[email protected]650b2d52013-02-10 03:41:45176 base::FilePath path_1 = GetProfilePath("path_1");
[email protected]a04db822013-12-11 19:14:40177 GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("name_1"),
rogertae22659602015-05-11 21:58:21178 std::string(), base::string16(), 0,
179 std::string());
[email protected]6730b1e2011-09-29 05:23:52180 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39181
[email protected]650b2d52013-02-10 03:41:45182 base::FilePath path_2 = GetProfilePath("path_2");
[email protected]a04db822013-12-11 19:14:40183 base::string16 name_2 = ASCIIToUTF16("name_2");
rogertae22659602015-05-11 21:58:21184 GetCache()->AddProfileToCache(path_2, name_2, std::string(), base::string16(),
185 0, std::string());
[email protected]6730b1e2011-09-29 05:23:52186 EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39187
[email protected]6730b1e2011-09-29 05:23:52188 GetCache()->DeleteProfileFromCache(path_1);
189 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
190 EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(0));
[email protected]dee810e2011-06-27 19:43:39191
[email protected]6730b1e2011-09-29 05:23:52192 GetCache()->DeleteProfileFromCache(path_2);
193 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39194}
195
[email protected]d4f5d1162011-11-30 01:41:52196TEST_F(ProfileInfoCacheTest, MutateProfile) {
197 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21198 GetProfilePath("path_1"), ASCIIToUTF16("name_1"), std::string(),
[email protected]a04db822013-12-11 19:14:40199 base::string16(), 0, std::string());
[email protected]d4f5d1162011-11-30 01:41:52200 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21201 GetProfilePath("path_2"), ASCIIToUTF16("name_2"), std::string(),
[email protected]a04db822013-12-11 19:14:40202 base::string16(), 0, std::string());
[email protected]dee810e2011-06-27 19:43:39203
[email protected]a04db822013-12-11 19:14:40204 base::string16 new_name = ASCIIToUTF16("new_name");
[email protected]6730b1e2011-09-29 05:23:52205 GetCache()->SetNameOfProfileAtIndex(1, new_name);
206 EXPECT_EQ(new_name, GetCache()->GetNameOfProfileAtIndex(1));
207 EXPECT_NE(new_name, GetCache()->GetNameOfProfileAtIndex(0));
[email protected]dee810e2011-06-27 19:43:39208
[email protected]a04db822013-12-11 19:14:40209 base::string16 new_user_name = ASCIIToUTF16("user_name");
rogertae22659602015-05-11 21:58:21210 std::string new_gaia_id = "12345";
211 GetCache()->SetAuthInfoOfProfileAtIndex(1, new_gaia_id, new_user_name);
[email protected]e8e78092011-09-29 18:15:38212 EXPECT_EQ(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(1));
rogertae22659602015-05-11 21:58:21213 EXPECT_EQ(new_gaia_id, GetCache()->GetGAIAIdOfProfileAtIndex(1));
[email protected]e8e78092011-09-29 18:15:38214 EXPECT_NE(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(0));
215
[email protected]dee810e2011-06-27 19:43:39216 size_t new_icon_index = 3;
[email protected]6730b1e2011-09-29 05:23:52217 GetCache()->SetAvatarIconOfProfileAtIndex(1, new_icon_index);
[email protected]dee810e2011-06-27 19:43:39218 // Not much to test.
[email protected]6730b1e2011-09-29 05:23:52219 GetCache()->GetAvatarIconOfProfileAtIndex(1);
[email protected]dee810e2011-06-27 19:43:39220}
221
[email protected]d4f5d1162011-11-30 01:41:52222TEST_F(ProfileInfoCacheTest, Sort) {
[email protected]a04db822013-12-11 19:14:40223 base::string16 name_a = ASCIIToUTF16("apple");
[email protected]d4f5d1162011-11-30 01:41:52224 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21225 GetProfilePath("path_a"), name_a, std::string(), base::string16(), 0,
226 std::string());
[email protected]cb114f142011-11-23 20:18:04227
[email protected]a04db822013-12-11 19:14:40228 base::string16 name_c = ASCIIToUTF16("cat");
[email protected]d4f5d1162011-11-30 01:41:52229 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21230 GetProfilePath("path_c"), name_c, std::string(), base::string16(), 0,
231 std::string());
[email protected]cb114f142011-11-23 20:18:04232
233 // Sanity check the initial order.
234 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
235 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
236
237 // Add a new profile (start with a capital to test case insensitive sorting.
[email protected]a04db822013-12-11 19:14:40238 base::string16 name_b = ASCIIToUTF16("Banana");
[email protected]d4f5d1162011-11-30 01:41:52239 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21240 GetProfilePath("path_b"), name_b, std::string(), base::string16(), 0,
241 std::string());
[email protected]cb114f142011-11-23 20:18:04242
243 // Verify the new order.
244 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
245 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(1));
246 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(2));
247
248 // Change the name of an existing profile.
249 name_a = UTF8ToUTF16("dog");
250 GetCache()->SetNameOfProfileAtIndex(0, name_a);
251
252 // Verify the new order.
253 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
254 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
255 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(2));
256
257 // Delete a profile.
[email protected]d4f5d1162011-11-30 01:41:52258 GetCache()->DeleteProfileFromCache(GetProfilePath("path_c"));
[email protected]cb114f142011-11-23 20:18:04259
260 // Verify the new order.
261 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
262 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(1));
263}
264
[email protected]d4f5d1162011-11-30 01:41:52265TEST_F(ProfileInfoCacheTest, BackgroundModeStatus) {
[email protected]279170832011-10-12 23:38:03266 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13267 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
rogertae22659602015-05-11 21:58:21268 std::string(), base::string16(), 0, std::string());
[email protected]279170832011-10-12 23:38:03269 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13270 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
rogertae22659602015-05-11 21:58:21271 std::string(), base::string16(), 0, std::string());
[email protected]279170832011-10-12 23:38:03272
273 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
274 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
275
276 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, true);
277
278 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
279 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
280
281 GetCache()->SetBackgroundStatusOfProfileAtIndex(0, true);
282
283 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
284 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
285
286 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, false);
287
288 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
289 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
290}
291
[email protected]85503a02014-01-16 17:27:22292TEST_F(ProfileInfoCacheTest, ProfileActiveTime) {
293 GetCache()->AddProfileToCache(
294 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
rogertae22659602015-05-11 21:58:21295 std::string(), base::string16(), 0, std::string());
[email protected]85503a02014-01-16 17:27:22296 EXPECT_EQ(base::Time(), GetCache()->GetProfileActiveTimeAtIndex(0));
[email protected]87727e02014-02-27 18:16:53297 // Before & After times are artificially shifted because just relying upon
298 // the system time can yield problems due to inaccuracies in the
299 // underlying storage system (which uses a double with only 52 bits of
300 // precision to store the 64-bit "time" number). http://crbug.com/346827
[email protected]85503a02014-01-16 17:27:22301 base::Time before = base::Time::Now();
[email protected]87727e02014-02-27 18:16:53302 before -= base::TimeDelta::FromSeconds(1);
[email protected]85503a02014-01-16 17:27:22303 GetCache()->SetProfileActiveTimeAtIndex(0);
304 base::Time after = base::Time::Now();
[email protected]87727e02014-02-27 18:16:53305 after += base::TimeDelta::FromSeconds(1);
[email protected]85503a02014-01-16 17:27:22306 EXPECT_LE(before, GetCache()->GetProfileActiveTimeAtIndex(0));
307 EXPECT_GE(after, GetCache()->GetProfileActiveTimeAtIndex(0));
308}
309
[email protected]d4f5d1162011-11-30 01:41:52310TEST_F(ProfileInfoCacheTest, GAIAName) {
[email protected]cb114f142011-11-23 20:18:04311 GetCache()->AddProfileToCache(
[email protected]a4352d02014-03-06 06:00:27312 GetProfilePath("path_1"), ASCIIToUTF16("Person 1"),
rogertae22659602015-05-11 21:58:21313 std::string(), base::string16(), 0, std::string());
[email protected]a4352d02014-03-06 06:00:27314 base::string16 profile_name(ASCIIToUTF16("Person 2"));
[email protected]cb114f142011-11-23 20:18:04315 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21316 GetProfilePath("path_2"), profile_name, std::string(), base::string16(),
317 0, std::string());
[email protected]cb114f142011-11-23 20:18:04318
[email protected]a4352d02014-03-06 06:00:27319 int index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
320 int index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
321
[email protected]cb114f142011-11-23 20:18:04322 // Sanity check.
[email protected]a4352d02014-03-06 06:00:27323 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty());
324 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index2).empty());
[email protected]cb114f142011-11-23 20:18:04325
[email protected]425d6ab2014-03-25 05:59:39326 // Set GAIA name. This re-sorts the cache.
[email protected]a04db822013-12-11 19:14:40327 base::string16 gaia_name(ASCIIToUTF16("Pat Smith"));
[email protected]a4352d02014-03-06 06:00:27328 GetCache()->SetGAIANameOfProfileAtIndex(index2, gaia_name);
[email protected]425d6ab2014-03-25 05:59:39329 index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
330 index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
331
332 // Since there is a GAIA name, we use that as a display name.
[email protected]a4352d02014-03-06 06:00:27333 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty());
334 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2));
[email protected]a4352d02014-03-06 06:00:27335 EXPECT_EQ(gaia_name, GetCache()->GetNameOfProfileAtIndex(index2));
[email protected]cb114f142011-11-23 20:18:04336
[email protected]a4352d02014-03-06 06:00:27337 // Don't use GAIA name as profile name. This re-sorts the cache.
[email protected]425d6ab2014-03-25 05:59:39338 base::string16 custom_name(ASCIIToUTF16("Custom name"));
339 GetCache()->SetNameOfProfileAtIndex(index2, custom_name);
[email protected]1cdf0932014-08-16 06:00:49340 GetCache()->SetProfileIsUsingDefaultNameAtIndex(index2, false);
341
[email protected]a4352d02014-03-06 06:00:27342 index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
343 index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
344
[email protected]425d6ab2014-03-25 05:59:39345 EXPECT_EQ(custom_name, GetCache()->GetNameOfProfileAtIndex(index2));
[email protected]a4352d02014-03-06 06:00:27346 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2));
[email protected]cb114f142011-11-23 20:18:04347}
348
[email protected]d4f5d1162011-11-30 01:41:52349TEST_F(ProfileInfoCacheTest, GAIAPicture) {
[email protected]22900902014-07-30 11:50:41350 const int kDefaultAvatarIndex = 0;
351 const int kOtherAvatarIndex = 1;
[email protected]979dc4f2014-08-12 22:52:11352 const int kGaiaPictureSize = 256; // Standard size of a Gaia account picture.
[email protected]cb114f142011-11-23 20:18:04353 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13354 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
rogertae22659602015-05-11 21:58:21355 std::string(), base::string16(), kDefaultAvatarIndex, std::string());
[email protected]cb114f142011-11-23 20:18:04356 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13357 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
rogertae22659602015-05-11 21:58:21358 std::string(), base::string16(), kDefaultAvatarIndex, std::string());
[email protected]cb114f142011-11-23 20:18:04359
360 // Sanity check.
[email protected]2f3c00f2011-11-30 04:36:22361 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
362 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(1));
[email protected]cb114f142011-11-23 20:18:04363 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0));
364 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
365
366 // The profile icon should be the default one.
[email protected]22900902014-07-30 11:50:41367 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(0));
368 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
369 int default_avatar_id =
370 profiles::GetDefaultAvatarIconResourceIDAtIndex(kDefaultAvatarIndex);
371 const gfx::Image& default_avatar_image(
372 ResourceBundle::GetSharedInstance().GetImageNamed(default_avatar_id));
[email protected]d4f5d1162011-11-30 01:41:52373 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]22900902014-07-30 11:50:41374 default_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
[email protected]cb114f142011-11-23 20:18:04375
376 // Set GAIA picture.
[email protected]979dc4f2014-08-12 22:52:11377 gfx::Image gaia_image(gfx::test::CreateImage(
378 kGaiaPictureSize, kGaiaPictureSize));
[email protected]2f3c00f2011-11-30 04:36:22379 GetCache()->SetGAIAPictureOfProfileAtIndex(1, &gaia_image);
380 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
[email protected]d4f5d1162011-11-30 01:41:52381 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22382 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
[email protected]22900902014-07-30 11:50:41383 // Since we're still using the default avatar, the GAIA image should be
384 // preferred over the generic avatar image.
385 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
386 EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
[email protected]d4f5d1162011-11-30 01:41:52387 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]22900902014-07-30 11:50:41388 gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
[email protected]cb114f142011-11-23 20:18:04389
[email protected]1cdf0932014-08-16 06:00:49390 // Set a non-default avatar. This should be preferred over the GAIA image.
[email protected]22900902014-07-30 11:50:41391 GetCache()->SetAvatarIconOfProfileAtIndex(1, kOtherAvatarIndex);
[email protected]1cdf0932014-08-16 06:00:49392 GetCache()->SetProfileIsUsingDefaultAvatarAtIndex(1, false);
[email protected]22900902014-07-30 11:50:41393 EXPECT_FALSE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
394 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
395 int other_avatar_id =
396 profiles::GetDefaultAvatarIconResourceIDAtIndex(kOtherAvatarIndex);
397 const gfx::Image& other_avatar_image(
398 ResourceBundle::GetSharedInstance().GetImageNamed(other_avatar_id));
399 EXPECT_TRUE(gfx::test::IsEqual(
400 other_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
401
402 // Explicitly setting the GAIA picture should make it preferred again.
[email protected]cb114f142011-11-23 20:18:04403 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, true);
[email protected]22900902014-07-30 11:50:41404 EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
[email protected]d4f5d1162011-11-30 01:41:52405 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22406 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
[email protected]d4f5d1162011-11-30 01:41:52407 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]cb114f142011-11-23 20:18:04408 gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
409
[email protected]22900902014-07-30 11:50:41410 // Clearing the IsUsingGAIAPicture flag should result in the generic image
411 // being used again.
[email protected]cb114f142011-11-23 20:18:04412 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, false);
[email protected]22900902014-07-30 11:50:41413 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
[email protected]d4f5d1162011-11-30 01:41:52414 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22415 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
[email protected]d4f5d1162011-11-30 01:41:52416 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]22900902014-07-30 11:50:41417 other_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
[email protected]cb114f142011-11-23 20:18:04418}
419
[email protected]754bebc2011-12-01 16:42:16420TEST_F(ProfileInfoCacheTest, PersistGAIAPicture) {
[email protected]cb114f142011-11-23 20:18:04421 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13422 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
rogertae22659602015-05-11 21:58:21423 std::string(), base::string16(), 0, std::string());
[email protected]d4f5d1162011-11-30 01:41:52424 gfx::Image gaia_image(gfx::test::CreateImage());
[email protected]cb114f142011-11-23 20:18:04425
[email protected]2f3c00f2011-11-30 04:36:22426 GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_image);
noms75ca0192015-02-26 03:14:30427
428 // Make sure everything has completed, and the file has been written to disk.
429 base::RunLoop().RunUntilIdle();
430
[email protected]d4f5d1162011-11-30 01:41:52431 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22432 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
[email protected]cb114f142011-11-23 20:18:04433
[email protected]cb114f142011-11-23 20:18:04434 ResetCache();
[email protected]cb114f142011-11-23 20:18:04435 // Try to get the GAIA picture. This should return NULL until the read from
436 // disk is done.
[email protected]2f3c00f2011-11-30 04:36:22437 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
noms75ca0192015-02-26 03:14:30438 base::RunLoop().RunUntilIdle();
439
[email protected]d4f5d1162011-11-30 01:41:52440 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22441 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
442}
443
[email protected]d20d0432014-06-12 17:14:05444TEST_F(ProfileInfoCacheTest, SetSupervisedUserId) {
[email protected]0aa018a2013-07-31 15:08:54445 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13446 GetProfilePath("test"), ASCIIToUTF16("Test"),
rogertae22659602015-05-11 21:58:21447 std::string(), base::string16(), 0, std::string());
[email protected]d20d0432014-06-12 17:14:05448 EXPECT_FALSE(GetCache()->ProfileIsSupervisedAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54449
[email protected]d20d0432014-06-12 17:14:05450 GetCache()->SetSupervisedUserIdOfProfileAtIndex(0, "TEST_ID");
451 EXPECT_TRUE(GetCache()->ProfileIsSupervisedAtIndex(0));
452 EXPECT_EQ("TEST_ID", GetCache()->GetSupervisedUserIdOfProfileAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54453
454 ResetCache();
[email protected]d20d0432014-06-12 17:14:05455 EXPECT_TRUE(GetCache()->ProfileIsSupervisedAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54456
[email protected]d20d0432014-06-12 17:14:05457 GetCache()->SetSupervisedUserIdOfProfileAtIndex(0, std::string());
458 EXPECT_FALSE(GetCache()->ProfileIsSupervisedAtIndex(0));
459 EXPECT_EQ("", GetCache()->GetSupervisedUserIdOfProfileAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54460}
461
[email protected]2f3c00f2011-11-30 04:36:22462TEST_F(ProfileInfoCacheTest, EmptyGAIAInfo) {
[email protected]a04db822013-12-11 19:14:40463 base::string16 profile_name = ASCIIToUTF16("name_1");
[email protected]c3e559772014-04-09 04:02:54464 int id = profiles::GetDefaultAvatarIconResourceIDAtIndex(0);
[email protected]2f3c00f2011-11-30 04:36:22465 const gfx::Image& profile_image(
466 ResourceBundle::GetSharedInstance().GetImageNamed(id));
467
468 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21469 GetProfilePath("path_1"), profile_name, std::string(), base::string16(),
470 0, std::string());
[email protected]2f3c00f2011-11-30 04:36:22471
472 // Set empty GAIA info.
[email protected]a04db822013-12-11 19:14:40473 GetCache()->SetGAIANameOfProfileAtIndex(0, base::string16());
[email protected]2f3c00f2011-11-30 04:36:22474 GetCache()->SetGAIAPictureOfProfileAtIndex(0, NULL);
[email protected]2f3c00f2011-11-30 04:36:22475 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(0, true);
476
477 // Verify that the profile name and picture are not empty.
478 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(0));
479 EXPECT_TRUE(gfx::test::IsEqual(
480 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(0)));
[email protected]cb114f142011-11-23 20:18:04481}
482
[email protected]d20d0432014-06-12 17:14:05483TEST_F(ProfileInfoCacheTest, CreateSupervisedTestingProfile) {
[email protected]0aa018a2013-07-31 15:08:54484 testing_profile_manager_.CreateTestingProfile("default");
[email protected]d20d0432014-06-12 17:14:05485 base::string16 supervised_user_name = ASCIIToUTF16("Supervised User");
[email protected]0aa018a2013-07-31 15:08:54486 testing_profile_manager_.CreateTestingProfile(
[email protected]a6e01b42013-08-05 14:13:13487 "test1", scoped_ptr<PrefServiceSyncable>(),
[email protected]d20d0432014-06-12 17:14:05488 supervised_user_name, 0, "TEST_ID", TestingProfile::TestingFactories());
[email protected]0aa018a2013-07-31 15:08:54489 for (size_t i = 0; i < GetCache()->GetNumberOfProfiles(); i++) {
[email protected]d20d0432014-06-12 17:14:05490 bool is_supervised =
491 GetCache()->GetNameOfProfileAtIndex(i) == supervised_user_name;
492 EXPECT_EQ(is_supervised, GetCache()->ProfileIsSupervisedAtIndex(i));
493 std::string supervised_user_id = is_supervised ? "TEST_ID" : "";
494 EXPECT_EQ(supervised_user_id,
495 GetCache()->GetSupervisedUserIdOfProfileAtIndex(i));
[email protected]0aa018a2013-07-31 15:08:54496 }
[email protected]371662e372013-10-17 22:05:22497
[email protected]d20d0432014-06-12 17:14:05498 // Supervised profiles have a custom theme, which needs to be deleted on the
499 // FILE thread. Reset the profile manager now so everything is deleted while
500 // we still have a FILE thread.
[email protected]371662e372013-10-17 22:05:22501 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
[email protected]0aa018a2013-07-31 15:08:54502}
503
[email protected]047481802013-09-16 22:26:38504TEST_F(ProfileInfoCacheTest, AddStubProfile) {
505 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
506
507 // Add some profiles with and without a '.' in their paths.
508 const struct {
509 const char* profile_path;
510 const char* profile_name;
511 } kTestCases[] = {
512 { "path.test0", "name_0" },
513 { "path_test1", "name_1" },
514 { "path.test2", "name_2" },
515 { "path_test3", "name_3" },
516 };
517
viettrungluu9e65ad12014-10-16 04:22:26518 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
[email protected]047481802013-09-16 22:26:38519 base::FilePath profile_path = GetProfilePath(kTestCases[i].profile_path);
[email protected]a04db822013-12-11 19:14:40520 base::string16 profile_name = ASCIIToUTF16(kTestCases[i].profile_name);
[email protected]047481802013-09-16 22:26:38521
rogertae22659602015-05-11 21:58:21522 GetCache()->AddProfileToCache(profile_path, profile_name, std::string(),
523 base::string16(), i, "");
[email protected]047481802013-09-16 22:26:38524
525 EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i));
526 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
527 }
528
529 ASSERT_EQ(4U, GetCache()->GetNumberOfProfiles());
530
531 // Check that the profiles can be extracted from the local state.
noms693f35aa2015-02-05 16:03:16532 std::vector<base::string16> names;
533 PrefService* local_state = g_browser_process->local_state();
534 const base::DictionaryValue* cache = local_state->GetDictionary(
535 prefs::kProfileInfoCache);
536 base::string16 name;
537 for (base::DictionaryValue::Iterator it(*cache); !it.IsAtEnd();
538 it.Advance()) {
539 const base::DictionaryValue* info = NULL;
540 it.value().GetAsDictionary(&info);
541 info->GetString("name", &name);
542 names.push_back(name);
543 }
544
[email protected]047481802013-09-16 22:26:38545 for (size_t i = 0; i < 4; i++)
546 ASSERT_FALSE(names[i].empty());
547}
548
[email protected]b09bfa12014-08-15 19:05:25549// High res avatar downloading is only supported on desktop.
550#if !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_CHROMEOS)
[email protected]9de44302014-05-01 16:32:27551TEST_F(ProfileInfoCacheTest, DownloadHighResAvatarTest) {
avi556c05022014-12-22 23:31:43552 switches::EnableNewAvatarMenuForTesting(
553 base::CommandLine::ForCurrentProcess());
[email protected]5cf8bcd92014-07-17 20:27:40554
mlerman3ab32772015-04-08 14:48:56555 // The TestingProfileManager's ProfileInfoCache doesn't download avatars.
556 ProfileInfoCache profile_info_cache(g_browser_process->local_state(),
557 testing_profile_manager_.profile_manager()->user_data_dir());
558
559 // // Make sure there are no avatars already on disk.
560 const size_t kIconIndex = 0;
561 base::FilePath icon_path =
562 profiles::GetPathOfHighResAvatarAtIndex(kIconIndex);
563 EXPECT_FALSE(base::PathExists(icon_path));
564
565 EXPECT_EQ(0U, profile_info_cache.GetNumberOfProfiles());
[email protected]9de44302014-05-01 16:32:27566 base::FilePath path_1 = GetProfilePath("path_1");
mlerman3ab32772015-04-08 14:48:56567 profile_info_cache.AddProfileToCache(path_1, ASCIIToUTF16("name_1"),
rogertae22659602015-05-11 21:58:21568 std::string(), base::string16(), kIconIndex, std::string());
mlerman3ab32772015-04-08 14:48:56569 EXPECT_EQ(1U, profile_info_cache.GetNumberOfProfiles());
noms28693ab22015-01-28 21:44:54570 base::RunLoop().RunUntilIdle();
[email protected]9de44302014-05-01 16:32:27571
572 // We haven't downloaded any high-res avatars yet.
mlerman3ab32772015-04-08 14:48:56573 EXPECT_EQ(0U, profile_info_cache.cached_avatar_images_.size());
[email protected]5cf8bcd92014-07-17 20:27:40574
575 // After adding a new profile, the download of high-res avatar will be
576 // triggered if the flag kNewAvatarMenu has been set. But the downloader
577 // won't ever call OnFetchComplete in the test.
mlerman3ab32772015-04-08 14:48:56578 EXPECT_EQ(1U, profile_info_cache.avatar_images_downloads_in_progress_.size());
[email protected]5cf8bcd92014-07-17 20:27:40579
mlerman3ab32772015-04-08 14:48:56580 EXPECT_FALSE(profile_info_cache.GetHighResAvatarOfProfileAtIndex(0));
[email protected]9de44302014-05-01 16:32:27581
582 // Simulate downloading a high-res avatar.
[email protected]2d605712014-05-14 12:03:07583 ProfileAvatarDownloader avatar_downloader(
mlerman3ab32772015-04-08 14:48:56584 kIconIndex, profile_info_cache.GetPathOfProfileAtIndex(0),
585 &profile_info_cache);
[email protected]9de44302014-05-01 16:32:27586
587 // Put a real bitmap into "bitmap". 2x2 bitmap of green 32 bit pixels.
588 SkBitmap bitmap;
[email protected]e61cad52014-07-01 21:01:57589 bitmap.allocN32Pixels(2, 2);
[email protected]9de44302014-05-01 16:32:27590 bitmap.eraseColor(SK_ColorGREEN);
591
592 avatar_downloader.OnFetchComplete(
593 GURL("http://www.google.com/avatar.png"), &bitmap);
594
treibaf255502015-01-30 17:47:53595 // Now the download should not be in progress anymore.
mlerman3ab32772015-04-08 14:48:56596 EXPECT_EQ(0U, profile_info_cache.avatar_images_downloads_in_progress_.size());
treibaf255502015-01-30 17:47:53597
[email protected]9de44302014-05-01 16:32:27598 std::string file_name =
599 profiles::GetDefaultAvatarIconFileNameAtIndex(kIconIndex);
600
601 // The file should have been cached and saved.
mlerman3ab32772015-04-08 14:48:56602 EXPECT_EQ(1U, profile_info_cache.cached_avatar_images_.size());
603 EXPECT_TRUE(profile_info_cache.GetHighResAvatarOfProfileAtIndex(0));
604 EXPECT_EQ(profile_info_cache.cached_avatar_images_[file_name],
605 profile_info_cache.GetHighResAvatarOfProfileAtIndex(0));
[email protected]9de44302014-05-01 16:32:27606
607 // Make sure everything has completed, and the file has been written to disk.
608 base::RunLoop().RunUntilIdle();
609
610 // Clean up.
[email protected]9de44302014-05-01 16:32:27611 EXPECT_NE(std::string::npos, icon_path.MaybeAsASCII().find(file_name));
612 EXPECT_TRUE(base::PathExists(icon_path));
613 EXPECT_TRUE(base::DeleteFile(icon_path, true));
614 EXPECT_FALSE(base::PathExists(icon_path));
615}
nomsbe073e52014-08-23 22:01:04616
617TEST_F(ProfileInfoCacheTest, MigrateLegacyProfileNamesWithNewAvatarMenu) {
avi556c05022014-12-22 23:31:43618 switches::EnableNewAvatarMenuForTesting(
619 base::CommandLine::ForCurrentProcess());
nomsbe073e52014-08-23 22:01:04620 EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles());
621
622 base::FilePath path_1 = GetProfilePath("path_1");
623 GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("Default Profile"),
rogertae22659602015-05-11 21:58:21624 std::string(), base::string16(), 0,
625 std::string());
nomsbe073e52014-08-23 22:01:04626 base::FilePath path_2 = GetProfilePath("path_2");
627 GetCache()->AddProfileToCache(path_2, ASCIIToUTF16("First user"),
rogertae22659602015-05-11 21:58:21628 std::string(), base::string16(), 1,
629 std::string());
nomsbe073e52014-08-23 22:01:04630 base::string16 name_3 = ASCIIToUTF16("Lemonade");
631 base::FilePath path_3 = GetProfilePath("path_3");
632 GetCache()->AddProfileToCache(path_3, name_3,
rogertae22659602015-05-11 21:58:21633 std::string(), base::string16(), 2,
634 std::string());
nomsbe073e52014-08-23 22:01:04635 base::string16 name_4 = ASCIIToUTF16("Batman");
636 base::FilePath path_4 = GetProfilePath("path_4");
637 GetCache()->AddProfileToCache(path_4, name_4,
rogertae22659602015-05-11 21:58:21638 std::string(), base::string16(), 3,
639 std::string());
nomsbe073e52014-08-23 22:01:04640 base::string16 name_5 = ASCIIToUTF16("Person 2");
641 base::FilePath path_5 = GetProfilePath("path_5");
642 GetCache()->AddProfileToCache(path_5, name_5,
rogertae22659602015-05-11 21:58:21643 std::string(), base::string16(), 2,
644 std::string());
nomsbe073e52014-08-23 22:01:04645
646 EXPECT_EQ(5U, GetCache()->GetNumberOfProfiles());
647
648
649 ResetCache();
650
651 // Legacy profile names like "Default Profile" and "First user" should be
652 // migrated to "Person %n" type names.
653 EXPECT_EQ(ASCIIToUTF16("Person 1"), GetCache()->GetNameOfProfileAtIndex(
654 GetCache()->GetIndexOfProfileWithPath(path_1)));
655 EXPECT_EQ(ASCIIToUTF16("Person 3"), GetCache()->GetNameOfProfileAtIndex(
656 GetCache()->GetIndexOfProfileWithPath(path_2)));
657
658 // Other profile names should not be migrated even if they're the old
659 // default cartoon profile names.
660 EXPECT_EQ(name_3, GetCache()->GetNameOfProfileAtIndex(
661 GetCache()->GetIndexOfProfileWithPath(path_3)));
662 EXPECT_EQ(name_4, GetCache()->GetNameOfProfileAtIndex(
663 GetCache()->GetIndexOfProfileWithPath(path_4)));
664 EXPECT_EQ(name_5, GetCache()->GetNameOfProfileAtIndex(
665 GetCache()->GetIndexOfProfileWithPath(path_5)));
666}
[email protected]b09bfa12014-08-15 19:05:25667#endif
nomsbe073e52014-08-23 22:01:04668
669TEST_F(ProfileInfoCacheTest,
670 DontMigrateLegacyProfileNamesWithoutNewAvatarMenu) {
avi556c05022014-12-22 23:31:43671 switches::DisableNewAvatarMenuForTesting(
672 base::CommandLine::ForCurrentProcess());
treib33335ad2014-08-27 09:17:19673
nomsbe073e52014-08-23 22:01:04674 EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles());
675
676 base::string16 name_1 = ASCIIToUTF16("Default Profile");
677 base::FilePath path_1 = GetProfilePath("path_1");
678 GetCache()->AddProfileToCache(path_1, name_1,
rogertae22659602015-05-11 21:58:21679 std::string(), base::string16(), 0,
680 std::string());
nomsbe073e52014-08-23 22:01:04681 base::string16 name_2 = ASCIIToUTF16("First user");
682 base::FilePath path_2 = GetProfilePath("path_2");
683 GetCache()->AddProfileToCache(path_2, name_2,
rogertae22659602015-05-11 21:58:21684 std::string(), base::string16(), 1,
685 std::string());
nomsbe073e52014-08-23 22:01:04686 base::string16 name_3 = ASCIIToUTF16("Lemonade");
687 base::FilePath path_3 = GetProfilePath("path_3");
688 GetCache()->AddProfileToCache(path_3, name_3,
rogertae22659602015-05-11 21:58:21689 std::string(), base::string16(), 2,
690 std::string());
nomsbe073e52014-08-23 22:01:04691 base::string16 name_4 = ASCIIToUTF16("Batman");
692 base::FilePath path_4 = GetProfilePath("path_4");
693 GetCache()->AddProfileToCache(path_4, name_4,
rogertae22659602015-05-11 21:58:21694 std::string(), base::string16(), 3,
695 std::string());
nomsbe073e52014-08-23 22:01:04696 EXPECT_EQ(4U, GetCache()->GetNumberOfProfiles());
697
698 ResetCache();
699
700 // Profile names should have been preserved.
701 EXPECT_EQ(name_1, GetCache()->GetNameOfProfileAtIndex(
702 GetCache()->GetIndexOfProfileWithPath(path_1)));
703 EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(
704 GetCache()->GetIndexOfProfileWithPath(path_2)));
705 EXPECT_EQ(name_3, GetCache()->GetNameOfProfileAtIndex(
706 GetCache()->GetIndexOfProfileWithPath(path_3)));
707 EXPECT_EQ(name_4, GetCache()->GetNameOfProfileAtIndex(
708 GetCache()->GetIndexOfProfileWithPath(path_4)));
709}