[email protected] | ca59107 | 2012-03-27 01:54:44 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 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 | |
[email protected] | e5ba874f | 2013-02-14 17:20:19 | [diff] [blame^] | 7 | #include "base/prefs/testing_pref_service.h" |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 8 | #include "base/stringprintf.h" |
| 9 | #include "base/utf_string_conversions.h" |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 10 | #include "chrome/browser/browser_process.h" |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 11 | #include "chrome/browser/profiles/profile_info_cache.h" |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 12 | #include "chrome/browser/profiles/profile_manager.h" |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 13 | #include "chrome/common/chrome_notification_types.h" |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 14 | #include "chrome/test/base/testing_browser_process.h" |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 15 | #include "content/public/browser/notification_observer.h" |
| 16 | #include "content/public/browser/notification_registrar.h" |
[email protected] | c0b03090 | 2011-12-19 16:21:05 | [diff] [blame] | 17 | #include "content/public/browser/notification_service.h" |
[email protected] | a7fe911 | 2012-07-20 02:34:45 | [diff] [blame] | 18 | #include "content/public/test/test_utils.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] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 36 | const base::FilePath& profile_path) { |
[email protected] | 6a46066 | 2011-12-22 22:05:16 | [diff] [blame] | 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] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 44 | const base::FilePath& profile_path) { |
[email protected] | 6a46066 | 2011-12-22 22:05:16 | [diff] [blame] | 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] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 52 | const base::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] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 58 | const base::FilePath& profile_path, |
[email protected] | 6a46066 | 2011-12-22 22:05:16 | [diff] [blame] | 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] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 69 | const base::FilePath& profile_path) { |
[email protected] | 6a46066 | 2011-12-22 22:05:16 | [diff] [blame] | 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_( |
[email protected] | c494d08 | 2013-01-04 20:41:22 | [diff] [blame] | 81 | TestingBrowserProcess::GetGlobal()), |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 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. |
[email protected] | b8f50ce | 2012-11-17 12:37:57 | [diff] [blame] | 98 | ui_loop_.RunUntilIdle(); |
[email protected] | 1302dcf | 2011-11-30 21:47:05 | [diff] [blame] | 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 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 105 | base::FilePath ProfileInfoCacheTest::GetProfilePath( |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 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 | |
[email protected] | ca59107 | 2012-03-27 01:54:44 | [diff] [blame] | 120 | ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 121 | for (uint32 i = 0; i < 4; ++i) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 122 | base::FilePath profile_path = GetProfilePath(StringPrintf("path_%ud", i)); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 123 | string16 profile_name = ASCIIToUTF16(StringPrintf("name_%ud", i)); |
[email protected] | ca59107 | 2012-03-27 01:54:44 | [diff] [blame] | 124 | const SkBitmap* icon = rb.GetImageNamed( |
| 125 | ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex( |
| 126 | i)).ToSkBitmap(); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 127 | |
[email protected] | fea25d8 | 2012-12-19 01:19:50 | [diff] [blame] | 128 | GetCache()->AddProfileToCache(profile_path, profile_name, string16(), i, |
| 129 | false); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 130 | GetCache()->SetBackgroundStatusOfProfileAtIndex(i, true); |
| 131 | string16 gaia_name = ASCIIToUTF16(StringPrintf("gaia_%ud", i)); |
| 132 | GetCache()->SetGAIANameOfProfileAtIndex(i, gaia_name); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 133 | |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 134 | EXPECT_EQ(i + 1, GetCache()->GetNumberOfProfiles()); |
| 135 | EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i)); |
| 136 | EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i)); |
[email protected] | ca59107 | 2012-03-27 01:54:44 | [diff] [blame] | 137 | const SkBitmap* actual_icon = GetCache()->GetAvatarIconOfProfileAtIndex( |
| 138 | i).ToSkBitmap(); |
| 139 | EXPECT_EQ(icon->width(), actual_icon->width()); |
| 140 | EXPECT_EQ(icon->height(), actual_icon->height()); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 141 | } |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 142 | |
| 143 | // Reset the cache and test the it reloads correctly. |
| 144 | ResetCache(); |
| 145 | |
| 146 | EXPECT_EQ(4u, GetCache()->GetNumberOfProfiles()); |
| 147 | for (uint32 i = 0; i < 4; ++i) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 148 | base::FilePath profile_path = GetProfilePath(StringPrintf("path_%ud", i)); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 149 | EXPECT_EQ(i, GetCache()->GetIndexOfProfileWithPath(profile_path)); |
| 150 | string16 profile_name = ASCIIToUTF16(StringPrintf("name_%ud", i)); |
| 151 | EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i)); |
| 152 | EXPECT_EQ(i, GetCache()->GetAvatarIconIndexOfProfileAtIndex(i)); |
| 153 | EXPECT_EQ(true, GetCache()->GetBackgroundStatusOfProfileAtIndex(i)); |
| 154 | string16 gaia_name = ASCIIToUTF16(StringPrintf("gaia_%ud", i)); |
| 155 | EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(i)); |
| 156 | } |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 157 | } |
| 158 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 159 | TEST_F(ProfileInfoCacheTest, DeleteProfile) { |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 160 | EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles()); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 161 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 162 | base::FilePath path_1 = GetProfilePath("path_1"); |
[email protected] | 73cb372 | 2011-10-11 18:12:13 | [diff] [blame] | 163 | GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("name_1"), string16(), |
[email protected] | fea25d8 | 2012-12-19 01:19:50 | [diff] [blame] | 164 | 0, false); |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 165 | EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles()); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 166 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 167 | base::FilePath path_2 = GetProfilePath("path_2"); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 168 | string16 name_2 = ASCIIToUTF16("name_2"); |
[email protected] | fea25d8 | 2012-12-19 01:19:50 | [diff] [blame] | 169 | GetCache()->AddProfileToCache(path_2, name_2, string16(), 0, false); |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 170 | EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles()); |
[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_1); |
| 173 | EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles()); |
| 174 | EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(0)); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 175 | |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 176 | GetCache()->DeleteProfileFromCache(path_2); |
| 177 | EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles()); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 178 | } |
| 179 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 180 | TEST_F(ProfileInfoCacheTest, MutateProfile) { |
| 181 | GetCache()->AddProfileToCache( |
[email protected] | fea25d8 | 2012-12-19 01:19:50 | [diff] [blame] | 182 | GetProfilePath("path_1"), ASCIIToUTF16("name_1"), string16(), 0, false); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 183 | GetCache()->AddProfileToCache( |
[email protected] | fea25d8 | 2012-12-19 01:19:50 | [diff] [blame] | 184 | GetProfilePath("path_2"), ASCIIToUTF16("name_2"), string16(), 0, false); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 185 | |
| 186 | string16 new_name = ASCIIToUTF16("new_name"); |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 187 | GetCache()->SetNameOfProfileAtIndex(1, new_name); |
| 188 | EXPECT_EQ(new_name, GetCache()->GetNameOfProfileAtIndex(1)); |
| 189 | EXPECT_NE(new_name, GetCache()->GetNameOfProfileAtIndex(0)); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 190 | |
[email protected] | e8e7809 | 2011-09-29 18:15:38 | [diff] [blame] | 191 | string16 new_user_name = ASCIIToUTF16("user_name"); |
| 192 | GetCache()->SetUserNameOfProfileAtIndex(1, new_user_name); |
| 193 | EXPECT_EQ(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(1)); |
| 194 | EXPECT_NE(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(0)); |
| 195 | |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 196 | size_t new_icon_index = 3; |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 197 | GetCache()->SetAvatarIconOfProfileAtIndex(1, new_icon_index); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 198 | // Not much to test. |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 199 | GetCache()->GetAvatarIconOfProfileAtIndex(1); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 200 | } |
| 201 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 202 | TEST_F(ProfileInfoCacheTest, Sort) { |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 203 | string16 name_a = ASCIIToUTF16("apple"); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 204 | GetCache()->AddProfileToCache( |
[email protected] | fea25d8 | 2012-12-19 01:19:50 | [diff] [blame] | 205 | GetProfilePath("path_a"), name_a, string16(), 0, false); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 206 | |
| 207 | string16 name_c = ASCIIToUTF16("cat"); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 208 | GetCache()->AddProfileToCache( |
[email protected] | fea25d8 | 2012-12-19 01:19:50 | [diff] [blame] | 209 | GetProfilePath("path_c"), name_c, string16(), 0, false); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 210 | |
| 211 | // Sanity check the initial order. |
| 212 | EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0)); |
| 213 | EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1)); |
| 214 | |
| 215 | // Add a new profile (start with a capital to test case insensitive sorting. |
| 216 | string16 name_b = ASCIIToUTF16("Banana"); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 217 | GetCache()->AddProfileToCache( |
[email protected] | fea25d8 | 2012-12-19 01:19:50 | [diff] [blame] | 218 | GetProfilePath("path_b"), name_b, string16(), 0, false); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 219 | |
| 220 | // Verify the new order. |
| 221 | EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0)); |
| 222 | EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(1)); |
| 223 | EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(2)); |
| 224 | |
| 225 | // Change the name of an existing profile. |
| 226 | name_a = UTF8ToUTF16("dog"); |
| 227 | GetCache()->SetNameOfProfileAtIndex(0, name_a); |
| 228 | |
| 229 | // Verify the new order. |
| 230 | EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0)); |
| 231 | EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1)); |
| 232 | EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(2)); |
| 233 | |
| 234 | // Delete a profile. |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 235 | GetCache()->DeleteProfileFromCache(GetProfilePath("path_c")); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 236 | |
| 237 | // Verify the new order. |
| 238 | EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0)); |
| 239 | EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(1)); |
| 240 | } |
| 241 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 242 | TEST_F(ProfileInfoCacheTest, BackgroundModeStatus) { |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 243 | GetCache()->AddProfileToCache( |
[email protected] | fea25d8 | 2012-12-19 01:19:50 | [diff] [blame] | 244 | GetProfilePath("path_1"), ASCIIToUTF16("name_1"), string16(), 0, false); |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 245 | GetCache()->AddProfileToCache( |
[email protected] | fea25d8 | 2012-12-19 01:19:50 | [diff] [blame] | 246 | GetProfilePath("path_2"), ASCIIToUTF16("name_2"), string16(), 0, false); |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 247 | |
| 248 | EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0)); |
| 249 | EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1)); |
| 250 | |
| 251 | GetCache()->SetBackgroundStatusOfProfileAtIndex(1, true); |
| 252 | |
| 253 | EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0)); |
| 254 | EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1)); |
| 255 | |
| 256 | GetCache()->SetBackgroundStatusOfProfileAtIndex(0, true); |
| 257 | |
| 258 | EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0)); |
| 259 | EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1)); |
| 260 | |
| 261 | GetCache()->SetBackgroundStatusOfProfileAtIndex(1, false); |
| 262 | |
| 263 | EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0)); |
| 264 | EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1)); |
| 265 | } |
| 266 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 267 | TEST_F(ProfileInfoCacheTest, HasMigrated) { |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 268 | GetCache()->AddProfileToCache( |
[email protected] | fea25d8 | 2012-12-19 01:19:50 | [diff] [blame] | 269 | GetProfilePath("path_1"), ASCIIToUTF16("name_1"), string16(), 0, false); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 270 | GetCache()->AddProfileToCache( |
[email protected] | fea25d8 | 2012-12-19 01:19:50 | [diff] [blame] | 271 | GetProfilePath("path_2"), ASCIIToUTF16("name_2"), string16(), 0, false); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 272 | |
| 273 | // Sanity check. |
| 274 | EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(0)); |
| 275 | EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(1)); |
| 276 | |
| 277 | // Set migrated state for 2nd profile. |
| 278 | GetCache()->SetHasMigratedToGAIAInfoOfProfileAtIndex(1, true); |
| 279 | EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(0)); |
| 280 | EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(1)); |
| 281 | |
| 282 | // Set migrated state for 1st profile. |
| 283 | GetCache()->SetHasMigratedToGAIAInfoOfProfileAtIndex(0, true); |
| 284 | EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(0)); |
| 285 | EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(1)); |
| 286 | |
| 287 | // Unset migrated state for 2nd profile. |
| 288 | GetCache()->SetHasMigratedToGAIAInfoOfProfileAtIndex(1, false); |
| 289 | EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(0)); |
| 290 | EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(1)); |
| 291 | } |
| 292 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 293 | TEST_F(ProfileInfoCacheTest, GAIAName) { |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 294 | GetCache()->AddProfileToCache( |
[email protected] | fea25d8 | 2012-12-19 01:19:50 | [diff] [blame] | 295 | GetProfilePath("path_1"), ASCIIToUTF16("name_1"), string16(), 0, false); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 296 | string16 profile_name(ASCIIToUTF16("profile name 2")); |
| 297 | GetCache()->AddProfileToCache( |
[email protected] | fea25d8 | 2012-12-19 01:19:50 | [diff] [blame] | 298 | GetProfilePath("path_2"), profile_name, string16(), 0, false); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 299 | |
| 300 | // Sanity check. |
| 301 | EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(0).empty()); |
| 302 | EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(1).empty()); |
| 303 | EXPECT_FALSE(GetCache()->IsUsingGAIANameOfProfileAtIndex(0)); |
| 304 | EXPECT_FALSE(GetCache()->IsUsingGAIANameOfProfileAtIndex(1)); |
| 305 | |
| 306 | // Set GAIA name. |
| 307 | string16 gaia_name(ASCIIToUTF16("Pat Smith")); |
| 308 | GetCache()->SetGAIANameOfProfileAtIndex(1, gaia_name); |
| 309 | EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(0).empty()); |
| 310 | EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(1)); |
| 311 | EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(1)); |
| 312 | |
| 313 | // Use GAIA name as profile name. |
| 314 | GetCache()->SetIsUsingGAIANameOfProfileAtIndex(1, true); |
| 315 | |
| 316 | EXPECT_EQ(gaia_name, GetCache()->GetNameOfProfileAtIndex(1)); |
| 317 | EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(1)); |
| 318 | |
| 319 | // Don't use GAIA name as profile name. |
| 320 | GetCache()->SetIsUsingGAIANameOfProfileAtIndex(1, false); |
| 321 | EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(1)); |
| 322 | EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(1)); |
| 323 | } |
| 324 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 325 | TEST_F(ProfileInfoCacheTest, GAIAPicture) { |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 326 | GetCache()->AddProfileToCache( |
[email protected] | fea25d8 | 2012-12-19 01:19:50 | [diff] [blame] | 327 | GetProfilePath("path_1"), ASCIIToUTF16("name_1"), string16(), 0, false); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 328 | GetCache()->AddProfileToCache( |
[email protected] | fea25d8 | 2012-12-19 01:19:50 | [diff] [blame] | 329 | GetProfilePath("path_2"), ASCIIToUTF16("name_2"), string16(), 0, false); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 330 | |
| 331 | // Sanity check. |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 332 | EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0)); |
| 333 | EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(1)); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 334 | EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0)); |
| 335 | EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1)); |
| 336 | |
| 337 | // The profile icon should be the default one. |
| 338 | int id = ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(0); |
| 339 | const gfx::Image& profile_image( |
| 340 | ResourceBundle::GetSharedInstance().GetImageNamed(id)); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 341 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 342 | profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1))); |
| 343 | |
| 344 | // Set GAIA picture. |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 345 | gfx::Image gaia_image(gfx::test::CreateImage()); |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 346 | GetCache()->SetGAIAPictureOfProfileAtIndex(1, &gaia_image); |
| 347 | EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0)); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 348 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 349 | gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1))); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 350 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 351 | profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1))); |
| 352 | |
| 353 | // Use GAIA picture as profile picture. |
| 354 | GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, true); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 355 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 356 | gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1))); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 357 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 358 | gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1))); |
| 359 | |
| 360 | // Don't use GAIA picture as profile picture. |
| 361 | GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, false); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 362 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 363 | gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1))); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 364 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 365 | profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1))); |
| 366 | } |
| 367 | |
[email protected] | 754bebc | 2011-12-01 16:42:16 | [diff] [blame] | 368 | TEST_F(ProfileInfoCacheTest, PersistGAIAPicture) { |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 369 | GetCache()->AddProfileToCache( |
[email protected] | fea25d8 | 2012-12-19 01:19:50 | [diff] [blame] | 370 | GetProfilePath("path_1"), ASCIIToUTF16("name_1"), string16(), 0, false); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 371 | gfx::Image gaia_image(gfx::test::CreateImage()); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 372 | |
[email protected] | a7fe911 | 2012-07-20 02:34:45 | [diff] [blame] | 373 | content::WindowedNotificationObserver save_observer( |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 374 | chrome::NOTIFICATION_PROFILE_CACHE_PICTURE_SAVED, |
| 375 | content::NotificationService::AllSources()); |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 376 | GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_image); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 377 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 378 | gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0))); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 379 | |
| 380 | // Wait for the file to be written to disk then reset the cache. |
| 381 | save_observer.Wait(); |
| 382 | ResetCache(); |
| 383 | |
| 384 | // Try to get the GAIA picture. This should return NULL until the read from |
| 385 | // disk is done. |
[email protected] | a7fe911 | 2012-07-20 02:34:45 | [diff] [blame] | 386 | content::WindowedNotificationObserver read_observer( |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 387 | chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
| 388 | content::NotificationService::AllSources()); |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 389 | EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0)); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 390 | read_observer.Wait(); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 391 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 392 | gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0))); |
| 393 | } |
| 394 | |
| 395 | TEST_F(ProfileInfoCacheTest, EmptyGAIAInfo) { |
| 396 | string16 profile_name = ASCIIToUTF16("name_1"); |
| 397 | int id = ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(0); |
| 398 | const gfx::Image& profile_image( |
| 399 | ResourceBundle::GetSharedInstance().GetImageNamed(id)); |
| 400 | |
| 401 | GetCache()->AddProfileToCache( |
[email protected] | fea25d8 | 2012-12-19 01:19:50 | [diff] [blame] | 402 | GetProfilePath("path_1"), profile_name, string16(), 0, false); |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 403 | |
| 404 | // Set empty GAIA info. |
| 405 | GetCache()->SetGAIANameOfProfileAtIndex(0, string16()); |
| 406 | GetCache()->SetGAIAPictureOfProfileAtIndex(0, NULL); |
| 407 | GetCache()->SetIsUsingGAIANameOfProfileAtIndex(0, true); |
| 408 | GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(0, true); |
| 409 | |
| 410 | // Verify that the profile name and picture are not empty. |
| 411 | EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(0)); |
| 412 | EXPECT_TRUE(gfx::test::IsEqual( |
| 413 | profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(0))); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 414 | } |
| 415 | |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 416 | } // namespace |