blob: 3da169c66ce96b1b829cddb4f87139e72f27a68b [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
avib896c712015-12-26 02:10:437#include <stddef.h>
8#include <stdint.h>
9
[email protected]047481802013-09-16 22:26:3810#include <vector>
11
[email protected]5cf8bcd92014-07-17 20:27:4012#include "base/command_line.h"
thestig18dfb7a52014-08-26 10:44:0413#include "base/files/file_util.h"
avib896c712015-12-26 02:10:4314#include "base/macros.h"
[email protected]76fb05c2013-06-11 04:38:0515#include "base/strings/stringprintf.h"
[email protected]e309f312013-06-07 21:50:0816#include "base/strings/utf_string_conversions.h"
[email protected]85503a02014-01-16 17:27:2217#include "base/time/time.h"
avib896c712015-12-26 02:10:4318#include "build/build_config.h"
[email protected]583844c2011-08-27 00:38:3519#include "chrome/browser/browser_process.h"
[email protected]9de44302014-05-01 16:32:2720#include "chrome/browser/profiles/profile_avatar_downloader.h"
[email protected]c3e559772014-04-09 04:02:5421#include "chrome/browser/profiles/profile_avatar_icon_util.h"
[email protected]d4f5d1162011-11-30 01:41:5222#include "chrome/browser/profiles/profile_info_cache.h"
[email protected]6730b1e2011-09-29 05:23:5223#include "chrome/browser/profiles/profile_manager.h"
emaxxa2e11492015-05-12 17:19:1124#include "chrome/common/chrome_paths.h"
[email protected]5cf8bcd92014-07-17 20:27:4025#include "chrome/common/chrome_switches.h"
noms693f35aa2015-02-05 16:03:1626#include "chrome/common/pref_names.h"
[email protected]583844c2011-08-27 00:38:3527#include "chrome/test/base/testing_browser_process.h"
brettwb1fc1b82016-02-02 00:19:0828#include "components/prefs/testing_pref_service.h"
[email protected]5cf8bcd92014-07-17 20:27:4029#include "components/signin/core/common/profile_management_switches.h"
sdefresne875d0782015-09-16 12:01:2830#include "components/syncable_prefs/pref_service_syncable.h"
[email protected]371662e372013-10-17 22:05:2231#include "content/public/test/test_browser_thread_bundle.h"
[email protected]a7fe9112012-07-20 02:34:4532#include "content/public/test/test_utils.h"
[email protected]dee810e2011-06-27 19:43:3933#include "third_party/skia/include/core/SkBitmap.h"
34#include "ui/base/resource/resource_bundle.h"
35#include "ui/gfx/image/image.h"
[email protected]d4f5d1162011-11-30 01:41:5236#include "ui/gfx/image/image_unittest_util.h"
[email protected]dee810e2011-06-27 19:43:3937
[email protected]6778fed2013-12-24 20:09:3738using base::ASCIIToUTF16;
[email protected]f729d7a2013-12-26 07:07:5639using base::UTF8ToUTF16;
[email protected]cb114f142011-11-23 20:18:0440using content::BrowserThread;
41
[email protected]6a460662011-12-22 22:05:1642ProfileNameVerifierObserver::ProfileNameVerifierObserver(
43 TestingProfileManager* testing_profile_manager)
44 : testing_profile_manager_(testing_profile_manager) {
45 DCHECK(testing_profile_manager_);
[email protected]590e189b2011-12-13 22:07:0346}
47
48ProfileNameVerifierObserver::~ProfileNameVerifierObserver() {
49}
50
51void ProfileNameVerifierObserver::OnProfileAdded(
[email protected]650b2d52013-02-10 03:41:4552 const base::FilePath& profile_path) {
[email protected]a04db822013-12-11 19:14:4053 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1654 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0355 EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end());
56 profile_names_.insert(profile_name);
57}
58
[email protected]7b0af152011-12-16 17:02:0659void ProfileNameVerifierObserver::OnProfileWillBeRemoved(
[email protected]650b2d52013-02-10 03:41:4560 const base::FilePath& profile_path) {
[email protected]a04db822013-12-11 19:14:4061 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1662 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0363 EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end());
64 profile_names_.erase(profile_name);
65}
66
[email protected]7b0af152011-12-16 17:02:0667void ProfileNameVerifierObserver::OnProfileWasRemoved(
[email protected]650b2d52013-02-10 03:41:4568 const base::FilePath& profile_path,
[email protected]a04db822013-12-11 19:14:4069 const base::string16& profile_name) {
[email protected]7b0af152011-12-16 17:02:0670 EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end());
71}
72
[email protected]590e189b2011-12-13 22:07:0373void ProfileNameVerifierObserver::OnProfileNameChanged(
[email protected]650b2d52013-02-10 03:41:4574 const base::FilePath& profile_path,
[email protected]a04db822013-12-11 19:14:4075 const base::string16& old_profile_name) {
76 base::string16 new_profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1677 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0378 EXPECT_TRUE(profile_names_.find(old_profile_name) != profile_names_.end());
79 EXPECT_TRUE(profile_names_.find(new_profile_name) == profile_names_.end());
80 profile_names_.erase(old_profile_name);
81 profile_names_.insert(new_profile_name);
82}
83
84void ProfileNameVerifierObserver::OnProfileAvatarChanged(
[email protected]650b2d52013-02-10 03:41:4585 const base::FilePath& profile_path) {
[email protected]a04db822013-12-11 19:14:4086 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1687 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0388 EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end());
89}
90
[email protected]6a460662011-12-22 22:05:1691ProfileInfoCache* ProfileNameVerifierObserver::GetCache() {
92 return testing_profile_manager_->profile_info_cache();
93}
94
[email protected]d4f5d1162011-11-30 01:41:5295ProfileInfoCacheTest::ProfileInfoCacheTest()
[email protected]0aa018a2013-07-31 15:08:5496 : testing_profile_manager_(TestingBrowserProcess::GetGlobal()),
emaxxa2e11492015-05-12 17:19:1197 name_observer_(&testing_profile_manager_),
98 user_data_dir_override_(chrome::DIR_USER_DATA) {
[email protected]d4f5d1162011-11-30 01:41:5299}
100
101ProfileInfoCacheTest::~ProfileInfoCacheTest() {
102}
103
104void ProfileInfoCacheTest::SetUp() {
105 ASSERT_TRUE(testing_profile_manager_.SetUp());
[email protected]590e189b2011-12-13 22:07:03106 testing_profile_manager_.profile_info_cache()->AddObserver(&name_observer_);
[email protected]d4f5d1162011-11-30 01:41:52107}
108
[email protected]1302dcf2011-11-30 21:47:05109void ProfileInfoCacheTest::TearDown() {
110 // Drain the UI thread to make sure all tasks are completed. This prevents
111 // memory leaks.
[email protected]0aa018a2013-07-31 15:08:54112 base::RunLoop().RunUntilIdle();
[email protected]1302dcf2011-11-30 21:47:05113}
114
[email protected]d4f5d1162011-11-30 01:41:52115ProfileInfoCache* ProfileInfoCacheTest::GetCache() {
116 return testing_profile_manager_.profile_info_cache();
117}
118
[email protected]650b2d52013-02-10 03:41:45119base::FilePath ProfileInfoCacheTest::GetProfilePath(
[email protected]d4f5d1162011-11-30 01:41:52120 const std::string& base_name) {
121 return testing_profile_manager_.profile_manager()->user_data_dir().
122 AppendASCII(base_name);
123}
124
125void ProfileInfoCacheTest::ResetCache() {
126 testing_profile_manager_.DeleteProfileInfoCache();
127}
128
[email protected]d4f5d1162011-11-30 01:41:52129TEST_F(ProfileInfoCacheTest, AddProfiles) {
[email protected]6730b1e2011-09-29 05:23:52130 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39131
[email protected]ca591072012-03-27 01:54:44132 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
avib896c712015-12-26 02:10:43133 for (uint32_t i = 0; i < 4; ++i) {
[email protected]7d3cbc92013-03-18 22:33:04134 base::FilePath profile_path =
135 GetProfilePath(base::StringPrintf("path_%ud", i));
[email protected]a04db822013-12-11 19:14:40136 base::string16 profile_name =
137 ASCIIToUTF16(base::StringPrintf("name_%ud", i));
[email protected]ca591072012-03-27 01:54:44138 const SkBitmap* icon = rb.GetImageNamed(
[email protected]c3e559772014-04-09 04:02:54139 profiles::GetDefaultAvatarIconResourceIDAtIndex(
[email protected]ca591072012-03-27 01:54:44140 i)).ToSkBitmap();
[email protected]d20d0432014-06-12 17:14:05141 std::string supervised_user_id = i == 3 ? "TEST_ID" : "";
[email protected]dee810e2011-06-27 19:43:39142
rogertae22659602015-05-11 21:58:21143 GetCache()->AddProfileToCache(profile_path, profile_name, std::string(),
144 base::string16(), i, supervised_user_id);
[email protected]cb114f142011-11-23 20:18:04145 GetCache()->SetBackgroundStatusOfProfileAtIndex(i, true);
[email protected]a04db822013-12-11 19:14:40146 base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
[email protected]cb114f142011-11-23 20:18:04147 GetCache()->SetGAIANameOfProfileAtIndex(i, gaia_name);
[email protected]dee810e2011-06-27 19:43:39148
[email protected]6730b1e2011-09-29 05:23:52149 EXPECT_EQ(i + 1, GetCache()->GetNumberOfProfiles());
150 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
151 EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i));
[email protected]0aa018a2013-07-31 15:08:54152 const SkBitmap* actual_icon =
153 GetCache()->GetAvatarIconOfProfileAtIndex(i).ToSkBitmap();
[email protected]ca591072012-03-27 01:54:44154 EXPECT_EQ(icon->width(), actual_icon->width());
155 EXPECT_EQ(icon->height(), actual_icon->height());
[email protected]d20d0432014-06-12 17:14:05156 EXPECT_EQ(i == 3, GetCache()->ProfileIsSupervisedAtIndex(i));
[email protected]5ddfade2014-02-03 10:24:53157 EXPECT_EQ(i == 3, GetCache()->IsOmittedProfileAtIndex(i));
[email protected]d20d0432014-06-12 17:14:05158 EXPECT_EQ(supervised_user_id,
159 GetCache()->GetSupervisedUserIdOfProfileAtIndex(i));
[email protected]dee810e2011-06-27 19:43:39160 }
[email protected]cb114f142011-11-23 20:18:04161
162 // Reset the cache and test the it reloads correctly.
163 ResetCache();
164
165 EXPECT_EQ(4u, GetCache()->GetNumberOfProfiles());
avib896c712015-12-26 02:10:43166 for (uint32_t i = 0; i < 4; ++i) {
[email protected]7d3cbc92013-03-18 22:33:04167 base::FilePath profile_path =
168 GetProfilePath(base::StringPrintf("path_%ud", i));
[email protected]cb114f142011-11-23 20:18:04169 EXPECT_EQ(i, GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]a04db822013-12-11 19:14:40170 base::string16 profile_name =
171 ASCIIToUTF16(base::StringPrintf("name_%ud", i));
[email protected]cb114f142011-11-23 20:18:04172 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
173 EXPECT_EQ(i, GetCache()->GetAvatarIconIndexOfProfileAtIndex(i));
174 EXPECT_EQ(true, GetCache()->GetBackgroundStatusOfProfileAtIndex(i));
[email protected]a04db822013-12-11 19:14:40175 base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
[email protected]cb114f142011-11-23 20:18:04176 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(i));
177 }
[email protected]dee810e2011-06-27 19:43:39178}
179
[email protected]d4f5d1162011-11-30 01:41:52180TEST_F(ProfileInfoCacheTest, DeleteProfile) {
[email protected]6730b1e2011-09-29 05:23:52181 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39182
[email protected]650b2d52013-02-10 03:41:45183 base::FilePath path_1 = GetProfilePath("path_1");
[email protected]a04db822013-12-11 19:14:40184 GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("name_1"),
rogertae22659602015-05-11 21:58:21185 std::string(), base::string16(), 0,
186 std::string());
[email protected]6730b1e2011-09-29 05:23:52187 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39188
[email protected]650b2d52013-02-10 03:41:45189 base::FilePath path_2 = GetProfilePath("path_2");
[email protected]a04db822013-12-11 19:14:40190 base::string16 name_2 = ASCIIToUTF16("name_2");
rogertae22659602015-05-11 21:58:21191 GetCache()->AddProfileToCache(path_2, name_2, std::string(), base::string16(),
192 0, std::string());
[email protected]6730b1e2011-09-29 05:23:52193 EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39194
[email protected]6730b1e2011-09-29 05:23:52195 GetCache()->DeleteProfileFromCache(path_1);
196 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
197 EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(0));
[email protected]dee810e2011-06-27 19:43:39198
[email protected]6730b1e2011-09-29 05:23:52199 GetCache()->DeleteProfileFromCache(path_2);
200 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39201}
202
[email protected]d4f5d1162011-11-30 01:41:52203TEST_F(ProfileInfoCacheTest, MutateProfile) {
204 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21205 GetProfilePath("path_1"), ASCIIToUTF16("name_1"), std::string(),
[email protected]a04db822013-12-11 19:14:40206 base::string16(), 0, std::string());
[email protected]d4f5d1162011-11-30 01:41:52207 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21208 GetProfilePath("path_2"), ASCIIToUTF16("name_2"), std::string(),
[email protected]a04db822013-12-11 19:14:40209 base::string16(), 0, std::string());
[email protected]dee810e2011-06-27 19:43:39210
[email protected]a04db822013-12-11 19:14:40211 base::string16 new_name = ASCIIToUTF16("new_name");
[email protected]6730b1e2011-09-29 05:23:52212 GetCache()->SetNameOfProfileAtIndex(1, new_name);
213 EXPECT_EQ(new_name, GetCache()->GetNameOfProfileAtIndex(1));
214 EXPECT_NE(new_name, GetCache()->GetNameOfProfileAtIndex(0));
[email protected]dee810e2011-06-27 19:43:39215
[email protected]a04db822013-12-11 19:14:40216 base::string16 new_user_name = ASCIIToUTF16("user_name");
rogertae22659602015-05-11 21:58:21217 std::string new_gaia_id = "12345";
218 GetCache()->SetAuthInfoOfProfileAtIndex(1, new_gaia_id, new_user_name);
[email protected]e8e78092011-09-29 18:15:38219 EXPECT_EQ(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(1));
rogertae22659602015-05-11 21:58:21220 EXPECT_EQ(new_gaia_id, GetCache()->GetGAIAIdOfProfileAtIndex(1));
[email protected]e8e78092011-09-29 18:15:38221 EXPECT_NE(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(0));
222
[email protected]dee810e2011-06-27 19:43:39223 size_t new_icon_index = 3;
[email protected]6730b1e2011-09-29 05:23:52224 GetCache()->SetAvatarIconOfProfileAtIndex(1, new_icon_index);
[email protected]dee810e2011-06-27 19:43:39225 // Not much to test.
[email protected]6730b1e2011-09-29 05:23:52226 GetCache()->GetAvatarIconOfProfileAtIndex(1);
[email protected]dee810e2011-06-27 19:43:39227}
228
[email protected]d4f5d1162011-11-30 01:41:52229TEST_F(ProfileInfoCacheTest, Sort) {
[email protected]a04db822013-12-11 19:14:40230 base::string16 name_a = ASCIIToUTF16("apple");
[email protected]d4f5d1162011-11-30 01:41:52231 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21232 GetProfilePath("path_a"), name_a, std::string(), base::string16(), 0,
233 std::string());
[email protected]cb114f142011-11-23 20:18:04234
[email protected]a04db822013-12-11 19:14:40235 base::string16 name_c = ASCIIToUTF16("cat");
[email protected]d4f5d1162011-11-30 01:41:52236 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21237 GetProfilePath("path_c"), name_c, std::string(), base::string16(), 0,
238 std::string());
[email protected]cb114f142011-11-23 20:18:04239
240 // Sanity check the initial order.
241 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
242 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
243
244 // Add a new profile (start with a capital to test case insensitive sorting.
[email protected]a04db822013-12-11 19:14:40245 base::string16 name_b = ASCIIToUTF16("Banana");
[email protected]d4f5d1162011-11-30 01:41:52246 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21247 GetProfilePath("path_b"), name_b, std::string(), base::string16(), 0,
248 std::string());
[email protected]cb114f142011-11-23 20:18:04249
250 // Verify the new order.
251 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
252 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(1));
253 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(2));
254
255 // Change the name of an existing profile.
256 name_a = UTF8ToUTF16("dog");
257 GetCache()->SetNameOfProfileAtIndex(0, name_a);
258
259 // Verify the new order.
260 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
261 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
262 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(2));
263
264 // Delete a profile.
[email protected]d4f5d1162011-11-30 01:41:52265 GetCache()->DeleteProfileFromCache(GetProfilePath("path_c"));
[email protected]cb114f142011-11-23 20:18:04266
267 // Verify the new order.
268 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
269 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(1));
270}
271
[email protected]d4f5d1162011-11-30 01:41:52272TEST_F(ProfileInfoCacheTest, BackgroundModeStatus) {
[email protected]279170832011-10-12 23:38:03273 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13274 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
rogertae22659602015-05-11 21:58:21275 std::string(), base::string16(), 0, std::string());
[email protected]279170832011-10-12 23:38:03276 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13277 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
rogertae22659602015-05-11 21:58:21278 std::string(), base::string16(), 0, std::string());
[email protected]279170832011-10-12 23:38:03279
280 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
281 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
282
283 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, true);
284
285 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
286 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
287
288 GetCache()->SetBackgroundStatusOfProfileAtIndex(0, true);
289
290 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
291 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
292
293 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, false);
294
295 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
296 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
297}
298
[email protected]85503a02014-01-16 17:27:22299TEST_F(ProfileInfoCacheTest, ProfileActiveTime) {
300 GetCache()->AddProfileToCache(
301 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
rogertae22659602015-05-11 21:58:21302 std::string(), base::string16(), 0, std::string());
[email protected]85503a02014-01-16 17:27:22303 EXPECT_EQ(base::Time(), GetCache()->GetProfileActiveTimeAtIndex(0));
[email protected]87727e02014-02-27 18:16:53304 // Before & After times are artificially shifted because just relying upon
305 // the system time can yield problems due to inaccuracies in the
306 // underlying storage system (which uses a double with only 52 bits of
307 // precision to store the 64-bit "time" number). http://crbug.com/346827
[email protected]85503a02014-01-16 17:27:22308 base::Time before = base::Time::Now();
[email protected]87727e02014-02-27 18:16:53309 before -= base::TimeDelta::FromSeconds(1);
[email protected]85503a02014-01-16 17:27:22310 GetCache()->SetProfileActiveTimeAtIndex(0);
311 base::Time after = base::Time::Now();
[email protected]87727e02014-02-27 18:16:53312 after += base::TimeDelta::FromSeconds(1);
[email protected]85503a02014-01-16 17:27:22313 EXPECT_LE(before, GetCache()->GetProfileActiveTimeAtIndex(0));
314 EXPECT_GE(after, GetCache()->GetProfileActiveTimeAtIndex(0));
315}
316
[email protected]d4f5d1162011-11-30 01:41:52317TEST_F(ProfileInfoCacheTest, GAIAName) {
[email protected]cb114f142011-11-23 20:18:04318 GetCache()->AddProfileToCache(
[email protected]a4352d02014-03-06 06:00:27319 GetProfilePath("path_1"), ASCIIToUTF16("Person 1"),
rogertae22659602015-05-11 21:58:21320 std::string(), base::string16(), 0, std::string());
[email protected]a4352d02014-03-06 06:00:27321 base::string16 profile_name(ASCIIToUTF16("Person 2"));
[email protected]cb114f142011-11-23 20:18:04322 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21323 GetProfilePath("path_2"), profile_name, std::string(), base::string16(),
324 0, std::string());
[email protected]cb114f142011-11-23 20:18:04325
[email protected]a4352d02014-03-06 06:00:27326 int index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
327 int index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
328
[email protected]cb114f142011-11-23 20:18:04329 // Sanity check.
[email protected]a4352d02014-03-06 06:00:27330 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty());
331 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index2).empty());
[email protected]cb114f142011-11-23 20:18:04332
[email protected]425d6ab2014-03-25 05:59:39333 // Set GAIA name. This re-sorts the cache.
[email protected]a04db822013-12-11 19:14:40334 base::string16 gaia_name(ASCIIToUTF16("Pat Smith"));
[email protected]a4352d02014-03-06 06:00:27335 GetCache()->SetGAIANameOfProfileAtIndex(index2, gaia_name);
[email protected]425d6ab2014-03-25 05:59:39336 index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
337 index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
338
339 // Since there is a GAIA name, we use that as a display name.
[email protected]a4352d02014-03-06 06:00:27340 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty());
341 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2));
[email protected]a4352d02014-03-06 06:00:27342 EXPECT_EQ(gaia_name, GetCache()->GetNameOfProfileAtIndex(index2));
[email protected]cb114f142011-11-23 20:18:04343
[email protected]a4352d02014-03-06 06:00:27344 // Don't use GAIA name as profile name. This re-sorts the cache.
[email protected]425d6ab2014-03-25 05:59:39345 base::string16 custom_name(ASCIIToUTF16("Custom name"));
346 GetCache()->SetNameOfProfileAtIndex(index2, custom_name);
[email protected]1cdf0932014-08-16 06:00:49347 GetCache()->SetProfileIsUsingDefaultNameAtIndex(index2, false);
348
[email protected]a4352d02014-03-06 06:00:27349 index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
350 index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
351
[email protected]425d6ab2014-03-25 05:59:39352 EXPECT_EQ(custom_name, GetCache()->GetNameOfProfileAtIndex(index2));
[email protected]a4352d02014-03-06 06:00:27353 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2));
[email protected]cb114f142011-11-23 20:18:04354}
355
[email protected]d4f5d1162011-11-30 01:41:52356TEST_F(ProfileInfoCacheTest, GAIAPicture) {
[email protected]22900902014-07-30 11:50:41357 const int kDefaultAvatarIndex = 0;
358 const int kOtherAvatarIndex = 1;
[email protected]979dc4f2014-08-12 22:52:11359 const int kGaiaPictureSize = 256; // Standard size of a Gaia account picture.
[email protected]cb114f142011-11-23 20:18:04360 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13361 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
rogertae22659602015-05-11 21:58:21362 std::string(), base::string16(), kDefaultAvatarIndex, std::string());
[email protected]cb114f142011-11-23 20:18:04363 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13364 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
rogertae22659602015-05-11 21:58:21365 std::string(), base::string16(), kDefaultAvatarIndex, std::string());
[email protected]cb114f142011-11-23 20:18:04366
367 // Sanity check.
[email protected]2f3c00f2011-11-30 04:36:22368 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
369 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(1));
[email protected]cb114f142011-11-23 20:18:04370 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0));
371 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
372
373 // The profile icon should be the default one.
[email protected]22900902014-07-30 11:50:41374 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(0));
375 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
376 int default_avatar_id =
377 profiles::GetDefaultAvatarIconResourceIDAtIndex(kDefaultAvatarIndex);
378 const gfx::Image& default_avatar_image(
379 ResourceBundle::GetSharedInstance().GetImageNamed(default_avatar_id));
pkotwiczb312b512015-09-29 23:44:43380 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]22900902014-07-30 11:50:41381 default_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
[email protected]cb114f142011-11-23 20:18:04382
383 // Set GAIA picture.
[email protected]979dc4f2014-08-12 22:52:11384 gfx::Image gaia_image(gfx::test::CreateImage(
385 kGaiaPictureSize, kGaiaPictureSize));
[email protected]2f3c00f2011-11-30 04:36:22386 GetCache()->SetGAIAPictureOfProfileAtIndex(1, &gaia_image);
387 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
pkotwiczb312b512015-09-29 23:44:43388 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22389 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
[email protected]22900902014-07-30 11:50:41390 // Since we're still using the default avatar, the GAIA image should be
391 // preferred over the generic avatar image.
392 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
393 EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
pkotwiczb312b512015-09-29 23:44:43394 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]22900902014-07-30 11:50:41395 gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
[email protected]cb114f142011-11-23 20:18:04396
[email protected]1cdf0932014-08-16 06:00:49397 // Set a non-default avatar. This should be preferred over the GAIA image.
[email protected]22900902014-07-30 11:50:41398 GetCache()->SetAvatarIconOfProfileAtIndex(1, kOtherAvatarIndex);
[email protected]1cdf0932014-08-16 06:00:49399 GetCache()->SetProfileIsUsingDefaultAvatarAtIndex(1, false);
[email protected]22900902014-07-30 11:50:41400 EXPECT_FALSE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
401 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
402 int other_avatar_id =
403 profiles::GetDefaultAvatarIconResourceIDAtIndex(kOtherAvatarIndex);
404 const gfx::Image& other_avatar_image(
405 ResourceBundle::GetSharedInstance().GetImageNamed(other_avatar_id));
pkotwiczb312b512015-09-29 23:44:43406 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]22900902014-07-30 11:50:41407 other_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
408
409 // Explicitly setting the GAIA picture should make it preferred again.
[email protected]cb114f142011-11-23 20:18:04410 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, true);
[email protected]22900902014-07-30 11:50:41411 EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
pkotwiczb312b512015-09-29 23:44:43412 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22413 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
pkotwiczb312b512015-09-29 23:44:43414 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]cb114f142011-11-23 20:18:04415 gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
416
[email protected]22900902014-07-30 11:50:41417 // Clearing the IsUsingGAIAPicture flag should result in the generic image
418 // being used again.
[email protected]cb114f142011-11-23 20:18:04419 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, false);
[email protected]22900902014-07-30 11:50:41420 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
pkotwiczb312b512015-09-29 23:44:43421 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22422 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
pkotwiczb312b512015-09-29 23:44:43423 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]22900902014-07-30 11:50:41424 other_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
[email protected]cb114f142011-11-23 20:18:04425}
426
[email protected]754bebc2011-12-01 16:42:16427TEST_F(ProfileInfoCacheTest, PersistGAIAPicture) {
[email protected]cb114f142011-11-23 20:18:04428 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13429 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
rogertae22659602015-05-11 21:58:21430 std::string(), base::string16(), 0, std::string());
[email protected]d4f5d1162011-11-30 01:41:52431 gfx::Image gaia_image(gfx::test::CreateImage());
[email protected]cb114f142011-11-23 20:18:04432
[email protected]2f3c00f2011-11-30 04:36:22433 GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_image);
noms75ca0192015-02-26 03:14:30434
435 // Make sure everything has completed, and the file has been written to disk.
436 base::RunLoop().RunUntilIdle();
437
pkotwiczb312b512015-09-29 23:44:43438 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22439 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
[email protected]cb114f142011-11-23 20:18:04440
[email protected]cb114f142011-11-23 20:18:04441 ResetCache();
[email protected]cb114f142011-11-23 20:18:04442 // Try to get the GAIA picture. This should return NULL until the read from
443 // disk is done.
[email protected]2f3c00f2011-11-30 04:36:22444 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
noms75ca0192015-02-26 03:14:30445 base::RunLoop().RunUntilIdle();
446
pkotwiczb312b512015-09-29 23:44:43447 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22448 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
449}
450
[email protected]d20d0432014-06-12 17:14:05451TEST_F(ProfileInfoCacheTest, SetSupervisedUserId) {
[email protected]0aa018a2013-07-31 15:08:54452 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13453 GetProfilePath("test"), ASCIIToUTF16("Test"),
rogertae22659602015-05-11 21:58:21454 std::string(), base::string16(), 0, std::string());
[email protected]d20d0432014-06-12 17:14:05455 EXPECT_FALSE(GetCache()->ProfileIsSupervisedAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54456
[email protected]d20d0432014-06-12 17:14:05457 GetCache()->SetSupervisedUserIdOfProfileAtIndex(0, "TEST_ID");
458 EXPECT_TRUE(GetCache()->ProfileIsSupervisedAtIndex(0));
459 EXPECT_EQ("TEST_ID", GetCache()->GetSupervisedUserIdOfProfileAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54460
461 ResetCache();
[email protected]d20d0432014-06-12 17:14:05462 EXPECT_TRUE(GetCache()->ProfileIsSupervisedAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54463
[email protected]d20d0432014-06-12 17:14:05464 GetCache()->SetSupervisedUserIdOfProfileAtIndex(0, std::string());
465 EXPECT_FALSE(GetCache()->ProfileIsSupervisedAtIndex(0));
466 EXPECT_EQ("", GetCache()->GetSupervisedUserIdOfProfileAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54467}
468
[email protected]2f3c00f2011-11-30 04:36:22469TEST_F(ProfileInfoCacheTest, EmptyGAIAInfo) {
[email protected]a04db822013-12-11 19:14:40470 base::string16 profile_name = ASCIIToUTF16("name_1");
[email protected]c3e559772014-04-09 04:02:54471 int id = profiles::GetDefaultAvatarIconResourceIDAtIndex(0);
[email protected]2f3c00f2011-11-30 04:36:22472 const gfx::Image& profile_image(
473 ResourceBundle::GetSharedInstance().GetImageNamed(id));
474
475 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21476 GetProfilePath("path_1"), profile_name, std::string(), base::string16(),
477 0, std::string());
[email protected]2f3c00f2011-11-30 04:36:22478
479 // Set empty GAIA info.
[email protected]a04db822013-12-11 19:14:40480 GetCache()->SetGAIANameOfProfileAtIndex(0, base::string16());
[email protected]2f3c00f2011-11-30 04:36:22481 GetCache()->SetGAIAPictureOfProfileAtIndex(0, NULL);
[email protected]2f3c00f2011-11-30 04:36:22482 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(0, true);
483
484 // Verify that the profile name and picture are not empty.
485 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(0));
pkotwiczb312b512015-09-29 23:44:43486 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22487 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(0)));
[email protected]cb114f142011-11-23 20:18:04488}
489
[email protected]d20d0432014-06-12 17:14:05490TEST_F(ProfileInfoCacheTest, CreateSupervisedTestingProfile) {
[email protected]0aa018a2013-07-31 15:08:54491 testing_profile_manager_.CreateTestingProfile("default");
[email protected]d20d0432014-06-12 17:14:05492 base::string16 supervised_user_name = ASCIIToUTF16("Supervised User");
[email protected]0aa018a2013-07-31 15:08:54493 testing_profile_manager_.CreateTestingProfile(
sdefresne50c1e522015-09-18 09:47:51494 "test1", scoped_ptr<syncable_prefs::PrefServiceSyncable>(),
[email protected]d20d0432014-06-12 17:14:05495 supervised_user_name, 0, "TEST_ID", TestingProfile::TestingFactories());
[email protected]0aa018a2013-07-31 15:08:54496 for (size_t i = 0; i < GetCache()->GetNumberOfProfiles(); i++) {
[email protected]d20d0432014-06-12 17:14:05497 bool is_supervised =
498 GetCache()->GetNameOfProfileAtIndex(i) == supervised_user_name;
499 EXPECT_EQ(is_supervised, GetCache()->ProfileIsSupervisedAtIndex(i));
500 std::string supervised_user_id = is_supervised ? "TEST_ID" : "";
501 EXPECT_EQ(supervised_user_id,
502 GetCache()->GetSupervisedUserIdOfProfileAtIndex(i));
[email protected]0aa018a2013-07-31 15:08:54503 }
[email protected]371662e372013-10-17 22:05:22504
[email protected]d20d0432014-06-12 17:14:05505 // Supervised profiles have a custom theme, which needs to be deleted on the
506 // FILE thread. Reset the profile manager now so everything is deleted while
507 // we still have a FILE thread.
[email protected]371662e372013-10-17 22:05:22508 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
[email protected]0aa018a2013-07-31 15:08:54509}
510
[email protected]047481802013-09-16 22:26:38511TEST_F(ProfileInfoCacheTest, AddStubProfile) {
512 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
513
514 // Add some profiles with and without a '.' in their paths.
515 const struct {
516 const char* profile_path;
517 const char* profile_name;
518 } kTestCases[] = {
519 { "path.test0", "name_0" },
520 { "path_test1", "name_1" },
521 { "path.test2", "name_2" },
522 { "path_test3", "name_3" },
523 };
524
viettrungluu9e65ad12014-10-16 04:22:26525 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
[email protected]047481802013-09-16 22:26:38526 base::FilePath profile_path = GetProfilePath(kTestCases[i].profile_path);
[email protected]a04db822013-12-11 19:14:40527 base::string16 profile_name = ASCIIToUTF16(kTestCases[i].profile_name);
[email protected]047481802013-09-16 22:26:38528
rogertae22659602015-05-11 21:58:21529 GetCache()->AddProfileToCache(profile_path, profile_name, std::string(),
530 base::string16(), i, "");
[email protected]047481802013-09-16 22:26:38531
532 EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i));
533 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
534 }
535
536 ASSERT_EQ(4U, GetCache()->GetNumberOfProfiles());
537
538 // Check that the profiles can be extracted from the local state.
noms693f35aa2015-02-05 16:03:16539 std::vector<base::string16> names;
540 PrefService* local_state = g_browser_process->local_state();
541 const base::DictionaryValue* cache = local_state->GetDictionary(
542 prefs::kProfileInfoCache);
543 base::string16 name;
544 for (base::DictionaryValue::Iterator it(*cache); !it.IsAtEnd();
545 it.Advance()) {
546 const base::DictionaryValue* info = NULL;
547 it.value().GetAsDictionary(&info);
548 info->GetString("name", &name);
549 names.push_back(name);
550 }
551
[email protected]047481802013-09-16 22:26:38552 for (size_t i = 0; i < 4; i++)
553 ASSERT_FALSE(names[i].empty());
554}
555
[email protected]b09bfa12014-08-15 19:05:25556// High res avatar downloading is only supported on desktop.
557#if !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_CHROMEOS)
[email protected]9de44302014-05-01 16:32:27558TEST_F(ProfileInfoCacheTest, DownloadHighResAvatarTest) {
mlerman3ab32772015-04-08 14:48:56559 // The TestingProfileManager's ProfileInfoCache doesn't download avatars.
560 ProfileInfoCache profile_info_cache(g_browser_process->local_state(),
561 testing_profile_manager_.profile_manager()->user_data_dir());
562
emaxxa2e11492015-05-12 17:19:11563 // Make sure there are no avatars already on disk.
mlerman3ab32772015-04-08 14:48:56564 const size_t kIconIndex = 0;
565 base::FilePath icon_path =
566 profiles::GetPathOfHighResAvatarAtIndex(kIconIndex);
567 EXPECT_FALSE(base::PathExists(icon_path));
568
569 EXPECT_EQ(0U, profile_info_cache.GetNumberOfProfiles());
[email protected]9de44302014-05-01 16:32:27570 base::FilePath path_1 = GetProfilePath("path_1");
mlerman3ab32772015-04-08 14:48:56571 profile_info_cache.AddProfileToCache(path_1, ASCIIToUTF16("name_1"),
rogertae22659602015-05-11 21:58:21572 std::string(), base::string16(), kIconIndex, std::string());
mlerman3ab32772015-04-08 14:48:56573 EXPECT_EQ(1U, profile_info_cache.GetNumberOfProfiles());
noms28693ab22015-01-28 21:44:54574 base::RunLoop().RunUntilIdle();
[email protected]9de44302014-05-01 16:32:27575
576 // We haven't downloaded any high-res avatars yet.
mlerman3ab32772015-04-08 14:48:56577 EXPECT_EQ(0U, profile_info_cache.cached_avatar_images_.size());
[email protected]5cf8bcd92014-07-17 20:27:40578
579 // After adding a new profile, the download of high-res avatar will be
580 // triggered if the flag kNewAvatarMenu has been set. But the downloader
581 // won't ever call OnFetchComplete in the test.
mlerman3ab32772015-04-08 14:48:56582 EXPECT_EQ(1U, profile_info_cache.avatar_images_downloads_in_progress_.size());
[email protected]5cf8bcd92014-07-17 20:27:40583
mlerman3ab32772015-04-08 14:48:56584 EXPECT_FALSE(profile_info_cache.GetHighResAvatarOfProfileAtIndex(0));
[email protected]9de44302014-05-01 16:32:27585
586 // Simulate downloading a high-res avatar.
[email protected]2d605712014-05-14 12:03:07587 ProfileAvatarDownloader avatar_downloader(
mlerman3ab32772015-04-08 14:48:56588 kIconIndex, profile_info_cache.GetPathOfProfileAtIndex(0),
589 &profile_info_cache);
[email protected]9de44302014-05-01 16:32:27590
591 // Put a real bitmap into "bitmap". 2x2 bitmap of green 32 bit pixels.
592 SkBitmap bitmap;
[email protected]e61cad52014-07-01 21:01:57593 bitmap.allocN32Pixels(2, 2);
[email protected]9de44302014-05-01 16:32:27594 bitmap.eraseColor(SK_ColorGREEN);
595
596 avatar_downloader.OnFetchComplete(
597 GURL("http://www.google.com/avatar.png"), &bitmap);
598
treibaf255502015-01-30 17:47:53599 // Now the download should not be in progress anymore.
mlerman3ab32772015-04-08 14:48:56600 EXPECT_EQ(0U, profile_info_cache.avatar_images_downloads_in_progress_.size());
treibaf255502015-01-30 17:47:53601
[email protected]9de44302014-05-01 16:32:27602 std::string file_name =
603 profiles::GetDefaultAvatarIconFileNameAtIndex(kIconIndex);
604
605 // The file should have been cached and saved.
mlerman3ab32772015-04-08 14:48:56606 EXPECT_EQ(1U, profile_info_cache.cached_avatar_images_.size());
607 EXPECT_TRUE(profile_info_cache.GetHighResAvatarOfProfileAtIndex(0));
608 EXPECT_EQ(profile_info_cache.cached_avatar_images_[file_name],
609 profile_info_cache.GetHighResAvatarOfProfileAtIndex(0));
[email protected]9de44302014-05-01 16:32:27610
611 // Make sure everything has completed, and the file has been written to disk.
612 base::RunLoop().RunUntilIdle();
613
614 // Clean up.
[email protected]9de44302014-05-01 16:32:27615 EXPECT_NE(std::string::npos, icon_path.MaybeAsASCII().find(file_name));
616 EXPECT_TRUE(base::PathExists(icon_path));
thestig16a4bcae2015-12-07 08:36:17617 EXPECT_TRUE(base::DeleteFile(icon_path, false));
[email protected]9de44302014-05-01 16:32:27618 EXPECT_FALSE(base::PathExists(icon_path));
619}
nomsbe073e52014-08-23 22:01:04620
emaxx0cb1957132015-05-18 20:22:54621TEST_F(ProfileInfoCacheTest, NothingToDownloadHighResAvatarTest) {
emaxx0cb1957132015-05-18 20:22:54622 // The TestingProfileManager's ProfileInfoCache doesn't download avatars.
623 ProfileInfoCache profile_info_cache(
624 g_browser_process->local_state(),
625 testing_profile_manager_.profile_manager()->user_data_dir());
626
627 const size_t kIconIndex = profiles::GetPlaceholderAvatarIndex();
628
629 EXPECT_EQ(0U, profile_info_cache.GetNumberOfProfiles());
630 base::FilePath path_1 = GetProfilePath("path_1");
631 profile_info_cache.AddProfileToCache(path_1, ASCIIToUTF16("name_1"),
632 std::string(), base::string16(),
633 kIconIndex, std::string());
634 EXPECT_EQ(1U, profile_info_cache.GetNumberOfProfiles());
635 base::RunLoop().RunUntilIdle();
636
637 // We haven't tried to download any high-res avatars as the specified icon is
638 // just a placeholder.
639 EXPECT_EQ(0U, profile_info_cache.cached_avatar_images_.size());
640 EXPECT_EQ(0U, profile_info_cache.avatar_images_downloads_in_progress_.size());
641}
642
nomsbe073e52014-08-23 22:01:04643TEST_F(ProfileInfoCacheTest, MigrateLegacyProfileNamesWithNewAvatarMenu) {
nomsbe073e52014-08-23 22:01:04644 EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles());
645
646 base::FilePath path_1 = GetProfilePath("path_1");
647 GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("Default Profile"),
rogertae22659602015-05-11 21:58:21648 std::string(), base::string16(), 0,
649 std::string());
nomsbe073e52014-08-23 22:01:04650 base::FilePath path_2 = GetProfilePath("path_2");
651 GetCache()->AddProfileToCache(path_2, ASCIIToUTF16("First user"),
rogertae22659602015-05-11 21:58:21652 std::string(), base::string16(), 1,
653 std::string());
nomsbe073e52014-08-23 22:01:04654 base::string16 name_3 = ASCIIToUTF16("Lemonade");
655 base::FilePath path_3 = GetProfilePath("path_3");
656 GetCache()->AddProfileToCache(path_3, name_3,
rogertae22659602015-05-11 21:58:21657 std::string(), base::string16(), 2,
658 std::string());
nomsbe073e52014-08-23 22:01:04659 base::string16 name_4 = ASCIIToUTF16("Batman");
660 base::FilePath path_4 = GetProfilePath("path_4");
661 GetCache()->AddProfileToCache(path_4, name_4,
rogertae22659602015-05-11 21:58:21662 std::string(), base::string16(), 3,
663 std::string());
nomsbe073e52014-08-23 22:01:04664 base::string16 name_5 = ASCIIToUTF16("Person 2");
665 base::FilePath path_5 = GetProfilePath("path_5");
666 GetCache()->AddProfileToCache(path_5, name_5,
rogertae22659602015-05-11 21:58:21667 std::string(), base::string16(), 2,
668 std::string());
nomsbe073e52014-08-23 22:01:04669
670 EXPECT_EQ(5U, GetCache()->GetNumberOfProfiles());
671
672
673 ResetCache();
674
675 // Legacy profile names like "Default Profile" and "First user" should be
676 // migrated to "Person %n" type names.
677 EXPECT_EQ(ASCIIToUTF16("Person 1"), GetCache()->GetNameOfProfileAtIndex(
678 GetCache()->GetIndexOfProfileWithPath(path_1)));
679 EXPECT_EQ(ASCIIToUTF16("Person 3"), GetCache()->GetNameOfProfileAtIndex(
680 GetCache()->GetIndexOfProfileWithPath(path_2)));
681
682 // Other profile names should not be migrated even if they're the old
683 // default cartoon profile names.
684 EXPECT_EQ(name_3, GetCache()->GetNameOfProfileAtIndex(
685 GetCache()->GetIndexOfProfileWithPath(path_3)));
686 EXPECT_EQ(name_4, GetCache()->GetNameOfProfileAtIndex(
687 GetCache()->GetIndexOfProfileWithPath(path_4)));
688 EXPECT_EQ(name_5, GetCache()->GetNameOfProfileAtIndex(
689 GetCache()->GetIndexOfProfileWithPath(path_5)));
690}
[email protected]b09bfa12014-08-15 19:05:25691#endif
nomsbe073e52014-08-23 22:01:04692
anthonyvd66aa95d2015-10-02 15:40:26693#if defined(OS_CHROMEOS) || defined(OS_ANDROID) || defined(OS_IOS)
nomsbe073e52014-08-23 22:01:04694TEST_F(ProfileInfoCacheTest,
695 DontMigrateLegacyProfileNamesWithoutNewAvatarMenu) {
nomsbe073e52014-08-23 22:01:04696 EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles());
697
698 base::string16 name_1 = ASCIIToUTF16("Default Profile");
699 base::FilePath path_1 = GetProfilePath("path_1");
700 GetCache()->AddProfileToCache(path_1, name_1,
rogertae22659602015-05-11 21:58:21701 std::string(), base::string16(), 0,
702 std::string());
nomsbe073e52014-08-23 22:01:04703 base::string16 name_2 = ASCIIToUTF16("First user");
704 base::FilePath path_2 = GetProfilePath("path_2");
705 GetCache()->AddProfileToCache(path_2, name_2,
rogertae22659602015-05-11 21:58:21706 std::string(), base::string16(), 1,
707 std::string());
nomsbe073e52014-08-23 22:01:04708 base::string16 name_3 = ASCIIToUTF16("Lemonade");
709 base::FilePath path_3 = GetProfilePath("path_3");
710 GetCache()->AddProfileToCache(path_3, name_3,
rogertae22659602015-05-11 21:58:21711 std::string(), base::string16(), 2,
712 std::string());
nomsbe073e52014-08-23 22:01:04713 base::string16 name_4 = ASCIIToUTF16("Batman");
714 base::FilePath path_4 = GetProfilePath("path_4");
715 GetCache()->AddProfileToCache(path_4, name_4,
rogertae22659602015-05-11 21:58:21716 std::string(), base::string16(), 3,
717 std::string());
nomsbe073e52014-08-23 22:01:04718 EXPECT_EQ(4U, GetCache()->GetNumberOfProfiles());
719
720 ResetCache();
721
722 // Profile names should have been preserved.
723 EXPECT_EQ(name_1, GetCache()->GetNameOfProfileAtIndex(
724 GetCache()->GetIndexOfProfileWithPath(path_1)));
725 EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(
726 GetCache()->GetIndexOfProfileWithPath(path_2)));
727 EXPECT_EQ(name_3, GetCache()->GetNameOfProfileAtIndex(
728 GetCache()->GetIndexOfProfileWithPath(path_3)));
729 EXPECT_EQ(name_4, GetCache()->GetNameOfProfileAtIndex(
730 GetCache()->GetIndexOfProfileWithPath(path_4)));
731}
anthonyvd66aa95d2015-10-02 15:40:26732#endif