Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/strings/stringprintf.h" | |
| 6 #include "base/strings/utf_string_conversions.h" | |
| 7 #include "chrome/browser/profiles/profile_info_cache.h" | |
| 8 #include "chrome/browser/profiles/profile_manager.h" | |
| 9 #include "chrome/browser/supervised_user/supervised_user_constants.h" | |
| 10 #include "chrome/test/base/testing_browser_process.h" | |
| 11 #include "chrome/test/base/testing_profile_manager.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 namespace { | |
| 15 // The ProfileMetadataEntry accessors aren't just plain old accessors to local | |
| 16 // members so they'll be tested. The following helpers will make the testing | |
| 17 // code less verbose. | |
| 18 #define TEST_ACCESSORS(entry, member, first_value, second_value) \ | |
| 19 TestAccessors(&entry, \ | |
| 20 &decltype(entry)::Get ## member, \ | |
| 21 &decltype(entry)::Set ## member, \ | |
| 22 first_value, \ | |
| 23 second_value); | |
| 24 | |
| 25 #define TEST_STRING16_ACCESSORS(entry, member) \ | |
| 26 TEST_ACCESSORS(entry, member, \ | |
| 27 base::ASCIIToUTF16("first_" #member "_value"), \ | |
| 28 base::ASCIIToUTF16("second_" #member "_value")); | |
| 29 | |
| 30 #define TEST_STRING_ACCESSORS(entry, member) \ | |
| 31 TEST_ACCESSORS(entry, member, \ | |
| 32 std::string("first_" #member "_value"), \ | |
| 33 std::string("second_" #member "_value")); | |
| 34 | |
| 35 #define TEST_BOOL_ACCESSORS(entry, member) \ | |
| 36 TestAccessors(&entry, \ | |
| 37 &decltype(entry)::member, \ | |
| 38 &decltype(entry)::Set ## member, \ | |
| 39 false, \ | |
| 40 true); | |
| 41 | |
| 42 template<typename TValue, typename TGetter, typename TSetter> | |
| 43 void TestAccessors(ProfileAttributesEntry* entry, | |
| 44 TGetter getter_func, | |
| 45 TSetter setter_func, | |
| 46 TValue first_value, | |
| 47 TValue second_value) { | |
| 48 (entry->*setter_func)(first_value); | |
| 49 EXPECT_EQ(first_value, (entry->*getter_func)()); | |
| 50 (entry->*setter_func)(second_value); | |
| 51 EXPECT_EQ(second_value, (entry->*getter_func)()); | |
| 52 } | |
| 53 } // namespace | |
| 54 | |
| 55 class ProfileAttributesStorageTest : public testing::Test { | |
| 56 public: | |
| 57 ProfileAttributesStorageTest() | |
| 58 : testing_profile_manager_(TestingBrowserProcess::GetGlobal()) {} | |
| 59 ~ProfileAttributesStorageTest() override {} | |
| 60 | |
| 61 protected: | |
| 62 void SetUp() override { | |
| 63 ASSERT_TRUE(testing_profile_manager_.SetUp()); | |
| 64 } | |
| 65 | |
| 66 void TearDown() override { | |
| 67 } | |
| 68 | |
| 69 base::FilePath GetProfilePath(const std::string& base_name) { | |
| 70 return testing_profile_manager_.profile_manager()->user_data_dir(). | |
| 71 AppendASCII(base_name); | |
| 72 } | |
| 73 | |
| 74 ProfileAttributesStorage* storage() { | |
| 75 return testing_profile_manager_.profile_info_cache(); | |
| 76 } | |
| 77 | |
| 78 void AddTestingProfile() { | |
| 79 size_t number_of_profiles = storage()->GetNumberOfProfiles(); | |
| 80 | |
| 81 storage()->AddProfile( | |
| 82 GetProfilePath( | |
| 83 base::StringPrintf("testing_profile_path%lu", number_of_profiles)), | |
| 84 base::ASCIIToUTF16( | |
| 85 base::StringPrintf("testing_profile_name%lu", number_of_profiles)), | |
| 86 std::string( | |
| 87 base::StringPrintf("testing_profile_gaia%lu", number_of_profiles)), | |
| 88 base::ASCIIToUTF16( | |
| 89 base::StringPrintf("testing_profile_user%lu", number_of_profiles)), | |
| 90 number_of_profiles, | |
| 91 std::string("")); | |
| 92 | |
| 93 EXPECT_EQ(number_of_profiles + 1, storage()->GetNumberOfProfiles()); | |
| 94 } | |
| 95 | |
| 96 private: | |
| 97 TestingProfileManager testing_profile_manager_; | |
| 98 }; | |
| 99 | |
| 100 TEST_F(ProfileAttributesStorageTest, ProfileNotFound) { | |
| 101 EXPECT_EQ(0U, storage()->GetNumberOfProfiles()); | |
| 102 | |
| 103 ProfileAttributesEntry entry; | |
| 104 ASSERT_FALSE(storage()->GetProfileAttributesWithPath( | |
| 105 GetProfilePath("testing_profile_path0"), &entry)); | |
| 106 | |
| 107 AddTestingProfile(); | |
| 108 EXPECT_EQ(1U, storage()->GetNumberOfProfiles()); | |
| 109 | |
| 110 ASSERT_TRUE(storage()->GetProfileAttributesWithPath( | |
| 111 GetProfilePath("testing_profile_path0"), &entry)); | |
| 112 ASSERT_FALSE(storage()->GetProfileAttributesWithPath( | |
| 113 GetProfilePath("testing_profile_path1"), &entry)); | |
| 114 } | |
| 115 | |
| 116 TEST_F(ProfileAttributesStorageTest, AddProfile) { | |
| 117 EXPECT_EQ(0U, storage()->GetNumberOfProfiles()); | |
| 118 | |
| 119 storage()->AddProfile(GetProfilePath("new_profile_path_1"), | |
| 120 base::ASCIIToUTF16("new_profile_name_1"), | |
| 121 std::string("new_profile_gaia_1"), | |
| 122 base::ASCIIToUTF16("new_profile_username_1"), | |
| 123 1, | |
| 124 std::string("")); | |
| 125 | |
| 126 EXPECT_EQ(1U, storage()->GetNumberOfProfiles()); | |
| 127 | |
| 128 ProfileAttributesEntry entry; | |
| 129 ASSERT_TRUE(storage()->GetProfileAttributesWithPath( | |
| 130 GetProfilePath("new_profile_path_1"), &entry)); | |
| 131 EXPECT_EQ(base::ASCIIToUTF16("new_profile_name_1"), entry.GetName()); | |
| 132 } | |
| 133 | |
| 134 TEST_F(ProfileAttributesStorageTest, DeleteProfile) { | |
| 135 EXPECT_EQ(0U, storage()->GetNumberOfProfiles()); | |
| 136 | |
| 137 ProfileAttributesEntry entry; | |
| 138 ASSERT_FALSE(storage()->GetProfileAttributesWithPath( | |
| 139 GetProfilePath("testing_profile_path0"), &entry)); | |
| 140 | |
| 141 AddTestingProfile(); | |
| 142 EXPECT_EQ(1U, storage()->GetNumberOfProfiles()); | |
| 143 | |
| 144 // Deleting an existing profile should make it un-retrievable. | |
| 145 storage()->DeleteProfile(GetProfilePath("testing_profile_path0")); | |
| 146 EXPECT_EQ(0U, storage()->GetNumberOfProfiles()); | |
| 147 | |
| 148 ASSERT_FALSE(storage()->GetProfileAttributesWithPath( | |
| 149 GetProfilePath("testing_profile_path1"), &entry)); | |
| 150 ASSERT_FALSE(storage()->GetProfileAttributesWithPath( | |
| 151 GetProfilePath("testing_profile_path1"), &entry)); | |
| 152 } | |
| 153 | |
| 154 TEST_F(ProfileAttributesStorageTest, MultipleProfiles) { | |
| 155 EXPECT_EQ(0U, storage()->GetNumberOfProfiles()); | |
| 156 | |
| 157 for (size_t i = 0; i < 5; ++i) { | |
| 158 AddTestingProfile(); | |
| 159 EXPECT_EQ(i + 1, storage()->GetNumberOfProfiles()); | |
| 160 std::vector<ProfileAttributesEntry> entries = | |
| 161 storage()->GetAllProfilesAttributes(); | |
| 162 EXPECT_EQ(i + 1, entries.size()); | |
| 163 } | |
| 164 | |
| 165 EXPECT_EQ(5U, storage()->GetNumberOfProfiles()); | |
| 166 | |
| 167 ProfileAttributesEntry entry; | |
| 168 ASSERT_TRUE(storage()->GetProfileAttributesWithPath( | |
| 169 GetProfilePath("testing_profile_path0"), &entry)); | |
| 170 EXPECT_EQ(base::ASCIIToUTF16("testing_profile_name0"), entry.GetName()); | |
| 171 | |
| 172 storage()->DeleteProfile(GetProfilePath("testing_profile_path0")); | |
| 173 ASSERT_FALSE(storage()->GetProfileAttributesWithPath( | |
| 174 GetProfilePath("testing_profile_path0"), &entry)); | |
| 175 EXPECT_EQ(4U, storage()->GetNumberOfProfiles()); | |
| 176 | |
| 177 std::vector<ProfileAttributesEntry> entries = | |
| 178 storage()->GetAllProfilesAttributes(); | |
| 179 for (auto& entry: entries) { | |
| 180 EXPECT_NE(GetProfilePath("testing_profile_path0"), entry.GetPath()); | |
| 181 } | |
| 182 } | |
| 183 | |
| 184 TEST_F(ProfileAttributesStorageTest, InitialValues) { | |
| 185 AddTestingProfile(); | |
| 186 | |
| 187 ProfileAttributesEntry entry; | |
| 188 ASSERT_TRUE(storage()->GetProfileAttributesWithPath( | |
| 189 GetProfilePath("testing_profile_path0"), &entry)); | |
| 190 EXPECT_EQ(GetProfilePath("testing_profile_path0"), entry.GetPath()); | |
| 191 EXPECT_EQ(base::ASCIIToUTF16("testing_profile_name0"), entry.GetName()); | |
| 192 EXPECT_EQ(std::string("testing_profile_gaia0"), entry.GetGAIAId()); | |
| 193 EXPECT_EQ(base::ASCIIToUTF16("testing_profile_user0"), entry.GetUserName()); | |
| 194 EXPECT_EQ(0U, entry.GetAvatarIconIndex()); | |
| 195 EXPECT_EQ(std::string(""), entry.GetSupervisedUserId()); | |
| 196 } | |
| 197 | |
| 198 TEST_F(ProfileAttributesStorageTest, EntryAccessors) { | |
| 199 AddTestingProfile(); | |
| 200 | |
| 201 ProfileAttributesEntry entry; | |
| 202 ASSERT_TRUE(storage()->GetProfileAttributesWithPath( | |
| 203 GetProfilePath("testing_profile_path0"), &entry)); | |
| 204 | |
| 205 EXPECT_EQ(GetProfilePath("testing_profile_path0"), entry.GetPath()); | |
| 206 | |
| 207 TEST_STRING16_ACCESSORS(entry, Name); | |
| 208 TEST_STRING16_ACCESSORS(entry, ShortcutName); | |
| 209 TEST_STRING_ACCESSORS(entry, LocalAuthCredentials); | |
| 210 TEST_STRING_ACCESSORS(entry, PasswordChangeDetectionToken); | |
| 211 TEST_ACCESSORS(entry, BackgroundStatus, false, true); | |
| 212 TEST_STRING16_ACCESSORS(entry, GAIAName); | |
| 213 TEST_STRING16_ACCESSORS(entry, GAIAGivenName); | |
| 214 TEST_BOOL_ACCESSORS(entry, IsUsingGAIAPicture); | |
| 215 TEST_BOOL_ACCESSORS(entry, IsOmitted); | |
| 216 TEST_BOOL_ACCESSORS(entry, IsSigninRequired); | |
| 217 TEST_STRING_ACCESSORS(entry, SupervisedUserId); | |
| 218 TEST_BOOL_ACCESSORS(entry, IsEphemeral); | |
| 219 TEST_BOOL_ACCESSORS(entry, IsUsingDefaultName); | |
| 220 TEST_BOOL_ACCESSORS(entry, IsUsingDefaultAvatar); | |
| 221 TEST_BOOL_ACCESSORS(entry, IsAuthError); | |
| 222 } | |
| 223 | |
| 224 TEST_F(ProfileAttributesStorageTest, AuthInfo) { | |
| 225 AddTestingProfile(); | |
| 226 | |
| 227 ProfileAttributesEntry entry; | |
| 228 ASSERT_TRUE(storage()->GetProfileAttributesWithPath( | |
| 229 GetProfilePath("testing_profile_path0"), &entry)); | |
| 230 | |
| 231 entry.SetAuthInfo("", base::string16()); | |
| 232 ASSERT_FALSE(entry.IsAuthenticated()); | |
| 233 EXPECT_EQ(base::string16(), entry.GetUserName()); | |
| 234 EXPECT_EQ("", entry.GetGAIAId()); | |
| 235 | |
| 236 entry.SetAuthInfo("foo", base::ASCIIToUTF16("bar")); | |
| 237 ASSERT_TRUE(entry.IsAuthenticated()); | |
| 238 EXPECT_EQ(base::ASCIIToUTF16("bar"), entry.GetUserName()); | |
| 239 EXPECT_EQ("foo", entry.GetGAIAId()); | |
| 240 } | |
| 241 | |
| 242 TEST_F(ProfileAttributesStorageTest, SupervisedUsersAccessors) { | |
| 243 AddTestingProfile(); | |
| 244 | |
| 245 ProfileAttributesEntry entry; | |
| 246 ASSERT_TRUE(storage()->GetProfileAttributesWithPath( | |
| 247 GetProfilePath("testing_profile_path0"), &entry)); | |
| 248 | |
| 249 entry.SetSupervisedUserId(""); | |
| 250 ASSERT_FALSE(entry.IsSupervised()); | |
| 251 ASSERT_FALSE(entry.IsChild()); | |
| 252 ASSERT_FALSE(entry.IsLegacySupervised()); | |
| 253 | |
| 254 entry.SetSupervisedUserId("some_supervised_user_id"); | |
| 255 ASSERT_TRUE(entry.IsSupervised()); | |
| 256 ASSERT_FALSE(entry.IsChild()); | |
| 257 ASSERT_TRUE(entry.IsLegacySupervised()); | |
| 258 | |
| 259 entry.SetSupervisedUserId(supervised_users::kChildAccountSUID); | |
| 260 ASSERT_TRUE(entry.IsSupervised()); | |
| 261 ASSERT_TRUE(entry.IsChild()); | |
| 262 ASSERT_FALSE(entry.IsLegacySupervised()); | |
| 263 } | |
|
Mike Lerman
2015/07/02 18:01:30
Can you add some tests around:
- I get a ProfileAt
anthonyvd
2015/07/07 21:05:32
Done. Also testing deleting from the ProfileInfoCa
Mike Lerman
2015/07/08 18:26:34
Undefined contract - understood. Makes sense.
I a
anthonyvd
2015/07/09 17:02:13
Good point, I added a DCHECK in ProfileInfoAttribu
| |
| OLD | NEW |