blob: 82c1f3f1d30e7a29a1a678d4a373910424f2be56 [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]5cf8bcd92014-07-17 20:27:409#include "base/command_line.h"
[email protected]9de44302014-05-01 16:32:2710#include "base/file_util.h"
[email protected]e5ba874f2013-02-14 17:20:1911#include "base/prefs/testing_pref_service.h"
[email protected]76fb05c2013-06-11 04:38:0512#include "base/strings/stringprintf.h"
[email protected]e309f312013-06-07 21:50:0813#include "base/strings/utf_string_conversions.h"
[email protected]85503a02014-01-16 17:27:2214#include "base/time/time.h"
[email protected]583844c2011-08-27 00:38:3515#include "chrome/browser/browser_process.h"
[email protected]25ff0862013-07-12 00:59:0316#include "chrome/browser/chrome_notification_types.h"
[email protected]0aa018a2013-07-31 15:08:5417#include "chrome/browser/prefs/pref_service_syncable.h"
[email protected]9de44302014-05-01 16:32:2718#include "chrome/browser/profiles/profile_avatar_downloader.h"
[email protected]c3e559772014-04-09 04:02:5419#include "chrome/browser/profiles/profile_avatar_icon_util.h"
[email protected]d4f5d1162011-11-30 01:41:5220#include "chrome/browser/profiles/profile_info_cache.h"
[email protected]6730b1e2011-09-29 05:23:5221#include "chrome/browser/profiles/profile_manager.h"
[email protected]5cf8bcd92014-07-17 20:27:4022#include "chrome/common/chrome_switches.h"
[email protected]583844c2011-08-27 00:38:3523#include "chrome/test/base/testing_browser_process.h"
[email protected]5cf8bcd92014-07-17 20:27:4024#include "components/signin/core/common/profile_management_switches.h"
[email protected]cb114f142011-11-23 20:18:0425#include "content/public/browser/notification_observer.h"
26#include "content/public/browser/notification_registrar.h"
[email protected]c0b030902011-12-19 16:21:0527#include "content/public/browser/notification_service.h"
[email protected]371662e372013-10-17 22:05:2228#include "content/public/test/test_browser_thread_bundle.h"
[email protected]a7fe9112012-07-20 02:34:4529#include "content/public/test/test_utils.h"
[email protected]dee810e2011-06-27 19:43:3930#include "third_party/skia/include/core/SkBitmap.h"
31#include "ui/base/resource/resource_bundle.h"
32#include "ui/gfx/image/image.h"
[email protected]d4f5d1162011-11-30 01:41:5233#include "ui/gfx/image/image_unittest_util.h"
[email protected]dee810e2011-06-27 19:43:3934
[email protected]6778fed2013-12-24 20:09:3735using base::ASCIIToUTF16;
[email protected]f729d7a2013-12-26 07:07:5636using base::UTF8ToUTF16;
[email protected]cb114f142011-11-23 20:18:0437using content::BrowserThread;
38
[email protected]6a460662011-12-22 22:05:1639ProfileNameVerifierObserver::ProfileNameVerifierObserver(
40 TestingProfileManager* testing_profile_manager)
41 : testing_profile_manager_(testing_profile_manager) {
42 DCHECK(testing_profile_manager_);
[email protected]590e189b2011-12-13 22:07:0343}
44
45ProfileNameVerifierObserver::~ProfileNameVerifierObserver() {
46}
47
48void ProfileNameVerifierObserver::OnProfileAdded(
[email protected]650b2d52013-02-10 03:41:4549 const base::FilePath& profile_path) {
[email protected]a04db822013-12-11 19:14:4050 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1651 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0352 EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end());
53 profile_names_.insert(profile_name);
54}
55
[email protected]7b0af152011-12-16 17:02:0656void ProfileNameVerifierObserver::OnProfileWillBeRemoved(
[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_.erase(profile_name);
62}
63
[email protected]7b0af152011-12-16 17:02:0664void ProfileNameVerifierObserver::OnProfileWasRemoved(
[email protected]650b2d52013-02-10 03:41:4565 const base::FilePath& profile_path,
[email protected]a04db822013-12-11 19:14:4066 const base::string16& profile_name) {
[email protected]7b0af152011-12-16 17:02:0667 EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end());
68}
69
[email protected]590e189b2011-12-13 22:07:0370void ProfileNameVerifierObserver::OnProfileNameChanged(
[email protected]650b2d52013-02-10 03:41:4571 const base::FilePath& profile_path,
[email protected]a04db822013-12-11 19:14:4072 const base::string16& old_profile_name) {
73 base::string16 new_profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1674 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0375 EXPECT_TRUE(profile_names_.find(old_profile_name) != profile_names_.end());
76 EXPECT_TRUE(profile_names_.find(new_profile_name) == profile_names_.end());
77 profile_names_.erase(old_profile_name);
78 profile_names_.insert(new_profile_name);
79}
80
81void ProfileNameVerifierObserver::OnProfileAvatarChanged(
[email protected]650b2d52013-02-10 03:41:4582 const base::FilePath& profile_path) {
[email protected]a04db822013-12-11 19:14:4083 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1684 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0385 EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end());
86}
87
[email protected]6a460662011-12-22 22:05:1688ProfileInfoCache* ProfileNameVerifierObserver::GetCache() {
89 return testing_profile_manager_->profile_info_cache();
90}
91
[email protected]d4f5d1162011-11-30 01:41:5292ProfileInfoCacheTest::ProfileInfoCacheTest()
[email protected]0aa018a2013-07-31 15:08:5493 : testing_profile_manager_(TestingBrowserProcess::GetGlobal()),
[email protected]6a460662011-12-22 22:05:1694 name_observer_(&testing_profile_manager_) {
[email protected]d4f5d1162011-11-30 01:41:5295}
96
97ProfileInfoCacheTest::~ProfileInfoCacheTest() {
98}
99
100void ProfileInfoCacheTest::SetUp() {
101 ASSERT_TRUE(testing_profile_manager_.SetUp());
[email protected]590e189b2011-12-13 22:07:03102 testing_profile_manager_.profile_info_cache()->AddObserver(&name_observer_);
[email protected]d4f5d1162011-11-30 01:41:52103}
104
[email protected]1302dcf2011-11-30 21:47:05105void ProfileInfoCacheTest::TearDown() {
106 // Drain the UI thread to make sure all tasks are completed. This prevents
107 // memory leaks.
[email protected]0aa018a2013-07-31 15:08:54108 base::RunLoop().RunUntilIdle();
[email protected]1302dcf2011-11-30 21:47:05109}
110
[email protected]d4f5d1162011-11-30 01:41:52111ProfileInfoCache* ProfileInfoCacheTest::GetCache() {
112 return testing_profile_manager_.profile_info_cache();
113}
114
[email protected]650b2d52013-02-10 03:41:45115base::FilePath ProfileInfoCacheTest::GetProfilePath(
[email protected]d4f5d1162011-11-30 01:41:52116 const std::string& base_name) {
117 return testing_profile_manager_.profile_manager()->user_data_dir().
118 AppendASCII(base_name);
119}
120
121void ProfileInfoCacheTest::ResetCache() {
122 testing_profile_manager_.DeleteProfileInfoCache();
123}
124
[email protected]d4f5d1162011-11-30 01:41:52125TEST_F(ProfileInfoCacheTest, AddProfiles) {
[email protected]6730b1e2011-09-29 05:23:52126 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39127
[email protected]ca591072012-03-27 01:54:44128 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
[email protected]dee810e2011-06-27 19:43:39129 for (uint32 i = 0; i < 4; ++i) {
[email protected]7d3cbc92013-03-18 22:33:04130 base::FilePath profile_path =
131 GetProfilePath(base::StringPrintf("path_%ud", i));
[email protected]a04db822013-12-11 19:14:40132 base::string16 profile_name =
133 ASCIIToUTF16(base::StringPrintf("name_%ud", i));
[email protected]ca591072012-03-27 01:54:44134 const SkBitmap* icon = rb.GetImageNamed(
[email protected]c3e559772014-04-09 04:02:54135 profiles::GetDefaultAvatarIconResourceIDAtIndex(
[email protected]ca591072012-03-27 01:54:44136 i)).ToSkBitmap();
[email protected]d20d0432014-06-12 17:14:05137 std::string supervised_user_id = i == 3 ? "TEST_ID" : "";
[email protected]dee810e2011-06-27 19:43:39138
[email protected]a04db822013-12-11 19:14:40139 GetCache()->AddProfileToCache(profile_path, profile_name, base::string16(),
[email protected]d20d0432014-06-12 17:14:05140 i, supervised_user_id);
[email protected]cb114f142011-11-23 20:18:04141 GetCache()->SetBackgroundStatusOfProfileAtIndex(i, true);
[email protected]a04db822013-12-11 19:14:40142 base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
[email protected]cb114f142011-11-23 20:18:04143 GetCache()->SetGAIANameOfProfileAtIndex(i, gaia_name);
[email protected]dee810e2011-06-27 19:43:39144
[email protected]6730b1e2011-09-29 05:23:52145 EXPECT_EQ(i + 1, GetCache()->GetNumberOfProfiles());
146 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
147 EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i));
[email protected]0aa018a2013-07-31 15:08:54148 const SkBitmap* actual_icon =
149 GetCache()->GetAvatarIconOfProfileAtIndex(i).ToSkBitmap();
[email protected]ca591072012-03-27 01:54:44150 EXPECT_EQ(icon->width(), actual_icon->width());
151 EXPECT_EQ(icon->height(), actual_icon->height());
[email protected]d20d0432014-06-12 17:14:05152 EXPECT_EQ(i == 3, GetCache()->ProfileIsSupervisedAtIndex(i));
[email protected]5ddfade2014-02-03 10:24:53153 EXPECT_EQ(i == 3, GetCache()->IsOmittedProfileAtIndex(i));
[email protected]d20d0432014-06-12 17:14:05154 EXPECT_EQ(supervised_user_id,
155 GetCache()->GetSupervisedUserIdOfProfileAtIndex(i));
[email protected]dee810e2011-06-27 19:43:39156 }
[email protected]cb114f142011-11-23 20:18:04157
158 // Reset the cache and test the it reloads correctly.
159 ResetCache();
160
161 EXPECT_EQ(4u, GetCache()->GetNumberOfProfiles());
162 for (uint32 i = 0; i < 4; ++i) {
[email protected]7d3cbc92013-03-18 22:33:04163 base::FilePath profile_path =
164 GetProfilePath(base::StringPrintf("path_%ud", i));
[email protected]cb114f142011-11-23 20:18:04165 EXPECT_EQ(i, GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]a04db822013-12-11 19:14:40166 base::string16 profile_name =
167 ASCIIToUTF16(base::StringPrintf("name_%ud", i));
[email protected]cb114f142011-11-23 20:18:04168 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
169 EXPECT_EQ(i, GetCache()->GetAvatarIconIndexOfProfileAtIndex(i));
170 EXPECT_EQ(true, GetCache()->GetBackgroundStatusOfProfileAtIndex(i));
[email protected]a04db822013-12-11 19:14:40171 base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
[email protected]cb114f142011-11-23 20:18:04172 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(i));
173 }
[email protected]dee810e2011-06-27 19:43:39174}
175
[email protected]d4f5d1162011-11-30 01:41:52176TEST_F(ProfileInfoCacheTest, DeleteProfile) {
[email protected]6730b1e2011-09-29 05:23:52177 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39178
[email protected]650b2d52013-02-10 03:41:45179 base::FilePath path_1 = GetProfilePath("path_1");
[email protected]a04db822013-12-11 19:14:40180 GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("name_1"),
181 base::string16(), 0, std::string());
[email protected]6730b1e2011-09-29 05:23:52182 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39183
[email protected]650b2d52013-02-10 03:41:45184 base::FilePath path_2 = GetProfilePath("path_2");
[email protected]a04db822013-12-11 19:14:40185 base::string16 name_2 = ASCIIToUTF16("name_2");
186 GetCache()->AddProfileToCache(path_2, name_2, base::string16(), 0,
187 std::string());
[email protected]6730b1e2011-09-29 05:23:52188 EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39189
[email protected]6730b1e2011-09-29 05:23:52190 GetCache()->DeleteProfileFromCache(path_1);
191 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
192 EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(0));
[email protected]dee810e2011-06-27 19:43:39193
[email protected]6730b1e2011-09-29 05:23:52194 GetCache()->DeleteProfileFromCache(path_2);
195 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39196}
197
[email protected]d4f5d1162011-11-30 01:41:52198TEST_F(ProfileInfoCacheTest, MutateProfile) {
199 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13200 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
[email protected]a04db822013-12-11 19:14:40201 base::string16(), 0, std::string());
[email protected]d4f5d1162011-11-30 01:41:52202 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13203 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
[email protected]a04db822013-12-11 19:14:40204 base::string16(), 0, std::string());
[email protected]dee810e2011-06-27 19:43:39205
[email protected]a04db822013-12-11 19:14:40206 base::string16 new_name = ASCIIToUTF16("new_name");
[email protected]6730b1e2011-09-29 05:23:52207 GetCache()->SetNameOfProfileAtIndex(1, new_name);
208 EXPECT_EQ(new_name, GetCache()->GetNameOfProfileAtIndex(1));
209 EXPECT_NE(new_name, GetCache()->GetNameOfProfileAtIndex(0));
[email protected]dee810e2011-06-27 19:43:39210
[email protected]a04db822013-12-11 19:14:40211 base::string16 new_user_name = ASCIIToUTF16("user_name");
[email protected]e8e78092011-09-29 18:15:38212 GetCache()->SetUserNameOfProfileAtIndex(1, new_user_name);
213 EXPECT_EQ(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(1));
214 EXPECT_NE(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(0));
215
[email protected]dee810e2011-06-27 19:43:39216 size_t new_icon_index = 3;
[email protected]6730b1e2011-09-29 05:23:52217 GetCache()->SetAvatarIconOfProfileAtIndex(1, new_icon_index);
[email protected]dee810e2011-06-27 19:43:39218 // Not much to test.
[email protected]6730b1e2011-09-29 05:23:52219 GetCache()->GetAvatarIconOfProfileAtIndex(1);
[email protected]dee810e2011-06-27 19:43:39220}
221
[email protected]d4f5d1162011-11-30 01:41:52222TEST_F(ProfileInfoCacheTest, Sort) {
[email protected]a04db822013-12-11 19:14:40223 base::string16 name_a = ASCIIToUTF16("apple");
[email protected]d4f5d1162011-11-30 01:41:52224 GetCache()->AddProfileToCache(
[email protected]a04db822013-12-11 19:14:40225 GetProfilePath("path_a"), name_a, base::string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04226
[email protected]a04db822013-12-11 19:14:40227 base::string16 name_c = ASCIIToUTF16("cat");
[email protected]d4f5d1162011-11-30 01:41:52228 GetCache()->AddProfileToCache(
[email protected]a04db822013-12-11 19:14:40229 GetProfilePath("path_c"), name_c, base::string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04230
231 // Sanity check the initial order.
232 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
233 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
234
235 // Add a new profile (start with a capital to test case insensitive sorting.
[email protected]a04db822013-12-11 19:14:40236 base::string16 name_b = ASCIIToUTF16("Banana");
[email protected]d4f5d1162011-11-30 01:41:52237 GetCache()->AddProfileToCache(
[email protected]a04db822013-12-11 19:14:40238 GetProfilePath("path_b"), name_b, base::string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04239
240 // Verify the new order.
241 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
242 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(1));
243 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(2));
244
245 // Change the name of an existing profile.
246 name_a = UTF8ToUTF16("dog");
247 GetCache()->SetNameOfProfileAtIndex(0, name_a);
248
249 // Verify the new order.
250 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
251 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
252 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(2));
253
254 // Delete a profile.
[email protected]d4f5d1162011-11-30 01:41:52255 GetCache()->DeleteProfileFromCache(GetProfilePath("path_c"));
[email protected]cb114f142011-11-23 20:18:04256
257 // Verify the new order.
258 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
259 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(1));
260}
261
[email protected]d4f5d1162011-11-30 01:41:52262TEST_F(ProfileInfoCacheTest, BackgroundModeStatus) {
[email protected]279170832011-10-12 23:38:03263 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13264 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
[email protected]a04db822013-12-11 19:14:40265 base::string16(), 0, std::string());
[email protected]279170832011-10-12 23:38:03266 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13267 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
[email protected]a04db822013-12-11 19:14:40268 base::string16(), 0, std::string());
[email protected]279170832011-10-12 23:38:03269
270 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
271 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
272
273 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, true);
274
275 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
276 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
277
278 GetCache()->SetBackgroundStatusOfProfileAtIndex(0, true);
279
280 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
281 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
282
283 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, false);
284
285 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
286 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
287}
288
[email protected]85503a02014-01-16 17:27:22289TEST_F(ProfileInfoCacheTest, ProfileActiveTime) {
290 GetCache()->AddProfileToCache(
291 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
292 base::string16(), 0, std::string());
293 EXPECT_EQ(base::Time(), GetCache()->GetProfileActiveTimeAtIndex(0));
[email protected]87727e02014-02-27 18:16:53294 // Before & After times are artificially shifted because just relying upon
295 // the system time can yield problems due to inaccuracies in the
296 // underlying storage system (which uses a double with only 52 bits of
297 // precision to store the 64-bit "time" number). http://crbug.com/346827
[email protected]85503a02014-01-16 17:27:22298 base::Time before = base::Time::Now();
[email protected]87727e02014-02-27 18:16:53299 before -= base::TimeDelta::FromSeconds(1);
[email protected]85503a02014-01-16 17:27:22300 GetCache()->SetProfileActiveTimeAtIndex(0);
301 base::Time after = base::Time::Now();
[email protected]87727e02014-02-27 18:16:53302 after += base::TimeDelta::FromSeconds(1);
[email protected]85503a02014-01-16 17:27:22303 EXPECT_LE(before, GetCache()->GetProfileActiveTimeAtIndex(0));
304 EXPECT_GE(after, GetCache()->GetProfileActiveTimeAtIndex(0));
305}
306
[email protected]d4f5d1162011-11-30 01:41:52307TEST_F(ProfileInfoCacheTest, GAIAName) {
[email protected]cb114f142011-11-23 20:18:04308 GetCache()->AddProfileToCache(
[email protected]a4352d02014-03-06 06:00:27309 GetProfilePath("path_1"), ASCIIToUTF16("Person 1"),
[email protected]a04db822013-12-11 19:14:40310 base::string16(), 0, std::string());
[email protected]a4352d02014-03-06 06:00:27311 base::string16 profile_name(ASCIIToUTF16("Person 2"));
[email protected]cb114f142011-11-23 20:18:04312 GetCache()->AddProfileToCache(
[email protected]a04db822013-12-11 19:14:40313 GetProfilePath("path_2"), profile_name, base::string16(), 0,
314 std::string());
[email protected]cb114f142011-11-23 20:18:04315
[email protected]a4352d02014-03-06 06:00:27316 int index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
317 int index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
318
[email protected]cb114f142011-11-23 20:18:04319 // Sanity check.
[email protected]a4352d02014-03-06 06:00:27320 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty());
321 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index2).empty());
[email protected]cb114f142011-11-23 20:18:04322
[email protected]425d6ab2014-03-25 05:59:39323 // Set GAIA name. This re-sorts the cache.
[email protected]a04db822013-12-11 19:14:40324 base::string16 gaia_name(ASCIIToUTF16("Pat Smith"));
[email protected]a4352d02014-03-06 06:00:27325 GetCache()->SetGAIANameOfProfileAtIndex(index2, gaia_name);
[email protected]425d6ab2014-03-25 05:59:39326 index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
327 index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
328
329 // Since there is a GAIA name, we use that as a display name.
[email protected]a4352d02014-03-06 06:00:27330 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty());
331 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2));
[email protected]a4352d02014-03-06 06:00:27332 EXPECT_EQ(gaia_name, GetCache()->GetNameOfProfileAtIndex(index2));
[email protected]cb114f142011-11-23 20:18:04333
[email protected]a4352d02014-03-06 06:00:27334 // Don't use GAIA name as profile name. This re-sorts the cache.
[email protected]425d6ab2014-03-25 05:59:39335 base::string16 custom_name(ASCIIToUTF16("Custom name"));
336 GetCache()->SetNameOfProfileAtIndex(index2, custom_name);
[email protected]a4352d02014-03-06 06:00:27337 index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
338 index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
339
[email protected]425d6ab2014-03-25 05:59:39340 EXPECT_EQ(custom_name, GetCache()->GetNameOfProfileAtIndex(index2));
[email protected]a4352d02014-03-06 06:00:27341 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2));
[email protected]cb114f142011-11-23 20:18:04342}
343
[email protected]d4f5d1162011-11-30 01:41:52344TEST_F(ProfileInfoCacheTest, GAIAPicture) {
[email protected]22900902014-07-30 11:50:41345 const int kDefaultAvatarIndex = 0;
346 const int kOtherAvatarIndex = 1;
[email protected]979dc4f2014-08-12 22:52:11347 const int kGaiaPictureSize = 256; // Standard size of a Gaia account picture.
[email protected]cb114f142011-11-23 20:18:04348 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13349 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
[email protected]22900902014-07-30 11:50:41350 base::string16(), kDefaultAvatarIndex, std::string());
[email protected]cb114f142011-11-23 20:18:04351 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13352 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
[email protected]22900902014-07-30 11:50:41353 base::string16(), kDefaultAvatarIndex, std::string());
[email protected]cb114f142011-11-23 20:18:04354
355 // Sanity check.
[email protected]2f3c00f2011-11-30 04:36:22356 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
357 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(1));
[email protected]cb114f142011-11-23 20:18:04358 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0));
359 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
360
361 // The profile icon should be the default one.
[email protected]22900902014-07-30 11:50:41362 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(0));
363 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
364 int default_avatar_id =
365 profiles::GetDefaultAvatarIconResourceIDAtIndex(kDefaultAvatarIndex);
366 const gfx::Image& default_avatar_image(
367 ResourceBundle::GetSharedInstance().GetImageNamed(default_avatar_id));
[email protected]d4f5d1162011-11-30 01:41:52368 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]22900902014-07-30 11:50:41369 default_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
[email protected]cb114f142011-11-23 20:18:04370
371 // Set GAIA picture.
[email protected]979dc4f2014-08-12 22:52:11372 gfx::Image gaia_image(gfx::test::CreateImage(
373 kGaiaPictureSize, kGaiaPictureSize));
[email protected]2f3c00f2011-11-30 04:36:22374 GetCache()->SetGAIAPictureOfProfileAtIndex(1, &gaia_image);
375 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
[email protected]d4f5d1162011-11-30 01:41:52376 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22377 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
[email protected]22900902014-07-30 11:50:41378 // Since we're still using the default avatar, the GAIA image should be
379 // preferred over the generic avatar image.
380 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
381 EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
[email protected]d4f5d1162011-11-30 01:41:52382 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]22900902014-07-30 11:50:41383 gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
[email protected]cb114f142011-11-23 20:18:04384
[email protected]22900902014-07-30 11:50:41385 // Set another avatar. This should make it preferred over the GAIA image.
386 GetCache()->SetAvatarIconOfProfileAtIndex(1, kOtherAvatarIndex);
387 EXPECT_FALSE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
388 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
389 int other_avatar_id =
390 profiles::GetDefaultAvatarIconResourceIDAtIndex(kOtherAvatarIndex);
391 const gfx::Image& other_avatar_image(
392 ResourceBundle::GetSharedInstance().GetImageNamed(other_avatar_id));
393 EXPECT_TRUE(gfx::test::IsEqual(
394 other_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
395
396 // Explicitly setting the GAIA picture should make it preferred again.
[email protected]cb114f142011-11-23 20:18:04397 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, true);
[email protected]22900902014-07-30 11:50:41398 EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
[email protected]d4f5d1162011-11-30 01:41:52399 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22400 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
[email protected]d4f5d1162011-11-30 01:41:52401 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]cb114f142011-11-23 20:18:04402 gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
403
[email protected]22900902014-07-30 11:50:41404 // Clearing the IsUsingGAIAPicture flag should result in the generic image
405 // being used again.
[email protected]cb114f142011-11-23 20:18:04406 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, false);
[email protected]22900902014-07-30 11:50:41407 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
[email protected]d4f5d1162011-11-30 01:41:52408 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22409 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
[email protected]d4f5d1162011-11-30 01:41:52410 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]22900902014-07-30 11:50:41411 other_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
[email protected]cb114f142011-11-23 20:18:04412}
413
[email protected]754bebc2011-12-01 16:42:16414TEST_F(ProfileInfoCacheTest, PersistGAIAPicture) {
[email protected]cb114f142011-11-23 20:18:04415 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13416 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
[email protected]a04db822013-12-11 19:14:40417 base::string16(), 0, std::string());
[email protected]d4f5d1162011-11-30 01:41:52418 gfx::Image gaia_image(gfx::test::CreateImage());
[email protected]cb114f142011-11-23 20:18:04419
[email protected]a7fe9112012-07-20 02:34:45420 content::WindowedNotificationObserver save_observer(
[email protected]cb114f142011-11-23 20:18:04421 chrome::NOTIFICATION_PROFILE_CACHE_PICTURE_SAVED,
422 content::NotificationService::AllSources());
[email protected]2f3c00f2011-11-30 04:36:22423 GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_image);
[email protected]d4f5d1162011-11-30 01:41:52424 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22425 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
[email protected]cb114f142011-11-23 20:18:04426
427 // Wait for the file to be written to disk then reset the cache.
428 save_observer.Wait();
429 ResetCache();
430
431 // Try to get the GAIA picture. This should return NULL until the read from
432 // disk is done.
[email protected]a7fe9112012-07-20 02:34:45433 content::WindowedNotificationObserver read_observer(
[email protected]cb114f142011-11-23 20:18:04434 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED,
435 content::NotificationService::AllSources());
[email protected]2f3c00f2011-11-30 04:36:22436 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
[email protected]cb114f142011-11-23 20:18:04437 read_observer.Wait();
[email protected]d4f5d1162011-11-30 01:41:52438 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22439 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
440}
441
[email protected]d20d0432014-06-12 17:14:05442TEST_F(ProfileInfoCacheTest, SetSupervisedUserId) {
[email protected]0aa018a2013-07-31 15:08:54443 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13444 GetProfilePath("test"), ASCIIToUTF16("Test"),
[email protected]a04db822013-12-11 19:14:40445 base::string16(), 0, std::string());
[email protected]d20d0432014-06-12 17:14:05446 EXPECT_FALSE(GetCache()->ProfileIsSupervisedAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54447
[email protected]d20d0432014-06-12 17:14:05448 GetCache()->SetSupervisedUserIdOfProfileAtIndex(0, "TEST_ID");
449 EXPECT_TRUE(GetCache()->ProfileIsSupervisedAtIndex(0));
450 EXPECT_EQ("TEST_ID", GetCache()->GetSupervisedUserIdOfProfileAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54451
452 ResetCache();
[email protected]d20d0432014-06-12 17:14:05453 EXPECT_TRUE(GetCache()->ProfileIsSupervisedAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54454
[email protected]d20d0432014-06-12 17:14:05455 GetCache()->SetSupervisedUserIdOfProfileAtIndex(0, std::string());
456 EXPECT_FALSE(GetCache()->ProfileIsSupervisedAtIndex(0));
457 EXPECT_EQ("", GetCache()->GetSupervisedUserIdOfProfileAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54458}
459
[email protected]2f3c00f2011-11-30 04:36:22460TEST_F(ProfileInfoCacheTest, EmptyGAIAInfo) {
[email protected]a04db822013-12-11 19:14:40461 base::string16 profile_name = ASCIIToUTF16("name_1");
[email protected]c3e559772014-04-09 04:02:54462 int id = profiles::GetDefaultAvatarIconResourceIDAtIndex(0);
[email protected]2f3c00f2011-11-30 04:36:22463 const gfx::Image& profile_image(
464 ResourceBundle::GetSharedInstance().GetImageNamed(id));
465
466 GetCache()->AddProfileToCache(
[email protected]a04db822013-12-11 19:14:40467 GetProfilePath("path_1"), profile_name, base::string16(), 0,
468 std::string());
[email protected]2f3c00f2011-11-30 04:36:22469
470 // Set empty GAIA info.
[email protected]a04db822013-12-11 19:14:40471 GetCache()->SetGAIANameOfProfileAtIndex(0, base::string16());
[email protected]2f3c00f2011-11-30 04:36:22472 GetCache()->SetGAIAPictureOfProfileAtIndex(0, NULL);
[email protected]2f3c00f2011-11-30 04:36:22473 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(0, true);
474
475 // Verify that the profile name and picture are not empty.
476 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(0));
477 EXPECT_TRUE(gfx::test::IsEqual(
478 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(0)));
[email protected]cb114f142011-11-23 20:18:04479}
480
[email protected]d20d0432014-06-12 17:14:05481TEST_F(ProfileInfoCacheTest, CreateSupervisedTestingProfile) {
[email protected]0aa018a2013-07-31 15:08:54482 testing_profile_manager_.CreateTestingProfile("default");
[email protected]d20d0432014-06-12 17:14:05483 base::string16 supervised_user_name = ASCIIToUTF16("Supervised User");
[email protected]0aa018a2013-07-31 15:08:54484 testing_profile_manager_.CreateTestingProfile(
[email protected]a6e01b42013-08-05 14:13:13485 "test1", scoped_ptr<PrefServiceSyncable>(),
[email protected]d20d0432014-06-12 17:14:05486 supervised_user_name, 0, "TEST_ID", TestingProfile::TestingFactories());
[email protected]0aa018a2013-07-31 15:08:54487 for (size_t i = 0; i < GetCache()->GetNumberOfProfiles(); i++) {
[email protected]d20d0432014-06-12 17:14:05488 bool is_supervised =
489 GetCache()->GetNameOfProfileAtIndex(i) == supervised_user_name;
490 EXPECT_EQ(is_supervised, GetCache()->ProfileIsSupervisedAtIndex(i));
491 std::string supervised_user_id = is_supervised ? "TEST_ID" : "";
492 EXPECT_EQ(supervised_user_id,
493 GetCache()->GetSupervisedUserIdOfProfileAtIndex(i));
[email protected]0aa018a2013-07-31 15:08:54494 }
[email protected]371662e372013-10-17 22:05:22495
[email protected]d20d0432014-06-12 17:14:05496 // Supervised profiles have a custom theme, which needs to be deleted on the
497 // FILE thread. Reset the profile manager now so everything is deleted while
498 // we still have a FILE thread.
[email protected]371662e372013-10-17 22:05:22499 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
[email protected]0aa018a2013-07-31 15:08:54500}
501
[email protected]047481802013-09-16 22:26:38502TEST_F(ProfileInfoCacheTest, AddStubProfile) {
503 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
504
505 // Add some profiles with and without a '.' in their paths.
506 const struct {
507 const char* profile_path;
508 const char* profile_name;
509 } kTestCases[] = {
510 { "path.test0", "name_0" },
511 { "path_test1", "name_1" },
512 { "path.test2", "name_2" },
513 { "path_test3", "name_3" },
514 };
515
516 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) {
517 base::FilePath profile_path = GetProfilePath(kTestCases[i].profile_path);
[email protected]a04db822013-12-11 19:14:40518 base::string16 profile_name = ASCIIToUTF16(kTestCases[i].profile_name);
[email protected]047481802013-09-16 22:26:38519
[email protected]a04db822013-12-11 19:14:40520 GetCache()->AddProfileToCache(profile_path, profile_name, base::string16(),
521 i, "");
[email protected]047481802013-09-16 22:26:38522
523 EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i));
524 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
525 }
526
527 ASSERT_EQ(4U, GetCache()->GetNumberOfProfiles());
528
529 // Check that the profiles can be extracted from the local state.
[email protected]d2065e062013-12-12 23:49:52530 std::vector<base::string16> names = ProfileInfoCache::GetProfileNames();
[email protected]047481802013-09-16 22:26:38531 for (size_t i = 0; i < 4; i++)
532 ASSERT_FALSE(names[i].empty());
533}
534
[email protected]b09bfa12014-08-15 19:05:25535// High res avatar downloading is only supported on desktop.
536#if !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_CHROMEOS)
[email protected]9de44302014-05-01 16:32:27537TEST_F(ProfileInfoCacheTest, DownloadHighResAvatarTest) {
[email protected]9152a542014-08-06 05:37:45538 switches::EnableNewAvatarMenuForTesting(CommandLine::ForCurrentProcess());
[email protected]5cf8bcd92014-07-17 20:27:40539
[email protected]9de44302014-05-01 16:32:27540 EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles());
541 base::FilePath path_1 = GetProfilePath("path_1");
542 GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("name_1"),
543 base::string16(), 0, std::string());
544 EXPECT_EQ(1U, GetCache()->GetNumberOfProfiles());
545
546 // We haven't downloaded any high-res avatars yet.
547 EXPECT_EQ(0U, GetCache()->cached_avatar_images_.size());
[email protected]5cf8bcd92014-07-17 20:27:40548
549 // After adding a new profile, the download of high-res avatar will be
550 // triggered if the flag kNewAvatarMenu has been set. But the downloader
551 // won't ever call OnFetchComplete in the test.
552 EXPECT_EQ(1U, GetCache()->avatar_images_downloads_in_progress_.size());
553
[email protected]9de44302014-05-01 16:32:27554 EXPECT_FALSE(GetCache()->GetHighResAvatarOfProfileAtIndex(0));
555
556 // Simulate downloading a high-res avatar.
557 const size_t kIconIndex = 0;
[email protected]2d605712014-05-14 12:03:07558 ProfileAvatarDownloader avatar_downloader(
559 kIconIndex, GetCache()->GetPathOfProfileAtIndex(0), GetCache());
[email protected]9de44302014-05-01 16:32:27560
561 // Put a real bitmap into "bitmap". 2x2 bitmap of green 32 bit pixels.
562 SkBitmap bitmap;
[email protected]e61cad52014-07-01 21:01:57563 bitmap.allocN32Pixels(2, 2);
[email protected]9de44302014-05-01 16:32:27564 bitmap.eraseColor(SK_ColorGREEN);
565
566 avatar_downloader.OnFetchComplete(
567 GURL("http://www.google.com/avatar.png"), &bitmap);
568
569 std::string file_name =
570 profiles::GetDefaultAvatarIconFileNameAtIndex(kIconIndex);
571
572 // The file should have been cached and saved.
[email protected]5cf8bcd92014-07-17 20:27:40573 EXPECT_EQ(1U, GetCache()->avatar_images_downloads_in_progress_.size());
[email protected]9de44302014-05-01 16:32:27574 EXPECT_EQ(1U, GetCache()->cached_avatar_images_.size());
575 EXPECT_TRUE(GetCache()->GetHighResAvatarOfProfileAtIndex(0));
576 EXPECT_EQ(GetCache()->cached_avatar_images_[file_name],
577 GetCache()->GetHighResAvatarOfProfileAtIndex(0));
578
579 // Make sure everything has completed, and the file has been written to disk.
580 base::RunLoop().RunUntilIdle();
581
582 // Clean up.
583 base::FilePath icon_path =
584 profiles::GetPathOfHighResAvatarAtIndex(kIconIndex);
585 EXPECT_NE(std::string::npos, icon_path.MaybeAsASCII().find(file_name));
586 EXPECT_TRUE(base::PathExists(icon_path));
587 EXPECT_TRUE(base::DeleteFile(icon_path, true));
588 EXPECT_FALSE(base::PathExists(icon_path));
589}
[email protected]b09bfa12014-08-15 19:05:25590#endif