blob: 873f4ad09a89b67b9ed58675bb9557db6c3b144b [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"
Xi Cheng1b761382017-08-15 20:37:3421#include "chrome/browser/profiles/avatar_menu.h"
[email protected]9de44302014-05-01 16:32:2722#include "chrome/browser/profiles/profile_avatar_downloader.h"
[email protected]c3e559772014-04-09 04:02:5423#include "chrome/browser/profiles/profile_avatar_icon_util.h"
[email protected]d4f5d1162011-11-30 01:41:5224#include "chrome/browser/profiles/profile_info_cache.h"
[email protected]6730b1e2011-09-29 05:23:5225#include "chrome/browser/profiles/profile_manager.h"
emaxxa2e11492015-05-12 17:19:1126#include "chrome/common/chrome_paths.h"
[email protected]5cf8bcd92014-07-17 20:27:4027#include "chrome/common/chrome_switches.h"
noms693f35aa2015-02-05 16:03:1628#include "chrome/common/pref_names.h"
[email protected]583844c2011-08-27 00:38:3529#include "chrome/test/base/testing_browser_process.h"
brettwb1fc1b82016-02-02 00:19:0830#include "components/prefs/testing_pref_service.h"
[email protected]5cf8bcd92014-07-17 20:27:4031#include "components/signin/core/common/profile_management_switches.h"
maxbogueea16ff412016-10-28 16:35:2932#include "components/sync_preferences/pref_service_syncable.h"
[email protected]371662e372013-10-17 22:05:2233#include "content/public/test/test_browser_thread_bundle.h"
[email protected]a7fe9112012-07-20 02:34:4534#include "content/public/test/test_utils.h"
[email protected]dee810e2011-06-27 19:43:3935#include "third_party/skia/include/core/SkBitmap.h"
36#include "ui/base/resource/resource_bundle.h"
37#include "ui/gfx/image/image.h"
[email protected]d4f5d1162011-11-30 01:41:5238#include "ui/gfx/image/image_unittest_util.h"
[email protected]dee810e2011-06-27 19:43:3939
[email protected]6778fed2013-12-24 20:09:3740using base::ASCIIToUTF16;
[email protected]f729d7a2013-12-26 07:07:5641using base::UTF8ToUTF16;
[email protected]cb114f142011-11-23 20:18:0442using content::BrowserThread;
43
[email protected]6a460662011-12-22 22:05:1644ProfileNameVerifierObserver::ProfileNameVerifierObserver(
45 TestingProfileManager* testing_profile_manager)
46 : testing_profile_manager_(testing_profile_manager) {
47 DCHECK(testing_profile_manager_);
[email protected]590e189b2011-12-13 22:07:0348}
49
50ProfileNameVerifierObserver::~ProfileNameVerifierObserver() {
51}
52
53void ProfileNameVerifierObserver::OnProfileAdded(
[email protected]650b2d52013-02-10 03:41:4554 const base::FilePath& profile_path) {
[email protected]a04db822013-12-11 19:14:4055 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1656 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0357 EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end());
58 profile_names_.insert(profile_name);
59}
60
[email protected]7b0af152011-12-16 17:02:0661void ProfileNameVerifierObserver::OnProfileWillBeRemoved(
[email protected]650b2d52013-02-10 03:41:4562 const base::FilePath& profile_path) {
[email protected]a04db822013-12-11 19:14:4063 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1664 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0365 EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end());
66 profile_names_.erase(profile_name);
67}
68
[email protected]7b0af152011-12-16 17:02:0669void ProfileNameVerifierObserver::OnProfileWasRemoved(
[email protected]650b2d52013-02-10 03:41:4570 const base::FilePath& profile_path,
[email protected]a04db822013-12-11 19:14:4071 const base::string16& profile_name) {
[email protected]7b0af152011-12-16 17:02:0672 EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end());
73}
74
[email protected]590e189b2011-12-13 22:07:0375void ProfileNameVerifierObserver::OnProfileNameChanged(
[email protected]650b2d52013-02-10 03:41:4576 const base::FilePath& profile_path,
[email protected]a04db822013-12-11 19:14:4077 const base::string16& old_profile_name) {
78 base::string16 new_profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1679 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0380 EXPECT_TRUE(profile_names_.find(old_profile_name) != profile_names_.end());
81 EXPECT_TRUE(profile_names_.find(new_profile_name) == profile_names_.end());
82 profile_names_.erase(old_profile_name);
83 profile_names_.insert(new_profile_name);
84}
85
86void ProfileNameVerifierObserver::OnProfileAvatarChanged(
[email protected]650b2d52013-02-10 03:41:4587 const base::FilePath& profile_path) {
[email protected]a04db822013-12-11 19:14:4088 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1689 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0390 EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end());
91}
92
[email protected]6a460662011-12-22 22:05:1693ProfileInfoCache* ProfileNameVerifierObserver::GetCache() {
94 return testing_profile_manager_->profile_info_cache();
95}
96
[email protected]d4f5d1162011-11-30 01:41:5297ProfileInfoCacheTest::ProfileInfoCacheTest()
[email protected]0aa018a2013-07-31 15:08:5498 : testing_profile_manager_(TestingBrowserProcess::GetGlobal()),
emaxxa2e11492015-05-12 17:19:1199 name_observer_(&testing_profile_manager_),
100 user_data_dir_override_(chrome::DIR_USER_DATA) {
[email protected]d4f5d1162011-11-30 01:41:52101}
102
103ProfileInfoCacheTest::~ProfileInfoCacheTest() {
104}
105
106void ProfileInfoCacheTest::SetUp() {
107 ASSERT_TRUE(testing_profile_manager_.SetUp());
[email protected]590e189b2011-12-13 22:07:03108 testing_profile_manager_.profile_info_cache()->AddObserver(&name_observer_);
[email protected]d4f5d1162011-11-30 01:41:52109}
110
[email protected]1302dcf2011-11-30 21:47:05111void ProfileInfoCacheTest::TearDown() {
Gabriel Charette426214f2017-08-14 18:21:58112 // Drain remaining tasks to make sure all tasks are completed. This prevents
[email protected]1302dcf2011-11-30 21:47:05113 // memory leaks.
Gabriel Charette426214f2017-08-14 18:21:58114 content::RunAllBlockingPoolTasksUntilIdle();
[email protected]1302dcf2011-11-30 21:47:05115}
116
[email protected]d4f5d1162011-11-30 01:41:52117ProfileInfoCache* ProfileInfoCacheTest::GetCache() {
118 return testing_profile_manager_.profile_info_cache();
119}
120
[email protected]650b2d52013-02-10 03:41:45121base::FilePath ProfileInfoCacheTest::GetProfilePath(
[email protected]d4f5d1162011-11-30 01:41:52122 const std::string& base_name) {
123 return testing_profile_manager_.profile_manager()->user_data_dir().
124 AppendASCII(base_name);
125}
126
127void ProfileInfoCacheTest::ResetCache() {
128 testing_profile_manager_.DeleteProfileInfoCache();
129}
130
[email protected]d4f5d1162011-11-30 01:41:52131TEST_F(ProfileInfoCacheTest, AddProfiles) {
[email protected]6730b1e2011-09-29 05:23:52132 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39133
[email protected]ca591072012-03-27 01:54:44134 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
avib896c712015-12-26 02:10:43135 for (uint32_t i = 0; i < 4; ++i) {
[email protected]7d3cbc92013-03-18 22:33:04136 base::FilePath profile_path =
137 GetProfilePath(base::StringPrintf("path_%ud", i));
[email protected]a04db822013-12-11 19:14:40138 base::string16 profile_name =
139 ASCIIToUTF16(base::StringPrintf("name_%ud", i));
[email protected]ca591072012-03-27 01:54:44140 const SkBitmap* icon = rb.GetImageNamed(
[email protected]c3e559772014-04-09 04:02:54141 profiles::GetDefaultAvatarIconResourceIDAtIndex(
[email protected]ca591072012-03-27 01:54:44142 i)).ToSkBitmap();
[email protected]d20d0432014-06-12 17:14:05143 std::string supervised_user_id = i == 3 ? "TEST_ID" : "";
[email protected]dee810e2011-06-27 19:43:39144
rogertae22659602015-05-11 21:58:21145 GetCache()->AddProfileToCache(profile_path, profile_name, std::string(),
146 base::string16(), i, supervised_user_id);
[email protected]cb114f142011-11-23 20:18:04147 GetCache()->SetBackgroundStatusOfProfileAtIndex(i, true);
[email protected]a04db822013-12-11 19:14:40148 base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
[email protected]cb114f142011-11-23 20:18:04149 GetCache()->SetGAIANameOfProfileAtIndex(i, gaia_name);
[email protected]dee810e2011-06-27 19:43:39150
[email protected]6730b1e2011-09-29 05:23:52151 EXPECT_EQ(i + 1, GetCache()->GetNumberOfProfiles());
152 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
153 EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i));
[email protected]0aa018a2013-07-31 15:08:54154 const SkBitmap* actual_icon =
155 GetCache()->GetAvatarIconOfProfileAtIndex(i).ToSkBitmap();
[email protected]ca591072012-03-27 01:54:44156 EXPECT_EQ(icon->width(), actual_icon->width());
157 EXPECT_EQ(icon->height(), actual_icon->height());
[email protected]d20d0432014-06-12 17:14:05158 EXPECT_EQ(i == 3, GetCache()->ProfileIsSupervisedAtIndex(i));
[email protected]5ddfade2014-02-03 10:24:53159 EXPECT_EQ(i == 3, GetCache()->IsOmittedProfileAtIndex(i));
[email protected]d20d0432014-06-12 17:14:05160 EXPECT_EQ(supervised_user_id,
161 GetCache()->GetSupervisedUserIdOfProfileAtIndex(i));
[email protected]dee810e2011-06-27 19:43:39162 }
[email protected]cb114f142011-11-23 20:18:04163
164 // Reset the cache and test the it reloads correctly.
165 ResetCache();
166
167 EXPECT_EQ(4u, GetCache()->GetNumberOfProfiles());
avib896c712015-12-26 02:10:43168 for (uint32_t i = 0; i < 4; ++i) {
[email protected]7d3cbc92013-03-18 22:33:04169 base::FilePath profile_path =
170 GetProfilePath(base::StringPrintf("path_%ud", i));
[email protected]cb114f142011-11-23 20:18:04171 EXPECT_EQ(i, GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]a04db822013-12-11 19:14:40172 base::string16 profile_name =
173 ASCIIToUTF16(base::StringPrintf("name_%ud", i));
[email protected]cb114f142011-11-23 20:18:04174 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
175 EXPECT_EQ(i, GetCache()->GetAvatarIconIndexOfProfileAtIndex(i));
176 EXPECT_EQ(true, GetCache()->GetBackgroundStatusOfProfileAtIndex(i));
[email protected]a04db822013-12-11 19:14:40177 base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
[email protected]cb114f142011-11-23 20:18:04178 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(i));
179 }
[email protected]dee810e2011-06-27 19:43:39180}
181
[email protected]d4f5d1162011-11-30 01:41:52182TEST_F(ProfileInfoCacheTest, DeleteProfile) {
[email protected]6730b1e2011-09-29 05:23:52183 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39184
[email protected]650b2d52013-02-10 03:41:45185 base::FilePath path_1 = GetProfilePath("path_1");
[email protected]a04db822013-12-11 19:14:40186 GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("name_1"),
rogertae22659602015-05-11 21:58:21187 std::string(), base::string16(), 0,
188 std::string());
[email protected]6730b1e2011-09-29 05:23:52189 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39190
[email protected]650b2d52013-02-10 03:41:45191 base::FilePath path_2 = GetProfilePath("path_2");
[email protected]a04db822013-12-11 19:14:40192 base::string16 name_2 = ASCIIToUTF16("name_2");
rogertae22659602015-05-11 21:58:21193 GetCache()->AddProfileToCache(path_2, name_2, std::string(), base::string16(),
194 0, std::string());
[email protected]6730b1e2011-09-29 05:23:52195 EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39196
[email protected]6730b1e2011-09-29 05:23:52197 GetCache()->DeleteProfileFromCache(path_1);
198 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
199 EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(0));
[email protected]dee810e2011-06-27 19:43:39200
[email protected]6730b1e2011-09-29 05:23:52201 GetCache()->DeleteProfileFromCache(path_2);
202 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39203}
204
[email protected]d4f5d1162011-11-30 01:41:52205TEST_F(ProfileInfoCacheTest, MutateProfile) {
206 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21207 GetProfilePath("path_1"), ASCIIToUTF16("name_1"), std::string(),
[email protected]a04db822013-12-11 19:14:40208 base::string16(), 0, std::string());
[email protected]d4f5d1162011-11-30 01:41:52209 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21210 GetProfilePath("path_2"), ASCIIToUTF16("name_2"), std::string(),
[email protected]a04db822013-12-11 19:14:40211 base::string16(), 0, std::string());
[email protected]dee810e2011-06-27 19:43:39212
[email protected]a04db822013-12-11 19:14:40213 base::string16 new_name = ASCIIToUTF16("new_name");
[email protected]6730b1e2011-09-29 05:23:52214 GetCache()->SetNameOfProfileAtIndex(1, new_name);
215 EXPECT_EQ(new_name, GetCache()->GetNameOfProfileAtIndex(1));
216 EXPECT_NE(new_name, GetCache()->GetNameOfProfileAtIndex(0));
[email protected]dee810e2011-06-27 19:43:39217
[email protected]a04db822013-12-11 19:14:40218 base::string16 new_user_name = ASCIIToUTF16("user_name");
rogertae22659602015-05-11 21:58:21219 std::string new_gaia_id = "12345";
220 GetCache()->SetAuthInfoOfProfileAtIndex(1, new_gaia_id, new_user_name);
[email protected]e8e78092011-09-29 18:15:38221 EXPECT_EQ(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(1));
rogertae22659602015-05-11 21:58:21222 EXPECT_EQ(new_gaia_id, GetCache()->GetGAIAIdOfProfileAtIndex(1));
[email protected]e8e78092011-09-29 18:15:38223 EXPECT_NE(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(0));
224
lixan20fabcc2017-01-31 16:03:02225 const size_t new_icon_index = 3;
[email protected]6730b1e2011-09-29 05:23:52226 GetCache()->SetAvatarIconOfProfileAtIndex(1, new_icon_index);
lixan20fabcc2017-01-31 16:03:02227 EXPECT_EQ(new_icon_index, GetCache()->GetAvatarIconIndexOfProfileAtIndex(1));
[email protected]dee810e2011-06-27 19:43:39228 // Not much to test.
[email protected]6730b1e2011-09-29 05:23:52229 GetCache()->GetAvatarIconOfProfileAtIndex(1);
lixan20fabcc2017-01-31 16:03:02230
231 const size_t wrong_icon_index = profiles::GetDefaultAvatarIconCount() + 1;
232 const size_t generic_icon_index = 0;
233 GetCache()->SetAvatarIconOfProfileAtIndex(1, wrong_icon_index);
234 EXPECT_EQ(generic_icon_index,
235 GetCache()->GetAvatarIconIndexOfProfileAtIndex(1));
[email protected]dee810e2011-06-27 19:43:39236}
237
[email protected]d4f5d1162011-11-30 01:41:52238TEST_F(ProfileInfoCacheTest, Sort) {
[email protected]a04db822013-12-11 19:14:40239 base::string16 name_a = ASCIIToUTF16("apple");
[email protected]d4f5d1162011-11-30 01:41:52240 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21241 GetProfilePath("path_a"), name_a, std::string(), base::string16(), 0,
242 std::string());
[email protected]cb114f142011-11-23 20:18:04243
[email protected]a04db822013-12-11 19:14:40244 base::string16 name_c = ASCIIToUTF16("cat");
[email protected]d4f5d1162011-11-30 01:41:52245 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21246 GetProfilePath("path_c"), name_c, std::string(), base::string16(), 0,
247 std::string());
[email protected]cb114f142011-11-23 20:18:04248
249 // Sanity check the initial order.
250 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
251 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
252
253 // Add a new profile (start with a capital to test case insensitive sorting.
[email protected]a04db822013-12-11 19:14:40254 base::string16 name_b = ASCIIToUTF16("Banana");
[email protected]d4f5d1162011-11-30 01:41:52255 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21256 GetProfilePath("path_b"), name_b, std::string(), base::string16(), 0,
257 std::string());
[email protected]cb114f142011-11-23 20:18:04258
259 // Verify the new order.
260 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
261 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(1));
262 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(2));
263
264 // Change the name of an existing profile.
265 name_a = UTF8ToUTF16("dog");
266 GetCache()->SetNameOfProfileAtIndex(0, name_a);
267
268 // Verify the new order.
269 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
270 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
271 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(2));
272
273 // Delete a profile.
[email protected]d4f5d1162011-11-30 01:41:52274 GetCache()->DeleteProfileFromCache(GetProfilePath("path_c"));
[email protected]cb114f142011-11-23 20:18:04275
276 // Verify the new order.
277 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
278 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(1));
279}
280
WC Leung17756382017-08-25 04:57:36281// Will be removed SOON with ProfileInfoCache tests.
[email protected]d4f5d1162011-11-30 01:41:52282TEST_F(ProfileInfoCacheTest, BackgroundModeStatus) {
[email protected]279170832011-10-12 23:38:03283 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13284 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
rogertae22659602015-05-11 21:58:21285 std::string(), base::string16(), 0, std::string());
[email protected]279170832011-10-12 23:38:03286 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13287 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
rogertae22659602015-05-11 21:58:21288 std::string(), base::string16(), 0, std::string());
[email protected]279170832011-10-12 23:38:03289
290 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
291 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
292
293 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, true);
294
295 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
296 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
297
298 GetCache()->SetBackgroundStatusOfProfileAtIndex(0, true);
299
300 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
301 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
302
303 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, false);
304
305 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
306 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
307}
308
[email protected]d4f5d1162011-11-30 01:41:52309TEST_F(ProfileInfoCacheTest, GAIAName) {
[email protected]cb114f142011-11-23 20:18:04310 GetCache()->AddProfileToCache(
[email protected]a4352d02014-03-06 06:00:27311 GetProfilePath("path_1"), ASCIIToUTF16("Person 1"),
rogertae22659602015-05-11 21:58:21312 std::string(), base::string16(), 0, std::string());
[email protected]a4352d02014-03-06 06:00:27313 base::string16 profile_name(ASCIIToUTF16("Person 2"));
[email protected]cb114f142011-11-23 20:18:04314 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21315 GetProfilePath("path_2"), profile_name, std::string(), base::string16(),
316 0, std::string());
[email protected]cb114f142011-11-23 20:18:04317
[email protected]a4352d02014-03-06 06:00:27318 int index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
319 int index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
320
[email protected]cb114f142011-11-23 20:18:04321 // Sanity check.
[email protected]a4352d02014-03-06 06:00:27322 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty());
323 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index2).empty());
[email protected]cb114f142011-11-23 20:18:04324
[email protected]425d6ab2014-03-25 05:59:39325 // Set GAIA name. This re-sorts the cache.
[email protected]a04db822013-12-11 19:14:40326 base::string16 gaia_name(ASCIIToUTF16("Pat Smith"));
[email protected]a4352d02014-03-06 06:00:27327 GetCache()->SetGAIANameOfProfileAtIndex(index2, gaia_name);
[email protected]425d6ab2014-03-25 05:59:39328 index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
329 index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
330
331 // Since there is a GAIA name, we use that as a display name.
[email protected]a4352d02014-03-06 06:00:27332 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty());
333 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2));
[email protected]a4352d02014-03-06 06:00:27334 EXPECT_EQ(gaia_name, GetCache()->GetNameOfProfileAtIndex(index2));
[email protected]cb114f142011-11-23 20:18:04335
[email protected]a4352d02014-03-06 06:00:27336 // Don't use GAIA name as profile name. This re-sorts the cache.
[email protected]425d6ab2014-03-25 05:59:39337 base::string16 custom_name(ASCIIToUTF16("Custom name"));
338 GetCache()->SetNameOfProfileAtIndex(index2, custom_name);
[email protected]1cdf0932014-08-16 06:00:49339 GetCache()->SetProfileIsUsingDefaultNameAtIndex(index2, false);
340
[email protected]a4352d02014-03-06 06:00:27341 index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
342 index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
343
[email protected]425d6ab2014-03-25 05:59:39344 EXPECT_EQ(custom_name, GetCache()->GetNameOfProfileAtIndex(index2));
[email protected]a4352d02014-03-06 06:00:27345 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2));
[email protected]cb114f142011-11-23 20:18:04346}
347
[email protected]d4f5d1162011-11-30 01:41:52348TEST_F(ProfileInfoCacheTest, GAIAPicture) {
[email protected]22900902014-07-30 11:50:41349 const int kDefaultAvatarIndex = 0;
350 const int kOtherAvatarIndex = 1;
[email protected]979dc4f2014-08-12 22:52:11351 const int kGaiaPictureSize = 256; // Standard size of a Gaia account picture.
[email protected]cb114f142011-11-23 20:18:04352 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13353 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
rogertae22659602015-05-11 21:58:21354 std::string(), base::string16(), kDefaultAvatarIndex, std::string());
[email protected]cb114f142011-11-23 20:18:04355 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13356 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
rogertae22659602015-05-11 21:58:21357 std::string(), base::string16(), kDefaultAvatarIndex, std::string());
[email protected]cb114f142011-11-23 20:18:04358
359 // Sanity check.
[email protected]2f3c00f2011-11-30 04:36:22360 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
361 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(1));
[email protected]cb114f142011-11-23 20:18:04362 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0));
363 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
364
365 // The profile icon should be the default one.
[email protected]22900902014-07-30 11:50:41366 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(0));
367 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
368 int default_avatar_id =
369 profiles::GetDefaultAvatarIconResourceIDAtIndex(kDefaultAvatarIndex);
370 const gfx::Image& default_avatar_image(
371 ResourceBundle::GetSharedInstance().GetImageNamed(default_avatar_id));
pkotwiczb312b512015-09-29 23:44:43372 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]22900902014-07-30 11:50:41373 default_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
[email protected]cb114f142011-11-23 20:18:04374
375 // Set GAIA picture.
[email protected]979dc4f2014-08-12 22:52:11376 gfx::Image gaia_image(gfx::test::CreateImage(
377 kGaiaPictureSize, kGaiaPictureSize));
[email protected]2f3c00f2011-11-30 04:36:22378 GetCache()->SetGAIAPictureOfProfileAtIndex(1, &gaia_image);
379 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
pkotwiczb312b512015-09-29 23:44:43380 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22381 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
[email protected]22900902014-07-30 11:50:41382 // Since we're still using the default avatar, the GAIA image should be
383 // preferred over the generic avatar image.
384 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
385 EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
pkotwiczb312b512015-09-29 23:44:43386 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]22900902014-07-30 11:50:41387 gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
[email protected]cb114f142011-11-23 20:18:04388
[email protected]1cdf0932014-08-16 06:00:49389 // Set a non-default avatar. This should be preferred over the GAIA image.
[email protected]22900902014-07-30 11:50:41390 GetCache()->SetAvatarIconOfProfileAtIndex(1, kOtherAvatarIndex);
[email protected]1cdf0932014-08-16 06:00:49391 GetCache()->SetProfileIsUsingDefaultAvatarAtIndex(1, false);
[email protected]22900902014-07-30 11:50:41392 EXPECT_FALSE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
393 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
394 int other_avatar_id =
395 profiles::GetDefaultAvatarIconResourceIDAtIndex(kOtherAvatarIndex);
396 const gfx::Image& other_avatar_image(
397 ResourceBundle::GetSharedInstance().GetImageNamed(other_avatar_id));
pkotwiczb312b512015-09-29 23:44:43398 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]22900902014-07-30 11:50:41399 other_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
400
401 // Explicitly setting the GAIA picture should make it preferred again.
[email protected]cb114f142011-11-23 20:18:04402 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, true);
[email protected]22900902014-07-30 11:50:41403 EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
pkotwiczb312b512015-09-29 23:44:43404 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22405 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
pkotwiczb312b512015-09-29 23:44:43406 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]cb114f142011-11-23 20:18:04407 gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
408
[email protected]22900902014-07-30 11:50:41409 // Clearing the IsUsingGAIAPicture flag should result in the generic image
410 // being used again.
[email protected]cb114f142011-11-23 20:18:04411 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, false);
[email protected]22900902014-07-30 11:50:41412 EXPECT_FALSE(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]22900902014-07-30 11:50:41416 other_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
[email protected]cb114f142011-11-23 20:18:04417}
418
[email protected]754bebc2011-12-01 16:42:16419TEST_F(ProfileInfoCacheTest, PersistGAIAPicture) {
[email protected]cb114f142011-11-23 20:18:04420 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13421 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
rogertae22659602015-05-11 21:58:21422 std::string(), base::string16(), 0, std::string());
[email protected]d4f5d1162011-11-30 01:41:52423 gfx::Image gaia_image(gfx::test::CreateImage());
[email protected]cb114f142011-11-23 20:18:04424
[email protected]2f3c00f2011-11-30 04:36:22425 GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_image);
noms75ca0192015-02-26 03:14:30426
427 // Make sure everything has completed, and the file has been written to disk.
Gabriel Charette426214f2017-08-14 18:21:58428 content::RunAllBlockingPoolTasksUntilIdle();
noms75ca0192015-02-26 03:14:30429
pkotwiczb312b512015-09-29 23:44:43430 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22431 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
[email protected]cb114f142011-11-23 20:18:04432
[email protected]cb114f142011-11-23 20:18:04433 ResetCache();
[email protected]cb114f142011-11-23 20:18:04434 // Try to get the GAIA picture. This should return NULL until the read from
435 // disk is done.
[email protected]2f3c00f2011-11-30 04:36:22436 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
Gabriel Charette426214f2017-08-14 18:21:58437 content::RunAllBlockingPoolTasksUntilIdle();
noms75ca0192015-02-26 03:14:30438
pkotwiczb312b512015-09-29 23:44:43439 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22440 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
441}
442
[email protected]d20d0432014-06-12 17:14:05443TEST_F(ProfileInfoCacheTest, SetSupervisedUserId) {
[email protected]0aa018a2013-07-31 15:08:54444 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13445 GetProfilePath("test"), ASCIIToUTF16("Test"),
rogertae22659602015-05-11 21:58:21446 std::string(), base::string16(), 0, std::string());
[email protected]d20d0432014-06-12 17:14:05447 EXPECT_FALSE(GetCache()->ProfileIsSupervisedAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54448
[email protected]d20d0432014-06-12 17:14:05449 GetCache()->SetSupervisedUserIdOfProfileAtIndex(0, "TEST_ID");
450 EXPECT_TRUE(GetCache()->ProfileIsSupervisedAtIndex(0));
451 EXPECT_EQ("TEST_ID", GetCache()->GetSupervisedUserIdOfProfileAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54452
453 ResetCache();
[email protected]d20d0432014-06-12 17:14:05454 EXPECT_TRUE(GetCache()->ProfileIsSupervisedAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54455
[email protected]d20d0432014-06-12 17:14:05456 GetCache()->SetSupervisedUserIdOfProfileAtIndex(0, std::string());
457 EXPECT_FALSE(GetCache()->ProfileIsSupervisedAtIndex(0));
458 EXPECT_EQ("", GetCache()->GetSupervisedUserIdOfProfileAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54459}
460
[email protected]2f3c00f2011-11-30 04:36:22461TEST_F(ProfileInfoCacheTest, EmptyGAIAInfo) {
[email protected]a04db822013-12-11 19:14:40462 base::string16 profile_name = ASCIIToUTF16("name_1");
[email protected]c3e559772014-04-09 04:02:54463 int id = profiles::GetDefaultAvatarIconResourceIDAtIndex(0);
[email protected]2f3c00f2011-11-30 04:36:22464 const gfx::Image& profile_image(
465 ResourceBundle::GetSharedInstance().GetImageNamed(id));
466
467 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21468 GetProfilePath("path_1"), profile_name, std::string(), base::string16(),
469 0, std::string());
[email protected]2f3c00f2011-11-30 04:36:22470
471 // Set empty GAIA info.
[email protected]a04db822013-12-11 19:14:40472 GetCache()->SetGAIANameOfProfileAtIndex(0, base::string16());
[email protected]2f3c00f2011-11-30 04:36:22473 GetCache()->SetGAIAPictureOfProfileAtIndex(0, NULL);
[email protected]2f3c00f2011-11-30 04:36:22474 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(0, true);
475
476 // Verify that the profile name and picture are not empty.
477 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(0));
pkotwiczb312b512015-09-29 23:44:43478 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22479 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(0)));
[email protected]cb114f142011-11-23 20:18:04480}
481
[email protected]d20d0432014-06-12 17:14:05482TEST_F(ProfileInfoCacheTest, CreateSupervisedTestingProfile) {
[email protected]0aa018a2013-07-31 15:08:54483 testing_profile_manager_.CreateTestingProfile("default");
[email protected]d20d0432014-06-12 17:14:05484 base::string16 supervised_user_name = ASCIIToUTF16("Supervised User");
[email protected]0aa018a2013-07-31 15:08:54485 testing_profile_manager_.CreateTestingProfile(
maxbogueea16ff412016-10-28 16:35:29486 "test1", std::unique_ptr<sync_preferences::PrefServiceSyncable>(),
[email protected]d20d0432014-06-12 17:14:05487 supervised_user_name, 0, "TEST_ID", TestingProfile::TestingFactories());
[email protected]0aa018a2013-07-31 15:08:54488 for (size_t i = 0; i < GetCache()->GetNumberOfProfiles(); i++) {
[email protected]d20d0432014-06-12 17:14:05489 bool is_supervised =
490 GetCache()->GetNameOfProfileAtIndex(i) == supervised_user_name;
491 EXPECT_EQ(is_supervised, GetCache()->ProfileIsSupervisedAtIndex(i));
492 std::string supervised_user_id = is_supervised ? "TEST_ID" : "";
493 EXPECT_EQ(supervised_user_id,
494 GetCache()->GetSupervisedUserIdOfProfileAtIndex(i));
[email protected]0aa018a2013-07-31 15:08:54495 }
[email protected]371662e372013-10-17 22:05:22496
[email protected]d20d0432014-06-12 17:14:05497 // Supervised profiles have a custom theme, which needs to be deleted on the
498 // FILE thread. Reset the profile manager now so everything is deleted while
499 // we still have a FILE thread.
[email protected]371662e372013-10-17 22:05:22500 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
[email protected]0aa018a2013-07-31 15:08:54501}
502
[email protected]047481802013-09-16 22:26:38503TEST_F(ProfileInfoCacheTest, AddStubProfile) {
504 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
505
506 // Add some profiles with and without a '.' in their paths.
507 const struct {
508 const char* profile_path;
509 const char* profile_name;
510 } kTestCases[] = {
511 { "path.test0", "name_0" },
512 { "path_test1", "name_1" },
513 { "path.test2", "name_2" },
514 { "path_test3", "name_3" },
515 };
516
viettrungluu9e65ad12014-10-16 04:22:26517 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
[email protected]047481802013-09-16 22:26:38518 base::FilePath profile_path = GetProfilePath(kTestCases[i].profile_path);
[email protected]a04db822013-12-11 19:14:40519 base::string16 profile_name = ASCIIToUTF16(kTestCases[i].profile_name);
[email protected]047481802013-09-16 22:26:38520
rogertae22659602015-05-11 21:58:21521 GetCache()->AddProfileToCache(profile_path, profile_name, std::string(),
522 base::string16(), i, "");
[email protected]047481802013-09-16 22:26:38523
524 EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i));
525 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
526 }
527
528 ASSERT_EQ(4U, GetCache()->GetNumberOfProfiles());
529
530 // Check that the profiles can be extracted from the local state.
noms693f35aa2015-02-05 16:03:16531 std::vector<base::string16> names;
532 PrefService* local_state = g_browser_process->local_state();
533 const base::DictionaryValue* cache = local_state->GetDictionary(
534 prefs::kProfileInfoCache);
535 base::string16 name;
536 for (base::DictionaryValue::Iterator it(*cache); !it.IsAtEnd();
537 it.Advance()) {
538 const base::DictionaryValue* info = NULL;
539 it.value().GetAsDictionary(&info);
540 info->GetString("name", &name);
541 names.push_back(name);
542 }
543
[email protected]047481802013-09-16 22:26:38544 for (size_t i = 0; i < 4; i++)
545 ASSERT_FALSE(names[i].empty());
546}
547
lwchkg778965f2016-05-12 13:14:22548TEST_F(ProfileInfoCacheTest, EntriesInAttributesStorage) {
549 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
550
551 // Add some profiles with and without a '.' in their paths.
552 const struct {
553 const char* profile_path;
554 const char* profile_name;
555 } kTestCases[] = {
556 { "path.test0", "name_0" },
557 { "path_test1", "name_1" },
558 { "path.test2", "name_2" },
559 { "path_test3", "name_3" },
560 };
561
562 // Profiles are added and removed using all combinations of the old and the
563 // new interfaces. The content of |profile_attributes_entries_| in
564 // ProfileAttributesStorage is checked after each insert and delete operation.
565
566 // Add profiles.
567 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
568 base::FilePath profile_path = GetProfilePath(kTestCases[i].profile_path);
569 base::string16 profile_name = ASCIIToUTF16(kTestCases[i].profile_name);
570
571 ASSERT_EQ(0u, GetCache()->profile_attributes_entries_.count(
572 profile_path.value()));
573
574 // Use ProfileInfoCache in profiles 0 and 2, and ProfileAttributesStorage in
575 // profiles 1 and 3.
576 if (i | 1u) {
577 GetCache()->AddProfileToCache(profile_path, profile_name, std::string(),
578 base::string16(), i, "");
579 } else {
580 GetCache()->AddProfile(profile_path, profile_name, std::string(),
581 base::string16(), i, "");
582 }
583
584 ASSERT_EQ(i + 1, GetCache()->GetNumberOfProfiles());
585 ASSERT_EQ(i + 1, GetCache()->profile_attributes_entries_.size());
586
587 ASSERT_EQ(1u, GetCache()->profile_attributes_entries_.count(
588 profile_path.value()));
589 // TODO(anthonyvd) : check that the entry in |profile_attributes_entries_|
590 // is null before GetProfileAttributesWithPath is run. Currently this is
591 // impossible to check because GetProfileAttributesWithPath is called during
592 // profile creation.
593
594 ProfileAttributesEntry* entry = nullptr;
595 GetCache()->GetProfileAttributesWithPath(profile_path, &entry);
596 EXPECT_EQ(
597 entry,
598 GetCache()->profile_attributes_entries_[profile_path.value()].get());
599 }
600
601 // Remove profiles.
602 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
603 base::FilePath profile_path = GetProfilePath(kTestCases[i].profile_path);
604 ASSERT_EQ(1u, GetCache()->profile_attributes_entries_.count(
605 profile_path.value()));
606
607 // Use ProfileInfoCache in profiles 0 and 1, and ProfileAttributesStorage in
608 // profiles 2 and 3.
609 if (i | 2u)
610 GetCache()->DeleteProfileFromCache(profile_path);
611 else
612 GetCache()->RemoveProfile(profile_path);
613
614 ASSERT_EQ(0u, GetCache()->profile_attributes_entries_.count(
615 profile_path.value()));
616
617 ProfileAttributesEntry* entry = nullptr;
618 EXPECT_FALSE(GetCache()->GetProfileAttributesWithPath(profile_path,
619 &entry));
620 ASSERT_EQ(0u, GetCache()->profile_attributes_entries_.count(
621 profile_path.value()));
622 }
623}
624
[email protected]b09bfa12014-08-15 19:05:25625// High res avatar downloading is only supported on desktop.
jam1c5a91492016-02-24 20:47:53626#if !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
[email protected]9de44302014-05-01 16:32:27627TEST_F(ProfileInfoCacheTest, DownloadHighResAvatarTest) {
mlerman3ab32772015-04-08 14:48:56628 // The TestingProfileManager's ProfileInfoCache doesn't download avatars.
629 ProfileInfoCache profile_info_cache(g_browser_process->local_state(),
630 testing_profile_manager_.profile_manager()->user_data_dir());
631
emaxxa2e11492015-05-12 17:19:11632 // Make sure there are no avatars already on disk.
mlerman3ab32772015-04-08 14:48:56633 const size_t kIconIndex = 0;
634 base::FilePath icon_path =
635 profiles::GetPathOfHighResAvatarAtIndex(kIconIndex);
636 EXPECT_FALSE(base::PathExists(icon_path));
637
638 EXPECT_EQ(0U, profile_info_cache.GetNumberOfProfiles());
[email protected]9de44302014-05-01 16:32:27639 base::FilePath path_1 = GetProfilePath("path_1");
mlerman3ab32772015-04-08 14:48:56640 profile_info_cache.AddProfileToCache(path_1, ASCIIToUTF16("name_1"),
rogertae22659602015-05-11 21:58:21641 std::string(), base::string16(), kIconIndex, std::string());
mlerman3ab32772015-04-08 14:48:56642 EXPECT_EQ(1U, profile_info_cache.GetNumberOfProfiles());
Gabriel Charette426214f2017-08-14 18:21:58643 content::RunAllBlockingPoolTasksUntilIdle();
[email protected]9de44302014-05-01 16:32:27644
645 // We haven't downloaded any high-res avatars yet.
mlerman3ab32772015-04-08 14:48:56646 EXPECT_EQ(0U, profile_info_cache.cached_avatar_images_.size());
[email protected]5cf8bcd92014-07-17 20:27:40647
648 // After adding a new profile, the download of high-res avatar will be
649 // triggered if the flag kNewAvatarMenu has been set. But the downloader
650 // won't ever call OnFetchComplete in the test.
mlerman3ab32772015-04-08 14:48:56651 EXPECT_EQ(1U, profile_info_cache.avatar_images_downloads_in_progress_.size());
[email protected]5cf8bcd92014-07-17 20:27:40652
mlerman3ab32772015-04-08 14:48:56653 EXPECT_FALSE(profile_info_cache.GetHighResAvatarOfProfileAtIndex(0));
[email protected]9de44302014-05-01 16:32:27654
655 // Simulate downloading a high-res avatar.
[email protected]2d605712014-05-14 12:03:07656 ProfileAvatarDownloader avatar_downloader(
lwchkg3f4491202016-03-30 16:49:52657 kIconIndex,
658 base::Bind(&ProfileInfoCache::SaveAvatarImageAtPath,
659 base::Unretained(&profile_info_cache),
660 profile_info_cache.GetPathOfProfileAtIndex(0)));
[email protected]9de44302014-05-01 16:32:27661
662 // Put a real bitmap into "bitmap". 2x2 bitmap of green 32 bit pixels.
663 SkBitmap bitmap;
[email protected]e61cad52014-07-01 21:01:57664 bitmap.allocN32Pixels(2, 2);
[email protected]9de44302014-05-01 16:32:27665 bitmap.eraseColor(SK_ColorGREEN);
666
667 avatar_downloader.OnFetchComplete(
668 GURL("http://www.google.com/avatar.png"), &bitmap);
669
treibaf255502015-01-30 17:47:53670 // Now the download should not be in progress anymore.
mlerman3ab32772015-04-08 14:48:56671 EXPECT_EQ(0U, profile_info_cache.avatar_images_downloads_in_progress_.size());
treibaf255502015-01-30 17:47:53672
[email protected]9de44302014-05-01 16:32:27673 std::string file_name =
674 profiles::GetDefaultAvatarIconFileNameAtIndex(kIconIndex);
675
676 // The file should have been cached and saved.
mlerman3ab32772015-04-08 14:48:56677 EXPECT_EQ(1U, profile_info_cache.cached_avatar_images_.size());
678 EXPECT_TRUE(profile_info_cache.GetHighResAvatarOfProfileAtIndex(0));
lwchkgb17d86c2016-05-12 11:16:51679 EXPECT_EQ(profile_info_cache.cached_avatar_images_[file_name].get(),
mlerman3ab32772015-04-08 14:48:56680 profile_info_cache.GetHighResAvatarOfProfileAtIndex(0));
[email protected]9de44302014-05-01 16:32:27681
682 // Make sure everything has completed, and the file has been written to disk.
Gabriel Charette426214f2017-08-14 18:21:58683 content::RunAllBlockingPoolTasksUntilIdle();
[email protected]9de44302014-05-01 16:32:27684
685 // Clean up.
[email protected]9de44302014-05-01 16:32:27686 EXPECT_NE(std::string::npos, icon_path.MaybeAsASCII().find(file_name));
687 EXPECT_TRUE(base::PathExists(icon_path));
thestig16a4bcae2015-12-07 08:36:17688 EXPECT_TRUE(base::DeleteFile(icon_path, false));
[email protected]9de44302014-05-01 16:32:27689 EXPECT_FALSE(base::PathExists(icon_path));
690}
nomsbe073e52014-08-23 22:01:04691
emaxx0cb1957132015-05-18 20:22:54692TEST_F(ProfileInfoCacheTest, NothingToDownloadHighResAvatarTest) {
emaxx0cb1957132015-05-18 20:22:54693 // The TestingProfileManager's ProfileInfoCache doesn't download avatars.
694 ProfileInfoCache profile_info_cache(
695 g_browser_process->local_state(),
696 testing_profile_manager_.profile_manager()->user_data_dir());
697
698 const size_t kIconIndex = profiles::GetPlaceholderAvatarIndex();
699
700 EXPECT_EQ(0U, profile_info_cache.GetNumberOfProfiles());
701 base::FilePath path_1 = GetProfilePath("path_1");
702 profile_info_cache.AddProfileToCache(path_1, ASCIIToUTF16("name_1"),
703 std::string(), base::string16(),
704 kIconIndex, std::string());
705 EXPECT_EQ(1U, profile_info_cache.GetNumberOfProfiles());
Gabriel Charette426214f2017-08-14 18:21:58706 content::RunAllBlockingPoolTasksUntilIdle();
emaxx0cb1957132015-05-18 20:22:54707
708 // We haven't tried to download any high-res avatars as the specified icon is
709 // just a placeholder.
710 EXPECT_EQ(0U, profile_info_cache.cached_avatar_images_.size());
711 EXPECT_EQ(0U, profile_info_cache.avatar_images_downloads_in_progress_.size());
712}
713
nomsbe073e52014-08-23 22:01:04714TEST_F(ProfileInfoCacheTest, MigrateLegacyProfileNamesWithNewAvatarMenu) {
nomsbe073e52014-08-23 22:01:04715 EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles());
716
717 base::FilePath path_1 = GetProfilePath("path_1");
718 GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("Default Profile"),
rogertae22659602015-05-11 21:58:21719 std::string(), base::string16(), 0,
720 std::string());
nomsbe073e52014-08-23 22:01:04721 base::FilePath path_2 = GetProfilePath("path_2");
722 GetCache()->AddProfileToCache(path_2, ASCIIToUTF16("First user"),
rogertae22659602015-05-11 21:58:21723 std::string(), base::string16(), 1,
724 std::string());
nomsbe073e52014-08-23 22:01:04725 base::string16 name_3 = ASCIIToUTF16("Lemonade");
726 base::FilePath path_3 = GetProfilePath("path_3");
727 GetCache()->AddProfileToCache(path_3, name_3,
rogertae22659602015-05-11 21:58:21728 std::string(), base::string16(), 2,
729 std::string());
nomsbe073e52014-08-23 22:01:04730 base::string16 name_4 = ASCIIToUTF16("Batman");
731 base::FilePath path_4 = GetProfilePath("path_4");
732 GetCache()->AddProfileToCache(path_4, name_4,
rogertae22659602015-05-11 21:58:21733 std::string(), base::string16(), 3,
734 std::string());
nomsbe073e52014-08-23 22:01:04735 base::string16 name_5 = ASCIIToUTF16("Person 2");
736 base::FilePath path_5 = GetProfilePath("path_5");
737 GetCache()->AddProfileToCache(path_5, name_5,
rogertae22659602015-05-11 21:58:21738 std::string(), base::string16(), 2,
739 std::string());
nomsbe073e52014-08-23 22:01:04740
741 EXPECT_EQ(5U, GetCache()->GetNumberOfProfiles());
742
743
744 ResetCache();
745
746 // Legacy profile names like "Default Profile" and "First user" should be
WC Leung0ea09a642017-09-02 01:46:15747 // migrated to "Person %n" type names, i.e. any permutation of "Person 1" and
748 // "Person 3".
749 if (ASCIIToUTF16("Person 1") ==
750 GetCache()->GetNameOfProfileAtIndex(
751 GetCache()->GetIndexOfProfileWithPath(path_1))) {
752 EXPECT_EQ(ASCIIToUTF16("Person 3"),
753 GetCache()->GetNameOfProfileAtIndex(
754 GetCache()->GetIndexOfProfileWithPath(path_2)));
755 } else {
756 EXPECT_EQ(ASCIIToUTF16("Person 3"),
757 GetCache()->GetNameOfProfileAtIndex(
758 GetCache()->GetIndexOfProfileWithPath(path_1)));
759 EXPECT_EQ(ASCIIToUTF16("Person 1"),
760 GetCache()->GetNameOfProfileAtIndex(
761 GetCache()->GetIndexOfProfileWithPath(path_2)));
762 }
nomsbe073e52014-08-23 22:01:04763
764 // Other profile names should not be migrated even if they're the old
765 // default cartoon profile names.
766 EXPECT_EQ(name_3, GetCache()->GetNameOfProfileAtIndex(
767 GetCache()->GetIndexOfProfileWithPath(path_3)));
768 EXPECT_EQ(name_4, GetCache()->GetNameOfProfileAtIndex(
769 GetCache()->GetIndexOfProfileWithPath(path_4)));
770 EXPECT_EQ(name_5, GetCache()->GetNameOfProfileAtIndex(
771 GetCache()->GetIndexOfProfileWithPath(path_5)));
772}
Xi Cheng1b761382017-08-15 20:37:34773
774TEST_F(ProfileInfoCacheTest, GetGaiaImageForAvatarMenu) {
775 // The TestingProfileManager's ProfileInfoCache doesn't download avatars.
776 ProfileInfoCache profile_info_cache(
777 g_browser_process->local_state(),
778 testing_profile_manager_.profile_manager()->user_data_dir());
779
780 base::FilePath profile_path = GetProfilePath("path_1");
781
782 GetCache()->AddProfileToCache(profile_path, ASCIIToUTF16("name_1"),
783 std::string(), base::string16(), 0,
784 std::string());
785
786 gfx::Image gaia_image(gfx::test::CreateImage());
787 GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_image);
788
789 // Make sure everything has completed, and the file has been written to disk.
790 content::RunAllBlockingPoolTasksUntilIdle();
791
792 // Make sure this profile is using GAIA picture.
793 EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0));
794
795 ResetCache();
796
797 // We need to explicitly set the GAIA usage flag after resetting the cache.
798 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(0, true);
799 EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0));
800
801 gfx::Image image_loaded;
802
803 // Try to get the GAIA image. For the first time, it triggers an async image
804 // load from disk. The load status indicates the image is still being loaded.
805 EXPECT_EQ(AvatarMenu::ImageLoadStatus::LOADING,
806 AvatarMenu::GetImageForMenuButton(profile_path, &image_loaded));
807 EXPECT_FALSE(gfx::test::AreImagesEqual(gaia_image, image_loaded));
808
809 // Wait until the async image load finishes.
810 content::RunAllBlockingPoolTasksUntilIdle();
811
812 // Since the GAIA image is loaded now, we can get it this time.
813 EXPECT_EQ(AvatarMenu::ImageLoadStatus::LOADED,
814 AvatarMenu::GetImageForMenuButton(profile_path, &image_loaded));
815 EXPECT_TRUE(gfx::test::AreImagesEqual(gaia_image, image_loaded));
816}
[email protected]b09bfa12014-08-15 19:05:25817#endif
nomsbe073e52014-08-23 22:01:04818
jam1c5a91492016-02-24 20:47:53819#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
nomsbe073e52014-08-23 22:01:04820TEST_F(ProfileInfoCacheTest,
821 DontMigrateLegacyProfileNamesWithoutNewAvatarMenu) {
nomsbe073e52014-08-23 22:01:04822 EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles());
823
824 base::string16 name_1 = ASCIIToUTF16("Default Profile");
825 base::FilePath path_1 = GetProfilePath("path_1");
826 GetCache()->AddProfileToCache(path_1, name_1,
rogertae22659602015-05-11 21:58:21827 std::string(), base::string16(), 0,
828 std::string());
nomsbe073e52014-08-23 22:01:04829 base::string16 name_2 = ASCIIToUTF16("First user");
830 base::FilePath path_2 = GetProfilePath("path_2");
831 GetCache()->AddProfileToCache(path_2, name_2,
rogertae22659602015-05-11 21:58:21832 std::string(), base::string16(), 1,
833 std::string());
nomsbe073e52014-08-23 22:01:04834 base::string16 name_3 = ASCIIToUTF16("Lemonade");
835 base::FilePath path_3 = GetProfilePath("path_3");
836 GetCache()->AddProfileToCache(path_3, name_3,
rogertae22659602015-05-11 21:58:21837 std::string(), base::string16(), 2,
838 std::string());
nomsbe073e52014-08-23 22:01:04839 base::string16 name_4 = ASCIIToUTF16("Batman");
840 base::FilePath path_4 = GetProfilePath("path_4");
841 GetCache()->AddProfileToCache(path_4, name_4,
rogertae22659602015-05-11 21:58:21842 std::string(), base::string16(), 3,
843 std::string());
nomsbe073e52014-08-23 22:01:04844 EXPECT_EQ(4U, GetCache()->GetNumberOfProfiles());
845
846 ResetCache();
847
848 // Profile names should have been preserved.
849 EXPECT_EQ(name_1, GetCache()->GetNameOfProfileAtIndex(
850 GetCache()->GetIndexOfProfileWithPath(path_1)));
851 EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(
852 GetCache()->GetIndexOfProfileWithPath(path_2)));
853 EXPECT_EQ(name_3, GetCache()->GetNameOfProfileAtIndex(
854 GetCache()->GetIndexOfProfileWithPath(path_3)));
855 EXPECT_EQ(name_4, GetCache()->GetNameOfProfileAtIndex(
856 GetCache()->GetIndexOfProfileWithPath(path_4)));
857}
anthonyvd66aa95d2015-10-02 15:40:26858#endif