[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 | |
avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
[email protected] | 04748180 | 2013-09-16 22:26:38 | [diff] [blame] | 10 | #include <vector> |
| 11 | |
lwchkg | 3f449120 | 2016-03-30 16:49:52 | [diff] [blame] | 12 | #include "base/bind.h" |
[email protected] | 5cf8bcd9 | 2014-07-17 20:27:40 | [diff] [blame] | 13 | #include "base/command_line.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 14 | #include "base/files/file_util.h" |
Avi Drissman | 5f0fb8c | 2018-12-25 23:20:49 | [diff] [blame] | 15 | #include "base/stl_util.h" |
[email protected] | 76fb05c | 2013-06-11 04:38:05 | [diff] [blame] | 16 | #include "base/strings/stringprintf.h" |
[email protected] | e309f31 | 2013-06-07 21:50:08 | [diff] [blame] | 17 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 85503a0 | 2014-01-16 17:27:22 | [diff] [blame] | 18 | #include "base/time/time.h" |
avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 19 | #include "build/build_config.h" |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 20 | #include "chrome/browser/browser_process.h" |
Xi Cheng | 1b76138 | 2017-08-15 20:37:34 | [diff] [blame] | 21 | #include "chrome/browser/profiles/avatar_menu.h" |
[email protected] | c3e55977 | 2014-04-09 04:02:54 | [diff] [blame] | 22 | #include "chrome/browser/profiles/profile_avatar_icon_util.h" |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 23 | #include "chrome/browser/profiles/profile_info_cache.h" |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 24 | #include "chrome/browser/profiles/profile_manager.h" |
[email protected] | 5cf8bcd9 | 2014-07-17 20:27:40 | [diff] [blame] | 25 | #include "chrome/common/chrome_switches.h" |
noms | 693f35aa | 2015-02-05 16:03:16 | [diff] [blame] | 26 | #include "chrome/common/pref_names.h" |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 27 | #include "chrome/test/base/testing_browser_process.h" |
Alexander Alekseev | 3f09a8f | 2018-05-03 02:52:10 | [diff] [blame] | 28 | #include "components/account_id/account_id.h" |
brettw | b1fc1b8 | 2016-02-02 00:19:08 | [diff] [blame] | 29 | #include "components/prefs/testing_pref_service.h" |
Colin Blundell | ac55c997 | 2018-11-07 15:30:33 | [diff] [blame] | 30 | #include "components/signin/core/browser/account_consistency_method.h" |
maxbogue | ea16ff41 | 2016-10-28 16:35:29 | [diff] [blame] | 31 | #include "components/sync_preferences/pref_service_syncable.h" |
[email protected] | 371662e37 | 2013-10-17 22:05:22 | [diff] [blame] | 32 | #include "content/public/test/test_browser_thread_bundle.h" |
[email protected] | a7fe911 | 2012-07-20 02:34:45 | [diff] [blame] | 33 | #include "content/public/test/test_utils.h" |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 34 | #include "third_party/skia/include/core/SkBitmap.h" |
| 35 | #include "ui/base/resource/resource_bundle.h" |
| 36 | #include "ui/gfx/image/image.h" |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 37 | #include "ui/gfx/image/image_unittest_util.h" |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 38 | |
Jochen Eisinger | d84bb42a | 2018-07-23 22:39:03 | [diff] [blame] | 39 | #if BUILDFLAG(ENABLE_SUPERVISED_USERS) |
| 40 | #include "chrome/browser/supervised_user/supervised_user_constants.h" |
| 41 | #endif |
| 42 | |
[email protected] | 6778fed | 2013-12-24 20:09:37 | [diff] [blame] | 43 | using base::ASCIIToUTF16; |
[email protected] | f729d7a | 2013-12-26 07:07:56 | [diff] [blame] | 44 | using base::UTF8ToUTF16; |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 45 | using content::BrowserThread; |
| 46 | |
[email protected] | 6a46066 | 2011-12-22 22:05:16 | [diff] [blame] | 47 | ProfileNameVerifierObserver::ProfileNameVerifierObserver( |
| 48 | TestingProfileManager* testing_profile_manager) |
| 49 | : testing_profile_manager_(testing_profile_manager) { |
| 50 | DCHECK(testing_profile_manager_); |
[email protected] | 590e189b | 2011-12-13 22:07:03 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | ProfileNameVerifierObserver::~ProfileNameVerifierObserver() { |
| 54 | } |
| 55 | |
| 56 | void ProfileNameVerifierObserver::OnProfileAdded( |
[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_.insert(profile_name); |
| 62 | } |
| 63 | |
[email protected] | 7b0af15 | 2011-12-16 17:02:06 | [diff] [blame] | 64 | void ProfileNameVerifierObserver::OnProfileWillBeRemoved( |
[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 | base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex( |
[email protected] | 6a46066 | 2011-12-22 22:05:16 | [diff] [blame] | 67 | GetCache()->GetIndexOfProfileWithPath(profile_path)); |
[email protected] | 590e189b | 2011-12-13 22:07:03 | [diff] [blame] | 68 | EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end()); |
| 69 | profile_names_.erase(profile_name); |
| 70 | } |
| 71 | |
[email protected] | 7b0af15 | 2011-12-16 17:02:06 | [diff] [blame] | 72 | void ProfileNameVerifierObserver::OnProfileWasRemoved( |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 73 | const base::FilePath& profile_path, |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 74 | const base::string16& profile_name) { |
[email protected] | 7b0af15 | 2011-12-16 17:02:06 | [diff] [blame] | 75 | EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end()); |
| 76 | } |
| 77 | |
[email protected] | 590e189b | 2011-12-13 22:07:03 | [diff] [blame] | 78 | void ProfileNameVerifierObserver::OnProfileNameChanged( |
[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 | const base::string16& old_profile_name) { |
| 81 | base::string16 new_profile_name = GetCache()->GetNameOfProfileAtIndex( |
[email protected] | 6a46066 | 2011-12-22 22:05:16 | [diff] [blame] | 82 | GetCache()->GetIndexOfProfileWithPath(profile_path)); |
[email protected] | 590e189b | 2011-12-13 22:07:03 | [diff] [blame] | 83 | EXPECT_TRUE(profile_names_.find(old_profile_name) != profile_names_.end()); |
| 84 | EXPECT_TRUE(profile_names_.find(new_profile_name) == profile_names_.end()); |
| 85 | profile_names_.erase(old_profile_name); |
| 86 | profile_names_.insert(new_profile_name); |
| 87 | } |
| 88 | |
| 89 | void ProfileNameVerifierObserver::OnProfileAvatarChanged( |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 90 | const base::FilePath& profile_path) { |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 91 | base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex( |
[email protected] | 6a46066 | 2011-12-22 22:05:16 | [diff] [blame] | 92 | GetCache()->GetIndexOfProfileWithPath(profile_path)); |
[email protected] | 590e189b | 2011-12-13 22:07:03 | [diff] [blame] | 93 | EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end()); |
| 94 | } |
| 95 | |
[email protected] | 6a46066 | 2011-12-22 22:05:16 | [diff] [blame] | 96 | ProfileInfoCache* ProfileNameVerifierObserver::GetCache() { |
| 97 | return testing_profile_manager_->profile_info_cache(); |
| 98 | } |
| 99 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 100 | ProfileInfoCacheTest::ProfileInfoCacheTest() |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 101 | : testing_profile_manager_(TestingBrowserProcess::GetGlobal()), |
WC Leung | 6c602d4 | 2018-01-23 22:32:18 | [diff] [blame] | 102 | name_observer_(&testing_profile_manager_) {} |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 103 | |
| 104 | ProfileInfoCacheTest::~ProfileInfoCacheTest() { |
| 105 | } |
| 106 | |
| 107 | void ProfileInfoCacheTest::SetUp() { |
| 108 | ASSERT_TRUE(testing_profile_manager_.SetUp()); |
[email protected] | 590e189b | 2011-12-13 22:07:03 | [diff] [blame] | 109 | testing_profile_manager_.profile_info_cache()->AddObserver(&name_observer_); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 110 | } |
| 111 | |
[email protected] | 1302dcf | 2011-11-30 21:47:05 | [diff] [blame] | 112 | void ProfileInfoCacheTest::TearDown() { |
Gabriel Charette | 426214f | 2017-08-14 18:21:58 | [diff] [blame] | 113 | // Drain remaining tasks to make sure all tasks are completed. This prevents |
[email protected] | 1302dcf | 2011-11-30 21:47:05 | [diff] [blame] | 114 | // memory leaks. |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 115 | content::RunAllTasksUntilIdle(); |
[email protected] | 1302dcf | 2011-11-30 21:47:05 | [diff] [blame] | 116 | } |
| 117 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 118 | ProfileInfoCache* ProfileInfoCacheTest::GetCache() { |
| 119 | return testing_profile_manager_.profile_info_cache(); |
| 120 | } |
| 121 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 122 | base::FilePath ProfileInfoCacheTest::GetProfilePath( |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 123 | const std::string& base_name) { |
| 124 | return testing_profile_manager_.profile_manager()->user_data_dir(). |
| 125 | AppendASCII(base_name); |
| 126 | } |
| 127 | |
| 128 | void ProfileInfoCacheTest::ResetCache() { |
| 129 | testing_profile_manager_.DeleteProfileInfoCache(); |
| 130 | } |
| 131 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 132 | TEST_F(ProfileInfoCacheTest, AddProfiles) { |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 133 | EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles()); |
Jacques Chen | cf4971d | 2019-06-25 15:16:29 | [diff] [blame] | 134 | // Avatar icons not used on Android. |
| 135 | #if !defined(OS_ANDROID) |
[email protected] | ca59107 | 2012-03-27 01:54:44 | [diff] [blame] | 136 | ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
Jacques Chen | cf4971d | 2019-06-25 15:16:29 | [diff] [blame] | 137 | #endif |
| 138 | |
avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 139 | for (uint32_t i = 0; i < 4; ++i) { |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 140 | base::FilePath profile_path = |
| 141 | GetProfilePath(base::StringPrintf("path_%ud", i)); |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 142 | base::string16 profile_name = |
| 143 | ASCIIToUTF16(base::StringPrintf("name_%ud", i)); |
Jacques Chen | cf4971d | 2019-06-25 15:16:29 | [diff] [blame] | 144 | #if !defined(OS_ANDROID) |
[email protected] | ca59107 | 2012-03-27 01:54:44 | [diff] [blame] | 145 | const SkBitmap* icon = rb.GetImageNamed( |
[email protected] | c3e55977 | 2014-04-09 04:02:54 | [diff] [blame] | 146 | profiles::GetDefaultAvatarIconResourceIDAtIndex( |
[email protected] | ca59107 | 2012-03-27 01:54:44 | [diff] [blame] | 147 | i)).ToSkBitmap(); |
Jacques Chen | cf4971d | 2019-06-25 15:16:29 | [diff] [blame] | 148 | #endif |
Jochen Eisinger | d84bb42a | 2018-07-23 22:39:03 | [diff] [blame] | 149 | std::string supervised_user_id = ""; |
| 150 | #if BUILDFLAG(ENABLE_SUPERVISED_USERS) |
| 151 | if (i == 3) |
| 152 | supervised_user_id = supervised_users::kChildAccountSUID; |
| 153 | #endif |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 154 | |
rogerta | e2265960 | 2015-05-11 21:58:21 | [diff] [blame] | 155 | GetCache()->AddProfileToCache(profile_path, profile_name, std::string(), |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 156 | base::string16(), i, supervised_user_id, |
| 157 | EmptyAccountId()); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 158 | GetCache()->SetBackgroundStatusOfProfileAtIndex(i, true); |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 159 | base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i)); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 160 | GetCache()->SetGAIANameOfProfileAtIndex(i, gaia_name); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 161 | |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 162 | EXPECT_EQ(i + 1, GetCache()->GetNumberOfProfiles()); |
| 163 | EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i)); |
| 164 | EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i)); |
Jacques Chen | cf4971d | 2019-06-25 15:16:29 | [diff] [blame] | 165 | #if !defined(OS_ANDROID) |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 166 | const SkBitmap* actual_icon = |
| 167 | GetCache()->GetAvatarIconOfProfileAtIndex(i).ToSkBitmap(); |
[email protected] | ca59107 | 2012-03-27 01:54:44 | [diff] [blame] | 168 | EXPECT_EQ(icon->width(), actual_icon->width()); |
| 169 | EXPECT_EQ(icon->height(), actual_icon->height()); |
Jacques Chen | cf4971d | 2019-06-25 15:16:29 | [diff] [blame] | 170 | #endif |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 171 | EXPECT_EQ(i == 3, GetCache()->ProfileIsSupervisedAtIndex(i)); |
[email protected] | 5ddfade | 2014-02-03 10:24:53 | [diff] [blame] | 172 | EXPECT_EQ(i == 3, GetCache()->IsOmittedProfileAtIndex(i)); |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 173 | EXPECT_EQ(supervised_user_id, |
| 174 | GetCache()->GetSupervisedUserIdOfProfileAtIndex(i)); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 175 | } |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 176 | |
| 177 | // Reset the cache and test the it reloads correctly. |
| 178 | ResetCache(); |
| 179 | |
| 180 | EXPECT_EQ(4u, GetCache()->GetNumberOfProfiles()); |
avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 181 | for (uint32_t i = 0; i < 4; ++i) { |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 182 | base::FilePath profile_path = |
| 183 | GetProfilePath(base::StringPrintf("path_%ud", i)); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 184 | EXPECT_EQ(i, GetCache()->GetIndexOfProfileWithPath(profile_path)); |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 185 | base::string16 profile_name = |
| 186 | ASCIIToUTF16(base::StringPrintf("name_%ud", i)); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 187 | EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i)); |
Jacques Chen | cf4971d | 2019-06-25 15:16:29 | [diff] [blame] | 188 | #if !defined(OS_ANDROID) |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 189 | EXPECT_EQ(i, GetCache()->GetAvatarIconIndexOfProfileAtIndex(i)); |
Jacques Chen | cf4971d | 2019-06-25 15:16:29 | [diff] [blame] | 190 | #endif |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 191 | EXPECT_EQ(true, GetCache()->GetBackgroundStatusOfProfileAtIndex(i)); |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 192 | base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i)); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 193 | EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(i)); |
| 194 | } |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 195 | } |
| 196 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 197 | TEST_F(ProfileInfoCacheTest, DeleteProfile) { |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 198 | EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles()); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 199 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 200 | base::FilePath path_1 = GetProfilePath("path_1"); |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 201 | GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("name_1"), std::string(), |
| 202 | base::string16(), 0, std::string(), |
| 203 | EmptyAccountId()); |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 204 | EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles()); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 205 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 206 | base::FilePath path_2 = GetProfilePath("path_2"); |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 207 | base::string16 name_2 = ASCIIToUTF16("name_2"); |
rogerta | e2265960 | 2015-05-11 21:58:21 | [diff] [blame] | 208 | GetCache()->AddProfileToCache(path_2, name_2, std::string(), base::string16(), |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 209 | 0, std::string(), EmptyAccountId()); |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 210 | EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles()); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 211 | |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 212 | GetCache()->DeleteProfileFromCache(path_1); |
| 213 | EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles()); |
| 214 | EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(0)); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 215 | |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 216 | GetCache()->DeleteProfileFromCache(path_2); |
| 217 | EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles()); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 218 | } |
| 219 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 220 | TEST_F(ProfileInfoCacheTest, MutateProfile) { |
| 221 | GetCache()->AddProfileToCache( |
rogerta | e2265960 | 2015-05-11 21:58:21 | [diff] [blame] | 222 | GetProfilePath("path_1"), ASCIIToUTF16("name_1"), std::string(), |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 223 | base::string16(), 0, std::string(), EmptyAccountId()); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 224 | GetCache()->AddProfileToCache( |
rogerta | e2265960 | 2015-05-11 21:58:21 | [diff] [blame] | 225 | GetProfilePath("path_2"), ASCIIToUTF16("name_2"), std::string(), |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 226 | base::string16(), 0, std::string(), EmptyAccountId()); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 227 | |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 228 | base::string16 new_name = ASCIIToUTF16("new_name"); |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 229 | GetCache()->SetNameOfProfileAtIndex(1, new_name); |
| 230 | EXPECT_EQ(new_name, GetCache()->GetNameOfProfileAtIndex(1)); |
| 231 | EXPECT_NE(new_name, GetCache()->GetNameOfProfileAtIndex(0)); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 232 | |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 233 | base::string16 new_user_name = ASCIIToUTF16("user_name"); |
rogerta | e2265960 | 2015-05-11 21:58:21 | [diff] [blame] | 234 | std::string new_gaia_id = "12345"; |
| 235 | GetCache()->SetAuthInfoOfProfileAtIndex(1, new_gaia_id, new_user_name); |
[email protected] | e8e7809 | 2011-09-29 18:15:38 | [diff] [blame] | 236 | EXPECT_EQ(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(1)); |
rogerta | e2265960 | 2015-05-11 21:58:21 | [diff] [blame] | 237 | EXPECT_EQ(new_gaia_id, GetCache()->GetGAIAIdOfProfileAtIndex(1)); |
[email protected] | e8e7809 | 2011-09-29 18:15:38 | [diff] [blame] | 238 | EXPECT_NE(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(0)); |
| 239 | |
Jacques Chen | cf4971d | 2019-06-25 15:16:29 | [diff] [blame] | 240 | // Avatar icons not used on Android. |
| 241 | #if !defined(OS_ANDROID) |
lixan | 20fabcc | 2017-01-31 16:03:02 | [diff] [blame] | 242 | const size_t new_icon_index = 3; |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 243 | GetCache()->SetAvatarIconOfProfileAtIndex(1, new_icon_index); |
lixan | 20fabcc | 2017-01-31 16:03:02 | [diff] [blame] | 244 | EXPECT_EQ(new_icon_index, GetCache()->GetAvatarIconIndexOfProfileAtIndex(1)); |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 245 | // Not much to test. |
[email protected] | 6730b1e | 2011-09-29 05:23:52 | [diff] [blame] | 246 | GetCache()->GetAvatarIconOfProfileAtIndex(1); |
lixan | 20fabcc | 2017-01-31 16:03:02 | [diff] [blame] | 247 | |
| 248 | const size_t wrong_icon_index = profiles::GetDefaultAvatarIconCount() + 1; |
| 249 | const size_t generic_icon_index = 0; |
| 250 | GetCache()->SetAvatarIconOfProfileAtIndex(1, wrong_icon_index); |
| 251 | EXPECT_EQ(generic_icon_index, |
| 252 | GetCache()->GetAvatarIconIndexOfProfileAtIndex(1)); |
Jacques Chen | cf4971d | 2019-06-25 15:16:29 | [diff] [blame] | 253 | #endif |
[email protected] | dee810e | 2011-06-27 19:43:39 | [diff] [blame] | 254 | } |
| 255 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 256 | TEST_F(ProfileInfoCacheTest, Sort) { |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 257 | base::string16 name_a = ASCIIToUTF16("apple"); |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 258 | GetCache()->AddProfileToCache(GetProfilePath("path_a"), name_a, std::string(), |
| 259 | base::string16(), 0, std::string(), |
| 260 | EmptyAccountId()); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 261 | |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 262 | base::string16 name_c = ASCIIToUTF16("cat"); |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 263 | GetCache()->AddProfileToCache(GetProfilePath("path_c"), name_c, std::string(), |
| 264 | base::string16(), 0, std::string(), |
| 265 | EmptyAccountId()); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 266 | |
| 267 | // Sanity check the initial order. |
| 268 | EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0)); |
| 269 | EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1)); |
| 270 | |
| 271 | // Add a new profile (start with a capital to test case insensitive sorting. |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 272 | base::string16 name_b = ASCIIToUTF16("Banana"); |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 273 | GetCache()->AddProfileToCache(GetProfilePath("path_b"), name_b, std::string(), |
| 274 | base::string16(), 0, std::string(), |
| 275 | EmptyAccountId()); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 276 | |
| 277 | // Verify the new order. |
| 278 | EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0)); |
| 279 | EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(1)); |
| 280 | EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(2)); |
| 281 | |
| 282 | // Change the name of an existing profile. |
| 283 | name_a = UTF8ToUTF16("dog"); |
| 284 | GetCache()->SetNameOfProfileAtIndex(0, name_a); |
| 285 | |
| 286 | // Verify the new order. |
| 287 | EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0)); |
| 288 | EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1)); |
| 289 | EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(2)); |
| 290 | |
| 291 | // Delete a profile. |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 292 | GetCache()->DeleteProfileFromCache(GetProfilePath("path_c")); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 293 | |
| 294 | // Verify the new order. |
| 295 | EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0)); |
| 296 | EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(1)); |
| 297 | } |
| 298 | |
WC Leung | 1775638 | 2017-08-25 04:57:36 | [diff] [blame] | 299 | // Will be removed SOON with ProfileInfoCache tests. |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 300 | TEST_F(ProfileInfoCacheTest, BackgroundModeStatus) { |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 301 | GetCache()->AddProfileToCache( |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 302 | GetProfilePath("path_1"), ASCIIToUTF16("name_1"), std::string(), |
| 303 | base::string16(), 0, std::string(), EmptyAccountId()); |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 304 | GetCache()->AddProfileToCache( |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 305 | GetProfilePath("path_2"), ASCIIToUTF16("name_2"), std::string(), |
| 306 | base::string16(), 0, std::string(), EmptyAccountId()); |
[email protected] | 27917083 | 2011-10-12 23:38:03 | [diff] [blame] | 307 | |
| 308 | EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0)); |
| 309 | EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1)); |
| 310 | |
| 311 | GetCache()->SetBackgroundStatusOfProfileAtIndex(1, true); |
| 312 | |
| 313 | EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0)); |
| 314 | EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1)); |
| 315 | |
| 316 | GetCache()->SetBackgroundStatusOfProfileAtIndex(0, true); |
| 317 | |
| 318 | EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0)); |
| 319 | EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1)); |
| 320 | |
| 321 | GetCache()->SetBackgroundStatusOfProfileAtIndex(1, false); |
| 322 | |
| 323 | EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0)); |
| 324 | EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1)); |
| 325 | } |
| 326 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 327 | TEST_F(ProfileInfoCacheTest, GAIAName) { |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 328 | GetCache()->AddProfileToCache( |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 329 | GetProfilePath("path_1"), ASCIIToUTF16("Person 1"), std::string(), |
| 330 | base::string16(), 0, std::string(), EmptyAccountId()); |
[email protected] | a4352d0 | 2014-03-06 06:00:27 | [diff] [blame] | 331 | base::string16 profile_name(ASCIIToUTF16("Person 2")); |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 332 | GetCache()->AddProfileToCache(GetProfilePath("path_2"), profile_name, |
| 333 | std::string(), base::string16(), 0, |
| 334 | std::string(), EmptyAccountId()); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 335 | |
[email protected] | a4352d0 | 2014-03-06 06:00:27 | [diff] [blame] | 336 | int index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1")); |
| 337 | int index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2")); |
| 338 | |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 339 | // Sanity check. |
[email protected] | a4352d0 | 2014-03-06 06:00:27 | [diff] [blame] | 340 | EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty()); |
| 341 | EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index2).empty()); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 342 | |
[email protected] | 425d6ab | 2014-03-25 05:59:39 | [diff] [blame] | 343 | // Set GAIA name. This re-sorts the cache. |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 344 | base::string16 gaia_name(ASCIIToUTF16("Pat Smith")); |
[email protected] | a4352d0 | 2014-03-06 06:00:27 | [diff] [blame] | 345 | GetCache()->SetGAIANameOfProfileAtIndex(index2, gaia_name); |
[email protected] | 425d6ab | 2014-03-25 05:59:39 | [diff] [blame] | 346 | index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1")); |
| 347 | index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2")); |
| 348 | |
| 349 | // Since there is a GAIA name, we use that as a display name. |
[email protected] | a4352d0 | 2014-03-06 06:00:27 | [diff] [blame] | 350 | EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty()); |
| 351 | EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2)); |
[email protected] | a4352d0 | 2014-03-06 06:00:27 | [diff] [blame] | 352 | EXPECT_EQ(gaia_name, GetCache()->GetNameOfProfileAtIndex(index2)); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 353 | |
[email protected] | a4352d0 | 2014-03-06 06:00:27 | [diff] [blame] | 354 | // Don't use GAIA name as profile name. This re-sorts the cache. |
[email protected] | 425d6ab | 2014-03-25 05:59:39 | [diff] [blame] | 355 | base::string16 custom_name(ASCIIToUTF16("Custom name")); |
| 356 | GetCache()->SetNameOfProfileAtIndex(index2, custom_name); |
[email protected] | 1cdf093 | 2014-08-16 06:00:49 | [diff] [blame] | 357 | GetCache()->SetProfileIsUsingDefaultNameAtIndex(index2, false); |
| 358 | |
[email protected] | a4352d0 | 2014-03-06 06:00:27 | [diff] [blame] | 359 | index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1")); |
| 360 | index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2")); |
| 361 | |
[email protected] | 425d6ab | 2014-03-25 05:59:39 | [diff] [blame] | 362 | EXPECT_EQ(custom_name, GetCache()->GetNameOfProfileAtIndex(index2)); |
[email protected] | a4352d0 | 2014-03-06 06:00:27 | [diff] [blame] | 363 | EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2)); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 364 | } |
| 365 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 366 | TEST_F(ProfileInfoCacheTest, GAIAPicture) { |
[email protected] | 2290090 | 2014-07-30 11:50:41 | [diff] [blame] | 367 | const int kDefaultAvatarIndex = 0; |
| 368 | const int kOtherAvatarIndex = 1; |
[email protected] | 979dc4f | 2014-08-12 22:52:11 | [diff] [blame] | 369 | const int kGaiaPictureSize = 256; // Standard size of a Gaia account picture. |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 370 | GetCache()->AddProfileToCache( |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 371 | GetProfilePath("path_1"), ASCIIToUTF16("name_1"), std::string(), |
| 372 | base::string16(), kDefaultAvatarIndex, std::string(), EmptyAccountId()); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 373 | GetCache()->AddProfileToCache( |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 374 | GetProfilePath("path_2"), ASCIIToUTF16("name_2"), std::string(), |
| 375 | base::string16(), kDefaultAvatarIndex, std::string(), EmptyAccountId()); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 376 | |
| 377 | // Sanity check. |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 378 | EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0)); |
| 379 | EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(1)); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 380 | EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0)); |
| 381 | EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1)); |
| 382 | |
| 383 | // The profile icon should be the default one. |
[email protected] | 2290090 | 2014-07-30 11:50:41 | [diff] [blame] | 384 | EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(0)); |
| 385 | EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1)); |
| 386 | int default_avatar_id = |
| 387 | profiles::GetDefaultAvatarIconResourceIDAtIndex(kDefaultAvatarIndex); |
| 388 | const gfx::Image& default_avatar_image( |
Lei Zhang | 7640d54 | 2017-10-03 16:26:49 | [diff] [blame] | 389 | ui::ResourceBundle::GetSharedInstance().GetImageNamed(default_avatar_id)); |
pkotwicz | b312b51 | 2015-09-29 23:44:43 | [diff] [blame] | 390 | EXPECT_TRUE(gfx::test::AreImagesEqual( |
[email protected] | 2290090 | 2014-07-30 11:50:41 | [diff] [blame] | 391 | default_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1))); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 392 | |
| 393 | // Set GAIA picture. |
[email protected] | 979dc4f | 2014-08-12 22:52:11 | [diff] [blame] | 394 | gfx::Image gaia_image(gfx::test::CreateImage( |
| 395 | kGaiaPictureSize, kGaiaPictureSize)); |
Dana Fried | fa14d5f | 2019-04-21 20:49:36 | [diff] [blame] | 396 | GetCache()->SetGAIAPictureOfProfileAtIndex(1, gaia_image); |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 397 | EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0)); |
pkotwicz | b312b51 | 2015-09-29 23:44:43 | [diff] [blame] | 398 | EXPECT_TRUE(gfx::test::AreImagesEqual( |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 399 | gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1))); |
[email protected] | 2290090 | 2014-07-30 11:50:41 | [diff] [blame] | 400 | // Since we're still using the default avatar, the GAIA image should be |
| 401 | // preferred over the generic avatar image. |
| 402 | EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1)); |
| 403 | EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1)); |
pkotwicz | b312b51 | 2015-09-29 23:44:43 | [diff] [blame] | 404 | EXPECT_TRUE(gfx::test::AreImagesEqual( |
[email protected] | 2290090 | 2014-07-30 11:50:41 | [diff] [blame] | 405 | gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1))); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 406 | |
[email protected] | 1cdf093 | 2014-08-16 06:00:49 | [diff] [blame] | 407 | // Set a non-default avatar. This should be preferred over the GAIA image. |
[email protected] | 2290090 | 2014-07-30 11:50:41 | [diff] [blame] | 408 | GetCache()->SetAvatarIconOfProfileAtIndex(1, kOtherAvatarIndex); |
[email protected] | 1cdf093 | 2014-08-16 06:00:49 | [diff] [blame] | 409 | GetCache()->SetProfileIsUsingDefaultAvatarAtIndex(1, false); |
[email protected] | 2290090 | 2014-07-30 11:50:41 | [diff] [blame] | 410 | EXPECT_FALSE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1)); |
| 411 | EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1)); |
Jacques Chen | cf4971d | 2019-06-25 15:16:29 | [diff] [blame] | 412 | // Avatar icons not used on Android. |
| 413 | #if !defined(OS_ANDROID) |
[email protected] | 2290090 | 2014-07-30 11:50:41 | [diff] [blame] | 414 | int other_avatar_id = |
| 415 | profiles::GetDefaultAvatarIconResourceIDAtIndex(kOtherAvatarIndex); |
| 416 | const gfx::Image& other_avatar_image( |
Lei Zhang | 7640d54 | 2017-10-03 16:26:49 | [diff] [blame] | 417 | ui::ResourceBundle::GetSharedInstance().GetImageNamed(other_avatar_id)); |
pkotwicz | b312b51 | 2015-09-29 23:44:43 | [diff] [blame] | 418 | EXPECT_TRUE(gfx::test::AreImagesEqual( |
[email protected] | 2290090 | 2014-07-30 11:50:41 | [diff] [blame] | 419 | other_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1))); |
Jacques Chen | cf4971d | 2019-06-25 15:16:29 | [diff] [blame] | 420 | #endif |
[email protected] | 2290090 | 2014-07-30 11:50:41 | [diff] [blame] | 421 | |
| 422 | // Explicitly setting the GAIA picture should make it preferred again. |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 423 | GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, true); |
[email protected] | 2290090 | 2014-07-30 11:50:41 | [diff] [blame] | 424 | EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1)); |
pkotwicz | b312b51 | 2015-09-29 23:44:43 | [diff] [blame] | 425 | EXPECT_TRUE(gfx::test::AreImagesEqual( |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 426 | gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1))); |
pkotwicz | b312b51 | 2015-09-29 23:44:43 | [diff] [blame] | 427 | EXPECT_TRUE(gfx::test::AreImagesEqual( |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 428 | gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1))); |
| 429 | |
[email protected] | 2290090 | 2014-07-30 11:50:41 | [diff] [blame] | 430 | // Clearing the IsUsingGAIAPicture flag should result in the generic image |
| 431 | // being used again. |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 432 | GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, false); |
[email protected] | 2290090 | 2014-07-30 11:50:41 | [diff] [blame] | 433 | EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1)); |
pkotwicz | b312b51 | 2015-09-29 23:44:43 | [diff] [blame] | 434 | EXPECT_TRUE(gfx::test::AreImagesEqual( |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 435 | gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1))); |
Jacques Chen | cf4971d | 2019-06-25 15:16:29 | [diff] [blame] | 436 | #if !defined(OS_ANDROID) |
pkotwicz | b312b51 | 2015-09-29 23:44:43 | [diff] [blame] | 437 | EXPECT_TRUE(gfx::test::AreImagesEqual( |
[email protected] | 2290090 | 2014-07-30 11:50:41 | [diff] [blame] | 438 | other_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1))); |
Jacques Chen | cf4971d | 2019-06-25 15:16:29 | [diff] [blame] | 439 | #endif |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 440 | } |
| 441 | |
[email protected] | 754bebc | 2011-12-01 16:42:16 | [diff] [blame] | 442 | TEST_F(ProfileInfoCacheTest, PersistGAIAPicture) { |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 443 | GetCache()->AddProfileToCache( |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 444 | GetProfilePath("path_1"), ASCIIToUTF16("name_1"), std::string(), |
| 445 | base::string16(), 0, std::string(), EmptyAccountId()); |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 446 | gfx::Image gaia_image(gfx::test::CreateImage()); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 447 | |
Dana Fried | fa14d5f | 2019-04-21 20:49:36 | [diff] [blame] | 448 | GetCache()->SetGAIAPictureOfProfileAtIndex(0, gaia_image); |
noms | 75ca019 | 2015-02-26 03:14:30 | [diff] [blame] | 449 | |
| 450 | // Make sure everything has completed, and the file has been written to disk. |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 451 | content::RunAllTasksUntilIdle(); |
noms | 75ca019 | 2015-02-26 03:14:30 | [diff] [blame] | 452 | |
pkotwicz | b312b51 | 2015-09-29 23:44:43 | [diff] [blame] | 453 | EXPECT_TRUE(gfx::test::AreImagesEqual( |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 454 | gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0))); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 455 | |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 456 | ResetCache(); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 457 | // Try to get the GAIA picture. This should return NULL until the read from |
| 458 | // disk is done. |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 459 | EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0)); |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 460 | content::RunAllTasksUntilIdle(); |
noms | 75ca019 | 2015-02-26 03:14:30 | [diff] [blame] | 461 | |
pkotwicz | b312b51 | 2015-09-29 23:44:43 | [diff] [blame] | 462 | EXPECT_TRUE(gfx::test::AreImagesEqual( |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 463 | gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0))); |
| 464 | } |
| 465 | |
Jochen Eisinger | d84bb42a | 2018-07-23 22:39:03 | [diff] [blame] | 466 | #if BUILDFLAG(ENABLE_SUPERVISED_USERS) |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 467 | TEST_F(ProfileInfoCacheTest, SetSupervisedUserId) { |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 468 | GetCache()->AddProfileToCache(GetProfilePath("test"), ASCIIToUTF16("Test"), |
| 469 | std::string(), base::string16(), 0, |
| 470 | std::string(), EmptyAccountId()); |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 471 | EXPECT_FALSE(GetCache()->ProfileIsSupervisedAtIndex(0)); |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 472 | |
Jochen Eisinger | d84bb42a | 2018-07-23 22:39:03 | [diff] [blame] | 473 | GetCache()->SetSupervisedUserIdOfProfileAtIndex( |
| 474 | 0, supervised_users::kChildAccountSUID); |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 475 | EXPECT_TRUE(GetCache()->ProfileIsSupervisedAtIndex(0)); |
Jochen Eisinger | d84bb42a | 2018-07-23 22:39:03 | [diff] [blame] | 476 | EXPECT_EQ(supervised_users::kChildAccountSUID, |
| 477 | GetCache()->GetSupervisedUserIdOfProfileAtIndex(0)); |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 478 | |
| 479 | ResetCache(); |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 480 | EXPECT_TRUE(GetCache()->ProfileIsSupervisedAtIndex(0)); |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 481 | |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 482 | GetCache()->SetSupervisedUserIdOfProfileAtIndex(0, std::string()); |
| 483 | EXPECT_FALSE(GetCache()->ProfileIsSupervisedAtIndex(0)); |
| 484 | EXPECT_EQ("", GetCache()->GetSupervisedUserIdOfProfileAtIndex(0)); |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 485 | } |
Jochen Eisinger | d84bb42a | 2018-07-23 22:39:03 | [diff] [blame] | 486 | #endif // BUILDFLAG(ENABLE_SUPERVISED_USERS) |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 487 | |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 488 | TEST_F(ProfileInfoCacheTest, EmptyGAIAInfo) { |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 489 | base::string16 profile_name = ASCIIToUTF16("name_1"); |
[email protected] | c3e55977 | 2014-04-09 04:02:54 | [diff] [blame] | 490 | int id = profiles::GetDefaultAvatarIconResourceIDAtIndex(0); |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 491 | const gfx::Image& profile_image( |
Lei Zhang | 7640d54 | 2017-10-03 16:26:49 | [diff] [blame] | 492 | ui::ResourceBundle::GetSharedInstance().GetImageNamed(id)); |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 493 | |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 494 | GetCache()->AddProfileToCache(GetProfilePath("path_1"), profile_name, |
| 495 | std::string(), base::string16(), 0, |
| 496 | std::string(), EmptyAccountId()); |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 497 | |
| 498 | // Set empty GAIA info. |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 499 | GetCache()->SetGAIANameOfProfileAtIndex(0, base::string16()); |
Dana Fried | fa14d5f | 2019-04-21 20:49:36 | [diff] [blame] | 500 | GetCache()->SetGAIAPictureOfProfileAtIndex(0, gfx::Image()); |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 501 | GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(0, true); |
| 502 | |
| 503 | // Verify that the profile name and picture are not empty. |
| 504 | EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(0)); |
pkotwicz | b312b51 | 2015-09-29 23:44:43 | [diff] [blame] | 505 | EXPECT_TRUE(gfx::test::AreImagesEqual( |
[email protected] | 2f3c00f | 2011-11-30 04:36:22 | [diff] [blame] | 506 | profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(0))); |
[email protected] | cb114f14 | 2011-11-23 20:18:04 | [diff] [blame] | 507 | } |
| 508 | |
Jochen Eisinger | d84bb42a | 2018-07-23 22:39:03 | [diff] [blame] | 509 | #if BUILDFLAG(ENABLE_SUPERVISED_USERS) |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 510 | TEST_F(ProfileInfoCacheTest, CreateSupervisedTestingProfile) { |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 511 | testing_profile_manager_.CreateTestingProfile("default"); |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 512 | base::string16 supervised_user_name = ASCIIToUTF16("Supervised User"); |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 513 | testing_profile_manager_.CreateTestingProfile( |
maxbogue | ea16ff41 | 2016-10-28 16:35:29 | [diff] [blame] | 514 | "test1", std::unique_ptr<sync_preferences::PrefServiceSyncable>(), |
Jochen Eisinger | d84bb42a | 2018-07-23 22:39:03 | [diff] [blame] | 515 | supervised_user_name, 0, supervised_users::kChildAccountSUID, |
| 516 | TestingProfile::TestingFactories()); |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 517 | for (size_t i = 0; i < GetCache()->GetNumberOfProfiles(); i++) { |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 518 | bool is_supervised = |
| 519 | GetCache()->GetNameOfProfileAtIndex(i) == supervised_user_name; |
| 520 | EXPECT_EQ(is_supervised, GetCache()->ProfileIsSupervisedAtIndex(i)); |
Jochen Eisinger | d84bb42a | 2018-07-23 22:39:03 | [diff] [blame] | 521 | std::string supervised_user_id = |
| 522 | is_supervised ? supervised_users::kChildAccountSUID : ""; |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 523 | EXPECT_EQ(supervised_user_id, |
| 524 | GetCache()->GetSupervisedUserIdOfProfileAtIndex(i)); |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 525 | } |
[email protected] | 371662e37 | 2013-10-17 22:05:22 | [diff] [blame] | 526 | |
[email protected] | d20d043 | 2014-06-12 17:14:05 | [diff] [blame] | 527 | // Supervised profiles have a custom theme, which needs to be deleted on the |
| 528 | // FILE thread. Reset the profile manager now so everything is deleted while |
| 529 | // we still have a FILE thread. |
[email protected] | 371662e37 | 2013-10-17 22:05:22 | [diff] [blame] | 530 | TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL); |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 531 | } |
Jochen Eisinger | d84bb42a | 2018-07-23 22:39:03 | [diff] [blame] | 532 | #endif |
[email protected] | 0aa018a | 2013-07-31 15:08:54 | [diff] [blame] | 533 | |
[email protected] | 04748180 | 2013-09-16 22:26:38 | [diff] [blame] | 534 | TEST_F(ProfileInfoCacheTest, AddStubProfile) { |
| 535 | EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles()); |
| 536 | |
| 537 | // Add some profiles with and without a '.' in their paths. |
| 538 | const struct { |
| 539 | const char* profile_path; |
| 540 | const char* profile_name; |
| 541 | } kTestCases[] = { |
| 542 | { "path.test0", "name_0" }, |
| 543 | { "path_test1", "name_1" }, |
| 544 | { "path.test2", "name_2" }, |
| 545 | { "path_test3", "name_3" }, |
| 546 | }; |
| 547 | |
Avi Drissman | 5f0fb8c | 2018-12-25 23:20:49 | [diff] [blame] | 548 | for (size_t i = 0; i < base::size(kTestCases); ++i) { |
[email protected] | 04748180 | 2013-09-16 22:26:38 | [diff] [blame] | 549 | base::FilePath profile_path = GetProfilePath(kTestCases[i].profile_path); |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 550 | base::string16 profile_name = ASCIIToUTF16(kTestCases[i].profile_name); |
[email protected] | 04748180 | 2013-09-16 22:26:38 | [diff] [blame] | 551 | |
rogerta | e2265960 | 2015-05-11 21:58:21 | [diff] [blame] | 552 | GetCache()->AddProfileToCache(profile_path, profile_name, std::string(), |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 553 | base::string16(), i, "", EmptyAccountId()); |
[email protected] | 04748180 | 2013-09-16 22:26:38 | [diff] [blame] | 554 | |
| 555 | EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i)); |
| 556 | EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i)); |
| 557 | } |
| 558 | |
| 559 | ASSERT_EQ(4U, GetCache()->GetNumberOfProfiles()); |
| 560 | |
| 561 | // Check that the profiles can be extracted from the local state. |
noms | 693f35aa | 2015-02-05 16:03:16 | [diff] [blame] | 562 | std::vector<base::string16> names; |
| 563 | PrefService* local_state = g_browser_process->local_state(); |
| 564 | const base::DictionaryValue* cache = local_state->GetDictionary( |
| 565 | prefs::kProfileInfoCache); |
| 566 | base::string16 name; |
| 567 | for (base::DictionaryValue::Iterator it(*cache); !it.IsAtEnd(); |
| 568 | it.Advance()) { |
| 569 | const base::DictionaryValue* info = NULL; |
| 570 | it.value().GetAsDictionary(&info); |
| 571 | info->GetString("name", &name); |
| 572 | names.push_back(name); |
| 573 | } |
| 574 | |
[email protected] | 04748180 | 2013-09-16 22:26:38 | [diff] [blame] | 575 | for (size_t i = 0; i < 4; i++) |
| 576 | ASSERT_FALSE(names[i].empty()); |
| 577 | } |
| 578 | |
lwchkg | 778965f | 2016-05-12 13:14:22 | [diff] [blame] | 579 | TEST_F(ProfileInfoCacheTest, EntriesInAttributesStorage) { |
| 580 | EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles()); |
| 581 | |
| 582 | // Add some profiles with and without a '.' in their paths. |
| 583 | const struct { |
| 584 | const char* profile_path; |
| 585 | const char* profile_name; |
| 586 | } kTestCases[] = { |
| 587 | { "path.test0", "name_0" }, |
| 588 | { "path_test1", "name_1" }, |
| 589 | { "path.test2", "name_2" }, |
| 590 | { "path_test3", "name_3" }, |
| 591 | }; |
| 592 | |
| 593 | // Profiles are added and removed using all combinations of the old and the |
| 594 | // new interfaces. The content of |profile_attributes_entries_| in |
| 595 | // ProfileAttributesStorage is checked after each insert and delete operation. |
| 596 | |
| 597 | // Add profiles. |
Avi Drissman | 5f0fb8c | 2018-12-25 23:20:49 | [diff] [blame] | 598 | for (size_t i = 0; i < base::size(kTestCases); ++i) { |
lwchkg | 778965f | 2016-05-12 13:14:22 | [diff] [blame] | 599 | base::FilePath profile_path = GetProfilePath(kTestCases[i].profile_path); |
| 600 | base::string16 profile_name = ASCIIToUTF16(kTestCases[i].profile_name); |
| 601 | |
| 602 | ASSERT_EQ(0u, GetCache()->profile_attributes_entries_.count( |
| 603 | profile_path.value())); |
| 604 | |
| 605 | // Use ProfileInfoCache in profiles 0 and 2, and ProfileAttributesStorage in |
| 606 | // profiles 1 and 3. |
| 607 | if (i | 1u) { |
| 608 | GetCache()->AddProfileToCache(profile_path, profile_name, std::string(), |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 609 | base::string16(), i, "", EmptyAccountId()); |
lwchkg | 778965f | 2016-05-12 13:14:22 | [diff] [blame] | 610 | } else { |
| 611 | GetCache()->AddProfile(profile_path, profile_name, std::string(), |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 612 | base::string16(), i, "", EmptyAccountId()); |
lwchkg | 778965f | 2016-05-12 13:14:22 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | ASSERT_EQ(i + 1, GetCache()->GetNumberOfProfiles()); |
| 616 | ASSERT_EQ(i + 1, GetCache()->profile_attributes_entries_.size()); |
| 617 | |
| 618 | ASSERT_EQ(1u, GetCache()->profile_attributes_entries_.count( |
| 619 | profile_path.value())); |
| 620 | // TODO(anthonyvd) : check that the entry in |profile_attributes_entries_| |
| 621 | // is null before GetProfileAttributesWithPath is run. Currently this is |
| 622 | // impossible to check because GetProfileAttributesWithPath is called during |
| 623 | // profile creation. |
| 624 | |
| 625 | ProfileAttributesEntry* entry = nullptr; |
| 626 | GetCache()->GetProfileAttributesWithPath(profile_path, &entry); |
| 627 | EXPECT_EQ( |
| 628 | entry, |
| 629 | GetCache()->profile_attributes_entries_[profile_path.value()].get()); |
| 630 | } |
| 631 | |
| 632 | // Remove profiles. |
Avi Drissman | 5f0fb8c | 2018-12-25 23:20:49 | [diff] [blame] | 633 | for (size_t i = 0; i < base::size(kTestCases); ++i) { |
lwchkg | 778965f | 2016-05-12 13:14:22 | [diff] [blame] | 634 | base::FilePath profile_path = GetProfilePath(kTestCases[i].profile_path); |
| 635 | ASSERT_EQ(1u, GetCache()->profile_attributes_entries_.count( |
| 636 | profile_path.value())); |
| 637 | |
| 638 | // Use ProfileInfoCache in profiles 0 and 1, and ProfileAttributesStorage in |
| 639 | // profiles 2 and 3. |
| 640 | if (i | 2u) |
| 641 | GetCache()->DeleteProfileFromCache(profile_path); |
| 642 | else |
| 643 | GetCache()->RemoveProfile(profile_path); |
| 644 | |
| 645 | ASSERT_EQ(0u, GetCache()->profile_attributes_entries_.count( |
| 646 | profile_path.value())); |
| 647 | |
| 648 | ProfileAttributesEntry* entry = nullptr; |
| 649 | EXPECT_FALSE(GetCache()->GetProfileAttributesWithPath(profile_path, |
| 650 | &entry)); |
| 651 | ASSERT_EQ(0u, GetCache()->profile_attributes_entries_.count( |
| 652 | profile_path.value())); |
| 653 | } |
| 654 | } |
| 655 | |
jam | 1c5a9149 | 2016-02-24 20:47:53 | [diff] [blame] | 656 | #if !defined(OS_ANDROID) && !defined(OS_CHROMEOS) |
noms | be073e5 | 2014-08-23 22:01:04 | [diff] [blame] | 657 | TEST_F(ProfileInfoCacheTest, MigrateLegacyProfileNamesWithNewAvatarMenu) { |
noms | be073e5 | 2014-08-23 22:01:04 | [diff] [blame] | 658 | EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles()); |
| 659 | |
| 660 | base::FilePath path_1 = GetProfilePath("path_1"); |
| 661 | GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("Default Profile"), |
rogerta | e2265960 | 2015-05-11 21:58:21 | [diff] [blame] | 662 | std::string(), base::string16(), 0, |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 663 | std::string(), EmptyAccountId()); |
noms | be073e5 | 2014-08-23 22:01:04 | [diff] [blame] | 664 | base::FilePath path_2 = GetProfilePath("path_2"); |
| 665 | GetCache()->AddProfileToCache(path_2, ASCIIToUTF16("First user"), |
rogerta | e2265960 | 2015-05-11 21:58:21 | [diff] [blame] | 666 | std::string(), base::string16(), 1, |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 667 | std::string(), EmptyAccountId()); |
noms | be073e5 | 2014-08-23 22:01:04 | [diff] [blame] | 668 | base::string16 name_3 = ASCIIToUTF16("Lemonade"); |
| 669 | base::FilePath path_3 = GetProfilePath("path_3"); |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 670 | GetCache()->AddProfileToCache(path_3, name_3, std::string(), base::string16(), |
| 671 | 2, std::string(), EmptyAccountId()); |
noms | be073e5 | 2014-08-23 22:01:04 | [diff] [blame] | 672 | base::string16 name_4 = ASCIIToUTF16("Batman"); |
| 673 | base::FilePath path_4 = GetProfilePath("path_4"); |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 674 | GetCache()->AddProfileToCache(path_4, name_4, std::string(), base::string16(), |
| 675 | 3, std::string(), EmptyAccountId()); |
noms | be073e5 | 2014-08-23 22:01:04 | [diff] [blame] | 676 | base::string16 name_5 = ASCIIToUTF16("Person 2"); |
| 677 | base::FilePath path_5 = GetProfilePath("path_5"); |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 678 | GetCache()->AddProfileToCache(path_5, name_5, std::string(), base::string16(), |
| 679 | 2, std::string(), EmptyAccountId()); |
noms | be073e5 | 2014-08-23 22:01:04 | [diff] [blame] | 680 | |
| 681 | EXPECT_EQ(5U, GetCache()->GetNumberOfProfiles()); |
| 682 | |
| 683 | |
| 684 | ResetCache(); |
| 685 | |
| 686 | // Legacy profile names like "Default Profile" and "First user" should be |
WC Leung | 0ea09a64 | 2017-09-02 01:46:15 | [diff] [blame] | 687 | // migrated to "Person %n" type names, i.e. any permutation of "Person 1" and |
| 688 | // "Person 3". |
| 689 | if (ASCIIToUTF16("Person 1") == |
| 690 | GetCache()->GetNameOfProfileAtIndex( |
| 691 | GetCache()->GetIndexOfProfileWithPath(path_1))) { |
| 692 | EXPECT_EQ(ASCIIToUTF16("Person 3"), |
| 693 | GetCache()->GetNameOfProfileAtIndex( |
| 694 | GetCache()->GetIndexOfProfileWithPath(path_2))); |
| 695 | } else { |
| 696 | EXPECT_EQ(ASCIIToUTF16("Person 3"), |
| 697 | GetCache()->GetNameOfProfileAtIndex( |
| 698 | GetCache()->GetIndexOfProfileWithPath(path_1))); |
| 699 | EXPECT_EQ(ASCIIToUTF16("Person 1"), |
| 700 | GetCache()->GetNameOfProfileAtIndex( |
| 701 | GetCache()->GetIndexOfProfileWithPath(path_2))); |
| 702 | } |
noms | be073e5 | 2014-08-23 22:01:04 | [diff] [blame] | 703 | |
| 704 | // Other profile names should not be migrated even if they're the old |
| 705 | // default cartoon profile names. |
| 706 | EXPECT_EQ(name_3, GetCache()->GetNameOfProfileAtIndex( |
| 707 | GetCache()->GetIndexOfProfileWithPath(path_3))); |
| 708 | EXPECT_EQ(name_4, GetCache()->GetNameOfProfileAtIndex( |
| 709 | GetCache()->GetIndexOfProfileWithPath(path_4))); |
| 710 | EXPECT_EQ(name_5, GetCache()->GetNameOfProfileAtIndex( |
| 711 | GetCache()->GetIndexOfProfileWithPath(path_5))); |
| 712 | } |
Xi Cheng | 1b76138 | 2017-08-15 20:37:34 | [diff] [blame] | 713 | |
| 714 | TEST_F(ProfileInfoCacheTest, GetGaiaImageForAvatarMenu) { |
| 715 | // The TestingProfileManager's ProfileInfoCache doesn't download avatars. |
| 716 | ProfileInfoCache profile_info_cache( |
| 717 | g_browser_process->local_state(), |
| 718 | testing_profile_manager_.profile_manager()->user_data_dir()); |
| 719 | |
| 720 | base::FilePath profile_path = GetProfilePath("path_1"); |
| 721 | |
| 722 | GetCache()->AddProfileToCache(profile_path, ASCIIToUTF16("name_1"), |
| 723 | std::string(), base::string16(), 0, |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 724 | std::string(), EmptyAccountId()); |
Xi Cheng | 1b76138 | 2017-08-15 20:37:34 | [diff] [blame] | 725 | |
| 726 | gfx::Image gaia_image(gfx::test::CreateImage()); |
Dana Fried | fa14d5f | 2019-04-21 20:49:36 | [diff] [blame] | 727 | GetCache()->SetGAIAPictureOfProfileAtIndex(0, gaia_image); |
Xi Cheng | 1b76138 | 2017-08-15 20:37:34 | [diff] [blame] | 728 | |
| 729 | // Make sure everything has completed, and the file has been written to disk. |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 730 | content::RunAllTasksUntilIdle(); |
Xi Cheng | 1b76138 | 2017-08-15 20:37:34 | [diff] [blame] | 731 | |
| 732 | // Make sure this profile is using GAIA picture. |
| 733 | EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0)); |
| 734 | |
| 735 | ResetCache(); |
| 736 | |
| 737 | // We need to explicitly set the GAIA usage flag after resetting the cache. |
| 738 | GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(0, true); |
| 739 | EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0)); |
| 740 | |
| 741 | gfx::Image image_loaded; |
| 742 | |
| 743 | // Try to get the GAIA image. For the first time, it triggers an async image |
| 744 | // load from disk. The load status indicates the image is still being loaded. |
| 745 | EXPECT_EQ(AvatarMenu::ImageLoadStatus::LOADING, |
| 746 | AvatarMenu::GetImageForMenuButton(profile_path, &image_loaded)); |
| 747 | EXPECT_FALSE(gfx::test::AreImagesEqual(gaia_image, image_loaded)); |
| 748 | |
| 749 | // Wait until the async image load finishes. |
Gabriel Charette | 01507a2 | 2017-09-27 21:30:08 | [diff] [blame] | 750 | content::RunAllTasksUntilIdle(); |
Xi Cheng | 1b76138 | 2017-08-15 20:37:34 | [diff] [blame] | 751 | |
| 752 | // Since the GAIA image is loaded now, we can get it this time. |
| 753 | EXPECT_EQ(AvatarMenu::ImageLoadStatus::LOADED, |
| 754 | AvatarMenu::GetImageForMenuButton(profile_path, &image_loaded)); |
| 755 | EXPECT_TRUE(gfx::test::AreImagesEqual(gaia_image, image_loaded)); |
| 756 | } |
[email protected] | b09bfa1 | 2014-08-15 19:05:25 | [diff] [blame] | 757 | #endif |
noms | be073e5 | 2014-08-23 22:01:04 | [diff] [blame] | 758 | |
jam | 1c5a9149 | 2016-02-24 20:47:53 | [diff] [blame] | 759 | #if defined(OS_CHROMEOS) || defined(OS_ANDROID) |
noms | be073e5 | 2014-08-23 22:01:04 | [diff] [blame] | 760 | TEST_F(ProfileInfoCacheTest, |
| 761 | DontMigrateLegacyProfileNamesWithoutNewAvatarMenu) { |
| 762 | EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles()); |
| 763 | |
| 764 | base::string16 name_1 = ASCIIToUTF16("Default Profile"); |
| 765 | base::FilePath path_1 = GetProfilePath("path_1"); |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 766 | GetCache()->AddProfileToCache(path_1, name_1, std::string(), base::string16(), |
| 767 | 0, std::string(), EmptyAccountId()); |
noms | be073e5 | 2014-08-23 22:01:04 | [diff] [blame] | 768 | base::string16 name_2 = ASCIIToUTF16("First user"); |
| 769 | base::FilePath path_2 = GetProfilePath("path_2"); |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 770 | GetCache()->AddProfileToCache(path_2, name_2, std::string(), base::string16(), |
| 771 | 1, std::string(), EmptyAccountId()); |
noms | be073e5 | 2014-08-23 22:01:04 | [diff] [blame] | 772 | base::string16 name_3 = ASCIIToUTF16("Lemonade"); |
| 773 | base::FilePath path_3 = GetProfilePath("path_3"); |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 774 | GetCache()->AddProfileToCache(path_3, name_3, std::string(), base::string16(), |
| 775 | 2, std::string(), EmptyAccountId()); |
noms | be073e5 | 2014-08-23 22:01:04 | [diff] [blame] | 776 | base::string16 name_4 = ASCIIToUTF16("Batman"); |
| 777 | base::FilePath path_4 = GetProfilePath("path_4"); |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 778 | GetCache()->AddProfileToCache(path_4, name_4, std::string(), base::string16(), |
| 779 | 3, std::string(), EmptyAccountId()); |
noms | be073e5 | 2014-08-23 22:01:04 | [diff] [blame] | 780 | EXPECT_EQ(4U, GetCache()->GetNumberOfProfiles()); |
| 781 | |
| 782 | ResetCache(); |
| 783 | |
| 784 | // Profile names should have been preserved. |
| 785 | EXPECT_EQ(name_1, GetCache()->GetNameOfProfileAtIndex( |
| 786 | GetCache()->GetIndexOfProfileWithPath(path_1))); |
| 787 | EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex( |
| 788 | GetCache()->GetIndexOfProfileWithPath(path_2))); |
| 789 | EXPECT_EQ(name_3, GetCache()->GetNameOfProfileAtIndex( |
| 790 | GetCache()->GetIndexOfProfileWithPath(path_3))); |
| 791 | EXPECT_EQ(name_4, GetCache()->GetNameOfProfileAtIndex( |
| 792 | GetCache()->GetIndexOfProfileWithPath(path_4))); |
| 793 | } |
anthonyvd | 66aa95d | 2015-10-02 15:40:26 | [diff] [blame] | 794 | #endif |
Alexander Alekseev | 4084f50 | 2018-02-20 22:16:46 | [diff] [blame] | 795 | |
| 796 | TEST_F(ProfileInfoCacheTest, RemoveProfileByAccountId) { |
| 797 | EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles()); |
| 798 | |
| 799 | base::FilePath path_1 = GetProfilePath("path_1"); |
| 800 | const AccountId account_id_1( |
| 801 | AccountId::FromUserEmailGaiaId("email1", "111111")); |
| 802 | base::string16 name_1 = ASCIIToUTF16("name_1"); |
| 803 | GetCache()->AddProfileToCache(path_1, name_1, account_id_1.GetGaiaId(), |
| 804 | UTF8ToUTF16(account_id_1.GetUserEmail()), 0, |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 805 | std::string(), EmptyAccountId()); |
Alexander Alekseev | 4084f50 | 2018-02-20 22:16:46 | [diff] [blame] | 806 | EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles()); |
| 807 | |
| 808 | base::FilePath path_2 = GetProfilePath("path_2"); |
| 809 | base::string16 name_2 = ASCIIToUTF16("name_2"); |
| 810 | const AccountId account_id_2( |
| 811 | AccountId::FromUserEmailGaiaId("email2", "222222")); |
| 812 | GetCache()->AddProfileToCache(path_2, name_2, account_id_2.GetGaiaId(), |
| 813 | UTF8ToUTF16(account_id_2.GetUserEmail()), 0, |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 814 | std::string(), EmptyAccountId()); |
Alexander Alekseev | 4084f50 | 2018-02-20 22:16:46 | [diff] [blame] | 815 | EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles()); |
| 816 | |
| 817 | base::FilePath path_3 = GetProfilePath("path_3"); |
| 818 | base::string16 name_3 = ASCIIToUTF16("name_3"); |
| 819 | const AccountId account_id_3( |
| 820 | AccountId::FromUserEmailGaiaId("email3", "333333")); |
| 821 | GetCache()->AddProfileToCache(path_3, name_3, account_id_3.GetGaiaId(), |
| 822 | UTF8ToUTF16(account_id_3.GetUserEmail()), 0, |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 823 | std::string(), EmptyAccountId()); |
Alexander Alekseev | 4084f50 | 2018-02-20 22:16:46 | [diff] [blame] | 824 | EXPECT_EQ(3u, GetCache()->GetNumberOfProfiles()); |
| 825 | |
| 826 | base::FilePath path_4 = GetProfilePath("path_4"); |
| 827 | base::string16 name_4 = ASCIIToUTF16("name_4"); |
| 828 | const AccountId account_id_4( |
| 829 | AccountId::FromUserEmailGaiaId("email4", "444444")); |
| 830 | GetCache()->AddProfileToCache(path_4, name_4, account_id_4.GetGaiaId(), |
| 831 | UTF8ToUTF16(account_id_4.GetUserEmail()), 0, |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 832 | std::string(), EmptyAccountId()); |
Alexander Alekseev | 4084f50 | 2018-02-20 22:16:46 | [diff] [blame] | 833 | EXPECT_EQ(4u, GetCache()->GetNumberOfProfiles()); |
| 834 | |
| 835 | GetCache()->RemoveProfileByAccountId(account_id_3); |
| 836 | EXPECT_EQ(3u, GetCache()->GetNumberOfProfiles()); |
| 837 | EXPECT_EQ(name_1, GetCache()->GetNameOfProfileAtIndex(0)); |
| 838 | |
| 839 | GetCache()->RemoveProfileByAccountId(account_id_1); |
| 840 | EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles()); |
| 841 | EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(0)); |
| 842 | |
| 843 | // this profile is already deleted. |
| 844 | GetCache()->RemoveProfileByAccountId(account_id_3); |
| 845 | EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles()); |
| 846 | EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(0)); |
| 847 | |
| 848 | // Remove profile by partial match |
| 849 | GetCache()->RemoveProfileByAccountId( |
| 850 | AccountId::FromUserEmail(account_id_2.GetUserEmail())); |
| 851 | EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles()); |
| 852 | EXPECT_EQ(name_4, GetCache()->GetNameOfProfileAtIndex(0)); |
| 853 | |
| 854 | // Remove last profile |
| 855 | GetCache()->RemoveProfileByAccountId(account_id_4); |
| 856 | EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles()); |
| 857 | } |