[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 5 | #include "chrome/browser/profiles/profile_info_cache_unittest.h" |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 6 | |
| 7 | #include "base/stringprintf.h" |
| 8 | #include "base/utf_string_conversions.h" |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 9 | #include "chrome/browser/browser_process.h" |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 10 | #include "chrome/browser/profiles/profile_info_cache.h" |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 11 | #include "chrome/browser/profiles/profile_manager.h" |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 12 | #include "chrome/common/chrome_notification_types.h" |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 13 | #include "chrome/test/base/testing_browser_process.h" |
[email protected] | 8ad3636e | 2011-08-01 22:31:40 | [diff] [blame] | 14 | #include "chrome/test/base/testing_pref_service.h" |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 15 | #include "chrome/test/base/ui_test_utils.h" |
| 16 | #include "content/public/browser/notification_observer.h" |
| 17 | #include "content/public/browser/notification_registrar.h" |
[email protected] | c0b03090 | 2011-12-19 16:21:05 | [diff] [blame] | 18 | #include "content/public/browser/notification_service.h" |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 19 | #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] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 22 | #include "ui/gfx/image/image_unittest_util.h" |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 23 | |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 24 | using content::BrowserThread; |
| 25 | |
[email protected] | 6a46066 | 2011-12-22 22:05:16 | [diff] [blame^] | 26 | ProfileNameVerifierObserver::ProfileNameVerifierObserver( |
| 27 | TestingProfileManager* testing_profile_manager) |
| 28 | : testing_profile_manager_(testing_profile_manager) { |
| 29 | DCHECK(testing_profile_manager_); |
[email protected] | 590e189b | 2011-12-13 22:07:03 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | ProfileNameVerifierObserver::~ProfileNameVerifierObserver() { |
| 33 | } |
| 34 | |
| 35 | void ProfileNameVerifierObserver::OnProfileAdded( |
[email protected] | 6a46066 | 2011-12-22 22:05:16 | [diff] [blame^] | 36 | const FilePath& profile_path) { |
| 37 | string16 profile_name = GetCache()->GetNameOfProfileAtIndex( |
| 38 | GetCache()->GetIndexOfProfileWithPath(profile_path)); |
[email protected] | 590e189b | 2011-12-13 22:07:03 | [diff] [blame] | 39 | EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end()); |
| 40 | profile_names_.insert(profile_name); |
| 41 | } |
| 42 | |
[email protected] | 7b0af15 | 2011-12-16 17:02:06 | [diff] [blame] | 43 | void ProfileNameVerifierObserver::OnProfileWillBeRemoved( |
[email protected] | 6a46066 | 2011-12-22 22:05:16 | [diff] [blame^] | 44 | const FilePath& profile_path) { |
| 45 | string16 profile_name = GetCache()->GetNameOfProfileAtIndex( |
| 46 | GetCache()->GetIndexOfProfileWithPath(profile_path)); |
[email protected] | 590e189b | 2011-12-13 22:07:03 | [diff] [blame] | 47 | EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end()); |
| 48 | profile_names_.erase(profile_name); |
| 49 | } |
| 50 | |
[email protected] | 7b0af15 | 2011-12-16 17:02:06 | [diff] [blame] | 51 | void ProfileNameVerifierObserver::OnProfileWasRemoved( |
[email protected] | 6a46066 | 2011-12-22 22:05:16 | [diff] [blame^] | 52 | const FilePath& profile_path, |
[email protected] | 7b0af15 | 2011-12-16 17:02:06 | [diff] [blame] | 53 | const string16& profile_name) { |
| 54 | EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end()); |
| 55 | } |
| 56 | |
[email protected] | 590e189b | 2011-12-13 22:07:03 | [diff] [blame] | 57 | void ProfileNameVerifierObserver::OnProfileNameChanged( |
[email protected] | 6a46066 | 2011-12-22 22:05:16 | [diff] [blame^] | 58 | const FilePath& profile_path, |
| 59 | const string16& old_profile_name) { |
| 60 | string16 new_profile_name = GetCache()->GetNameOfProfileAtIndex( |
| 61 | GetCache()->GetIndexOfProfileWithPath(profile_path)); |
[email protected] | 590e189b | 2011-12-13 22:07:03 | [diff] [blame] | 62 | 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 | |
| 68 | void ProfileNameVerifierObserver::OnProfileAvatarChanged( |
[email protected] | 6a46066 | 2011-12-22 22:05:16 | [diff] [blame^] | 69 | const FilePath& profile_path) { |
| 70 | string16 profile_name = GetCache()->GetNameOfProfileAtIndex( |
| 71 | GetCache()->GetIndexOfProfileWithPath(profile_path)); |
[email protected] | 590e189b | 2011-12-13 22:07:03 | [diff] [blame] | 72 | EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end()); |
| 73 | } |
| 74 | |
[email protected] | 6a46066 | 2011-12-22 22:05:16 | [diff] [blame^] | 75 | ProfileInfoCache* ProfileNameVerifierObserver::GetCache() { |
| 76 | return testing_profile_manager_->profile_info_cache(); |
| 77 | } |
| 78 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 79 | ProfileInfoCacheTest::ProfileInfoCacheTest() |
| 80 | : testing_profile_manager_( |
| 81 | static_cast<TestingBrowserProcess*>(g_browser_process)), |
| 82 | ui_thread_(BrowserThread::UI, &ui_loop_), |
[email protected] | 6a46066 | 2011-12-22 22:05:16 | [diff] [blame^] | 83 | file_thread_(BrowserThread::FILE, &ui_loop_), |
| 84 | name_observer_(&testing_profile_manager_) { |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | ProfileInfoCacheTest::~ProfileInfoCacheTest() { |
| 88 | } |
| 89 | |
| 90 | void ProfileInfoCacheTest::SetUp() { |
| 91 | ASSERT_TRUE(testing_profile_manager_.SetUp()); |
[email protected] | 590e189b | 2011-12-13 22:07:03 | [diff] [blame] | 92 | testing_profile_manager_.profile_info_cache()->AddObserver(&name_observer_); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 93 | } |
| 94 | |
[email protected] | 1302dcf | 2011-11-30 21:47:05 | [diff] [blame] | 95 | void ProfileInfoCacheTest::TearDown() { |
| 96 | // Drain the UI thread to make sure all tasks are completed. This prevents |
| 97 | // memory leaks. |
| 98 | ui_loop_.RunAllPending(); |
| 99 | } |
| 100 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 101 | ProfileInfoCache* ProfileInfoCacheTest::GetCache() { |
| 102 | return testing_profile_manager_.profile_info_cache(); |
| 103 | } |
| 104 | |
| 105 | FilePath ProfileInfoCacheTest::GetProfilePath( |
| 106 | const std::string& base_name) { |
| 107 | return testing_profile_manager_.profile_manager()->user_data_dir(). |
| 108 | AppendASCII(base_name); |
| 109 | } |
| 110 | |
| 111 | void ProfileInfoCacheTest::ResetCache() { |
| 112 | testing_profile_manager_.DeleteProfileInfoCache(); |
| 113 | } |
| 114 | |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 115 | namespace { |
| 116 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 117 | TEST_F(ProfileInfoCacheTest, AddProfiles) { |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 118 | EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles()); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 119 | |
| 120 | for (uint32 i = 0; i < 4; ++i) { |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 121 | FilePath profile_path = GetProfilePath(StringPrintf("path_%ud", i)); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 122 | string16 profile_name = ASCIIToUTF16(StringPrintf("name_%ud", i)); |
| 123 | const SkBitmap& icon = ResourceBundle::GetSharedInstance().GetImageNamed( |
| 124 | ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(i)); |
| 125 | |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 126 | GetCache()->AddProfileToCache(profile_path, profile_name, string16(), i); |
| 127 | GetCache()->SetBackgroundStatusOfProfileAtIndex(i, true); |
| 128 | string16 gaia_name = ASCIIToUTF16(StringPrintf("gaia_%ud", i)); |
| 129 | GetCache()->SetGAIANameOfProfileAtIndex(i, gaia_name); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 130 | |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 131 | EXPECT_EQ(i + 1, GetCache()->GetNumberOfProfiles()); |
| 132 | EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i)); |
| 133 | EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i)); |
| 134 | const SkBitmap& actual_icon = GetCache()->GetAvatarIconOfProfileAtIndex(i); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 135 | EXPECT_EQ(icon.width(), actual_icon.width()); |
| 136 | EXPECT_EQ(icon.height(), actual_icon.height()); |
| 137 | } |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 138 | |
| 139 | // Reset the cache and test the it reloads correctly. |
| 140 | ResetCache(); |
| 141 | |
| 142 | EXPECT_EQ(4u, GetCache()->GetNumberOfProfiles()); |
| 143 | for (uint32 i = 0; i < 4; ++i) { |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 144 | FilePath profile_path = GetProfilePath(StringPrintf("path_%ud", i)); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 145 | EXPECT_EQ(i, GetCache()->GetIndexOfProfileWithPath(profile_path)); |
| 146 | string16 profile_name = ASCIIToUTF16(StringPrintf("name_%ud", i)); |
| 147 | EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i)); |
| 148 | EXPECT_EQ(i, GetCache()->GetAvatarIconIndexOfProfileAtIndex(i)); |
| 149 | EXPECT_EQ(true, GetCache()->GetBackgroundStatusOfProfileAtIndex(i)); |
| 150 | string16 gaia_name = ASCIIToUTF16(StringPrintf("gaia_%ud", i)); |
| 151 | EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(i)); |
| 152 | } |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 153 | } |
| 154 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 155 | TEST_F(ProfileInfoCacheTest, DeleteProfile) { |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 156 | EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles()); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 157 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 158 | FilePath path_1 = GetProfilePath("path_1"); |
[email protected] | 73cb372 | 2011-10-11 18:12:13 | [diff] [blame] | 159 | GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("name_1"), string16(), |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 160 | 0); |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 161 | EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles()); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 162 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 163 | FilePath path_2 = GetProfilePath("path_2"); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 164 | string16 name_2 = ASCIIToUTF16("name_2"); |
[email protected] | 73cb372 | 2011-10-11 18:12:13 | [diff] [blame] | 165 | GetCache()->AddProfileToCache(path_2, name_2, string16(), 0); |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 166 | EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles()); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 167 | |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 168 | GetCache()->DeleteProfileFromCache(path_1); |
| 169 | EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles()); |
| 170 | EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(0)); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 171 | |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 172 | GetCache()->DeleteProfileFromCache(path_2); |
| 173 | EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles()); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 174 | } |
| 175 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 176 | TEST_F(ProfileInfoCacheTest, MutateProfile) { |
| 177 | GetCache()->AddProfileToCache( |
| 178 | GetProfilePath("path_1"), ASCIIToUTF16("name_1"), string16(), 0); |
| 179 | GetCache()->AddProfileToCache( |
| 180 | GetProfilePath("path_2"), ASCIIToUTF16("name_2"), string16(), 0); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 181 | |
| 182 | string16 new_name = ASCIIToUTF16("new_name"); |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 183 | GetCache()->SetNameOfProfileAtIndex(1, new_name); |
| 184 | EXPECT_EQ(new_name, GetCache()->GetNameOfProfileAtIndex(1)); |
| 185 | EXPECT_NE(new_name, GetCache()->GetNameOfProfileAtIndex(0)); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 186 | |
[email protected] | e8e7809 | 2011-09-29 18:15:38 | [diff] [blame] | 187 | string16 new_user_name = ASCIIToUTF16("user_name"); |
| 188 | GetCache()->SetUserNameOfProfileAtIndex(1, new_user_name); |
| 189 | EXPECT_EQ(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(1)); |
| 190 | EXPECT_NE(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(0)); |
| 191 | |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 192 | size_t new_icon_index = 3; |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 193 | GetCache()->SetAvatarIconOfProfileAtIndex(1, new_icon_index); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 194 | // Not much to test. |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 195 | GetCache()->GetAvatarIconOfProfileAtIndex(1); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 196 | } |
| 197 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 198 | TEST_F(ProfileInfoCacheTest, Sort) { |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 199 | string16 name_a = ASCIIToUTF16("apple"); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 200 | GetCache()->AddProfileToCache( |
| 201 | GetProfilePath("path_a"), name_a, string16(), 0); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 202 | |
| 203 | string16 name_c = ASCIIToUTF16("cat"); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 204 | GetCache()->AddProfileToCache( |
| 205 | GetProfilePath("path_c"), name_c, string16(), 0); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 206 | |
| 207 | // Sanity check the initial order. |
| 208 | EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0)); |
| 209 | EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1)); |
| 210 | |
| 211 | // Add a new profile (start with a capital to test case insensitive sorting. |
| 212 | string16 name_b = ASCIIToUTF16("Banana"); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 213 | GetCache()->AddProfileToCache( |
| 214 | GetProfilePath("path_b"), name_b, string16(), 0); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 215 | |
| 216 | // Verify the new order. |
| 217 | EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0)); |
| 218 | EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(1)); |
| 219 | EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(2)); |
| 220 | |
| 221 | // Change the name of an existing profile. |
| 222 | name_a = UTF8ToUTF16("dog"); |
| 223 | GetCache()->SetNameOfProfileAtIndex(0, name_a); |
| 224 | |
| 225 | // Verify the new order. |
| 226 | EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0)); |
| 227 | EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1)); |
| 228 | EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(2)); |
| 229 | |
| 230 | // Delete a profile. |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 231 | GetCache()->DeleteProfileFromCache(GetProfilePath("path_c")); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 232 | |
| 233 | // Verify the new order. |
| 234 | EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0)); |
| 235 | EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(1)); |
| 236 | } |
| 237 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 238 | TEST_F(ProfileInfoCacheTest, BackgroundModeStatus) { |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 239 | GetCache()->AddProfileToCache( |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 240 | GetProfilePath("path_1"), ASCIIToUTF16("name_1"), string16(), 0); |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 241 | GetCache()->AddProfileToCache( |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 242 | GetProfilePath("path_2"), ASCIIToUTF16("name_2"), string16(), 0); |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 243 | |
| 244 | EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0)); |
| 245 | EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1)); |
| 246 | |
| 247 | GetCache()->SetBackgroundStatusOfProfileAtIndex(1, true); |
| 248 | |
| 249 | EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0)); |
| 250 | EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1)); |
| 251 | |
| 252 | GetCache()->SetBackgroundStatusOfProfileAtIndex(0, true); |
| 253 | |
| 254 | EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0)); |
| 255 | EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1)); |
| 256 | |
| 257 | GetCache()->SetBackgroundStatusOfProfileAtIndex(1, false); |
| 258 | |
| 259 | EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0)); |
| 260 | EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1)); |
| 261 | } |
| 262 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 263 | TEST_F(ProfileInfoCacheTest, HasMigrated) { |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 264 | GetCache()->AddProfileToCache( |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 265 | GetProfilePath("path_1"), ASCIIToUTF16("name_1"), string16(), 0); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 266 | GetCache()->AddProfileToCache( |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 267 | GetProfilePath("path_2"), ASCIIToUTF16("name_2"), string16(), 0); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 268 | |
| 269 | // Sanity check. |
| 270 | EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(0)); |
| 271 | EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(1)); |
| 272 | |
| 273 | // Set migrated state for 2nd profile. |
| 274 | GetCache()->SetHasMigratedToGAIAInfoOfProfileAtIndex(1, true); |
| 275 | EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(0)); |
| 276 | EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(1)); |
| 277 | |
| 278 | // Set migrated state for 1st profile. |
| 279 | GetCache()->SetHasMigratedToGAIAInfoOfProfileAtIndex(0, true); |
| 280 | EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(0)); |
| 281 | EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(1)); |
| 282 | |
| 283 | // Unset migrated state for 2nd profile. |
| 284 | GetCache()->SetHasMigratedToGAIAInfoOfProfileAtIndex(1, false); |
| 285 | EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(0)); |
| 286 | EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(1)); |
| 287 | } |
| 288 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 289 | TEST_F(ProfileInfoCacheTest, GAIAName) { |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 290 | GetCache()->AddProfileToCache( |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 291 | GetProfilePath("path_1"), ASCIIToUTF16("name_1"), string16(), 0); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 292 | string16 profile_name(ASCIIToUTF16("profile name 2")); |
| 293 | GetCache()->AddProfileToCache( |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 294 | GetProfilePath("path_2"), profile_name, string16(), 0); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 295 | |
| 296 | // Sanity check. |
| 297 | EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(0).empty()); |
| 298 | EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(1).empty()); |
| 299 | EXPECT_FALSE(GetCache()->IsUsingGAIANameOfProfileAtIndex(0)); |
| 300 | EXPECT_FALSE(GetCache()->IsUsingGAIANameOfProfileAtIndex(1)); |
| 301 | |
| 302 | // Set GAIA name. |
| 303 | string16 gaia_name(ASCIIToUTF16("Pat Smith")); |
| 304 | GetCache()->SetGAIANameOfProfileAtIndex(1, gaia_name); |
| 305 | EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(0).empty()); |
| 306 | EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(1)); |
| 307 | EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(1)); |
| 308 | |
| 309 | // Use GAIA name as profile name. |
| 310 | GetCache()->SetIsUsingGAIANameOfProfileAtIndex(1, true); |
| 311 | |
| 312 | EXPECT_EQ(gaia_name, GetCache()->GetNameOfProfileAtIndex(1)); |
| 313 | EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(1)); |
| 314 | |
| 315 | // Don't use GAIA name as profile name. |
| 316 | GetCache()->SetIsUsingGAIANameOfProfileAtIndex(1, false); |
| 317 | EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(1)); |
| 318 | EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(1)); |
| 319 | } |
| 320 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 321 | TEST_F(ProfileInfoCacheTest, GAIAPicture) { |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 322 | GetCache()->AddProfileToCache( |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 323 | GetProfilePath("path_1"), ASCIIToUTF16("name_1"), string16(), 0); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 324 | GetCache()->AddProfileToCache( |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 325 | GetProfilePath("path_2"), ASCIIToUTF16("name_2"), string16(), 0); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 326 | |
| 327 | // Sanity check. |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 328 | EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0)); |
| 329 | EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(1)); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 330 | EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0)); |
| 331 | EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1)); |
| 332 | |
| 333 | // The profile icon should be the default one. |
| 334 | int id = ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(0); |
| 335 | const gfx::Image& profile_image( |
| 336 | ResourceBundle::GetSharedInstance().GetImageNamed(id)); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 337 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 338 | profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1))); |
| 339 | |
| 340 | // Set GAIA picture. |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 341 | gfx::Image gaia_image(gfx::test::CreateImage()); |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 342 | GetCache()->SetGAIAPictureOfProfileAtIndex(1, &gaia_image); |
| 343 | EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0)); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 344 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 345 | gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1))); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 346 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 347 | profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1))); |
| 348 | |
| 349 | // Use GAIA picture as profile picture. |
| 350 | GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, true); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 351 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 352 | gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1))); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 353 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 354 | gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1))); |
| 355 | |
| 356 | // Don't use GAIA picture as profile picture. |
| 357 | GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, false); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 358 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 359 | gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1))); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 360 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 361 | profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1))); |
| 362 | } |
| 363 | |
[email protected] | 754bebc | 2011-12-01 16:42:16 | [diff] [blame] | 364 | TEST_F(ProfileInfoCacheTest, PersistGAIAPicture) { |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 365 | GetCache()->AddProfileToCache( |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 366 | GetProfilePath("path_1"), ASCIIToUTF16("name_1"), string16(), 0); |
| 367 | gfx::Image gaia_image(gfx::test::CreateImage()); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 368 | |
| 369 | ui_test_utils::WindowedNotificationObserver save_observer( |
| 370 | chrome::NOTIFICATION_PROFILE_CACHE_PICTURE_SAVED, |
| 371 | content::NotificationService::AllSources()); |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 372 | GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_image); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 373 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 374 | gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0))); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 375 | |
| 376 | // Wait for the file to be written to disk then reset the cache. |
| 377 | save_observer.Wait(); |
| 378 | ResetCache(); |
| 379 | |
| 380 | // Try to get the GAIA picture. This should return NULL until the read from |
| 381 | // disk is done. |
| 382 | ui_test_utils::WindowedNotificationObserver read_observer( |
| 383 | chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
| 384 | content::NotificationService::AllSources()); |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 385 | EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0)); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 386 | read_observer.Wait(); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 387 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 388 | gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0))); |
| 389 | } |
| 390 | |
| 391 | TEST_F(ProfileInfoCacheTest, EmptyGAIAInfo) { |
| 392 | string16 profile_name = ASCIIToUTF16("name_1"); |
| 393 | int id = ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(0); |
| 394 | const gfx::Image& profile_image( |
| 395 | ResourceBundle::GetSharedInstance().GetImageNamed(id)); |
| 396 | |
| 397 | GetCache()->AddProfileToCache( |
| 398 | GetProfilePath("path_1"), profile_name, string16(), 0); |
| 399 | |
| 400 | // Set empty GAIA info. |
| 401 | GetCache()->SetGAIANameOfProfileAtIndex(0, string16()); |
| 402 | GetCache()->SetGAIAPictureOfProfileAtIndex(0, NULL); |
| 403 | GetCache()->SetIsUsingGAIANameOfProfileAtIndex(0, true); |
| 404 | GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(0, true); |
| 405 | |
| 406 | // Verify that the profile name and picture are not empty. |
| 407 | EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(0)); |
| 408 | EXPECT_TRUE(gfx::test::IsEqual( |
| 409 | profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(0))); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 410 | } |
| 411 | |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 412 | } // namespace |