blob: 47b7ca0c53d9705249003061014672563dfb69fa [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"
Avi Drissman5f0fb8c2018-12-25 23:20:4915#include "base/stl_util.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());
Jacques Chencf4971d2019-06-25 15:16:29134 // Avatar icons not used on Android.
135#if !defined(OS_ANDROID)
[email protected]ca591072012-03-27 01:54:44136 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
Jacques Chencf4971d2019-06-25 15:16:29137#endif
138
avib896c712015-12-26 02:10:43139 for (uint32_t i = 0; i < 4; ++i) {
[email protected]7d3cbc92013-03-18 22:33:04140 base::FilePath profile_path =
141 GetProfilePath(base::StringPrintf("path_%ud", i));
[email protected]a04db822013-12-11 19:14:40142 base::string16 profile_name =
143 ASCIIToUTF16(base::StringPrintf("name_%ud", i));
Jacques Chencf4971d2019-06-25 15:16:29144#if !defined(OS_ANDROID)
[email protected]ca591072012-03-27 01:54:44145 const SkBitmap* icon = rb.GetImageNamed(
[email protected]c3e559772014-04-09 04:02:54146 profiles::GetDefaultAvatarIconResourceIDAtIndex(
[email protected]ca591072012-03-27 01:54:44147 i)).ToSkBitmap();
Jacques Chencf4971d2019-06-25 15:16:29148#endif
Jochen Eisingerd84bb42a2018-07-23 22:39:03149 std::string supervised_user_id = "";
150#if BUILDFLAG(ENABLE_SUPERVISED_USERS)
151 if (i == 3)
152 supervised_user_id = supervised_users::kChildAccountSUID;
153#endif
[email protected]dee810e2011-06-27 19:43:39154
rogertae22659602015-05-11 21:58:21155 GetCache()->AddProfileToCache(profile_path, profile_name, std::string(),
Roman Sorokin8ead4d72018-04-16 15:58:58156 base::string16(), i, supervised_user_id,
157 EmptyAccountId());
[email protected]cb114f142011-11-23 20:18:04158 GetCache()->SetBackgroundStatusOfProfileAtIndex(i, true);
[email protected]a04db822013-12-11 19:14:40159 base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
[email protected]cb114f142011-11-23 20:18:04160 GetCache()->SetGAIANameOfProfileAtIndex(i, gaia_name);
[email protected]dee810e2011-06-27 19:43:39161
[email protected]6730b1e2011-09-29 05:23:52162 EXPECT_EQ(i + 1, GetCache()->GetNumberOfProfiles());
163 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
164 EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i));
Jacques Chencf4971d2019-06-25 15:16:29165#if !defined(OS_ANDROID)
[email protected]0aa018a2013-07-31 15:08:54166 const SkBitmap* actual_icon =
167 GetCache()->GetAvatarIconOfProfileAtIndex(i).ToSkBitmap();
[email protected]ca591072012-03-27 01:54:44168 EXPECT_EQ(icon->width(), actual_icon->width());
169 EXPECT_EQ(icon->height(), actual_icon->height());
Jacques Chencf4971d2019-06-25 15:16:29170#endif
[email protected]d20d0432014-06-12 17:14:05171 EXPECT_EQ(i == 3, GetCache()->ProfileIsSupervisedAtIndex(i));
[email protected]5ddfade2014-02-03 10:24:53172 EXPECT_EQ(i == 3, GetCache()->IsOmittedProfileAtIndex(i));
[email protected]d20d0432014-06-12 17:14:05173 EXPECT_EQ(supervised_user_id,
174 GetCache()->GetSupervisedUserIdOfProfileAtIndex(i));
[email protected]dee810e2011-06-27 19:43:39175 }
[email protected]cb114f142011-11-23 20:18:04176
177 // Reset the cache and test the it reloads correctly.
178 ResetCache();
179
180 EXPECT_EQ(4u, GetCache()->GetNumberOfProfiles());
avib896c712015-12-26 02:10:43181 for (uint32_t i = 0; i < 4; ++i) {
[email protected]7d3cbc92013-03-18 22:33:04182 base::FilePath profile_path =
183 GetProfilePath(base::StringPrintf("path_%ud", i));
[email protected]cb114f142011-11-23 20:18:04184 EXPECT_EQ(i, GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]a04db822013-12-11 19:14:40185 base::string16 profile_name =
186 ASCIIToUTF16(base::StringPrintf("name_%ud", i));
[email protected]cb114f142011-11-23 20:18:04187 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
Jacques Chencf4971d2019-06-25 15:16:29188#if !defined(OS_ANDROID)
[email protected]cb114f142011-11-23 20:18:04189 EXPECT_EQ(i, GetCache()->GetAvatarIconIndexOfProfileAtIndex(i));
Jacques Chencf4971d2019-06-25 15:16:29190#endif
[email protected]cb114f142011-11-23 20:18:04191 EXPECT_EQ(true, GetCache()->GetBackgroundStatusOfProfileAtIndex(i));
[email protected]a04db822013-12-11 19:14:40192 base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
[email protected]cb114f142011-11-23 20:18:04193 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(i));
194 }
[email protected]dee810e2011-06-27 19:43:39195}
196
[email protected]d4f5d1162011-11-30 01:41:52197TEST_F(ProfileInfoCacheTest, DeleteProfile) {
[email protected]6730b1e2011-09-29 05:23:52198 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39199
[email protected]650b2d52013-02-10 03:41:45200 base::FilePath path_1 = GetProfilePath("path_1");
Roman Sorokin8ead4d72018-04-16 15:58:58201 GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("name_1"), std::string(),
202 base::string16(), 0, std::string(),
203 EmptyAccountId());
[email protected]6730b1e2011-09-29 05:23:52204 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39205
[email protected]650b2d52013-02-10 03:41:45206 base::FilePath path_2 = GetProfilePath("path_2");
[email protected]a04db822013-12-11 19:14:40207 base::string16 name_2 = ASCIIToUTF16("name_2");
rogertae22659602015-05-11 21:58:21208 GetCache()->AddProfileToCache(path_2, name_2, std::string(), base::string16(),
Roman Sorokin8ead4d72018-04-16 15:58:58209 0, std::string(), EmptyAccountId());
[email protected]6730b1e2011-09-29 05:23:52210 EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39211
[email protected]6730b1e2011-09-29 05:23:52212 GetCache()->DeleteProfileFromCache(path_1);
213 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
214 EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(0));
[email protected]dee810e2011-06-27 19:43:39215
[email protected]6730b1e2011-09-29 05:23:52216 GetCache()->DeleteProfileFromCache(path_2);
217 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39218}
219
[email protected]d4f5d1162011-11-30 01:41:52220TEST_F(ProfileInfoCacheTest, MutateProfile) {
221 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21222 GetProfilePath("path_1"), ASCIIToUTF16("name_1"), std::string(),
Roman Sorokin8ead4d72018-04-16 15:58:58223 base::string16(), 0, std::string(), EmptyAccountId());
[email protected]d4f5d1162011-11-30 01:41:52224 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21225 GetProfilePath("path_2"), ASCIIToUTF16("name_2"), std::string(),
Roman Sorokin8ead4d72018-04-16 15:58:58226 base::string16(), 0, std::string(), EmptyAccountId());
[email protected]dee810e2011-06-27 19:43:39227
[email protected]a04db822013-12-11 19:14:40228 base::string16 new_name = ASCIIToUTF16("new_name");
[email protected]6730b1e2011-09-29 05:23:52229 GetCache()->SetNameOfProfileAtIndex(1, new_name);
230 EXPECT_EQ(new_name, GetCache()->GetNameOfProfileAtIndex(1));
231 EXPECT_NE(new_name, GetCache()->GetNameOfProfileAtIndex(0));
[email protected]dee810e2011-06-27 19:43:39232
[email protected]a04db822013-12-11 19:14:40233 base::string16 new_user_name = ASCIIToUTF16("user_name");
rogertae22659602015-05-11 21:58:21234 std::string new_gaia_id = "12345";
235 GetCache()->SetAuthInfoOfProfileAtIndex(1, new_gaia_id, new_user_name);
[email protected]e8e78092011-09-29 18:15:38236 EXPECT_EQ(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(1));
rogertae22659602015-05-11 21:58:21237 EXPECT_EQ(new_gaia_id, GetCache()->GetGAIAIdOfProfileAtIndex(1));
[email protected]e8e78092011-09-29 18:15:38238 EXPECT_NE(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(0));
239
Jacques Chencf4971d2019-06-25 15:16:29240 // Avatar icons not used on Android.
241#if !defined(OS_ANDROID)
lixan20fabcc2017-01-31 16:03:02242 const size_t new_icon_index = 3;
[email protected]6730b1e2011-09-29 05:23:52243 GetCache()->SetAvatarIconOfProfileAtIndex(1, new_icon_index);
lixan20fabcc2017-01-31 16:03:02244 EXPECT_EQ(new_icon_index, GetCache()->GetAvatarIconIndexOfProfileAtIndex(1));
[email protected]dee810e2011-06-27 19:43:39245 // Not much to test.
[email protected]6730b1e2011-09-29 05:23:52246 GetCache()->GetAvatarIconOfProfileAtIndex(1);
lixan20fabcc2017-01-31 16:03:02247
248 const size_t wrong_icon_index = profiles::GetDefaultAvatarIconCount() + 1;
249 const size_t generic_icon_index = 0;
250 GetCache()->SetAvatarIconOfProfileAtIndex(1, wrong_icon_index);
251 EXPECT_EQ(generic_icon_index,
252 GetCache()->GetAvatarIconIndexOfProfileAtIndex(1));
Jacques Chencf4971d2019-06-25 15:16:29253#endif
[email protected]dee810e2011-06-27 19:43:39254}
255
[email protected]d4f5d1162011-11-30 01:41:52256TEST_F(ProfileInfoCacheTest, Sort) {
[email protected]a04db822013-12-11 19:14:40257 base::string16 name_a = ASCIIToUTF16("apple");
Roman Sorokin8ead4d72018-04-16 15:58:58258 GetCache()->AddProfileToCache(GetProfilePath("path_a"), name_a, std::string(),
259 base::string16(), 0, std::string(),
260 EmptyAccountId());
[email protected]cb114f142011-11-23 20:18:04261
[email protected]a04db822013-12-11 19:14:40262 base::string16 name_c = ASCIIToUTF16("cat");
Roman Sorokin8ead4d72018-04-16 15:58:58263 GetCache()->AddProfileToCache(GetProfilePath("path_c"), name_c, std::string(),
264 base::string16(), 0, std::string(),
265 EmptyAccountId());
[email protected]cb114f142011-11-23 20:18:04266
267 // Sanity check the initial order.
268 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
269 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
270
271 // Add a new profile (start with a capital to test case insensitive sorting.
[email protected]a04db822013-12-11 19:14:40272 base::string16 name_b = ASCIIToUTF16("Banana");
Roman Sorokin8ead4d72018-04-16 15:58:58273 GetCache()->AddProfileToCache(GetProfilePath("path_b"), name_b, std::string(),
274 base::string16(), 0, std::string(),
275 EmptyAccountId());
[email protected]cb114f142011-11-23 20:18:04276
277 // Verify the new order.
278 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
279 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(1));
280 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(2));
281
282 // Change the name of an existing profile.
283 name_a = UTF8ToUTF16("dog");
284 GetCache()->SetNameOfProfileAtIndex(0, name_a);
285
286 // Verify the new order.
287 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
288 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
289 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(2));
290
291 // Delete a profile.
[email protected]d4f5d1162011-11-30 01:41:52292 GetCache()->DeleteProfileFromCache(GetProfilePath("path_c"));
[email protected]cb114f142011-11-23 20:18:04293
294 // Verify the new order.
295 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
296 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(1));
297}
298
WC Leung17756382017-08-25 04:57:36299// Will be removed SOON with ProfileInfoCache tests.
[email protected]d4f5d1162011-11-30 01:41:52300TEST_F(ProfileInfoCacheTest, BackgroundModeStatus) {
[email protected]279170832011-10-12 23:38:03301 GetCache()->AddProfileToCache(
Roman Sorokin8ead4d72018-04-16 15:58:58302 GetProfilePath("path_1"), ASCIIToUTF16("name_1"), std::string(),
303 base::string16(), 0, std::string(), EmptyAccountId());
[email protected]279170832011-10-12 23:38:03304 GetCache()->AddProfileToCache(
Roman Sorokin8ead4d72018-04-16 15:58:58305 GetProfilePath("path_2"), ASCIIToUTF16("name_2"), std::string(),
306 base::string16(), 0, std::string(), EmptyAccountId());
[email protected]279170832011-10-12 23:38:03307
308 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
309 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
310
311 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, true);
312
313 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
314 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
315
316 GetCache()->SetBackgroundStatusOfProfileAtIndex(0, true);
317
318 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
319 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
320
321 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, false);
322
323 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
324 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
325}
326
[email protected]d4f5d1162011-11-30 01:41:52327TEST_F(ProfileInfoCacheTest, GAIAName) {
[email protected]cb114f142011-11-23 20:18:04328 GetCache()->AddProfileToCache(
Roman Sorokin8ead4d72018-04-16 15:58:58329 GetProfilePath("path_1"), ASCIIToUTF16("Person 1"), std::string(),
330 base::string16(), 0, std::string(), EmptyAccountId());
[email protected]a4352d02014-03-06 06:00:27331 base::string16 profile_name(ASCIIToUTF16("Person 2"));
Roman Sorokin8ead4d72018-04-16 15:58:58332 GetCache()->AddProfileToCache(GetProfilePath("path_2"), profile_name,
333 std::string(), base::string16(), 0,
334 std::string(), EmptyAccountId());
[email protected]cb114f142011-11-23 20:18:04335
[email protected]a4352d02014-03-06 06:00:27336 int index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
337 int index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
338
[email protected]cb114f142011-11-23 20:18:04339 // Sanity check.
[email protected]a4352d02014-03-06 06:00:27340 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty());
341 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index2).empty());
[email protected]cb114f142011-11-23 20:18:04342
[email protected]425d6ab2014-03-25 05:59:39343 // Set GAIA name. This re-sorts the cache.
[email protected]a04db822013-12-11 19:14:40344 base::string16 gaia_name(ASCIIToUTF16("Pat Smith"));
[email protected]a4352d02014-03-06 06:00:27345 GetCache()->SetGAIANameOfProfileAtIndex(index2, gaia_name);
[email protected]425d6ab2014-03-25 05:59:39346 index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
347 index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
348
349 // Since there is a GAIA name, we use that as a display name.
[email protected]a4352d02014-03-06 06:00:27350 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty());
351 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2));
[email protected]a4352d02014-03-06 06:00:27352 EXPECT_EQ(gaia_name, GetCache()->GetNameOfProfileAtIndex(index2));
[email protected]cb114f142011-11-23 20:18:04353
[email protected]a4352d02014-03-06 06:00:27354 // Don't use GAIA name as profile name. This re-sorts the cache.
[email protected]425d6ab2014-03-25 05:59:39355 base::string16 custom_name(ASCIIToUTF16("Custom name"));
356 GetCache()->SetNameOfProfileAtIndex(index2, custom_name);
[email protected]1cdf0932014-08-16 06:00:49357 GetCache()->SetProfileIsUsingDefaultNameAtIndex(index2, false);
358
[email protected]a4352d02014-03-06 06:00:27359 index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
360 index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
361
[email protected]425d6ab2014-03-25 05:59:39362 EXPECT_EQ(custom_name, GetCache()->GetNameOfProfileAtIndex(index2));
[email protected]a4352d02014-03-06 06:00:27363 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2));
[email protected]cb114f142011-11-23 20:18:04364}
365
[email protected]d4f5d1162011-11-30 01:41:52366TEST_F(ProfileInfoCacheTest, GAIAPicture) {
[email protected]22900902014-07-30 11:50:41367 const int kDefaultAvatarIndex = 0;
368 const int kOtherAvatarIndex = 1;
[email protected]979dc4f2014-08-12 22:52:11369 const int kGaiaPictureSize = 256; // Standard size of a Gaia account picture.
[email protected]cb114f142011-11-23 20:18:04370 GetCache()->AddProfileToCache(
Roman Sorokin8ead4d72018-04-16 15:58:58371 GetProfilePath("path_1"), ASCIIToUTF16("name_1"), std::string(),
372 base::string16(), kDefaultAvatarIndex, std::string(), EmptyAccountId());
[email protected]cb114f142011-11-23 20:18:04373 GetCache()->AddProfileToCache(
Roman Sorokin8ead4d72018-04-16 15:58:58374 GetProfilePath("path_2"), ASCIIToUTF16("name_2"), std::string(),
375 base::string16(), kDefaultAvatarIndex, std::string(), EmptyAccountId());
[email protected]cb114f142011-11-23 20:18:04376
377 // Sanity check.
[email protected]2f3c00f2011-11-30 04:36:22378 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
379 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(1));
[email protected]cb114f142011-11-23 20:18:04380 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0));
381 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
382
383 // The profile icon should be the default one.
[email protected]22900902014-07-30 11:50:41384 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(0));
385 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
386 int default_avatar_id =
387 profiles::GetDefaultAvatarIconResourceIDAtIndex(kDefaultAvatarIndex);
388 const gfx::Image& default_avatar_image(
Lei Zhang7640d542017-10-03 16:26:49389 ui::ResourceBundle::GetSharedInstance().GetImageNamed(default_avatar_id));
pkotwiczb312b512015-09-29 23:44:43390 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]22900902014-07-30 11:50:41391 default_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
[email protected]cb114f142011-11-23 20:18:04392
393 // Set GAIA picture.
[email protected]979dc4f2014-08-12 22:52:11394 gfx::Image gaia_image(gfx::test::CreateImage(
395 kGaiaPictureSize, kGaiaPictureSize));
Dana Friedfa14d5f2019-04-21 20:49:36396 GetCache()->SetGAIAPictureOfProfileAtIndex(1, gaia_image);
[email protected]2f3c00f2011-11-30 04:36:22397 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
pkotwiczb312b512015-09-29 23:44:43398 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22399 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
[email protected]22900902014-07-30 11:50:41400 // Since we're still using the default avatar, the GAIA image should be
401 // preferred over the generic avatar image.
402 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
403 EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
pkotwiczb312b512015-09-29 23:44:43404 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]22900902014-07-30 11:50:41405 gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
[email protected]cb114f142011-11-23 20:18:04406
[email protected]1cdf0932014-08-16 06:00:49407 // Set a non-default avatar. This should be preferred over the GAIA image.
[email protected]22900902014-07-30 11:50:41408 GetCache()->SetAvatarIconOfProfileAtIndex(1, kOtherAvatarIndex);
[email protected]1cdf0932014-08-16 06:00:49409 GetCache()->SetProfileIsUsingDefaultAvatarAtIndex(1, false);
[email protected]22900902014-07-30 11:50:41410 EXPECT_FALSE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
411 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
Jacques Chencf4971d2019-06-25 15:16:29412// Avatar icons not used on Android.
413#if !defined(OS_ANDROID)
[email protected]22900902014-07-30 11:50:41414 int other_avatar_id =
415 profiles::GetDefaultAvatarIconResourceIDAtIndex(kOtherAvatarIndex);
416 const gfx::Image& other_avatar_image(
Lei Zhang7640d542017-10-03 16:26:49417 ui::ResourceBundle::GetSharedInstance().GetImageNamed(other_avatar_id));
pkotwiczb312b512015-09-29 23:44:43418 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]22900902014-07-30 11:50:41419 other_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
Jacques Chencf4971d2019-06-25 15:16:29420#endif
[email protected]22900902014-07-30 11:50:41421
422 // Explicitly setting the GAIA picture should make it preferred again.
[email protected]cb114f142011-11-23 20:18:04423 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, true);
[email protected]22900902014-07-30 11:50:41424 EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
pkotwiczb312b512015-09-29 23:44:43425 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22426 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
pkotwiczb312b512015-09-29 23:44:43427 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]cb114f142011-11-23 20:18:04428 gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
429
[email protected]22900902014-07-30 11:50:41430 // Clearing the IsUsingGAIAPicture flag should result in the generic image
431 // being used again.
[email protected]cb114f142011-11-23 20:18:04432 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, false);
[email protected]22900902014-07-30 11:50:41433 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
pkotwiczb312b512015-09-29 23:44:43434 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22435 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
Jacques Chencf4971d2019-06-25 15:16:29436#if !defined(OS_ANDROID)
pkotwiczb312b512015-09-29 23:44:43437 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]22900902014-07-30 11:50:41438 other_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
Jacques Chencf4971d2019-06-25 15:16:29439#endif
[email protected]cb114f142011-11-23 20:18:04440}
441
[email protected]754bebc2011-12-01 16:42:16442TEST_F(ProfileInfoCacheTest, PersistGAIAPicture) {
[email protected]cb114f142011-11-23 20:18:04443 GetCache()->AddProfileToCache(
Roman Sorokin8ead4d72018-04-16 15:58:58444 GetProfilePath("path_1"), ASCIIToUTF16("name_1"), std::string(),
445 base::string16(), 0, std::string(), EmptyAccountId());
[email protected]d4f5d1162011-11-30 01:41:52446 gfx::Image gaia_image(gfx::test::CreateImage());
[email protected]cb114f142011-11-23 20:18:04447
Dana Friedfa14d5f2019-04-21 20:49:36448 GetCache()->SetGAIAPictureOfProfileAtIndex(0, gaia_image);
noms75ca0192015-02-26 03:14:30449
450 // Make sure everything has completed, and the file has been written to disk.
Gabriel Charette01507a22017-09-27 21:30:08451 content::RunAllTasksUntilIdle();
noms75ca0192015-02-26 03:14:30452
pkotwiczb312b512015-09-29 23:44:43453 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22454 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
[email protected]cb114f142011-11-23 20:18:04455
[email protected]cb114f142011-11-23 20:18:04456 ResetCache();
[email protected]cb114f142011-11-23 20:18:04457 // Try to get the GAIA picture. This should return NULL until the read from
458 // disk is done.
[email protected]2f3c00f2011-11-30 04:36:22459 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
Gabriel Charette01507a22017-09-27 21:30:08460 content::RunAllTasksUntilIdle();
noms75ca0192015-02-26 03:14:30461
pkotwiczb312b512015-09-29 23:44:43462 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22463 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
464}
465
Jochen Eisingerd84bb42a2018-07-23 22:39:03466#if BUILDFLAG(ENABLE_SUPERVISED_USERS)
[email protected]d20d0432014-06-12 17:14:05467TEST_F(ProfileInfoCacheTest, SetSupervisedUserId) {
Roman Sorokin8ead4d72018-04-16 15:58:58468 GetCache()->AddProfileToCache(GetProfilePath("test"), ASCIIToUTF16("Test"),
469 std::string(), base::string16(), 0,
470 std::string(), EmptyAccountId());
[email protected]d20d0432014-06-12 17:14:05471 EXPECT_FALSE(GetCache()->ProfileIsSupervisedAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54472
Jochen Eisingerd84bb42a2018-07-23 22:39:03473 GetCache()->SetSupervisedUserIdOfProfileAtIndex(
474 0, supervised_users::kChildAccountSUID);
[email protected]d20d0432014-06-12 17:14:05475 EXPECT_TRUE(GetCache()->ProfileIsSupervisedAtIndex(0));
Jochen Eisingerd84bb42a2018-07-23 22:39:03476 EXPECT_EQ(supervised_users::kChildAccountSUID,
477 GetCache()->GetSupervisedUserIdOfProfileAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54478
479 ResetCache();
[email protected]d20d0432014-06-12 17:14:05480 EXPECT_TRUE(GetCache()->ProfileIsSupervisedAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54481
[email protected]d20d0432014-06-12 17:14:05482 GetCache()->SetSupervisedUserIdOfProfileAtIndex(0, std::string());
483 EXPECT_FALSE(GetCache()->ProfileIsSupervisedAtIndex(0));
484 EXPECT_EQ("", GetCache()->GetSupervisedUserIdOfProfileAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54485}
Jochen Eisingerd84bb42a2018-07-23 22:39:03486#endif // BUILDFLAG(ENABLE_SUPERVISED_USERS)
[email protected]0aa018a2013-07-31 15:08:54487
[email protected]2f3c00f2011-11-30 04:36:22488TEST_F(ProfileInfoCacheTest, EmptyGAIAInfo) {
[email protected]a04db822013-12-11 19:14:40489 base::string16 profile_name = ASCIIToUTF16("name_1");
[email protected]c3e559772014-04-09 04:02:54490 int id = profiles::GetDefaultAvatarIconResourceIDAtIndex(0);
[email protected]2f3c00f2011-11-30 04:36:22491 const gfx::Image& profile_image(
Lei Zhang7640d542017-10-03 16:26:49492 ui::ResourceBundle::GetSharedInstance().GetImageNamed(id));
[email protected]2f3c00f2011-11-30 04:36:22493
Roman Sorokin8ead4d72018-04-16 15:58:58494 GetCache()->AddProfileToCache(GetProfilePath("path_1"), profile_name,
495 std::string(), base::string16(), 0,
496 std::string(), EmptyAccountId());
[email protected]2f3c00f2011-11-30 04:36:22497
498 // Set empty GAIA info.
[email protected]a04db822013-12-11 19:14:40499 GetCache()->SetGAIANameOfProfileAtIndex(0, base::string16());
Dana Friedfa14d5f2019-04-21 20:49:36500 GetCache()->SetGAIAPictureOfProfileAtIndex(0, gfx::Image());
[email protected]2f3c00f2011-11-30 04:36:22501 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(0, true);
502
503 // Verify that the profile name and picture are not empty.
504 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(0));
pkotwiczb312b512015-09-29 23:44:43505 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22506 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(0)));
[email protected]cb114f142011-11-23 20:18:04507}
508
Jochen Eisingerd84bb42a2018-07-23 22:39:03509#if BUILDFLAG(ENABLE_SUPERVISED_USERS)
[email protected]d20d0432014-06-12 17:14:05510TEST_F(ProfileInfoCacheTest, CreateSupervisedTestingProfile) {
[email protected]0aa018a2013-07-31 15:08:54511 testing_profile_manager_.CreateTestingProfile("default");
[email protected]d20d0432014-06-12 17:14:05512 base::string16 supervised_user_name = ASCIIToUTF16("Supervised User");
[email protected]0aa018a2013-07-31 15:08:54513 testing_profile_manager_.CreateTestingProfile(
maxbogueea16ff412016-10-28 16:35:29514 "test1", std::unique_ptr<sync_preferences::PrefServiceSyncable>(),
Jochen Eisingerd84bb42a2018-07-23 22:39:03515 supervised_user_name, 0, supervised_users::kChildAccountSUID,
516 TestingProfile::TestingFactories());
[email protected]0aa018a2013-07-31 15:08:54517 for (size_t i = 0; i < GetCache()->GetNumberOfProfiles(); i++) {
[email protected]d20d0432014-06-12 17:14:05518 bool is_supervised =
519 GetCache()->GetNameOfProfileAtIndex(i) == supervised_user_name;
520 EXPECT_EQ(is_supervised, GetCache()->ProfileIsSupervisedAtIndex(i));
Jochen Eisingerd84bb42a2018-07-23 22:39:03521 std::string supervised_user_id =
522 is_supervised ? supervised_users::kChildAccountSUID : "";
[email protected]d20d0432014-06-12 17:14:05523 EXPECT_EQ(supervised_user_id,
524 GetCache()->GetSupervisedUserIdOfProfileAtIndex(i));
[email protected]0aa018a2013-07-31 15:08:54525 }
[email protected]371662e372013-10-17 22:05:22526
[email protected]d20d0432014-06-12 17:14:05527 // Supervised profiles have a custom theme, which needs to be deleted on the
528 // FILE thread. Reset the profile manager now so everything is deleted while
529 // we still have a FILE thread.
[email protected]371662e372013-10-17 22:05:22530 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
[email protected]0aa018a2013-07-31 15:08:54531}
Jochen Eisingerd84bb42a2018-07-23 22:39:03532#endif
[email protected]0aa018a2013-07-31 15:08:54533
[email protected]047481802013-09-16 22:26:38534TEST_F(ProfileInfoCacheTest, AddStubProfile) {
535 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
536
537 // Add some profiles with and without a '.' in their paths.
538 const struct {
539 const char* profile_path;
540 const char* profile_name;
541 } kTestCases[] = {
542 { "path.test0", "name_0" },
543 { "path_test1", "name_1" },
544 { "path.test2", "name_2" },
545 { "path_test3", "name_3" },
546 };
547
Avi Drissman5f0fb8c2018-12-25 23:20:49548 for (size_t i = 0; i < base::size(kTestCases); ++i) {
[email protected]047481802013-09-16 22:26:38549 base::FilePath profile_path = GetProfilePath(kTestCases[i].profile_path);
[email protected]a04db822013-12-11 19:14:40550 base::string16 profile_name = ASCIIToUTF16(kTestCases[i].profile_name);
[email protected]047481802013-09-16 22:26:38551
rogertae22659602015-05-11 21:58:21552 GetCache()->AddProfileToCache(profile_path, profile_name, std::string(),
Roman Sorokin8ead4d72018-04-16 15:58:58553 base::string16(), i, "", EmptyAccountId());
[email protected]047481802013-09-16 22:26:38554
555 EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i));
556 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
557 }
558
559 ASSERT_EQ(4U, GetCache()->GetNumberOfProfiles());
560
561 // Check that the profiles can be extracted from the local state.
noms693f35aa2015-02-05 16:03:16562 std::vector<base::string16> names;
563 PrefService* local_state = g_browser_process->local_state();
564 const base::DictionaryValue* cache = local_state->GetDictionary(
565 prefs::kProfileInfoCache);
566 base::string16 name;
567 for (base::DictionaryValue::Iterator it(*cache); !it.IsAtEnd();
568 it.Advance()) {
569 const base::DictionaryValue* info = NULL;
570 it.value().GetAsDictionary(&info);
571 info->GetString("name", &name);
572 names.push_back(name);
573 }
574
[email protected]047481802013-09-16 22:26:38575 for (size_t i = 0; i < 4; i++)
576 ASSERT_FALSE(names[i].empty());
577}
578
lwchkg778965f2016-05-12 13:14:22579TEST_F(ProfileInfoCacheTest, EntriesInAttributesStorage) {
580 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
581
582 // Add some profiles with and without a '.' in their paths.
583 const struct {
584 const char* profile_path;
585 const char* profile_name;
586 } kTestCases[] = {
587 { "path.test0", "name_0" },
588 { "path_test1", "name_1" },
589 { "path.test2", "name_2" },
590 { "path_test3", "name_3" },
591 };
592
593 // Profiles are added and removed using all combinations of the old and the
594 // new interfaces. The content of |profile_attributes_entries_| in
595 // ProfileAttributesStorage is checked after each insert and delete operation.
596
597 // Add profiles.
Avi Drissman5f0fb8c2018-12-25 23:20:49598 for (size_t i = 0; i < base::size(kTestCases); ++i) {
lwchkg778965f2016-05-12 13:14:22599 base::FilePath profile_path = GetProfilePath(kTestCases[i].profile_path);
600 base::string16 profile_name = ASCIIToUTF16(kTestCases[i].profile_name);
601
602 ASSERT_EQ(0u, GetCache()->profile_attributes_entries_.count(
603 profile_path.value()));
604
605 // Use ProfileInfoCache in profiles 0 and 2, and ProfileAttributesStorage in
606 // profiles 1 and 3.
607 if (i | 1u) {
608 GetCache()->AddProfileToCache(profile_path, profile_name, std::string(),
Roman Sorokin8ead4d72018-04-16 15:58:58609 base::string16(), i, "", EmptyAccountId());
lwchkg778965f2016-05-12 13:14:22610 } else {
611 GetCache()->AddProfile(profile_path, profile_name, std::string(),
Roman Sorokin8ead4d72018-04-16 15:58:58612 base::string16(), i, "", EmptyAccountId());
lwchkg778965f2016-05-12 13:14:22613 }
614
615 ASSERT_EQ(i + 1, GetCache()->GetNumberOfProfiles());
616 ASSERT_EQ(i + 1, GetCache()->profile_attributes_entries_.size());
617
618 ASSERT_EQ(1u, GetCache()->profile_attributes_entries_.count(
619 profile_path.value()));
620 // TODO(anthonyvd) : check that the entry in |profile_attributes_entries_|
621 // is null before GetProfileAttributesWithPath is run. Currently this is
622 // impossible to check because GetProfileAttributesWithPath is called during
623 // profile creation.
624
625 ProfileAttributesEntry* entry = nullptr;
626 GetCache()->GetProfileAttributesWithPath(profile_path, &entry);
627 EXPECT_EQ(
628 entry,
629 GetCache()->profile_attributes_entries_[profile_path.value()].get());
630 }
631
632 // Remove profiles.
Avi Drissman5f0fb8c2018-12-25 23:20:49633 for (size_t i = 0; i < base::size(kTestCases); ++i) {
lwchkg778965f2016-05-12 13:14:22634 base::FilePath profile_path = GetProfilePath(kTestCases[i].profile_path);
635 ASSERT_EQ(1u, GetCache()->profile_attributes_entries_.count(
636 profile_path.value()));
637
638 // Use ProfileInfoCache in profiles 0 and 1, and ProfileAttributesStorage in
639 // profiles 2 and 3.
640 if (i | 2u)
641 GetCache()->DeleteProfileFromCache(profile_path);
642 else
643 GetCache()->RemoveProfile(profile_path);
644
645 ASSERT_EQ(0u, GetCache()->profile_attributes_entries_.count(
646 profile_path.value()));
647
648 ProfileAttributesEntry* entry = nullptr;
649 EXPECT_FALSE(GetCache()->GetProfileAttributesWithPath(profile_path,
650 &entry));
651 ASSERT_EQ(0u, GetCache()->profile_attributes_entries_.count(
652 profile_path.value()));
653 }
654}
655
jam1c5a91492016-02-24 20:47:53656#if !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
nomsbe073e52014-08-23 22:01:04657TEST_F(ProfileInfoCacheTest, MigrateLegacyProfileNamesWithNewAvatarMenu) {
nomsbe073e52014-08-23 22:01:04658 EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles());
659
660 base::FilePath path_1 = GetProfilePath("path_1");
661 GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("Default Profile"),
rogertae22659602015-05-11 21:58:21662 std::string(), base::string16(), 0,
Roman Sorokin8ead4d72018-04-16 15:58:58663 std::string(), EmptyAccountId());
nomsbe073e52014-08-23 22:01:04664 base::FilePath path_2 = GetProfilePath("path_2");
665 GetCache()->AddProfileToCache(path_2, ASCIIToUTF16("First user"),
rogertae22659602015-05-11 21:58:21666 std::string(), base::string16(), 1,
Roman Sorokin8ead4d72018-04-16 15:58:58667 std::string(), EmptyAccountId());
nomsbe073e52014-08-23 22:01:04668 base::string16 name_3 = ASCIIToUTF16("Lemonade");
669 base::FilePath path_3 = GetProfilePath("path_3");
Roman Sorokin8ead4d72018-04-16 15:58:58670 GetCache()->AddProfileToCache(path_3, name_3, std::string(), base::string16(),
671 2, std::string(), EmptyAccountId());
nomsbe073e52014-08-23 22:01:04672 base::string16 name_4 = ASCIIToUTF16("Batman");
673 base::FilePath path_4 = GetProfilePath("path_4");
Roman Sorokin8ead4d72018-04-16 15:58:58674 GetCache()->AddProfileToCache(path_4, name_4, std::string(), base::string16(),
675 3, std::string(), EmptyAccountId());
nomsbe073e52014-08-23 22:01:04676 base::string16 name_5 = ASCIIToUTF16("Person 2");
677 base::FilePath path_5 = GetProfilePath("path_5");
Roman Sorokin8ead4d72018-04-16 15:58:58678 GetCache()->AddProfileToCache(path_5, name_5, std::string(), base::string16(),
679 2, std::string(), EmptyAccountId());
nomsbe073e52014-08-23 22:01:04680
681 EXPECT_EQ(5U, GetCache()->GetNumberOfProfiles());
682
683
684 ResetCache();
685
686 // Legacy profile names like "Default Profile" and "First user" should be
WC Leung0ea09a642017-09-02 01:46:15687 // migrated to "Person %n" type names, i.e. any permutation of "Person 1" and
688 // "Person 3".
689 if (ASCIIToUTF16("Person 1") ==
690 GetCache()->GetNameOfProfileAtIndex(
691 GetCache()->GetIndexOfProfileWithPath(path_1))) {
692 EXPECT_EQ(ASCIIToUTF16("Person 3"),
693 GetCache()->GetNameOfProfileAtIndex(
694 GetCache()->GetIndexOfProfileWithPath(path_2)));
695 } else {
696 EXPECT_EQ(ASCIIToUTF16("Person 3"),
697 GetCache()->GetNameOfProfileAtIndex(
698 GetCache()->GetIndexOfProfileWithPath(path_1)));
699 EXPECT_EQ(ASCIIToUTF16("Person 1"),
700 GetCache()->GetNameOfProfileAtIndex(
701 GetCache()->GetIndexOfProfileWithPath(path_2)));
702 }
nomsbe073e52014-08-23 22:01:04703
704 // Other profile names should not be migrated even if they're the old
705 // default cartoon profile names.
706 EXPECT_EQ(name_3, GetCache()->GetNameOfProfileAtIndex(
707 GetCache()->GetIndexOfProfileWithPath(path_3)));
708 EXPECT_EQ(name_4, GetCache()->GetNameOfProfileAtIndex(
709 GetCache()->GetIndexOfProfileWithPath(path_4)));
710 EXPECT_EQ(name_5, GetCache()->GetNameOfProfileAtIndex(
711 GetCache()->GetIndexOfProfileWithPath(path_5)));
712}
Xi Cheng1b761382017-08-15 20:37:34713
714TEST_F(ProfileInfoCacheTest, GetGaiaImageForAvatarMenu) {
715 // The TestingProfileManager's ProfileInfoCache doesn't download avatars.
716 ProfileInfoCache profile_info_cache(
717 g_browser_process->local_state(),
718 testing_profile_manager_.profile_manager()->user_data_dir());
719
720 base::FilePath profile_path = GetProfilePath("path_1");
721
722 GetCache()->AddProfileToCache(profile_path, ASCIIToUTF16("name_1"),
723 std::string(), base::string16(), 0,
Roman Sorokin8ead4d72018-04-16 15:58:58724 std::string(), EmptyAccountId());
Xi Cheng1b761382017-08-15 20:37:34725
726 gfx::Image gaia_image(gfx::test::CreateImage());
Dana Friedfa14d5f2019-04-21 20:49:36727 GetCache()->SetGAIAPictureOfProfileAtIndex(0, gaia_image);
Xi Cheng1b761382017-08-15 20:37:34728
729 // Make sure everything has completed, and the file has been written to disk.
Gabriel Charette01507a22017-09-27 21:30:08730 content::RunAllTasksUntilIdle();
Xi Cheng1b761382017-08-15 20:37:34731
732 // Make sure this profile is using GAIA picture.
733 EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0));
734
735 ResetCache();
736
737 // We need to explicitly set the GAIA usage flag after resetting the cache.
738 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(0, true);
739 EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0));
740
741 gfx::Image image_loaded;
742
743 // Try to get the GAIA image. For the first time, it triggers an async image
744 // load from disk. The load status indicates the image is still being loaded.
745 EXPECT_EQ(AvatarMenu::ImageLoadStatus::LOADING,
746 AvatarMenu::GetImageForMenuButton(profile_path, &image_loaded));
747 EXPECT_FALSE(gfx::test::AreImagesEqual(gaia_image, image_loaded));
748
749 // Wait until the async image load finishes.
Gabriel Charette01507a22017-09-27 21:30:08750 content::RunAllTasksUntilIdle();
Xi Cheng1b761382017-08-15 20:37:34751
752 // Since the GAIA image is loaded now, we can get it this time.
753 EXPECT_EQ(AvatarMenu::ImageLoadStatus::LOADED,
754 AvatarMenu::GetImageForMenuButton(profile_path, &image_loaded));
755 EXPECT_TRUE(gfx::test::AreImagesEqual(gaia_image, image_loaded));
756}
[email protected]b09bfa12014-08-15 19:05:25757#endif
nomsbe073e52014-08-23 22:01:04758
jam1c5a91492016-02-24 20:47:53759#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
nomsbe073e52014-08-23 22:01:04760TEST_F(ProfileInfoCacheTest,
761 DontMigrateLegacyProfileNamesWithoutNewAvatarMenu) {
762 EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles());
763
764 base::string16 name_1 = ASCIIToUTF16("Default Profile");
765 base::FilePath path_1 = GetProfilePath("path_1");
Roman Sorokin8ead4d72018-04-16 15:58:58766 GetCache()->AddProfileToCache(path_1, name_1, std::string(), base::string16(),
767 0, std::string(), EmptyAccountId());
nomsbe073e52014-08-23 22:01:04768 base::string16 name_2 = ASCIIToUTF16("First user");
769 base::FilePath path_2 = GetProfilePath("path_2");
Roman Sorokin8ead4d72018-04-16 15:58:58770 GetCache()->AddProfileToCache(path_2, name_2, std::string(), base::string16(),
771 1, std::string(), EmptyAccountId());
nomsbe073e52014-08-23 22:01:04772 base::string16 name_3 = ASCIIToUTF16("Lemonade");
773 base::FilePath path_3 = GetProfilePath("path_3");
Roman Sorokin8ead4d72018-04-16 15:58:58774 GetCache()->AddProfileToCache(path_3, name_3, std::string(), base::string16(),
775 2, std::string(), EmptyAccountId());
nomsbe073e52014-08-23 22:01:04776 base::string16 name_4 = ASCIIToUTF16("Batman");
777 base::FilePath path_4 = GetProfilePath("path_4");
Roman Sorokin8ead4d72018-04-16 15:58:58778 GetCache()->AddProfileToCache(path_4, name_4, std::string(), base::string16(),
779 3, std::string(), EmptyAccountId());
nomsbe073e52014-08-23 22:01:04780 EXPECT_EQ(4U, GetCache()->GetNumberOfProfiles());
781
782 ResetCache();
783
784 // Profile names should have been preserved.
785 EXPECT_EQ(name_1, GetCache()->GetNameOfProfileAtIndex(
786 GetCache()->GetIndexOfProfileWithPath(path_1)));
787 EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(
788 GetCache()->GetIndexOfProfileWithPath(path_2)));
789 EXPECT_EQ(name_3, GetCache()->GetNameOfProfileAtIndex(
790 GetCache()->GetIndexOfProfileWithPath(path_3)));
791 EXPECT_EQ(name_4, GetCache()->GetNameOfProfileAtIndex(
792 GetCache()->GetIndexOfProfileWithPath(path_4)));
793}
anthonyvd66aa95d2015-10-02 15:40:26794#endif
Alexander Alekseev4084f502018-02-20 22:16:46795
796TEST_F(ProfileInfoCacheTest, RemoveProfileByAccountId) {
797 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
798
799 base::FilePath path_1 = GetProfilePath("path_1");
800 const AccountId account_id_1(
801 AccountId::FromUserEmailGaiaId("email1", "111111"));
802 base::string16 name_1 = ASCIIToUTF16("name_1");
803 GetCache()->AddProfileToCache(path_1, name_1, account_id_1.GetGaiaId(),
804 UTF8ToUTF16(account_id_1.GetUserEmail()), 0,
Roman Sorokin8ead4d72018-04-16 15:58:58805 std::string(), EmptyAccountId());
Alexander Alekseev4084f502018-02-20 22:16:46806 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
807
808 base::FilePath path_2 = GetProfilePath("path_2");
809 base::string16 name_2 = ASCIIToUTF16("name_2");
810 const AccountId account_id_2(
811 AccountId::FromUserEmailGaiaId("email2", "222222"));
812 GetCache()->AddProfileToCache(path_2, name_2, account_id_2.GetGaiaId(),
813 UTF8ToUTF16(account_id_2.GetUserEmail()), 0,
Roman Sorokin8ead4d72018-04-16 15:58:58814 std::string(), EmptyAccountId());
Alexander Alekseev4084f502018-02-20 22:16:46815 EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles());
816
817 base::FilePath path_3 = GetProfilePath("path_3");
818 base::string16 name_3 = ASCIIToUTF16("name_3");
819 const AccountId account_id_3(
820 AccountId::FromUserEmailGaiaId("email3", "333333"));
821 GetCache()->AddProfileToCache(path_3, name_3, account_id_3.GetGaiaId(),
822 UTF8ToUTF16(account_id_3.GetUserEmail()), 0,
Roman Sorokin8ead4d72018-04-16 15:58:58823 std::string(), EmptyAccountId());
Alexander Alekseev4084f502018-02-20 22:16:46824 EXPECT_EQ(3u, GetCache()->GetNumberOfProfiles());
825
826 base::FilePath path_4 = GetProfilePath("path_4");
827 base::string16 name_4 = ASCIIToUTF16("name_4");
828 const AccountId account_id_4(
829 AccountId::FromUserEmailGaiaId("email4", "444444"));
830 GetCache()->AddProfileToCache(path_4, name_4, account_id_4.GetGaiaId(),
831 UTF8ToUTF16(account_id_4.GetUserEmail()), 0,
Roman Sorokin8ead4d72018-04-16 15:58:58832 std::string(), EmptyAccountId());
Alexander Alekseev4084f502018-02-20 22:16:46833 EXPECT_EQ(4u, GetCache()->GetNumberOfProfiles());
834
835 GetCache()->RemoveProfileByAccountId(account_id_3);
836 EXPECT_EQ(3u, GetCache()->GetNumberOfProfiles());
837 EXPECT_EQ(name_1, GetCache()->GetNameOfProfileAtIndex(0));
838
839 GetCache()->RemoveProfileByAccountId(account_id_1);
840 EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles());
841 EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(0));
842
843 // this profile is already deleted.
844 GetCache()->RemoveProfileByAccountId(account_id_3);
845 EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles());
846 EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(0));
847
848 // Remove profile by partial match
849 GetCache()->RemoveProfileByAccountId(
850 AccountId::FromUserEmail(account_id_2.GetUserEmail()));
851 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
852 EXPECT_EQ(name_4, GetCache()->GetNameOfProfileAtIndex(0));
853
854 // Remove last profile
855 GetCache()->RemoveProfileByAccountId(account_id_4);
856 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
857}