blob: ee0c592480fb94124d38e28ad1cb58a055531424 [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]0aa018a2013-07-31 15:08:54126 bool is_managed = i == 3;
[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]0aa018a2013-07-31 15:08:54129 is_managed);
[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]0aa018a2013-07-31 15:08:54141 EXPECT_EQ(is_managed, GetCache()->ProfileIsManagedAtIndex(i));
[email protected]dee810e2011-06-27 19:43:39142 }
[email protected]cb114f142011-11-23 20:18:04143
144 // Reset the cache and test the it reloads correctly.
145 ResetCache();
146
147 EXPECT_EQ(4u, GetCache()->GetNumberOfProfiles());
148 for (uint32 i = 0; i < 4; ++i) {
[email protected]7d3cbc92013-03-18 22:33:04149 base::FilePath profile_path =
150 GetProfilePath(base::StringPrintf("path_%ud", i));
[email protected]cb114f142011-11-23 20:18:04151 EXPECT_EQ(i, GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]7d3cbc92013-03-18 22:33:04152 string16 profile_name = ASCIIToUTF16(base::StringPrintf("name_%ud", i));
[email protected]cb114f142011-11-23 20:18:04153 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
154 EXPECT_EQ(i, GetCache()->GetAvatarIconIndexOfProfileAtIndex(i));
155 EXPECT_EQ(true, GetCache()->GetBackgroundStatusOfProfileAtIndex(i));
[email protected]7d3cbc92013-03-18 22:33:04156 string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
[email protected]cb114f142011-11-23 20:18:04157 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(i));
158 }
[email protected]dee810e2011-06-27 19:43:39159}
160
[email protected]d4f5d1162011-11-30 01:41:52161TEST_F(ProfileInfoCacheTest, DeleteProfile) {
[email protected]6730b1e2011-09-29 05:23:52162 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39163
[email protected]650b2d52013-02-10 03:41:45164 base::FilePath path_1 = GetProfilePath("path_1");
[email protected]73cb3722011-10-11 18:12:13165 GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("name_1"), string16(),
[email protected]fea25d82012-12-19 01:19:50166 0, false);
[email protected]6730b1e2011-09-29 05:23:52167 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39168
[email protected]650b2d52013-02-10 03:41:45169 base::FilePath path_2 = GetProfilePath("path_2");
[email protected]dee810e2011-06-27 19:43:39170 string16 name_2 = ASCIIToUTF16("name_2");
[email protected]fea25d82012-12-19 01:19:50171 GetCache()->AddProfileToCache(path_2, name_2, string16(), 0, false);
[email protected]6730b1e2011-09-29 05:23:52172 EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39173
[email protected]6730b1e2011-09-29 05:23:52174 GetCache()->DeleteProfileFromCache(path_1);
175 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
176 EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(0));
[email protected]dee810e2011-06-27 19:43:39177
[email protected]6730b1e2011-09-29 05:23:52178 GetCache()->DeleteProfileFromCache(path_2);
179 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39180}
181
[email protected]d4f5d1162011-11-30 01:41:52182TEST_F(ProfileInfoCacheTest, MutateProfile) {
183 GetCache()->AddProfileToCache(
[email protected]fea25d82012-12-19 01:19:50184 GetProfilePath("path_1"), ASCIIToUTF16("name_1"), string16(), 0, false);
[email protected]d4f5d1162011-11-30 01:41:52185 GetCache()->AddProfileToCache(
[email protected]fea25d82012-12-19 01:19:50186 GetProfilePath("path_2"), ASCIIToUTF16("name_2"), string16(), 0, false);
[email protected]dee810e2011-06-27 19:43:39187
188 string16 new_name = ASCIIToUTF16("new_name");
[email protected]6730b1e2011-09-29 05:23:52189 GetCache()->SetNameOfProfileAtIndex(1, new_name);
190 EXPECT_EQ(new_name, GetCache()->GetNameOfProfileAtIndex(1));
191 EXPECT_NE(new_name, GetCache()->GetNameOfProfileAtIndex(0));
[email protected]dee810e2011-06-27 19:43:39192
[email protected]e8e78092011-09-29 18:15:38193 string16 new_user_name = ASCIIToUTF16("user_name");
194 GetCache()->SetUserNameOfProfileAtIndex(1, new_user_name);
195 EXPECT_EQ(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(1));
196 EXPECT_NE(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(0));
197
[email protected]dee810e2011-06-27 19:43:39198 size_t new_icon_index = 3;
[email protected]6730b1e2011-09-29 05:23:52199 GetCache()->SetAvatarIconOfProfileAtIndex(1, new_icon_index);
[email protected]dee810e2011-06-27 19:43:39200 // Not much to test.
[email protected]6730b1e2011-09-29 05:23:52201 GetCache()->GetAvatarIconOfProfileAtIndex(1);
[email protected]dee810e2011-06-27 19:43:39202}
203
[email protected]d4f5d1162011-11-30 01:41:52204TEST_F(ProfileInfoCacheTest, Sort) {
[email protected]cb114f142011-11-23 20:18:04205 string16 name_a = ASCIIToUTF16("apple");
[email protected]d4f5d1162011-11-30 01:41:52206 GetCache()->AddProfileToCache(
[email protected]fea25d82012-12-19 01:19:50207 GetProfilePath("path_a"), name_a, string16(), 0, false);
[email protected]cb114f142011-11-23 20:18:04208
209 string16 name_c = ASCIIToUTF16("cat");
[email protected]d4f5d1162011-11-30 01:41:52210 GetCache()->AddProfileToCache(
[email protected]fea25d82012-12-19 01:19:50211 GetProfilePath("path_c"), name_c, string16(), 0, false);
[email protected]cb114f142011-11-23 20:18:04212
213 // Sanity check the initial order.
214 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
215 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
216
217 // Add a new profile (start with a capital to test case insensitive sorting.
218 string16 name_b = ASCIIToUTF16("Banana");
[email protected]d4f5d1162011-11-30 01:41:52219 GetCache()->AddProfileToCache(
[email protected]fea25d82012-12-19 01:19:50220 GetProfilePath("path_b"), name_b, string16(), 0, false);
[email protected]cb114f142011-11-23 20:18:04221
222 // Verify the new order.
223 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
224 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(1));
225 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(2));
226
227 // Change the name of an existing profile.
228 name_a = UTF8ToUTF16("dog");
229 GetCache()->SetNameOfProfileAtIndex(0, name_a);
230
231 // Verify the new order.
232 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
233 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
234 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(2));
235
236 // Delete a profile.
[email protected]d4f5d1162011-11-30 01:41:52237 GetCache()->DeleteProfileFromCache(GetProfilePath("path_c"));
[email protected]cb114f142011-11-23 20:18:04238
239 // Verify the new order.
240 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
241 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(1));
242}
243
[email protected]d4f5d1162011-11-30 01:41:52244TEST_F(ProfileInfoCacheTest, BackgroundModeStatus) {
[email protected]279170832011-10-12 23:38:03245 GetCache()->AddProfileToCache(
[email protected]fea25d82012-12-19 01:19:50246 GetProfilePath("path_1"), ASCIIToUTF16("name_1"), string16(), 0, false);
[email protected]279170832011-10-12 23:38:03247 GetCache()->AddProfileToCache(
[email protected]fea25d82012-12-19 01:19:50248 GetProfilePath("path_2"), ASCIIToUTF16("name_2"), string16(), 0, false);
[email protected]279170832011-10-12 23:38:03249
250 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
251 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
252
253 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, true);
254
255 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
256 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
257
258 GetCache()->SetBackgroundStatusOfProfileAtIndex(0, true);
259
260 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
261 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
262
263 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, false);
264
265 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
266 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
267}
268
[email protected]d4f5d1162011-11-30 01:41:52269TEST_F(ProfileInfoCacheTest, HasMigrated) {
[email protected]cb114f142011-11-23 20:18:04270 GetCache()->AddProfileToCache(
[email protected]fea25d82012-12-19 01:19:50271 GetProfilePath("path_1"), ASCIIToUTF16("name_1"), string16(), 0, false);
[email protected]cb114f142011-11-23 20:18:04272 GetCache()->AddProfileToCache(
[email protected]fea25d82012-12-19 01:19:50273 GetProfilePath("path_2"), ASCIIToUTF16("name_2"), string16(), 0, false);
[email protected]cb114f142011-11-23 20:18:04274
275 // Sanity check.
276 EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(0));
277 EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(1));
278
279 // Set migrated state for 2nd profile.
280 GetCache()->SetHasMigratedToGAIAInfoOfProfileAtIndex(1, true);
281 EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(0));
282 EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(1));
283
284 // Set migrated state for 1st profile.
285 GetCache()->SetHasMigratedToGAIAInfoOfProfileAtIndex(0, true);
286 EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(0));
287 EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(1));
288
289 // Unset migrated state for 2nd profile.
290 GetCache()->SetHasMigratedToGAIAInfoOfProfileAtIndex(1, false);
291 EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(0));
292 EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(1));
293}
294
[email protected]d4f5d1162011-11-30 01:41:52295TEST_F(ProfileInfoCacheTest, GAIAName) {
[email protected]cb114f142011-11-23 20:18:04296 GetCache()->AddProfileToCache(
[email protected]fea25d82012-12-19 01:19:50297 GetProfilePath("path_1"), ASCIIToUTF16("name_1"), string16(), 0, false);
[email protected]cb114f142011-11-23 20:18:04298 string16 profile_name(ASCIIToUTF16("profile name 2"));
299 GetCache()->AddProfileToCache(
[email protected]fea25d82012-12-19 01:19:50300 GetProfilePath("path_2"), profile_name, string16(), 0, false);
[email protected]cb114f142011-11-23 20:18:04301
302 // Sanity check.
303 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(0).empty());
304 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(1).empty());
305 EXPECT_FALSE(GetCache()->IsUsingGAIANameOfProfileAtIndex(0));
306 EXPECT_FALSE(GetCache()->IsUsingGAIANameOfProfileAtIndex(1));
307
308 // Set GAIA name.
309 string16 gaia_name(ASCIIToUTF16("Pat Smith"));
310 GetCache()->SetGAIANameOfProfileAtIndex(1, gaia_name);
311 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(0).empty());
312 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(1));
313 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(1));
314
315 // Use GAIA name as profile name.
316 GetCache()->SetIsUsingGAIANameOfProfileAtIndex(1, true);
317
318 EXPECT_EQ(gaia_name, GetCache()->GetNameOfProfileAtIndex(1));
319 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(1));
320
321 // Don't use GAIA name as profile name.
322 GetCache()->SetIsUsingGAIANameOfProfileAtIndex(1, false);
323 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(1));
324 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(1));
325}
326
[email protected]d4f5d1162011-11-30 01:41:52327TEST_F(ProfileInfoCacheTest, GAIAPicture) {
[email protected]cb114f142011-11-23 20:18:04328 GetCache()->AddProfileToCache(
[email protected]fea25d82012-12-19 01:19:50329 GetProfilePath("path_1"), ASCIIToUTF16("name_1"), string16(), 0, false);
[email protected]cb114f142011-11-23 20:18:04330 GetCache()->AddProfileToCache(
[email protected]fea25d82012-12-19 01:19:50331 GetProfilePath("path_2"), ASCIIToUTF16("name_2"), string16(), 0, false);
[email protected]cb114f142011-11-23 20:18:04332
333 // Sanity check.
[email protected]2f3c00f2011-11-30 04:36:22334 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
335 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(1));
[email protected]cb114f142011-11-23 20:18:04336 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0));
337 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
338
339 // The profile icon should be the default one.
340 int id = ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(0);
341 const gfx::Image& profile_image(
342 ResourceBundle::GetSharedInstance().GetImageNamed(id));
[email protected]d4f5d1162011-11-30 01:41:52343 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]cb114f142011-11-23 20:18:04344 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
345
346 // Set GAIA picture.
[email protected]d4f5d1162011-11-30 01:41:52347 gfx::Image gaia_image(gfx::test::CreateImage());
[email protected]2f3c00f2011-11-30 04:36:22348 GetCache()->SetGAIAPictureOfProfileAtIndex(1, &gaia_image);
349 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
[email protected]d4f5d1162011-11-30 01:41:52350 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22351 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
[email protected]d4f5d1162011-11-30 01:41:52352 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]cb114f142011-11-23 20:18:04353 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
354
355 // Use GAIA picture as profile picture.
356 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, true);
[email protected]d4f5d1162011-11-30 01:41:52357 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22358 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
[email protected]d4f5d1162011-11-30 01:41:52359 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]cb114f142011-11-23 20:18:04360 gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
361
362 // Don't use GAIA picture as profile picture.
363 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, false);
[email protected]d4f5d1162011-11-30 01:41:52364 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22365 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
[email protected]d4f5d1162011-11-30 01:41:52366 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]cb114f142011-11-23 20:18:04367 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
368}
369
[email protected]754bebc2011-12-01 16:42:16370TEST_F(ProfileInfoCacheTest, PersistGAIAPicture) {
[email protected]cb114f142011-11-23 20:18:04371 GetCache()->AddProfileToCache(
[email protected]fea25d82012-12-19 01:19:50372 GetProfilePath("path_1"), ASCIIToUTF16("name_1"), string16(), 0, false);
[email protected]d4f5d1162011-11-30 01:41:52373 gfx::Image gaia_image(gfx::test::CreateImage());
[email protected]cb114f142011-11-23 20:18:04374
[email protected]a7fe9112012-07-20 02:34:45375 content::WindowedNotificationObserver save_observer(
[email protected]cb114f142011-11-23 20:18:04376 chrome::NOTIFICATION_PROFILE_CACHE_PICTURE_SAVED,
377 content::NotificationService::AllSources());
[email protected]2f3c00f2011-11-30 04:36:22378 GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_image);
[email protected]d4f5d1162011-11-30 01:41:52379 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22380 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
[email protected]cb114f142011-11-23 20:18:04381
382 // Wait for the file to be written to disk then reset the cache.
383 save_observer.Wait();
384 ResetCache();
385
386 // Try to get the GAIA picture. This should return NULL until the read from
387 // disk is done.
[email protected]a7fe9112012-07-20 02:34:45388 content::WindowedNotificationObserver read_observer(
[email protected]cb114f142011-11-23 20:18:04389 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED,
390 content::NotificationService::AllSources());
[email protected]2f3c00f2011-11-30 04:36:22391 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
[email protected]cb114f142011-11-23 20:18:04392 read_observer.Wait();
[email protected]d4f5d1162011-11-30 01:41:52393 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22394 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
395}
396
[email protected]0aa018a2013-07-31 15:08:54397TEST_F(ProfileInfoCacheTest, SetProfileIsManaged) {
398 GetCache()->AddProfileToCache(
399 GetProfilePath("test"), ASCIIToUTF16("Test"), string16(), 0, false);
400 EXPECT_FALSE(GetCache()->ProfileIsManagedAtIndex(0));
401
402 GetCache()->SetProfileIsManagedAtIndex(0, true);
403 EXPECT_TRUE(GetCache()->ProfileIsManagedAtIndex(0));
404
405 ResetCache();
406 EXPECT_TRUE(GetCache()->ProfileIsManagedAtIndex(0));
407
408 GetCache()->SetProfileIsManagedAtIndex(0, false);
409 EXPECT_FALSE(GetCache()->ProfileIsManagedAtIndex(0));
410}
411
[email protected]2f3c00f2011-11-30 04:36:22412TEST_F(ProfileInfoCacheTest, EmptyGAIAInfo) {
413 string16 profile_name = ASCIIToUTF16("name_1");
414 int id = ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(0);
415 const gfx::Image& profile_image(
416 ResourceBundle::GetSharedInstance().GetImageNamed(id));
417
418 GetCache()->AddProfileToCache(
[email protected]fea25d82012-12-19 01:19:50419 GetProfilePath("path_1"), profile_name, string16(), 0, false);
[email protected]2f3c00f2011-11-30 04:36:22420
421 // Set empty GAIA info.
422 GetCache()->SetGAIANameOfProfileAtIndex(0, string16());
423 GetCache()->SetGAIAPictureOfProfileAtIndex(0, NULL);
424 GetCache()->SetIsUsingGAIANameOfProfileAtIndex(0, true);
425 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(0, true);
426
427 // Verify that the profile name and picture are not empty.
428 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(0));
429 EXPECT_TRUE(gfx::test::IsEqual(
430 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(0)));
[email protected]cb114f142011-11-23 20:18:04431}
432
[email protected]0aa018a2013-07-31 15:08:54433TEST_F(ProfileInfoCacheTest, CreateManagedTestingProfile) {
434 testing_profile_manager_.CreateTestingProfile("default");
435 string16 managed_user_name = ASCIIToUTF16("Supervised User");
436 testing_profile_manager_.CreateTestingProfile(
437 "test1", scoped_ptr<PrefServiceSyncable>(), managed_user_name, 0, true);
438 for (size_t i = 0; i < GetCache()->GetNumberOfProfiles(); i++) {
439 bool is_managed =
440 GetCache()->GetNameOfProfileAtIndex(i) == managed_user_name;
441 EXPECT_EQ(is_managed, GetCache()->ProfileIsManagedAtIndex(i));
442 }
443}
444
[email protected]6730b1e2011-09-29 05:23:52445} // namespace