blob: e7b7fef8bcebfcb2a1cc1c4ea71d75de6986d11d [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]dee810e2011-06-27 19:43:398#include "base/stringprintf.h"
9#include "base/utf_string_conversions.h"
[email protected]583844c2011-08-27 00:38:3510#include "chrome/browser/browser_process.h"
[email protected]d4f5d1162011-11-30 01:41:5211#include "chrome/browser/profiles/profile_info_cache.h"
[email protected]6730b1e2011-09-29 05:23:5212#include "chrome/browser/profiles/profile_manager.h"
[email protected]cb114f142011-11-23 20:18:0413#include "chrome/common/chrome_notification_types.h"
[email protected]583844c2011-08-27 00:38:3514#include "chrome/test/base/testing_browser_process.h"
[email protected]cb114f142011-11-23 20:18:0415#include "content/public/browser/notification_observer.h"
16#include "content/public/browser/notification_registrar.h"
[email protected]c0b030902011-12-19 16:21:0517#include "content/public/browser/notification_service.h"
[email protected]a7fe9112012-07-20 02:34:4518#include "content/public/test/test_utils.h"
[email protected]dee810e2011-06-27 19:43:3919#include "third_party/skia/include/core/SkBitmap.h"
20#include "ui/base/resource/resource_bundle.h"
21#include "ui/gfx/image/image.h"
[email protected]d4f5d1162011-11-30 01:41:5222#include "ui/gfx/image/image_unittest_util.h"
[email protected]dee810e2011-06-27 19:43:3923
[email protected]cb114f142011-11-23 20:18:0424using content::BrowserThread;
25
[email protected]6a460662011-12-22 22:05:1626ProfileNameVerifierObserver::ProfileNameVerifierObserver(
27 TestingProfileManager* testing_profile_manager)
28 : testing_profile_manager_(testing_profile_manager) {
29 DCHECK(testing_profile_manager_);
[email protected]590e189b2011-12-13 22:07:0330}
31
32ProfileNameVerifierObserver::~ProfileNameVerifierObserver() {
33}
34
35void ProfileNameVerifierObserver::OnProfileAdded(
[email protected]650b2d52013-02-10 03:41:4536 const base::FilePath& profile_path) {
[email protected]6a460662011-12-22 22:05:1637 string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
38 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0339 EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end());
40 profile_names_.insert(profile_name);
41}
42
[email protected]7b0af152011-12-16 17:02:0643void ProfileNameVerifierObserver::OnProfileWillBeRemoved(
[email protected]650b2d52013-02-10 03:41:4544 const base::FilePath& profile_path) {
[email protected]6a460662011-12-22 22:05:1645 string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
46 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0347 EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end());
48 profile_names_.erase(profile_name);
49}
50
[email protected]7b0af152011-12-16 17:02:0651void ProfileNameVerifierObserver::OnProfileWasRemoved(
[email protected]650b2d52013-02-10 03:41:4552 const base::FilePath& profile_path,
[email protected]7b0af152011-12-16 17:02:0653 const string16& profile_name) {
54 EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end());
55}
56
[email protected]590e189b2011-12-13 22:07:0357void ProfileNameVerifierObserver::OnProfileNameChanged(
[email protected]650b2d52013-02-10 03:41:4558 const base::FilePath& profile_path,
[email protected]6a460662011-12-22 22:05:1659 const string16& old_profile_name) {
60 string16 new_profile_name = GetCache()->GetNameOfProfileAtIndex(
61 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0362 EXPECT_TRUE(profile_names_.find(old_profile_name) != profile_names_.end());
63 EXPECT_TRUE(profile_names_.find(new_profile_name) == profile_names_.end());
64 profile_names_.erase(old_profile_name);
65 profile_names_.insert(new_profile_name);
66}
67
68void ProfileNameVerifierObserver::OnProfileAvatarChanged(
[email protected]650b2d52013-02-10 03:41:4569 const base::FilePath& profile_path) {
[email protected]6a460662011-12-22 22:05:1670 string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
71 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0372 EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end());
73}
74
[email protected]6a460662011-12-22 22:05:1675ProfileInfoCache* ProfileNameVerifierObserver::GetCache() {
76 return testing_profile_manager_->profile_info_cache();
77}
78
[email protected]d4f5d1162011-11-30 01:41:5279ProfileInfoCacheTest::ProfileInfoCacheTest()
80 : testing_profile_manager_(
[email protected]c494d082013-01-04 20:41:2281 TestingBrowserProcess::GetGlobal()),
[email protected]d4f5d1162011-11-30 01:41:5282 ui_thread_(BrowserThread::UI, &ui_loop_),
[email protected]6a460662011-12-22 22:05:1683 file_thread_(BrowserThread::FILE, &ui_loop_),
84 name_observer_(&testing_profile_manager_) {
[email protected]d4f5d1162011-11-30 01:41:5285}
86
87ProfileInfoCacheTest::~ProfileInfoCacheTest() {
88}
89
90void ProfileInfoCacheTest::SetUp() {
91 ASSERT_TRUE(testing_profile_manager_.SetUp());
[email protected]590e189b2011-12-13 22:07:0392 testing_profile_manager_.profile_info_cache()->AddObserver(&name_observer_);
[email protected]d4f5d1162011-11-30 01:41:5293}
94
[email protected]1302dcf2011-11-30 21:47:0595void ProfileInfoCacheTest::TearDown() {
96 // Drain the UI thread to make sure all tasks are completed. This prevents
97 // memory leaks.
[email protected]b8f50ce2012-11-17 12:37:5798 ui_loop_.RunUntilIdle();
[email protected]1302dcf2011-11-30 21:47:0599}
100
[email protected]d4f5d1162011-11-30 01:41:52101ProfileInfoCache* ProfileInfoCacheTest::GetCache() {
102 return testing_profile_manager_.profile_info_cache();
103}
104
[email protected]650b2d52013-02-10 03:41:45105base::FilePath ProfileInfoCacheTest::GetProfilePath(
[email protected]d4f5d1162011-11-30 01:41:52106 const std::string& base_name) {
107 return testing_profile_manager_.profile_manager()->user_data_dir().
108 AppendASCII(base_name);
109}
110
111void ProfileInfoCacheTest::ResetCache() {
112 testing_profile_manager_.DeleteProfileInfoCache();
113}
114
[email protected]dee810e2011-06-27 19:43:39115namespace {
116
[email protected]d4f5d1162011-11-30 01:41:52117TEST_F(ProfileInfoCacheTest, AddProfiles) {
[email protected]6730b1e2011-09-29 05:23:52118 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39119
[email protected]ca591072012-03-27 01:54:44120 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
[email protected]dee810e2011-06-27 19:43:39121 for (uint32 i = 0; i < 4; ++i) {
[email protected]650b2d52013-02-10 03:41:45122 base::FilePath profile_path = GetProfilePath(StringPrintf("path_%ud", i));
[email protected]dee810e2011-06-27 19:43:39123 string16 profile_name = ASCIIToUTF16(StringPrintf("name_%ud", i));
[email protected]ca591072012-03-27 01:54:44124 const SkBitmap* icon = rb.GetImageNamed(
125 ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(
126 i)).ToSkBitmap();
[email protected]dee810e2011-06-27 19:43:39127
[email protected]fea25d82012-12-19 01:19:50128 GetCache()->AddProfileToCache(profile_path, profile_name, string16(), i,
129 false);
[email protected]cb114f142011-11-23 20:18:04130 GetCache()->SetBackgroundStatusOfProfileAtIndex(i, true);
131 string16 gaia_name = ASCIIToUTF16(StringPrintf("gaia_%ud", i));
132 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]ca591072012-03-27 01:54:44137 const SkBitmap* actual_icon = GetCache()->GetAvatarIconOfProfileAtIndex(
138 i).ToSkBitmap();
139 EXPECT_EQ(icon->width(), actual_icon->width());
140 EXPECT_EQ(icon->height(), actual_icon->height());
[email protected]dee810e2011-06-27 19:43:39141 }
[email protected]cb114f142011-11-23 20:18:04142
143 // Reset the cache and test the it reloads correctly.
144 ResetCache();
145
146 EXPECT_EQ(4u, GetCache()->GetNumberOfProfiles());
147 for (uint32 i = 0; i < 4; ++i) {
[email protected]650b2d52013-02-10 03:41:45148 base::FilePath profile_path = GetProfilePath(StringPrintf("path_%ud", i));
[email protected]cb114f142011-11-23 20:18:04149 EXPECT_EQ(i, GetCache()->GetIndexOfProfileWithPath(profile_path));
150 string16 profile_name = ASCIIToUTF16(StringPrintf("name_%ud", i));
151 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
152 EXPECT_EQ(i, GetCache()->GetAvatarIconIndexOfProfileAtIndex(i));
153 EXPECT_EQ(true, GetCache()->GetBackgroundStatusOfProfileAtIndex(i));
154 string16 gaia_name = ASCIIToUTF16(StringPrintf("gaia_%ud", i));
155 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(i));
156 }
[email protected]dee810e2011-06-27 19:43:39157}
158
[email protected]d4f5d1162011-11-30 01:41:52159TEST_F(ProfileInfoCacheTest, DeleteProfile) {
[email protected]6730b1e2011-09-29 05:23:52160 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39161
[email protected]650b2d52013-02-10 03:41:45162 base::FilePath path_1 = GetProfilePath("path_1");
[email protected]73cb3722011-10-11 18:12:13163 GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("name_1"), string16(),
[email protected]fea25d82012-12-19 01:19:50164 0, false);
[email protected]6730b1e2011-09-29 05:23:52165 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39166
[email protected]650b2d52013-02-10 03:41:45167 base::FilePath path_2 = GetProfilePath("path_2");
[email protected]dee810e2011-06-27 19:43:39168 string16 name_2 = ASCIIToUTF16("name_2");
[email protected]fea25d82012-12-19 01:19:50169 GetCache()->AddProfileToCache(path_2, name_2, string16(), 0, false);
[email protected]6730b1e2011-09-29 05:23:52170 EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39171
[email protected]6730b1e2011-09-29 05:23:52172 GetCache()->DeleteProfileFromCache(path_1);
173 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
174 EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(0));
[email protected]dee810e2011-06-27 19:43:39175
[email protected]6730b1e2011-09-29 05:23:52176 GetCache()->DeleteProfileFromCache(path_2);
177 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39178}
179
[email protected]d4f5d1162011-11-30 01:41:52180TEST_F(ProfileInfoCacheTest, MutateProfile) {
181 GetCache()->AddProfileToCache(
[email protected]fea25d82012-12-19 01:19:50182 GetProfilePath("path_1"), ASCIIToUTF16("name_1"), string16(), 0, false);
[email protected]d4f5d1162011-11-30 01:41:52183 GetCache()->AddProfileToCache(
[email protected]fea25d82012-12-19 01:19:50184 GetProfilePath("path_2"), ASCIIToUTF16("name_2"), string16(), 0, false);
[email protected]dee810e2011-06-27 19:43:39185
186 string16 new_name = ASCIIToUTF16("new_name");
[email protected]6730b1e2011-09-29 05:23:52187 GetCache()->SetNameOfProfileAtIndex(1, new_name);
188 EXPECT_EQ(new_name, GetCache()->GetNameOfProfileAtIndex(1));
189 EXPECT_NE(new_name, GetCache()->GetNameOfProfileAtIndex(0));
[email protected]dee810e2011-06-27 19:43:39190
[email protected]e8e78092011-09-29 18:15:38191 string16 new_user_name = ASCIIToUTF16("user_name");
192 GetCache()->SetUserNameOfProfileAtIndex(1, new_user_name);
193 EXPECT_EQ(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(1));
194 EXPECT_NE(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(0));
195
[email protected]dee810e2011-06-27 19:43:39196 size_t new_icon_index = 3;
[email protected]6730b1e2011-09-29 05:23:52197 GetCache()->SetAvatarIconOfProfileAtIndex(1, new_icon_index);
[email protected]dee810e2011-06-27 19:43:39198 // Not much to test.
[email protected]6730b1e2011-09-29 05:23:52199 GetCache()->GetAvatarIconOfProfileAtIndex(1);
[email protected]dee810e2011-06-27 19:43:39200}
201
[email protected]d4f5d1162011-11-30 01:41:52202TEST_F(ProfileInfoCacheTest, Sort) {
[email protected]cb114f142011-11-23 20:18:04203 string16 name_a = ASCIIToUTF16("apple");
[email protected]d4f5d1162011-11-30 01:41:52204 GetCache()->AddProfileToCache(
[email protected]fea25d82012-12-19 01:19:50205 GetProfilePath("path_a"), name_a, string16(), 0, false);
[email protected]cb114f142011-11-23 20:18:04206
207 string16 name_c = ASCIIToUTF16("cat");
[email protected]d4f5d1162011-11-30 01:41:52208 GetCache()->AddProfileToCache(
[email protected]fea25d82012-12-19 01:19:50209 GetProfilePath("path_c"), name_c, string16(), 0, false);
[email protected]cb114f142011-11-23 20:18:04210
211 // Sanity check the initial order.
212 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
213 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
214
215 // Add a new profile (start with a capital to test case insensitive sorting.
216 string16 name_b = ASCIIToUTF16("Banana");
[email protected]d4f5d1162011-11-30 01:41:52217 GetCache()->AddProfileToCache(
[email protected]fea25d82012-12-19 01:19:50218 GetProfilePath("path_b"), name_b, string16(), 0, false);
[email protected]cb114f142011-11-23 20:18:04219
220 // Verify the new order.
221 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
222 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(1));
223 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(2));
224
225 // Change the name of an existing profile.
226 name_a = UTF8ToUTF16("dog");
227 GetCache()->SetNameOfProfileAtIndex(0, name_a);
228
229 // Verify the new order.
230 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
231 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
232 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(2));
233
234 // Delete a profile.
[email protected]d4f5d1162011-11-30 01:41:52235 GetCache()->DeleteProfileFromCache(GetProfilePath("path_c"));
[email protected]cb114f142011-11-23 20:18:04236
237 // Verify the new order.
238 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
239 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(1));
240}
241
[email protected]d4f5d1162011-11-30 01:41:52242TEST_F(ProfileInfoCacheTest, BackgroundModeStatus) {
[email protected]279170832011-10-12 23:38:03243 GetCache()->AddProfileToCache(
[email protected]fea25d82012-12-19 01:19:50244 GetProfilePath("path_1"), ASCIIToUTF16("name_1"), string16(), 0, false);
[email protected]279170832011-10-12 23:38:03245 GetCache()->AddProfileToCache(
[email protected]fea25d82012-12-19 01:19:50246 GetProfilePath("path_2"), ASCIIToUTF16("name_2"), string16(), 0, false);
[email protected]279170832011-10-12 23:38:03247
248 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
249 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
250
251 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, true);
252
253 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
254 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
255
256 GetCache()->SetBackgroundStatusOfProfileAtIndex(0, true);
257
258 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
259 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
260
261 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, false);
262
263 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
264 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
265}
266
[email protected]d4f5d1162011-11-30 01:41:52267TEST_F(ProfileInfoCacheTest, HasMigrated) {
[email protected]cb114f142011-11-23 20:18:04268 GetCache()->AddProfileToCache(
[email protected]fea25d82012-12-19 01:19:50269 GetProfilePath("path_1"), ASCIIToUTF16("name_1"), string16(), 0, false);
[email protected]cb114f142011-11-23 20:18:04270 GetCache()->AddProfileToCache(
[email protected]fea25d82012-12-19 01:19:50271 GetProfilePath("path_2"), ASCIIToUTF16("name_2"), string16(), 0, false);
[email protected]cb114f142011-11-23 20:18:04272
273 // Sanity check.
274 EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(0));
275 EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(1));
276
277 // Set migrated state for 2nd profile.
278 GetCache()->SetHasMigratedToGAIAInfoOfProfileAtIndex(1, true);
279 EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(0));
280 EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(1));
281
282 // Set migrated state for 1st profile.
283 GetCache()->SetHasMigratedToGAIAInfoOfProfileAtIndex(0, true);
284 EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(0));
285 EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(1));
286
287 // Unset migrated state for 2nd profile.
288 GetCache()->SetHasMigratedToGAIAInfoOfProfileAtIndex(1, false);
289 EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(0));
290 EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(1));
291}
292
[email protected]d4f5d1162011-11-30 01:41:52293TEST_F(ProfileInfoCacheTest, GAIAName) {
[email protected]cb114f142011-11-23 20:18:04294 GetCache()->AddProfileToCache(
[email protected]fea25d82012-12-19 01:19:50295 GetProfilePath("path_1"), ASCIIToUTF16("name_1"), string16(), 0, false);
[email protected]cb114f142011-11-23 20:18:04296 string16 profile_name(ASCIIToUTF16("profile name 2"));
297 GetCache()->AddProfileToCache(
[email protected]fea25d82012-12-19 01:19:50298 GetProfilePath("path_2"), profile_name, string16(), 0, false);
[email protected]cb114f142011-11-23 20:18:04299
300 // Sanity check.
301 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(0).empty());
302 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(1).empty());
303 EXPECT_FALSE(GetCache()->IsUsingGAIANameOfProfileAtIndex(0));
304 EXPECT_FALSE(GetCache()->IsUsingGAIANameOfProfileAtIndex(1));
305
306 // Set GAIA name.
307 string16 gaia_name(ASCIIToUTF16("Pat Smith"));
308 GetCache()->SetGAIANameOfProfileAtIndex(1, gaia_name);
309 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(0).empty());
310 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(1));
311 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(1));
312
313 // Use GAIA name as profile name.
314 GetCache()->SetIsUsingGAIANameOfProfileAtIndex(1, true);
315
316 EXPECT_EQ(gaia_name, GetCache()->GetNameOfProfileAtIndex(1));
317 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(1));
318
319 // Don't use GAIA name as profile name.
320 GetCache()->SetIsUsingGAIANameOfProfileAtIndex(1, false);
321 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(1));
322 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(1));
323}
324
[email protected]d4f5d1162011-11-30 01:41:52325TEST_F(ProfileInfoCacheTest, GAIAPicture) {
[email protected]cb114f142011-11-23 20:18:04326 GetCache()->AddProfileToCache(
[email protected]fea25d82012-12-19 01:19:50327 GetProfilePath("path_1"), ASCIIToUTF16("name_1"), string16(), 0, false);
[email protected]cb114f142011-11-23 20:18:04328 GetCache()->AddProfileToCache(
[email protected]fea25d82012-12-19 01:19:50329 GetProfilePath("path_2"), ASCIIToUTF16("name_2"), string16(), 0, false);
[email protected]cb114f142011-11-23 20:18:04330
331 // Sanity check.
[email protected]2f3c00f2011-11-30 04:36:22332 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
333 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(1));
[email protected]cb114f142011-11-23 20:18:04334 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0));
335 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
336
337 // The profile icon should be the default one.
338 int id = ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(0);
339 const gfx::Image& profile_image(
340 ResourceBundle::GetSharedInstance().GetImageNamed(id));
[email protected]d4f5d1162011-11-30 01:41:52341 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]cb114f142011-11-23 20:18:04342 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
343
344 // Set GAIA picture.
[email protected]d4f5d1162011-11-30 01:41:52345 gfx::Image gaia_image(gfx::test::CreateImage());
[email protected]2f3c00f2011-11-30 04:36:22346 GetCache()->SetGAIAPictureOfProfileAtIndex(1, &gaia_image);
347 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
[email protected]d4f5d1162011-11-30 01:41:52348 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22349 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
[email protected]d4f5d1162011-11-30 01:41:52350 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]cb114f142011-11-23 20:18:04351 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
352
353 // Use GAIA picture as profile picture.
354 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, true);
[email protected]d4f5d1162011-11-30 01:41:52355 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22356 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
[email protected]d4f5d1162011-11-30 01:41:52357 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]cb114f142011-11-23 20:18:04358 gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
359
360 // Don't use GAIA picture as profile picture.
361 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, false);
[email protected]d4f5d1162011-11-30 01:41:52362 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22363 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
[email protected]d4f5d1162011-11-30 01:41:52364 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]cb114f142011-11-23 20:18:04365 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
366}
367
[email protected]754bebc2011-12-01 16:42:16368TEST_F(ProfileInfoCacheTest, PersistGAIAPicture) {
[email protected]cb114f142011-11-23 20:18:04369 GetCache()->AddProfileToCache(
[email protected]fea25d82012-12-19 01:19:50370 GetProfilePath("path_1"), ASCIIToUTF16("name_1"), string16(), 0, false);
[email protected]d4f5d1162011-11-30 01:41:52371 gfx::Image gaia_image(gfx::test::CreateImage());
[email protected]cb114f142011-11-23 20:18:04372
[email protected]a7fe9112012-07-20 02:34:45373 content::WindowedNotificationObserver save_observer(
[email protected]cb114f142011-11-23 20:18:04374 chrome::NOTIFICATION_PROFILE_CACHE_PICTURE_SAVED,
375 content::NotificationService::AllSources());
[email protected]2f3c00f2011-11-30 04:36:22376 GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_image);
[email protected]d4f5d1162011-11-30 01:41:52377 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22378 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
[email protected]cb114f142011-11-23 20:18:04379
380 // Wait for the file to be written to disk then reset the cache.
381 save_observer.Wait();
382 ResetCache();
383
384 // Try to get the GAIA picture. This should return NULL until the read from
385 // disk is done.
[email protected]a7fe9112012-07-20 02:34:45386 content::WindowedNotificationObserver read_observer(
[email protected]cb114f142011-11-23 20:18:04387 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED,
388 content::NotificationService::AllSources());
[email protected]2f3c00f2011-11-30 04:36:22389 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
[email protected]cb114f142011-11-23 20:18:04390 read_observer.Wait();
[email protected]d4f5d1162011-11-30 01:41:52391 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22392 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
393}
394
395TEST_F(ProfileInfoCacheTest, EmptyGAIAInfo) {
396 string16 profile_name = ASCIIToUTF16("name_1");
397 int id = ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(0);
398 const gfx::Image& profile_image(
399 ResourceBundle::GetSharedInstance().GetImageNamed(id));
400
401 GetCache()->AddProfileToCache(
[email protected]fea25d82012-12-19 01:19:50402 GetProfilePath("path_1"), profile_name, string16(), 0, false);
[email protected]2f3c00f2011-11-30 04:36:22403
404 // Set empty GAIA info.
405 GetCache()->SetGAIANameOfProfileAtIndex(0, string16());
406 GetCache()->SetGAIAPictureOfProfileAtIndex(0, NULL);
407 GetCache()->SetIsUsingGAIANameOfProfileAtIndex(0, true);
408 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(0, true);
409
410 // Verify that the profile name and picture are not empty.
411 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(0));
412 EXPECT_TRUE(gfx::test::IsEqual(
413 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(0)));
[email protected]cb114f142011-11-23 20:18:04414}
415
[email protected]6730b1e2011-09-29 05:23:52416} // namespace