OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/strings/stringprintf.h" | 5 #include "base/strings/stringprintf.h" |
6 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
| 7 #include "base/values.h" |
7 #include "chrome/browser/profiles/profile_info_cache.h" | 8 #include "chrome/browser/profiles/profile_info_cache.h" |
8 #include "chrome/browser/profiles/profile_manager.h" | 9 #include "chrome/browser/profiles/profile_manager.h" |
9 #include "chrome/browser/supervised_user/supervised_user_constants.h" | 10 #include "chrome/browser/supervised_user/supervised_user_constants.h" |
10 #include "chrome/test/base/testing_browser_process.h" | 11 #include "chrome/test/base/testing_browser_process.h" |
11 #include "chrome/test/base/testing_profile_manager.h" | 12 #include "chrome/test/base/testing_profile_manager.h" |
12 #include "content/public/test/test_browser_thread_bundle.h" | 13 #include "content/public/test/test_browser_thread_bundle.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
14 | 15 |
15 namespace { | 16 namespace { |
16 // The ProfileMetadataEntry accessors aren't just plain old accessors to local | 17 // The ProfileMetadataEntry accessors aren't just plain old accessors to local |
(...skipping 27 matching lines...) Expand all Loading... |
44 void TestAccessors(ProfileAttributesEntry** entry, | 45 void TestAccessors(ProfileAttributesEntry** entry, |
45 TGetter getter_func, | 46 TGetter getter_func, |
46 TSetter setter_func, | 47 TSetter setter_func, |
47 TValue first_value, | 48 TValue first_value, |
48 TValue second_value) { | 49 TValue second_value) { |
49 (*entry->*setter_func)(first_value); | 50 (*entry->*setter_func)(first_value); |
50 EXPECT_EQ(first_value, (*entry->*getter_func)()); | 51 EXPECT_EQ(first_value, (*entry->*getter_func)()); |
51 (*entry->*setter_func)(second_value); | 52 (*entry->*setter_func)(second_value); |
52 EXPECT_EQ(second_value, (*entry->*getter_func)()); | 53 EXPECT_EQ(second_value, (*entry->*getter_func)()); |
53 } | 54 } |
| 55 |
| 56 void VerifyStatistics(const ProfileAttributesEntry * const entry, |
| 57 const std::map<std::string, int>& expect_map, |
| 58 const std::vector<std::string>& categories_to_check) { |
| 59 // Check GetStatistic(). |
| 60 for (const auto& category : categories_to_check) { |
| 61 bool has_category = expect_map.count(category); |
| 62 int actual_value; |
| 63 ASSERT_EQ(has_category, entry->GetStatistic(category, &actual_value)); |
| 64 if (has_category) |
| 65 EXPECT_EQ(expect_map.at(category), actual_value); |
| 66 } |
| 67 |
| 68 // Check GetAllStatistics() |
| 69 const std::map<std::string, int> actual_map = entry->GetAllStatistics(); |
| 70 EXPECT_EQ(expect_map.size(), actual_map.size()); |
| 71 for (const auto& item : expect_map) { |
| 72 ASSERT_TRUE(actual_map.count(item.first)); |
| 73 ASSERT_EQ(item.second, actual_map.at(item.first)); |
| 74 } |
| 75 } |
54 } // namespace | 76 } // namespace |
55 | 77 |
56 class ProfileAttributesStorageTest : public testing::Test { | 78 class ProfileAttributesStorageTest : public testing::Test { |
57 public: | 79 public: |
58 ProfileAttributesStorageTest() | 80 ProfileAttributesStorageTest() |
59 : testing_profile_manager_(TestingBrowserProcess::GetGlobal()) {} | 81 : testing_profile_manager_(TestingBrowserProcess::GetGlobal()) {} |
60 ~ProfileAttributesStorageTest() override {} | 82 ~ProfileAttributesStorageTest() override {} |
61 | 83 |
62 protected: | 84 protected: |
63 void SetUp() override { | 85 void SetUp() override { |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 ASSERT_TRUE(entry->IsSupervised()); | 285 ASSERT_TRUE(entry->IsSupervised()); |
264 ASSERT_FALSE(entry->IsChild()); | 286 ASSERT_FALSE(entry->IsChild()); |
265 ASSERT_TRUE(entry->IsLegacySupervised()); | 287 ASSERT_TRUE(entry->IsLegacySupervised()); |
266 | 288 |
267 entry->SetSupervisedUserId(supervised_users::kChildAccountSUID); | 289 entry->SetSupervisedUserId(supervised_users::kChildAccountSUID); |
268 ASSERT_TRUE(entry->IsSupervised()); | 290 ASSERT_TRUE(entry->IsSupervised()); |
269 ASSERT_TRUE(entry->IsChild()); | 291 ASSERT_TRUE(entry->IsChild()); |
270 ASSERT_FALSE(entry->IsLegacySupervised()); | 292 ASSERT_FALSE(entry->IsLegacySupervised()); |
271 } | 293 } |
272 | 294 |
| 295 TEST_F(ProfileAttributesStorageTest, StatisticsAccessors) { |
| 296 AddTestingProfile(); |
| 297 |
| 298 ProfileAttributesEntry* entry; |
| 299 ASSERT_TRUE(storage()->GetProfileAttributesWithPath( |
| 300 GetProfilePath("testing_profile_path0"), &entry)); |
| 301 |
| 302 EXPECT_EQ(GetProfilePath("testing_profile_path0"), entry->GetPath()); |
| 303 |
| 304 std::vector<std::string> categories_to_check{"One", "Two", "Three"}; |
| 305 std::map<std::string, int> expect_map; |
| 306 std::vector<std::pair<std::string, int>> insertions{ |
| 307 std::make_pair("One", 3), // Insert data |
| 308 std::make_pair("Two", 4), // Insert data |
| 309 std::make_pair("One", 5) // Overwrite data |
| 310 }; |
| 311 |
| 312 // Now no keys are set. |
| 313 VerifyStatistics(entry, expect_map, categories_to_check); |
| 314 // Insert items and test after each insert. |
| 315 for (const auto& item : insertions) { |
| 316 entry->SetStatistic(item.first, item.second); |
| 317 expect_map[item.first] = item.second; |
| 318 VerifyStatistics(entry, expect_map, categories_to_check); |
| 319 } |
| 320 } |
| 321 |
273 TEST_F(ProfileAttributesStorageTest, ReSortTriggered) { | 322 TEST_F(ProfileAttributesStorageTest, ReSortTriggered) { |
274 storage()->AddProfile(GetProfilePath("alpha_path"), | 323 storage()->AddProfile(GetProfilePath("alpha_path"), |
275 base::ASCIIToUTF16("alpha"), | 324 base::ASCIIToUTF16("alpha"), |
276 std::string("alpha_gaia"), | 325 std::string("alpha_gaia"), |
277 base::ASCIIToUTF16("alpha_username"), | 326 base::ASCIIToUTF16("alpha_username"), |
278 1, | 327 1, |
279 std::string("")); | 328 std::string("")); |
280 | 329 |
281 storage()->AddProfile(GetProfilePath("lima_path"), | 330 storage()->AddProfile(GetProfilePath("lima_path"), |
282 base::ASCIIToUTF16("lima"), | 331 base::ASCIIToUTF16("lima"), |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 // should be reflected by the ProfileAttributesStorage. | 396 // should be reflected by the ProfileAttributesStorage. |
348 size_t index = profile_info_cache()->GetIndexOfProfileWithPath( | 397 size_t index = profile_info_cache()->GetIndexOfProfileWithPath( |
349 GetProfilePath("testing_profile_path0")); | 398 GetProfilePath("testing_profile_path0")); |
350 EXPECT_EQ(base::ASCIIToUTF16("NewName"), | 399 EXPECT_EQ(base::ASCIIToUTF16("NewName"), |
351 profile_info_cache()->GetNameOfProfileAtIndex(index)); | 400 profile_info_cache()->GetNameOfProfileAtIndex(index)); |
352 | 401 |
353 profile_info_cache()->SetNameOfProfileAtIndex( | 402 profile_info_cache()->SetNameOfProfileAtIndex( |
354 index, base::ASCIIToUTF16("OtherNewName")); | 403 index, base::ASCIIToUTF16("OtherNewName")); |
355 EXPECT_EQ(base::ASCIIToUTF16("OtherNewName"), first_entry->GetName()); | 404 EXPECT_EQ(base::ASCIIToUTF16("OtherNewName"), first_entry->GetName()); |
356 } | 405 } |
OLD | NEW |