| OLD | NEW |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 #include "components/password_manager/core/browser/test_password_store.h" | 28 #include "components/password_manager/core/browser/test_password_store.h" |
| 29 #include "content/public/test/test_utils.h" | 29 #include "content/public/test/test_utils.h" |
| 30 | 30 |
| 31 namespace { | 31 namespace { |
| 32 | 32 |
| 33 std::set<std::string> stats_categories() { | 33 std::set<std::string> stats_categories() { |
| 34 std::set<std::string> categories; | 34 std::set<std::string> categories; |
| 35 categories.insert(profiles::kProfileStatisticsBrowsingHistory); | 35 categories.insert(profiles::kProfileStatisticsBrowsingHistory); |
| 36 categories.insert(profiles::kProfileStatisticsPasswords); | 36 categories.insert(profiles::kProfileStatisticsPasswords); |
| 37 categories.insert(profiles::kProfileStatisticsBookmarks); | 37 categories.insert(profiles::kProfileStatisticsBookmarks); |
| 38 categories.insert(profiles::kProfileStatisticsSettings); | 38 categories.insert(profiles::kProfileStatisticsAutofill); |
| 39 EXPECT_EQ(4u, categories.size()); | 39 EXPECT_EQ(4u, categories.size()); |
| 40 return categories; | 40 return categories; |
| 41 } | 41 } |
| 42 | 42 |
| 43 bool IsProfileCategoryStatEqual(const profiles::ProfileCategoryStat& a, | 43 bool IsProfileCategoryStatEqual(const profiles::ProfileCategoryStat& a, |
| 44 const profiles::ProfileCategoryStat& b) { | 44 const profiles::ProfileCategoryStat& b) { |
| 45 return a.category == b.category && a.count == b.count && | 45 return a.category == b.category && a.count == b.count; |
| 46 a.success == b.success; | |
| 47 } | 46 } |
| 48 | 47 |
| 49 std::string ProfileCategoryStatToString( | 48 std::string ProfileCategoryStatToString( |
| 50 const profiles::ProfileCategoryStat& a) { | 49 const profiles::ProfileCategoryStat& a) { |
| 51 return base::StringPrintf("category = %s, count = %d, success = %s", | 50 return base::StringPrintf("category = %s, count = %d", a.category.c_str(), |
| 52 a.category.c_str(), a.count, a.success ? "true" : "false"); | 51 a.count); |
| 53 } | 52 } |
| 54 | 53 |
| 55 ::testing::AssertionResult AssertionProfileCategoryStatEqual( | 54 ::testing::AssertionResult AssertionProfileCategoryStatEqual( |
| 56 const char* actual_expression, | 55 const char* actual_expression, |
| 57 const char* expected_expression, | 56 const char* expected_expression, |
| 58 const profiles::ProfileCategoryStat& actual_value, | 57 const profiles::ProfileCategoryStat& actual_value, |
| 59 const profiles::ProfileCategoryStat& expected_value) { | 58 const profiles::ProfileCategoryStat& expected_value) { |
| 60 if (IsProfileCategoryStatEqual(actual_value, expected_value)) { | 59 if (IsProfileCategoryStatEqual(actual_value, expected_value)) { |
| 61 return ::testing::AssertionSuccess(); | 60 return ::testing::AssertionSuccess(); |
| 62 } else { | 61 } else { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 required_stat_count_ = required_stat_count; | 110 required_stat_count_ = required_stat_count; |
| 112 run_loop_.reset(new base::RunLoop()); | 111 run_loop_.reset(new base::RunLoop()); |
| 113 } | 112 } |
| 114 | 113 |
| 115 void WaitForStats() { | 114 void WaitForStats() { |
| 116 run_loop_->Run(); | 115 run_loop_->Run(); |
| 117 run_loop_.reset(); | 116 run_loop_.reset(); |
| 118 } | 117 } |
| 119 | 118 |
| 120 profiles::ProfileCategoryStats GetStats() const { return stats_; } | 119 profiles::ProfileCategoryStats GetStats() const { return stats_; } |
| 121 int GetNumOfFails() const { return num_of_fails_; } | |
| 122 | 120 |
| 123 void StatsCallback(profiles::ProfileCategoryStats stats_return) { | 121 void StatsCallback(profiles::ProfileCategoryStats stats_return) { |
| 124 size_t newCount = stats_return.size(); | 122 size_t newCount = stats_return.size(); |
| 125 // If newCount is 1, then a new GatherStatistics task has started. Discard | 123 // If newCount is 1, then a new GatherStatistics task has started. Discard |
| 126 // the old statistics by setting oldCount to 0 in this case. | 124 // the old statistics by setting oldCount to 0 in this case. |
| 127 size_t oldCount = newCount == 1u ? 0u : stats_.size(); | 125 size_t oldCount = newCount == 1u ? 0u : stats_.size(); |
| 128 | 126 |
| 129 // Only one new statistic arrives at a time. | 127 // Only one new statistic arrives at a time. |
| 130 EXPECT_EQ(oldCount + 1u, newCount); | 128 EXPECT_EQ(oldCount + 1u, newCount); |
| 131 for (size_t i = 0u; i < oldCount; i++) { | 129 for (size_t i = 0u; i < oldCount; i++) { |
| 132 // Exisiting statistics must be the same. | 130 // Exisiting statistics must be the same. |
| 133 EXPECT_PRED_FORMAT2(AssertionProfileCategoryStatEqual, | 131 EXPECT_PRED_FORMAT2(AssertionProfileCategoryStatEqual, |
| 134 stats_[i], stats_return[i]); | 132 stats_[i], stats_return[i]); |
| 135 } | 133 } |
| 136 | 134 |
| 137 num_of_fails_ = 0; | |
| 138 for (size_t i = 0u; i < newCount; i++) { | 135 for (size_t i = 0u; i < newCount; i++) { |
| 139 // The category must be a valid category. | 136 // The category must be a valid category. |
| 140 EXPECT_EQ(1u, stats_categories_.count(stats_return[i].category)); | 137 EXPECT_EQ(1u, stats_categories_.count(stats_return[i].category)); |
| 141 // The categories in |stats_return| must all different. | 138 // The categories in |stats_return| must all different. |
| 142 for (size_t j = 0u; j < i; j++) | 139 for (size_t j = 0u; j < i; j++) |
| 143 EXPECT_NE(stats_return[i].category, stats_return[j].category); | 140 EXPECT_NE(stats_return[i].category, stats_return[j].category); |
| 144 // Count the number of statistics failures. | |
| 145 if (!stats_return[i].success) | |
| 146 num_of_fails_++; | |
| 147 } | 141 } |
| 148 stats_ = stats_return; | 142 stats_ = stats_return; |
| 149 | 143 |
| 150 EXPECT_GE(num_of_stats_categories_, newCount); | 144 EXPECT_GE(num_of_stats_categories_, newCount); |
| 151 if (required_stat_count_ <= newCount) | 145 if (required_stat_count_ <= newCount) |
| 152 run_loop_->Quit(); | 146 run_loop_->Quit(); |
| 153 } | 147 } |
| 154 | 148 |
| 155 private: | 149 private: |
| 156 std::set<std::string> stats_categories_; | 150 std::set<std::string> stats_categories_; |
| 157 size_t num_of_stats_categories_; | 151 size_t num_of_stats_categories_; |
| 158 size_t required_stat_count_; | 152 size_t required_stat_count_; |
| 159 std::unique_ptr<base::RunLoop> run_loop_; | 153 std::unique_ptr<base::RunLoop> run_loop_; |
| 160 | 154 |
| 161 profiles::ProfileCategoryStats stats_; | 155 profiles::ProfileCategoryStats stats_; |
| 162 int num_of_fails_ = 0; | |
| 163 }; | 156 }; |
| 164 | 157 |
| 165 } // namespace | 158 } // namespace |
| 166 | 159 |
| 167 class ProfileStatisticsBrowserTest : public InProcessBrowserTest { | 160 class ProfileStatisticsBrowserTest : public InProcessBrowserTest { |
| 168 public: | 161 public: |
| 169 void SetUpOnMainThread() override { | 162 void SetUpOnMainThread() override { |
| 170 // Use TestPasswordStore to remove a possible race. Normally the | 163 // Use TestPasswordStore to remove a possible race. Normally the |
| 171 // PasswordStore does its database manipulation on the DB thread, which | 164 // PasswordStore does its database manipulation on the DB thread, which |
| 172 // creates a possible race during navigation. Specifically the | 165 // creates a possible race during navigation. Specifically the |
| (...skipping 13 matching lines...) Expand all Loading... |
| 186 ASSERT_TRUE(profile); | 179 ASSERT_TRUE(profile); |
| 187 ProfileStatistics* profile_stat = | 180 ProfileStatistics* profile_stat = |
| 188 ProfileStatisticsFactory::GetForProfile(profile); | 181 ProfileStatisticsFactory::GetForProfile(profile); |
| 189 | 182 |
| 190 ProfileStatisticsAggregatorState state; | 183 ProfileStatisticsAggregatorState state; |
| 191 profile_stat->GatherStatistics( | 184 profile_stat->GatherStatistics( |
| 192 base::Bind(&ProfileStatisticsAggregatorState::StatsCallback, | 185 base::Bind(&ProfileStatisticsAggregatorState::StatsCallback, |
| 193 base::Unretained(&state))); | 186 base::Unretained(&state))); |
| 194 state.WaitForStats(); | 187 state.WaitForStats(); |
| 195 | 188 |
| 196 EXPECT_EQ(0, state.GetNumOfFails()); | |
| 197 | |
| 198 profiles::ProfileCategoryStats stats = state.GetStats(); | 189 profiles::ProfileCategoryStats stats = state.GetStats(); |
| 199 for (const auto& stat : stats) { | 190 for (const auto& stat : stats) { |
| 200 if (stat.category != profiles::kProfileStatisticsSettings) | |
| 201 EXPECT_EQ(0, stat.count); | 191 EXPECT_EQ(0, stat.count); |
| 202 } | 192 } |
| 203 } | 193 } |
| 204 | 194 |
| 205 IN_PROC_BROWSER_TEST_F(ProfileStatisticsBrowserTest, | 195 IN_PROC_BROWSER_TEST_F(ProfileStatisticsBrowserTest, |
| 206 GatherStatisticsTwoCallbacks) { | 196 GatherStatisticsTwoCallbacks) { |
| 207 Profile* profile = ProfileManager::GetActiveUserProfile(); | 197 Profile* profile = ProfileManager::GetActiveUserProfile(); |
| 208 ASSERT_TRUE(profile); | 198 ASSERT_TRUE(profile); |
| 209 ProfileStatistics* profile_stat = | 199 ProfileStatistics* profile_stat = |
| 210 ProfileStatisticsFactory::GetForProfile(profile); | 200 ProfileStatisticsFactory::GetForProfile(profile); |
| 211 | 201 |
| 212 ProfileStatisticsAggregatorState state1(1u); | 202 ProfileStatisticsAggregatorState state1(1u); |
| 213 ProfileStatisticsAggregatorState state2; | 203 ProfileStatisticsAggregatorState state2; |
| 214 | 204 |
| 215 profile_stat->GatherStatistics( | 205 profile_stat->GatherStatistics( |
| 216 base::Bind(&ProfileStatisticsAggregatorState::StatsCallback, | 206 base::Bind(&ProfileStatisticsAggregatorState::StatsCallback, |
| 217 base::Unretained(&state1))); | 207 base::Unretained(&state1))); |
| 218 state1.WaitForStats(); | 208 state1.WaitForStats(); |
| 219 | 209 |
| 220 state1.SetRequiredStatCountAndCreateRunLoop(stats_categories().size()); | 210 state1.SetRequiredStatCountAndCreateRunLoop(stats_categories().size()); |
| 221 | 211 |
| 222 profile_stat->GatherStatistics( | 212 profile_stat->GatherStatistics( |
| 223 base::Bind(&ProfileStatisticsAggregatorState::StatsCallback, | 213 base::Bind(&ProfileStatisticsAggregatorState::StatsCallback, |
| 224 base::Unretained(&state2))); | 214 base::Unretained(&state2))); |
| 225 state1.WaitForStats(); | 215 state1.WaitForStats(); |
| 226 state2.WaitForStats(); | 216 state2.WaitForStats(); |
| 227 | 217 |
| 228 EXPECT_EQ(0, state1.GetNumOfFails()); | |
| 229 | |
| 230 EXPECT_PRED_FORMAT2(AssertionProfileCategoryStatsEqual, | 218 EXPECT_PRED_FORMAT2(AssertionProfileCategoryStatsEqual, |
| 231 state1.GetStats(), state2.GetStats()); | 219 state1.GetStats(), state2.GetStats()); |
| 232 } | 220 } |
| OLD | NEW |