blob: 1d5e6df7d0119a42ab26f372121b4fd412b5cb3a [file] [log] [blame]
[email protected]ca591072012-03-27 01:54:441// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]dee810e2011-06-27 19:43:392// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]d4f5d1162011-11-30 01:41:525#include "chrome/browser/profiles/profile_info_cache_unittest.h"
[email protected]dee810e2011-06-27 19:43:396
[email protected]047481802013-09-16 22:26:387#include <vector>
8
[email protected]e5ba874f2013-02-14 17:20:199#include "base/prefs/testing_pref_service.h"
[email protected]76fb05c2013-06-11 04:38:0510#include "base/strings/stringprintf.h"
[email protected]e309f312013-06-07 21:50:0811#include "base/strings/utf_string_conversions.h"
[email protected]583844c2011-08-27 00:38:3512#include "chrome/browser/browser_process.h"
[email protected]25ff0862013-07-12 00:59:0313#include "chrome/browser/chrome_notification_types.h"
[email protected]0aa018a2013-07-31 15:08:5414#include "chrome/browser/prefs/pref_service_syncable.h"
[email protected]d4f5d1162011-11-30 01:41:5215#include "chrome/browser/profiles/profile_info_cache.h"
[email protected]6730b1e2011-09-29 05:23:5216#include "chrome/browser/profiles/profile_manager.h"
[email protected]583844c2011-08-27 00:38:3517#include "chrome/test/base/testing_browser_process.h"
[email protected]cb114f142011-11-23 20:18:0418#include "content/public/browser/notification_observer.h"
19#include "content/public/browser/notification_registrar.h"
[email protected]c0b030902011-12-19 16:21:0520#include "content/public/browser/notification_service.h"
[email protected]a7fe9112012-07-20 02:34:4521#include "content/public/test/test_utils.h"
[email protected]dee810e2011-06-27 19:43:3922#include "third_party/skia/include/core/SkBitmap.h"
23#include "ui/base/resource/resource_bundle.h"
24#include "ui/gfx/image/image.h"
[email protected]d4f5d1162011-11-30 01:41:5225#include "ui/gfx/image/image_unittest_util.h"
[email protected]dee810e2011-06-27 19:43:3926
[email protected]cb114f142011-11-23 20:18:0427using content::BrowserThread;
28
[email protected]6a460662011-12-22 22:05:1629ProfileNameVerifierObserver::ProfileNameVerifierObserver(
30 TestingProfileManager* testing_profile_manager)
31 : testing_profile_manager_(testing_profile_manager) {
32 DCHECK(testing_profile_manager_);
[email protected]590e189b2011-12-13 22:07:0333}
34
35ProfileNameVerifierObserver::~ProfileNameVerifierObserver() {
36}
37
38void ProfileNameVerifierObserver::OnProfileAdded(
[email protected]650b2d52013-02-10 03:41:4539 const base::FilePath& profile_path) {
[email protected]6a460662011-12-22 22:05:1640 string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
41 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0342 EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end());
43 profile_names_.insert(profile_name);
44}
45
[email protected]7b0af152011-12-16 17:02:0646void ProfileNameVerifierObserver::OnProfileWillBeRemoved(
[email protected]650b2d52013-02-10 03:41:4547 const base::FilePath& profile_path) {
[email protected]6a460662011-12-22 22:05:1648 string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
49 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0350 EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end());
51 profile_names_.erase(profile_name);
52}
53
[email protected]7b0af152011-12-16 17:02:0654void ProfileNameVerifierObserver::OnProfileWasRemoved(
[email protected]650b2d52013-02-10 03:41:4555 const base::FilePath& profile_path,
[email protected]7b0af152011-12-16 17:02:0656 const string16& profile_name) {
57 EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end());
58}
59
[email protected]590e189b2011-12-13 22:07:0360void ProfileNameVerifierObserver::OnProfileNameChanged(
[email protected]650b2d52013-02-10 03:41:4561 const base::FilePath& profile_path,
[email protected]6a460662011-12-22 22:05:1662 const string16& old_profile_name) {
63 string16 new_profile_name = GetCache()->GetNameOfProfileAtIndex(
64 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0365 EXPECT_TRUE(profile_names_.find(old_profile_name) != profile_names_.end());
66 EXPECT_TRUE(profile_names_.find(new_profile_name) == profile_names_.end());
67 profile_names_.erase(old_profile_name);
68 profile_names_.insert(new_profile_name);
69}
70
71void ProfileNameVerifierObserver::OnProfileAvatarChanged(
[email protected]650b2d52013-02-10 03:41:4572 const base::FilePath& profile_path) {
[email protected]6a460662011-12-22 22:05:1673 string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
74 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0375 EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end());
76}
77
[email protected]6a460662011-12-22 22:05:1678ProfileInfoCache* ProfileNameVerifierObserver::GetCache() {
79 return testing_profile_manager_->profile_info_cache();
80}
81
[email protected]d4f5d1162011-11-30 01:41:5282ProfileInfoCacheTest::ProfileInfoCacheTest()
[email protected]0aa018a2013-07-31 15:08:5483 : testing_profile_manager_(TestingBrowserProcess::GetGlobal()),
[email protected]6a460662011-12-22 22:05:1684 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]0aa018a2013-07-31 15:08:5498 base::RunLoop().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]a6e01b42013-08-05 14:13:13128 std::string managed_user_id = i == 3 ? "TEST_ID" : "";
[email protected]dee810e2011-06-27 19:43:39129
[email protected]fea25d82012-12-19 01:19:50130 GetCache()->AddProfileToCache(profile_path, profile_name, string16(), i,
[email protected]a6e01b42013-08-05 14:13:13131 managed_user_id);
[email protected]cb114f142011-11-23 20:18:04132 GetCache()->SetBackgroundStatusOfProfileAtIndex(i, true);
[email protected]7d3cbc92013-03-18 22:33:04133 string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
[email protected]cb114f142011-11-23 20:18:04134 GetCache()->SetGAIANameOfProfileAtIndex(i, gaia_name);
[email protected]dee810e2011-06-27 19:43:39135
[email protected]6730b1e2011-09-29 05:23:52136 EXPECT_EQ(i + 1, GetCache()->GetNumberOfProfiles());
137 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
138 EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i));
[email protected]0aa018a2013-07-31 15:08:54139 const SkBitmap* actual_icon =
140 GetCache()->GetAvatarIconOfProfileAtIndex(i).ToSkBitmap();
[email protected]ca591072012-03-27 01:54:44141 EXPECT_EQ(icon->width(), actual_icon->width());
142 EXPECT_EQ(icon->height(), actual_icon->height());
[email protected]a6e01b42013-08-05 14:13:13143 EXPECT_EQ(i == 3, GetCache()->ProfileIsManagedAtIndex(i));
144 EXPECT_EQ(managed_user_id, GetCache()->GetManagedUserIdOfProfileAtIndex(i));
[email protected]dee810e2011-06-27 19:43:39145 }
[email protected]cb114f142011-11-23 20:18:04146
147 // Reset the cache and test the it reloads correctly.
148 ResetCache();
149
150 EXPECT_EQ(4u, GetCache()->GetNumberOfProfiles());
151 for (uint32 i = 0; i < 4; ++i) {
[email protected]7d3cbc92013-03-18 22:33:04152 base::FilePath profile_path =
153 GetProfilePath(base::StringPrintf("path_%ud", i));
[email protected]cb114f142011-11-23 20:18:04154 EXPECT_EQ(i, GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]7d3cbc92013-03-18 22:33:04155 string16 profile_name = ASCIIToUTF16(base::StringPrintf("name_%ud", i));
[email protected]cb114f142011-11-23 20:18:04156 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
157 EXPECT_EQ(i, GetCache()->GetAvatarIconIndexOfProfileAtIndex(i));
158 EXPECT_EQ(true, GetCache()->GetBackgroundStatusOfProfileAtIndex(i));
[email protected]7d3cbc92013-03-18 22:33:04159 string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
[email protected]cb114f142011-11-23 20:18:04160 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(i));
161 }
[email protected]dee810e2011-06-27 19:43:39162}
163
[email protected]d4f5d1162011-11-30 01:41:52164TEST_F(ProfileInfoCacheTest, DeleteProfile) {
[email protected]6730b1e2011-09-29 05:23:52165 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39166
[email protected]650b2d52013-02-10 03:41:45167 base::FilePath path_1 = GetProfilePath("path_1");
[email protected]73cb3722011-10-11 18:12:13168 GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("name_1"), string16(),
[email protected]a6e01b42013-08-05 14:13:13169 0, std::string());
[email protected]6730b1e2011-09-29 05:23:52170 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39171
[email protected]650b2d52013-02-10 03:41:45172 base::FilePath path_2 = GetProfilePath("path_2");
[email protected]dee810e2011-06-27 19:43:39173 string16 name_2 = ASCIIToUTF16("name_2");
[email protected]a6e01b42013-08-05 14:13:13174 GetCache()->AddProfileToCache(path_2, name_2, string16(), 0, std::string());
[email protected]6730b1e2011-09-29 05:23:52175 EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39176
[email protected]6730b1e2011-09-29 05:23:52177 GetCache()->DeleteProfileFromCache(path_1);
178 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
179 EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(0));
[email protected]dee810e2011-06-27 19:43:39180
[email protected]6730b1e2011-09-29 05:23:52181 GetCache()->DeleteProfileFromCache(path_2);
182 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39183}
184
[email protected]d4f5d1162011-11-30 01:41:52185TEST_F(ProfileInfoCacheTest, MutateProfile) {
186 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13187 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
188 string16(), 0, std::string());
[email protected]d4f5d1162011-11-30 01:41:52189 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13190 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
191 string16(), 0, std::string());
[email protected]dee810e2011-06-27 19:43:39192
193 string16 new_name = ASCIIToUTF16("new_name");
[email protected]6730b1e2011-09-29 05:23:52194 GetCache()->SetNameOfProfileAtIndex(1, new_name);
195 EXPECT_EQ(new_name, GetCache()->GetNameOfProfileAtIndex(1));
196 EXPECT_NE(new_name, GetCache()->GetNameOfProfileAtIndex(0));
[email protected]dee810e2011-06-27 19:43:39197
[email protected]e8e78092011-09-29 18:15:38198 string16 new_user_name = ASCIIToUTF16("user_name");
199 GetCache()->SetUserNameOfProfileAtIndex(1, new_user_name);
200 EXPECT_EQ(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(1));
201 EXPECT_NE(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(0));
202
[email protected]dee810e2011-06-27 19:43:39203 size_t new_icon_index = 3;
[email protected]6730b1e2011-09-29 05:23:52204 GetCache()->SetAvatarIconOfProfileAtIndex(1, new_icon_index);
[email protected]dee810e2011-06-27 19:43:39205 // Not much to test.
[email protected]6730b1e2011-09-29 05:23:52206 GetCache()->GetAvatarIconOfProfileAtIndex(1);
[email protected]dee810e2011-06-27 19:43:39207}
208
[email protected]d4f5d1162011-11-30 01:41:52209TEST_F(ProfileInfoCacheTest, Sort) {
[email protected]cb114f142011-11-23 20:18:04210 string16 name_a = ASCIIToUTF16("apple");
[email protected]d4f5d1162011-11-30 01:41:52211 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13212 GetProfilePath("path_a"), name_a, string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04213
214 string16 name_c = ASCIIToUTF16("cat");
[email protected]d4f5d1162011-11-30 01:41:52215 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13216 GetProfilePath("path_c"), name_c, string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04217
218 // Sanity check the initial order.
219 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
220 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
221
222 // Add a new profile (start with a capital to test case insensitive sorting.
223 string16 name_b = ASCIIToUTF16("Banana");
[email protected]d4f5d1162011-11-30 01:41:52224 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13225 GetProfilePath("path_b"), name_b, string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04226
227 // Verify the new order.
228 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
229 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(1));
230 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(2));
231
232 // Change the name of an existing profile.
233 name_a = UTF8ToUTF16("dog");
234 GetCache()->SetNameOfProfileAtIndex(0, name_a);
235
236 // Verify the new order.
237 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
238 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
239 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(2));
240
241 // Delete a profile.
[email protected]d4f5d1162011-11-30 01:41:52242 GetCache()->DeleteProfileFromCache(GetProfilePath("path_c"));
[email protected]cb114f142011-11-23 20:18:04243
244 // Verify the new order.
245 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
246 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(1));
247}
248
[email protected]d4f5d1162011-11-30 01:41:52249TEST_F(ProfileInfoCacheTest, BackgroundModeStatus) {
[email protected]279170832011-10-12 23:38:03250 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13251 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
252 string16(), 0, std::string());
[email protected]279170832011-10-12 23:38:03253 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13254 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
255 string16(), 0, std::string());
[email protected]279170832011-10-12 23:38:03256
257 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
258 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
259
260 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, true);
261
262 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
263 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
264
265 GetCache()->SetBackgroundStatusOfProfileAtIndex(0, true);
266
267 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
268 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
269
270 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, false);
271
272 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
273 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
274}
275
[email protected]d4f5d1162011-11-30 01:41:52276TEST_F(ProfileInfoCacheTest, HasMigrated) {
[email protected]cb114f142011-11-23 20:18:04277 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13278 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
279 string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04280 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13281 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
282 string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04283
284 // Sanity check.
285 EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(0));
286 EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(1));
287
288 // Set migrated state for 2nd profile.
289 GetCache()->SetHasMigratedToGAIAInfoOfProfileAtIndex(1, true);
290 EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(0));
291 EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(1));
292
293 // Set migrated state for 1st profile.
294 GetCache()->SetHasMigratedToGAIAInfoOfProfileAtIndex(0, true);
295 EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(0));
296 EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(1));
297
298 // Unset migrated state for 2nd profile.
299 GetCache()->SetHasMigratedToGAIAInfoOfProfileAtIndex(1, false);
300 EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(0));
301 EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(1));
302}
303
[email protected]d4f5d1162011-11-30 01:41:52304TEST_F(ProfileInfoCacheTest, GAIAName) {
[email protected]cb114f142011-11-23 20:18:04305 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13306 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
307 string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04308 string16 profile_name(ASCIIToUTF16("profile name 2"));
309 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13310 GetProfilePath("path_2"), profile_name, string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04311
312 // Sanity check.
313 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(0).empty());
314 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(1).empty());
315 EXPECT_FALSE(GetCache()->IsUsingGAIANameOfProfileAtIndex(0));
316 EXPECT_FALSE(GetCache()->IsUsingGAIANameOfProfileAtIndex(1));
317
318 // Set GAIA name.
319 string16 gaia_name(ASCIIToUTF16("Pat Smith"));
320 GetCache()->SetGAIANameOfProfileAtIndex(1, gaia_name);
321 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(0).empty());
322 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(1));
323 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(1));
324
325 // Use GAIA name as profile name.
326 GetCache()->SetIsUsingGAIANameOfProfileAtIndex(1, true);
327
328 EXPECT_EQ(gaia_name, GetCache()->GetNameOfProfileAtIndex(1));
329 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(1));
330
331 // Don't use GAIA name as profile name.
332 GetCache()->SetIsUsingGAIANameOfProfileAtIndex(1, false);
333 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(1));
334 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(1));
335}
336
[email protected]d4f5d1162011-11-30 01:41:52337TEST_F(ProfileInfoCacheTest, GAIAPicture) {
[email protected]cb114f142011-11-23 20:18:04338 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13339 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
340 string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04341 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13342 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
343 string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04344
345 // Sanity check.
[email protected]2f3c00f2011-11-30 04:36:22346 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
347 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(1));
[email protected]cb114f142011-11-23 20:18:04348 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0));
349 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
350
351 // The profile icon should be the default one.
352 int id = ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(0);
353 const gfx::Image& profile_image(
354 ResourceBundle::GetSharedInstance().GetImageNamed(id));
[email protected]d4f5d1162011-11-30 01:41:52355 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]cb114f142011-11-23 20:18:04356 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
357
358 // Set GAIA picture.
[email protected]d4f5d1162011-11-30 01:41:52359 gfx::Image gaia_image(gfx::test::CreateImage());
[email protected]2f3c00f2011-11-30 04:36:22360 GetCache()->SetGAIAPictureOfProfileAtIndex(1, &gaia_image);
361 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
[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 // Use GAIA picture as profile picture.
368 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, true);
[email protected]d4f5d1162011-11-30 01:41:52369 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22370 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
[email protected]d4f5d1162011-11-30 01:41:52371 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]cb114f142011-11-23 20:18:04372 gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
373
374 // Don't use GAIA picture as profile picture.
375 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, false);
[email protected]d4f5d1162011-11-30 01:41:52376 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22377 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
[email protected]d4f5d1162011-11-30 01:41:52378 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]cb114f142011-11-23 20:18:04379 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
380}
381
[email protected]754bebc2011-12-01 16:42:16382TEST_F(ProfileInfoCacheTest, PersistGAIAPicture) {
[email protected]cb114f142011-11-23 20:18:04383 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13384 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
385 string16(), 0, std::string());
[email protected]d4f5d1162011-11-30 01:41:52386 gfx::Image gaia_image(gfx::test::CreateImage());
[email protected]cb114f142011-11-23 20:18:04387
[email protected]a7fe9112012-07-20 02:34:45388 content::WindowedNotificationObserver save_observer(
[email protected]cb114f142011-11-23 20:18:04389 chrome::NOTIFICATION_PROFILE_CACHE_PICTURE_SAVED,
390 content::NotificationService::AllSources());
[email protected]2f3c00f2011-11-30 04:36:22391 GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_image);
[email protected]d4f5d1162011-11-30 01:41:52392 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22393 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
[email protected]cb114f142011-11-23 20:18:04394
395 // Wait for the file to be written to disk then reset the cache.
396 save_observer.Wait();
397 ResetCache();
398
399 // Try to get the GAIA picture. This should return NULL until the read from
400 // disk is done.
[email protected]a7fe9112012-07-20 02:34:45401 content::WindowedNotificationObserver read_observer(
[email protected]cb114f142011-11-23 20:18:04402 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED,
403 content::NotificationService::AllSources());
[email protected]2f3c00f2011-11-30 04:36:22404 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
[email protected]cb114f142011-11-23 20:18:04405 read_observer.Wait();
[email protected]d4f5d1162011-11-30 01:41:52406 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22407 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
408}
409
[email protected]a6e01b42013-08-05 14:13:13410TEST_F(ProfileInfoCacheTest, SetManagedUserId) {
[email protected]0aa018a2013-07-31 15:08:54411 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13412 GetProfilePath("test"), ASCIIToUTF16("Test"),
413 string16(), 0, std::string());
[email protected]0aa018a2013-07-31 15:08:54414 EXPECT_FALSE(GetCache()->ProfileIsManagedAtIndex(0));
415
[email protected]a6e01b42013-08-05 14:13:13416 GetCache()->SetManagedUserIdOfProfileAtIndex(0, "TEST_ID");
[email protected]0aa018a2013-07-31 15:08:54417 EXPECT_TRUE(GetCache()->ProfileIsManagedAtIndex(0));
[email protected]a6e01b42013-08-05 14:13:13418 EXPECT_EQ("TEST_ID", GetCache()->GetManagedUserIdOfProfileAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54419
420 ResetCache();
421 EXPECT_TRUE(GetCache()->ProfileIsManagedAtIndex(0));
422
[email protected]a6e01b42013-08-05 14:13:13423 GetCache()->SetManagedUserIdOfProfileAtIndex(0, std::string());
[email protected]0aa018a2013-07-31 15:08:54424 EXPECT_FALSE(GetCache()->ProfileIsManagedAtIndex(0));
[email protected]a6e01b42013-08-05 14:13:13425 EXPECT_EQ("", GetCache()->GetManagedUserIdOfProfileAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54426}
427
[email protected]2f3c00f2011-11-30 04:36:22428TEST_F(ProfileInfoCacheTest, EmptyGAIAInfo) {
429 string16 profile_name = ASCIIToUTF16("name_1");
430 int id = ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(0);
431 const gfx::Image& profile_image(
432 ResourceBundle::GetSharedInstance().GetImageNamed(id));
433
434 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13435 GetProfilePath("path_1"), profile_name, string16(), 0, std::string());
[email protected]2f3c00f2011-11-30 04:36:22436
437 // Set empty GAIA info.
438 GetCache()->SetGAIANameOfProfileAtIndex(0, string16());
439 GetCache()->SetGAIAPictureOfProfileAtIndex(0, NULL);
440 GetCache()->SetIsUsingGAIANameOfProfileAtIndex(0, true);
441 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(0, true);
442
443 // Verify that the profile name and picture are not empty.
444 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(0));
445 EXPECT_TRUE(gfx::test::IsEqual(
446 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(0)));
[email protected]cb114f142011-11-23 20:18:04447}
448
[email protected]0aa018a2013-07-31 15:08:54449TEST_F(ProfileInfoCacheTest, CreateManagedTestingProfile) {
450 testing_profile_manager_.CreateTestingProfile("default");
451 string16 managed_user_name = ASCIIToUTF16("Supervised User");
452 testing_profile_manager_.CreateTestingProfile(
[email protected]a6e01b42013-08-05 14:13:13453 "test1", scoped_ptr<PrefServiceSyncable>(),
454 managed_user_name, 0, "TEST_ID");
[email protected]0aa018a2013-07-31 15:08:54455 for (size_t i = 0; i < GetCache()->GetNumberOfProfiles(); i++) {
456 bool is_managed =
457 GetCache()->GetNameOfProfileAtIndex(i) == managed_user_name;
458 EXPECT_EQ(is_managed, GetCache()->ProfileIsManagedAtIndex(i));
[email protected]a6e01b42013-08-05 14:13:13459 std::string managed_user_id = is_managed ? "TEST_ID" : "";
460 EXPECT_EQ(managed_user_id, GetCache()->GetManagedUserIdOfProfileAtIndex(i));
[email protected]0aa018a2013-07-31 15:08:54461 }
462}
463
[email protected]047481802013-09-16 22:26:38464TEST_F(ProfileInfoCacheTest, AddStubProfile) {
465 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
466
467 // Add some profiles with and without a '.' in their paths.
468 const struct {
469 const char* profile_path;
470 const char* profile_name;
471 } kTestCases[] = {
472 { "path.test0", "name_0" },
473 { "path_test1", "name_1" },
474 { "path.test2", "name_2" },
475 { "path_test3", "name_3" },
476 };
477
478 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) {
479 base::FilePath profile_path = GetProfilePath(kTestCases[i].profile_path);
480 string16 profile_name = ASCIIToUTF16(kTestCases[i].profile_name);
481
482 GetCache()->AddProfileToCache(profile_path, profile_name, string16(), i,
483 "");
484
485 EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i));
486 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
487 }
488
489 ASSERT_EQ(4U, GetCache()->GetNumberOfProfiles());
490
491 // Check that the profiles can be extracted from the local state.
492 std::vector<string16> names = ProfileInfoCache::GetProfileNames();
493 for (size_t i = 0; i < 4; i++)
494 ASSERT_FALSE(names[i].empty());
495}
496
[email protected]6730b1e2011-09-29 05:23:52497} // namespace