[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] | 04748180 | 2013-09-16 22:26:38 | [diff] [blame] | 7 | #include <vector> |
| 8 | |
[email protected] | 9de4430 | 2014-05-01 16:32:27 | [diff] [blame] | 9 | #include "base/file_util.h" |
[email protected] | e5ba874f | 2013-02-14 17:20:19 | [diff] [blame] | 10 | #include "base/prefs/testing_pref_service.h" |
[email protected] | 76fb05c | 2013-06-11 04:38:05 | [diff] [blame] | 11 | #include "base/strings/stringprintf.h" |
[email protected] | e309f31 | 2013-06-07 21:50:08 | [diff] [blame] | 12 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 85503a0 | 2014-01-16 17:27:22 | [diff] [blame] | 13 | #include "base/time/time.h" |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 14 | #include "chrome/browser/browser_process.h" |
[email protected] | 25ff086 | 2013-07-12 00:59:03 | [diff] [blame] | 15 | #include "chrome/browser/chrome_notification_types.h" |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 16 | #include "chrome/browser/prefs/pref_service_syncable.h" |
[email protected] | 9de4430 | 2014-05-01 16:32:27 | [diff] [blame] | 17 | #include "chrome/browser/profiles/profile_avatar_downloader.h" |
[email protected] | c3e55977 | 2014-04-09 04:02:54 | [diff] [blame] | 18 | #include "chrome/browser/profiles/profile_avatar_icon_util.h" |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 19 | #include "chrome/browser/profiles/profile_info_cache.h" |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 20 | #include "chrome/browser/profiles/profile_manager.h" |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 21 | #include "chrome/test/base/testing_browser_process.h" |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 22 | #include "content/public/browser/notification_observer.h" |
| 23 | #include "content/public/browser/notification_registrar.h" |
[email protected] | c0b03090 | 2011-12-19 16:21:05 | [diff] [blame] | 24 | #include "content/public/browser/notification_service.h" |
[email protected] | 371662e37 | 2013-10-17 22:05:22 | [diff] [blame] | 25 | #include "content/public/test/test_browser_thread_bundle.h" |
[email protected] | a7fe911 | 2012-07-20 02:34:45 | [diff] [blame] | 26 | #include "content/public/test/test_utils.h" |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 27 | #include "third_party/skia/include/core/SkBitmap.h" |
| 28 | #include "ui/base/resource/resource_bundle.h" |
| 29 | #include "ui/gfx/image/image.h" |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 30 | #include "ui/gfx/image/image_unittest_util.h" |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 31 | |
[email protected] | 6778fed | 2013-12-24 20:09:37 | [diff] [blame] | 32 | using base::ASCIIToUTF16; |
[email protected] | f729d7a | 2013-12-26 07:07:56 | [diff] [blame] | 33 | using base::UTF8ToUTF16; |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 34 | using content::BrowserThread; |
| 35 | |
[email protected] | 6a46066 | 2011-12-22 22:05:16 | [diff] [blame] | 36 | ProfileNameVerifierObserver::ProfileNameVerifierObserver( |
| 37 | TestingProfileManager* testing_profile_manager) |
| 38 | : testing_profile_manager_(testing_profile_manager) { |
| 39 | DCHECK(testing_profile_manager_); |
[email protected] | 590e189b | 2011-12-13 22:07:03 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | ProfileNameVerifierObserver::~ProfileNameVerifierObserver() { |
| 43 | } |
| 44 | |
| 45 | void ProfileNameVerifierObserver::OnProfileAdded( |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 46 | const base::FilePath& profile_path) { |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 47 | base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex( |
[email protected] | 6a46066 | 2011-12-22 22:05:16 | [diff] [blame] | 48 | GetCache()->GetIndexOfProfileWithPath(profile_path)); |
[email protected] | 590e189b | 2011-12-13 22:07:03 | [diff] [blame] | 49 | EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end()); |
| 50 | profile_names_.insert(profile_name); |
| 51 | } |
| 52 | |
[email protected] | 7b0af15 | 2011-12-16 17:02:06 | [diff] [blame] | 53 | void ProfileNameVerifierObserver::OnProfileWillBeRemoved( |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 54 | const base::FilePath& profile_path) { |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 55 | base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex( |
[email protected] | 6a46066 | 2011-12-22 22:05:16 | [diff] [blame] | 56 | GetCache()->GetIndexOfProfileWithPath(profile_path)); |
[email protected] | 590e189b | 2011-12-13 22:07:03 | [diff] [blame] | 57 | EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end()); |
| 58 | profile_names_.erase(profile_name); |
| 59 | } |
| 60 | |
[email protected] | 7b0af15 | 2011-12-16 17:02:06 | [diff] [blame] | 61 | void ProfileNameVerifierObserver::OnProfileWasRemoved( |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 62 | const base::FilePath& profile_path, |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 63 | const base::string16& profile_name) { |
[email protected] | 7b0af15 | 2011-12-16 17:02:06 | [diff] [blame] | 64 | EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end()); |
| 65 | } |
| 66 | |
[email protected] | 590e189b | 2011-12-13 22:07:03 | [diff] [blame] | 67 | void ProfileNameVerifierObserver::OnProfileNameChanged( |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 68 | const base::FilePath& profile_path, |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 69 | const base::string16& old_profile_name) { |
| 70 | base::string16 new_profile_name = GetCache()->GetNameOfProfileAtIndex( |
[email protected] | 6a46066 | 2011-12-22 22:05:16 | [diff] [blame] | 71 | GetCache()->GetIndexOfProfileWithPath(profile_path)); |
[email protected] | 590e189b | 2011-12-13 22:07:03 | [diff] [blame] | 72 | EXPECT_TRUE(profile_names_.find(old_profile_name) != profile_names_.end()); |
| 73 | EXPECT_TRUE(profile_names_.find(new_profile_name) == profile_names_.end()); |
| 74 | profile_names_.erase(old_profile_name); |
| 75 | profile_names_.insert(new_profile_name); |
| 76 | } |
| 77 | |
| 78 | void ProfileNameVerifierObserver::OnProfileAvatarChanged( |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 79 | const base::FilePath& profile_path) { |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 80 | base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex( |
[email protected] | 6a46066 | 2011-12-22 22:05:16 | [diff] [blame] | 81 | GetCache()->GetIndexOfProfileWithPath(profile_path)); |
[email protected] | 590e189b | 2011-12-13 22:07:03 | [diff] [blame] | 82 | EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end()); |
| 83 | } |
| 84 | |
[email protected] | 6a46066 | 2011-12-22 22:05:16 | [diff] [blame] | 85 | ProfileInfoCache* ProfileNameVerifierObserver::GetCache() { |
| 86 | return testing_profile_manager_->profile_info_cache(); |
| 87 | } |
| 88 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 89 | ProfileInfoCacheTest::ProfileInfoCacheTest() |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 90 | : testing_profile_manager_(TestingBrowserProcess::GetGlobal()), |
[email protected] | 6a46066 | 2011-12-22 22:05:16 | [diff] [blame] | 91 | name_observer_(&testing_profile_manager_) { |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | ProfileInfoCacheTest::~ProfileInfoCacheTest() { |
| 95 | } |
| 96 | |
| 97 | void ProfileInfoCacheTest::SetUp() { |
| 98 | ASSERT_TRUE(testing_profile_manager_.SetUp()); |
[email protected] | 590e189b | 2011-12-13 22:07:03 | [diff] [blame] | 99 | testing_profile_manager_.profile_info_cache()->AddObserver(&name_observer_); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 100 | } |
| 101 | |
[email protected] | 1302dcf | 2011-11-30 21:47:05 | [diff] [blame] | 102 | void ProfileInfoCacheTest::TearDown() { |
| 103 | // Drain the UI thread to make sure all tasks are completed. This prevents |
| 104 | // memory leaks. |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 105 | base::RunLoop().RunUntilIdle(); |
[email protected] | 1302dcf | 2011-11-30 21:47:05 | [diff] [blame] | 106 | } |
| 107 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 108 | ProfileInfoCache* ProfileInfoCacheTest::GetCache() { |
| 109 | return testing_profile_manager_.profile_info_cache(); |
| 110 | } |
| 111 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 112 | base::FilePath ProfileInfoCacheTest::GetProfilePath( |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 113 | const std::string& base_name) { |
| 114 | return testing_profile_manager_.profile_manager()->user_data_dir(). |
| 115 | AppendASCII(base_name); |
| 116 | } |
| 117 | |
| 118 | void ProfileInfoCacheTest::ResetCache() { |
| 119 | testing_profile_manager_.DeleteProfileInfoCache(); |
| 120 | } |
| 121 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 122 | TEST_F(ProfileInfoCacheTest, AddProfiles) { |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 123 | EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles()); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 124 | |
[email protected] | ca59107 | 2012-03-27 01:54:44 | [diff] [blame] | 125 | ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 126 | for (uint32 i = 0; i < 4; ++i) { |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 127 | base::FilePath profile_path = |
| 128 | GetProfilePath(base::StringPrintf("path_%ud", i)); |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 129 | base::string16 profile_name = |
| 130 | ASCIIToUTF16(base::StringPrintf("name_%ud", i)); |
[email protected] | ca59107 | 2012-03-27 01:54:44 | [diff] [blame] | 131 | const SkBitmap* icon = rb.GetImageNamed( |
[email protected] | c3e55977 | 2014-04-09 04:02:54 | [diff] [blame] | 132 | profiles::GetDefaultAvatarIconResourceIDAtIndex( |
[email protected] | ca59107 | 2012-03-27 01:54:44 | [diff] [blame] | 133 | i)).ToSkBitmap(); |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 134 | std::string managed_user_id = i == 3 ? "TEST_ID" : ""; |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 135 | |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 136 | GetCache()->AddProfileToCache(profile_path, profile_name, base::string16(), |
| 137 | i, managed_user_id); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 138 | GetCache()->SetBackgroundStatusOfProfileAtIndex(i, true); |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 139 | base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i)); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 140 | GetCache()->SetGAIANameOfProfileAtIndex(i, gaia_name); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 141 | |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 142 | EXPECT_EQ(i + 1, GetCache()->GetNumberOfProfiles()); |
| 143 | EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i)); |
| 144 | EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i)); |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 145 | const SkBitmap* actual_icon = |
| 146 | GetCache()->GetAvatarIconOfProfileAtIndex(i).ToSkBitmap(); |
[email protected] | ca59107 | 2012-03-27 01:54:44 | [diff] [blame] | 147 | EXPECT_EQ(icon->width(), actual_icon->width()); |
| 148 | EXPECT_EQ(icon->height(), actual_icon->height()); |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 149 | EXPECT_EQ(i == 3, GetCache()->ProfileIsManagedAtIndex(i)); |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 150 | EXPECT_EQ(i == 3, GetCache()->IsOmittedProfileAtIndex(i)); |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 151 | EXPECT_EQ(managed_user_id, GetCache()->GetManagedUserIdOfProfileAtIndex(i)); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 152 | } |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 153 | |
| 154 | // Reset the cache and test the it reloads correctly. |
| 155 | ResetCache(); |
| 156 | |
| 157 | EXPECT_EQ(4u, GetCache()->GetNumberOfProfiles()); |
| 158 | for (uint32 i = 0; i < 4; ++i) { |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 159 | base::FilePath profile_path = |
| 160 | GetProfilePath(base::StringPrintf("path_%ud", i)); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 161 | EXPECT_EQ(i, GetCache()->GetIndexOfProfileWithPath(profile_path)); |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 162 | base::string16 profile_name = |
| 163 | ASCIIToUTF16(base::StringPrintf("name_%ud", i)); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 164 | EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i)); |
| 165 | EXPECT_EQ(i, GetCache()->GetAvatarIconIndexOfProfileAtIndex(i)); |
| 166 | EXPECT_EQ(true, GetCache()->GetBackgroundStatusOfProfileAtIndex(i)); |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 167 | base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i)); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 168 | EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(i)); |
| 169 | } |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 170 | } |
| 171 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 172 | TEST_F(ProfileInfoCacheTest, DeleteProfile) { |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 173 | EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles()); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 174 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 175 | base::FilePath path_1 = GetProfilePath("path_1"); |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 176 | GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("name_1"), |
| 177 | base::string16(), 0, std::string()); |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 178 | EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles()); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 179 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 180 | base::FilePath path_2 = GetProfilePath("path_2"); |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 181 | base::string16 name_2 = ASCIIToUTF16("name_2"); |
| 182 | GetCache()->AddProfileToCache(path_2, name_2, base::string16(), 0, |
| 183 | std::string()); |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 184 | EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles()); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 185 | |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 186 | GetCache()->DeleteProfileFromCache(path_1); |
| 187 | EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles()); |
| 188 | EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(0)); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 189 | |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 190 | GetCache()->DeleteProfileFromCache(path_2); |
| 191 | EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles()); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 192 | } |
| 193 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 194 | TEST_F(ProfileInfoCacheTest, MutateProfile) { |
| 195 | GetCache()->AddProfileToCache( |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 196 | GetProfilePath("path_1"), ASCIIToUTF16("name_1"), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 197 | base::string16(), 0, std::string()); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 198 | GetCache()->AddProfileToCache( |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 199 | GetProfilePath("path_2"), ASCIIToUTF16("name_2"), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 200 | base::string16(), 0, std::string()); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 201 | |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 202 | base::string16 new_name = ASCIIToUTF16("new_name"); |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 203 | GetCache()->SetNameOfProfileAtIndex(1, new_name); |
| 204 | EXPECT_EQ(new_name, GetCache()->GetNameOfProfileAtIndex(1)); |
| 205 | EXPECT_NE(new_name, GetCache()->GetNameOfProfileAtIndex(0)); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 206 | |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 207 | base::string16 new_user_name = ASCIIToUTF16("user_name"); |
[email protected] | e8e7809 | 2011-09-29 18:15:38 | [diff] [blame] | 208 | GetCache()->SetUserNameOfProfileAtIndex(1, new_user_name); |
| 209 | EXPECT_EQ(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(1)); |
| 210 | EXPECT_NE(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(0)); |
| 211 | |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 212 | size_t new_icon_index = 3; |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 213 | GetCache()->SetAvatarIconOfProfileAtIndex(1, new_icon_index); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 214 | // Not much to test. |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 215 | GetCache()->GetAvatarIconOfProfileAtIndex(1); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 216 | } |
| 217 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 218 | TEST_F(ProfileInfoCacheTest, Sort) { |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 219 | base::string16 name_a = ASCIIToUTF16("apple"); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 220 | GetCache()->AddProfileToCache( |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 221 | GetProfilePath("path_a"), name_a, base::string16(), 0, std::string()); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 222 | |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 223 | base::string16 name_c = ASCIIToUTF16("cat"); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 224 | GetCache()->AddProfileToCache( |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 225 | GetProfilePath("path_c"), name_c, base::string16(), 0, std::string()); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 226 | |
| 227 | // Sanity check the initial order. |
| 228 | EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0)); |
| 229 | EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1)); |
| 230 | |
| 231 | // Add a new profile (start with a capital to test case insensitive sorting. |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 232 | base::string16 name_b = ASCIIToUTF16("Banana"); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 233 | GetCache()->AddProfileToCache( |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 234 | GetProfilePath("path_b"), name_b, base::string16(), 0, std::string()); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 235 | |
| 236 | // Verify the new order. |
| 237 | EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0)); |
| 238 | EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(1)); |
| 239 | EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(2)); |
| 240 | |
| 241 | // Change the name of an existing profile. |
| 242 | name_a = UTF8ToUTF16("dog"); |
| 243 | GetCache()->SetNameOfProfileAtIndex(0, name_a); |
| 244 | |
| 245 | // Verify the new order. |
| 246 | EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0)); |
| 247 | EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1)); |
| 248 | EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(2)); |
| 249 | |
| 250 | // Delete a profile. |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 251 | GetCache()->DeleteProfileFromCache(GetProfilePath("path_c")); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 252 | |
| 253 | // Verify the new order. |
| 254 | EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0)); |
| 255 | EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(1)); |
| 256 | } |
| 257 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 258 | TEST_F(ProfileInfoCacheTest, BackgroundModeStatus) { |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 259 | GetCache()->AddProfileToCache( |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 260 | GetProfilePath("path_1"), ASCIIToUTF16("name_1"), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 261 | base::string16(), 0, std::string()); |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 262 | GetCache()->AddProfileToCache( |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 263 | GetProfilePath("path_2"), ASCIIToUTF16("name_2"), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 264 | base::string16(), 0, std::string()); |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 265 | |
| 266 | EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0)); |
| 267 | EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1)); |
| 268 | |
| 269 | GetCache()->SetBackgroundStatusOfProfileAtIndex(1, true); |
| 270 | |
| 271 | EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0)); |
| 272 | EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1)); |
| 273 | |
| 274 | GetCache()->SetBackgroundStatusOfProfileAtIndex(0, true); |
| 275 | |
| 276 | EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0)); |
| 277 | EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1)); |
| 278 | |
| 279 | GetCache()->SetBackgroundStatusOfProfileAtIndex(1, false); |
| 280 | |
| 281 | EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0)); |
| 282 | EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1)); |
| 283 | } |
| 284 | |
[email protected] | 85503a0 | 2014-01-16 17:27:22 | [diff] [blame] | 285 | TEST_F(ProfileInfoCacheTest, ProfileActiveTime) { |
| 286 | GetCache()->AddProfileToCache( |
| 287 | GetProfilePath("path_1"), ASCIIToUTF16("name_1"), |
| 288 | base::string16(), 0, std::string()); |
| 289 | EXPECT_EQ(base::Time(), GetCache()->GetProfileActiveTimeAtIndex(0)); |
[email protected] | 87727e0 | 2014-02-27 18:16:53 | [diff] [blame] | 290 | // Before & After times are artificially shifted because just relying upon |
| 291 | // the system time can yield problems due to inaccuracies in the |
| 292 | // underlying storage system (which uses a double with only 52 bits of |
| 293 | // precision to store the 64-bit "time" number). http://crbug.com/346827 |
[email protected] | 85503a0 | 2014-01-16 17:27:22 | [diff] [blame] | 294 | base::Time before = base::Time::Now(); |
[email protected] | 87727e0 | 2014-02-27 18:16:53 | [diff] [blame] | 295 | before -= base::TimeDelta::FromSeconds(1); |
[email protected] | 85503a0 | 2014-01-16 17:27:22 | [diff] [blame] | 296 | GetCache()->SetProfileActiveTimeAtIndex(0); |
| 297 | base::Time after = base::Time::Now(); |
[email protected] | 87727e0 | 2014-02-27 18:16:53 | [diff] [blame] | 298 | after += base::TimeDelta::FromSeconds(1); |
[email protected] | 85503a0 | 2014-01-16 17:27:22 | [diff] [blame] | 299 | EXPECT_LE(before, GetCache()->GetProfileActiveTimeAtIndex(0)); |
| 300 | EXPECT_GE(after, GetCache()->GetProfileActiveTimeAtIndex(0)); |
| 301 | } |
| 302 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 303 | TEST_F(ProfileInfoCacheTest, GAIAName) { |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 304 | GetCache()->AddProfileToCache( |
[email protected] | a4352d0 | 2014-03-06 06:00:27 | [diff] [blame] | 305 | GetProfilePath("path_1"), ASCIIToUTF16("Person 1"), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 306 | base::string16(), 0, std::string()); |
[email protected] | a4352d0 | 2014-03-06 06:00:27 | [diff] [blame] | 307 | base::string16 profile_name(ASCIIToUTF16("Person 2")); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 308 | GetCache()->AddProfileToCache( |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 309 | GetProfilePath("path_2"), profile_name, base::string16(), 0, |
| 310 | std::string()); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 311 | |
[email protected] | a4352d0 | 2014-03-06 06:00:27 | [diff] [blame] | 312 | int index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1")); |
| 313 | int index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2")); |
| 314 | |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 315 | // Sanity check. |
[email protected] | a4352d0 | 2014-03-06 06:00:27 | [diff] [blame] | 316 | EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty()); |
| 317 | EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index2).empty()); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 318 | |
[email protected] | 425d6ab | 2014-03-25 05:59:39 | [diff] [blame] | 319 | // Set GAIA name. This re-sorts the cache. |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 320 | base::string16 gaia_name(ASCIIToUTF16("Pat Smith")); |
[email protected] | a4352d0 | 2014-03-06 06:00:27 | [diff] [blame] | 321 | GetCache()->SetGAIANameOfProfileAtIndex(index2, gaia_name); |
[email protected] | 425d6ab | 2014-03-25 05:59:39 | [diff] [blame] | 322 | index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1")); |
| 323 | index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2")); |
| 324 | |
| 325 | // Since there is a GAIA name, we use that as a display name. |
[email protected] | a4352d0 | 2014-03-06 06:00:27 | [diff] [blame] | 326 | EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty()); |
| 327 | EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2)); |
[email protected] | a4352d0 | 2014-03-06 06:00:27 | [diff] [blame] | 328 | EXPECT_EQ(gaia_name, GetCache()->GetNameOfProfileAtIndex(index2)); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 329 | |
[email protected] | a4352d0 | 2014-03-06 06:00:27 | [diff] [blame] | 330 | // Don't use GAIA name as profile name. This re-sorts the cache. |
[email protected] | 425d6ab | 2014-03-25 05:59:39 | [diff] [blame] | 331 | base::string16 custom_name(ASCIIToUTF16("Custom name")); |
| 332 | GetCache()->SetNameOfProfileAtIndex(index2, custom_name); |
[email protected] | a4352d0 | 2014-03-06 06:00:27 | [diff] [blame] | 333 | index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1")); |
| 334 | index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2")); |
| 335 | |
[email protected] | 425d6ab | 2014-03-25 05:59:39 | [diff] [blame] | 336 | EXPECT_EQ(custom_name, GetCache()->GetNameOfProfileAtIndex(index2)); |
[email protected] | a4352d0 | 2014-03-06 06:00:27 | [diff] [blame] | 337 | EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2)); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 338 | } |
| 339 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 340 | TEST_F(ProfileInfoCacheTest, GAIAPicture) { |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 341 | GetCache()->AddProfileToCache( |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 342 | GetProfilePath("path_1"), ASCIIToUTF16("name_1"), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 343 | base::string16(), 0, std::string()); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 344 | GetCache()->AddProfileToCache( |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 345 | GetProfilePath("path_2"), ASCIIToUTF16("name_2"), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 346 | base::string16(), 0, std::string()); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 347 | |
| 348 | // Sanity check. |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 349 | EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0)); |
| 350 | EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(1)); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 351 | EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0)); |
| 352 | EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1)); |
| 353 | |
| 354 | // The profile icon should be the default one. |
[email protected] | c3e55977 | 2014-04-09 04:02:54 | [diff] [blame] | 355 | int id = profiles::GetDefaultAvatarIconResourceIDAtIndex(0); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 356 | const gfx::Image& profile_image( |
| 357 | ResourceBundle::GetSharedInstance().GetImageNamed(id)); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 358 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 359 | profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1))); |
| 360 | |
| 361 | // Set GAIA picture. |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 362 | gfx::Image gaia_image(gfx::test::CreateImage()); |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 363 | GetCache()->SetGAIAPictureOfProfileAtIndex(1, &gaia_image); |
| 364 | EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0)); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 365 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 366 | gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1))); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 367 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 368 | profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1))); |
| 369 | |
| 370 | // Use GAIA picture as profile picture. |
| 371 | GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, true); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 372 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 373 | gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1))); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 374 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 375 | gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1))); |
| 376 | |
| 377 | // Don't use GAIA picture as profile picture. |
| 378 | GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, false); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 379 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 380 | gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1))); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 381 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 382 | profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1))); |
| 383 | } |
| 384 | |
[email protected] | 754bebc | 2011-12-01 16:42:16 | [diff] [blame] | 385 | TEST_F(ProfileInfoCacheTest, PersistGAIAPicture) { |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 386 | GetCache()->AddProfileToCache( |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 387 | GetProfilePath("path_1"), ASCIIToUTF16("name_1"), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 388 | base::string16(), 0, std::string()); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 389 | gfx::Image gaia_image(gfx::test::CreateImage()); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 390 | |
[email protected] | a7fe911 | 2012-07-20 02:34:45 | [diff] [blame] | 391 | content::WindowedNotificationObserver save_observer( |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 392 | chrome::NOTIFICATION_PROFILE_CACHE_PICTURE_SAVED, |
| 393 | content::NotificationService::AllSources()); |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 394 | GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_image); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 395 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 396 | gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0))); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 397 | |
| 398 | // Wait for the file to be written to disk then reset the cache. |
| 399 | save_observer.Wait(); |
| 400 | ResetCache(); |
| 401 | |
| 402 | // Try to get the GAIA picture. This should return NULL until the read from |
| 403 | // disk is done. |
[email protected] | a7fe911 | 2012-07-20 02:34:45 | [diff] [blame] | 404 | content::WindowedNotificationObserver read_observer( |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 405 | chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
| 406 | content::NotificationService::AllSources()); |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 407 | EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0)); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 408 | read_observer.Wait(); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 409 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 410 | gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0))); |
| 411 | } |
| 412 | |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 413 | TEST_F(ProfileInfoCacheTest, SetManagedUserId) { |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 414 | GetCache()->AddProfileToCache( |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 415 | GetProfilePath("test"), ASCIIToUTF16("Test"), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 416 | base::string16(), 0, std::string()); |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 417 | EXPECT_FALSE(GetCache()->ProfileIsManagedAtIndex(0)); |
| 418 | |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 419 | GetCache()->SetManagedUserIdOfProfileAtIndex(0, "TEST_ID"); |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 420 | EXPECT_TRUE(GetCache()->ProfileIsManagedAtIndex(0)); |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 421 | EXPECT_EQ("TEST_ID", GetCache()->GetManagedUserIdOfProfileAtIndex(0)); |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 422 | |
| 423 | ResetCache(); |
| 424 | EXPECT_TRUE(GetCache()->ProfileIsManagedAtIndex(0)); |
| 425 | |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 426 | GetCache()->SetManagedUserIdOfProfileAtIndex(0, std::string()); |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 427 | EXPECT_FALSE(GetCache()->ProfileIsManagedAtIndex(0)); |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 428 | EXPECT_EQ("", GetCache()->GetManagedUserIdOfProfileAtIndex(0)); |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 429 | } |
| 430 | |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 431 | TEST_F(ProfileInfoCacheTest, EmptyGAIAInfo) { |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 432 | base::string16 profile_name = ASCIIToUTF16("name_1"); |
[email protected] | c3e55977 | 2014-04-09 04:02:54 | [diff] [blame] | 433 | int id = profiles::GetDefaultAvatarIconResourceIDAtIndex(0); |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 434 | const gfx::Image& profile_image( |
| 435 | ResourceBundle::GetSharedInstance().GetImageNamed(id)); |
| 436 | |
| 437 | GetCache()->AddProfileToCache( |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 438 | GetProfilePath("path_1"), profile_name, base::string16(), 0, |
| 439 | std::string()); |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 440 | |
| 441 | // Set empty GAIA info. |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 442 | GetCache()->SetGAIANameOfProfileAtIndex(0, base::string16()); |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 443 | GetCache()->SetGAIAPictureOfProfileAtIndex(0, NULL); |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 444 | GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(0, true); |
| 445 | |
| 446 | // Verify that the profile name and picture are not empty. |
| 447 | EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(0)); |
| 448 | EXPECT_TRUE(gfx::test::IsEqual( |
| 449 | profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(0))); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 450 | } |
| 451 | |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 452 | TEST_F(ProfileInfoCacheTest, CreateManagedTestingProfile) { |
| 453 | testing_profile_manager_.CreateTestingProfile("default"); |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 454 | base::string16 managed_user_name = ASCIIToUTF16("Supervised User"); |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 455 | testing_profile_manager_.CreateTestingProfile( |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 456 | "test1", scoped_ptr<PrefServiceSyncable>(), |
[email protected] | cb0323d | 2013-11-22 23:21:26 | [diff] [blame] | 457 | managed_user_name, 0, "TEST_ID", TestingProfile::TestingFactories()); |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 458 | for (size_t i = 0; i < GetCache()->GetNumberOfProfiles(); i++) { |
| 459 | bool is_managed = |
| 460 | GetCache()->GetNameOfProfileAtIndex(i) == managed_user_name; |
| 461 | EXPECT_EQ(is_managed, GetCache()->ProfileIsManagedAtIndex(i)); |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 462 | std::string managed_user_id = is_managed ? "TEST_ID" : ""; |
| 463 | EXPECT_EQ(managed_user_id, GetCache()->GetManagedUserIdOfProfileAtIndex(i)); |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 464 | } |
[email protected] | 371662e37 | 2013-10-17 22:05:22 | [diff] [blame] | 465 | |
| 466 | // Managed profiles have a custom theme, which needs to be deleted on the FILE |
| 467 | // thread. Reset the profile manager now so everything is deleted while we |
| 468 | // still have a FILE thread. |
| 469 | TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL); |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 470 | } |
| 471 | |
[email protected] | 04748180 | 2013-09-16 22:26:38 | [diff] [blame] | 472 | TEST_F(ProfileInfoCacheTest, AddStubProfile) { |
| 473 | EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles()); |
| 474 | |
| 475 | // Add some profiles with and without a '.' in their paths. |
| 476 | const struct { |
| 477 | const char* profile_path; |
| 478 | const char* profile_name; |
| 479 | } kTestCases[] = { |
| 480 | { "path.test0", "name_0" }, |
| 481 | { "path_test1", "name_1" }, |
| 482 | { "path.test2", "name_2" }, |
| 483 | { "path_test3", "name_3" }, |
| 484 | }; |
| 485 | |
| 486 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { |
| 487 | base::FilePath profile_path = GetProfilePath(kTestCases[i].profile_path); |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 488 | base::string16 profile_name = ASCIIToUTF16(kTestCases[i].profile_name); |
[email protected] | 04748180 | 2013-09-16 22:26:38 | [diff] [blame] | 489 | |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 490 | GetCache()->AddProfileToCache(profile_path, profile_name, base::string16(), |
| 491 | i, ""); |
[email protected] | 04748180 | 2013-09-16 22:26:38 | [diff] [blame] | 492 | |
| 493 | EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i)); |
| 494 | EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i)); |
| 495 | } |
| 496 | |
| 497 | ASSERT_EQ(4U, GetCache()->GetNumberOfProfiles()); |
| 498 | |
| 499 | // Check that the profiles can be extracted from the local state. |
[email protected] | d2065e06 | 2013-12-12 23:49:52 | [diff] [blame] | 500 | std::vector<base::string16> names = ProfileInfoCache::GetProfileNames(); |
[email protected] | 04748180 | 2013-09-16 22:26:38 | [diff] [blame] | 501 | for (size_t i = 0; i < 4; i++) |
| 502 | ASSERT_FALSE(names[i].empty()); |
| 503 | } |
| 504 | |
[email protected] | 9de4430 | 2014-05-01 16:32:27 | [diff] [blame] | 505 | TEST_F(ProfileInfoCacheTest, DownloadHighResAvatarTest) { |
| 506 | EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles()); |
| 507 | base::FilePath path_1 = GetProfilePath("path_1"); |
| 508 | GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("name_1"), |
| 509 | base::string16(), 0, std::string()); |
| 510 | EXPECT_EQ(1U, GetCache()->GetNumberOfProfiles()); |
| 511 | |
| 512 | // We haven't downloaded any high-res avatars yet. |
| 513 | EXPECT_EQ(0U, GetCache()->cached_avatar_images_.size()); |
| 514 | EXPECT_EQ(0U, GetCache()->avatar_images_downloads_in_progress_.size()); |
| 515 | EXPECT_FALSE(GetCache()->GetHighResAvatarOfProfileAtIndex(0)); |
| 516 | |
| 517 | // Simulate downloading a high-res avatar. |
| 518 | const size_t kIconIndex = 0; |
[email protected] | 2d60571 | 2014-05-14 12:03:07 | [diff] [blame^] | 519 | ProfileAvatarDownloader avatar_downloader( |
| 520 | kIconIndex, GetCache()->GetPathOfProfileAtIndex(0), GetCache()); |
[email protected] | 9de4430 | 2014-05-01 16:32:27 | [diff] [blame] | 521 | |
| 522 | // Put a real bitmap into "bitmap". 2x2 bitmap of green 32 bit pixels. |
| 523 | SkBitmap bitmap; |
| 524 | bitmap.setConfig(SkBitmap::kARGB_8888_Config, 2, 2); |
| 525 | bitmap.allocPixels(); |
| 526 | bitmap.eraseColor(SK_ColorGREEN); |
| 527 | |
| 528 | avatar_downloader.OnFetchComplete( |
| 529 | GURL("http://www.google.com/avatar.png"), &bitmap); |
| 530 | |
| 531 | std::string file_name = |
| 532 | profiles::GetDefaultAvatarIconFileNameAtIndex(kIconIndex); |
| 533 | |
| 534 | // The file should have been cached and saved. |
| 535 | EXPECT_EQ(0U, GetCache()->avatar_images_downloads_in_progress_.size()); |
| 536 | EXPECT_EQ(1U, GetCache()->cached_avatar_images_.size()); |
| 537 | EXPECT_TRUE(GetCache()->GetHighResAvatarOfProfileAtIndex(0)); |
| 538 | EXPECT_EQ(GetCache()->cached_avatar_images_[file_name], |
| 539 | GetCache()->GetHighResAvatarOfProfileAtIndex(0)); |
| 540 | |
| 541 | // Make sure everything has completed, and the file has been written to disk. |
| 542 | base::RunLoop().RunUntilIdle(); |
| 543 | |
| 544 | // Clean up. |
| 545 | base::FilePath icon_path = |
| 546 | profiles::GetPathOfHighResAvatarAtIndex(kIconIndex); |
| 547 | EXPECT_NE(std::string::npos, icon_path.MaybeAsASCII().find(file_name)); |
| 548 | EXPECT_TRUE(base::PathExists(icon_path)); |
| 549 | EXPECT_TRUE(base::DeleteFile(icon_path, true)); |
| 550 | EXPECT_FALSE(base::PathExists(icon_path)); |
| 551 | } |