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.
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 "content/public/test/test_browser_thread_bundle.h"
13 #include "testing/gtest/include/gtest/gtest.h"
16 // The ProfileMetadataEntry accessors aren't just plain old accessors to local
17 // members so they'll be tested. The following helpers will make the testing
19 #define TEST_ACCESSORS(entry_type, entry, member, first_value, second_value) \
20 TestAccessors(&entry, \
21 &entry_type::Get ## member, \
22 &entry_type::Set ## member, \
26 #define TEST_STRING16_ACCESSORS(entry_type, entry, member) \
27 TEST_ACCESSORS(entry_type, entry, member, \
28 base::ASCIIToUTF16("first_" #member "_value"), \
29 base::ASCIIToUTF16("second_" #member "_value"));
31 #define TEST_STRING_ACCESSORS(entry_type, entry, member) \
32 TEST_ACCESSORS(entry_type, entry, member, \
33 std::string("first_" #member "_value"), \
34 std::string("second_" #member "_value"));
36 #define TEST_BOOL_ACCESSORS(entry_type, entry, member) \
37 TestAccessors(&entry, \
38 &entry_type::member, \
39 &entry_type::Set ## member, \
43 template<typename TValue
, typename TGetter
, typename TSetter
>
44 void TestAccessors(ProfileAttributesEntry
** entry
,
48 TValue second_value
) {
49 (*entry
->*setter_func
)(first_value
);
50 EXPECT_EQ(first_value
, (*entry
->*getter_func
)());
51 (*entry
->*setter_func
)(second_value
);
52 EXPECT_EQ(second_value
, (*entry
->*getter_func
)());
56 class ProfileAttributesStorageTest
: public testing::Test
{
58 ProfileAttributesStorageTest()
59 : testing_profile_manager_(TestingBrowserProcess::GetGlobal()) {}
60 ~ProfileAttributesStorageTest() override
{}
63 void SetUp() override
{
64 ASSERT_TRUE(testing_profile_manager_
.SetUp());
67 void TearDown() override
{
70 base::FilePath
GetProfilePath(const std::string
& base_name
) {
71 return testing_profile_manager_
.profile_manager()->user_data_dir().
72 AppendASCII(base_name
);
75 ProfileAttributesStorage
* storage() {
76 return profile_info_cache();
79 ProfileInfoCache
* profile_info_cache() {
80 return testing_profile_manager_
.profile_info_cache();
83 void AddTestingProfile() {
84 unsigned long number_of_profiles
=
85 static_cast<unsigned long>(storage()->GetNumberOfProfiles());
87 storage()->AddProfile(
89 base::StringPrintf("testing_profile_path%lu", number_of_profiles
)),
91 base::StringPrintf("testing_profile_name%lu", number_of_profiles
)),
93 base::StringPrintf("testing_profile_gaia%lu", number_of_profiles
)),
95 base::StringPrintf("testing_profile_user%lu", number_of_profiles
)),
99 EXPECT_EQ(number_of_profiles
+ 1, storage()->GetNumberOfProfiles());
103 TestingProfileManager testing_profile_manager_
;
104 content::TestBrowserThreadBundle thread_bundle_
;
107 TEST_F(ProfileAttributesStorageTest
, ProfileNotFound
) {
108 EXPECT_EQ(0U, storage()->GetNumberOfProfiles());
110 ProfileAttributesEntry
* entry
;
111 ASSERT_FALSE(storage()->GetProfileAttributesWithPath(
112 GetProfilePath("testing_profile_path0"), &entry
));
115 EXPECT_EQ(1U, storage()->GetNumberOfProfiles());
117 ASSERT_TRUE(storage()->GetProfileAttributesWithPath(
118 GetProfilePath("testing_profile_path0"), &entry
));
119 ASSERT_FALSE(storage()->GetProfileAttributesWithPath(
120 GetProfilePath("testing_profile_path1"), &entry
));
123 TEST_F(ProfileAttributesStorageTest
, AddProfile
) {
124 EXPECT_EQ(0U, storage()->GetNumberOfProfiles());
126 storage()->AddProfile(GetProfilePath("new_profile_path_1"),
127 base::ASCIIToUTF16("new_profile_name_1"),
128 std::string("new_profile_gaia_1"),
129 base::ASCIIToUTF16("new_profile_username_1"),
133 EXPECT_EQ(1U, storage()->GetNumberOfProfiles());
135 ProfileAttributesEntry
* entry
;
136 ASSERT_TRUE(storage()->GetProfileAttributesWithPath(
137 GetProfilePath("new_profile_path_1"), &entry
));
138 EXPECT_EQ(base::ASCIIToUTF16("new_profile_name_1"), entry
->GetName());
141 TEST_F(ProfileAttributesStorageTest
, RemoveProfile
) {
142 EXPECT_EQ(0U, storage()->GetNumberOfProfiles());
144 ProfileAttributesEntry
* entry
;
145 ASSERT_FALSE(storage()->GetProfileAttributesWithPath(
146 GetProfilePath("testing_profile_path0"), &entry
));
149 EXPECT_EQ(1U, storage()->GetNumberOfProfiles());
151 // Deleting an existing profile should make it un-retrievable.
152 storage()->RemoveProfile(GetProfilePath("testing_profile_path0"));
153 EXPECT_EQ(0U, storage()->GetNumberOfProfiles());
155 ASSERT_FALSE(storage()->GetProfileAttributesWithPath(
156 GetProfilePath("testing_profile_path1"), &entry
));
157 ASSERT_FALSE(storage()->GetProfileAttributesWithPath(
158 GetProfilePath("testing_profile_path1"), &entry
));
161 TEST_F(ProfileAttributesStorageTest
, MultipleProfiles
) {
162 EXPECT_EQ(0U, storage()->GetNumberOfProfiles());
164 for (size_t i
= 0; i
< 5; ++i
) {
166 EXPECT_EQ(i
+ 1, storage()->GetNumberOfProfiles());
167 std::vector
<ProfileAttributesEntry
*> entries
=
168 storage()->GetAllProfilesAttributes();
169 EXPECT_EQ(i
+ 1, entries
.size());
172 EXPECT_EQ(5U, storage()->GetNumberOfProfiles());
174 ProfileAttributesEntry
* entry
;
175 ASSERT_TRUE(storage()->GetProfileAttributesWithPath(
176 GetProfilePath("testing_profile_path0"), &entry
));
177 EXPECT_EQ(base::ASCIIToUTF16("testing_profile_name0"), entry
->GetName());
179 storage()->RemoveProfile(GetProfilePath("testing_profile_path0"));
180 ASSERT_FALSE(storage()->GetProfileAttributesWithPath(
181 GetProfilePath("testing_profile_path0"), &entry
));
182 EXPECT_EQ(4U, storage()->GetNumberOfProfiles());
184 std::vector
<ProfileAttributesEntry
*> entries
=
185 storage()->GetAllProfilesAttributes();
186 for (auto& entry
: entries
) {
187 EXPECT_NE(GetProfilePath("testing_profile_path0"), entry
->GetPath());
191 TEST_F(ProfileAttributesStorageTest
, InitialValues
) {
194 ProfileAttributesEntry
* entry
;
195 ASSERT_TRUE(storage()->GetProfileAttributesWithPath(
196 GetProfilePath("testing_profile_path0"), &entry
));
197 EXPECT_EQ(GetProfilePath("testing_profile_path0"), entry
->GetPath());
198 EXPECT_EQ(base::ASCIIToUTF16("testing_profile_name0"), entry
->GetName());
199 EXPECT_EQ(std::string("testing_profile_gaia0"), entry
->GetGAIAId());
200 EXPECT_EQ(base::ASCIIToUTF16("testing_profile_user0"), entry
->GetUserName());
201 EXPECT_EQ(0U, entry
->GetAvatarIconIndex());
202 EXPECT_EQ(std::string(""), entry
->GetSupervisedUserId());
205 TEST_F(ProfileAttributesStorageTest
, EntryAccessors
) {
208 ProfileAttributesEntry
* entry
;
209 ASSERT_TRUE(storage()->GetProfileAttributesWithPath(
210 GetProfilePath("testing_profile_path0"), &entry
));
212 EXPECT_EQ(GetProfilePath("testing_profile_path0"), entry
->GetPath());
214 TEST_STRING16_ACCESSORS(ProfileAttributesEntry
, entry
, Name
);
215 TEST_STRING16_ACCESSORS(ProfileAttributesEntry
, entry
, ShortcutName
);
216 TEST_STRING_ACCESSORS(ProfileAttributesEntry
, entry
, LocalAuthCredentials
);
217 TEST_STRING_ACCESSORS(
218 ProfileAttributesEntry
, entry
, PasswordChangeDetectionToken
);
219 TEST_ACCESSORS(ProfileAttributesEntry
, entry
, BackgroundStatus
, false, true);
220 TEST_STRING16_ACCESSORS(ProfileAttributesEntry
, entry
, GAIAName
);
221 TEST_STRING16_ACCESSORS(ProfileAttributesEntry
, entry
, GAIAGivenName
);
222 TEST_BOOL_ACCESSORS(ProfileAttributesEntry
, entry
, IsUsingGAIAPicture
);
223 TEST_BOOL_ACCESSORS(ProfileAttributesEntry
, entry
, IsOmitted
);
224 TEST_BOOL_ACCESSORS(ProfileAttributesEntry
, entry
, IsSigninRequired
);
225 TEST_STRING_ACCESSORS(ProfileAttributesEntry
, entry
, SupervisedUserId
);
226 TEST_BOOL_ACCESSORS(ProfileAttributesEntry
, entry
, IsEphemeral
);
227 TEST_BOOL_ACCESSORS(ProfileAttributesEntry
, entry
, IsUsingDefaultName
);
228 TEST_BOOL_ACCESSORS(ProfileAttributesEntry
, entry
, IsUsingDefaultAvatar
);
229 TEST_BOOL_ACCESSORS(ProfileAttributesEntry
, entry
, IsAuthError
);
232 TEST_F(ProfileAttributesStorageTest
, AuthInfo
) {
235 ProfileAttributesEntry
* entry
;
236 ASSERT_TRUE(storage()->GetProfileAttributesWithPath(
237 GetProfilePath("testing_profile_path0"), &entry
));
239 entry
->SetAuthInfo("", base::string16());
240 ASSERT_FALSE(entry
->IsAuthenticated());
241 EXPECT_EQ(base::string16(), entry
->GetUserName());
242 EXPECT_EQ("", entry
->GetGAIAId());
244 entry
->SetAuthInfo("foo", base::ASCIIToUTF16("bar"));
245 ASSERT_TRUE(entry
->IsAuthenticated());
246 EXPECT_EQ(base::ASCIIToUTF16("bar"), entry
->GetUserName());
247 EXPECT_EQ("foo", entry
->GetGAIAId());
250 TEST_F(ProfileAttributesStorageTest
, SupervisedUsersAccessors
) {
253 ProfileAttributesEntry
* entry
;
254 ASSERT_TRUE(storage()->GetProfileAttributesWithPath(
255 GetProfilePath("testing_profile_path0"), &entry
));
257 entry
->SetSupervisedUserId("");
258 ASSERT_FALSE(entry
->IsSupervised());
259 ASSERT_FALSE(entry
->IsChild());
260 ASSERT_FALSE(entry
->IsLegacySupervised());
262 entry
->SetSupervisedUserId("some_supervised_user_id");
263 ASSERT_TRUE(entry
->IsSupervised());
264 ASSERT_FALSE(entry
->IsChild());
265 ASSERT_TRUE(entry
->IsLegacySupervised());
267 entry
->SetSupervisedUserId(supervised_users::kChildAccountSUID
);
268 ASSERT_TRUE(entry
->IsSupervised());
269 ASSERT_TRUE(entry
->IsChild());
270 ASSERT_FALSE(entry
->IsLegacySupervised());
273 TEST_F(ProfileAttributesStorageTest
, ReSortTriggered
) {
274 storage()->AddProfile(GetProfilePath("alpha_path"),
275 base::ASCIIToUTF16("alpha"),
276 std::string("alpha_gaia"),
277 base::ASCIIToUTF16("alpha_username"),
281 storage()->AddProfile(GetProfilePath("lima_path"),
282 base::ASCIIToUTF16("lima"),
283 std::string("lima_gaia"),
284 base::ASCIIToUTF16("lima_username"),
288 ProfileAttributesEntry
* entry
;
289 ASSERT_TRUE(storage()->GetProfileAttributesWithPath(
290 GetProfilePath("alpha_path"), &entry
));
292 // Trigger a ProfileInfoCache re-sort.
293 entry
->SetName(base::ASCIIToUTF16("zulu_name"));
294 EXPECT_EQ(GetProfilePath("alpha_path"), entry
->GetPath());
297 TEST_F(ProfileAttributesStorageTest
, RemoveOtherProfile
) {
302 EXPECT_EQ(3U, storage()->GetNumberOfProfiles());
304 ProfileAttributesEntry
* first_entry
;
305 ASSERT_TRUE(storage()->GetProfileAttributesWithPath(
306 GetProfilePath("testing_profile_path0"), &first_entry
));
308 ProfileAttributesEntry
* second_entry
;
309 ASSERT_TRUE(storage()->GetProfileAttributesWithPath(
310 GetProfilePath("testing_profile_path1"), &second_entry
));
313 base::ASCIIToUTF16("testing_profile_name0"),first_entry
->GetName());
315 storage()->RemoveProfile(GetProfilePath("testing_profile_path1"));
316 ASSERT_FALSE(storage()->GetProfileAttributesWithPath(
317 GetProfilePath("testing_profile_path1"), &second_entry
));
319 EXPECT_EQ(GetProfilePath("testing_profile_path0"), first_entry
->GetPath());
321 base::ASCIIToUTF16("testing_profile_name0"), first_entry
->GetName());
323 // Deleting through the ProfileInfoCache should be reflected in the
324 // ProfileAttributesStorage as well.
325 profile_info_cache()->RemoveProfile(
326 GetProfilePath("testing_profile_path2"));
327 ASSERT_FALSE(storage()->GetProfileAttributesWithPath(
328 GetProfilePath("testing_profile_path2"), &second_entry
));
331 TEST_F(ProfileAttributesStorageTest
, AccessFromElsewhere
) {
334 ProfileAttributesEntry
* first_entry
;
335 ASSERT_TRUE(storage()->GetProfileAttributesWithPath(
336 GetProfilePath("testing_profile_path0"), &first_entry
));
338 ProfileAttributesEntry
* second_entry
;
339 ASSERT_TRUE(storage()->GetProfileAttributesWithPath(
340 GetProfilePath("testing_profile_path0"), &second_entry
));
342 first_entry
->SetName(base::ASCIIToUTF16("NewName"));
343 EXPECT_EQ(base::ASCIIToUTF16("NewName"), second_entry
->GetName());
344 EXPECT_EQ(first_entry
, second_entry
);
346 // The ProfileInfoCache should also reflect the changes and its changes
347 // should be reflected by the ProfileAttributesStorage.
348 size_t index
= profile_info_cache()->GetIndexOfProfileWithPath(
349 GetProfilePath("testing_profile_path0"));
350 EXPECT_EQ(base::ASCIIToUTF16("NewName"),
351 profile_info_cache()->GetNameOfProfileAtIndex(index
));
353 profile_info_cache()->SetNameOfProfileAtIndex(
354 index
, base::ASCIIToUTF16("OtherNewName"));
355 EXPECT_EQ(base::ASCIIToUTF16("OtherNewName"), first_entry
->GetName());