blob: 6cb72ecb150c82c2715422c24401f37f01082a91 [file] [log] [blame]
[email protected]ca591072012-03-27 01:54:441// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]dee810e2011-06-27 19:43:392// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]d4f5d1162011-11-30 01:41:525#include "chrome/browser/profiles/profile_info_cache_unittest.h"
[email protected]dee810e2011-06-27 19:43:396
avib896c712015-12-26 02:10:437#include <stddef.h>
8#include <stdint.h>
9
[email protected]047481802013-09-16 22:26:3810#include <vector>
11
lwchkg3f4491202016-03-30 16:49:5212#include "base/bind.h"
[email protected]5cf8bcd92014-07-17 20:27:4013#include "base/command_line.h"
thestig18dfb7a52014-08-26 10:44:0414#include "base/files/file_util.h"
avib896c712015-12-26 02:10:4315#include "base/macros.h"
[email protected]76fb05c2013-06-11 04:38:0516#include "base/strings/stringprintf.h"
[email protected]e309f312013-06-07 21:50:0817#include "base/strings/utf_string_conversions.h"
[email protected]85503a02014-01-16 17:27:2218#include "base/time/time.h"
avib896c712015-12-26 02:10:4319#include "build/build_config.h"
[email protected]583844c2011-08-27 00:38:3520#include "chrome/browser/browser_process.h"
[email protected]9de44302014-05-01 16:32:2721#include "chrome/browser/profiles/profile_avatar_downloader.h"
[email protected]c3e559772014-04-09 04:02:5422#include "chrome/browser/profiles/profile_avatar_icon_util.h"
[email protected]d4f5d1162011-11-30 01:41:5223#include "chrome/browser/profiles/profile_info_cache.h"
[email protected]6730b1e2011-09-29 05:23:5224#include "chrome/browser/profiles/profile_manager.h"
emaxxa2e11492015-05-12 17:19:1125#include "chrome/common/chrome_paths.h"
[email protected]5cf8bcd92014-07-17 20:27:4026#include "chrome/common/chrome_switches.h"
noms693f35aa2015-02-05 16:03:1627#include "chrome/common/pref_names.h"
[email protected]583844c2011-08-27 00:38:3528#include "chrome/test/base/testing_browser_process.h"
brettwb1fc1b82016-02-02 00:19:0829#include "components/prefs/testing_pref_service.h"
[email protected]5cf8bcd92014-07-17 20:27:4030#include "components/signin/core/common/profile_management_switches.h"
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
[email protected]6778fed2013-12-24 20:09:3739using base::ASCIIToUTF16;
[email protected]f729d7a2013-12-26 07:07:5640using base::UTF8ToUTF16;
[email protected]cb114f142011-11-23 20:18:0441using content::BrowserThread;
42
[email protected]6a460662011-12-22 22:05:1643ProfileNameVerifierObserver::ProfileNameVerifierObserver(
44 TestingProfileManager* testing_profile_manager)
45 : testing_profile_manager_(testing_profile_manager) {
46 DCHECK(testing_profile_manager_);
[email protected]590e189b2011-12-13 22:07:0347}
48
49ProfileNameVerifierObserver::~ProfileNameVerifierObserver() {
50}
51
52void ProfileNameVerifierObserver::OnProfileAdded(
[email protected]650b2d52013-02-10 03:41:4553 const base::FilePath& profile_path) {
[email protected]a04db822013-12-11 19:14:4054 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1655 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0356 EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end());
57 profile_names_.insert(profile_name);
58}
59
[email protected]7b0af152011-12-16 17:02:0660void ProfileNameVerifierObserver::OnProfileWillBeRemoved(
[email protected]650b2d52013-02-10 03:41:4561 const base::FilePath& profile_path) {
[email protected]a04db822013-12-11 19:14:4062 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1663 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0364 EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end());
65 profile_names_.erase(profile_name);
66}
67
[email protected]7b0af152011-12-16 17:02:0668void ProfileNameVerifierObserver::OnProfileWasRemoved(
[email protected]650b2d52013-02-10 03:41:4569 const base::FilePath& profile_path,
[email protected]a04db822013-12-11 19:14:4070 const base::string16& profile_name) {
[email protected]7b0af152011-12-16 17:02:0671 EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end());
72}
73
[email protected]590e189b2011-12-13 22:07:0374void ProfileNameVerifierObserver::OnProfileNameChanged(
[email protected]650b2d52013-02-10 03:41:4575 const base::FilePath& profile_path,
[email protected]a04db822013-12-11 19:14:4076 const base::string16& old_profile_name) {
77 base::string16 new_profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1678 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0379 EXPECT_TRUE(profile_names_.find(old_profile_name) != profile_names_.end());
80 EXPECT_TRUE(profile_names_.find(new_profile_name) == profile_names_.end());
81 profile_names_.erase(old_profile_name);
82 profile_names_.insert(new_profile_name);
83}
84
85void ProfileNameVerifierObserver::OnProfileAvatarChanged(
[email protected]650b2d52013-02-10 03:41:4586 const base::FilePath& profile_path) {
[email protected]a04db822013-12-11 19:14:4087 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1688 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0389 EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end());
90}
91
[email protected]6a460662011-12-22 22:05:1692ProfileInfoCache* ProfileNameVerifierObserver::GetCache() {
93 return testing_profile_manager_->profile_info_cache();
94}
95
[email protected]d4f5d1162011-11-30 01:41:5296ProfileInfoCacheTest::ProfileInfoCacheTest()
[email protected]0aa018a2013-07-31 15:08:5497 : testing_profile_manager_(TestingBrowserProcess::GetGlobal()),
emaxxa2e11492015-05-12 17:19:1198 name_observer_(&testing_profile_manager_),
99 user_data_dir_override_(chrome::DIR_USER_DATA) {
[email protected]d4f5d1162011-11-30 01:41:52100}
101
102ProfileInfoCacheTest::~ProfileInfoCacheTest() {
103}
104
105void ProfileInfoCacheTest::SetUp() {
106 ASSERT_TRUE(testing_profile_manager_.SetUp());
[email protected]590e189b2011-12-13 22:07:03107 testing_profile_manager_.profile_info_cache()->AddObserver(&name_observer_);
[email protected]d4f5d1162011-11-30 01:41:52108}
109
[email protected]1302dcf2011-11-30 21:47:05110void ProfileInfoCacheTest::TearDown() {
111 // Drain the UI thread to make sure all tasks are completed. This prevents
112 // memory leaks.
[email protected]0aa018a2013-07-31 15:08:54113 base::RunLoop().RunUntilIdle();
[email protected]1302dcf2011-11-30 21:47:05114}
115
[email protected]d4f5d1162011-11-30 01:41:52116ProfileInfoCache* ProfileInfoCacheTest::GetCache() {
117 return testing_profile_manager_.profile_info_cache();
118}
119
[email protected]650b2d52013-02-10 03:41:45120base::FilePath ProfileInfoCacheTest::GetProfilePath(
[email protected]d4f5d1162011-11-30 01:41:52121 const std::string& base_name) {
122 return testing_profile_manager_.profile_manager()->user_data_dir().
123 AppendASCII(base_name);
124}
125
126void ProfileInfoCacheTest::ResetCache() {
127 testing_profile_manager_.DeleteProfileInfoCache();
128}
129
[email protected]d4f5d1162011-11-30 01:41:52130TEST_F(ProfileInfoCacheTest, AddProfiles) {
[email protected]6730b1e2011-09-29 05:23:52131 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39132
[email protected]ca591072012-03-27 01:54:44133 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
avib896c712015-12-26 02:10:43134 for (uint32_t i = 0; i < 4; ++i) {
[email protected]7d3cbc92013-03-18 22:33:04135 base::FilePath profile_path =
136 GetProfilePath(base::StringPrintf("path_%ud", i));
[email protected]a04db822013-12-11 19:14:40137 base::string16 profile_name =
138 ASCIIToUTF16(base::StringPrintf("name_%ud", i));
[email protected]ca591072012-03-27 01:54:44139 const SkBitmap* icon = rb.GetImageNamed(
[email protected]c3e559772014-04-09 04:02:54140 profiles::GetDefaultAvatarIconResourceIDAtIndex(
[email protected]ca591072012-03-27 01:54:44141 i)).ToSkBitmap();
[email protected]d20d0432014-06-12 17:14:05142 std::string supervised_user_id = i == 3 ? "TEST_ID" : "";
[email protected]dee810e2011-06-27 19:43:39143
rogertae22659602015-05-11 21:58:21144 GetCache()->AddProfileToCache(profile_path, profile_name, std::string(),
145 base::string16(), i, supervised_user_id);
[email protected]cb114f142011-11-23 20:18:04146 GetCache()->SetBackgroundStatusOfProfileAtIndex(i, true);
[email protected]a04db822013-12-11 19:14:40147 base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
[email protected]cb114f142011-11-23 20:18:04148 GetCache()->SetGAIANameOfProfileAtIndex(i, gaia_name);
[email protected]dee810e2011-06-27 19:43:39149
[email protected]6730b1e2011-09-29 05:23:52150 EXPECT_EQ(i + 1, GetCache()->GetNumberOfProfiles());
151 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
152 EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i));
[email protected]0aa018a2013-07-31 15:08:54153 const SkBitmap* actual_icon =
154 GetCache()->GetAvatarIconOfProfileAtIndex(i).ToSkBitmap();
[email protected]ca591072012-03-27 01:54:44155 EXPECT_EQ(icon->width(), actual_icon->width());
156 EXPECT_EQ(icon->height(), actual_icon->height());
[email protected]d20d0432014-06-12 17:14:05157 EXPECT_EQ(i == 3, GetCache()->ProfileIsSupervisedAtIndex(i));
[email protected]5ddfade2014-02-03 10:24:53158 EXPECT_EQ(i == 3, GetCache()->IsOmittedProfileAtIndex(i));
[email protected]d20d0432014-06-12 17:14:05159 EXPECT_EQ(supervised_user_id,
160 GetCache()->GetSupervisedUserIdOfProfileAtIndex(i));
[email protected]dee810e2011-06-27 19:43:39161 }
[email protected]cb114f142011-11-23 20:18:04162
163 // Reset the cache and test the it reloads correctly.
164 ResetCache();
165
166 EXPECT_EQ(4u, GetCache()->GetNumberOfProfiles());
avib896c712015-12-26 02:10:43167 for (uint32_t i = 0; i < 4; ++i) {
[email protected]7d3cbc92013-03-18 22:33:04168 base::FilePath profile_path =
169 GetProfilePath(base::StringPrintf("path_%ud", i));
[email protected]cb114f142011-11-23 20:18:04170 EXPECT_EQ(i, GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]a04db822013-12-11 19:14:40171 base::string16 profile_name =
172 ASCIIToUTF16(base::StringPrintf("name_%ud", i));
[email protected]cb114f142011-11-23 20:18:04173 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
174 EXPECT_EQ(i, GetCache()->GetAvatarIconIndexOfProfileAtIndex(i));
175 EXPECT_EQ(true, GetCache()->GetBackgroundStatusOfProfileAtIndex(i));
[email protected]a04db822013-12-11 19:14:40176 base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
[email protected]cb114f142011-11-23 20:18:04177 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(i));
178 }
[email protected]dee810e2011-06-27 19:43:39179}
180
[email protected]d4f5d1162011-11-30 01:41:52181TEST_F(ProfileInfoCacheTest, DeleteProfile) {
[email protected]6730b1e2011-09-29 05:23:52182 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39183
[email protected]650b2d52013-02-10 03:41:45184 base::FilePath path_1 = GetProfilePath("path_1");
[email protected]a04db822013-12-11 19:14:40185 GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("name_1"),
rogertae22659602015-05-11 21:58:21186 std::string(), base::string16(), 0,
187 std::string());
[email protected]6730b1e2011-09-29 05:23:52188 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39189
[email protected]650b2d52013-02-10 03:41:45190 base::FilePath path_2 = GetProfilePath("path_2");
[email protected]a04db822013-12-11 19:14:40191 base::string16 name_2 = ASCIIToUTF16("name_2");
rogertae22659602015-05-11 21:58:21192 GetCache()->AddProfileToCache(path_2, name_2, std::string(), base::string16(),
193 0, std::string());
[email protected]6730b1e2011-09-29 05:23:52194 EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39195
[email protected]6730b1e2011-09-29 05:23:52196 GetCache()->DeleteProfileFromCache(path_1);
197 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
198 EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(0));
[email protected]dee810e2011-06-27 19:43:39199
[email protected]6730b1e2011-09-29 05:23:52200 GetCache()->DeleteProfileFromCache(path_2);
201 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39202}
203
[email protected]d4f5d1162011-11-30 01:41:52204TEST_F(ProfileInfoCacheTest, MutateProfile) {
205 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21206 GetProfilePath("path_1"), ASCIIToUTF16("name_1"), std::string(),
[email protected]a04db822013-12-11 19:14:40207 base::string16(), 0, std::string());
[email protected]d4f5d1162011-11-30 01:41:52208 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21209 GetProfilePath("path_2"), ASCIIToUTF16("name_2"), std::string(),
[email protected]a04db822013-12-11 19:14:40210 base::string16(), 0, std::string());
[email protected]dee810e2011-06-27 19:43:39211
[email protected]a04db822013-12-11 19:14:40212 base::string16 new_name = ASCIIToUTF16("new_name");
[email protected]6730b1e2011-09-29 05:23:52213 GetCache()->SetNameOfProfileAtIndex(1, new_name);
214 EXPECT_EQ(new_name, GetCache()->GetNameOfProfileAtIndex(1));
215 EXPECT_NE(new_name, GetCache()->GetNameOfProfileAtIndex(0));
[email protected]dee810e2011-06-27 19:43:39216
[email protected]a04db822013-12-11 19:14:40217 base::string16 new_user_name = ASCIIToUTF16("user_name");
rogertae22659602015-05-11 21:58:21218 std::string new_gaia_id = "12345";
219 GetCache()->SetAuthInfoOfProfileAtIndex(1, new_gaia_id, new_user_name);
[email protected]e8e78092011-09-29 18:15:38220 EXPECT_EQ(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(1));
rogertae22659602015-05-11 21:58:21221 EXPECT_EQ(new_gaia_id, GetCache()->GetGAIAIdOfProfileAtIndex(1));
[email protected]e8e78092011-09-29 18:15:38222 EXPECT_NE(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(0));
223
lixan20fabcc2017-01-31 16:03:02224 const size_t new_icon_index = 3;
[email protected]6730b1e2011-09-29 05:23:52225 GetCache()->SetAvatarIconOfProfileAtIndex(1, new_icon_index);
lixan20fabcc2017-01-31 16:03:02226 EXPECT_EQ(new_icon_index, GetCache()->GetAvatarIconIndexOfProfileAtIndex(1));
[email protected]dee810e2011-06-27 19:43:39227 // Not much to test.
[email protected]6730b1e2011-09-29 05:23:52228 GetCache()->GetAvatarIconOfProfileAtIndex(1);
lixan20fabcc2017-01-31 16:03:02229
230 const size_t wrong_icon_index = profiles::GetDefaultAvatarIconCount() + 1;
231 const size_t generic_icon_index = 0;
232 GetCache()->SetAvatarIconOfProfileAtIndex(1, wrong_icon_index);
233 EXPECT_EQ(generic_icon_index,
234 GetCache()->GetAvatarIconIndexOfProfileAtIndex(1));
[email protected]dee810e2011-06-27 19:43:39235}
236
[email protected]d4f5d1162011-11-30 01:41:52237TEST_F(ProfileInfoCacheTest, Sort) {
[email protected]a04db822013-12-11 19:14:40238 base::string16 name_a = ASCIIToUTF16("apple");
[email protected]d4f5d1162011-11-30 01:41:52239 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21240 GetProfilePath("path_a"), name_a, std::string(), base::string16(), 0,
241 std::string());
[email protected]cb114f142011-11-23 20:18:04242
[email protected]a04db822013-12-11 19:14:40243 base::string16 name_c = ASCIIToUTF16("cat");
[email protected]d4f5d1162011-11-30 01:41:52244 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21245 GetProfilePath("path_c"), name_c, std::string(), base::string16(), 0,
246 std::string());
[email protected]cb114f142011-11-23 20:18:04247
248 // Sanity check the initial order.
249 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
250 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
251
252 // Add a new profile (start with a capital to test case insensitive sorting.
[email protected]a04db822013-12-11 19:14:40253 base::string16 name_b = ASCIIToUTF16("Banana");
[email protected]d4f5d1162011-11-30 01:41:52254 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21255 GetProfilePath("path_b"), name_b, std::string(), base::string16(), 0,
256 std::string());
[email protected]cb114f142011-11-23 20:18:04257
258 // Verify the new order.
259 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
260 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(1));
261 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(2));
262
263 // Change the name of an existing profile.
264 name_a = UTF8ToUTF16("dog");
265 GetCache()->SetNameOfProfileAtIndex(0, name_a);
266
267 // Verify the new order.
268 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
269 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
270 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(2));
271
272 // Delete a profile.
[email protected]d4f5d1162011-11-30 01:41:52273 GetCache()->DeleteProfileFromCache(GetProfilePath("path_c"));
[email protected]cb114f142011-11-23 20:18:04274
275 // Verify the new order.
276 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
277 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(1));
278}
279
[email protected]d4f5d1162011-11-30 01:41:52280TEST_F(ProfileInfoCacheTest, BackgroundModeStatus) {
[email protected]279170832011-10-12 23:38:03281 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13282 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
rogertae22659602015-05-11 21:58:21283 std::string(), base::string16(), 0, std::string());
[email protected]279170832011-10-12 23:38:03284 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13285 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
rogertae22659602015-05-11 21:58:21286 std::string(), base::string16(), 0, std::string());
[email protected]279170832011-10-12 23:38:03287
288 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
289 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
290
291 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, true);
292
293 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
294 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
295
296 GetCache()->SetBackgroundStatusOfProfileAtIndex(0, true);
297
298 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
299 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
300
301 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, false);
302
303 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
304 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
305}
306
[email protected]85503a02014-01-16 17:27:22307TEST_F(ProfileInfoCacheTest, ProfileActiveTime) {
308 GetCache()->AddProfileToCache(
309 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
rogertae22659602015-05-11 21:58:21310 std::string(), base::string16(), 0, std::string());
[email protected]85503a02014-01-16 17:27:22311 EXPECT_EQ(base::Time(), GetCache()->GetProfileActiveTimeAtIndex(0));
[email protected]87727e02014-02-27 18:16:53312 // Before & After times are artificially shifted because just relying upon
313 // the system time can yield problems due to inaccuracies in the
314 // underlying storage system (which uses a double with only 52 bits of
315 // precision to store the 64-bit "time" number). http://crbug.com/346827
[email protected]85503a02014-01-16 17:27:22316 base::Time before = base::Time::Now();
[email protected]87727e02014-02-27 18:16:53317 before -= base::TimeDelta::FromSeconds(1);
[email protected]85503a02014-01-16 17:27:22318 GetCache()->SetProfileActiveTimeAtIndex(0);
319 base::Time after = base::Time::Now();
[email protected]87727e02014-02-27 18:16:53320 after += base::TimeDelta::FromSeconds(1);
[email protected]85503a02014-01-16 17:27:22321 EXPECT_LE(before, GetCache()->GetProfileActiveTimeAtIndex(0));
322 EXPECT_GE(after, GetCache()->GetProfileActiveTimeAtIndex(0));
323}
324
[email protected]d4f5d1162011-11-30 01:41:52325TEST_F(ProfileInfoCacheTest, GAIAName) {
[email protected]cb114f142011-11-23 20:18:04326 GetCache()->AddProfileToCache(
[email protected]a4352d02014-03-06 06:00:27327 GetProfilePath("path_1"), ASCIIToUTF16("Person 1"),
rogertae22659602015-05-11 21:58:21328 std::string(), base::string16(), 0, std::string());
[email protected]a4352d02014-03-06 06:00:27329 base::string16 profile_name(ASCIIToUTF16("Person 2"));
[email protected]cb114f142011-11-23 20:18:04330 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21331 GetProfilePath("path_2"), profile_name, std::string(), base::string16(),
332 0, std::string());
[email protected]cb114f142011-11-23 20:18:04333
[email protected]a4352d02014-03-06 06:00:27334 int index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
335 int index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
336
[email protected]cb114f142011-11-23 20:18:04337 // Sanity check.
[email protected]a4352d02014-03-06 06:00:27338 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty());
339 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index2).empty());
[email protected]cb114f142011-11-23 20:18:04340
[email protected]425d6ab2014-03-25 05:59:39341 // Set GAIA name. This re-sorts the cache.
[email protected]a04db822013-12-11 19:14:40342 base::string16 gaia_name(ASCIIToUTF16("Pat Smith"));
[email protected]a4352d02014-03-06 06:00:27343 GetCache()->SetGAIANameOfProfileAtIndex(index2, gaia_name);
[email protected]425d6ab2014-03-25 05:59:39344 index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
345 index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
346
347 // Since there is a GAIA name, we use that as a display name.
[email protected]a4352d02014-03-06 06:00:27348 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty());
349 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2));
[email protected]a4352d02014-03-06 06:00:27350 EXPECT_EQ(gaia_name, GetCache()->GetNameOfProfileAtIndex(index2));
[email protected]cb114f142011-11-23 20:18:04351
[email protected]a4352d02014-03-06 06:00:27352 // Don't use GAIA name as profile name. This re-sorts the cache.
[email protected]425d6ab2014-03-25 05:59:39353 base::string16 custom_name(ASCIIToUTF16("Custom name"));
354 GetCache()->SetNameOfProfileAtIndex(index2, custom_name);
[email protected]1cdf0932014-08-16 06:00:49355 GetCache()->SetProfileIsUsingDefaultNameAtIndex(index2, false);
356
[email protected]a4352d02014-03-06 06:00:27357 index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
358 index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
359
[email protected]425d6ab2014-03-25 05:59:39360 EXPECT_EQ(custom_name, GetCache()->GetNameOfProfileAtIndex(index2));
[email protected]a4352d02014-03-06 06:00:27361 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2));
[email protected]cb114f142011-11-23 20:18:04362}
363
[email protected]d4f5d1162011-11-30 01:41:52364TEST_F(ProfileInfoCacheTest, GAIAPicture) {
[email protected]22900902014-07-30 11:50:41365 const int kDefaultAvatarIndex = 0;
366 const int kOtherAvatarIndex = 1;
[email protected]979dc4f2014-08-12 22:52:11367 const int kGaiaPictureSize = 256; // Standard size of a Gaia account picture.
[email protected]cb114f142011-11-23 20:18:04368 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13369 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
rogertae22659602015-05-11 21:58:21370 std::string(), base::string16(), kDefaultAvatarIndex, std::string());
[email protected]cb114f142011-11-23 20:18:04371 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13372 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
rogertae22659602015-05-11 21:58:21373 std::string(), base::string16(), kDefaultAvatarIndex, std::string());
[email protected]cb114f142011-11-23 20:18:04374
375 // Sanity check.
[email protected]2f3c00f2011-11-30 04:36:22376 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
377 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(1));
[email protected]cb114f142011-11-23 20:18:04378 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0));
379 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
380
381 // The profile icon should be the default one.
[email protected]22900902014-07-30 11:50:41382 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(0));
383 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
384 int default_avatar_id =
385 profiles::GetDefaultAvatarIconResourceIDAtIndex(kDefaultAvatarIndex);
386 const gfx::Image& default_avatar_image(
387 ResourceBundle::GetSharedInstance().GetImageNamed(default_avatar_id));
pkotwiczb312b512015-09-29 23:44:43388 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]22900902014-07-30 11:50:41389 default_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
[email protected]cb114f142011-11-23 20:18:04390
391 // Set GAIA picture.
[email protected]979dc4f2014-08-12 22:52:11392 gfx::Image gaia_image(gfx::test::CreateImage(
393 kGaiaPictureSize, kGaiaPictureSize));
[email protected]2f3c00f2011-11-30 04:36:22394 GetCache()->SetGAIAPictureOfProfileAtIndex(1, &gaia_image);
395 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
pkotwiczb312b512015-09-29 23:44:43396 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22397 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
[email protected]22900902014-07-30 11:50:41398 // Since we're still using the default avatar, the GAIA image should be
399 // preferred over the generic avatar image.
400 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
401 EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
pkotwiczb312b512015-09-29 23:44:43402 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]22900902014-07-30 11:50:41403 gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
[email protected]cb114f142011-11-23 20:18:04404
[email protected]1cdf0932014-08-16 06:00:49405 // Set a non-default avatar. This should be preferred over the GAIA image.
[email protected]22900902014-07-30 11:50:41406 GetCache()->SetAvatarIconOfProfileAtIndex(1, kOtherAvatarIndex);
[email protected]1cdf0932014-08-16 06:00:49407 GetCache()->SetProfileIsUsingDefaultAvatarAtIndex(1, false);
[email protected]22900902014-07-30 11:50:41408 EXPECT_FALSE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
409 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
410 int other_avatar_id =
411 profiles::GetDefaultAvatarIconResourceIDAtIndex(kOtherAvatarIndex);
412 const gfx::Image& other_avatar_image(
413 ResourceBundle::GetSharedInstance().GetImageNamed(other_avatar_id));
pkotwiczb312b512015-09-29 23:44:43414 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]22900902014-07-30 11:50:41415 other_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
416
417 // Explicitly setting the GAIA picture should make it preferred again.
[email protected]cb114f142011-11-23 20:18:04418 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, true);
[email protected]22900902014-07-30 11:50:41419 EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
pkotwiczb312b512015-09-29 23:44:43420 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22421 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
pkotwiczb312b512015-09-29 23:44:43422 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]cb114f142011-11-23 20:18:04423 gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
424
[email protected]22900902014-07-30 11:50:41425 // Clearing the IsUsingGAIAPicture flag should result in the generic image
426 // being used again.
[email protected]cb114f142011-11-23 20:18:04427 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, false);
[email protected]22900902014-07-30 11:50:41428 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
pkotwiczb312b512015-09-29 23:44:43429 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22430 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
pkotwiczb312b512015-09-29 23:44:43431 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]22900902014-07-30 11:50:41432 other_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
[email protected]cb114f142011-11-23 20:18:04433}
434
[email protected]754bebc2011-12-01 16:42:16435TEST_F(ProfileInfoCacheTest, PersistGAIAPicture) {
[email protected]cb114f142011-11-23 20:18:04436 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13437 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
rogertae22659602015-05-11 21:58:21438 std::string(), base::string16(), 0, std::string());
[email protected]d4f5d1162011-11-30 01:41:52439 gfx::Image gaia_image(gfx::test::CreateImage());
[email protected]cb114f142011-11-23 20:18:04440
[email protected]2f3c00f2011-11-30 04:36:22441 GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_image);
noms75ca0192015-02-26 03:14:30442
443 // Make sure everything has completed, and the file has been written to disk.
444 base::RunLoop().RunUntilIdle();
445
pkotwiczb312b512015-09-29 23:44:43446 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22447 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
[email protected]cb114f142011-11-23 20:18:04448
[email protected]cb114f142011-11-23 20:18:04449 ResetCache();
[email protected]cb114f142011-11-23 20:18:04450 // Try to get the GAIA picture. This should return NULL until the read from
451 // disk is done.
[email protected]2f3c00f2011-11-30 04:36:22452 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
noms75ca0192015-02-26 03:14:30453 base::RunLoop().RunUntilIdle();
454
pkotwiczb312b512015-09-29 23:44:43455 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22456 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
457}
458
[email protected]d20d0432014-06-12 17:14:05459TEST_F(ProfileInfoCacheTest, SetSupervisedUserId) {
[email protected]0aa018a2013-07-31 15:08:54460 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13461 GetProfilePath("test"), ASCIIToUTF16("Test"),
rogertae22659602015-05-11 21:58:21462 std::string(), base::string16(), 0, std::string());
[email protected]d20d0432014-06-12 17:14:05463 EXPECT_FALSE(GetCache()->ProfileIsSupervisedAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54464
[email protected]d20d0432014-06-12 17:14:05465 GetCache()->SetSupervisedUserIdOfProfileAtIndex(0, "TEST_ID");
466 EXPECT_TRUE(GetCache()->ProfileIsSupervisedAtIndex(0));
467 EXPECT_EQ("TEST_ID", GetCache()->GetSupervisedUserIdOfProfileAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54468
469 ResetCache();
[email protected]d20d0432014-06-12 17:14:05470 EXPECT_TRUE(GetCache()->ProfileIsSupervisedAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54471
[email protected]d20d0432014-06-12 17:14:05472 GetCache()->SetSupervisedUserIdOfProfileAtIndex(0, std::string());
473 EXPECT_FALSE(GetCache()->ProfileIsSupervisedAtIndex(0));
474 EXPECT_EQ("", GetCache()->GetSupervisedUserIdOfProfileAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54475}
476
[email protected]2f3c00f2011-11-30 04:36:22477TEST_F(ProfileInfoCacheTest, EmptyGAIAInfo) {
[email protected]a04db822013-12-11 19:14:40478 base::string16 profile_name = ASCIIToUTF16("name_1");
[email protected]c3e559772014-04-09 04:02:54479 int id = profiles::GetDefaultAvatarIconResourceIDAtIndex(0);
[email protected]2f3c00f2011-11-30 04:36:22480 const gfx::Image& profile_image(
481 ResourceBundle::GetSharedInstance().GetImageNamed(id));
482
483 GetCache()->AddProfileToCache(
rogertae22659602015-05-11 21:58:21484 GetProfilePath("path_1"), profile_name, std::string(), base::string16(),
485 0, std::string());
[email protected]2f3c00f2011-11-30 04:36:22486
487 // Set empty GAIA info.
[email protected]a04db822013-12-11 19:14:40488 GetCache()->SetGAIANameOfProfileAtIndex(0, base::string16());
[email protected]2f3c00f2011-11-30 04:36:22489 GetCache()->SetGAIAPictureOfProfileAtIndex(0, NULL);
[email protected]2f3c00f2011-11-30 04:36:22490 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(0, true);
491
492 // Verify that the profile name and picture are not empty.
493 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(0));
pkotwiczb312b512015-09-29 23:44:43494 EXPECT_TRUE(gfx::test::AreImagesEqual(
[email protected]2f3c00f2011-11-30 04:36:22495 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(0)));
[email protected]cb114f142011-11-23 20:18:04496}
497
[email protected]d20d0432014-06-12 17:14:05498TEST_F(ProfileInfoCacheTest, CreateSupervisedTestingProfile) {
[email protected]0aa018a2013-07-31 15:08:54499 testing_profile_manager_.CreateTestingProfile("default");
[email protected]d20d0432014-06-12 17:14:05500 base::string16 supervised_user_name = ASCIIToUTF16("Supervised User");
[email protected]0aa018a2013-07-31 15:08:54501 testing_profile_manager_.CreateTestingProfile(
maxbogueea16ff412016-10-28 16:35:29502 "test1", std::unique_ptr<sync_preferences::PrefServiceSyncable>(),
[email protected]d20d0432014-06-12 17:14:05503 supervised_user_name, 0, "TEST_ID", TestingProfile::TestingFactories());
[email protected]0aa018a2013-07-31 15:08:54504 for (size_t i = 0; i < GetCache()->GetNumberOfProfiles(); i++) {
[email protected]d20d0432014-06-12 17:14:05505 bool is_supervised =
506 GetCache()->GetNameOfProfileAtIndex(i) == supervised_user_name;
507 EXPECT_EQ(is_supervised, GetCache()->ProfileIsSupervisedAtIndex(i));
508 std::string supervised_user_id = is_supervised ? "TEST_ID" : "";
509 EXPECT_EQ(supervised_user_id,
510 GetCache()->GetSupervisedUserIdOfProfileAtIndex(i));
[email protected]0aa018a2013-07-31 15:08:54511 }
[email protected]371662e372013-10-17 22:05:22512
[email protected]d20d0432014-06-12 17:14:05513 // Supervised profiles have a custom theme, which needs to be deleted on the
514 // FILE thread. Reset the profile manager now so everything is deleted while
515 // we still have a FILE thread.
[email protected]371662e372013-10-17 22:05:22516 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
[email protected]0aa018a2013-07-31 15:08:54517}
518
[email protected]047481802013-09-16 22:26:38519TEST_F(ProfileInfoCacheTest, AddStubProfile) {
520 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
521
522 // Add some profiles with and without a '.' in their paths.
523 const struct {
524 const char* profile_path;
525 const char* profile_name;
526 } kTestCases[] = {
527 { "path.test0", "name_0" },
528 { "path_test1", "name_1" },
529 { "path.test2", "name_2" },
530 { "path_test3", "name_3" },
531 };
532
viettrungluu9e65ad12014-10-16 04:22:26533 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
[email protected]047481802013-09-16 22:26:38534 base::FilePath profile_path = GetProfilePath(kTestCases[i].profile_path);
[email protected]a04db822013-12-11 19:14:40535 base::string16 profile_name = ASCIIToUTF16(kTestCases[i].profile_name);
[email protected]047481802013-09-16 22:26:38536
rogertae22659602015-05-11 21:58:21537 GetCache()->AddProfileToCache(profile_path, profile_name, std::string(),
538 base::string16(), i, "");
[email protected]047481802013-09-16 22:26:38539
540 EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i));
541 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
542 }
543
544 ASSERT_EQ(4U, GetCache()->GetNumberOfProfiles());
545
546 // Check that the profiles can be extracted from the local state.
noms693f35aa2015-02-05 16:03:16547 std::vector<base::string16> names;
548 PrefService* local_state = g_browser_process->local_state();
549 const base::DictionaryValue* cache = local_state->GetDictionary(
550 prefs::kProfileInfoCache);
551 base::string16 name;
552 for (base::DictionaryValue::Iterator it(*cache); !it.IsAtEnd();
553 it.Advance()) {
554 const base::DictionaryValue* info = NULL;
555 it.value().GetAsDictionary(&info);
556 info->GetString("name", &name);
557 names.push_back(name);
558 }
559
[email protected]047481802013-09-16 22:26:38560 for (size_t i = 0; i < 4; i++)
561 ASSERT_FALSE(names[i].empty());
562}
563
lwchkg778965f2016-05-12 13:14:22564TEST_F(ProfileInfoCacheTest, EntriesInAttributesStorage) {
565 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
566
567 // Add some profiles with and without a '.' in their paths.
568 const struct {
569 const char* profile_path;
570 const char* profile_name;
571 } kTestCases[] = {
572 { "path.test0", "name_0" },
573 { "path_test1", "name_1" },
574 { "path.test2", "name_2" },
575 { "path_test3", "name_3" },
576 };
577
578 // Profiles are added and removed using all combinations of the old and the
579 // new interfaces. The content of |profile_attributes_entries_| in
580 // ProfileAttributesStorage is checked after each insert and delete operation.
581
582 // Add profiles.
583 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
584 base::FilePath profile_path = GetProfilePath(kTestCases[i].profile_path);
585 base::string16 profile_name = ASCIIToUTF16(kTestCases[i].profile_name);
586
587 ASSERT_EQ(0u, GetCache()->profile_attributes_entries_.count(
588 profile_path.value()));
589
590 // Use ProfileInfoCache in profiles 0 and 2, and ProfileAttributesStorage in
591 // profiles 1 and 3.
592 if (i | 1u) {
593 GetCache()->AddProfileToCache(profile_path, profile_name, std::string(),
594 base::string16(), i, "");
595 } else {
596 GetCache()->AddProfile(profile_path, profile_name, std::string(),
597 base::string16(), i, "");
598 }
599
600 ASSERT_EQ(i + 1, GetCache()->GetNumberOfProfiles());
601 ASSERT_EQ(i + 1, GetCache()->profile_attributes_entries_.size());
602
603 ASSERT_EQ(1u, GetCache()->profile_attributes_entries_.count(
604 profile_path.value()));
605 // TODO(anthonyvd) : check that the entry in |profile_attributes_entries_|
606 // is null before GetProfileAttributesWithPath is run. Currently this is
607 // impossible to check because GetProfileAttributesWithPath is called during
608 // profile creation.
609
610 ProfileAttributesEntry* entry = nullptr;
611 GetCache()->GetProfileAttributesWithPath(profile_path, &entry);
612 EXPECT_EQ(
613 entry,
614 GetCache()->profile_attributes_entries_[profile_path.value()].get());
615 }
616
617 // Remove profiles.
618 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
619 base::FilePath profile_path = GetProfilePath(kTestCases[i].profile_path);
620 ASSERT_EQ(1u, GetCache()->profile_attributes_entries_.count(
621 profile_path.value()));
622
623 // Use ProfileInfoCache in profiles 0 and 1, and ProfileAttributesStorage in
624 // profiles 2 and 3.
625 if (i | 2u)
626 GetCache()->DeleteProfileFromCache(profile_path);
627 else
628 GetCache()->RemoveProfile(profile_path);
629
630 ASSERT_EQ(0u, GetCache()->profile_attributes_entries_.count(
631 profile_path.value()));
632
633 ProfileAttributesEntry* entry = nullptr;
634 EXPECT_FALSE(GetCache()->GetProfileAttributesWithPath(profile_path,
635 &entry));
636 ASSERT_EQ(0u, GetCache()->profile_attributes_entries_.count(
637 profile_path.value()));
638 }
639}
640
[email protected]b09bfa12014-08-15 19:05:25641// High res avatar downloading is only supported on desktop.
jam1c5a91492016-02-24 20:47:53642#if !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
[email protected]9de44302014-05-01 16:32:27643TEST_F(ProfileInfoCacheTest, DownloadHighResAvatarTest) {
mlerman3ab32772015-04-08 14:48:56644 // The TestingProfileManager's ProfileInfoCache doesn't download avatars.
645 ProfileInfoCache profile_info_cache(g_browser_process->local_state(),
646 testing_profile_manager_.profile_manager()->user_data_dir());
647
emaxxa2e11492015-05-12 17:19:11648 // Make sure there are no avatars already on disk.
mlerman3ab32772015-04-08 14:48:56649 const size_t kIconIndex = 0;
650 base::FilePath icon_path =
651 profiles::GetPathOfHighResAvatarAtIndex(kIconIndex);
652 EXPECT_FALSE(base::PathExists(icon_path));
653
654 EXPECT_EQ(0U, profile_info_cache.GetNumberOfProfiles());
[email protected]9de44302014-05-01 16:32:27655 base::FilePath path_1 = GetProfilePath("path_1");
mlerman3ab32772015-04-08 14:48:56656 profile_info_cache.AddProfileToCache(path_1, ASCIIToUTF16("name_1"),
rogertae22659602015-05-11 21:58:21657 std::string(), base::string16(), kIconIndex, std::string());
mlerman3ab32772015-04-08 14:48:56658 EXPECT_EQ(1U, profile_info_cache.GetNumberOfProfiles());
noms28693ab22015-01-28 21:44:54659 base::RunLoop().RunUntilIdle();
[email protected]9de44302014-05-01 16:32:27660
661 // We haven't downloaded any high-res avatars yet.
mlerman3ab32772015-04-08 14:48:56662 EXPECT_EQ(0U, profile_info_cache.cached_avatar_images_.size());
[email protected]5cf8bcd92014-07-17 20:27:40663
664 // After adding a new profile, the download of high-res avatar will be
665 // triggered if the flag kNewAvatarMenu has been set. But the downloader
666 // won't ever call OnFetchComplete in the test.
mlerman3ab32772015-04-08 14:48:56667 EXPECT_EQ(1U, profile_info_cache.avatar_images_downloads_in_progress_.size());
[email protected]5cf8bcd92014-07-17 20:27:40668
mlerman3ab32772015-04-08 14:48:56669 EXPECT_FALSE(profile_info_cache.GetHighResAvatarOfProfileAtIndex(0));
[email protected]9de44302014-05-01 16:32:27670
671 // Simulate downloading a high-res avatar.
[email protected]2d605712014-05-14 12:03:07672 ProfileAvatarDownloader avatar_downloader(
lwchkg3f4491202016-03-30 16:49:52673 kIconIndex,
674 base::Bind(&ProfileInfoCache::SaveAvatarImageAtPath,
675 base::Unretained(&profile_info_cache),
676 profile_info_cache.GetPathOfProfileAtIndex(0)));
[email protected]9de44302014-05-01 16:32:27677
678 // Put a real bitmap into "bitmap". 2x2 bitmap of green 32 bit pixels.
679 SkBitmap bitmap;
[email protected]e61cad52014-07-01 21:01:57680 bitmap.allocN32Pixels(2, 2);
[email protected]9de44302014-05-01 16:32:27681 bitmap.eraseColor(SK_ColorGREEN);
682
683 avatar_downloader.OnFetchComplete(
684 GURL("http://www.google.com/avatar.png"), &bitmap);
685
treibaf255502015-01-30 17:47:53686 // Now the download should not be in progress anymore.
mlerman3ab32772015-04-08 14:48:56687 EXPECT_EQ(0U, profile_info_cache.avatar_images_downloads_in_progress_.size());
treibaf255502015-01-30 17:47:53688
[email protected]9de44302014-05-01 16:32:27689 std::string file_name =
690 profiles::GetDefaultAvatarIconFileNameAtIndex(kIconIndex);
691
692 // The file should have been cached and saved.
mlerman3ab32772015-04-08 14:48:56693 EXPECT_EQ(1U, profile_info_cache.cached_avatar_images_.size());
694 EXPECT_TRUE(profile_info_cache.GetHighResAvatarOfProfileAtIndex(0));
lwchkgb17d86c2016-05-12 11:16:51695 EXPECT_EQ(profile_info_cache.cached_avatar_images_[file_name].get(),
mlerman3ab32772015-04-08 14:48:56696 profile_info_cache.GetHighResAvatarOfProfileAtIndex(0));
[email protected]9de44302014-05-01 16:32:27697
698 // Make sure everything has completed, and the file has been written to disk.
699 base::RunLoop().RunUntilIdle();
700
701 // Clean up.
[email protected]9de44302014-05-01 16:32:27702 EXPECT_NE(std::string::npos, icon_path.MaybeAsASCII().find(file_name));
703 EXPECT_TRUE(base::PathExists(icon_path));
thestig16a4bcae2015-12-07 08:36:17704 EXPECT_TRUE(base::DeleteFile(icon_path, false));
[email protected]9de44302014-05-01 16:32:27705 EXPECT_FALSE(base::PathExists(icon_path));
706}
nomsbe073e52014-08-23 22:01:04707
emaxx0cb1957132015-05-18 20:22:54708TEST_F(ProfileInfoCacheTest, NothingToDownloadHighResAvatarTest) {
emaxx0cb1957132015-05-18 20:22:54709 // The TestingProfileManager's ProfileInfoCache doesn't download avatars.
710 ProfileInfoCache profile_info_cache(
711 g_browser_process->local_state(),
712 testing_profile_manager_.profile_manager()->user_data_dir());
713
714 const size_t kIconIndex = profiles::GetPlaceholderAvatarIndex();
715
716 EXPECT_EQ(0U, profile_info_cache.GetNumberOfProfiles());
717 base::FilePath path_1 = GetProfilePath("path_1");
718 profile_info_cache.AddProfileToCache(path_1, ASCIIToUTF16("name_1"),
719 std::string(), base::string16(),
720 kIconIndex, std::string());
721 EXPECT_EQ(1U, profile_info_cache.GetNumberOfProfiles());
722 base::RunLoop().RunUntilIdle();
723
724 // We haven't tried to download any high-res avatars as the specified icon is
725 // just a placeholder.
726 EXPECT_EQ(0U, profile_info_cache.cached_avatar_images_.size());
727 EXPECT_EQ(0U, profile_info_cache.avatar_images_downloads_in_progress_.size());
728}
729
nomsbe073e52014-08-23 22:01:04730TEST_F(ProfileInfoCacheTest, MigrateLegacyProfileNamesWithNewAvatarMenu) {
nomsbe073e52014-08-23 22:01:04731 EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles());
732
733 base::FilePath path_1 = GetProfilePath("path_1");
734 GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("Default Profile"),
rogertae22659602015-05-11 21:58:21735 std::string(), base::string16(), 0,
736 std::string());
nomsbe073e52014-08-23 22:01:04737 base::FilePath path_2 = GetProfilePath("path_2");
738 GetCache()->AddProfileToCache(path_2, ASCIIToUTF16("First user"),
rogertae22659602015-05-11 21:58:21739 std::string(), base::string16(), 1,
740 std::string());
nomsbe073e52014-08-23 22:01:04741 base::string16 name_3 = ASCIIToUTF16("Lemonade");
742 base::FilePath path_3 = GetProfilePath("path_3");
743 GetCache()->AddProfileToCache(path_3, name_3,
rogertae22659602015-05-11 21:58:21744 std::string(), base::string16(), 2,
745 std::string());
nomsbe073e52014-08-23 22:01:04746 base::string16 name_4 = ASCIIToUTF16("Batman");
747 base::FilePath path_4 = GetProfilePath("path_4");
748 GetCache()->AddProfileToCache(path_4, name_4,
rogertae22659602015-05-11 21:58:21749 std::string(), base::string16(), 3,
750 std::string());
nomsbe073e52014-08-23 22:01:04751 base::string16 name_5 = ASCIIToUTF16("Person 2");
752 base::FilePath path_5 = GetProfilePath("path_5");
753 GetCache()->AddProfileToCache(path_5, name_5,
rogertae22659602015-05-11 21:58:21754 std::string(), base::string16(), 2,
755 std::string());
nomsbe073e52014-08-23 22:01:04756
757 EXPECT_EQ(5U, GetCache()->GetNumberOfProfiles());
758
759
760 ResetCache();
761
762 // Legacy profile names like "Default Profile" and "First user" should be
763 // migrated to "Person %n" type names.
764 EXPECT_EQ(ASCIIToUTF16("Person 1"), GetCache()->GetNameOfProfileAtIndex(
765 GetCache()->GetIndexOfProfileWithPath(path_1)));
766 EXPECT_EQ(ASCIIToUTF16("Person 3"), GetCache()->GetNameOfProfileAtIndex(
767 GetCache()->GetIndexOfProfileWithPath(path_2)));
768
769 // Other profile names should not be migrated even if they're the old
770 // default cartoon profile names.
771 EXPECT_EQ(name_3, GetCache()->GetNameOfProfileAtIndex(
772 GetCache()->GetIndexOfProfileWithPath(path_3)));
773 EXPECT_EQ(name_4, GetCache()->GetNameOfProfileAtIndex(
774 GetCache()->GetIndexOfProfileWithPath(path_4)));
775 EXPECT_EQ(name_5, GetCache()->GetNameOfProfileAtIndex(
776 GetCache()->GetIndexOfProfileWithPath(path_5)));
777}
[email protected]b09bfa12014-08-15 19:05:25778#endif
nomsbe073e52014-08-23 22:01:04779
jam1c5a91492016-02-24 20:47:53780#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
nomsbe073e52014-08-23 22:01:04781TEST_F(ProfileInfoCacheTest,
782 DontMigrateLegacyProfileNamesWithoutNewAvatarMenu) {
nomsbe073e52014-08-23 22:01:04783 EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles());
784
785 base::string16 name_1 = ASCIIToUTF16("Default Profile");
786 base::FilePath path_1 = GetProfilePath("path_1");
787 GetCache()->AddProfileToCache(path_1, name_1,
rogertae22659602015-05-11 21:58:21788 std::string(), base::string16(), 0,
789 std::string());
nomsbe073e52014-08-23 22:01:04790 base::string16 name_2 = ASCIIToUTF16("First user");
791 base::FilePath path_2 = GetProfilePath("path_2");
792 GetCache()->AddProfileToCache(path_2, name_2,
rogertae22659602015-05-11 21:58:21793 std::string(), base::string16(), 1,
794 std::string());
nomsbe073e52014-08-23 22:01:04795 base::string16 name_3 = ASCIIToUTF16("Lemonade");
796 base::FilePath path_3 = GetProfilePath("path_3");
797 GetCache()->AddProfileToCache(path_3, name_3,
rogertae22659602015-05-11 21:58:21798 std::string(), base::string16(), 2,
799 std::string());
nomsbe073e52014-08-23 22:01:04800 base::string16 name_4 = ASCIIToUTF16("Batman");
801 base::FilePath path_4 = GetProfilePath("path_4");
802 GetCache()->AddProfileToCache(path_4, name_4,
rogertae22659602015-05-11 21:58:21803 std::string(), base::string16(), 3,
804 std::string());
nomsbe073e52014-08-23 22:01:04805 EXPECT_EQ(4U, GetCache()->GetNumberOfProfiles());
806
807 ResetCache();
808
809 // Profile names should have been preserved.
810 EXPECT_EQ(name_1, GetCache()->GetNameOfProfileAtIndex(
811 GetCache()->GetIndexOfProfileWithPath(path_1)));
812 EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(
813 GetCache()->GetIndexOfProfileWithPath(path_2)));
814 EXPECT_EQ(name_3, GetCache()->GetNameOfProfileAtIndex(
815 GetCache()->GetIndexOfProfileWithPath(path_3)));
816 EXPECT_EQ(name_4, GetCache()->GetNameOfProfileAtIndex(
817 GetCache()->GetIndexOfProfileWithPath(path_4)));
818}
anthonyvd66aa95d2015-10-02 15:40:26819#endif