blob: 51e1fd456e30b63b3f939c6f9fb155abd94be296 [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]e5ba874f2013-02-14 17:20:197#include "base/prefs/testing_pref_service.h"
[email protected]76fb05c2013-06-11 04:38:058#include "base/strings/stringprintf.h"
[email protected]e309f312013-06-07 21:50:089#include "base/strings/utf_string_conversions.h"
[email protected]583844c2011-08-27 00:38:3510#include "chrome/browser/browser_process.h"
[email protected]25ff0862013-07-12 00:59:0311#include "chrome/browser/chrome_notification_types.h"
[email protected]0aa018a2013-07-31 15:08:5412#include "chrome/browser/prefs/pref_service_syncable.h"
[email protected]d4f5d1162011-11-30 01:41:5213#include "chrome/browser/profiles/profile_info_cache.h"
[email protected]6730b1e2011-09-29 05:23:5214#include "chrome/browser/profiles/profile_manager.h"
[email protected]583844c2011-08-27 00:38:3515#include "chrome/test/base/testing_browser_process.h"
[email protected]cb114f142011-11-23 20:18:0416#include "content/public/browser/notification_observer.h"
17#include "content/public/browser/notification_registrar.h"
[email protected]c0b030902011-12-19 16:21:0518#include "content/public/browser/notification_service.h"
[email protected]a7fe9112012-07-20 02:34:4519#include "content/public/test/test_utils.h"
[email protected]dee810e2011-06-27 19:43:3920#include "third_party/skia/include/core/SkBitmap.h"
21#include "ui/base/resource/resource_bundle.h"
22#include "ui/gfx/image/image.h"
[email protected]d4f5d1162011-11-30 01:41:5223#include "ui/gfx/image/image_unittest_util.h"
[email protected]dee810e2011-06-27 19:43:3924
[email protected]cb114f142011-11-23 20:18:0425using content::BrowserThread;
26
[email protected]6a460662011-12-22 22:05:1627ProfileNameVerifierObserver::ProfileNameVerifierObserver(
28 TestingProfileManager* testing_profile_manager)
29 : testing_profile_manager_(testing_profile_manager) {
30 DCHECK(testing_profile_manager_);
[email protected]590e189b2011-12-13 22:07:0331}
32
33ProfileNameVerifierObserver::~ProfileNameVerifierObserver() {
34}
35
36void ProfileNameVerifierObserver::OnProfileAdded(
[email protected]650b2d52013-02-10 03:41:4537 const base::FilePath& profile_path) {
[email protected]6a460662011-12-22 22:05:1638 string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
39 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0340 EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end());
41 profile_names_.insert(profile_name);
42}
43
[email protected]7b0af152011-12-16 17:02:0644void ProfileNameVerifierObserver::OnProfileWillBeRemoved(
[email protected]650b2d52013-02-10 03:41:4545 const base::FilePath& profile_path) {
[email protected]6a460662011-12-22 22:05:1646 string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
47 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0348 EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end());
49 profile_names_.erase(profile_name);
50}
51
[email protected]7b0af152011-12-16 17:02:0652void ProfileNameVerifierObserver::OnProfileWasRemoved(
[email protected]650b2d52013-02-10 03:41:4553 const base::FilePath& profile_path,
[email protected]7b0af152011-12-16 17:02:0654 const string16& profile_name) {
55 EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end());
56}
57
[email protected]590e189b2011-12-13 22:07:0358void ProfileNameVerifierObserver::OnProfileNameChanged(
[email protected]650b2d52013-02-10 03:41:4559 const base::FilePath& profile_path,
[email protected]6a460662011-12-22 22:05:1660 const string16& old_profile_name) {
61 string16 new_profile_name = GetCache()->GetNameOfProfileAtIndex(
62 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0363 EXPECT_TRUE(profile_names_.find(old_profile_name) != profile_names_.end());
64 EXPECT_TRUE(profile_names_.find(new_profile_name) == profile_names_.end());
65 profile_names_.erase(old_profile_name);
66 profile_names_.insert(new_profile_name);
67}
68
69void ProfileNameVerifierObserver::OnProfileAvatarChanged(
[email protected]650b2d52013-02-10 03:41:4570 const base::FilePath& profile_path) {
[email protected]6a460662011-12-22 22:05:1671 string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
72 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0373 EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end());
74}
75
[email protected]6a460662011-12-22 22:05:1676ProfileInfoCache* ProfileNameVerifierObserver::GetCache() {
77 return testing_profile_manager_->profile_info_cache();
78}
79
[email protected]d4f5d1162011-11-30 01:41:5280ProfileInfoCacheTest::ProfileInfoCacheTest()
[email protected]0aa018a2013-07-31 15:08:5481 : testing_profile_manager_(TestingBrowserProcess::GetGlobal()),
[email protected]6a460662011-12-22 22:05:1682 name_observer_(&testing_profile_manager_) {
[email protected]d4f5d1162011-11-30 01:41:5283}
84
85ProfileInfoCacheTest::~ProfileInfoCacheTest() {
86}
87
88void ProfileInfoCacheTest::SetUp() {
89 ASSERT_TRUE(testing_profile_manager_.SetUp());
[email protected]590e189b2011-12-13 22:07:0390 testing_profile_manager_.profile_info_cache()->AddObserver(&name_observer_);
[email protected]d4f5d1162011-11-30 01:41:5291}
92
[email protected]1302dcf2011-11-30 21:47:0593void ProfileInfoCacheTest::TearDown() {
94 // Drain the UI thread to make sure all tasks are completed. This prevents
95 // memory leaks.
[email protected]0aa018a2013-07-31 15:08:5496 base::RunLoop().RunUntilIdle();
[email protected]1302dcf2011-11-30 21:47:0597}
98
[email protected]d4f5d1162011-11-30 01:41:5299ProfileInfoCache* ProfileInfoCacheTest::GetCache() {
100 return testing_profile_manager_.profile_info_cache();
101}
102
[email protected]650b2d52013-02-10 03:41:45103base::FilePath ProfileInfoCacheTest::GetProfilePath(
[email protected]d4f5d1162011-11-30 01:41:52104 const std::string& base_name) {
105 return testing_profile_manager_.profile_manager()->user_data_dir().
106 AppendASCII(base_name);
107}
108
109void ProfileInfoCacheTest::ResetCache() {
110 testing_profile_manager_.DeleteProfileInfoCache();
111}
112
[email protected]dee810e2011-06-27 19:43:39113namespace {
114
[email protected]d4f5d1162011-11-30 01:41:52115TEST_F(ProfileInfoCacheTest, AddProfiles) {
[email protected]6730b1e2011-09-29 05:23:52116 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39117
[email protected]ca591072012-03-27 01:54:44118 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
[email protected]dee810e2011-06-27 19:43:39119 for (uint32 i = 0; i < 4; ++i) {
[email protected]7d3cbc92013-03-18 22:33:04120 base::FilePath profile_path =
121 GetProfilePath(base::StringPrintf("path_%ud", i));
122 string16 profile_name = ASCIIToUTF16(base::StringPrintf("name_%ud", i));
[email protected]ca591072012-03-27 01:54:44123 const SkBitmap* icon = rb.GetImageNamed(
124 ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(
125 i)).ToSkBitmap();
[email protected]a6e01b42013-08-05 14:13:13126 std::string managed_user_id = i == 3 ? "TEST_ID" : "";
[email protected]dee810e2011-06-27 19:43:39127
[email protected]fea25d82012-12-19 01:19:50128 GetCache()->AddProfileToCache(profile_path, profile_name, string16(), i,
[email protected]a6e01b42013-08-05 14:13:13129 managed_user_id);
[email protected]cb114f142011-11-23 20:18:04130 GetCache()->SetBackgroundStatusOfProfileAtIndex(i, true);
[email protected]7d3cbc92013-03-18 22:33:04131 string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
[email protected]cb114f142011-11-23 20:18:04132 GetCache()->SetGAIANameOfProfileAtIndex(i, gaia_name);
[email protected]dee810e2011-06-27 19:43:39133
[email protected]6730b1e2011-09-29 05:23:52134 EXPECT_EQ(i + 1, GetCache()->GetNumberOfProfiles());
135 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
136 EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i));
[email protected]0aa018a2013-07-31 15:08:54137 const SkBitmap* actual_icon =
138 GetCache()->GetAvatarIconOfProfileAtIndex(i).ToSkBitmap();
[email protected]ca591072012-03-27 01:54:44139 EXPECT_EQ(icon->width(), actual_icon->width());
140 EXPECT_EQ(icon->height(), actual_icon->height());
[email protected]a6e01b42013-08-05 14:13:13141 EXPECT_EQ(i == 3, GetCache()->ProfileIsManagedAtIndex(i));
142 EXPECT_EQ(managed_user_id, GetCache()->GetManagedUserIdOfProfileAtIndex(i));
[email protected]dee810e2011-06-27 19:43:39143 }
[email protected]cb114f142011-11-23 20:18:04144
145 // Reset the cache and test the it reloads correctly.
146 ResetCache();
147
148 EXPECT_EQ(4u, GetCache()->GetNumberOfProfiles());
149 for (uint32 i = 0; i < 4; ++i) {
[email protected]7d3cbc92013-03-18 22:33:04150 base::FilePath profile_path =
151 GetProfilePath(base::StringPrintf("path_%ud", i));
[email protected]cb114f142011-11-23 20:18:04152 EXPECT_EQ(i, GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]7d3cbc92013-03-18 22:33:04153 string16 profile_name = ASCIIToUTF16(base::StringPrintf("name_%ud", i));
[email protected]cb114f142011-11-23 20:18:04154 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
155 EXPECT_EQ(i, GetCache()->GetAvatarIconIndexOfProfileAtIndex(i));
156 EXPECT_EQ(true, GetCache()->GetBackgroundStatusOfProfileAtIndex(i));
[email protected]7d3cbc92013-03-18 22:33:04157 string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
[email protected]cb114f142011-11-23 20:18:04158 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(i));
159 }
[email protected]dee810e2011-06-27 19:43:39160}
161
[email protected]d4f5d1162011-11-30 01:41:52162TEST_F(ProfileInfoCacheTest, DeleteProfile) {
[email protected]6730b1e2011-09-29 05:23:52163 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39164
[email protected]650b2d52013-02-10 03:41:45165 base::FilePath path_1 = GetProfilePath("path_1");
[email protected]73cb3722011-10-11 18:12:13166 GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("name_1"), string16(),
[email protected]a6e01b42013-08-05 14:13:13167 0, std::string());
[email protected]6730b1e2011-09-29 05:23:52168 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39169
[email protected]650b2d52013-02-10 03:41:45170 base::FilePath path_2 = GetProfilePath("path_2");
[email protected]dee810e2011-06-27 19:43:39171 string16 name_2 = ASCIIToUTF16("name_2");
[email protected]a6e01b42013-08-05 14:13:13172 GetCache()->AddProfileToCache(path_2, name_2, string16(), 0, std::string());
[email protected]6730b1e2011-09-29 05:23:52173 EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39174
[email protected]6730b1e2011-09-29 05:23:52175 GetCache()->DeleteProfileFromCache(path_1);
176 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
177 EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(0));
[email protected]dee810e2011-06-27 19:43:39178
[email protected]6730b1e2011-09-29 05:23:52179 GetCache()->DeleteProfileFromCache(path_2);
180 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39181}
182
[email protected]d4f5d1162011-11-30 01:41:52183TEST_F(ProfileInfoCacheTest, MutateProfile) {
184 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13185 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
186 string16(), 0, std::string());
[email protected]d4f5d1162011-11-30 01:41:52187 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13188 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
189 string16(), 0, std::string());
[email protected]dee810e2011-06-27 19:43:39190
191 string16 new_name = ASCIIToUTF16("new_name");
[email protected]6730b1e2011-09-29 05:23:52192 GetCache()->SetNameOfProfileAtIndex(1, new_name);
193 EXPECT_EQ(new_name, GetCache()->GetNameOfProfileAtIndex(1));
194 EXPECT_NE(new_name, GetCache()->GetNameOfProfileAtIndex(0));
[email protected]dee810e2011-06-27 19:43:39195
[email protected]e8e78092011-09-29 18:15:38196 string16 new_user_name = ASCIIToUTF16("user_name");
197 GetCache()->SetUserNameOfProfileAtIndex(1, new_user_name);
198 EXPECT_EQ(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(1));
199 EXPECT_NE(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(0));
200
[email protected]dee810e2011-06-27 19:43:39201 size_t new_icon_index = 3;
[email protected]6730b1e2011-09-29 05:23:52202 GetCache()->SetAvatarIconOfProfileAtIndex(1, new_icon_index);
[email protected]dee810e2011-06-27 19:43:39203 // Not much to test.
[email protected]6730b1e2011-09-29 05:23:52204 GetCache()->GetAvatarIconOfProfileAtIndex(1);
[email protected]dee810e2011-06-27 19:43:39205}
206
[email protected]d4f5d1162011-11-30 01:41:52207TEST_F(ProfileInfoCacheTest, Sort) {
[email protected]cb114f142011-11-23 20:18:04208 string16 name_a = ASCIIToUTF16("apple");
[email protected]d4f5d1162011-11-30 01:41:52209 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13210 GetProfilePath("path_a"), name_a, string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04211
212 string16 name_c = ASCIIToUTF16("cat");
[email protected]d4f5d1162011-11-30 01:41:52213 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13214 GetProfilePath("path_c"), name_c, string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04215
216 // Sanity check the initial order.
217 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
218 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
219
220 // Add a new profile (start with a capital to test case insensitive sorting.
221 string16 name_b = ASCIIToUTF16("Banana");
[email protected]d4f5d1162011-11-30 01:41:52222 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13223 GetProfilePath("path_b"), name_b, string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04224
225 // Verify the new order.
226 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
227 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(1));
228 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(2));
229
230 // Change the name of an existing profile.
231 name_a = UTF8ToUTF16("dog");
232 GetCache()->SetNameOfProfileAtIndex(0, name_a);
233
234 // Verify the new order.
235 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
236 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
237 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(2));
238
239 // Delete a profile.
[email protected]d4f5d1162011-11-30 01:41:52240 GetCache()->DeleteProfileFromCache(GetProfilePath("path_c"));
[email protected]cb114f142011-11-23 20:18:04241
242 // Verify the new order.
243 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
244 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(1));
245}
246
[email protected]d4f5d1162011-11-30 01:41:52247TEST_F(ProfileInfoCacheTest, BackgroundModeStatus) {
[email protected]279170832011-10-12 23:38:03248 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13249 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
250 string16(), 0, std::string());
[email protected]279170832011-10-12 23:38:03251 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13252 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
253 string16(), 0, std::string());
[email protected]279170832011-10-12 23:38:03254
255 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
256 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
257
258 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, true);
259
260 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
261 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
262
263 GetCache()->SetBackgroundStatusOfProfileAtIndex(0, true);
264
265 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
266 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
267
268 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, false);
269
270 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
271 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
272}
273
[email protected]d4f5d1162011-11-30 01:41:52274TEST_F(ProfileInfoCacheTest, HasMigrated) {
[email protected]cb114f142011-11-23 20:18:04275 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13276 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
277 string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04278 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13279 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
280 string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04281
282 // Sanity check.
283 EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(0));
284 EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(1));
285
286 // Set migrated state for 2nd profile.
287 GetCache()->SetHasMigratedToGAIAInfoOfProfileAtIndex(1, true);
288 EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(0));
289 EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(1));
290
291 // Set migrated state for 1st profile.
292 GetCache()->SetHasMigratedToGAIAInfoOfProfileAtIndex(0, true);
293 EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(0));
294 EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(1));
295
296 // Unset migrated state for 2nd profile.
297 GetCache()->SetHasMigratedToGAIAInfoOfProfileAtIndex(1, false);
298 EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(0));
299 EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(1));
300}
301
[email protected]d4f5d1162011-11-30 01:41:52302TEST_F(ProfileInfoCacheTest, GAIAName) {
[email protected]cb114f142011-11-23 20:18:04303 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13304 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
305 string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04306 string16 profile_name(ASCIIToUTF16("profile name 2"));
307 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13308 GetProfilePath("path_2"), profile_name, string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04309
310 // Sanity check.
311 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(0).empty());
312 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(1).empty());
313 EXPECT_FALSE(GetCache()->IsUsingGAIANameOfProfileAtIndex(0));
314 EXPECT_FALSE(GetCache()->IsUsingGAIANameOfProfileAtIndex(1));
315
316 // Set GAIA name.
317 string16 gaia_name(ASCIIToUTF16("Pat Smith"));
318 GetCache()->SetGAIANameOfProfileAtIndex(1, gaia_name);
319 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(0).empty());
320 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(1));
321 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(1));
322
323 // Use GAIA name as profile name.
324 GetCache()->SetIsUsingGAIANameOfProfileAtIndex(1, true);
325
326 EXPECT_EQ(gaia_name, GetCache()->GetNameOfProfileAtIndex(1));
327 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(1));
328
329 // Don't use GAIA name as profile name.
330 GetCache()->SetIsUsingGAIANameOfProfileAtIndex(1, false);
331 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(1));
332 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(1));
333}
334
[email protected]d4f5d1162011-11-30 01:41:52335TEST_F(ProfileInfoCacheTest, GAIAPicture) {
[email protected]cb114f142011-11-23 20:18:04336 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13337 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
338 string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04339 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13340 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
341 string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04342
343 // Sanity check.
[email protected]2f3c00f2011-11-30 04:36:22344 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
345 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(1));
[email protected]cb114f142011-11-23 20:18:04346 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0));
347 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
348
349 // The profile icon should be the default one.
350 int id = ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(0);
351 const gfx::Image& profile_image(
352 ResourceBundle::GetSharedInstance().GetImageNamed(id));
[email protected]d4f5d1162011-11-30 01:41:52353 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]cb114f142011-11-23 20:18:04354 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
355
356 // Set GAIA picture.
[email protected]d4f5d1162011-11-30 01:41:52357 gfx::Image gaia_image(gfx::test::CreateImage());
[email protected]2f3c00f2011-11-30 04:36:22358 GetCache()->SetGAIAPictureOfProfileAtIndex(1, &gaia_image);
359 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
[email protected]d4f5d1162011-11-30 01:41:52360 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22361 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
[email protected]d4f5d1162011-11-30 01:41:52362 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]cb114f142011-11-23 20:18:04363 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
364
365 // Use GAIA picture as profile picture.
366 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, true);
[email protected]d4f5d1162011-11-30 01:41:52367 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22368 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
[email protected]d4f5d1162011-11-30 01:41:52369 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]cb114f142011-11-23 20:18:04370 gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
371
372 // Don't use GAIA picture as profile picture.
373 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, false);
[email protected]d4f5d1162011-11-30 01:41:52374 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22375 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
[email protected]d4f5d1162011-11-30 01:41:52376 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]cb114f142011-11-23 20:18:04377 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
378}
379
[email protected]754bebc2011-12-01 16:42:16380TEST_F(ProfileInfoCacheTest, PersistGAIAPicture) {
[email protected]cb114f142011-11-23 20:18:04381 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13382 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
383 string16(), 0, std::string());
[email protected]d4f5d1162011-11-30 01:41:52384 gfx::Image gaia_image(gfx::test::CreateImage());
[email protected]cb114f142011-11-23 20:18:04385
[email protected]a7fe9112012-07-20 02:34:45386 content::WindowedNotificationObserver save_observer(
[email protected]cb114f142011-11-23 20:18:04387 chrome::NOTIFICATION_PROFILE_CACHE_PICTURE_SAVED,
388 content::NotificationService::AllSources());
[email protected]2f3c00f2011-11-30 04:36:22389 GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_image);
[email protected]d4f5d1162011-11-30 01:41:52390 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22391 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
[email protected]cb114f142011-11-23 20:18:04392
393 // Wait for the file to be written to disk then reset the cache.
394 save_observer.Wait();
395 ResetCache();
396
397 // Try to get the GAIA picture. This should return NULL until the read from
398 // disk is done.
[email protected]a7fe9112012-07-20 02:34:45399 content::WindowedNotificationObserver read_observer(
[email protected]cb114f142011-11-23 20:18:04400 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED,
401 content::NotificationService::AllSources());
[email protected]2f3c00f2011-11-30 04:36:22402 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
[email protected]cb114f142011-11-23 20:18:04403 read_observer.Wait();
[email protected]d4f5d1162011-11-30 01:41:52404 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22405 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
406}
407
[email protected]a6e01b42013-08-05 14:13:13408TEST_F(ProfileInfoCacheTest, SetManagedUserId) {
[email protected]0aa018a2013-07-31 15:08:54409 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13410 GetProfilePath("test"), ASCIIToUTF16("Test"),
411 string16(), 0, std::string());
[email protected]0aa018a2013-07-31 15:08:54412 EXPECT_FALSE(GetCache()->ProfileIsManagedAtIndex(0));
413
[email protected]a6e01b42013-08-05 14:13:13414 GetCache()->SetManagedUserIdOfProfileAtIndex(0, "TEST_ID");
[email protected]0aa018a2013-07-31 15:08:54415 EXPECT_TRUE(GetCache()->ProfileIsManagedAtIndex(0));
[email protected]a6e01b42013-08-05 14:13:13416 EXPECT_EQ("TEST_ID", GetCache()->GetManagedUserIdOfProfileAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54417
418 ResetCache();
419 EXPECT_TRUE(GetCache()->ProfileIsManagedAtIndex(0));
420
[email protected]a6e01b42013-08-05 14:13:13421 GetCache()->SetManagedUserIdOfProfileAtIndex(0, std::string());
[email protected]0aa018a2013-07-31 15:08:54422 EXPECT_FALSE(GetCache()->ProfileIsManagedAtIndex(0));
[email protected]a6e01b42013-08-05 14:13:13423 EXPECT_EQ("", GetCache()->GetManagedUserIdOfProfileAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54424}
425
[email protected]2f3c00f2011-11-30 04:36:22426TEST_F(ProfileInfoCacheTest, EmptyGAIAInfo) {
427 string16 profile_name = ASCIIToUTF16("name_1");
428 int id = ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(0);
429 const gfx::Image& profile_image(
430 ResourceBundle::GetSharedInstance().GetImageNamed(id));
431
432 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13433 GetProfilePath("path_1"), profile_name, string16(), 0, std::string());
[email protected]2f3c00f2011-11-30 04:36:22434
435 // Set empty GAIA info.
436 GetCache()->SetGAIANameOfProfileAtIndex(0, string16());
437 GetCache()->SetGAIAPictureOfProfileAtIndex(0, NULL);
438 GetCache()->SetIsUsingGAIANameOfProfileAtIndex(0, true);
439 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(0, true);
440
441 // Verify that the profile name and picture are not empty.
442 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(0));
443 EXPECT_TRUE(gfx::test::IsEqual(
444 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(0)));
[email protected]cb114f142011-11-23 20:18:04445}
446
[email protected]0aa018a2013-07-31 15:08:54447TEST_F(ProfileInfoCacheTest, CreateManagedTestingProfile) {
448 testing_profile_manager_.CreateTestingProfile("default");
449 string16 managed_user_name = ASCIIToUTF16("Supervised User");
450 testing_profile_manager_.CreateTestingProfile(
[email protected]a6e01b42013-08-05 14:13:13451 "test1", scoped_ptr<PrefServiceSyncable>(),
452 managed_user_name, 0, "TEST_ID");
[email protected]0aa018a2013-07-31 15:08:54453 for (size_t i = 0; i < GetCache()->GetNumberOfProfiles(); i++) {
454 bool is_managed =
455 GetCache()->GetNameOfProfileAtIndex(i) == managed_user_name;
456 EXPECT_EQ(is_managed, GetCache()->ProfileIsManagedAtIndex(i));
[email protected]a6e01b42013-08-05 14:13:13457 std::string managed_user_id = is_managed ? "TEST_ID" : "";
458 EXPECT_EQ(managed_user_id, GetCache()->GetManagedUserIdOfProfileAtIndex(i));
[email protected]0aa018a2013-07-31 15:08:54459 }
460}
461
[email protected]6730b1e2011-09-29 05:23:52462} // namespace