[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] | 5cf8bcd9 | 2014-07-17 20:27:40 | [diff] [blame] | 9 | #include "base/command_line.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 10 | #include "base/files/file_util.h" |
[email protected] | e5ba874f | 2013-02-14 17:20:19 | [diff] [blame] | 11 | #include "base/prefs/testing_pref_service.h" |
[email protected] | 76fb05c | 2013-06-11 04:38:05 | [diff] [blame] | 12 | #include "base/strings/stringprintf.h" |
[email protected] | e309f31 | 2013-06-07 21:50:08 | [diff] [blame] | 13 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 85503a0 | 2014-01-16 17:27:22 | [diff] [blame] | 14 | #include "base/time/time.h" |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 15 | #include "chrome/browser/browser_process.h" |
[email protected] | 25ff086 | 2013-07-12 00:59:03 | [diff] [blame] | 16 | #include "chrome/browser/chrome_notification_types.h" |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 17 | #include "chrome/browser/prefs/pref_service_syncable.h" |
[email protected] | 9de4430 | 2014-05-01 16:32:27 | [diff] [blame] | 18 | #include "chrome/browser/profiles/profile_avatar_downloader.h" |
[email protected] | c3e55977 | 2014-04-09 04:02:54 | [diff] [blame] | 19 | #include "chrome/browser/profiles/profile_avatar_icon_util.h" |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 20 | #include "chrome/browser/profiles/profile_info_cache.h" |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 21 | #include "chrome/browser/profiles/profile_manager.h" |
[email protected] | 5cf8bcd9 | 2014-07-17 20:27:40 | [diff] [blame] | 22 | #include "chrome/common/chrome_switches.h" |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 23 | #include "chrome/test/base/testing_browser_process.h" |
[email protected] | 5cf8bcd9 | 2014-07-17 20:27:40 | [diff] [blame] | 24 | #include "components/signin/core/common/profile_management_switches.h" |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 25 | #include "content/public/browser/notification_observer.h" |
| 26 | #include "content/public/browser/notification_registrar.h" |
[email protected] | c0b03090 | 2011-12-19 16:21:05 | [diff] [blame] | 27 | #include "content/public/browser/notification_service.h" |
[email protected] | 371662e37 | 2013-10-17 22:05:22 | [diff] [blame] | 28 | #include "content/public/test/test_browser_thread_bundle.h" |
[email protected] | a7fe911 | 2012-07-20 02:34:45 | [diff] [blame] | 29 | #include "content/public/test/test_utils.h" |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 30 | #include "third_party/skia/include/core/SkBitmap.h" |
| 31 | #include "ui/base/resource/resource_bundle.h" |
| 32 | #include "ui/gfx/image/image.h" |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 33 | #include "ui/gfx/image/image_unittest_util.h" |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 34 | |
[email protected] | 6778fed | 2013-12-24 20:09:37 | [diff] [blame] | 35 | using base::ASCIIToUTF16; |
[email protected] | f729d7a | 2013-12-26 07:07:56 | [diff] [blame] | 36 | using base::UTF8ToUTF16; |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 37 | using content::BrowserThread; |
| 38 | |
[email protected] | 6a46066 | 2011-12-22 22:05:16 | [diff] [blame] | 39 | ProfileNameVerifierObserver::ProfileNameVerifierObserver( |
| 40 | TestingProfileManager* testing_profile_manager) |
| 41 | : testing_profile_manager_(testing_profile_manager) { |
| 42 | DCHECK(testing_profile_manager_); |
[email protected] | 590e189b | 2011-12-13 22:07:03 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | ProfileNameVerifierObserver::~ProfileNameVerifierObserver() { |
| 46 | } |
| 47 | |
| 48 | void ProfileNameVerifierObserver::OnProfileAdded( |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 49 | const base::FilePath& profile_path) { |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 50 | base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex( |
[email protected] | 6a46066 | 2011-12-22 22:05:16 | [diff] [blame] | 51 | GetCache()->GetIndexOfProfileWithPath(profile_path)); |
[email protected] | 590e189b | 2011-12-13 22:07:03 | [diff] [blame] | 52 | EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end()); |
| 53 | profile_names_.insert(profile_name); |
| 54 | } |
| 55 | |
[email protected] | 7b0af15 | 2011-12-16 17:02:06 | [diff] [blame] | 56 | void ProfileNameVerifierObserver::OnProfileWillBeRemoved( |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 57 | const base::FilePath& profile_path) { |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 58 | base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex( |
[email protected] | 6a46066 | 2011-12-22 22:05:16 | [diff] [blame] | 59 | GetCache()->GetIndexOfProfileWithPath(profile_path)); |
[email protected] | 590e189b | 2011-12-13 22:07:03 | [diff] [blame] | 60 | EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end()); |
| 61 | profile_names_.erase(profile_name); |
| 62 | } |
| 63 | |
[email protected] | 7b0af15 | 2011-12-16 17:02:06 | [diff] [blame] | 64 | void ProfileNameVerifierObserver::OnProfileWasRemoved( |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 65 | const base::FilePath& profile_path, |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 66 | const base::string16& profile_name) { |
[email protected] | 7b0af15 | 2011-12-16 17:02:06 | [diff] [blame] | 67 | EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end()); |
| 68 | } |
| 69 | |
[email protected] | 590e189b | 2011-12-13 22:07:03 | [diff] [blame] | 70 | void ProfileNameVerifierObserver::OnProfileNameChanged( |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 71 | const base::FilePath& profile_path, |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 72 | const base::string16& old_profile_name) { |
| 73 | base::string16 new_profile_name = GetCache()->GetNameOfProfileAtIndex( |
[email protected] | 6a46066 | 2011-12-22 22:05:16 | [diff] [blame] | 74 | GetCache()->GetIndexOfProfileWithPath(profile_path)); |
[email protected] | 590e189b | 2011-12-13 22:07:03 | [diff] [blame] | 75 | EXPECT_TRUE(profile_names_.find(old_profile_name) != profile_names_.end()); |
| 76 | EXPECT_TRUE(profile_names_.find(new_profile_name) == profile_names_.end()); |
| 77 | profile_names_.erase(old_profile_name); |
| 78 | profile_names_.insert(new_profile_name); |
| 79 | } |
| 80 | |
| 81 | void ProfileNameVerifierObserver::OnProfileAvatarChanged( |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 82 | const base::FilePath& profile_path) { |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 83 | base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex( |
[email protected] | 6a46066 | 2011-12-22 22:05:16 | [diff] [blame] | 84 | GetCache()->GetIndexOfProfileWithPath(profile_path)); |
[email protected] | 590e189b | 2011-12-13 22:07:03 | [diff] [blame] | 85 | EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end()); |
| 86 | } |
| 87 | |
[email protected] | 6a46066 | 2011-12-22 22:05:16 | [diff] [blame] | 88 | ProfileInfoCache* ProfileNameVerifierObserver::GetCache() { |
| 89 | return testing_profile_manager_->profile_info_cache(); |
| 90 | } |
| 91 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 92 | ProfileInfoCacheTest::ProfileInfoCacheTest() |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 93 | : testing_profile_manager_(TestingBrowserProcess::GetGlobal()), |
[email protected] | 6a46066 | 2011-12-22 22:05:16 | [diff] [blame] | 94 | name_observer_(&testing_profile_manager_) { |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | ProfileInfoCacheTest::~ProfileInfoCacheTest() { |
| 98 | } |
| 99 | |
| 100 | void ProfileInfoCacheTest::SetUp() { |
| 101 | ASSERT_TRUE(testing_profile_manager_.SetUp()); |
[email protected] | 590e189b | 2011-12-13 22:07:03 | [diff] [blame] | 102 | testing_profile_manager_.profile_info_cache()->AddObserver(&name_observer_); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 103 | } |
| 104 | |
[email protected] | 1302dcf | 2011-11-30 21:47:05 | [diff] [blame] | 105 | void ProfileInfoCacheTest::TearDown() { |
| 106 | // Drain the UI thread to make sure all tasks are completed. This prevents |
| 107 | // memory leaks. |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 108 | base::RunLoop().RunUntilIdle(); |
[email protected] | 1302dcf | 2011-11-30 21:47:05 | [diff] [blame] | 109 | } |
| 110 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 111 | ProfileInfoCache* ProfileInfoCacheTest::GetCache() { |
| 112 | return testing_profile_manager_.profile_info_cache(); |
| 113 | } |
| 114 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 115 | base::FilePath ProfileInfoCacheTest::GetProfilePath( |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 116 | const std::string& base_name) { |
| 117 | return testing_profile_manager_.profile_manager()->user_data_dir(). |
| 118 | AppendASCII(base_name); |
| 119 | } |
| 120 | |
| 121 | void ProfileInfoCacheTest::ResetCache() { |
| 122 | testing_profile_manager_.DeleteProfileInfoCache(); |
| 123 | } |
| 124 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 125 | TEST_F(ProfileInfoCacheTest, AddProfiles) { |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 126 | EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles()); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 127 | |
[email protected] | ca59107 | 2012-03-27 01:54:44 | [diff] [blame] | 128 | ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 129 | for (uint32 i = 0; i < 4; ++i) { |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 130 | base::FilePath profile_path = |
| 131 | GetProfilePath(base::StringPrintf("path_%ud", i)); |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 132 | base::string16 profile_name = |
| 133 | ASCIIToUTF16(base::StringPrintf("name_%ud", i)); |
[email protected] | ca59107 | 2012-03-27 01:54:44 | [diff] [blame] | 134 | const SkBitmap* icon = rb.GetImageNamed( |
[email protected] | c3e55977 | 2014-04-09 04:02:54 | [diff] [blame] | 135 | profiles::GetDefaultAvatarIconResourceIDAtIndex( |
[email protected] | ca59107 | 2012-03-27 01:54:44 | [diff] [blame] | 136 | i)).ToSkBitmap(); |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 137 | std::string supervised_user_id = i == 3 ? "TEST_ID" : ""; |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 138 | |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 139 | GetCache()->AddProfileToCache(profile_path, profile_name, base::string16(), |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 140 | i, supervised_user_id); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 141 | GetCache()->SetBackgroundStatusOfProfileAtIndex(i, true); |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 142 | base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i)); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 143 | GetCache()->SetGAIANameOfProfileAtIndex(i, gaia_name); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 144 | |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 145 | EXPECT_EQ(i + 1, GetCache()->GetNumberOfProfiles()); |
| 146 | EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i)); |
| 147 | EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i)); |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 148 | const SkBitmap* actual_icon = |
| 149 | GetCache()->GetAvatarIconOfProfileAtIndex(i).ToSkBitmap(); |
[email protected] | ca59107 | 2012-03-27 01:54:44 | [diff] [blame] | 150 | EXPECT_EQ(icon->width(), actual_icon->width()); |
| 151 | EXPECT_EQ(icon->height(), actual_icon->height()); |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 152 | EXPECT_EQ(i == 3, GetCache()->ProfileIsSupervisedAtIndex(i)); |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 153 | EXPECT_EQ(i == 3, GetCache()->IsOmittedProfileAtIndex(i)); |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 154 | EXPECT_EQ(supervised_user_id, |
| 155 | GetCache()->GetSupervisedUserIdOfProfileAtIndex(i)); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 156 | } |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 157 | |
| 158 | // Reset the cache and test the it reloads correctly. |
| 159 | ResetCache(); |
| 160 | |
| 161 | EXPECT_EQ(4u, GetCache()->GetNumberOfProfiles()); |
| 162 | for (uint32 i = 0; i < 4; ++i) { |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 163 | base::FilePath profile_path = |
| 164 | GetProfilePath(base::StringPrintf("path_%ud", i)); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 165 | EXPECT_EQ(i, GetCache()->GetIndexOfProfileWithPath(profile_path)); |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 166 | base::string16 profile_name = |
| 167 | ASCIIToUTF16(base::StringPrintf("name_%ud", i)); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 168 | EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i)); |
| 169 | EXPECT_EQ(i, GetCache()->GetAvatarIconIndexOfProfileAtIndex(i)); |
| 170 | EXPECT_EQ(true, GetCache()->GetBackgroundStatusOfProfileAtIndex(i)); |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 171 | base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i)); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 172 | EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(i)); |
| 173 | } |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 174 | } |
| 175 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 176 | TEST_F(ProfileInfoCacheTest, DeleteProfile) { |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 177 | EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles()); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 178 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 179 | base::FilePath path_1 = GetProfilePath("path_1"); |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 180 | GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("name_1"), |
| 181 | base::string16(), 0, std::string()); |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 182 | EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles()); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 183 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 184 | base::FilePath path_2 = GetProfilePath("path_2"); |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 185 | base::string16 name_2 = ASCIIToUTF16("name_2"); |
| 186 | GetCache()->AddProfileToCache(path_2, name_2, base::string16(), 0, |
| 187 | std::string()); |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 188 | EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles()); |
[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_1); |
| 191 | EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles()); |
| 192 | EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(0)); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 193 | |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 194 | GetCache()->DeleteProfileFromCache(path_2); |
| 195 | EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles()); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 196 | } |
| 197 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 198 | TEST_F(ProfileInfoCacheTest, MutateProfile) { |
| 199 | GetCache()->AddProfileToCache( |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 200 | GetProfilePath("path_1"), ASCIIToUTF16("name_1"), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 201 | base::string16(), 0, std::string()); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 202 | GetCache()->AddProfileToCache( |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 203 | GetProfilePath("path_2"), ASCIIToUTF16("name_2"), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 204 | base::string16(), 0, std::string()); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 205 | |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 206 | base::string16 new_name = ASCIIToUTF16("new_name"); |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 207 | GetCache()->SetNameOfProfileAtIndex(1, new_name); |
| 208 | EXPECT_EQ(new_name, GetCache()->GetNameOfProfileAtIndex(1)); |
| 209 | EXPECT_NE(new_name, GetCache()->GetNameOfProfileAtIndex(0)); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 210 | |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 211 | base::string16 new_user_name = ASCIIToUTF16("user_name"); |
[email protected] | e8e7809 | 2011-09-29 18:15:38 | [diff] [blame] | 212 | GetCache()->SetUserNameOfProfileAtIndex(1, new_user_name); |
| 213 | EXPECT_EQ(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(1)); |
| 214 | EXPECT_NE(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(0)); |
| 215 | |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 216 | size_t new_icon_index = 3; |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 217 | GetCache()->SetAvatarIconOfProfileAtIndex(1, new_icon_index); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 218 | // Not much to test. |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 219 | GetCache()->GetAvatarIconOfProfileAtIndex(1); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 220 | } |
| 221 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 222 | TEST_F(ProfileInfoCacheTest, Sort) { |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 223 | base::string16 name_a = ASCIIToUTF16("apple"); |
[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_a"), name_a, base::string16(), 0, std::string()); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 226 | |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 227 | base::string16 name_c = ASCIIToUTF16("cat"); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 228 | GetCache()->AddProfileToCache( |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 229 | GetProfilePath("path_c"), name_c, base::string16(), 0, std::string()); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 230 | |
| 231 | // Sanity check the initial order. |
| 232 | EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0)); |
| 233 | EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1)); |
| 234 | |
| 235 | // Add a new profile (start with a capital to test case insensitive sorting. |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 236 | base::string16 name_b = ASCIIToUTF16("Banana"); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 237 | GetCache()->AddProfileToCache( |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 238 | GetProfilePath("path_b"), name_b, base::string16(), 0, std::string()); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 239 | |
| 240 | // Verify the new order. |
| 241 | EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0)); |
| 242 | EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(1)); |
| 243 | EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(2)); |
| 244 | |
| 245 | // Change the name of an existing profile. |
| 246 | name_a = UTF8ToUTF16("dog"); |
| 247 | GetCache()->SetNameOfProfileAtIndex(0, name_a); |
| 248 | |
| 249 | // Verify the new order. |
| 250 | EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0)); |
| 251 | EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1)); |
| 252 | EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(2)); |
| 253 | |
| 254 | // Delete a profile. |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 255 | GetCache()->DeleteProfileFromCache(GetProfilePath("path_c")); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 256 | |
| 257 | // Verify the new order. |
| 258 | EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0)); |
| 259 | EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(1)); |
| 260 | } |
| 261 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 262 | TEST_F(ProfileInfoCacheTest, BackgroundModeStatus) { |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 263 | GetCache()->AddProfileToCache( |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 264 | GetProfilePath("path_1"), ASCIIToUTF16("name_1"), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 265 | base::string16(), 0, std::string()); |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 266 | GetCache()->AddProfileToCache( |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 267 | GetProfilePath("path_2"), ASCIIToUTF16("name_2"), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 268 | base::string16(), 0, std::string()); |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 269 | |
| 270 | EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0)); |
| 271 | EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1)); |
| 272 | |
| 273 | GetCache()->SetBackgroundStatusOfProfileAtIndex(1, true); |
| 274 | |
| 275 | EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0)); |
| 276 | EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1)); |
| 277 | |
| 278 | GetCache()->SetBackgroundStatusOfProfileAtIndex(0, true); |
| 279 | |
| 280 | EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0)); |
| 281 | EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1)); |
| 282 | |
| 283 | GetCache()->SetBackgroundStatusOfProfileAtIndex(1, false); |
| 284 | |
| 285 | EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0)); |
| 286 | EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1)); |
| 287 | } |
| 288 | |
[email protected] | 85503a0 | 2014-01-16 17:27:22 | [diff] [blame] | 289 | TEST_F(ProfileInfoCacheTest, ProfileActiveTime) { |
| 290 | GetCache()->AddProfileToCache( |
| 291 | GetProfilePath("path_1"), ASCIIToUTF16("name_1"), |
| 292 | base::string16(), 0, std::string()); |
| 293 | EXPECT_EQ(base::Time(), GetCache()->GetProfileActiveTimeAtIndex(0)); |
[email protected] | 87727e0 | 2014-02-27 18:16:53 | [diff] [blame] | 294 | // Before & After times are artificially shifted because just relying upon |
| 295 | // the system time can yield problems due to inaccuracies in the |
| 296 | // underlying storage system (which uses a double with only 52 bits of |
| 297 | // precision to store the 64-bit "time" number). http://crbug.com/346827 |
[email protected] | 85503a0 | 2014-01-16 17:27:22 | [diff] [blame] | 298 | base::Time before = base::Time::Now(); |
[email protected] | 87727e0 | 2014-02-27 18:16:53 | [diff] [blame] | 299 | before -= base::TimeDelta::FromSeconds(1); |
[email protected] | 85503a0 | 2014-01-16 17:27:22 | [diff] [blame] | 300 | GetCache()->SetProfileActiveTimeAtIndex(0); |
| 301 | base::Time after = base::Time::Now(); |
[email protected] | 87727e0 | 2014-02-27 18:16:53 | [diff] [blame] | 302 | after += base::TimeDelta::FromSeconds(1); |
[email protected] | 85503a0 | 2014-01-16 17:27:22 | [diff] [blame] | 303 | EXPECT_LE(before, GetCache()->GetProfileActiveTimeAtIndex(0)); |
| 304 | EXPECT_GE(after, GetCache()->GetProfileActiveTimeAtIndex(0)); |
| 305 | } |
| 306 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 307 | TEST_F(ProfileInfoCacheTest, GAIAName) { |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 308 | GetCache()->AddProfileToCache( |
[email protected] | a4352d0 | 2014-03-06 06:00:27 | [diff] [blame] | 309 | GetProfilePath("path_1"), ASCIIToUTF16("Person 1"), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 310 | base::string16(), 0, std::string()); |
[email protected] | a4352d0 | 2014-03-06 06:00:27 | [diff] [blame] | 311 | base::string16 profile_name(ASCIIToUTF16("Person 2")); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 312 | GetCache()->AddProfileToCache( |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 313 | GetProfilePath("path_2"), profile_name, base::string16(), 0, |
| 314 | std::string()); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 315 | |
[email protected] | a4352d0 | 2014-03-06 06:00:27 | [diff] [blame] | 316 | int index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1")); |
| 317 | int index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2")); |
| 318 | |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 319 | // Sanity check. |
[email protected] | a4352d0 | 2014-03-06 06:00:27 | [diff] [blame] | 320 | EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty()); |
| 321 | EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index2).empty()); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 322 | |
[email protected] | 425d6ab | 2014-03-25 05:59:39 | [diff] [blame] | 323 | // Set GAIA name. This re-sorts the cache. |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 324 | base::string16 gaia_name(ASCIIToUTF16("Pat Smith")); |
[email protected] | a4352d0 | 2014-03-06 06:00:27 | [diff] [blame] | 325 | GetCache()->SetGAIANameOfProfileAtIndex(index2, gaia_name); |
[email protected] | 425d6ab | 2014-03-25 05:59:39 | [diff] [blame] | 326 | index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1")); |
| 327 | index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2")); |
| 328 | |
| 329 | // Since there is a GAIA name, we use that as a display name. |
[email protected] | a4352d0 | 2014-03-06 06:00:27 | [diff] [blame] | 330 | EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty()); |
| 331 | EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2)); |
[email protected] | a4352d0 | 2014-03-06 06:00:27 | [diff] [blame] | 332 | EXPECT_EQ(gaia_name, GetCache()->GetNameOfProfileAtIndex(index2)); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 333 | |
[email protected] | a4352d0 | 2014-03-06 06:00:27 | [diff] [blame] | 334 | // Don't use GAIA name as profile name. This re-sorts the cache. |
[email protected] | 425d6ab | 2014-03-25 05:59:39 | [diff] [blame] | 335 | base::string16 custom_name(ASCIIToUTF16("Custom name")); |
| 336 | GetCache()->SetNameOfProfileAtIndex(index2, custom_name); |
[email protected] | 1cdf093 | 2014-08-16 06:00:49 | [diff] [blame] | 337 | GetCache()->SetProfileIsUsingDefaultNameAtIndex(index2, false); |
| 338 | |
[email protected] | a4352d0 | 2014-03-06 06:00:27 | [diff] [blame] | 339 | index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1")); |
| 340 | index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2")); |
| 341 | |
[email protected] | 425d6ab | 2014-03-25 05:59:39 | [diff] [blame] | 342 | EXPECT_EQ(custom_name, GetCache()->GetNameOfProfileAtIndex(index2)); |
[email protected] | a4352d0 | 2014-03-06 06:00:27 | [diff] [blame] | 343 | EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2)); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 344 | } |
| 345 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 346 | TEST_F(ProfileInfoCacheTest, GAIAPicture) { |
[email protected] | 2290090 | 2014-07-30 11:50:41 | [diff] [blame] | 347 | const int kDefaultAvatarIndex = 0; |
| 348 | const int kOtherAvatarIndex = 1; |
[email protected] | 979dc4f | 2014-08-12 22:52:11 | [diff] [blame] | 349 | const int kGaiaPictureSize = 256; // Standard size of a Gaia account picture. |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 350 | GetCache()->AddProfileToCache( |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 351 | GetProfilePath("path_1"), ASCIIToUTF16("name_1"), |
[email protected] | 2290090 | 2014-07-30 11:50:41 | [diff] [blame] | 352 | base::string16(), kDefaultAvatarIndex, std::string()); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 353 | GetCache()->AddProfileToCache( |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 354 | GetProfilePath("path_2"), ASCIIToUTF16("name_2"), |
[email protected] | 2290090 | 2014-07-30 11:50:41 | [diff] [blame] | 355 | base::string16(), kDefaultAvatarIndex, std::string()); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 356 | |
| 357 | // Sanity check. |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 358 | EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0)); |
| 359 | EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(1)); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 360 | EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0)); |
| 361 | EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1)); |
| 362 | |
| 363 | // The profile icon should be the default one. |
[email protected] | 2290090 | 2014-07-30 11:50:41 | [diff] [blame] | 364 | EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(0)); |
| 365 | EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1)); |
| 366 | int default_avatar_id = |
| 367 | profiles::GetDefaultAvatarIconResourceIDAtIndex(kDefaultAvatarIndex); |
| 368 | const gfx::Image& default_avatar_image( |
| 369 | ResourceBundle::GetSharedInstance().GetImageNamed(default_avatar_id)); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 370 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | 2290090 | 2014-07-30 11:50:41 | [diff] [blame] | 371 | default_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1))); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 372 | |
| 373 | // Set GAIA picture. |
[email protected] | 979dc4f | 2014-08-12 22:52:11 | [diff] [blame] | 374 | gfx::Image gaia_image(gfx::test::CreateImage( |
| 375 | kGaiaPictureSize, kGaiaPictureSize)); |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 376 | GetCache()->SetGAIAPictureOfProfileAtIndex(1, &gaia_image); |
| 377 | EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0)); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 378 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 379 | gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1))); |
[email protected] | 2290090 | 2014-07-30 11:50:41 | [diff] [blame] | 380 | // Since we're still using the default avatar, the GAIA image should be |
| 381 | // preferred over the generic avatar image. |
| 382 | EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1)); |
| 383 | EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1)); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 384 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | 2290090 | 2014-07-30 11:50:41 | [diff] [blame] | 385 | gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1))); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 386 | |
[email protected] | 1cdf093 | 2014-08-16 06:00:49 | [diff] [blame] | 387 | // Set a non-default avatar. This should be preferred over the GAIA image. |
[email protected] | 2290090 | 2014-07-30 11:50:41 | [diff] [blame] | 388 | GetCache()->SetAvatarIconOfProfileAtIndex(1, kOtherAvatarIndex); |
[email protected] | 1cdf093 | 2014-08-16 06:00:49 | [diff] [blame] | 389 | GetCache()->SetProfileIsUsingDefaultAvatarAtIndex(1, false); |
[email protected] | 2290090 | 2014-07-30 11:50:41 | [diff] [blame] | 390 | EXPECT_FALSE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1)); |
| 391 | EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1)); |
| 392 | int other_avatar_id = |
| 393 | profiles::GetDefaultAvatarIconResourceIDAtIndex(kOtherAvatarIndex); |
| 394 | const gfx::Image& other_avatar_image( |
| 395 | ResourceBundle::GetSharedInstance().GetImageNamed(other_avatar_id)); |
| 396 | EXPECT_TRUE(gfx::test::IsEqual( |
| 397 | other_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1))); |
| 398 | |
| 399 | // Explicitly setting the GAIA picture should make it preferred again. |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 400 | GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, true); |
[email protected] | 2290090 | 2014-07-30 11:50:41 | [diff] [blame] | 401 | EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1)); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 402 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 403 | gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1))); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 404 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 405 | gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1))); |
| 406 | |
[email protected] | 2290090 | 2014-07-30 11:50:41 | [diff] [blame] | 407 | // Clearing the IsUsingGAIAPicture flag should result in the generic image |
| 408 | // being used again. |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 409 | GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, false); |
[email protected] | 2290090 | 2014-07-30 11:50:41 | [diff] [blame] | 410 | EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1)); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 411 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 412 | gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1))); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 413 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | 2290090 | 2014-07-30 11:50:41 | [diff] [blame] | 414 | other_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1))); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 415 | } |
| 416 | |
[email protected] | 754bebc | 2011-12-01 16:42:16 | [diff] [blame] | 417 | TEST_F(ProfileInfoCacheTest, PersistGAIAPicture) { |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 418 | GetCache()->AddProfileToCache( |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 419 | GetProfilePath("path_1"), ASCIIToUTF16("name_1"), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 420 | base::string16(), 0, std::string()); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 421 | gfx::Image gaia_image(gfx::test::CreateImage()); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 422 | |
[email protected] | a7fe911 | 2012-07-20 02:34:45 | [diff] [blame] | 423 | content::WindowedNotificationObserver save_observer( |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 424 | chrome::NOTIFICATION_PROFILE_CACHE_PICTURE_SAVED, |
| 425 | content::NotificationService::AllSources()); |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 426 | GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_image); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 427 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 428 | gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0))); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 429 | |
| 430 | // Wait for the file to be written to disk then reset the cache. |
| 431 | save_observer.Wait(); |
| 432 | ResetCache(); |
| 433 | |
| 434 | // Try to get the GAIA picture. This should return NULL until the read from |
| 435 | // disk is done. |
[email protected] | a7fe911 | 2012-07-20 02:34:45 | [diff] [blame] | 436 | content::WindowedNotificationObserver read_observer( |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 437 | chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
| 438 | content::NotificationService::AllSources()); |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 439 | EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0)); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 440 | read_observer.Wait(); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 441 | EXPECT_TRUE(gfx::test::IsEqual( |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 442 | gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0))); |
| 443 | } |
| 444 | |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 445 | TEST_F(ProfileInfoCacheTest, SetSupervisedUserId) { |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 446 | GetCache()->AddProfileToCache( |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 447 | GetProfilePath("test"), ASCIIToUTF16("Test"), |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 448 | base::string16(), 0, std::string()); |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 449 | EXPECT_FALSE(GetCache()->ProfileIsSupervisedAtIndex(0)); |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 450 | |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 451 | GetCache()->SetSupervisedUserIdOfProfileAtIndex(0, "TEST_ID"); |
| 452 | EXPECT_TRUE(GetCache()->ProfileIsSupervisedAtIndex(0)); |
| 453 | EXPECT_EQ("TEST_ID", GetCache()->GetSupervisedUserIdOfProfileAtIndex(0)); |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 454 | |
| 455 | ResetCache(); |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 456 | EXPECT_TRUE(GetCache()->ProfileIsSupervisedAtIndex(0)); |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 457 | |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 458 | GetCache()->SetSupervisedUserIdOfProfileAtIndex(0, std::string()); |
| 459 | EXPECT_FALSE(GetCache()->ProfileIsSupervisedAtIndex(0)); |
| 460 | EXPECT_EQ("", GetCache()->GetSupervisedUserIdOfProfileAtIndex(0)); |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 461 | } |
| 462 | |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 463 | TEST_F(ProfileInfoCacheTest, EmptyGAIAInfo) { |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 464 | base::string16 profile_name = ASCIIToUTF16("name_1"); |
[email protected] | c3e55977 | 2014-04-09 04:02:54 | [diff] [blame] | 465 | int id = profiles::GetDefaultAvatarIconResourceIDAtIndex(0); |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 466 | const gfx::Image& profile_image( |
| 467 | ResourceBundle::GetSharedInstance().GetImageNamed(id)); |
| 468 | |
| 469 | GetCache()->AddProfileToCache( |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 470 | GetProfilePath("path_1"), profile_name, base::string16(), 0, |
| 471 | std::string()); |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 472 | |
| 473 | // Set empty GAIA info. |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 474 | GetCache()->SetGAIANameOfProfileAtIndex(0, base::string16()); |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 475 | GetCache()->SetGAIAPictureOfProfileAtIndex(0, NULL); |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 476 | GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(0, true); |
| 477 | |
| 478 | // Verify that the profile name and picture are not empty. |
| 479 | EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(0)); |
| 480 | EXPECT_TRUE(gfx::test::IsEqual( |
| 481 | profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(0))); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 482 | } |
| 483 | |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 484 | TEST_F(ProfileInfoCacheTest, CreateSupervisedTestingProfile) { |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 485 | testing_profile_manager_.CreateTestingProfile("default"); |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 486 | base::string16 supervised_user_name = ASCIIToUTF16("Supervised User"); |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 487 | testing_profile_manager_.CreateTestingProfile( |
[email protected] | a6e01b4 | 2013-08-05 14:13:13 | [diff] [blame] | 488 | "test1", scoped_ptr<PrefServiceSyncable>(), |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 489 | supervised_user_name, 0, "TEST_ID", TestingProfile::TestingFactories()); |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 490 | for (size_t i = 0; i < GetCache()->GetNumberOfProfiles(); i++) { |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 491 | bool is_supervised = |
| 492 | GetCache()->GetNameOfProfileAtIndex(i) == supervised_user_name; |
| 493 | EXPECT_EQ(is_supervised, GetCache()->ProfileIsSupervisedAtIndex(i)); |
| 494 | std::string supervised_user_id = is_supervised ? "TEST_ID" : ""; |
| 495 | EXPECT_EQ(supervised_user_id, |
| 496 | GetCache()->GetSupervisedUserIdOfProfileAtIndex(i)); |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 497 | } |
[email protected] | 371662e37 | 2013-10-17 22:05:22 | [diff] [blame] | 498 | |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 499 | // Supervised profiles have a custom theme, which needs to be deleted on the |
| 500 | // FILE thread. Reset the profile manager now so everything is deleted while |
| 501 | // we still have a FILE thread. |
[email protected] | 371662e37 | 2013-10-17 22:05:22 | [diff] [blame] | 502 | TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL); |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 503 | } |
| 504 | |
[email protected] | 04748180 | 2013-09-16 22:26:38 | [diff] [blame] | 505 | TEST_F(ProfileInfoCacheTest, AddStubProfile) { |
| 506 | EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles()); |
| 507 | |
| 508 | // Add some profiles with and without a '.' in their paths. |
| 509 | const struct { |
| 510 | const char* profile_path; |
| 511 | const char* profile_name; |
| 512 | } kTestCases[] = { |
| 513 | { "path.test0", "name_0" }, |
| 514 | { "path_test1", "name_1" }, |
| 515 | { "path.test2", "name_2" }, |
| 516 | { "path_test3", "name_3" }, |
| 517 | }; |
| 518 | |
viettrungluu | 9e65ad1 | 2014-10-16 04:22:26 | [diff] [blame] | 519 | for (size_t i = 0; i < arraysize(kTestCases); ++i) { |
[email protected] | 04748180 | 2013-09-16 22:26:38 | [diff] [blame] | 520 | base::FilePath profile_path = GetProfilePath(kTestCases[i].profile_path); |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 521 | base::string16 profile_name = ASCIIToUTF16(kTestCases[i].profile_name); |
[email protected] | 04748180 | 2013-09-16 22:26:38 | [diff] [blame] | 522 | |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 523 | GetCache()->AddProfileToCache(profile_path, profile_name, base::string16(), |
| 524 | i, ""); |
[email protected] | 04748180 | 2013-09-16 22:26:38 | [diff] [blame] | 525 | |
| 526 | EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i)); |
| 527 | EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i)); |
| 528 | } |
| 529 | |
| 530 | ASSERT_EQ(4U, GetCache()->GetNumberOfProfiles()); |
| 531 | |
| 532 | // Check that the profiles can be extracted from the local state. |
[email protected] | d2065e06 | 2013-12-12 23:49:52 | [diff] [blame] | 533 | std::vector<base::string16> names = ProfileInfoCache::GetProfileNames(); |
[email protected] | 04748180 | 2013-09-16 22:26:38 | [diff] [blame] | 534 | for (size_t i = 0; i < 4; i++) |
| 535 | ASSERT_FALSE(names[i].empty()); |
| 536 | } |
| 537 | |
[email protected] | b09bfa1 | 2014-08-15 19:05:25 | [diff] [blame] | 538 | // High res avatar downloading is only supported on desktop. |
| 539 | #if !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_CHROMEOS) |
[email protected] | 9de4430 | 2014-05-01 16:32:27 | [diff] [blame] | 540 | TEST_F(ProfileInfoCacheTest, DownloadHighResAvatarTest) { |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 541 | switches::EnableNewAvatarMenuForTesting( |
| 542 | base::CommandLine::ForCurrentProcess()); |
[email protected] | 5cf8bcd9 | 2014-07-17 20:27:40 | [diff] [blame] | 543 | |
[email protected] | 9de4430 | 2014-05-01 16:32:27 | [diff] [blame] | 544 | EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles()); |
| 545 | base::FilePath path_1 = GetProfilePath("path_1"); |
| 546 | GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("name_1"), |
| 547 | base::string16(), 0, std::string()); |
| 548 | EXPECT_EQ(1U, GetCache()->GetNumberOfProfiles()); |
noms | 28693ab2 | 2015-01-28 21:44:54 | [diff] [blame] | 549 | base::RunLoop().RunUntilIdle(); |
[email protected] | 9de4430 | 2014-05-01 16:32:27 | [diff] [blame] | 550 | |
| 551 | // We haven't downloaded any high-res avatars yet. |
| 552 | EXPECT_EQ(0U, GetCache()->cached_avatar_images_.size()); |
[email protected] | 5cf8bcd9 | 2014-07-17 20:27:40 | [diff] [blame] | 553 | |
| 554 | // After adding a new profile, the download of high-res avatar will be |
| 555 | // triggered if the flag kNewAvatarMenu has been set. But the downloader |
| 556 | // won't ever call OnFetchComplete in the test. |
| 557 | EXPECT_EQ(1U, GetCache()->avatar_images_downloads_in_progress_.size()); |
| 558 | |
[email protected] | 9de4430 | 2014-05-01 16:32:27 | [diff] [blame] | 559 | EXPECT_FALSE(GetCache()->GetHighResAvatarOfProfileAtIndex(0)); |
| 560 | |
| 561 | // Simulate downloading a high-res avatar. |
| 562 | const size_t kIconIndex = 0; |
[email protected] | 2d60571 | 2014-05-14 12:03:07 | [diff] [blame] | 563 | ProfileAvatarDownloader avatar_downloader( |
| 564 | kIconIndex, GetCache()->GetPathOfProfileAtIndex(0), GetCache()); |
[email protected] | 9de4430 | 2014-05-01 16:32:27 | [diff] [blame] | 565 | |
| 566 | // Put a real bitmap into "bitmap". 2x2 bitmap of green 32 bit pixels. |
| 567 | SkBitmap bitmap; |
[email protected] | e61cad5 | 2014-07-01 21:01:57 | [diff] [blame] | 568 | bitmap.allocN32Pixels(2, 2); |
[email protected] | 9de4430 | 2014-05-01 16:32:27 | [diff] [blame] | 569 | bitmap.eraseColor(SK_ColorGREEN); |
| 570 | |
| 571 | avatar_downloader.OnFetchComplete( |
| 572 | GURL("http://www.google.com/avatar.png"), &bitmap); |
| 573 | |
treib | af25550 | 2015-01-30 17:47:53 | [diff] [blame^] | 574 | // Now the download should not be in progress anymore. |
| 575 | EXPECT_EQ(0U, GetCache()->avatar_images_downloads_in_progress_.size()); |
| 576 | |
[email protected] | 9de4430 | 2014-05-01 16:32:27 | [diff] [blame] | 577 | std::string file_name = |
| 578 | profiles::GetDefaultAvatarIconFileNameAtIndex(kIconIndex); |
| 579 | |
| 580 | // The file should have been cached and saved. |
[email protected] | 9de4430 | 2014-05-01 16:32:27 | [diff] [blame] | 581 | EXPECT_EQ(1U, GetCache()->cached_avatar_images_.size()); |
| 582 | EXPECT_TRUE(GetCache()->GetHighResAvatarOfProfileAtIndex(0)); |
| 583 | EXPECT_EQ(GetCache()->cached_avatar_images_[file_name], |
| 584 | GetCache()->GetHighResAvatarOfProfileAtIndex(0)); |
| 585 | |
| 586 | // Make sure everything has completed, and the file has been written to disk. |
| 587 | base::RunLoop().RunUntilIdle(); |
| 588 | |
| 589 | // Clean up. |
| 590 | base::FilePath icon_path = |
| 591 | profiles::GetPathOfHighResAvatarAtIndex(kIconIndex); |
| 592 | EXPECT_NE(std::string::npos, icon_path.MaybeAsASCII().find(file_name)); |
| 593 | EXPECT_TRUE(base::PathExists(icon_path)); |
| 594 | EXPECT_TRUE(base::DeleteFile(icon_path, true)); |
| 595 | EXPECT_FALSE(base::PathExists(icon_path)); |
| 596 | } |
noms | be073e5 | 2014-08-23 22:01:04 | [diff] [blame] | 597 | |
| 598 | TEST_F(ProfileInfoCacheTest, MigrateLegacyProfileNamesWithNewAvatarMenu) { |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 599 | switches::EnableNewAvatarMenuForTesting( |
| 600 | base::CommandLine::ForCurrentProcess()); |
noms | be073e5 | 2014-08-23 22:01:04 | [diff] [blame] | 601 | EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles()); |
| 602 | |
| 603 | base::FilePath path_1 = GetProfilePath("path_1"); |
| 604 | GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("Default Profile"), |
| 605 | base::string16(), 0, std::string()); |
| 606 | base::FilePath path_2 = GetProfilePath("path_2"); |
| 607 | GetCache()->AddProfileToCache(path_2, ASCIIToUTF16("First user"), |
| 608 | base::string16(), 1, std::string()); |
| 609 | base::string16 name_3 = ASCIIToUTF16("Lemonade"); |
| 610 | base::FilePath path_3 = GetProfilePath("path_3"); |
| 611 | GetCache()->AddProfileToCache(path_3, name_3, |
| 612 | base::string16(), 2, std::string()); |
| 613 | base::string16 name_4 = ASCIIToUTF16("Batman"); |
| 614 | base::FilePath path_4 = GetProfilePath("path_4"); |
| 615 | GetCache()->AddProfileToCache(path_4, name_4, |
| 616 | base::string16(), 3, std::string()); |
| 617 | base::string16 name_5 = ASCIIToUTF16("Person 2"); |
| 618 | base::FilePath path_5 = GetProfilePath("path_5"); |
| 619 | GetCache()->AddProfileToCache(path_5, name_5, |
| 620 | base::string16(), 2, std::string()); |
| 621 | |
| 622 | EXPECT_EQ(5U, GetCache()->GetNumberOfProfiles()); |
| 623 | |
| 624 | |
| 625 | ResetCache(); |
| 626 | |
| 627 | // Legacy profile names like "Default Profile" and "First user" should be |
| 628 | // migrated to "Person %n" type names. |
| 629 | EXPECT_EQ(ASCIIToUTF16("Person 1"), GetCache()->GetNameOfProfileAtIndex( |
| 630 | GetCache()->GetIndexOfProfileWithPath(path_1))); |
| 631 | EXPECT_EQ(ASCIIToUTF16("Person 3"), GetCache()->GetNameOfProfileAtIndex( |
| 632 | GetCache()->GetIndexOfProfileWithPath(path_2))); |
| 633 | |
| 634 | // Other profile names should not be migrated even if they're the old |
| 635 | // default cartoon profile names. |
| 636 | EXPECT_EQ(name_3, GetCache()->GetNameOfProfileAtIndex( |
| 637 | GetCache()->GetIndexOfProfileWithPath(path_3))); |
| 638 | EXPECT_EQ(name_4, GetCache()->GetNameOfProfileAtIndex( |
| 639 | GetCache()->GetIndexOfProfileWithPath(path_4))); |
| 640 | EXPECT_EQ(name_5, GetCache()->GetNameOfProfileAtIndex( |
| 641 | GetCache()->GetIndexOfProfileWithPath(path_5))); |
| 642 | } |
[email protected] | b09bfa1 | 2014-08-15 19:05:25 | [diff] [blame] | 643 | #endif |
noms | be073e5 | 2014-08-23 22:01:04 | [diff] [blame] | 644 | |
| 645 | TEST_F(ProfileInfoCacheTest, |
| 646 | DontMigrateLegacyProfileNamesWithoutNewAvatarMenu) { |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 647 | switches::DisableNewAvatarMenuForTesting( |
| 648 | base::CommandLine::ForCurrentProcess()); |
treib | 33335ad | 2014-08-27 09:17:19 | [diff] [blame] | 649 | |
noms | be073e5 | 2014-08-23 22:01:04 | [diff] [blame] | 650 | EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles()); |
| 651 | |
| 652 | base::string16 name_1 = ASCIIToUTF16("Default Profile"); |
| 653 | base::FilePath path_1 = GetProfilePath("path_1"); |
| 654 | GetCache()->AddProfileToCache(path_1, name_1, |
| 655 | base::string16(), 0, std::string()); |
| 656 | base::string16 name_2 = ASCIIToUTF16("First user"); |
| 657 | base::FilePath path_2 = GetProfilePath("path_2"); |
| 658 | GetCache()->AddProfileToCache(path_2, name_2, |
| 659 | base::string16(), 1, std::string()); |
| 660 | base::string16 name_3 = ASCIIToUTF16("Lemonade"); |
| 661 | base::FilePath path_3 = GetProfilePath("path_3"); |
| 662 | GetCache()->AddProfileToCache(path_3, name_3, |
| 663 | base::string16(), 2, std::string()); |
| 664 | base::string16 name_4 = ASCIIToUTF16("Batman"); |
| 665 | base::FilePath path_4 = GetProfilePath("path_4"); |
| 666 | GetCache()->AddProfileToCache(path_4, name_4, |
| 667 | base::string16(), 3, std::string()); |
| 668 | EXPECT_EQ(4U, GetCache()->GetNumberOfProfiles()); |
| 669 | |
| 670 | ResetCache(); |
| 671 | |
| 672 | // Profile names should have been preserved. |
| 673 | EXPECT_EQ(name_1, GetCache()->GetNameOfProfileAtIndex( |
| 674 | GetCache()->GetIndexOfProfileWithPath(path_1))); |
| 675 | EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex( |
| 676 | GetCache()->GetIndexOfProfileWithPath(path_2))); |
| 677 | EXPECT_EQ(name_3, GetCache()->GetNameOfProfileAtIndex( |
| 678 | GetCache()->GetIndexOfProfileWithPath(path_3))); |
| 679 | EXPECT_EQ(name_4, GetCache()->GetNameOfProfileAtIndex( |
| 680 | GetCache()->GetIndexOfProfileWithPath(path_4))); |
| 681 | } |