blob: 15fd1a7405f1bd171be2f9cb8813965e46ee91de [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
lwchkg3f4491202016-03-30 16:49:5212#include "base/bind.h"
[email protected]5cf8bcd92014-07-17 20:27:4013#include "base/command_line.h"
thestig18dfb7a52014-08-26 10:44:0414#include "base/files/file_util.h"
avib896c712015-12-26 02:10:4315#include "base/macros.h"
[email protected]76fb05c2013-06-11 04:38:0516#include "base/strings/stringprintf.h"
[email protected]e309f312013-06-07 21:50:0817#include "base/strings/utf_string_conversions.h"
[email protected]85503a02014-01-16 17:27:2218#include "base/time/time.h"
avib896c712015-12-26 02:10:4319#include "build/build_config.h"
[email protected]583844c2011-08-27 00:38:3520#include "chrome/browser/browser_process.h"
[email protected]9de44302014-05-01 16:32:2721#include "chrome/browser/profiles/profile_avatar_downloader.h"
[email protected]c3e559772014-04-09 04:02:5422#include "chrome/browser/profiles/profile_avatar_icon_util.h"
[email protected]d4f5d1162011-11-30 01:41:5223#include "chrome/browser/profiles/profile_info_cache.h"
[email protected]6730b1e2011-09-29 05:23:5224#include "chrome/browser/profiles/profile_manager.h"
emaxxa2e11492015-05-12 17:19:1125#include "chrome/common/chrome_paths.h"
[email protected]5cf8bcd92014-07-17 20:27:4026#include "chrome/common/chrome_switches.h"
noms693f35aa2015-02-05 16:03:1627#include "chrome/common/pref_names.h"
[email protected]583844c2011-08-27 00:38:3528#include "chrome/test/base/testing_browser_process.h"
brettwb1fc1b82016-02-02 00:19:0829#include "components/prefs/testing_pref_service.h"
[email protected]5cf8bcd92014-07-17 20:27:4030#include "components/signin/core/common/profile_management_switches.h"
sdefresne875d0782015-09-16 12:01:2831#include "components/syncable_prefs/pref_service_syncable.h"
[email protected]371662e372013-10-17 22:05:2232#include "content/public/test/test_browser_thread_bundle.h"
[email protected]a7fe9112012-07-20 02:34:4533#include "content/public/test/test_utils.h"
[email protected]dee810e2011-06-27 19:43:3934#include "third_party/skia/include/core/SkBitmap.h"
35#include "ui/base/resource/resource_bundle.h"
36#include "ui/gfx/image/image.h"
[email protected]d4f5d1162011-11-30 01:41:5237#include "ui/gfx/image/image_unittest_util.h"
[email protected]dee810e2011-06-27 19:43:3938
[email protected]6778fed2013-12-24 20:09:3739using base::ASCIIToUTF16;
[email protected]f729d7a2013-12-26 07:07:5640using base::UTF8ToUTF16;
[email protected]cb114f142011-11-23 20:18:0441using content::BrowserThread;
42
[email protected]6a460662011-12-22 22:05:1643ProfileNameVerifierObserver::ProfileNameVerifierObserver(
44 TestingProfileManager* testing_profile_manager)
45 : testing_profile_manager_(testing_profile_manager) {
46 DCHECK(testing_profile_manager_);
[email protected]590e189b2011-12-13 22:07:0347}
48
49ProfileNameVerifierObserver::~ProfileNameVerifierObserver() {
50}
51
52void ProfileNameVerifierObserver::OnProfileAdded(
[email protected]650b2d52013-02-10 03:41:4553 const base::FilePath& profile_path) {
[email protected]a04db822013-12-11 19:14:4054 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1655 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0356 EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end());
57 profile_names_.insert(profile_name);
58}
59
[email protected]7b0af152011-12-16 17:02:0660void ProfileNameVerifierObserver::OnProfileWillBeRemoved(
[email protected]650b2d52013-02-10 03:41:4561 const base::FilePath& profile_path) {
[email protected]a04db822013-12-11 19:14:4062 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1663 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0364 EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end());
65 profile_names_.erase(profile_name);
66}
67
[email protected]7b0af152011-12-16 17:02:0668void ProfileNameVerifierObserver::OnProfileWasRemoved(
[email protected]650b2d52013-02-10 03:41:4569 const base::FilePath& profile_path,
[email protected]a04db822013-12-11 19:14:4070 const base::string16& profile_name) {
[email protected]7b0af152011-12-16 17:02:0671 EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end());
72}
73
[email protected]590e189b2011-12-13 22:07:0374void ProfileNameVerifierObserver::OnProfileNameChanged(
[email protected]650b2d52013-02-10 03:41:4575 const base::FilePath& profile_path,
[email protected]a04db822013-12-11 19:14:4076 const base::string16& old_profile_name) {
77 base::string16 new_profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1678 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0379 EXPECT_TRUE(profile_names_.find(old_profile_name) != profile_names_.end());
80 EXPECT_TRUE(profile_names_.find(new_profile_name) == profile_names_.end());
81 profile_names_.erase(old_profile_name);
82 profile_names_.insert(new_profile_name);
83}
84
85void ProfileNameVerifierObserver::OnProfileAvatarChanged(
[email protected]650b2d52013-02-10 03:41:4586 const base::FilePath& profile_path) {
[email protected]a04db822013-12-11 19:14:4087 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1688 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0389 EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end());
90}
91
[email protected]6a460662011-12-22 22:05:1692ProfileInfoCache* ProfileNameVerifierObserver::GetCache() {
93 return testing_profile_manager_->profile_info_cache();
94}
95
[email protected]d4f5d1162011-11-30 01:41:5296ProfileInfoCacheTest::ProfileInfoCacheTest()
[email protected]0aa018a2013-07-31 15:08:5497 : testing_profile_manager_(TestingBrowserProcess::GetGlobal()),
emaxxa2e11492015-05-12 17:19:1198 name_observer_(&testing_profile_manager_),
99 user_data_dir_override_(chrome::DIR_USER_DATA) {
[email protected]d4f5d1162011-11-30 01:41:52100}
101
102ProfileInfoCacheTest::~ProfileInfoCacheTest() {
103}
104
105void ProfileInfoCacheTest::SetUp() {
106 ASSERT_TRUE(testing_profile_manager_.SetUp());
[email protected]590e189b2011-12-13 22:07:03107 testing_profile_manager_.profile_info_cache()->AddObserver(&name_observer_);
[email protected]d4f5d1162011-11-30 01:41:52108}
109
[email protected]1302dcf2011-11-30 21:47:05110void ProfileInfoCacheTest::TearDown() {
111 // Drain the UI thread to make sure all tasks are completed. This prevents
112 // memory leaks.
[email protected]0aa018a2013-07-31 15:08:54113 base::RunLoop().RunUntilIdle();
[email protected]1302dcf2011-11-30 21:47:05114}
115
[email protected]d4f5d1162011-11-30 01:41:52116ProfileInfoCache* ProfileInfoCacheTest::GetCache() {
117 return testing_profile_manager_.profile_info_cache();
118}
119
[email protected]650b2d52013-02-10 03:41:45120base::FilePath ProfileInfoCacheTest::GetProfilePath(
[email protected]d4f5d1162011-11-30 01:41:52121 const std::string& base_name) {
122 return testing_profile_manager_.profile_manager()->user_data_dir().
123 AppendASCII(base_name);
124}
125
126void ProfileInfoCacheTest::ResetCache() {
127 testing_profile_manager_.DeleteProfileInfoCache();
128}
129
[email protected]d4f5d1162011-11-30 01:41:52130TEST_F(ProfileInfoCacheTest, AddProfiles) {
[email protected]6730b1e2011-09-29 05:23:52131 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39132
[email protected]ca591072012-03-27 01:54:44133 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
avib896c712015-12-26 02:10:43134 for (uint32_t i = 0; i < 4; ++i) {
[email protected]7d3cbc92013-03-18 22:33:04135 base::FilePath profile_path =
136 GetProfilePath(base::StringPrintf("path_%ud", i));
[email protected]a04db822013-12-11 19:14:40137 base::string16 profile_name =
138 ASCIIToUTF16(base::StringPrintf("name_%ud", i));
[email protected]ca591072012-03-27 01:54:44139 const SkBitmap* icon = rb.GetImageNamed(
[email protected]c3e559772014-04-09 04:02:54140 profiles::GetDefaultAvatarIconResourceIDAtIndex(
[email protected]ca591072012-03-27 01:54:44141 i)).ToSkBitmap();
[email protected]d20d0432014-06-12 17:14:05142 std::string supervised_user_id = i == 3 ? "TEST_ID" : "";
[email protected]dee810e2011-06-27 19:43:39143
rogertae22659602015-05-11 21:58:21144 GetCache()->AddProfileToCache(profile_path, profile_name, std::string(),
145 base::string16(), i, supervised_user_id);
[email protected]cb114f142011-11-23 20:18:04146 GetCache()->SetBackgroundStatusOfProfileAtIndex(i, true);
[email protected]a04db822013-12-11 19:14:40147 base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
[email protected]cb114f142011-11-23 20:18:04148 GetCache()->SetGAIANameOfProfileAtIndex(i, gaia_name);
[email protected]dee810e2011-06-27 19:43:39149
[email protected]6730b1e2011-09-29 05:23:52150 EXPECT_EQ(i + 1, GetCache()->GetNumberOfProfiles());
151 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
152 EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i));
[email protected]0aa018a2013-07-31 15:08:54153 const SkBitmap* actual_icon =
154 GetCache()->GetAvatarIconOfProfileAtIndex(i).ToSkBitmap();
[email protected]ca591072012-03-27 01:54:44155 EXPECT_EQ(icon->width(), actual_icon->width());
156 EXPECT_EQ(icon->height(), actual_icon->height());
[email protected]d20d0432014-06-12 17:14:05157 EXPECT_EQ(i == 3, GetCache()->ProfileIsSupervisedAtIndex(i));
[email protected]5ddfade2014-02-03 10:24:53158 EXPECT_EQ(i == 3, GetCache()->IsOmittedProfileAtIndex(i));
[email protected]d20d0432014-06-12 17:14:05159 EXPECT_EQ(supervised_user_id,
160 GetCache()->GetSupervisedUserIdOfProfileAtIndex(i));
[email protected]dee810e2011-06-27 19:43:39161 }
[email protected]cb114f142011-11-23 20:18:04162
163 // Reset the cache and test the it reloads correctly.
164 ResetCache();
165
166 EXPECT_EQ(4u, GetCache()->GetNumberOfProfiles());
avib896c712015-12-26 02:10:43167 for (uint32_t i = 0; i < 4; ++i) {
[email protected]7d3cbc92013-03-18 22:33:04168 base::FilePath profile_path =
169 GetProfilePath(base::StringPrintf("path_%ud", i));
[email protected]cb114f142011-11-23 20:18:04170 EXPECT_EQ(i, GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]a04db822013-12-11 19:14:40171 base::string16 profile_name =
172 ASCIIToUTF16(base::StringPrintf("name_%ud", i));
[email protected]cb114f142011-11-23 20:18:04173 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
174 EXPECT_EQ(i, GetCache()->GetAvatarIconIndexOfProfileAtIndex(i));
175 EXPECT_EQ(true, GetCache()->GetBackgroundStatusOfProfileAtIndex(i));
[email protected]a04db822013-12-11 19:14:40176 base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
[email protected]cb114f142011-11-23 20:18:04177 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(i));
178 }
[email protected]dee810e2011-06-27 19:43:39179}
180
[email protected]d4f5d1162011-11-30 01:41:52181TEST_F(ProfileInfoCacheTest, DeleteProfile) {
[email protected]6730b1e2011-09-29 05:23:52182 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39183
[email protected]650b2d52013-02-10 03:41:45184 base::FilePath path_1 = GetProfilePath("path_1");
[email protected]a04db822013-12-11 19:14:40185 GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("name_1"),
rogertae22659602015-05-11 21:58:21186 std::string(), base::string16(), 0,
187 std::string());
[email protected]6730b1e2011-09-29 05:23:52188 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39189
[email protected]650b2d52013-02-10 03:41:45190 base::FilePath path_2 = GetProfilePath("path_2");
[email protected]a04db822013-12-11 19:14:40191 base::string16 name_2 = ASCIIToUTF16("name_2");
rogertae22659602015-05-11 21:58:21192 GetCache()->AddProfileToCache(path_2, name_2, std::string(), base::string16(),
193 0, std::string());
[email protected]6730b1e2011-09-29 05:23:52194 EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39195
[email protected]6730b1e2011-09-29 05:23:52196 GetCache()->DeleteProfileFromCache(path_1);
197 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
198 EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(0));
[email protected]dee810e2011-06-27 19:43:39199
[email protected]6730b1e2011-09-29 05:23:52200 GetCache()->DeleteProfileFromCache(path_2);
201 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39202}
203
[email protected]d4f5d1162011-11-30 01:41:52204TEST_F(ProfileInfoCacheTest, MutateProfile) {
205 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21206 GetProfilePath("path_1"), ASCIIToUTF16("name_1"), std::string(),
[email protected]a04db822013-12-11 19:14:40207 base::string16(), 0, std::string());
[email protected]d4f5d1162011-11-30 01:41:52208 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21209 GetProfilePath("path_2"), ASCIIToUTF16("name_2"), std::string(),
[email protected]a04db822013-12-11 19:14:40210 base::string16(), 0, std::string());
[email protected]dee810e2011-06-27 19:43:39211
[email protected]a04db822013-12-11 19:14:40212 base::string16 new_name = ASCIIToUTF16("new_name");
[email protected]6730b1e2011-09-29 05:23:52213 GetCache()->SetNameOfProfileAtIndex(1, new_name);
214 EXPECT_EQ(new_name, GetCache()->GetNameOfProfileAtIndex(1));
215 EXPECT_NE(new_name, GetCache()->GetNameOfProfileAtIndex(0));
[email protected]dee810e2011-06-27 19:43:39216
[email protected]a04db822013-12-11 19:14:40217 base::string16 new_user_name = ASCIIToUTF16("user_name");
rogertae22659602015-05-11 21:58:21218 std::string new_gaia_id = "12345";
219 GetCache()->SetAuthInfoOfProfileAtIndex(1, new_gaia_id, new_user_name);
[email protected]e8e78092011-09-29 18:15:38220 EXPECT_EQ(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(1));
rogertae22659602015-05-11 21:58:21221 EXPECT_EQ(new_gaia_id, GetCache()->GetGAIAIdOfProfileAtIndex(1));
[email protected]e8e78092011-09-29 18:15:38222 EXPECT_NE(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(0));
223
[email protected]dee810e2011-06-27 19:43:39224 size_t new_icon_index = 3;
[email protected]6730b1e2011-09-29 05:23:52225 GetCache()->SetAvatarIconOfProfileAtIndex(1, new_icon_index);
[email protected]dee810e2011-06-27 19:43:39226 // Not much to test.
[email protected]6730b1e2011-09-29 05:23:52227 GetCache()->GetAvatarIconOfProfileAtIndex(1);
[email protected]dee810e2011-06-27 19:43:39228}
229
[email protected]d4f5d1162011-11-30 01:41:52230TEST_F(ProfileInfoCacheTest, Sort) {
[email protected]a04db822013-12-11 19:14:40231 base::string16 name_a = ASCIIToUTF16("apple");
[email protected]d4f5d1162011-11-30 01:41:52232 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21233 GetProfilePath("path_a"), name_a, std::string(), base::string16(), 0,
234 std::string());
[email protected]cb114f142011-11-23 20:18:04235
[email protected]a04db822013-12-11 19:14:40236 base::string16 name_c = ASCIIToUTF16("cat");
[email protected]d4f5d1162011-11-30 01:41:52237 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21238 GetProfilePath("path_c"), name_c, std::string(), base::string16(), 0,
239 std::string());
[email protected]cb114f142011-11-23 20:18:04240
241 // Sanity check the initial order.
242 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
243 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
244
245 // Add a new profile (start with a capital to test case insensitive sorting.
[email protected]a04db822013-12-11 19:14:40246 base::string16 name_b = ASCIIToUTF16("Banana");
[email protected]d4f5d1162011-11-30 01:41:52247 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21248 GetProfilePath("path_b"), name_b, std::string(), base::string16(), 0,
249 std::string());
[email protected]cb114f142011-11-23 20:18:04250
251 // Verify the new order.
252 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
253 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(1));
254 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(2));
255
256 // Change the name of an existing profile.
257 name_a = UTF8ToUTF16("dog");
258 GetCache()->SetNameOfProfileAtIndex(0, name_a);
259
260 // Verify the new order.
261 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
262 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
263 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(2));
264
265 // Delete a profile.
[email protected]d4f5d1162011-11-30 01:41:52266 GetCache()->DeleteProfileFromCache(GetProfilePath("path_c"));
[email protected]cb114f142011-11-23 20:18:04267
268 // Verify the new order.
269 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
270 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(1));
271}
272
[email protected]d4f5d1162011-11-30 01:41:52273TEST_F(ProfileInfoCacheTest, BackgroundModeStatus) {
[email protected]279170832011-10-12 23:38:03274 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13275 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
rogertae22659602015-05-11 21:58:21276 std::string(), base::string16(), 0, std::string());
[email protected]279170832011-10-12 23:38:03277 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13278 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
rogertae22659602015-05-11 21:58:21279 std::string(), base::string16(), 0, std::string());
[email protected]279170832011-10-12 23:38:03280
281 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
282 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
283
284 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, true);
285
286 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
287 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
288
289 GetCache()->SetBackgroundStatusOfProfileAtIndex(0, true);
290
291 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
292 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
293
294 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, false);
295
296 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
297 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
298}
299
[email protected]85503a02014-01-16 17:27:22300TEST_F(ProfileInfoCacheTest, ProfileActiveTime) {
301 GetCache()->AddProfileToCache(
302 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
rogertae22659602015-05-11 21:58:21303 std::string(), base::string16(), 0, std::string());
[email protected]85503a02014-01-16 17:27:22304 EXPECT_EQ(base::Time(), GetCache()->GetProfileActiveTimeAtIndex(0));
[email protected]87727e02014-02-27 18:16:53305 // Before & After times are artificially shifted because just relying upon
306 // the system time can yield problems due to inaccuracies in the
307 // underlying storage system (which uses a double with only 52 bits of
308 // precision to store the 64-bit "time" number). http://crbug.com/346827
[email protected]85503a02014-01-16 17:27:22309 base::Time before = base::Time::Now();
[email protected]87727e02014-02-27 18:16:53310 before -= base::TimeDelta::FromSeconds(1);
[email protected]85503a02014-01-16 17:27:22311 GetCache()->SetProfileActiveTimeAtIndex(0);
312 base::Time after = base::Time::Now();
[email protected]87727e02014-02-27 18:16:53313 after += base::TimeDelta::FromSeconds(1);
[email protected]85503a02014-01-16 17:27:22314 EXPECT_LE(before, GetCache()->GetProfileActiveTimeAtIndex(0));
315 EXPECT_GE(after, GetCache()->GetProfileActiveTimeAtIndex(0));
316}
317
[email protected]d4f5d1162011-11-30 01:41:52318TEST_F(ProfileInfoCacheTest, GAIAName) {
[email protected]cb114f142011-11-23 20:18:04319 GetCache()->AddProfileToCache(
[email protected]a4352d02014-03-06 06:00:27320 GetProfilePath("path_1"), ASCIIToUTF16("Person 1"),
rogertae22659602015-05-11 21:58:21321 std::string(), base::string16(), 0, std::string());
[email protected]a4352d02014-03-06 06:00:27322 base::string16 profile_name(ASCIIToUTF16("Person 2"));
[email protected]cb114f142011-11-23 20:18:04323 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21324 GetProfilePath("path_2"), profile_name, std::string(), base::string16(),
325 0, std::string());
[email protected]cb114f142011-11-23 20:18:04326
[email protected]a4352d02014-03-06 06:00:27327 int index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
328 int index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
329
[email protected]cb114f142011-11-23 20:18:04330 // Sanity check.
[email protected]a4352d02014-03-06 06:00:27331 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty());
332 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index2).empty());
[email protected]cb114f142011-11-23 20:18:04333
[email protected]425d6ab2014-03-25 05:59:39334 // Set GAIA name. This re-sorts the cache.
[email protected]a04db822013-12-11 19:14:40335 base::string16 gaia_name(ASCIIToUTF16("Pat Smith"));
[email protected]a4352d02014-03-06 06:00:27336 GetCache()->SetGAIANameOfProfileAtIndex(index2, gaia_name);
[email protected]425d6ab2014-03-25 05:59:39337 index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
338 index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
339
340 // Since there is a GAIA name, we use that as a display name.
[email protected]a4352d02014-03-06 06:00:27341 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty());
342 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2));
[email protected]a4352d02014-03-06 06:00:27343 EXPECT_EQ(gaia_name, GetCache()->GetNameOfProfileAtIndex(index2));
[email protected]cb114f142011-11-23 20:18:04344
[email protected]a4352d02014-03-06 06:00:27345 // Don't use GAIA name as profile name. This re-sorts the cache.
[email protected]425d6ab2014-03-25 05:59:39346 base::string16 custom_name(ASCIIToUTF16("Custom name"));
347 GetCache()->SetNameOfProfileAtIndex(index2, custom_name);
[email protected]1cdf0932014-08-16 06:00:49348 GetCache()->SetProfileIsUsingDefaultNameAtIndex(index2, false);
349
[email protected]a4352d02014-03-06 06:00:27350 index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
351 index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
352
[email protected]425d6ab2014-03-25 05:59:39353 EXPECT_EQ(custom_name, GetCache()->GetNameOfProfileAtIndex(index2));
[email protected]a4352d02014-03-06 06:00:27354 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2));
[email protected]cb114f142011-11-23 20:18:04355}
356
[email protected]d4f5d1162011-11-30 01:41:52357TEST_F(ProfileInfoCacheTest, GAIAPicture) {
[email protected]22900902014-07-30 11:50:41358 const int kDefaultAvatarIndex = 0;
359 const int kOtherAvatarIndex = 1;
[email protected]979dc4f2014-08-12 22:52:11360 const int kGaiaPictureSize = 256; // Standard size of a Gaia account picture.
[email protected]cb114f142011-11-23 20:18:04361 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13362 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
rogertae22659602015-05-11 21:58:21363 std::string(), base::string16(), kDefaultAvatarIndex, std::string());
[email protected]cb114f142011-11-23 20:18:04364 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13365 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
rogertae22659602015-05-11 21:58:21366 std::string(), base::string16(), kDefaultAvatarIndex, std::string());
[email protected]cb114f142011-11-23 20:18:04367
368 // Sanity check.
[email protected]2f3c00f2011-11-30 04:36:22369 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
370 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(1));
[email protected]cb114f142011-11-23 20:18:04371 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0));
372 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
373
374 // The profile icon should be the default one.
[email protected]22900902014-07-30 11:50:41375 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(0));
376 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
377 int default_avatar_id =
378 profiles::GetDefaultAvatarIconResourceIDAtIndex(kDefaultAvatarIndex);
379 const gfx::Image& default_avatar_image(
380 ResourceBundle::GetSharedInstance().GetImageNamed(default_avatar_id));
pkotwiczb312b512015-09-29 23:44:43381 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]22900902014-07-30 11:50:41382 default_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
[email protected]cb114f142011-11-23 20:18:04383
384 // Set GAIA picture.
[email protected]979dc4f2014-08-12 22:52:11385 gfx::Image gaia_image(gfx::test::CreateImage(
386 kGaiaPictureSize, kGaiaPictureSize));
[email protected]2f3c00f2011-11-30 04:36:22387 GetCache()->SetGAIAPictureOfProfileAtIndex(1, &gaia_image);
388 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
pkotwiczb312b512015-09-29 23:44:43389 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22390 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
[email protected]22900902014-07-30 11:50:41391 // Since we're still using the default avatar, the GAIA image should be
392 // preferred over the generic avatar image.
393 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
394 EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
pkotwiczb312b512015-09-29 23:44:43395 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]22900902014-07-30 11:50:41396 gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
[email protected]cb114f142011-11-23 20:18:04397
[email protected]1cdf0932014-08-16 06:00:49398 // Set a non-default avatar. This should be preferred over the GAIA image.
[email protected]22900902014-07-30 11:50:41399 GetCache()->SetAvatarIconOfProfileAtIndex(1, kOtherAvatarIndex);
[email protected]1cdf0932014-08-16 06:00:49400 GetCache()->SetProfileIsUsingDefaultAvatarAtIndex(1, false);
[email protected]22900902014-07-30 11:50:41401 EXPECT_FALSE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
402 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
403 int other_avatar_id =
404 profiles::GetDefaultAvatarIconResourceIDAtIndex(kOtherAvatarIndex);
405 const gfx::Image& other_avatar_image(
406 ResourceBundle::GetSharedInstance().GetImageNamed(other_avatar_id));
pkotwiczb312b512015-09-29 23:44:43407 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]22900902014-07-30 11:50:41408 other_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
409
410 // Explicitly setting the GAIA picture should make it preferred again.
[email protected]cb114f142011-11-23 20:18:04411 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, true);
[email protected]22900902014-07-30 11:50:41412 EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
pkotwiczb312b512015-09-29 23:44:43413 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22414 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
pkotwiczb312b512015-09-29 23:44:43415 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]cb114f142011-11-23 20:18:04416 gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
417
[email protected]22900902014-07-30 11:50:41418 // Clearing the IsUsingGAIAPicture flag should result in the generic image
419 // being used again.
[email protected]cb114f142011-11-23 20:18:04420 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, false);
[email protected]22900902014-07-30 11:50:41421 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
pkotwiczb312b512015-09-29 23:44:43422 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22423 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
pkotwiczb312b512015-09-29 23:44:43424 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]22900902014-07-30 11:50:41425 other_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
[email protected]cb114f142011-11-23 20:18:04426}
427
[email protected]754bebc2011-12-01 16:42:16428TEST_F(ProfileInfoCacheTest, PersistGAIAPicture) {
[email protected]cb114f142011-11-23 20:18:04429 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13430 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
rogertae22659602015-05-11 21:58:21431 std::string(), base::string16(), 0, std::string());
[email protected]d4f5d1162011-11-30 01:41:52432 gfx::Image gaia_image(gfx::test::CreateImage());
[email protected]cb114f142011-11-23 20:18:04433
[email protected]2f3c00f2011-11-30 04:36:22434 GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_image);
noms75ca0192015-02-26 03:14:30435
436 // Make sure everything has completed, and the file has been written to disk.
437 base::RunLoop().RunUntilIdle();
438
pkotwiczb312b512015-09-29 23:44:43439 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22440 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
[email protected]cb114f142011-11-23 20:18:04441
[email protected]cb114f142011-11-23 20:18:04442 ResetCache();
[email protected]cb114f142011-11-23 20:18:04443 // Try to get the GAIA picture. This should return NULL until the read from
444 // disk is done.
[email protected]2f3c00f2011-11-30 04:36:22445 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
noms75ca0192015-02-26 03:14:30446 base::RunLoop().RunUntilIdle();
447
pkotwiczb312b512015-09-29 23:44:43448 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22449 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
450}
451
[email protected]d20d0432014-06-12 17:14:05452TEST_F(ProfileInfoCacheTest, SetSupervisedUserId) {
[email protected]0aa018a2013-07-31 15:08:54453 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13454 GetProfilePath("test"), ASCIIToUTF16("Test"),
rogertae22659602015-05-11 21:58:21455 std::string(), base::string16(), 0, std::string());
[email protected]d20d0432014-06-12 17:14:05456 EXPECT_FALSE(GetCache()->ProfileIsSupervisedAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54457
[email protected]d20d0432014-06-12 17:14:05458 GetCache()->SetSupervisedUserIdOfProfileAtIndex(0, "TEST_ID");
459 EXPECT_TRUE(GetCache()->ProfileIsSupervisedAtIndex(0));
460 EXPECT_EQ("TEST_ID", GetCache()->GetSupervisedUserIdOfProfileAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54461
462 ResetCache();
[email protected]d20d0432014-06-12 17:14:05463 EXPECT_TRUE(GetCache()->ProfileIsSupervisedAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54464
[email protected]d20d0432014-06-12 17:14:05465 GetCache()->SetSupervisedUserIdOfProfileAtIndex(0, std::string());
466 EXPECT_FALSE(GetCache()->ProfileIsSupervisedAtIndex(0));
467 EXPECT_EQ("", GetCache()->GetSupervisedUserIdOfProfileAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54468}
469
[email protected]2f3c00f2011-11-30 04:36:22470TEST_F(ProfileInfoCacheTest, EmptyGAIAInfo) {
[email protected]a04db822013-12-11 19:14:40471 base::string16 profile_name = ASCIIToUTF16("name_1");
[email protected]c3e559772014-04-09 04:02:54472 int id = profiles::GetDefaultAvatarIconResourceIDAtIndex(0);
[email protected]2f3c00f2011-11-30 04:36:22473 const gfx::Image& profile_image(
474 ResourceBundle::GetSharedInstance().GetImageNamed(id));
475
476 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21477 GetProfilePath("path_1"), profile_name, std::string(), base::string16(),
478 0, std::string());
[email protected]2f3c00f2011-11-30 04:36:22479
480 // Set empty GAIA info.
[email protected]a04db822013-12-11 19:14:40481 GetCache()->SetGAIANameOfProfileAtIndex(0, base::string16());
[email protected]2f3c00f2011-11-30 04:36:22482 GetCache()->SetGAIAPictureOfProfileAtIndex(0, NULL);
[email protected]2f3c00f2011-11-30 04:36:22483 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(0, true);
484
485 // Verify that the profile name and picture are not empty.
486 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(0));
pkotwiczb312b512015-09-29 23:44:43487 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22488 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(0)));
[email protected]cb114f142011-11-23 20:18:04489}
490
[email protected]d20d0432014-06-12 17:14:05491TEST_F(ProfileInfoCacheTest, CreateSupervisedTestingProfile) {
[email protected]0aa018a2013-07-31 15:08:54492 testing_profile_manager_.CreateTestingProfile("default");
[email protected]d20d0432014-06-12 17:14:05493 base::string16 supervised_user_name = ASCIIToUTF16("Supervised User");
[email protected]0aa018a2013-07-31 15:08:54494 testing_profile_manager_.CreateTestingProfile(
dcheng6e25ed32016-04-08 03:30:03495 "test1", std::unique_ptr<syncable_prefs::PrefServiceSyncable>(),
[email protected]d20d0432014-06-12 17:14:05496 supervised_user_name, 0, "TEST_ID", TestingProfile::TestingFactories());
[email protected]0aa018a2013-07-31 15:08:54497 for (size_t i = 0; i < GetCache()->GetNumberOfProfiles(); i++) {
[email protected]d20d0432014-06-12 17:14:05498 bool is_supervised =
499 GetCache()->GetNameOfProfileAtIndex(i) == supervised_user_name;
500 EXPECT_EQ(is_supervised, GetCache()->ProfileIsSupervisedAtIndex(i));
501 std::string supervised_user_id = is_supervised ? "TEST_ID" : "";
502 EXPECT_EQ(supervised_user_id,
503 GetCache()->GetSupervisedUserIdOfProfileAtIndex(i));
[email protected]0aa018a2013-07-31 15:08:54504 }
[email protected]371662e372013-10-17 22:05:22505
[email protected]d20d0432014-06-12 17:14:05506 // Supervised profiles have a custom theme, which needs to be deleted on the
507 // FILE thread. Reset the profile manager now so everything is deleted while
508 // we still have a FILE thread.
[email protected]371662e372013-10-17 22:05:22509 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
[email protected]0aa018a2013-07-31 15:08:54510}
511
[email protected]047481802013-09-16 22:26:38512TEST_F(ProfileInfoCacheTest, AddStubProfile) {
513 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
514
515 // Add some profiles with and without a '.' in their paths.
516 const struct {
517 const char* profile_path;
518 const char* profile_name;
519 } kTestCases[] = {
520 { "path.test0", "name_0" },
521 { "path_test1", "name_1" },
522 { "path.test2", "name_2" },
523 { "path_test3", "name_3" },
524 };
525
viettrungluu9e65ad12014-10-16 04:22:26526 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
[email protected]047481802013-09-16 22:26:38527 base::FilePath profile_path = GetProfilePath(kTestCases[i].profile_path);
[email protected]a04db822013-12-11 19:14:40528 base::string16 profile_name = ASCIIToUTF16(kTestCases[i].profile_name);
[email protected]047481802013-09-16 22:26:38529
rogertae22659602015-05-11 21:58:21530 GetCache()->AddProfileToCache(profile_path, profile_name, std::string(),
531 base::string16(), i, "");
[email protected]047481802013-09-16 22:26:38532
533 EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i));
534 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
535 }
536
537 ASSERT_EQ(4U, GetCache()->GetNumberOfProfiles());
538
539 // Check that the profiles can be extracted from the local state.
noms693f35aa2015-02-05 16:03:16540 std::vector<base::string16> names;
541 PrefService* local_state = g_browser_process->local_state();
542 const base::DictionaryValue* cache = local_state->GetDictionary(
543 prefs::kProfileInfoCache);
544 base::string16 name;
545 for (base::DictionaryValue::Iterator it(*cache); !it.IsAtEnd();
546 it.Advance()) {
547 const base::DictionaryValue* info = NULL;
548 it.value().GetAsDictionary(&info);
549 info->GetString("name", &name);
550 names.push_back(name);
551 }
552
[email protected]047481802013-09-16 22:26:38553 for (size_t i = 0; i < 4; i++)
554 ASSERT_FALSE(names[i].empty());
555}
556
[email protected]b09bfa12014-08-15 19:05:25557// High res avatar downloading is only supported on desktop.
jam1c5a91492016-02-24 20:47:53558#if !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
[email protected]9de44302014-05-01 16:32:27559TEST_F(ProfileInfoCacheTest, DownloadHighResAvatarTest) {
mlerman3ab32772015-04-08 14:48:56560 // The TestingProfileManager's ProfileInfoCache doesn't download avatars.
561 ProfileInfoCache profile_info_cache(g_browser_process->local_state(),
562 testing_profile_manager_.profile_manager()->user_data_dir());
563
emaxxa2e11492015-05-12 17:19:11564 // Make sure there are no avatars already on disk.
mlerman3ab32772015-04-08 14:48:56565 const size_t kIconIndex = 0;
566 base::FilePath icon_path =
567 profiles::GetPathOfHighResAvatarAtIndex(kIconIndex);
568 EXPECT_FALSE(base::PathExists(icon_path));
569
570 EXPECT_EQ(0U, profile_info_cache.GetNumberOfProfiles());
[email protected]9de44302014-05-01 16:32:27571 base::FilePath path_1 = GetProfilePath("path_1");
mlerman3ab32772015-04-08 14:48:56572 profile_info_cache.AddProfileToCache(path_1, ASCIIToUTF16("name_1"),
rogertae22659602015-05-11 21:58:21573 std::string(), base::string16(), kIconIndex, std::string());
mlerman3ab32772015-04-08 14:48:56574 EXPECT_EQ(1U, profile_info_cache.GetNumberOfProfiles());
noms28693ab22015-01-28 21:44:54575 base::RunLoop().RunUntilIdle();
[email protected]9de44302014-05-01 16:32:27576
577 // We haven't downloaded any high-res avatars yet.
mlerman3ab32772015-04-08 14:48:56578 EXPECT_EQ(0U, profile_info_cache.cached_avatar_images_.size());
[email protected]5cf8bcd92014-07-17 20:27:40579
580 // After adding a new profile, the download of high-res avatar will be
581 // triggered if the flag kNewAvatarMenu has been set. But the downloader
582 // won't ever call OnFetchComplete in the test.
mlerman3ab32772015-04-08 14:48:56583 EXPECT_EQ(1U, profile_info_cache.avatar_images_downloads_in_progress_.size());
[email protected]5cf8bcd92014-07-17 20:27:40584
mlerman3ab32772015-04-08 14:48:56585 EXPECT_FALSE(profile_info_cache.GetHighResAvatarOfProfileAtIndex(0));
[email protected]9de44302014-05-01 16:32:27586
587 // Simulate downloading a high-res avatar.
[email protected]2d605712014-05-14 12:03:07588 ProfileAvatarDownloader avatar_downloader(
lwchkg3f4491202016-03-30 16:49:52589 kIconIndex,
590 base::Bind(&ProfileInfoCache::SaveAvatarImageAtPath,
591 base::Unretained(&profile_info_cache),
592 profile_info_cache.GetPathOfProfileAtIndex(0)));
[email protected]9de44302014-05-01 16:32:27593
594 // Put a real bitmap into "bitmap". 2x2 bitmap of green 32 bit pixels.
595 SkBitmap bitmap;
[email protected]e61cad52014-07-01 21:01:57596 bitmap.allocN32Pixels(2, 2);
[email protected]9de44302014-05-01 16:32:27597 bitmap.eraseColor(SK_ColorGREEN);
598
599 avatar_downloader.OnFetchComplete(
600 GURL("http://www.google.com/avatar.png"), &bitmap);
601
treibaf255502015-01-30 17:47:53602 // Now the download should not be in progress anymore.
mlerman3ab32772015-04-08 14:48:56603 EXPECT_EQ(0U, profile_info_cache.avatar_images_downloads_in_progress_.size());
treibaf255502015-01-30 17:47:53604
[email protected]9de44302014-05-01 16:32:27605 std::string file_name =
606 profiles::GetDefaultAvatarIconFileNameAtIndex(kIconIndex);
607
608 // The file should have been cached and saved.
mlerman3ab32772015-04-08 14:48:56609 EXPECT_EQ(1U, profile_info_cache.cached_avatar_images_.size());
610 EXPECT_TRUE(profile_info_cache.GetHighResAvatarOfProfileAtIndex(0));
lwchkgb17d86c2016-05-12 11:16:51611 EXPECT_EQ(profile_info_cache.cached_avatar_images_[file_name].get(),
mlerman3ab32772015-04-08 14:48:56612 profile_info_cache.GetHighResAvatarOfProfileAtIndex(0));
[email protected]9de44302014-05-01 16:32:27613
614 // Make sure everything has completed, and the file has been written to disk.
615 base::RunLoop().RunUntilIdle();
616
617 // Clean up.
[email protected]9de44302014-05-01 16:32:27618 EXPECT_NE(std::string::npos, icon_path.MaybeAsASCII().find(file_name));
619 EXPECT_TRUE(base::PathExists(icon_path));
thestig16a4bcae2015-12-07 08:36:17620 EXPECT_TRUE(base::DeleteFile(icon_path, false));
[email protected]9de44302014-05-01 16:32:27621 EXPECT_FALSE(base::PathExists(icon_path));
622}
nomsbe073e52014-08-23 22:01:04623
emaxx0cb1957132015-05-18 20:22:54624TEST_F(ProfileInfoCacheTest, NothingToDownloadHighResAvatarTest) {
emaxx0cb1957132015-05-18 20:22:54625 // The TestingProfileManager's ProfileInfoCache doesn't download avatars.
626 ProfileInfoCache profile_info_cache(
627 g_browser_process->local_state(),
628 testing_profile_manager_.profile_manager()->user_data_dir());
629
630 const size_t kIconIndex = profiles::GetPlaceholderAvatarIndex();
631
632 EXPECT_EQ(0U, profile_info_cache.GetNumberOfProfiles());
633 base::FilePath path_1 = GetProfilePath("path_1");
634 profile_info_cache.AddProfileToCache(path_1, ASCIIToUTF16("name_1"),
635 std::string(), base::string16(),
636 kIconIndex, std::string());
637 EXPECT_EQ(1U, profile_info_cache.GetNumberOfProfiles());
638 base::RunLoop().RunUntilIdle();
639
640 // We haven't tried to download any high-res avatars as the specified icon is
641 // just a placeholder.
642 EXPECT_EQ(0U, profile_info_cache.cached_avatar_images_.size());
643 EXPECT_EQ(0U, profile_info_cache.avatar_images_downloads_in_progress_.size());
644}
645
nomsbe073e52014-08-23 22:01:04646TEST_F(ProfileInfoCacheTest, MigrateLegacyProfileNamesWithNewAvatarMenu) {
nomsbe073e52014-08-23 22:01:04647 EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles());
648
649 base::FilePath path_1 = GetProfilePath("path_1");
650 GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("Default Profile"),
rogertae22659602015-05-11 21:58:21651 std::string(), base::string16(), 0,
652 std::string());
nomsbe073e52014-08-23 22:01:04653 base::FilePath path_2 = GetProfilePath("path_2");
654 GetCache()->AddProfileToCache(path_2, ASCIIToUTF16("First user"),
rogertae22659602015-05-11 21:58:21655 std::string(), base::string16(), 1,
656 std::string());
nomsbe073e52014-08-23 22:01:04657 base::string16 name_3 = ASCIIToUTF16("Lemonade");
658 base::FilePath path_3 = GetProfilePath("path_3");
659 GetCache()->AddProfileToCache(path_3, name_3,
rogertae22659602015-05-11 21:58:21660 std::string(), base::string16(), 2,
661 std::string());
nomsbe073e52014-08-23 22:01:04662 base::string16 name_4 = ASCIIToUTF16("Batman");
663 base::FilePath path_4 = GetProfilePath("path_4");
664 GetCache()->AddProfileToCache(path_4, name_4,
rogertae22659602015-05-11 21:58:21665 std::string(), base::string16(), 3,
666 std::string());
nomsbe073e52014-08-23 22:01:04667 base::string16 name_5 = ASCIIToUTF16("Person 2");
668 base::FilePath path_5 = GetProfilePath("path_5");
669 GetCache()->AddProfileToCache(path_5, name_5,
rogertae22659602015-05-11 21:58:21670 std::string(), base::string16(), 2,
671 std::string());
nomsbe073e52014-08-23 22:01:04672
673 EXPECT_EQ(5U, GetCache()->GetNumberOfProfiles());
674
675
676 ResetCache();
677
678 // Legacy profile names like "Default Profile" and "First user" should be
679 // migrated to "Person %n" type names.
680 EXPECT_EQ(ASCIIToUTF16("Person 1"), GetCache()->GetNameOfProfileAtIndex(
681 GetCache()->GetIndexOfProfileWithPath(path_1)));
682 EXPECT_EQ(ASCIIToUTF16("Person 3"), GetCache()->GetNameOfProfileAtIndex(
683 GetCache()->GetIndexOfProfileWithPath(path_2)));
684
685 // Other profile names should not be migrated even if they're the old
686 // default cartoon profile names.
687 EXPECT_EQ(name_3, GetCache()->GetNameOfProfileAtIndex(
688 GetCache()->GetIndexOfProfileWithPath(path_3)));
689 EXPECT_EQ(name_4, GetCache()->GetNameOfProfileAtIndex(
690 GetCache()->GetIndexOfProfileWithPath(path_4)));
691 EXPECT_EQ(name_5, GetCache()->GetNameOfProfileAtIndex(
692 GetCache()->GetIndexOfProfileWithPath(path_5)));
693}
[email protected]b09bfa12014-08-15 19:05:25694#endif
nomsbe073e52014-08-23 22:01:04695
jam1c5a91492016-02-24 20:47:53696#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
nomsbe073e52014-08-23 22:01:04697TEST_F(ProfileInfoCacheTest,
698 DontMigrateLegacyProfileNamesWithoutNewAvatarMenu) {
nomsbe073e52014-08-23 22:01:04699 EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles());
700
701 base::string16 name_1 = ASCIIToUTF16("Default Profile");
702 base::FilePath path_1 = GetProfilePath("path_1");
703 GetCache()->AddProfileToCache(path_1, name_1,
rogertae22659602015-05-11 21:58:21704 std::string(), base::string16(), 0,
705 std::string());
nomsbe073e52014-08-23 22:01:04706 base::string16 name_2 = ASCIIToUTF16("First user");
707 base::FilePath path_2 = GetProfilePath("path_2");
708 GetCache()->AddProfileToCache(path_2, name_2,
rogertae22659602015-05-11 21:58:21709 std::string(), base::string16(), 1,
710 std::string());
nomsbe073e52014-08-23 22:01:04711 base::string16 name_3 = ASCIIToUTF16("Lemonade");
712 base::FilePath path_3 = GetProfilePath("path_3");
713 GetCache()->AddProfileToCache(path_3, name_3,
rogertae22659602015-05-11 21:58:21714 std::string(), base::string16(), 2,
715 std::string());
nomsbe073e52014-08-23 22:01:04716 base::string16 name_4 = ASCIIToUTF16("Batman");
717 base::FilePath path_4 = GetProfilePath("path_4");
718 GetCache()->AddProfileToCache(path_4, name_4,
rogertae22659602015-05-11 21:58:21719 std::string(), base::string16(), 3,
720 std::string());
nomsbe073e52014-08-23 22:01:04721 EXPECT_EQ(4U, GetCache()->GetNumberOfProfiles());
722
723 ResetCache();
724
725 // Profile names should have been preserved.
726 EXPECT_EQ(name_1, GetCache()->GetNameOfProfileAtIndex(
727 GetCache()->GetIndexOfProfileWithPath(path_1)));
728 EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(
729 GetCache()->GetIndexOfProfileWithPath(path_2)));
730 EXPECT_EQ(name_3, GetCache()->GetNameOfProfileAtIndex(
731 GetCache()->GetIndexOfProfileWithPath(path_3)));
732 EXPECT_EQ(name_4, GetCache()->GetNameOfProfileAtIndex(
733 GetCache()->GetIndexOfProfileWithPath(path_4)));
734}
anthonyvd66aa95d2015-10-02 15:40:26735#endif