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