blob: 3c2fcaa86eae1b15846d7a3ac7ca73ccba357ab7 [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]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]7d3cbc92013-03-18 22:33:04122 base::FilePath profile_path =
123 GetProfilePath(base::StringPrintf("path_%ud", i));
124 string16 profile_name = ASCIIToUTF16(base::StringPrintf("name_%ud", i));
[email protected]ca591072012-03-27 01:54:44125 const SkBitmap* icon = rb.GetImageNamed(
126 ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(
127 i)).ToSkBitmap();
[email protected]dee810e2011-06-27 19:43:39128
[email protected]fea25d82012-12-19 01:19:50129 GetCache()->AddProfileToCache(profile_path, profile_name, string16(), i,
130 false);
[email protected]cb114f142011-11-23 20:18:04131 GetCache()->SetBackgroundStatusOfProfileAtIndex(i, true);
[email protected]7d3cbc92013-03-18 22:33:04132 string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
[email protected]cb114f142011-11-23 20:18:04133 GetCache()->SetGAIANameOfProfileAtIndex(i, gaia_name);
[email protected]dee810e2011-06-27 19:43:39134
[email protected]6730b1e2011-09-29 05:23:52135 EXPECT_EQ(i + 1, GetCache()->GetNumberOfProfiles());
136 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
137 EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i));
[email protected]ca591072012-03-27 01:54:44138 const SkBitmap* actual_icon = GetCache()->GetAvatarIconOfProfileAtIndex(
139 i).ToSkBitmap();
140 EXPECT_EQ(icon->width(), actual_icon->width());
141 EXPECT_EQ(icon->height(), actual_icon->height());
[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
397TEST_F(ProfileInfoCacheTest, EmptyGAIAInfo) {
398 string16 profile_name = ASCIIToUTF16("name_1");
399 int id = ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(0);
400 const gfx::Image& profile_image(
401 ResourceBundle::GetSharedInstance().GetImageNamed(id));
402
403 GetCache()->AddProfileToCache(
[email protected]fea25d82012-12-19 01:19:50404 GetProfilePath("path_1"), profile_name, string16(), 0, false);
[email protected]2f3c00f2011-11-30 04:36:22405
406 // Set empty GAIA info.
407 GetCache()->SetGAIANameOfProfileAtIndex(0, string16());
408 GetCache()->SetGAIAPictureOfProfileAtIndex(0, NULL);
409 GetCache()->SetIsUsingGAIANameOfProfileAtIndex(0, true);
410 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(0, true);
411
412 // Verify that the profile name and picture are not empty.
413 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(0));
414 EXPECT_TRUE(gfx::test::IsEqual(
415 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(0)));
[email protected]cb114f142011-11-23 20:18:04416}
417
[email protected]6730b1e2011-09-29 05:23:52418} // namespace