Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(104)

Side by Side Diff: chrome/browser/profiles/profile_attributes_storage_unittest.cc

Issue 1214483002: Improve the ProfileInfoCache API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address feedback and slightly change the interface. Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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_type, entry, member, first_value, second_value) \
19 TestAccessors(&entry, \
20 &entry_type::Get ## member, \
21 &entry_type::Set ## member, \
22 first_value, \
23 second_value);
24
25 #define TEST_STRING16_ACCESSORS(entry_type, entry, member) \
26 TEST_ACCESSORS(entry_type, entry, member, \
27 base::ASCIIToUTF16("first_" #member "_value"), \
28 base::ASCIIToUTF16("second_" #member "_value"));
29
30 #define TEST_STRING_ACCESSORS(entry_type, entry, member) \
31 TEST_ACCESSORS(entry_type, entry, member, \
32 std::string("first_" #member "_value"), \
33 std::string("second_" #member "_value"));
34
35 #define TEST_BOOL_ACCESSORS(entry_type, entry, member) \
36 TestAccessors(&entry, \
37 &entry_type::member, \
38 &entry_type::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 profile_info_cache();
76 }
77
78 ProfileInfoCache* profile_info_cache() {
79 return testing_profile_manager_.profile_info_cache();
80 }
81
82 void AddTestingProfile() {
83 size_t number_of_profiles = storage()->GetNumberOfProfiles();
84
85 storage()->AddProfile(
86 GetProfilePath(
87 base::StringPrintf("testing_profile_path%lu", number_of_profiles)),
88 base::ASCIIToUTF16(
89 base::StringPrintf("testing_profile_name%lu", number_of_profiles)),
90 std::string(
91 base::StringPrintf("testing_profile_gaia%lu", number_of_profiles)),
92 base::ASCIIToUTF16(
93 base::StringPrintf("testing_profile_user%lu", number_of_profiles)),
94 number_of_profiles,
95 std::string(""));
96
97 EXPECT_EQ(number_of_profiles + 1, storage()->GetNumberOfProfiles());
98 }
99
100 private:
101 TestingProfileManager testing_profile_manager_;
102 };
103
104 TEST_F(ProfileAttributesStorageTest, ProfileNotFound) {
105 EXPECT_EQ(0U, storage()->GetNumberOfProfiles());
106
107 ProfileAttributesEntry* entry;
108 ASSERT_FALSE(storage()->GetProfileAttributesWithPath(
109 GetProfilePath("testing_profile_path0"), &entry));
110
111 AddTestingProfile();
112 EXPECT_EQ(1U, storage()->GetNumberOfProfiles());
113
114 ASSERT_TRUE(storage()->GetProfileAttributesWithPath(
115 GetProfilePath("testing_profile_path0"), &entry));
116 ASSERT_FALSE(storage()->GetProfileAttributesWithPath(
117 GetProfilePath("testing_profile_path1"), &entry));
118 }
119
120 TEST_F(ProfileAttributesStorageTest, AddProfile) {
121 EXPECT_EQ(0U, storage()->GetNumberOfProfiles());
122
123 storage()->AddProfile(GetProfilePath("new_profile_path_1"),
124 base::ASCIIToUTF16("new_profile_name_1"),
125 std::string("new_profile_gaia_1"),
126 base::ASCIIToUTF16("new_profile_username_1"),
127 1,
128 std::string(""));
129
130 EXPECT_EQ(1U, storage()->GetNumberOfProfiles());
131
132 ProfileAttributesEntry* entry;
133 ASSERT_TRUE(storage()->GetProfileAttributesWithPath(
134 GetProfilePath("new_profile_path_1"), &entry));
135 EXPECT_EQ(base::ASCIIToUTF16("new_profile_name_1"), entry->GetName());
136 }
137
138 TEST_F(ProfileAttributesStorageTest, DeleteProfile) {
139 EXPECT_EQ(0U, storage()->GetNumberOfProfiles());
140
141 ProfileAttributesEntry* entry;
142 ASSERT_FALSE(storage()->GetProfileAttributesWithPath(
143 GetProfilePath("testing_profile_path0"), &entry));
144
145 AddTestingProfile();
146 EXPECT_EQ(1U, storage()->GetNumberOfProfiles());
147
148 // Deleting an existing profile should make it un-retrievable.
149 storage()->DeleteProfile(GetProfilePath("testing_profile_path0"));
150 EXPECT_EQ(0U, storage()->GetNumberOfProfiles());
151
152 ASSERT_FALSE(storage()->GetProfileAttributesWithPath(
153 GetProfilePath("testing_profile_path1"), &entry));
154 ASSERT_FALSE(storage()->GetProfileAttributesWithPath(
155 GetProfilePath("testing_profile_path1"), &entry));
156 }
157
158 TEST_F(ProfileAttributesStorageTest, MultipleProfiles) {
159 EXPECT_EQ(0U, storage()->GetNumberOfProfiles());
160
161 for (size_t i = 0; i < 5; ++i) {
162 AddTestingProfile();
163 EXPECT_EQ(i + 1, storage()->GetNumberOfProfiles());
164 std::vector<ProfileAttributesEntry*> entries =
165 storage()->GetAllProfilesAttributes();
166 EXPECT_EQ(i + 1, entries.size());
167 }
168
169 EXPECT_EQ(5U, storage()->GetNumberOfProfiles());
170
171 ProfileAttributesEntry* entry;
172 ASSERT_TRUE(storage()->GetProfileAttributesWithPath(
173 GetProfilePath("testing_profile_path0"), &entry));
174 EXPECT_EQ(base::ASCIIToUTF16("testing_profile_name0"), entry->GetName());
175
176 storage()->DeleteProfile(GetProfilePath("testing_profile_path0"));
177 ASSERT_FALSE(storage()->GetProfileAttributesWithPath(
178 GetProfilePath("testing_profile_path0"), &entry));
179 EXPECT_EQ(4U, storage()->GetNumberOfProfiles());
180
181 std::vector<ProfileAttributesEntry*> entries =
182 storage()->GetAllProfilesAttributes();
183 for (auto& entry: entries) {
184 EXPECT_NE(GetProfilePath("testing_profile_path0"), entry->GetPath());
185 }
186 }
187
188 TEST_F(ProfileAttributesStorageTest, InitialValues) {
189 AddTestingProfile();
190
191 ProfileAttributesEntry* entry;
192 ASSERT_TRUE(storage()->GetProfileAttributesWithPath(
193 GetProfilePath("testing_profile_path0"), &entry));
194 EXPECT_EQ(GetProfilePath("testing_profile_path0"), entry->GetPath());
195 EXPECT_EQ(base::ASCIIToUTF16("testing_profile_name0"), entry->GetName());
196 EXPECT_EQ(std::string("testing_profile_gaia0"), entry->GetGAIAId());
197 EXPECT_EQ(base::ASCIIToUTF16("testing_profile_user0"), entry->GetUserName());
198 EXPECT_EQ(0U, entry->GetAvatarIconIndex());
199 EXPECT_EQ(std::string(""), entry->GetSupervisedUserId());
200 }
201
202 TEST_F(ProfileAttributesStorageTest, EntryAccessors) {
203 AddTestingProfile();
204
205 ProfileAttributesEntry* entry;
206 ASSERT_TRUE(storage()->GetProfileAttributesWithPath(
207 GetProfilePath("testing_profile_path0"), &entry));
208
209 EXPECT_EQ(GetProfilePath("testing_profile_path0"), entry->GetPath());
210
211 TEST_STRING16_ACCESSORS(ProfileAttributesEntry, entry, Name);
212 TEST_STRING16_ACCESSORS(ProfileAttributesEntry, entry, ShortcutName);
213 TEST_STRING_ACCESSORS(ProfileAttributesEntry, entry, LocalAuthCredentials);
214 TEST_STRING_ACCESSORS(
215 ProfileAttributesEntry, entry, PasswordChangeDetectionToken);
216 TEST_ACCESSORS(ProfileAttributesEntry, entry, BackgroundStatus, false, true);
217 TEST_STRING16_ACCESSORS(ProfileAttributesEntry, entry, GAIAName);
218 TEST_STRING16_ACCESSORS(ProfileAttributesEntry, entry, GAIAGivenName);
219 TEST_BOOL_ACCESSORS(ProfileAttributesEntry, entry, IsUsingGAIAPicture);
220 TEST_BOOL_ACCESSORS(ProfileAttributesEntry, entry, IsOmitted);
221 TEST_BOOL_ACCESSORS(ProfileAttributesEntry, entry, IsSigninRequired);
222 TEST_STRING_ACCESSORS(ProfileAttributesEntry, entry, SupervisedUserId);
223 TEST_BOOL_ACCESSORS(ProfileAttributesEntry, entry, IsEphemeral);
224 TEST_BOOL_ACCESSORS(ProfileAttributesEntry, entry, IsUsingDefaultName);
225 TEST_BOOL_ACCESSORS(ProfileAttributesEntry, entry, IsUsingDefaultAvatar);
226 TEST_BOOL_ACCESSORS(ProfileAttributesEntry, entry, IsAuthError);
227 }
228
229 TEST_F(ProfileAttributesStorageTest, AuthInfo) {
230 AddTestingProfile();
231
232 ProfileAttributesEntry* entry;
233 ASSERT_TRUE(storage()->GetProfileAttributesWithPath(
234 GetProfilePath("testing_profile_path0"), &entry));
235
236 entry->SetAuthInfo("", base::string16());
237 ASSERT_FALSE(entry->IsAuthenticated());
238 EXPECT_EQ(base::string16(), entry->GetUserName());
239 EXPECT_EQ("", entry->GetGAIAId());
240
241 entry->SetAuthInfo("foo", base::ASCIIToUTF16("bar"));
242 ASSERT_TRUE(entry->IsAuthenticated());
243 EXPECT_EQ(base::ASCIIToUTF16("bar"), entry->GetUserName());
244 EXPECT_EQ("foo", entry->GetGAIAId());
245 }
246
247 TEST_F(ProfileAttributesStorageTest, SupervisedUsersAccessors) {
248 AddTestingProfile();
249
250 ProfileAttributesEntry* entry;
251 ASSERT_TRUE(storage()->GetProfileAttributesWithPath(
252 GetProfilePath("testing_profile_path0"), &entry));
253
254 entry->SetSupervisedUserId("");
255 ASSERT_FALSE(entry->IsSupervised());
256 ASSERT_FALSE(entry->IsChild());
257 ASSERT_FALSE(entry->IsLegacySupervised());
258
259 entry->SetSupervisedUserId("some_supervised_user_id");
260 ASSERT_TRUE(entry->IsSupervised());
261 ASSERT_FALSE(entry->IsChild());
262 ASSERT_TRUE(entry->IsLegacySupervised());
263
264 entry->SetSupervisedUserId(supervised_users::kChildAccountSUID);
265 ASSERT_TRUE(entry->IsSupervised());
266 ASSERT_TRUE(entry->IsChild());
267 ASSERT_FALSE(entry->IsLegacySupervised());
268 }
269
270 TEST_F(ProfileAttributesStorageTest, ReSortTriggered) {
271 storage()->AddProfile(GetProfilePath("alpha_path"),
272 base::ASCIIToUTF16("alpha"),
273 std::string("alpha_gaia"),
274 base::ASCIIToUTF16("alpha_username"),
275 1,
276 std::string(""));
277
278 storage()->AddProfile(GetProfilePath("lima_path"),
279 base::ASCIIToUTF16("lima"),
280 std::string("lima_gaia"),
281 base::ASCIIToUTF16("lima_username"),
282 1,
283 std::string(""));
284
285 ProfileAttributesEntry* entry;
286 ASSERT_TRUE(storage()->GetProfileAttributesWithPath(
287 GetProfilePath("alpha_path"), &entry));
288
289 entry->SetName(base::ASCIIToUTF16("zulu_name"));
Mike Lerman 2015/07/08 18:26:35 // Trigger a resort.
anthonyvd 2015/07/09 17:02:13 Done.
290 EXPECT_EQ(GetProfilePath("alpha_path"), entry->GetPath());
291 }
292
293 TEST_F(ProfileAttributesStorageTest, DeleteOtherProfile) {
294 AddTestingProfile();
295 AddTestingProfile();
296 AddTestingProfile();
297
298 EXPECT_EQ(3U, storage()->GetNumberOfProfiles());
299
300 ProfileAttributesEntry* first_entry;
301 ASSERT_TRUE(storage()->GetProfileAttributesWithPath(
302 GetProfilePath("testing_profile_path0"), &first_entry));
303
304 ProfileAttributesEntry* second_entry;
305 ASSERT_TRUE(storage()->GetProfileAttributesWithPath(
306 GetProfilePath("testing_profile_path1"), &second_entry));
307
308 EXPECT_EQ(
309 base::ASCIIToUTF16("testing_profile_name0"),first_entry->GetName());
310
311 storage()->DeleteProfile(GetProfilePath("testing_profile_path1"));
312 ASSERT_FALSE(storage()->GetProfileAttributesWithPath(
313 GetProfilePath("testing_profile_path1"), &second_entry));
314
315 EXPECT_EQ(GetProfilePath("testing_profile_path0"), first_entry->GetPath());
316 EXPECT_EQ(
317 base::ASCIIToUTF16("testing_profile_name0"), first_entry->GetName());
318
319 // Deleting through the ProfileInfoCache should be reflected in the
320 // ProfileAttributesStorage as well.
321 profile_info_cache()->DeleteProfileFromCache(
322 GetProfilePath("testing_profile_path2"));
323 ASSERT_FALSE(storage()->GetProfileAttributesWithPath(
324 GetProfilePath("testing_profile_path2"), &second_entry));
325 }
326
327 TEST_F(ProfileAttributesStorageTest, AccessFromElsewhere) {
328 AddTestingProfile();
329
330 ProfileAttributesEntry* first_entry;
331 ASSERT_TRUE(storage()->GetProfileAttributesWithPath(
332 GetProfilePath("testing_profile_path0"), &first_entry));
333
334 ProfileAttributesEntry* second_entry;
335 ASSERT_TRUE(storage()->GetProfileAttributesWithPath(
336 GetProfilePath("testing_profile_path0"), &second_entry));
337
338 first_entry->SetName(base::ASCIIToUTF16("NewName"));
339 EXPECT_EQ(base::ASCIIToUTF16("NewName"), second_entry->GetName());
Mike Lerman 2015/07/08 18:26:35 We can also test pointer equality. We should have
anthonyvd 2015/07/09 17:02:14 Done.
340
341 // The ProfileInfoCache should also reflect the changes and its changes
342 // should be reflected by the ProfileAttributesStorage.
343 size_t index = profile_info_cache()->GetIndexOfProfileWithPath(
344 GetProfilePath("testing_profile_path0"));
345 EXPECT_EQ(base::ASCIIToUTF16("NewName"),
346 profile_info_cache()->GetNameOfProfileAtIndex(index));
347
348 profile_info_cache()->SetNameOfProfileAtIndex(
349 index, base::ASCIIToUTF16("OtherNewName"));
350 EXPECT_EQ(base::ASCIIToUTF16("OtherNewName"), first_entry->GetName());
351 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698