blob: 37cfed138e205b09c144810ca91ccea65c9d2b6d [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]85503a02014-01-16 17:27:2212#include "base/time/time.h"
[email protected]583844c2011-08-27 00:38:3513#include "chrome/browser/browser_process.h"
[email protected]25ff0862013-07-12 00:59:0314#include "chrome/browser/chrome_notification_types.h"
[email protected]0aa018a2013-07-31 15:08:5415#include "chrome/browser/prefs/pref_service_syncable.h"
[email protected]d4f5d1162011-11-30 01:41:5216#include "chrome/browser/profiles/profile_info_cache.h"
[email protected]6730b1e2011-09-29 05:23:5217#include "chrome/browser/profiles/profile_manager.h"
[email protected]583844c2011-08-27 00:38:3518#include "chrome/test/base/testing_browser_process.h"
[email protected]cb114f142011-11-23 20:18:0419#include "content/public/browser/notification_observer.h"
20#include "content/public/browser/notification_registrar.h"
[email protected]c0b030902011-12-19 16:21:0521#include "content/public/browser/notification_service.h"
[email protected]371662e372013-10-17 22:05:2222#include "content/public/test/test_browser_thread_bundle.h"
[email protected]a7fe9112012-07-20 02:34:4523#include "content/public/test/test_utils.h"
[email protected]dee810e2011-06-27 19:43:3924#include "third_party/skia/include/core/SkBitmap.h"
25#include "ui/base/resource/resource_bundle.h"
26#include "ui/gfx/image/image.h"
[email protected]d4f5d1162011-11-30 01:41:5227#include "ui/gfx/image/image_unittest_util.h"
[email protected]dee810e2011-06-27 19:43:3928
[email protected]6778fed2013-12-24 20:09:3729using base::ASCIIToUTF16;
[email protected]f729d7a2013-12-26 07:07:5630using base::UTF8ToUTF16;
[email protected]cb114f142011-11-23 20:18:0431using content::BrowserThread;
32
[email protected]6a460662011-12-22 22:05:1633ProfileNameVerifierObserver::ProfileNameVerifierObserver(
34 TestingProfileManager* testing_profile_manager)
35 : testing_profile_manager_(testing_profile_manager) {
36 DCHECK(testing_profile_manager_);
[email protected]590e189b2011-12-13 22:07:0337}
38
39ProfileNameVerifierObserver::~ProfileNameVerifierObserver() {
40}
41
42void ProfileNameVerifierObserver::OnProfileAdded(
[email protected]650b2d52013-02-10 03:41:4543 const base::FilePath& profile_path) {
[email protected]a04db822013-12-11 19:14:4044 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1645 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0346 EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end());
47 profile_names_.insert(profile_name);
48}
49
[email protected]7b0af152011-12-16 17:02:0650void ProfileNameVerifierObserver::OnProfileWillBeRemoved(
[email protected]650b2d52013-02-10 03:41:4551 const base::FilePath& profile_path) {
[email protected]a04db822013-12-11 19:14:4052 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1653 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0354 EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end());
55 profile_names_.erase(profile_name);
56}
57
[email protected]7b0af152011-12-16 17:02:0658void ProfileNameVerifierObserver::OnProfileWasRemoved(
[email protected]650b2d52013-02-10 03:41:4559 const base::FilePath& profile_path,
[email protected]a04db822013-12-11 19:14:4060 const base::string16& profile_name) {
[email protected]7b0af152011-12-16 17:02:0661 EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end());
62}
63
[email protected]590e189b2011-12-13 22:07:0364void ProfileNameVerifierObserver::OnProfileNameChanged(
[email protected]650b2d52013-02-10 03:41:4565 const base::FilePath& profile_path,
[email protected]a04db822013-12-11 19:14:4066 const base::string16& old_profile_name) {
67 base::string16 new_profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1668 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0369 EXPECT_TRUE(profile_names_.find(old_profile_name) != profile_names_.end());
70 EXPECT_TRUE(profile_names_.find(new_profile_name) == profile_names_.end());
71 profile_names_.erase(old_profile_name);
72 profile_names_.insert(new_profile_name);
73}
74
75void ProfileNameVerifierObserver::OnProfileAvatarChanged(
[email protected]650b2d52013-02-10 03:41:4576 const base::FilePath& profile_path) {
[email protected]a04db822013-12-11 19:14:4077 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
[email protected]6a460662011-12-22 22:05:1678 GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]590e189b2011-12-13 22:07:0379 EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end());
80}
81
[email protected]6a460662011-12-22 22:05:1682ProfileInfoCache* ProfileNameVerifierObserver::GetCache() {
83 return testing_profile_manager_->profile_info_cache();
84}
85
[email protected]d4f5d1162011-11-30 01:41:5286ProfileInfoCacheTest::ProfileInfoCacheTest()
[email protected]0aa018a2013-07-31 15:08:5487 : testing_profile_manager_(TestingBrowserProcess::GetGlobal()),
[email protected]6a460662011-12-22 22:05:1688 name_observer_(&testing_profile_manager_) {
[email protected]d4f5d1162011-11-30 01:41:5289}
90
91ProfileInfoCacheTest::~ProfileInfoCacheTest() {
92}
93
94void ProfileInfoCacheTest::SetUp() {
95 ASSERT_TRUE(testing_profile_manager_.SetUp());
[email protected]590e189b2011-12-13 22:07:0396 testing_profile_manager_.profile_info_cache()->AddObserver(&name_observer_);
[email protected]d4f5d1162011-11-30 01:41:5297}
98
[email protected]1302dcf2011-11-30 21:47:0599void ProfileInfoCacheTest::TearDown() {
100 // Drain the UI thread to make sure all tasks are completed. This prevents
101 // memory leaks.
[email protected]0aa018a2013-07-31 15:08:54102 base::RunLoop().RunUntilIdle();
[email protected]1302dcf2011-11-30 21:47:05103}
104
[email protected]d4f5d1162011-11-30 01:41:52105ProfileInfoCache* ProfileInfoCacheTest::GetCache() {
106 return testing_profile_manager_.profile_info_cache();
107}
108
[email protected]650b2d52013-02-10 03:41:45109base::FilePath ProfileInfoCacheTest::GetProfilePath(
[email protected]d4f5d1162011-11-30 01:41:52110 const std::string& base_name) {
111 return testing_profile_manager_.profile_manager()->user_data_dir().
112 AppendASCII(base_name);
113}
114
115void ProfileInfoCacheTest::ResetCache() {
116 testing_profile_manager_.DeleteProfileInfoCache();
117}
118
[email protected]dee810e2011-06-27 19:43:39119namespace {
120
[email protected]d4f5d1162011-11-30 01:41:52121TEST_F(ProfileInfoCacheTest, AddProfiles) {
[email protected]6730b1e2011-09-29 05:23:52122 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39123
[email protected]ca591072012-03-27 01:54:44124 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
[email protected]dee810e2011-06-27 19:43:39125 for (uint32 i = 0; i < 4; ++i) {
[email protected]7d3cbc92013-03-18 22:33:04126 base::FilePath profile_path =
127 GetProfilePath(base::StringPrintf("path_%ud", i));
[email protected]a04db822013-12-11 19:14:40128 base::string16 profile_name =
129 ASCIIToUTF16(base::StringPrintf("name_%ud", i));
[email protected]ca591072012-03-27 01:54:44130 const SkBitmap* icon = rb.GetImageNamed(
131 ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(
132 i)).ToSkBitmap();
[email protected]a6e01b42013-08-05 14:13:13133 std::string managed_user_id = i == 3 ? "TEST_ID" : "";
[email protected]dee810e2011-06-27 19:43:39134
[email protected]a04db822013-12-11 19:14:40135 GetCache()->AddProfileToCache(profile_path, profile_name, base::string16(),
136 i, managed_user_id);
[email protected]cb114f142011-11-23 20:18:04137 GetCache()->SetBackgroundStatusOfProfileAtIndex(i, true);
[email protected]a04db822013-12-11 19:14:40138 base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
[email protected]cb114f142011-11-23 20:18:04139 GetCache()->SetGAIANameOfProfileAtIndex(i, gaia_name);
[email protected]dee810e2011-06-27 19:43:39140
[email protected]6730b1e2011-09-29 05:23:52141 EXPECT_EQ(i + 1, GetCache()->GetNumberOfProfiles());
142 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
143 EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i));
[email protected]0aa018a2013-07-31 15:08:54144 const SkBitmap* actual_icon =
145 GetCache()->GetAvatarIconOfProfileAtIndex(i).ToSkBitmap();
[email protected]ca591072012-03-27 01:54:44146 EXPECT_EQ(icon->width(), actual_icon->width());
147 EXPECT_EQ(icon->height(), actual_icon->height());
[email protected]a6e01b42013-08-05 14:13:13148 EXPECT_EQ(i == 3, GetCache()->ProfileIsManagedAtIndex(i));
[email protected]5ddfade2014-02-03 10:24:53149 EXPECT_EQ(i == 3, GetCache()->IsOmittedProfileAtIndex(i));
[email protected]a6e01b42013-08-05 14:13:13150 EXPECT_EQ(managed_user_id, GetCache()->GetManagedUserIdOfProfileAtIndex(i));
[email protected]dee810e2011-06-27 19:43:39151 }
[email protected]cb114f142011-11-23 20:18:04152
153 // Reset the cache and test the it reloads correctly.
154 ResetCache();
155
156 EXPECT_EQ(4u, GetCache()->GetNumberOfProfiles());
157 for (uint32 i = 0; i < 4; ++i) {
[email protected]7d3cbc92013-03-18 22:33:04158 base::FilePath profile_path =
159 GetProfilePath(base::StringPrintf("path_%ud", i));
[email protected]cb114f142011-11-23 20:18:04160 EXPECT_EQ(i, GetCache()->GetIndexOfProfileWithPath(profile_path));
[email protected]a04db822013-12-11 19:14:40161 base::string16 profile_name =
162 ASCIIToUTF16(base::StringPrintf("name_%ud", i));
[email protected]cb114f142011-11-23 20:18:04163 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
164 EXPECT_EQ(i, GetCache()->GetAvatarIconIndexOfProfileAtIndex(i));
165 EXPECT_EQ(true, GetCache()->GetBackgroundStatusOfProfileAtIndex(i));
[email protected]a04db822013-12-11 19:14:40166 base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
[email protected]cb114f142011-11-23 20:18:04167 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(i));
168 }
[email protected]dee810e2011-06-27 19:43:39169}
170
[email protected]d4f5d1162011-11-30 01:41:52171TEST_F(ProfileInfoCacheTest, DeleteProfile) {
[email protected]6730b1e2011-09-29 05:23:52172 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39173
[email protected]650b2d52013-02-10 03:41:45174 base::FilePath path_1 = GetProfilePath("path_1");
[email protected]a04db822013-12-11 19:14:40175 GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("name_1"),
176 base::string16(), 0, std::string());
[email protected]6730b1e2011-09-29 05:23:52177 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39178
[email protected]650b2d52013-02-10 03:41:45179 base::FilePath path_2 = GetProfilePath("path_2");
[email protected]a04db822013-12-11 19:14:40180 base::string16 name_2 = ASCIIToUTF16("name_2");
181 GetCache()->AddProfileToCache(path_2, name_2, base::string16(), 0,
182 std::string());
[email protected]6730b1e2011-09-29 05:23:52183 EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39184
[email protected]6730b1e2011-09-29 05:23:52185 GetCache()->DeleteProfileFromCache(path_1);
186 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
187 EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(0));
[email protected]dee810e2011-06-27 19:43:39188
[email protected]6730b1e2011-09-29 05:23:52189 GetCache()->DeleteProfileFromCache(path_2);
190 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
[email protected]dee810e2011-06-27 19:43:39191}
192
[email protected]d4f5d1162011-11-30 01:41:52193TEST_F(ProfileInfoCacheTest, MutateProfile) {
194 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13195 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
[email protected]a04db822013-12-11 19:14:40196 base::string16(), 0, std::string());
[email protected]d4f5d1162011-11-30 01:41:52197 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13198 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
[email protected]a04db822013-12-11 19:14:40199 base::string16(), 0, std::string());
[email protected]dee810e2011-06-27 19:43:39200
[email protected]a04db822013-12-11 19:14:40201 base::string16 new_name = ASCIIToUTF16("new_name");
[email protected]6730b1e2011-09-29 05:23:52202 GetCache()->SetNameOfProfileAtIndex(1, new_name);
203 EXPECT_EQ(new_name, GetCache()->GetNameOfProfileAtIndex(1));
204 EXPECT_NE(new_name, GetCache()->GetNameOfProfileAtIndex(0));
[email protected]dee810e2011-06-27 19:43:39205
[email protected]a04db822013-12-11 19:14:40206 base::string16 new_user_name = ASCIIToUTF16("user_name");
[email protected]e8e78092011-09-29 18:15:38207 GetCache()->SetUserNameOfProfileAtIndex(1, new_user_name);
208 EXPECT_EQ(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(1));
209 EXPECT_NE(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(0));
210
[email protected]dee810e2011-06-27 19:43:39211 size_t new_icon_index = 3;
[email protected]6730b1e2011-09-29 05:23:52212 GetCache()->SetAvatarIconOfProfileAtIndex(1, new_icon_index);
[email protected]dee810e2011-06-27 19:43:39213 // Not much to test.
[email protected]6730b1e2011-09-29 05:23:52214 GetCache()->GetAvatarIconOfProfileAtIndex(1);
[email protected]dee810e2011-06-27 19:43:39215}
216
[email protected]d4f5d1162011-11-30 01:41:52217TEST_F(ProfileInfoCacheTest, Sort) {
[email protected]a04db822013-12-11 19:14:40218 base::string16 name_a = ASCIIToUTF16("apple");
[email protected]d4f5d1162011-11-30 01:41:52219 GetCache()->AddProfileToCache(
[email protected]a04db822013-12-11 19:14:40220 GetProfilePath("path_a"), name_a, base::string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04221
[email protected]a04db822013-12-11 19:14:40222 base::string16 name_c = ASCIIToUTF16("cat");
[email protected]d4f5d1162011-11-30 01:41:52223 GetCache()->AddProfileToCache(
[email protected]a04db822013-12-11 19:14:40224 GetProfilePath("path_c"), name_c, base::string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04225
226 // Sanity check the initial order.
227 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
228 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
229
230 // Add a new profile (start with a capital to test case insensitive sorting.
[email protected]a04db822013-12-11 19:14:40231 base::string16 name_b = ASCIIToUTF16("Banana");
[email protected]d4f5d1162011-11-30 01:41:52232 GetCache()->AddProfileToCache(
[email protected]a04db822013-12-11 19:14:40233 GetProfilePath("path_b"), name_b, base::string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04234
235 // Verify the new order.
236 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
237 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(1));
238 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(2));
239
240 // Change the name of an existing profile.
241 name_a = UTF8ToUTF16("dog");
242 GetCache()->SetNameOfProfileAtIndex(0, name_a);
243
244 // Verify the new order.
245 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
246 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
247 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(2));
248
249 // Delete a profile.
[email protected]d4f5d1162011-11-30 01:41:52250 GetCache()->DeleteProfileFromCache(GetProfilePath("path_c"));
[email protected]cb114f142011-11-23 20:18:04251
252 // Verify the new order.
253 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
254 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(1));
255}
256
[email protected]d4f5d1162011-11-30 01:41:52257TEST_F(ProfileInfoCacheTest, BackgroundModeStatus) {
[email protected]279170832011-10-12 23:38:03258 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13259 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
[email protected]a04db822013-12-11 19:14:40260 base::string16(), 0, std::string());
[email protected]279170832011-10-12 23:38:03261 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13262 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
[email protected]a04db822013-12-11 19:14:40263 base::string16(), 0, std::string());
[email protected]279170832011-10-12 23:38:03264
265 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
266 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
267
268 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, true);
269
270 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
271 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
272
273 GetCache()->SetBackgroundStatusOfProfileAtIndex(0, true);
274
275 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
276 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
277
278 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, false);
279
280 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
281 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
282}
283
[email protected]d4f5d1162011-11-30 01:41:52284TEST_F(ProfileInfoCacheTest, HasMigrated) {
[email protected]cb114f142011-11-23 20:18:04285 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13286 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
[email protected]a04db822013-12-11 19:14:40287 base::string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04288 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13289 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
[email protected]a04db822013-12-11 19:14:40290 base::string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04291
292 // Sanity check.
293 EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(0));
294 EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(1));
295
296 // Set migrated state for 2nd profile.
297 GetCache()->SetHasMigratedToGAIAInfoOfProfileAtIndex(1, true);
298 EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(0));
299 EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(1));
300
301 // Set migrated state for 1st profile.
302 GetCache()->SetHasMigratedToGAIAInfoOfProfileAtIndex(0, true);
303 EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(0));
304 EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(1));
305
306 // Unset migrated state for 2nd profile.
307 GetCache()->SetHasMigratedToGAIAInfoOfProfileAtIndex(1, false);
308 EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(0));
309 EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(1));
310}
311
[email protected]85503a02014-01-16 17:27:22312TEST_F(ProfileInfoCacheTest, ProfileActiveTime) {
313 GetCache()->AddProfileToCache(
314 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
315 base::string16(), 0, std::string());
316 EXPECT_EQ(base::Time(), GetCache()->GetProfileActiveTimeAtIndex(0));
[email protected]87727e02014-02-27 18:16:53317 // Before & After times are artificially shifted because just relying upon
318 // the system time can yield problems due to inaccuracies in the
319 // underlying storage system (which uses a double with only 52 bits of
320 // precision to store the 64-bit "time" number). http://crbug.com/346827
[email protected]85503a02014-01-16 17:27:22321 base::Time before = base::Time::Now();
[email protected]87727e02014-02-27 18:16:53322 before -= base::TimeDelta::FromSeconds(1);
[email protected]85503a02014-01-16 17:27:22323 GetCache()->SetProfileActiveTimeAtIndex(0);
324 base::Time after = base::Time::Now();
[email protected]87727e02014-02-27 18:16:53325 after += base::TimeDelta::FromSeconds(1);
[email protected]85503a02014-01-16 17:27:22326 EXPECT_LE(before, GetCache()->GetProfileActiveTimeAtIndex(0));
327 EXPECT_GE(after, GetCache()->GetProfileActiveTimeAtIndex(0));
328}
329
[email protected]d4f5d1162011-11-30 01:41:52330TEST_F(ProfileInfoCacheTest, GAIAName) {
[email protected]cb114f142011-11-23 20:18:04331 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13332 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
[email protected]a04db822013-12-11 19:14:40333 base::string16(), 0, std::string());
334 base::string16 profile_name(ASCIIToUTF16("profile name 2"));
[email protected]cb114f142011-11-23 20:18:04335 GetCache()->AddProfileToCache(
[email protected]a04db822013-12-11 19:14:40336 GetProfilePath("path_2"), profile_name, base::string16(), 0,
337 std::string());
[email protected]cb114f142011-11-23 20:18:04338
339 // Sanity check.
340 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(0).empty());
341 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(1).empty());
342 EXPECT_FALSE(GetCache()->IsUsingGAIANameOfProfileAtIndex(0));
343 EXPECT_FALSE(GetCache()->IsUsingGAIANameOfProfileAtIndex(1));
344
345 // Set GAIA name.
[email protected]a04db822013-12-11 19:14:40346 base::string16 gaia_name(ASCIIToUTF16("Pat Smith"));
[email protected]cb114f142011-11-23 20:18:04347 GetCache()->SetGAIANameOfProfileAtIndex(1, gaia_name);
348 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(0).empty());
349 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(1));
350 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(1));
351
352 // Use GAIA name as profile name.
353 GetCache()->SetIsUsingGAIANameOfProfileAtIndex(1, true);
354
355 EXPECT_EQ(gaia_name, GetCache()->GetNameOfProfileAtIndex(1));
356 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(1));
357
358 // Don't use GAIA name as profile name.
359 GetCache()->SetIsUsingGAIANameOfProfileAtIndex(1, false);
360 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(1));
361 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(1));
362}
363
[email protected]d4f5d1162011-11-30 01:41:52364TEST_F(ProfileInfoCacheTest, GAIAPicture) {
[email protected]cb114f142011-11-23 20:18:04365 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13366 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
[email protected]a04db822013-12-11 19:14:40367 base::string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04368 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13369 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
[email protected]a04db822013-12-11 19:14:40370 base::string16(), 0, std::string());
[email protected]cb114f142011-11-23 20:18:04371
372 // Sanity check.
[email protected]2f3c00f2011-11-30 04:36:22373 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
374 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(1));
[email protected]cb114f142011-11-23 20:18:04375 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0));
376 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
377
378 // The profile icon should be the default one.
379 int id = ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(0);
380 const gfx::Image& profile_image(
381 ResourceBundle::GetSharedInstance().GetImageNamed(id));
[email protected]d4f5d1162011-11-30 01:41:52382 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]cb114f142011-11-23 20:18:04383 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
384
385 // Set GAIA picture.
[email protected]d4f5d1162011-11-30 01:41:52386 gfx::Image gaia_image(gfx::test::CreateImage());
[email protected]2f3c00f2011-11-30 04:36:22387 GetCache()->SetGAIAPictureOfProfileAtIndex(1, &gaia_image);
388 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
[email protected]d4f5d1162011-11-30 01:41:52389 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22390 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
[email protected]d4f5d1162011-11-30 01:41:52391 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]cb114f142011-11-23 20:18:04392 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
393
394 // Use GAIA picture as profile picture.
395 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, true);
[email protected]d4f5d1162011-11-30 01:41:52396 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22397 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
[email protected]d4f5d1162011-11-30 01:41:52398 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]cb114f142011-11-23 20:18:04399 gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
400
401 // Don't use GAIA picture as profile picture.
402 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, false);
[email protected]d4f5d1162011-11-30 01:41:52403 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22404 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
[email protected]d4f5d1162011-11-30 01:41:52405 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]cb114f142011-11-23 20:18:04406 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
407}
408
[email protected]754bebc2011-12-01 16:42:16409TEST_F(ProfileInfoCacheTest, PersistGAIAPicture) {
[email protected]cb114f142011-11-23 20:18:04410 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13411 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
[email protected]a04db822013-12-11 19:14:40412 base::string16(), 0, std::string());
[email protected]d4f5d1162011-11-30 01:41:52413 gfx::Image gaia_image(gfx::test::CreateImage());
[email protected]cb114f142011-11-23 20:18:04414
[email protected]a7fe9112012-07-20 02:34:45415 content::WindowedNotificationObserver save_observer(
[email protected]cb114f142011-11-23 20:18:04416 chrome::NOTIFICATION_PROFILE_CACHE_PICTURE_SAVED,
417 content::NotificationService::AllSources());
[email protected]2f3c00f2011-11-30 04:36:22418 GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_image);
[email protected]d4f5d1162011-11-30 01:41:52419 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22420 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
[email protected]cb114f142011-11-23 20:18:04421
422 // Wait for the file to be written to disk then reset the cache.
423 save_observer.Wait();
424 ResetCache();
425
426 // Try to get the GAIA picture. This should return NULL until the read from
427 // disk is done.
[email protected]a7fe9112012-07-20 02:34:45428 content::WindowedNotificationObserver read_observer(
[email protected]cb114f142011-11-23 20:18:04429 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED,
430 content::NotificationService::AllSources());
[email protected]2f3c00f2011-11-30 04:36:22431 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
[email protected]cb114f142011-11-23 20:18:04432 read_observer.Wait();
[email protected]d4f5d1162011-11-30 01:41:52433 EXPECT_TRUE(gfx::test::IsEqual(
[email protected]2f3c00f2011-11-30 04:36:22434 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
435}
436
[email protected]a6e01b42013-08-05 14:13:13437TEST_F(ProfileInfoCacheTest, SetManagedUserId) {
[email protected]0aa018a2013-07-31 15:08:54438 GetCache()->AddProfileToCache(
[email protected]a6e01b42013-08-05 14:13:13439 GetProfilePath("test"), ASCIIToUTF16("Test"),
[email protected]a04db822013-12-11 19:14:40440 base::string16(), 0, std::string());
[email protected]0aa018a2013-07-31 15:08:54441 EXPECT_FALSE(GetCache()->ProfileIsManagedAtIndex(0));
442
[email protected]a6e01b42013-08-05 14:13:13443 GetCache()->SetManagedUserIdOfProfileAtIndex(0, "TEST_ID");
[email protected]0aa018a2013-07-31 15:08:54444 EXPECT_TRUE(GetCache()->ProfileIsManagedAtIndex(0));
[email protected]a6e01b42013-08-05 14:13:13445 EXPECT_EQ("TEST_ID", GetCache()->GetManagedUserIdOfProfileAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54446
447 ResetCache();
448 EXPECT_TRUE(GetCache()->ProfileIsManagedAtIndex(0));
449
[email protected]a6e01b42013-08-05 14:13:13450 GetCache()->SetManagedUserIdOfProfileAtIndex(0, std::string());
[email protected]0aa018a2013-07-31 15:08:54451 EXPECT_FALSE(GetCache()->ProfileIsManagedAtIndex(0));
[email protected]a6e01b42013-08-05 14:13:13452 EXPECT_EQ("", GetCache()->GetManagedUserIdOfProfileAtIndex(0));
[email protected]0aa018a2013-07-31 15:08:54453}
454
[email protected]2f3c00f2011-11-30 04:36:22455TEST_F(ProfileInfoCacheTest, EmptyGAIAInfo) {
[email protected]a04db822013-12-11 19:14:40456 base::string16 profile_name = ASCIIToUTF16("name_1");
[email protected]2f3c00f2011-11-30 04:36:22457 int id = ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(0);
458 const gfx::Image& profile_image(
459 ResourceBundle::GetSharedInstance().GetImageNamed(id));
460
461 GetCache()->AddProfileToCache(
[email protected]a04db822013-12-11 19:14:40462 GetProfilePath("path_1"), profile_name, base::string16(), 0,
463 std::string());
[email protected]2f3c00f2011-11-30 04:36:22464
465 // Set empty GAIA info.
[email protected]a04db822013-12-11 19:14:40466 GetCache()->SetGAIANameOfProfileAtIndex(0, base::string16());
[email protected]2f3c00f2011-11-30 04:36:22467 GetCache()->SetGAIAPictureOfProfileAtIndex(0, NULL);
468 GetCache()->SetIsUsingGAIANameOfProfileAtIndex(0, true);
469 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(0, true);
470
471 // Verify that the profile name and picture are not empty.
472 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(0));
473 EXPECT_TRUE(gfx::test::IsEqual(
474 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(0)));
[email protected]cb114f142011-11-23 20:18:04475}
476
[email protected]0aa018a2013-07-31 15:08:54477TEST_F(ProfileInfoCacheTest, CreateManagedTestingProfile) {
478 testing_profile_manager_.CreateTestingProfile("default");
[email protected]a04db822013-12-11 19:14:40479 base::string16 managed_user_name = ASCIIToUTF16("Supervised User");
[email protected]0aa018a2013-07-31 15:08:54480 testing_profile_manager_.CreateTestingProfile(
[email protected]a6e01b42013-08-05 14:13:13481 "test1", scoped_ptr<PrefServiceSyncable>(),
[email protected]cb0323d2013-11-22 23:21:26482 managed_user_name, 0, "TEST_ID", TestingProfile::TestingFactories());
[email protected]0aa018a2013-07-31 15:08:54483 for (size_t i = 0; i < GetCache()->GetNumberOfProfiles(); i++) {
484 bool is_managed =
485 GetCache()->GetNameOfProfileAtIndex(i) == managed_user_name;
486 EXPECT_EQ(is_managed, GetCache()->ProfileIsManagedAtIndex(i));
[email protected]a6e01b42013-08-05 14:13:13487 std::string managed_user_id = is_managed ? "TEST_ID" : "";
488 EXPECT_EQ(managed_user_id, GetCache()->GetManagedUserIdOfProfileAtIndex(i));
[email protected]0aa018a2013-07-31 15:08:54489 }
[email protected]371662e372013-10-17 22:05:22490
491 // Managed profiles have a custom theme, which needs to be deleted on the FILE
492 // thread. Reset the profile manager now so everything is deleted while we
493 // still have a FILE thread.
494 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
[email protected]0aa018a2013-07-31 15:08:54495}
496
[email protected]047481802013-09-16 22:26:38497TEST_F(ProfileInfoCacheTest, AddStubProfile) {
498 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
499
500 // Add some profiles with and without a '.' in their paths.
501 const struct {
502 const char* profile_path;
503 const char* profile_name;
504 } kTestCases[] = {
505 { "path.test0", "name_0" },
506 { "path_test1", "name_1" },
507 { "path.test2", "name_2" },
508 { "path_test3", "name_3" },
509 };
510
511 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) {
512 base::FilePath profile_path = GetProfilePath(kTestCases[i].profile_path);
[email protected]a04db822013-12-11 19:14:40513 base::string16 profile_name = ASCIIToUTF16(kTestCases[i].profile_name);
[email protected]047481802013-09-16 22:26:38514
[email protected]a04db822013-12-11 19:14:40515 GetCache()->AddProfileToCache(profile_path, profile_name, base::string16(),
516 i, "");
[email protected]047481802013-09-16 22:26:38517
518 EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i));
519 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
520 }
521
522 ASSERT_EQ(4U, GetCache()->GetNumberOfProfiles());
523
524 // Check that the profiles can be extracted from the local state.
[email protected]d2065e062013-12-12 23:49:52525 std::vector<base::string16> names = ProfileInfoCache::GetProfileNames();
[email protected]047481802013-09-16 22:26:38526 for (size_t i = 0; i < 4; i++)
527 ASSERT_FALSE(names[i].empty());
528}
529
[email protected]6730b1e2011-09-29 05:23:52530} // namespace