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