blob: 92638ae3e89c7c0cb20cda2c61ff0284c206453c [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
[email protected]047481802013-09-16 22:26:387#include <vector>
8
[email protected]9de44302014-05-01 16:32:279#include "base/file_util.h"
[email protected]e5ba874f2013-02-14 17:20:1910#include "base/prefs/testing_pref_service.h"
[email protected]76fb05c2013-06-11 04:38:0511#include "base/strings/stringprintf.h"
[email protected]e309f312013-06-07 21:50:0812#include "base/strings/utf_string_conversions.h"
[email protected]85503a02014-01-16 17:27:2213#include "base/time/time.h"
[email protected]583844c2011-08-27 00:38:3514#include "chrome/browser/browser_process.h"
[email protected]25ff0862013-07-12 00:59:0315#include "chrome/browser/chrome_notification_types.h"
[email protected]0aa018a2013-07-31 15:08:5416#include "chrome/browser/prefs/pref_service_syncable.h"
[email protected]9de44302014-05-01 16:32:2717#include "chrome/browser/profiles/profile_avatar_downloader.h"
[email protected]c3e559772014-04-09 04:02:5418#include "chrome/browser/profiles/profile_avatar_icon_util.h"
[email protected]d4f5d1162011-11-30 01:41:5219#include "chrome/browser/profiles/profile_info_cache.h"
[email protected]6730b1e2011-09-29 05:23:5220#include "chrome/browser/profiles/profile_manager.h"
[email protected]583844c2011-08-27 00:38:3521#include "chrome/test/base/testing_browser_process.h"
[email protected]cb114f142011-11-23 20:18:0422#include "content/public/browser/notification_observer.h"
23#include "content/public/browser/notification_registrar.h"
[email protected]c0b030902011-12-19 16:21:0524#include "content/public/browser/notification_service.h"
[email protected]371662e372013-10-17 22:05:2225#include "content/public/test/test_browser_thread_bundle.h"
[email protected]a7fe9112012-07-20 02:34:4526#include "content/public/test/test_utils.h"
[email protected]dee810e2011-06-27 19:43:3927#include "third_party/skia/include/core/SkBitmap.h"
28#include "ui/base/resource/resource_bundle.h"
29#include "ui/gfx/image/image.h"
[email protected]d4f5d1162011-11-30 01:41:5230#include "ui/gfx/image/image_unittest_util.h"
[email protected]dee810e2011-06-27 19:43:3931
[email protected]6778fed2013-12-24 20:09:3732using base::ASCIIToUTF16;
[email protected]f729d7a2013-12-26 07:07:5633using base::UTF8ToUTF16;
[email protected]cb114f142011-11-23 20:18:0434using content::BrowserThread;
35
[email protected]6a460662011-12-22 22:05:1636ProfileNameVerifierObserver::ProfileNameVerifierObserver(
37 TestingProfileManager* testing_profile_manager)
38 : testing_profile_manager_(testing_profile_manager) {
39 DCHECK(testing_profile_manager_);
[email protected]590e189b2011-12-13 22:07:0340}
41
42ProfileNameVerifierObserver::~ProfileNameVerifierObserver() {
43}
44
45void ProfileNameVerifierObserver::OnProfileAdded(
[email protected]650b2d52013-02-10 03:41:4546 const base::FilePath& profile_path) {
[email protected]a04db822013-12-11 19:14:4047 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1648 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0349 EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end());
50 profile_names_.insert(profile_name);
51}
52
[email protected]7b0af152011-12-16 17:02:0653void ProfileNameVerifierObserver::OnProfileWillBeRemoved(
[email protected]650b2d52013-02-10 03:41:4554 const base::FilePath& profile_path) {
[email protected]a04db822013-12-11 19:14:4055 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1656 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0357 EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end());
58 profile_names_.erase(profile_name);
59}
60
[email protected]7b0af152011-12-16 17:02:0661void ProfileNameVerifierObserver::OnProfileWasRemoved(
[email protected]650b2d52013-02-10 03:41:4562 const base::FilePath& profile_path,
[email protected]a04db822013-12-11 19:14:4063 const base::string16& profile_name) {
[email protected]7b0af152011-12-16 17:02:0664 EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end());
65}
66
[email protected]590e189b2011-12-13 22:07:0367void ProfileNameVerifierObserver::OnProfileNameChanged(
[email protected]650b2d52013-02-10 03:41:4568 const base::FilePath& profile_path,
[email protected]a04db822013-12-11 19:14:4069 const base::string16& old_profile_name) {
70 base::string16 new_profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1671 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0372 EXPECT_TRUE(profile_names_.find(old_profile_name) != profile_names_.end());
73 EXPECT_TRUE(profile_names_.find(new_profile_name) == profile_names_.end());
74 profile_names_.erase(old_profile_name);
75 profile_names_.insert(new_profile_name);
76}
77
78void ProfileNameVerifierObserver::OnProfileAvatarChanged(
[email protected]650b2d52013-02-10 03:41:4579 const base::FilePath& profile_path) {
[email protected]a04db822013-12-11 19:14:4080 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1681 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0382 EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end());
83}
84
[email protected]6a460662011-12-22 22:05:1685ProfileInfoCache* ProfileNameVerifierObserver::GetCache() {
86 return testing_profile_manager_->profile_info_cache();
87}
88
[email protected]d4f5d1162011-11-30 01:41:5289ProfileInfoCacheTest::ProfileInfoCacheTest()
[email protected]0aa018a2013-07-31 15:08:5490 : testing_profile_manager_(TestingBrowserProcess::GetGlobal()),
[email protected]6a460662011-12-22 22:05:1691 name_observer_(&testing_profile_manager_) {
[email protected]d4f5d1162011-11-30 01:41:5292}
93
94ProfileInfoCacheTest::~ProfileInfoCacheTest() {
95}
96
97void ProfileInfoCacheTest::SetUp() {
98 ASSERT_TRUE(testing_profile_manager_.SetUp());
[email protected]590e189b2011-12-13 22:07:0399 testing_profile_manager_.profile_info_cache()->AddObserver(&name_observer_);
[email protected]d4f5d1162011-11-30 01:41:52100}
101
[email protected]1302dcf2011-11-30 21:47:05102void ProfileInfoCacheTest::TearDown() {
103 // Drain the UI thread to make sure all tasks are completed. This prevents
104 // memory leaks.
[email protected]0aa018a2013-07-31 15:08:54105 base::RunLoop().RunUntilIdle();
[email protected]1302dcf2011-11-30 21:47:05106}
107
[email protected]d4f5d1162011-11-30 01:41:52108ProfileInfoCache* ProfileInfoCacheTest::GetCache() {
109 return testing_profile_manager_.profile_info_cache();
110}
111
[email protected]650b2d52013-02-10 03:41:45112base::FilePath ProfileInfoCacheTest::GetProfilePath(
[email protected]d4f5d1162011-11-30 01:41:52113 const std::string& base_name) {
114 return testing_profile_manager_.profile_manager()->user_data_dir().
115 AppendASCII(base_name);
116}
117
118void ProfileInfoCacheTest::ResetCache() {
119 testing_profile_manager_.DeleteProfileInfoCache();
120}
121
[email protected]d4f5d1162011-11-30 01:41:52122TEST_F(ProfileInfoCacheTest, AddProfiles) {
[email protected]6730b1e2011-09-29 05:23:52123 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39124
[email protected]ca591072012-03-27 01:54:44125 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
[email protected]dee810e2011-06-27 19:43:39126 for (uint32 i = 0; i < 4; ++i) {
[email protected]7d3cbc92013-03-18 22:33:04127 base::FilePath profile_path =
128 GetProfilePath(base::StringPrintf("path_%ud", i));
[email protected]a04db822013-12-11 19:14:40129 base::string16 profile_name =
130 ASCIIToUTF16(base::StringPrintf("name_%ud", i));
[email protected]ca591072012-03-27 01:54:44131 const SkBitmap* icon = rb.GetImageNamed(
[email protected]c3e559772014-04-09 04:02:54132 profiles::GetDefaultAvatarIconResourceIDAtIndex(
[email protected]ca591072012-03-27 01:54:44133 i)).ToSkBitmap();
[email protected]d20d0432014-06-12 17:14:05134 std::string supervised_user_id = i == 3 ? "TEST_ID" : "";
[email protected]dee810e2011-06-27 19:43:39135
[email protected]a04db822013-12-11 19:14:40136 GetCache()->AddProfileToCache(profile_path, profile_name, base::string16(),
[email protected]d20d0432014-06-12 17:14:05137 i, supervised_user_id);
[email protected]cb114f142011-11-23 20:18:04138 GetCache()->SetBackgroundStatusOfProfileAtIndex(i, true);
[email protected]a04db822013-12-11 19:14:40139 base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
[email protected]cb114f142011-11-23 20:18:04140 GetCache()->SetGAIANameOfProfileAtIndex(i, gaia_name);
[email protected]dee810e2011-06-27 19:43:39141
[email protected]6730b1e2011-09-29 05:23:52142 EXPECT_EQ(i + 1, GetCache()->GetNumberOfProfiles());
143 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
144 EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i));
[email protected]0aa018a2013-07-31 15:08:54145 const SkBitmap* actual_icon =
146 GetCache()->GetAvatarIconOfProfileAtIndex(i).ToSkBitmap();
[email protected]ca591072012-03-27 01:54:44147 EXPECT_EQ(icon->width(), actual_icon->width());
148 EXPECT_EQ(icon->height(), actual_icon->height());
[email protected]d20d0432014-06-12 17:14:05149 EXPECT_EQ(i == 3, GetCache()->ProfileIsSupervisedAtIndex(i));
[email protected]5ddfade2014-02-03 10:24:53150 EXPECT_EQ(i == 3, GetCache()->IsOmittedProfileAtIndex(i));
[email protected]d20d0432014-06-12 17:14:05151 EXPECT_EQ(supervised_user_id,
152 GetCache()->GetSupervisedUserIdOfProfileAtIndex(i));
[email protected]dee810e2011-06-27 19:43:39153 }
[email protected]cb114f142011-11-23 20:18:04154
155 // Reset the cache and test the it reloads correctly.
156 ResetCache();
157
158 EXPECT_EQ(4u, GetCache()->GetNumberOfProfiles());
159 for (uint32 i = 0; i < 4; ++i) {
[email protected]7d3cbc92013-03-18 22:33:04160 base::FilePath profile_path =
161 GetProfilePath(base::StringPrintf("path_%ud", i));
[email protected]cb114f142011-11-23 20:18:04162 EXPECT_EQ(i, GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]a04db822013-12-11 19:14:40163 base::string16 profile_name =
164 ASCIIToUTF16(base::StringPrintf("name_%ud", i));
[email protected]cb114f142011-11-23 20:18:04165 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
166 EXPECT_EQ(i, GetCache()->GetAvatarIconIndexOfProfileAtIndex(i));
167 EXPECT_EQ(true, GetCache()->GetBackgroundStatusOfProfileAtIndex(i));
[email protected]a04db822013-12-11 19:14:40168 base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
[email protected]cb114f142011-11-23 20:18:04169 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(i));
170 }
[email protected]dee810e2011-06-27 19:43:39171}
172
[email protected]d4f5d1162011-11-30 01:41:52173TEST_F(ProfileInfoCacheTest, DeleteProfile) {
[email protected]6730b1e2011-09-29 05:23:52174 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39175
[email protected]650b2d52013-02-10 03:41:45176 base::FilePath path_1 = GetProfilePath("path_1");
[email protected]a04db822013-12-11 19:14:40177 GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("name_1"),
178 base::string16(), 0, std::string());
[email protected]6730b1e2011-09-29 05:23:52179 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39180
[email protected]650b2d52013-02-10 03:41:45181 base::FilePath path_2 = GetProfilePath("path_2");
[email protected]a04db822013-12-11 19:14:40182 base::string16 name_2 = ASCIIToUTF16("name_2");
183 GetCache()->AddProfileToCache(path_2, name_2, base::string16(), 0,
184 std::string());
[email protected]6730b1e2011-09-29 05:23:52185 EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39186
[email protected]6730b1e2011-09-29 05:23:52187 GetCache()->DeleteProfileFromCache(path_1);
188 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
189 EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(0));
[email protected]dee810e2011-06-27 19:43:39190
[email protected]6730b1e2011-09-29 05:23:52191 GetCache()->DeleteProfileFromCache(path_2);
192 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39193}
194
[email protected]d4f5d1162011-11-30 01:41:52195TEST_F(ProfileInfoCacheTest, MutateProfile) {
196 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13197 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
[email protected]a04db822013-12-11 19:14:40198 base::string16(), 0, std::string());
[email protected]d4f5d1162011-11-30 01:41:52199 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13200 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
[email protected]a04db822013-12-11 19:14:40201 base::string16(), 0, std::string());
[email protected]dee810e2011-06-27 19:43:39202
[email protected]a04db822013-12-11 19:14:40203 base::string16 new_name = ASCIIToUTF16("new_name");
[email protected]6730b1e2011-09-29 05:23:52204 GetCache()->SetNameOfProfileAtIndex(1, new_name);
205 EXPECT_EQ(new_name, GetCache()->GetNameOfProfileAtIndex(1));
206 EXPECT_NE(new_name, GetCache()->GetNameOfProfileAtIndex(0));
[email protected]dee810e2011-06-27 19:43:39207
[email protected]a04db822013-12-11 19:14:40208 base::string16 new_user_name = ASCIIToUTF16("user_name");
[email protected]e8e78092011-09-29 18:15:38209 GetCache()->SetUserNameOfProfileAtIndex(1, new_user_name);
210 EXPECT_EQ(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(1));
211 EXPECT_NE(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(0));
212
[email protected]dee810e2011-06-27 19:43:39213 size_t new_icon_index = 3;
[email protected]6730b1e2011-09-29 05:23:52214 GetCache()->SetAvatarIconOfProfileAtIndex(1, new_icon_index);
[email protected]dee810e2011-06-27 19:43:39215 // Not much to test.
[email protected]6730b1e2011-09-29 05:23:52216 GetCache()->GetAvatarIconOfProfileAtIndex(1);
[email protected]dee810e2011-06-27 19:43:39217}
218
[email protected]d4f5d1162011-11-30 01:41:52219TEST_F(ProfileInfoCacheTest, Sort) {
[email protected]a04db822013-12-11 19:14:40220 base::string16 name_a = ASCIIToUTF16("apple");
[email protected]d4f5d1162011-11-30 01:41:52221 GetCache()->AddProfileToCache(
[email protected]a04db822013-12-11 19:14:40222 GetProfilePath("path_a"), name_a, base::string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04223
[email protected]a04db822013-12-11 19:14:40224 base::string16 name_c = ASCIIToUTF16("cat");
[email protected]d4f5d1162011-11-30 01:41:52225 GetCache()->AddProfileToCache(
[email protected]a04db822013-12-11 19:14:40226 GetProfilePath("path_c"), name_c, base::string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04227
228 // Sanity check the initial order.
229 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
230 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
231
232 // Add a new profile (start with a capital to test case insensitive sorting.
[email protected]a04db822013-12-11 19:14:40233 base::string16 name_b = ASCIIToUTF16("Banana");
[email protected]d4f5d1162011-11-30 01:41:52234 GetCache()->AddProfileToCache(
[email protected]a04db822013-12-11 19:14:40235 GetProfilePath("path_b"), name_b, base::string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04236
237 // Verify the new order.
238 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
239 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(1));
240 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(2));
241
242 // Change the name of an existing profile.
243 name_a = UTF8ToUTF16("dog");
244 GetCache()->SetNameOfProfileAtIndex(0, name_a);
245
246 // Verify the new order.
247 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
248 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
249 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(2));
250
251 // Delete a profile.
[email protected]d4f5d1162011-11-30 01:41:52252 GetCache()->DeleteProfileFromCache(GetProfilePath("path_c"));
[email protected]cb114f142011-11-23 20:18:04253
254 // Verify the new order.
255 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
256 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(1));
257}
258
[email protected]d4f5d1162011-11-30 01:41:52259TEST_F(ProfileInfoCacheTest, BackgroundModeStatus) {
[email protected]279170832011-10-12 23:38:03260 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13261 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
[email protected]a04db822013-12-11 19:14:40262 base::string16(), 0, std::string());
[email protected]279170832011-10-12 23:38:03263 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13264 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
[email protected]a04db822013-12-11 19:14:40265 base::string16(), 0, std::string());
[email protected]279170832011-10-12 23:38:03266
267 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
268 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
269
270 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, true);
271
272 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
273 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
274
275 GetCache()->SetBackgroundStatusOfProfileAtIndex(0, true);
276
277 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
278 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
279
280 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, false);
281
282 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
283 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
284}
285
[email protected]85503a02014-01-16 17:27:22286TEST_F(ProfileInfoCacheTest, ProfileActiveTime) {
287 GetCache()->AddProfileToCache(
288 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
289 base::string16(), 0, std::string());
290 EXPECT_EQ(base::Time(), GetCache()->GetProfileActiveTimeAtIndex(0));
[email protected]87727e02014-02-27 18:16:53291 // Before & After times are artificially shifted because just relying upon
292 // the system time can yield problems due to inaccuracies in the
293 // underlying storage system (which uses a double with only 52 bits of
294 // precision to store the 64-bit "time" number). http://crbug.com/346827
[email protected]85503a02014-01-16 17:27:22295 base::Time before = base::Time::Now();
[email protected]87727e02014-02-27 18:16:53296 before -= base::TimeDelta::FromSeconds(1);
[email protected]85503a02014-01-16 17:27:22297 GetCache()->SetProfileActiveTimeAtIndex(0);
298 base::Time after = base::Time::Now();
[email protected]87727e02014-02-27 18:16:53299 after += base::TimeDelta::FromSeconds(1);
[email protected]85503a02014-01-16 17:27:22300 EXPECT_LE(before, GetCache()->GetProfileActiveTimeAtIndex(0));
301 EXPECT_GE(after, GetCache()->GetProfileActiveTimeAtIndex(0));
302}
303
[email protected]d4f5d1162011-11-30 01:41:52304TEST_F(ProfileInfoCacheTest, GAIAName) {
[email protected]cb114f142011-11-23 20:18:04305 GetCache()->AddProfileToCache(
[email protected]a4352d02014-03-06 06:00:27306 GetProfilePath("path_1"), ASCIIToUTF16("Person 1"),
[email protected]a04db822013-12-11 19:14:40307 base::string16(), 0, std::string());
[email protected]a4352d02014-03-06 06:00:27308 base::string16 profile_name(ASCIIToUTF16("Person 2"));
[email protected]cb114f142011-11-23 20:18:04309 GetCache()->AddProfileToCache(
[email protected]a04db822013-12-11 19:14:40310 GetProfilePath("path_2"), profile_name, base::string16(), 0,
311 std::string());
[email protected]cb114f142011-11-23 20:18:04312
[email protected]a4352d02014-03-06 06:00:27313 int index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
314 int index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
315
[email protected]cb114f142011-11-23 20:18:04316 // Sanity check.
[email protected]a4352d02014-03-06 06:00:27317 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty());
318 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index2).empty());
[email protected]cb114f142011-11-23 20:18:04319
[email protected]425d6ab2014-03-25 05:59:39320 // Set GAIA name. This re-sorts the cache.
[email protected]a04db822013-12-11 19:14:40321 base::string16 gaia_name(ASCIIToUTF16("Pat Smith"));
[email protected]a4352d02014-03-06 06:00:27322 GetCache()->SetGAIANameOfProfileAtIndex(index2, gaia_name);
[email protected]425d6ab2014-03-25 05:59:39323 index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
324 index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
325
326 // Since there is a GAIA name, we use that as a display name.
[email protected]a4352d02014-03-06 06:00:27327 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty());
328 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2));
[email protected]a4352d02014-03-06 06:00:27329 EXPECT_EQ(gaia_name, GetCache()->GetNameOfProfileAtIndex(index2));
[email protected]cb114f142011-11-23 20:18:04330
[email protected]a4352d02014-03-06 06:00:27331 // Don't use GAIA name as profile name. This re-sorts the cache.
[email protected]425d6ab2014-03-25 05:59:39332 base::string16 custom_name(ASCIIToUTF16("Custom name"));
333 GetCache()->SetNameOfProfileAtIndex(index2, custom_name);
[email protected]a4352d02014-03-06 06:00:27334 index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
335 index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
336
[email protected]425d6ab2014-03-25 05:59:39337 EXPECT_EQ(custom_name, GetCache()->GetNameOfProfileAtIndex(index2));
[email protected]a4352d02014-03-06 06:00:27338 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2));
[email protected]cb114f142011-11-23 20:18:04339}
340
[email protected]d4f5d1162011-11-30 01:41:52341TEST_F(ProfileInfoCacheTest, GAIAPicture) {
[email protected]cb114f142011-11-23 20:18:04342 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13343 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
[email protected]a04db822013-12-11 19:14:40344 base::string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04345 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13346 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
[email protected]a04db822013-12-11 19:14:40347 base::string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04348
349 // Sanity check.
[email protected]2f3c00f2011-11-30 04:36:22350 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
351 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(1));
[email protected]cb114f142011-11-23 20:18:04352 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0));
353 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
354
355 // The profile icon should be the default one.
[email protected]c3e559772014-04-09 04:02:54356 int id = profiles::GetDefaultAvatarIconResourceIDAtIndex(0);
[email protected]cb114f142011-11-23 20:18:04357 const gfx::Image& profile_image(
358 ResourceBundle::GetSharedInstance().GetImageNamed(id));
[email protected]d4f5d1162011-11-30 01:41:52359 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]cb114f142011-11-23 20:18:04360 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
361
362 // Set GAIA picture.
[email protected]d4f5d1162011-11-30 01:41:52363 gfx::Image gaia_image(gfx::test::CreateImage());
[email protected]2f3c00f2011-11-30 04:36:22364 GetCache()->SetGAIAPictureOfProfileAtIndex(1, &gaia_image);
365 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
[email protected]d4f5d1162011-11-30 01:41:52366 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22367 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
[email protected]d4f5d1162011-11-30 01:41:52368 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]cb114f142011-11-23 20:18:04369 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
370
371 // Use GAIA picture as profile picture.
372 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, true);
[email protected]d4f5d1162011-11-30 01:41:52373 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22374 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
[email protected]d4f5d1162011-11-30 01:41:52375 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]cb114f142011-11-23 20:18:04376 gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
377
378 // Don't use GAIA picture as profile picture.
379 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, false);
[email protected]d4f5d1162011-11-30 01:41:52380 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22381 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
[email protected]d4f5d1162011-11-30 01:41:52382 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]cb114f142011-11-23 20:18:04383 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
384}
385
[email protected]754bebc2011-12-01 16:42:16386TEST_F(ProfileInfoCacheTest, PersistGAIAPicture) {
[email protected]cb114f142011-11-23 20:18:04387 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13388 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
[email protected]a04db822013-12-11 19:14:40389 base::string16(), 0, std::string());
[email protected]d4f5d1162011-11-30 01:41:52390 gfx::Image gaia_image(gfx::test::CreateImage());
[email protected]cb114f142011-11-23 20:18:04391
[email protected]a7fe9112012-07-20 02:34:45392 content::WindowedNotificationObserver save_observer(
[email protected]cb114f142011-11-23 20:18:04393 chrome::NOTIFICATION_PROFILE_CACHE_PICTURE_SAVED,
394 content::NotificationService::AllSources());
[email protected]2f3c00f2011-11-30 04:36:22395 GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_image);
[email protected]d4f5d1162011-11-30 01:41:52396 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22397 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
[email protected]cb114f142011-11-23 20:18:04398
399 // Wait for the file to be written to disk then reset the cache.
400 save_observer.Wait();
401 ResetCache();
402
403 // Try to get the GAIA picture. This should return NULL until the read from
404 // disk is done.
[email protected]a7fe9112012-07-20 02:34:45405 content::WindowedNotificationObserver read_observer(
[email protected]cb114f142011-11-23 20:18:04406 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED,
407 content::NotificationService::AllSources());
[email protected]2f3c00f2011-11-30 04:36:22408 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
[email protected]cb114f142011-11-23 20:18:04409 read_observer.Wait();
[email protected]d4f5d1162011-11-30 01:41:52410 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22411 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
412}
413
[email protected]d20d0432014-06-12 17:14:05414TEST_F(ProfileInfoCacheTest, SetSupervisedUserId) {
[email protected]0aa018a2013-07-31 15:08:54415 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13416 GetProfilePath("test"), ASCIIToUTF16("Test"),
[email protected]a04db822013-12-11 19:14:40417 base::string16(), 0, std::string());
[email protected]d20d0432014-06-12 17:14:05418 EXPECT_FALSE(GetCache()->ProfileIsSupervisedAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54419
[email protected]d20d0432014-06-12 17:14:05420 GetCache()->SetSupervisedUserIdOfProfileAtIndex(0, "TEST_ID");
421 EXPECT_TRUE(GetCache()->ProfileIsSupervisedAtIndex(0));
422 EXPECT_EQ("TEST_ID", GetCache()->GetSupervisedUserIdOfProfileAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54423
424 ResetCache();
[email protected]d20d0432014-06-12 17:14:05425 EXPECT_TRUE(GetCache()->ProfileIsSupervisedAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54426
[email protected]d20d0432014-06-12 17:14:05427 GetCache()->SetSupervisedUserIdOfProfileAtIndex(0, std::string());
428 EXPECT_FALSE(GetCache()->ProfileIsSupervisedAtIndex(0));
429 EXPECT_EQ("", GetCache()->GetSupervisedUserIdOfProfileAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54430}
431
[email protected]2f3c00f2011-11-30 04:36:22432TEST_F(ProfileInfoCacheTest, EmptyGAIAInfo) {
[email protected]a04db822013-12-11 19:14:40433 base::string16 profile_name = ASCIIToUTF16("name_1");
[email protected]c3e559772014-04-09 04:02:54434 int id = profiles::GetDefaultAvatarIconResourceIDAtIndex(0);
[email protected]2f3c00f2011-11-30 04:36:22435 const gfx::Image& profile_image(
436 ResourceBundle::GetSharedInstance().GetImageNamed(id));
437
438 GetCache()->AddProfileToCache(
[email protected]a04db822013-12-11 19:14:40439 GetProfilePath("path_1"), profile_name, base::string16(), 0,
440 std::string());
[email protected]2f3c00f2011-11-30 04:36:22441
442 // Set empty GAIA info.
[email protected]a04db822013-12-11 19:14:40443 GetCache()->SetGAIANameOfProfileAtIndex(0, base::string16());
[email protected]2f3c00f2011-11-30 04:36:22444 GetCache()->SetGAIAPictureOfProfileAtIndex(0, NULL);
[email protected]2f3c00f2011-11-30 04:36:22445 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(0, true);
446
447 // Verify that the profile name and picture are not empty.
448 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(0));
449 EXPECT_TRUE(gfx::test::IsEqual(
450 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(0)));
[email protected]cb114f142011-11-23 20:18:04451}
452
[email protected]d20d0432014-06-12 17:14:05453TEST_F(ProfileInfoCacheTest, CreateSupervisedTestingProfile) {
[email protected]0aa018a2013-07-31 15:08:54454 testing_profile_manager_.CreateTestingProfile("default");
[email protected]d20d0432014-06-12 17:14:05455 base::string16 supervised_user_name = ASCIIToUTF16("Supervised User");
[email protected]0aa018a2013-07-31 15:08:54456 testing_profile_manager_.CreateTestingProfile(
[email protected]a6e01b42013-08-05 14:13:13457 "test1", scoped_ptr<PrefServiceSyncable>(),
[email protected]d20d0432014-06-12 17:14:05458 supervised_user_name, 0, "TEST_ID", TestingProfile::TestingFactories());
[email protected]0aa018a2013-07-31 15:08:54459 for (size_t i = 0; i < GetCache()->GetNumberOfProfiles(); i++) {
[email protected]d20d0432014-06-12 17:14:05460 bool is_supervised =
461 GetCache()->GetNameOfProfileAtIndex(i) == supervised_user_name;
462 EXPECT_EQ(is_supervised, GetCache()->ProfileIsSupervisedAtIndex(i));
463 std::string supervised_user_id = is_supervised ? "TEST_ID" : "";
464 EXPECT_EQ(supervised_user_id,
465 GetCache()->GetSupervisedUserIdOfProfileAtIndex(i));
[email protected]0aa018a2013-07-31 15:08:54466 }
[email protected]371662e372013-10-17 22:05:22467
[email protected]d20d0432014-06-12 17:14:05468 // Supervised profiles have a custom theme, which needs to be deleted on the
469 // FILE thread. Reset the profile manager now so everything is deleted while
470 // we still have a FILE thread.
[email protected]371662e372013-10-17 22:05:22471 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
[email protected]0aa018a2013-07-31 15:08:54472}
473
[email protected]047481802013-09-16 22:26:38474TEST_F(ProfileInfoCacheTest, AddStubProfile) {
475 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
476
477 // Add some profiles with and without a '.' in their paths.
478 const struct {
479 const char* profile_path;
480 const char* profile_name;
481 } kTestCases[] = {
482 { "path.test0", "name_0" },
483 { "path_test1", "name_1" },
484 { "path.test2", "name_2" },
485 { "path_test3", "name_3" },
486 };
487
488 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) {
489 base::FilePath profile_path = GetProfilePath(kTestCases[i].profile_path);
[email protected]a04db822013-12-11 19:14:40490 base::string16 profile_name = ASCIIToUTF16(kTestCases[i].profile_name);
[email protected]047481802013-09-16 22:26:38491
[email protected]a04db822013-12-11 19:14:40492 GetCache()->AddProfileToCache(profile_path, profile_name, base::string16(),
493 i, "");
[email protected]047481802013-09-16 22:26:38494
495 EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i));
496 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
497 }
498
499 ASSERT_EQ(4U, GetCache()->GetNumberOfProfiles());
500
501 // Check that the profiles can be extracted from the local state.
[email protected]d2065e062013-12-12 23:49:52502 std::vector<base::string16> names = ProfileInfoCache::GetProfileNames();
[email protected]047481802013-09-16 22:26:38503 for (size_t i = 0; i < 4; i++)
504 ASSERT_FALSE(names[i].empty());
505}
506
[email protected]9de44302014-05-01 16:32:27507TEST_F(ProfileInfoCacheTest, DownloadHighResAvatarTest) {
508 EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles());
509 base::FilePath path_1 = GetProfilePath("path_1");
510 GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("name_1"),
511 base::string16(), 0, std::string());
512 EXPECT_EQ(1U, GetCache()->GetNumberOfProfiles());
513
514 // We haven't downloaded any high-res avatars yet.
515 EXPECT_EQ(0U, GetCache()->cached_avatar_images_.size());
516 EXPECT_EQ(0U, GetCache()->avatar_images_downloads_in_progress_.size());
517 EXPECT_FALSE(GetCache()->GetHighResAvatarOfProfileAtIndex(0));
518
519 // Simulate downloading a high-res avatar.
520 const size_t kIconIndex = 0;
[email protected]2d605712014-05-14 12:03:07521 ProfileAvatarDownloader avatar_downloader(
522 kIconIndex, GetCache()->GetPathOfProfileAtIndex(0), GetCache());
[email protected]9de44302014-05-01 16:32:27523
524 // Put a real bitmap into "bitmap". 2x2 bitmap of green 32 bit pixels.
525 SkBitmap bitmap;
526 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 2, 2);
527 bitmap.allocPixels();
528 bitmap.eraseColor(SK_ColorGREEN);
529
530 avatar_downloader.OnFetchComplete(
531 GURL("http://www.google.com/avatar.png"), &bitmap);
532
533 std::string file_name =
534 profiles::GetDefaultAvatarIconFileNameAtIndex(kIconIndex);
535
536 // The file should have been cached and saved.
537 EXPECT_EQ(0U, GetCache()->avatar_images_downloads_in_progress_.size());
538 EXPECT_EQ(1U, GetCache()->cached_avatar_images_.size());
539 EXPECT_TRUE(GetCache()->GetHighResAvatarOfProfileAtIndex(0));
540 EXPECT_EQ(GetCache()->cached_avatar_images_[file_name],
541 GetCache()->GetHighResAvatarOfProfileAtIndex(0));
542
543 // Make sure everything has completed, and the file has been written to disk.
544 base::RunLoop().RunUntilIdle();
545
546 // Clean up.
547 base::FilePath icon_path =
548 profiles::GetPathOfHighResAvatarAtIndex(kIconIndex);
549 EXPECT_NE(std::string::npos, icon_path.MaybeAsASCII().find(file_name));
550 EXPECT_TRUE(base::PathExists(icon_path));
551 EXPECT_TRUE(base::DeleteFile(icon_path, true));
552 EXPECT_FALSE(base::PathExists(icon_path));
553}