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