blob: b71b0626e0c920fe5d9733bd187f8a7f1d772152 [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]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"
[email protected]5cf8bcd92014-07-17 20:27:4025#include "chrome/common/chrome_switches.h"
noms693f35aa2015-02-05 16:03:1626#include "chrome/common/pref_names.h"
[email protected]583844c2011-08-27 00:38:3527#include "chrome/test/base/testing_browser_process.h"
Alexander Alekseev3f09a8f2018-05-03 02:52:1028#include "components/account_id/account_id.h"
brettwb1fc1b82016-02-02 00:19:0829#include "components/prefs/testing_pref_service.h"
Colin Blundellac55c9972018-11-07 15:30:3330#include "components/signin/core/browser/account_consistency_method.h"
maxbogueea16ff412016-10-28 16:35:2931#include "components/sync_preferences/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
Jochen Eisingerd84bb42a2018-07-23 22:39:0339#if BUILDFLAG(ENABLE_SUPERVISED_USERS)
40#include "chrome/browser/supervised_user/supervised_user_constants.h"
41#endif
42
[email protected]6778fed2013-12-24 20:09:3743using base::ASCIIToUTF16;
[email protected]f729d7a2013-12-26 07:07:5644using base::UTF8ToUTF16;
[email protected]cb114f142011-11-23 20:18:0445using content::BrowserThread;
46
[email protected]6a460662011-12-22 22:05:1647ProfileNameVerifierObserver::ProfileNameVerifierObserver(
48 TestingProfileManager* testing_profile_manager)
49 : testing_profile_manager_(testing_profile_manager) {
50 DCHECK(testing_profile_manager_);
[email protected]590e189b2011-12-13 22:07:0351}
52
53ProfileNameVerifierObserver::~ProfileNameVerifierObserver() {
54}
55
56void ProfileNameVerifierObserver::OnProfileAdded(
[email protected]650b2d52013-02-10 03:41:4557 const base::FilePath& profile_path) {
[email protected]a04db822013-12-11 19:14:4058 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1659 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0360 EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end());
61 profile_names_.insert(profile_name);
62}
63
[email protected]7b0af152011-12-16 17:02:0664void ProfileNameVerifierObserver::OnProfileWillBeRemoved(
[email protected]650b2d52013-02-10 03:41:4565 const base::FilePath& profile_path) {
[email protected]a04db822013-12-11 19:14:4066 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1667 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0368 EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end());
69 profile_names_.erase(profile_name);
70}
71
[email protected]7b0af152011-12-16 17:02:0672void ProfileNameVerifierObserver::OnProfileWasRemoved(
[email protected]650b2d52013-02-10 03:41:4573 const base::FilePath& profile_path,
[email protected]a04db822013-12-11 19:14:4074 const base::string16& profile_name) {
[email protected]7b0af152011-12-16 17:02:0675 EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end());
76}
77
[email protected]590e189b2011-12-13 22:07:0378void ProfileNameVerifierObserver::OnProfileNameChanged(
[email protected]650b2d52013-02-10 03:41:4579 const base::FilePath& profile_path,
[email protected]a04db822013-12-11 19:14:4080 const base::string16& old_profile_name) {
81 base::string16 new_profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1682 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0383 EXPECT_TRUE(profile_names_.find(old_profile_name) != profile_names_.end());
84 EXPECT_TRUE(profile_names_.find(new_profile_name) == profile_names_.end());
85 profile_names_.erase(old_profile_name);
86 profile_names_.insert(new_profile_name);
87}
88
89void ProfileNameVerifierObserver::OnProfileAvatarChanged(
[email protected]650b2d52013-02-10 03:41:4590 const base::FilePath& profile_path) {
[email protected]a04db822013-12-11 19:14:4091 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1692 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0393 EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end());
94}
95
[email protected]6a460662011-12-22 22:05:1696ProfileInfoCache* ProfileNameVerifierObserver::GetCache() {
97 return testing_profile_manager_->profile_info_cache();
98}
99
[email protected]d4f5d1162011-11-30 01:41:52100ProfileInfoCacheTest::ProfileInfoCacheTest()
[email protected]0aa018a2013-07-31 15:08:54101 : testing_profile_manager_(TestingBrowserProcess::GetGlobal()),
WC Leung6c602d42018-01-23 22:32:18102 name_observer_(&testing_profile_manager_) {}
[email protected]d4f5d1162011-11-30 01:41:52103
104ProfileInfoCacheTest::~ProfileInfoCacheTest() {
105}
106
107void ProfileInfoCacheTest::SetUp() {
108 ASSERT_TRUE(testing_profile_manager_.SetUp());
[email protected]590e189b2011-12-13 22:07:03109 testing_profile_manager_.profile_info_cache()->AddObserver(&name_observer_);
[email protected]d4f5d1162011-11-30 01:41:52110}
111
[email protected]1302dcf2011-11-30 21:47:05112void ProfileInfoCacheTest::TearDown() {
Gabriel Charette426214f2017-08-14 18:21:58113 // Drain remaining tasks to make sure all tasks are completed. This prevents
[email protected]1302dcf2011-11-30 21:47:05114 // memory leaks.
Gabriel Charette01507a22017-09-27 21:30:08115 content::RunAllTasksUntilIdle();
[email protected]1302dcf2011-11-30 21:47:05116}
117
[email protected]d4f5d1162011-11-30 01:41:52118ProfileInfoCache* ProfileInfoCacheTest::GetCache() {
119 return testing_profile_manager_.profile_info_cache();
120}
121
[email protected]650b2d52013-02-10 03:41:45122base::FilePath ProfileInfoCacheTest::GetProfilePath(
[email protected]d4f5d1162011-11-30 01:41:52123 const std::string& base_name) {
124 return testing_profile_manager_.profile_manager()->user_data_dir().
125 AppendASCII(base_name);
126}
127
128void ProfileInfoCacheTest::ResetCache() {
129 testing_profile_manager_.DeleteProfileInfoCache();
130}
131
[email protected]d4f5d1162011-11-30 01:41:52132TEST_F(ProfileInfoCacheTest, AddProfiles) {
[email protected]6730b1e2011-09-29 05:23:52133 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39134
[email protected]ca591072012-03-27 01:54:44135 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
avib896c712015-12-26 02:10:43136 for (uint32_t i = 0; i < 4; ++i) {
[email protected]7d3cbc92013-03-18 22:33:04137 base::FilePath profile_path =
138 GetProfilePath(base::StringPrintf("path_%ud", i));
[email protected]a04db822013-12-11 19:14:40139 base::string16 profile_name =
140 ASCIIToUTF16(base::StringPrintf("name_%ud", i));
[email protected]ca591072012-03-27 01:54:44141 const SkBitmap* icon = rb.GetImageNamed(
[email protected]c3e559772014-04-09 04:02:54142 profiles::GetDefaultAvatarIconResourceIDAtIndex(
[email protected]ca591072012-03-27 01:54:44143 i)).ToSkBitmap();
Jochen Eisingerd84bb42a2018-07-23 22:39:03144 std::string supervised_user_id = "";
145#if BUILDFLAG(ENABLE_SUPERVISED_USERS)
146 if (i == 3)
147 supervised_user_id = supervised_users::kChildAccountSUID;
148#endif
[email protected]dee810e2011-06-27 19:43:39149
rogertae22659602015-05-11 21:58:21150 GetCache()->AddProfileToCache(profile_path, profile_name, std::string(),
Roman Sorokin8ead4d72018-04-16 15:58:58151 base::string16(), i, supervised_user_id,
152 EmptyAccountId());
[email protected]cb114f142011-11-23 20:18:04153 GetCache()->SetBackgroundStatusOfProfileAtIndex(i, true);
[email protected]a04db822013-12-11 19:14:40154 base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
[email protected]cb114f142011-11-23 20:18:04155 GetCache()->SetGAIANameOfProfileAtIndex(i, gaia_name);
[email protected]dee810e2011-06-27 19:43:39156
[email protected]6730b1e2011-09-29 05:23:52157 EXPECT_EQ(i + 1, GetCache()->GetNumberOfProfiles());
158 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
159 EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i));
[email protected]0aa018a2013-07-31 15:08:54160 const SkBitmap* actual_icon =
161 GetCache()->GetAvatarIconOfProfileAtIndex(i).ToSkBitmap();
[email protected]ca591072012-03-27 01:54:44162 EXPECT_EQ(icon->width(), actual_icon->width());
163 EXPECT_EQ(icon->height(), actual_icon->height());
[email protected]d20d0432014-06-12 17:14:05164 EXPECT_EQ(i == 3, GetCache()->ProfileIsSupervisedAtIndex(i));
[email protected]5ddfade2014-02-03 10:24:53165 EXPECT_EQ(i == 3, GetCache()->IsOmittedProfileAtIndex(i));
[email protected]d20d0432014-06-12 17:14:05166 EXPECT_EQ(supervised_user_id,
167 GetCache()->GetSupervisedUserIdOfProfileAtIndex(i));
[email protected]dee810e2011-06-27 19:43:39168 }
[email protected]cb114f142011-11-23 20:18:04169
170 // Reset the cache and test the it reloads correctly.
171 ResetCache();
172
173 EXPECT_EQ(4u, GetCache()->GetNumberOfProfiles());
avib896c712015-12-26 02:10:43174 for (uint32_t i = 0; i < 4; ++i) {
[email protected]7d3cbc92013-03-18 22:33:04175 base::FilePath profile_path =
176 GetProfilePath(base::StringPrintf("path_%ud", i));
[email protected]cb114f142011-11-23 20:18:04177 EXPECT_EQ(i, GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]a04db822013-12-11 19:14:40178 base::string16 profile_name =
179 ASCIIToUTF16(base::StringPrintf("name_%ud", i));
[email protected]cb114f142011-11-23 20:18:04180 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
181 EXPECT_EQ(i, GetCache()->GetAvatarIconIndexOfProfileAtIndex(i));
182 EXPECT_EQ(true, GetCache()->GetBackgroundStatusOfProfileAtIndex(i));
[email protected]a04db822013-12-11 19:14:40183 base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
[email protected]cb114f142011-11-23 20:18:04184 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(i));
185 }
[email protected]dee810e2011-06-27 19:43:39186}
187
[email protected]d4f5d1162011-11-30 01:41:52188TEST_F(ProfileInfoCacheTest, DeleteProfile) {
[email protected]6730b1e2011-09-29 05:23:52189 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39190
[email protected]650b2d52013-02-10 03:41:45191 base::FilePath path_1 = GetProfilePath("path_1");
Roman Sorokin8ead4d72018-04-16 15:58:58192 GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("name_1"), std::string(),
193 base::string16(), 0, std::string(),
194 EmptyAccountId());
[email protected]6730b1e2011-09-29 05:23:52195 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39196
[email protected]650b2d52013-02-10 03:41:45197 base::FilePath path_2 = GetProfilePath("path_2");
[email protected]a04db822013-12-11 19:14:40198 base::string16 name_2 = ASCIIToUTF16("name_2");
rogertae22659602015-05-11 21:58:21199 GetCache()->AddProfileToCache(path_2, name_2, std::string(), base::string16(),
Roman Sorokin8ead4d72018-04-16 15:58:58200 0, std::string(), EmptyAccountId());
[email protected]6730b1e2011-09-29 05:23:52201 EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39202
[email protected]6730b1e2011-09-29 05:23:52203 GetCache()->DeleteProfileFromCache(path_1);
204 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
205 EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(0));
[email protected]dee810e2011-06-27 19:43:39206
[email protected]6730b1e2011-09-29 05:23:52207 GetCache()->DeleteProfileFromCache(path_2);
208 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39209}
210
[email protected]d4f5d1162011-11-30 01:41:52211TEST_F(ProfileInfoCacheTest, MutateProfile) {
212 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21213 GetProfilePath("path_1"), ASCIIToUTF16("name_1"), std::string(),
Roman Sorokin8ead4d72018-04-16 15:58:58214 base::string16(), 0, std::string(), EmptyAccountId());
[email protected]d4f5d1162011-11-30 01:41:52215 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21216 GetProfilePath("path_2"), ASCIIToUTF16("name_2"), std::string(),
Roman Sorokin8ead4d72018-04-16 15:58:58217 base::string16(), 0, std::string(), EmptyAccountId());
[email protected]dee810e2011-06-27 19:43:39218
[email protected]a04db822013-12-11 19:14:40219 base::string16 new_name = ASCIIToUTF16("new_name");
[email protected]6730b1e2011-09-29 05:23:52220 GetCache()->SetNameOfProfileAtIndex(1, new_name);
221 EXPECT_EQ(new_name, GetCache()->GetNameOfProfileAtIndex(1));
222 EXPECT_NE(new_name, GetCache()->GetNameOfProfileAtIndex(0));
[email protected]dee810e2011-06-27 19:43:39223
[email protected]a04db822013-12-11 19:14:40224 base::string16 new_user_name = ASCIIToUTF16("user_name");
rogertae22659602015-05-11 21:58:21225 std::string new_gaia_id = "12345";
226 GetCache()->SetAuthInfoOfProfileAtIndex(1, new_gaia_id, new_user_name);
[email protected]e8e78092011-09-29 18:15:38227 EXPECT_EQ(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(1));
rogertae22659602015-05-11 21:58:21228 EXPECT_EQ(new_gaia_id, GetCache()->GetGAIAIdOfProfileAtIndex(1));
[email protected]e8e78092011-09-29 18:15:38229 EXPECT_NE(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(0));
230
lixan20fabcc2017-01-31 16:03:02231 const size_t new_icon_index = 3;
[email protected]6730b1e2011-09-29 05:23:52232 GetCache()->SetAvatarIconOfProfileAtIndex(1, new_icon_index);
lixan20fabcc2017-01-31 16:03:02233 EXPECT_EQ(new_icon_index, GetCache()->GetAvatarIconIndexOfProfileAtIndex(1));
[email protected]dee810e2011-06-27 19:43:39234 // Not much to test.
[email protected]6730b1e2011-09-29 05:23:52235 GetCache()->GetAvatarIconOfProfileAtIndex(1);
lixan20fabcc2017-01-31 16:03:02236
237 const size_t wrong_icon_index = profiles::GetDefaultAvatarIconCount() + 1;
238 const size_t generic_icon_index = 0;
239 GetCache()->SetAvatarIconOfProfileAtIndex(1, wrong_icon_index);
240 EXPECT_EQ(generic_icon_index,
241 GetCache()->GetAvatarIconIndexOfProfileAtIndex(1));
[email protected]dee810e2011-06-27 19:43:39242}
243
[email protected]d4f5d1162011-11-30 01:41:52244TEST_F(ProfileInfoCacheTest, Sort) {
[email protected]a04db822013-12-11 19:14:40245 base::string16 name_a = ASCIIToUTF16("apple");
Roman Sorokin8ead4d72018-04-16 15:58:58246 GetCache()->AddProfileToCache(GetProfilePath("path_a"), name_a, std::string(),
247 base::string16(), 0, std::string(),
248 EmptyAccountId());
[email protected]cb114f142011-11-23 20:18:04249
[email protected]a04db822013-12-11 19:14:40250 base::string16 name_c = ASCIIToUTF16("cat");
Roman Sorokin8ead4d72018-04-16 15:58:58251 GetCache()->AddProfileToCache(GetProfilePath("path_c"), name_c, std::string(),
252 base::string16(), 0, std::string(),
253 EmptyAccountId());
[email protected]cb114f142011-11-23 20:18:04254
255 // Sanity check the initial order.
256 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
257 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
258
259 // Add a new profile (start with a capital to test case insensitive sorting.
[email protected]a04db822013-12-11 19:14:40260 base::string16 name_b = ASCIIToUTF16("Banana");
Roman Sorokin8ead4d72018-04-16 15:58:58261 GetCache()->AddProfileToCache(GetProfilePath("path_b"), name_b, std::string(),
262 base::string16(), 0, std::string(),
263 EmptyAccountId());
[email protected]cb114f142011-11-23 20:18:04264
265 // Verify the new order.
266 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
267 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(1));
268 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(2));
269
270 // Change the name of an existing profile.
271 name_a = UTF8ToUTF16("dog");
272 GetCache()->SetNameOfProfileAtIndex(0, name_a);
273
274 // Verify the new order.
275 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
276 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
277 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(2));
278
279 // Delete a profile.
[email protected]d4f5d1162011-11-30 01:41:52280 GetCache()->DeleteProfileFromCache(GetProfilePath("path_c"));
[email protected]cb114f142011-11-23 20:18:04281
282 // Verify the new order.
283 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
284 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(1));
285}
286
WC Leung17756382017-08-25 04:57:36287// Will be removed SOON with ProfileInfoCache tests.
[email protected]d4f5d1162011-11-30 01:41:52288TEST_F(ProfileInfoCacheTest, BackgroundModeStatus) {
[email protected]279170832011-10-12 23:38:03289 GetCache()->AddProfileToCache(
Roman Sorokin8ead4d72018-04-16 15:58:58290 GetProfilePath("path_1"), ASCIIToUTF16("name_1"), std::string(),
291 base::string16(), 0, std::string(), EmptyAccountId());
[email protected]279170832011-10-12 23:38:03292 GetCache()->AddProfileToCache(
Roman Sorokin8ead4d72018-04-16 15:58:58293 GetProfilePath("path_2"), ASCIIToUTF16("name_2"), std::string(),
294 base::string16(), 0, std::string(), EmptyAccountId());
[email protected]279170832011-10-12 23:38:03295
296 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
297 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
298
299 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, true);
300
301 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
302 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
303
304 GetCache()->SetBackgroundStatusOfProfileAtIndex(0, true);
305
306 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
307 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
308
309 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, false);
310
311 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
312 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
313}
314
[email protected]d4f5d1162011-11-30 01:41:52315TEST_F(ProfileInfoCacheTest, GAIAName) {
[email protected]cb114f142011-11-23 20:18:04316 GetCache()->AddProfileToCache(
Roman Sorokin8ead4d72018-04-16 15:58:58317 GetProfilePath("path_1"), ASCIIToUTF16("Person 1"), std::string(),
318 base::string16(), 0, std::string(), EmptyAccountId());
[email protected]a4352d02014-03-06 06:00:27319 base::string16 profile_name(ASCIIToUTF16("Person 2"));
Roman Sorokin8ead4d72018-04-16 15:58:58320 GetCache()->AddProfileToCache(GetProfilePath("path_2"), profile_name,
321 std::string(), base::string16(), 0,
322 std::string(), EmptyAccountId());
[email protected]cb114f142011-11-23 20:18:04323
[email protected]a4352d02014-03-06 06:00:27324 int index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
325 int index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
326
[email protected]cb114f142011-11-23 20:18:04327 // Sanity check.
[email protected]a4352d02014-03-06 06:00:27328 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty());
329 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index2).empty());
[email protected]cb114f142011-11-23 20:18:04330
[email protected]425d6ab2014-03-25 05:59:39331 // Set GAIA name. This re-sorts the cache.
[email protected]a04db822013-12-11 19:14:40332 base::string16 gaia_name(ASCIIToUTF16("Pat Smith"));
[email protected]a4352d02014-03-06 06:00:27333 GetCache()->SetGAIANameOfProfileAtIndex(index2, gaia_name);
[email protected]425d6ab2014-03-25 05:59:39334 index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
335 index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
336
337 // Since there is a GAIA name, we use that as a display name.
[email protected]a4352d02014-03-06 06:00:27338 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty());
339 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2));
[email protected]a4352d02014-03-06 06:00:27340 EXPECT_EQ(gaia_name, GetCache()->GetNameOfProfileAtIndex(index2));
[email protected]cb114f142011-11-23 20:18:04341
[email protected]a4352d02014-03-06 06:00:27342 // Don't use GAIA name as profile name. This re-sorts the cache.
[email protected]425d6ab2014-03-25 05:59:39343 base::string16 custom_name(ASCIIToUTF16("Custom name"));
344 GetCache()->SetNameOfProfileAtIndex(index2, custom_name);
[email protected]1cdf0932014-08-16 06:00:49345 GetCache()->SetProfileIsUsingDefaultNameAtIndex(index2, false);
346
[email protected]a4352d02014-03-06 06:00:27347 index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
348 index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
349
[email protected]425d6ab2014-03-25 05:59:39350 EXPECT_EQ(custom_name, GetCache()->GetNameOfProfileAtIndex(index2));
[email protected]a4352d02014-03-06 06:00:27351 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2));
[email protected]cb114f142011-11-23 20:18:04352}
353
[email protected]d4f5d1162011-11-30 01:41:52354TEST_F(ProfileInfoCacheTest, GAIAPicture) {
[email protected]22900902014-07-30 11:50:41355 const int kDefaultAvatarIndex = 0;
356 const int kOtherAvatarIndex = 1;
[email protected]979dc4f2014-08-12 22:52:11357 const int kGaiaPictureSize = 256; // Standard size of a Gaia account picture.
[email protected]cb114f142011-11-23 20:18:04358 GetCache()->AddProfileToCache(
Roman Sorokin8ead4d72018-04-16 15:58:58359 GetProfilePath("path_1"), ASCIIToUTF16("name_1"), std::string(),
360 base::string16(), kDefaultAvatarIndex, std::string(), EmptyAccountId());
[email protected]cb114f142011-11-23 20:18:04361 GetCache()->AddProfileToCache(
Roman Sorokin8ead4d72018-04-16 15:58:58362 GetProfilePath("path_2"), ASCIIToUTF16("name_2"), std::string(),
363 base::string16(), kDefaultAvatarIndex, std::string(), EmptyAccountId());
[email protected]cb114f142011-11-23 20:18:04364
365 // Sanity check.
[email protected]2f3c00f2011-11-30 04:36:22366 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
367 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(1));
[email protected]cb114f142011-11-23 20:18:04368 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0));
369 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
370
371 // The profile icon should be the default one.
[email protected]22900902014-07-30 11:50:41372 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(0));
373 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
374 int default_avatar_id =
375 profiles::GetDefaultAvatarIconResourceIDAtIndex(kDefaultAvatarIndex);
376 const gfx::Image& default_avatar_image(
Lei Zhang7640d542017-10-03 16:26:49377 ui::ResourceBundle::GetSharedInstance().GetImageNamed(default_avatar_id));
pkotwiczb312b512015-09-29 23:44:43378 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]22900902014-07-30 11:50:41379 default_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
[email protected]cb114f142011-11-23 20:18:04380
381 // Set GAIA picture.
[email protected]979dc4f2014-08-12 22:52:11382 gfx::Image gaia_image(gfx::test::CreateImage(
383 kGaiaPictureSize, kGaiaPictureSize));
[email protected]2f3c00f2011-11-30 04:36:22384 GetCache()->SetGAIAPictureOfProfileAtIndex(1, &gaia_image);
385 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
pkotwiczb312b512015-09-29 23:44:43386 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22387 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
[email protected]22900902014-07-30 11:50:41388 // Since we're still using the default avatar, the GAIA image should be
389 // preferred over the generic avatar image.
390 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
391 EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
pkotwiczb312b512015-09-29 23:44:43392 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]22900902014-07-30 11:50:41393 gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
[email protected]cb114f142011-11-23 20:18:04394
[email protected]1cdf0932014-08-16 06:00:49395 // Set a non-default avatar. This should be preferred over the GAIA image.
[email protected]22900902014-07-30 11:50:41396 GetCache()->SetAvatarIconOfProfileAtIndex(1, kOtherAvatarIndex);
[email protected]1cdf0932014-08-16 06:00:49397 GetCache()->SetProfileIsUsingDefaultAvatarAtIndex(1, false);
[email protected]22900902014-07-30 11:50:41398 EXPECT_FALSE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
399 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
400 int other_avatar_id =
401 profiles::GetDefaultAvatarIconResourceIDAtIndex(kOtherAvatarIndex);
402 const gfx::Image& other_avatar_image(
Lei Zhang7640d542017-10-03 16:26:49403 ui::ResourceBundle::GetSharedInstance().GetImageNamed(other_avatar_id));
pkotwiczb312b512015-09-29 23:44:43404 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]22900902014-07-30 11:50:41405 other_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
406
407 // Explicitly setting the GAIA picture should make it preferred again.
[email protected]cb114f142011-11-23 20:18:04408 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, true);
[email protected]22900902014-07-30 11:50:41409 EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
pkotwiczb312b512015-09-29 23:44:43410 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22411 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
pkotwiczb312b512015-09-29 23:44:43412 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]cb114f142011-11-23 20:18:04413 gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
414
[email protected]22900902014-07-30 11:50:41415 // Clearing the IsUsingGAIAPicture flag should result in the generic image
416 // being used again.
[email protected]cb114f142011-11-23 20:18:04417 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, false);
[email protected]22900902014-07-30 11:50:41418 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
pkotwiczb312b512015-09-29 23:44:43419 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22420 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
pkotwiczb312b512015-09-29 23:44:43421 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]22900902014-07-30 11:50:41422 other_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
[email protected]cb114f142011-11-23 20:18:04423}
424
[email protected]754bebc2011-12-01 16:42:16425TEST_F(ProfileInfoCacheTest, PersistGAIAPicture) {
[email protected]cb114f142011-11-23 20:18:04426 GetCache()->AddProfileToCache(
Roman Sorokin8ead4d72018-04-16 15:58:58427 GetProfilePath("path_1"), ASCIIToUTF16("name_1"), std::string(),
428 base::string16(), 0, std::string(), EmptyAccountId());
[email protected]d4f5d1162011-11-30 01:41:52429 gfx::Image gaia_image(gfx::test::CreateImage());
[email protected]cb114f142011-11-23 20:18:04430
[email protected]2f3c00f2011-11-30 04:36:22431 GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_image);
noms75ca0192015-02-26 03:14:30432
433 // Make sure everything has completed, and the file has been written to disk.
Gabriel Charette01507a22017-09-27 21:30:08434 content::RunAllTasksUntilIdle();
noms75ca0192015-02-26 03:14:30435
pkotwiczb312b512015-09-29 23:44:43436 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22437 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
[email protected]cb114f142011-11-23 20:18:04438
[email protected]cb114f142011-11-23 20:18:04439 ResetCache();
[email protected]cb114f142011-11-23 20:18:04440 // Try to get the GAIA picture. This should return NULL until the read from
441 // disk is done.
[email protected]2f3c00f2011-11-30 04:36:22442 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
Gabriel Charette01507a22017-09-27 21:30:08443 content::RunAllTasksUntilIdle();
noms75ca0192015-02-26 03:14:30444
pkotwiczb312b512015-09-29 23:44:43445 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22446 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
447}
448
Jochen Eisingerd84bb42a2018-07-23 22:39:03449#if BUILDFLAG(ENABLE_SUPERVISED_USERS)
[email protected]d20d0432014-06-12 17:14:05450TEST_F(ProfileInfoCacheTest, SetSupervisedUserId) {
Roman Sorokin8ead4d72018-04-16 15:58:58451 GetCache()->AddProfileToCache(GetProfilePath("test"), ASCIIToUTF16("Test"),
452 std::string(), base::string16(), 0,
453 std::string(), EmptyAccountId());
[email protected]d20d0432014-06-12 17:14:05454 EXPECT_FALSE(GetCache()->ProfileIsSupervisedAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54455
Jochen Eisingerd84bb42a2018-07-23 22:39:03456 GetCache()->SetSupervisedUserIdOfProfileAtIndex(
457 0, supervised_users::kChildAccountSUID);
[email protected]d20d0432014-06-12 17:14:05458 EXPECT_TRUE(GetCache()->ProfileIsSupervisedAtIndex(0));
Jochen Eisingerd84bb42a2018-07-23 22:39:03459 EXPECT_EQ(supervised_users::kChildAccountSUID,
460 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}
Jochen Eisingerd84bb42a2018-07-23 22:39:03469#endif // BUILDFLAG(ENABLE_SUPERVISED_USERS)
[email protected]0aa018a2013-07-31 15:08:54470
[email protected]2f3c00f2011-11-30 04:36:22471TEST_F(ProfileInfoCacheTest, EmptyGAIAInfo) {
[email protected]a04db822013-12-11 19:14:40472 base::string16 profile_name = ASCIIToUTF16("name_1");
[email protected]c3e559772014-04-09 04:02:54473 int id = profiles::GetDefaultAvatarIconResourceIDAtIndex(0);
[email protected]2f3c00f2011-11-30 04:36:22474 const gfx::Image& profile_image(
Lei Zhang7640d542017-10-03 16:26:49475 ui::ResourceBundle::GetSharedInstance().GetImageNamed(id));
[email protected]2f3c00f2011-11-30 04:36:22476
Roman Sorokin8ead4d72018-04-16 15:58:58477 GetCache()->AddProfileToCache(GetProfilePath("path_1"), profile_name,
478 std::string(), base::string16(), 0,
479 std::string(), EmptyAccountId());
[email protected]2f3c00f2011-11-30 04:36:22480
481 // Set empty GAIA info.
[email protected]a04db822013-12-11 19:14:40482 GetCache()->SetGAIANameOfProfileAtIndex(0, base::string16());
[email protected]2f3c00f2011-11-30 04:36:22483 GetCache()->SetGAIAPictureOfProfileAtIndex(0, NULL);
[email protected]2f3c00f2011-11-30 04:36:22484 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(0, true);
485
486 // Verify that the profile name and picture are not empty.
487 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(0));
pkotwiczb312b512015-09-29 23:44:43488 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22489 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(0)));
[email protected]cb114f142011-11-23 20:18:04490}
491
Jochen Eisingerd84bb42a2018-07-23 22:39:03492#if BUILDFLAG(ENABLE_SUPERVISED_USERS)
[email protected]d20d0432014-06-12 17:14:05493TEST_F(ProfileInfoCacheTest, CreateSupervisedTestingProfile) {
[email protected]0aa018a2013-07-31 15:08:54494 testing_profile_manager_.CreateTestingProfile("default");
[email protected]d20d0432014-06-12 17:14:05495 base::string16 supervised_user_name = ASCIIToUTF16("Supervised User");
[email protected]0aa018a2013-07-31 15:08:54496 testing_profile_manager_.CreateTestingProfile(
maxbogueea16ff412016-10-28 16:35:29497 "test1", std::unique_ptr<sync_preferences::PrefServiceSyncable>(),
Jochen Eisingerd84bb42a2018-07-23 22:39:03498 supervised_user_name, 0, supervised_users::kChildAccountSUID,
499 TestingProfile::TestingFactories());
[email protected]0aa018a2013-07-31 15:08:54500 for (size_t i = 0; i < GetCache()->GetNumberOfProfiles(); i++) {
[email protected]d20d0432014-06-12 17:14:05501 bool is_supervised =
502 GetCache()->GetNameOfProfileAtIndex(i) == supervised_user_name;
503 EXPECT_EQ(is_supervised, GetCache()->ProfileIsSupervisedAtIndex(i));
Jochen Eisingerd84bb42a2018-07-23 22:39:03504 std::string supervised_user_id =
505 is_supervised ? supervised_users::kChildAccountSUID : "";
[email protected]d20d0432014-06-12 17:14:05506 EXPECT_EQ(supervised_user_id,
507 GetCache()->GetSupervisedUserIdOfProfileAtIndex(i));
[email protected]0aa018a2013-07-31 15:08:54508 }
[email protected]371662e372013-10-17 22:05:22509
[email protected]d20d0432014-06-12 17:14:05510 // Supervised profiles have a custom theme, which needs to be deleted on the
511 // FILE thread. Reset the profile manager now so everything is deleted while
512 // we still have a FILE thread.
[email protected]371662e372013-10-17 22:05:22513 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
[email protected]0aa018a2013-07-31 15:08:54514}
Jochen Eisingerd84bb42a2018-07-23 22:39:03515#endif
[email protected]0aa018a2013-07-31 15:08:54516
[email protected]047481802013-09-16 22:26:38517TEST_F(ProfileInfoCacheTest, AddStubProfile) {
518 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
519
520 // Add some profiles with and without a '.' in their paths.
521 const struct {
522 const char* profile_path;
523 const char* profile_name;
524 } kTestCases[] = {
525 { "path.test0", "name_0" },
526 { "path_test1", "name_1" },
527 { "path.test2", "name_2" },
528 { "path_test3", "name_3" },
529 };
530
viettrungluu9e65ad12014-10-16 04:22:26531 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
[email protected]047481802013-09-16 22:26:38532 base::FilePath profile_path = GetProfilePath(kTestCases[i].profile_path);
[email protected]a04db822013-12-11 19:14:40533 base::string16 profile_name = ASCIIToUTF16(kTestCases[i].profile_name);
[email protected]047481802013-09-16 22:26:38534
rogertae22659602015-05-11 21:58:21535 GetCache()->AddProfileToCache(profile_path, profile_name, std::string(),
Roman Sorokin8ead4d72018-04-16 15:58:58536 base::string16(), i, "", EmptyAccountId());
[email protected]047481802013-09-16 22:26:38537
538 EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i));
539 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
540 }
541
542 ASSERT_EQ(4U, GetCache()->GetNumberOfProfiles());
543
544 // Check that the profiles can be extracted from the local state.
noms693f35aa2015-02-05 16:03:16545 std::vector<base::string16> names;
546 PrefService* local_state = g_browser_process->local_state();
547 const base::DictionaryValue* cache = local_state->GetDictionary(
548 prefs::kProfileInfoCache);
549 base::string16 name;
550 for (base::DictionaryValue::Iterator it(*cache); !it.IsAtEnd();
551 it.Advance()) {
552 const base::DictionaryValue* info = NULL;
553 it.value().GetAsDictionary(&info);
554 info->GetString("name", &name);
555 names.push_back(name);
556 }
557
[email protected]047481802013-09-16 22:26:38558 for (size_t i = 0; i < 4; i++)
559 ASSERT_FALSE(names[i].empty());
560}
561
lwchkg778965f2016-05-12 13:14:22562TEST_F(ProfileInfoCacheTest, EntriesInAttributesStorage) {
563 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
564
565 // Add some profiles with and without a '.' in their paths.
566 const struct {
567 const char* profile_path;
568 const char* profile_name;
569 } kTestCases[] = {
570 { "path.test0", "name_0" },
571 { "path_test1", "name_1" },
572 { "path.test2", "name_2" },
573 { "path_test3", "name_3" },
574 };
575
576 // Profiles are added and removed using all combinations of the old and the
577 // new interfaces. The content of |profile_attributes_entries_| in
578 // ProfileAttributesStorage is checked after each insert and delete operation.
579
580 // Add profiles.
581 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
582 base::FilePath profile_path = GetProfilePath(kTestCases[i].profile_path);
583 base::string16 profile_name = ASCIIToUTF16(kTestCases[i].profile_name);
584
585 ASSERT_EQ(0u, GetCache()->profile_attributes_entries_.count(
586 profile_path.value()));
587
588 // Use ProfileInfoCache in profiles 0 and 2, and ProfileAttributesStorage in
589 // profiles 1 and 3.
590 if (i | 1u) {
591 GetCache()->AddProfileToCache(profile_path, profile_name, std::string(),
Roman Sorokin8ead4d72018-04-16 15:58:58592 base::string16(), i, "", EmptyAccountId());
lwchkg778965f2016-05-12 13:14:22593 } else {
594 GetCache()->AddProfile(profile_path, profile_name, std::string(),
Roman Sorokin8ead4d72018-04-16 15:58:58595 base::string16(), i, "", EmptyAccountId());
lwchkg778965f2016-05-12 13:14:22596 }
597
598 ASSERT_EQ(i + 1, GetCache()->GetNumberOfProfiles());
599 ASSERT_EQ(i + 1, GetCache()->profile_attributes_entries_.size());
600
601 ASSERT_EQ(1u, GetCache()->profile_attributes_entries_.count(
602 profile_path.value()));
603 // TODO(anthonyvd) : check that the entry in |profile_attributes_entries_|
604 // is null before GetProfileAttributesWithPath is run. Currently this is
605 // impossible to check because GetProfileAttributesWithPath is called during
606 // profile creation.
607
608 ProfileAttributesEntry* entry = nullptr;
609 GetCache()->GetProfileAttributesWithPath(profile_path, &entry);
610 EXPECT_EQ(
611 entry,
612 GetCache()->profile_attributes_entries_[profile_path.value()].get());
613 }
614
615 // Remove profiles.
616 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
617 base::FilePath profile_path = GetProfilePath(kTestCases[i].profile_path);
618 ASSERT_EQ(1u, GetCache()->profile_attributes_entries_.count(
619 profile_path.value()));
620
621 // Use ProfileInfoCache in profiles 0 and 1, and ProfileAttributesStorage in
622 // profiles 2 and 3.
623 if (i | 2u)
624 GetCache()->DeleteProfileFromCache(profile_path);
625 else
626 GetCache()->RemoveProfile(profile_path);
627
628 ASSERT_EQ(0u, GetCache()->profile_attributes_entries_.count(
629 profile_path.value()));
630
631 ProfileAttributesEntry* entry = nullptr;
632 EXPECT_FALSE(GetCache()->GetProfileAttributesWithPath(profile_path,
633 &entry));
634 ASSERT_EQ(0u, GetCache()->profile_attributes_entries_.count(
635 profile_path.value()));
636 }
637}
638
jam1c5a91492016-02-24 20:47:53639#if !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
nomsbe073e52014-08-23 22:01:04640TEST_F(ProfileInfoCacheTest, MigrateLegacyProfileNamesWithNewAvatarMenu) {
nomsbe073e52014-08-23 22:01:04641 EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles());
642
643 base::FilePath path_1 = GetProfilePath("path_1");
644 GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("Default Profile"),
rogertae22659602015-05-11 21:58:21645 std::string(), base::string16(), 0,
Roman Sorokin8ead4d72018-04-16 15:58:58646 std::string(), EmptyAccountId());
nomsbe073e52014-08-23 22:01:04647 base::FilePath path_2 = GetProfilePath("path_2");
648 GetCache()->AddProfileToCache(path_2, ASCIIToUTF16("First user"),
rogertae22659602015-05-11 21:58:21649 std::string(), base::string16(), 1,
Roman Sorokin8ead4d72018-04-16 15:58:58650 std::string(), EmptyAccountId());
nomsbe073e52014-08-23 22:01:04651 base::string16 name_3 = ASCIIToUTF16("Lemonade");
652 base::FilePath path_3 = GetProfilePath("path_3");
Roman Sorokin8ead4d72018-04-16 15:58:58653 GetCache()->AddProfileToCache(path_3, name_3, std::string(), base::string16(),
654 2, std::string(), EmptyAccountId());
nomsbe073e52014-08-23 22:01:04655 base::string16 name_4 = ASCIIToUTF16("Batman");
656 base::FilePath path_4 = GetProfilePath("path_4");
Roman Sorokin8ead4d72018-04-16 15:58:58657 GetCache()->AddProfileToCache(path_4, name_4, std::string(), base::string16(),
658 3, std::string(), EmptyAccountId());
nomsbe073e52014-08-23 22:01:04659 base::string16 name_5 = ASCIIToUTF16("Person 2");
660 base::FilePath path_5 = GetProfilePath("path_5");
Roman Sorokin8ead4d72018-04-16 15:58:58661 GetCache()->AddProfileToCache(path_5, name_5, std::string(), base::string16(),
662 2, std::string(), EmptyAccountId());
nomsbe073e52014-08-23 22:01:04663
664 EXPECT_EQ(5U, GetCache()->GetNumberOfProfiles());
665
666
667 ResetCache();
668
669 // Legacy profile names like "Default Profile" and "First user" should be
WC Leung0ea09a642017-09-02 01:46:15670 // migrated to "Person %n" type names, i.e. any permutation of "Person 1" and
671 // "Person 3".
672 if (ASCIIToUTF16("Person 1") ==
673 GetCache()->GetNameOfProfileAtIndex(
674 GetCache()->GetIndexOfProfileWithPath(path_1))) {
675 EXPECT_EQ(ASCIIToUTF16("Person 3"),
676 GetCache()->GetNameOfProfileAtIndex(
677 GetCache()->GetIndexOfProfileWithPath(path_2)));
678 } else {
679 EXPECT_EQ(ASCIIToUTF16("Person 3"),
680 GetCache()->GetNameOfProfileAtIndex(
681 GetCache()->GetIndexOfProfileWithPath(path_1)));
682 EXPECT_EQ(ASCIIToUTF16("Person 1"),
683 GetCache()->GetNameOfProfileAtIndex(
684 GetCache()->GetIndexOfProfileWithPath(path_2)));
685 }
nomsbe073e52014-08-23 22:01:04686
687 // Other profile names should not be migrated even if they're the old
688 // default cartoon profile names.
689 EXPECT_EQ(name_3, GetCache()->GetNameOfProfileAtIndex(
690 GetCache()->GetIndexOfProfileWithPath(path_3)));
691 EXPECT_EQ(name_4, GetCache()->GetNameOfProfileAtIndex(
692 GetCache()->GetIndexOfProfileWithPath(path_4)));
693 EXPECT_EQ(name_5, GetCache()->GetNameOfProfileAtIndex(
694 GetCache()->GetIndexOfProfileWithPath(path_5)));
695}
Xi Cheng1b761382017-08-15 20:37:34696
697TEST_F(ProfileInfoCacheTest, GetGaiaImageForAvatarMenu) {
698 // The TestingProfileManager's ProfileInfoCache doesn't download avatars.
699 ProfileInfoCache profile_info_cache(
700 g_browser_process->local_state(),
701 testing_profile_manager_.profile_manager()->user_data_dir());
702
703 base::FilePath profile_path = GetProfilePath("path_1");
704
705 GetCache()->AddProfileToCache(profile_path, ASCIIToUTF16("name_1"),
706 std::string(), base::string16(), 0,
Roman Sorokin8ead4d72018-04-16 15:58:58707 std::string(), EmptyAccountId());
Xi Cheng1b761382017-08-15 20:37:34708
709 gfx::Image gaia_image(gfx::test::CreateImage());
710 GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_image);
711
712 // Make sure everything has completed, and the file has been written to disk.
Gabriel Charette01507a22017-09-27 21:30:08713 content::RunAllTasksUntilIdle();
Xi Cheng1b761382017-08-15 20:37:34714
715 // Make sure this profile is using GAIA picture.
716 EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0));
717
718 ResetCache();
719
720 // We need to explicitly set the GAIA usage flag after resetting the cache.
721 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(0, true);
722 EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0));
723
724 gfx::Image image_loaded;
725
726 // Try to get the GAIA image. For the first time, it triggers an async image
727 // load from disk. The load status indicates the image is still being loaded.
728 EXPECT_EQ(AvatarMenu::ImageLoadStatus::LOADING,
729 AvatarMenu::GetImageForMenuButton(profile_path, &image_loaded));
730 EXPECT_FALSE(gfx::test::AreImagesEqual(gaia_image, image_loaded));
731
732 // Wait until the async image load finishes.
Gabriel Charette01507a22017-09-27 21:30:08733 content::RunAllTasksUntilIdle();
Xi Cheng1b761382017-08-15 20:37:34734
735 // Since the GAIA image is loaded now, we can get it this time.
736 EXPECT_EQ(AvatarMenu::ImageLoadStatus::LOADED,
737 AvatarMenu::GetImageForMenuButton(profile_path, &image_loaded));
738 EXPECT_TRUE(gfx::test::AreImagesEqual(gaia_image, image_loaded));
739}
[email protected]b09bfa12014-08-15 19:05:25740#endif
nomsbe073e52014-08-23 22:01:04741
jam1c5a91492016-02-24 20:47:53742#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
nomsbe073e52014-08-23 22:01:04743TEST_F(ProfileInfoCacheTest,
744 DontMigrateLegacyProfileNamesWithoutNewAvatarMenu) {
745 EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles());
746
747 base::string16 name_1 = ASCIIToUTF16("Default Profile");
748 base::FilePath path_1 = GetProfilePath("path_1");
Roman Sorokin8ead4d72018-04-16 15:58:58749 GetCache()->AddProfileToCache(path_1, name_1, std::string(), base::string16(),
750 0, std::string(), EmptyAccountId());
nomsbe073e52014-08-23 22:01:04751 base::string16 name_2 = ASCIIToUTF16("First user");
752 base::FilePath path_2 = GetProfilePath("path_2");
Roman Sorokin8ead4d72018-04-16 15:58:58753 GetCache()->AddProfileToCache(path_2, name_2, std::string(), base::string16(),
754 1, std::string(), EmptyAccountId());
nomsbe073e52014-08-23 22:01:04755 base::string16 name_3 = ASCIIToUTF16("Lemonade");
756 base::FilePath path_3 = GetProfilePath("path_3");
Roman Sorokin8ead4d72018-04-16 15:58:58757 GetCache()->AddProfileToCache(path_3, name_3, std::string(), base::string16(),
758 2, std::string(), EmptyAccountId());
nomsbe073e52014-08-23 22:01:04759 base::string16 name_4 = ASCIIToUTF16("Batman");
760 base::FilePath path_4 = GetProfilePath("path_4");
Roman Sorokin8ead4d72018-04-16 15:58:58761 GetCache()->AddProfileToCache(path_4, name_4, std::string(), base::string16(),
762 3, std::string(), EmptyAccountId());
nomsbe073e52014-08-23 22:01:04763 EXPECT_EQ(4U, GetCache()->GetNumberOfProfiles());
764
765 ResetCache();
766
767 // Profile names should have been preserved.
768 EXPECT_EQ(name_1, GetCache()->GetNameOfProfileAtIndex(
769 GetCache()->GetIndexOfProfileWithPath(path_1)));
770 EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(
771 GetCache()->GetIndexOfProfileWithPath(path_2)));
772 EXPECT_EQ(name_3, GetCache()->GetNameOfProfileAtIndex(
773 GetCache()->GetIndexOfProfileWithPath(path_3)));
774 EXPECT_EQ(name_4, GetCache()->GetNameOfProfileAtIndex(
775 GetCache()->GetIndexOfProfileWithPath(path_4)));
776}
anthonyvd66aa95d2015-10-02 15:40:26777#endif
Alexander Alekseev4084f502018-02-20 22:16:46778
779TEST_F(ProfileInfoCacheTest, RemoveProfileByAccountId) {
780 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
781
782 base::FilePath path_1 = GetProfilePath("path_1");
783 const AccountId account_id_1(
784 AccountId::FromUserEmailGaiaId("email1", "111111"));
785 base::string16 name_1 = ASCIIToUTF16("name_1");
786 GetCache()->AddProfileToCache(path_1, name_1, account_id_1.GetGaiaId(),
787 UTF8ToUTF16(account_id_1.GetUserEmail()), 0,
Roman Sorokin8ead4d72018-04-16 15:58:58788 std::string(), EmptyAccountId());
Alexander Alekseev4084f502018-02-20 22:16:46789 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
790
791 base::FilePath path_2 = GetProfilePath("path_2");
792 base::string16 name_2 = ASCIIToUTF16("name_2");
793 const AccountId account_id_2(
794 AccountId::FromUserEmailGaiaId("email2", "222222"));
795 GetCache()->AddProfileToCache(path_2, name_2, account_id_2.GetGaiaId(),
796 UTF8ToUTF16(account_id_2.GetUserEmail()), 0,
Roman Sorokin8ead4d72018-04-16 15:58:58797 std::string(), EmptyAccountId());
Alexander Alekseev4084f502018-02-20 22:16:46798 EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles());
799
800 base::FilePath path_3 = GetProfilePath("path_3");
801 base::string16 name_3 = ASCIIToUTF16("name_3");
802 const AccountId account_id_3(
803 AccountId::FromUserEmailGaiaId("email3", "333333"));
804 GetCache()->AddProfileToCache(path_3, name_3, account_id_3.GetGaiaId(),
805 UTF8ToUTF16(account_id_3.GetUserEmail()), 0,
Roman Sorokin8ead4d72018-04-16 15:58:58806 std::string(), EmptyAccountId());
Alexander Alekseev4084f502018-02-20 22:16:46807 EXPECT_EQ(3u, GetCache()->GetNumberOfProfiles());
808
809 base::FilePath path_4 = GetProfilePath("path_4");
810 base::string16 name_4 = ASCIIToUTF16("name_4");
811 const AccountId account_id_4(
812 AccountId::FromUserEmailGaiaId("email4", "444444"));
813 GetCache()->AddProfileToCache(path_4, name_4, account_id_4.GetGaiaId(),
814 UTF8ToUTF16(account_id_4.GetUserEmail()), 0,
Roman Sorokin8ead4d72018-04-16 15:58:58815 std::string(), EmptyAccountId());
Alexander Alekseev4084f502018-02-20 22:16:46816 EXPECT_EQ(4u, GetCache()->GetNumberOfProfiles());
817
818 GetCache()->RemoveProfileByAccountId(account_id_3);
819 EXPECT_EQ(3u, GetCache()->GetNumberOfProfiles());
820 EXPECT_EQ(name_1, GetCache()->GetNameOfProfileAtIndex(0));
821
822 GetCache()->RemoveProfileByAccountId(account_id_1);
823 EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles());
824 EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(0));
825
826 // this profile is already deleted.
827 GetCache()->RemoveProfileByAccountId(account_id_3);
828 EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles());
829 EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(0));
830
831 // Remove profile by partial match
832 GetCache()->RemoveProfileByAccountId(
833 AccountId::FromUserEmail(account_id_2.GetUserEmail()));
834 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
835 EXPECT_EQ(name_4, GetCache()->GetNameOfProfileAtIndex(0));
836
837 // Remove last profile
838 GetCache()->RemoveProfileByAccountId(account_id_4);
839 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
840}