| OLD | NEW |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "chrome/browser/browsing_data/autofill_counter.h" | 5 #include "components/browsing_data/core/counters/autofill_counter.h" |
| 6 | 6 |
| 7 #include "base/guid.h" | 7 #include "base/guid.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "base/threading/platform_thread.h" | 13 #include "base/threading/platform_thread.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 void SetAutofillDeletionPref(bool value) { | 135 void SetAutofillDeletionPref(bool value) { |
| 136 browser()->profile()->GetPrefs()->SetBoolean( | 136 browser()->profile()->GetPrefs()->SetBoolean( |
| 137 browsing_data::prefs::kDeleteFormData, value); | 137 browsing_data::prefs::kDeleteFormData, value); |
| 138 } | 138 } |
| 139 | 139 |
| 140 void SetDeletionPeriodPref(browsing_data::TimePeriod period) { | 140 void SetDeletionPeriodPref(browsing_data::TimePeriod period) { |
| 141 browser()->profile()->GetPrefs()->SetInteger( | 141 browser()->profile()->GetPrefs()->SetInteger( |
| 142 browsing_data::prefs::kDeleteTimePeriod, static_cast<int>(period)); | 142 browsing_data::prefs::kDeleteTimePeriod, static_cast<int>(period)); |
| 143 } | 143 } |
| 144 | 144 |
| 145 scoped_refptr<autofill::AutofillWebDataService> GetWebDataService() { |
| 146 return web_data_service_; |
| 147 } |
| 148 |
| 145 // Callback and result retrieval --------------------------------------------- | 149 // Callback and result retrieval --------------------------------------------- |
| 146 | 150 |
| 147 void WaitForCounting() { | 151 void WaitForCounting() { |
| 148 run_loop_.reset(new base::RunLoop()); | 152 run_loop_.reset(new base::RunLoop()); |
| 149 run_loop_->Run(); | 153 run_loop_->Run(); |
| 150 } | 154 } |
| 151 | 155 |
| 152 browsing_data::BrowsingDataCounter::ResultInt GetNumSuggestions() { | 156 browsing_data::BrowsingDataCounter::ResultInt GetNumSuggestions() { |
| 153 DCHECK(finished_); | 157 DCHECK(finished_); |
| 154 return num_suggestions_; | 158 return num_suggestions_; |
| 155 } | 159 } |
| 156 | 160 |
| 157 browsing_data::BrowsingDataCounter::ResultInt GetNumCreditCards() { | 161 browsing_data::BrowsingDataCounter::ResultInt GetNumCreditCards() { |
| 158 DCHECK(finished_); | 162 DCHECK(finished_); |
| 159 return num_credit_cards_; | 163 return num_credit_cards_; |
| 160 } | 164 } |
| 161 | 165 |
| 162 browsing_data::BrowsingDataCounter::ResultInt GetNumAddresses() { | 166 browsing_data::BrowsingDataCounter::ResultInt GetNumAddresses() { |
| 163 DCHECK(finished_); | 167 DCHECK(finished_); |
| 164 return num_addresses_; | 168 return num_addresses_; |
| 165 } | 169 } |
| 166 | 170 |
| 167 void Callback( | 171 void Callback( |
| 168 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) { | 172 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) { |
| 169 finished_ = result->Finished(); | 173 finished_ = result->Finished(); |
| 170 | 174 |
| 171 if (finished_) { | 175 if (finished_) { |
| 172 AutofillCounter::AutofillResult* autofill_result = | 176 browsing_data::AutofillCounter::AutofillResult* autofill_result = |
| 173 static_cast<AutofillCounter::AutofillResult*>(result.get()); | 177 static_cast<browsing_data::AutofillCounter::AutofillResult*>( |
| 178 result.get()); |
| 174 | 179 |
| 175 num_suggestions_ = autofill_result->Value(); | 180 num_suggestions_ = autofill_result->Value(); |
| 176 num_credit_cards_ = autofill_result->num_credit_cards(); | 181 num_credit_cards_ = autofill_result->num_credit_cards(); |
| 177 num_addresses_ = autofill_result->num_addresses(); | 182 num_addresses_ = autofill_result->num_addresses(); |
| 178 } | 183 } |
| 179 | 184 |
| 180 if (run_loop_ && finished_) | 185 if (run_loop_ && finished_) |
| 181 run_loop_->Quit(); | 186 run_loop_->Quit(); |
| 182 } | 187 } |
| 183 | 188 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 196 | 201 |
| 197 DISALLOW_COPY_AND_ASSIGN(AutofillCounterTest); | 202 DISALLOW_COPY_AND_ASSIGN(AutofillCounterTest); |
| 198 }; | 203 }; |
| 199 | 204 |
| 200 // Tests that the counter does not count when the form data deletion preference | 205 // Tests that the counter does not count when the form data deletion preference |
| 201 // is false. | 206 // is false. |
| 202 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, PrefIsFalse) { | 207 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, PrefIsFalse) { |
| 203 SetAutofillDeletionPref(false); | 208 SetAutofillDeletionPref(false); |
| 204 | 209 |
| 205 Profile* profile = browser()->profile(); | 210 Profile* profile = browser()->profile(); |
| 206 AutofillCounter counter(profile); | 211 browsing_data::AutofillCounter counter(GetWebDataService()); |
| 207 counter.Init(profile->GetPrefs(), base::Bind(&AutofillCounterTest::Callback, | 212 counter.Init(profile->GetPrefs(), base::Bind(&AutofillCounterTest::Callback, |
| 208 base::Unretained(this))); | 213 base::Unretained(this))); |
| 209 counter.Restart(); | 214 counter.Restart(); |
| 210 | 215 |
| 211 EXPECT_FALSE(counter.HasPendingQuery()); | 216 EXPECT_FALSE(counter.HasPendingQuery()); |
| 212 } | 217 } |
| 213 | 218 |
| 214 // Tests that we count the correct number of autocomplete suggestions. | 219 // Tests that we count the correct number of autocomplete suggestions. |
| 215 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, AutocompleteSuggestions) { | 220 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, AutocompleteSuggestions) { |
| 216 Profile* profile = browser()->profile(); | 221 Profile* profile = browser()->profile(); |
| 217 AutofillCounter counter(profile); | 222 browsing_data::AutofillCounter counter(GetWebDataService()); |
| 218 counter.Init(profile->GetPrefs(), base::Bind(&AutofillCounterTest::Callback, | 223 counter.Init(profile->GetPrefs(), base::Bind(&AutofillCounterTest::Callback, |
| 219 base::Unretained(this))); | 224 base::Unretained(this))); |
| 220 counter.Restart(); | 225 counter.Restart(); |
| 221 WaitForCounting(); | 226 WaitForCounting(); |
| 222 EXPECT_EQ(0, GetNumSuggestions()); | 227 EXPECT_EQ(0, GetNumSuggestions()); |
| 223 | 228 |
| 224 AddAutocompleteSuggestion("email", "example@example.com"); | 229 AddAutocompleteSuggestion("email", "example@example.com"); |
| 225 counter.Restart(); | 230 counter.Restart(); |
| 226 WaitForCounting(); | 231 WaitForCounting(); |
| 227 EXPECT_EQ(1, GetNumSuggestions()); | 232 EXPECT_EQ(1, GetNumSuggestions()); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 243 | 248 |
| 244 ClearAutocompleteSuggestions(); | 249 ClearAutocompleteSuggestions(); |
| 245 counter.Restart(); | 250 counter.Restart(); |
| 246 WaitForCounting(); | 251 WaitForCounting(); |
| 247 EXPECT_EQ(0, GetNumSuggestions()); | 252 EXPECT_EQ(0, GetNumSuggestions()); |
| 248 } | 253 } |
| 249 | 254 |
| 250 // Tests that we count the correct number of credit cards. | 255 // Tests that we count the correct number of credit cards. |
| 251 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, CreditCards) { | 256 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, CreditCards) { |
| 252 Profile* profile = browser()->profile(); | 257 Profile* profile = browser()->profile(); |
| 253 AutofillCounter counter(profile); | 258 browsing_data::AutofillCounter counter(GetWebDataService()); |
| 254 counter.Init(profile->GetPrefs(), base::Bind(&AutofillCounterTest::Callback, | 259 counter.Init(profile->GetPrefs(), base::Bind(&AutofillCounterTest::Callback, |
| 255 base::Unretained(this))); | 260 base::Unretained(this))); |
| 256 counter.Restart(); | 261 counter.Restart(); |
| 257 WaitForCounting(); | 262 WaitForCounting(); |
| 258 EXPECT_EQ(0, GetNumCreditCards()); | 263 EXPECT_EQ(0, GetNumCreditCards()); |
| 259 | 264 |
| 260 AddCreditCard("0000-0000-0000-0000", 1, 2015); | 265 AddCreditCard("0000-0000-0000-0000", 1, 2015); |
| 261 counter.Restart(); | 266 counter.Restart(); |
| 262 WaitForCounting(); | 267 WaitForCounting(); |
| 263 EXPECT_EQ(1, GetNumCreditCards()); | 268 EXPECT_EQ(1, GetNumCreditCards()); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 279 | 284 |
| 280 ClearCreditCardsAndAddresses(); | 285 ClearCreditCardsAndAddresses(); |
| 281 counter.Restart(); | 286 counter.Restart(); |
| 282 WaitForCounting(); | 287 WaitForCounting(); |
| 283 EXPECT_EQ(0, GetNumCreditCards()); | 288 EXPECT_EQ(0, GetNumCreditCards()); |
| 284 } | 289 } |
| 285 | 290 |
| 286 // Tests that we count the correct number of addresses. | 291 // Tests that we count the correct number of addresses. |
| 287 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, Addresses) { | 292 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, Addresses) { |
| 288 Profile* profile = browser()->profile(); | 293 Profile* profile = browser()->profile(); |
| 289 AutofillCounter counter(profile); | 294 browsing_data::AutofillCounter counter(GetWebDataService()); |
| 290 counter.Init(profile->GetPrefs(), base::Bind(&AutofillCounterTest::Callback, | 295 counter.Init(profile->GetPrefs(), base::Bind(&AutofillCounterTest::Callback, |
| 291 base::Unretained(this))); | 296 base::Unretained(this))); |
| 292 counter.Restart(); | 297 counter.Restart(); |
| 293 WaitForCounting(); | 298 WaitForCounting(); |
| 294 EXPECT_EQ(0, GetNumAddresses()); | 299 EXPECT_EQ(0, GetNumAddresses()); |
| 295 | 300 |
| 296 AddAddress("John", "Doe", "Main Street 12345"); | 301 AddAddress("John", "Doe", "Main Street 12345"); |
| 297 counter.Restart(); | 302 counter.Restart(); |
| 298 WaitForCounting(); | 303 WaitForCounting(); |
| 299 EXPECT_EQ(1, GetNumAddresses()); | 304 EXPECT_EQ(1, GetNumAddresses()); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 329 AddAutocompleteSuggestion("city", "Munich"); | 334 AddAutocompleteSuggestion("city", "Munich"); |
| 330 | 335 |
| 331 AddCreditCard("0000-0000-0000-0000", 1, 2015); | 336 AddCreditCard("0000-0000-0000-0000", 1, 2015); |
| 332 AddCreditCard("1211-1098-7654-3210", 10, 2030); | 337 AddCreditCard("1211-1098-7654-3210", 10, 2030); |
| 333 | 338 |
| 334 AddAddress("John", "Doe", "Main Street 12345"); | 339 AddAddress("John", "Doe", "Main Street 12345"); |
| 335 AddAddress("Jane", "Smith", "Main Street 12346"); | 340 AddAddress("Jane", "Smith", "Main Street 12346"); |
| 336 AddAddress("John", "Smith", "Side Street 47"); | 341 AddAddress("John", "Smith", "Side Street 47"); |
| 337 | 342 |
| 338 Profile* profile = browser()->profile(); | 343 Profile* profile = browser()->profile(); |
| 339 AutofillCounter counter(profile); | 344 browsing_data::AutofillCounter counter(GetWebDataService()); |
| 340 counter.Init(profile->GetPrefs(), base::Bind(&AutofillCounterTest::Callback, | 345 counter.Init(profile->GetPrefs(), base::Bind(&AutofillCounterTest::Callback, |
| 341 base::Unretained(this))); | 346 base::Unretained(this))); |
| 342 counter.Restart(); | 347 counter.Restart(); |
| 343 WaitForCounting(); | 348 WaitForCounting(); |
| 344 EXPECT_EQ(5, GetNumSuggestions()); | 349 EXPECT_EQ(5, GetNumSuggestions()); |
| 345 EXPECT_EQ(2, GetNumCreditCards()); | 350 EXPECT_EQ(2, GetNumCreditCards()); |
| 346 EXPECT_EQ(3, GetNumAddresses()); | 351 EXPECT_EQ(3, GetNumAddresses()); |
| 347 } | 352 } |
| 348 | 353 |
| 349 // Tests that the counting respects time ranges. | 354 // Tests that the counting respects time ranges. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 expected_num_credit_cards; | 389 expected_num_credit_cards; |
| 385 const browsing_data::BrowsingDataCounter::ResultInt expected_num_addresses; | 390 const browsing_data::BrowsingDataCounter::ResultInt expected_num_addresses; |
| 386 } test_cases[] = { | 391 } test_cases[] = { |
| 387 { base::Time(), 2, 3, 3}, | 392 { base::Time(), 2, 3, 3}, |
| 388 { time1, 2, 3, 3}, | 393 { time1, 2, 3, 3}, |
| 389 { time2, 1, 2, 2}, | 394 { time2, 1, 2, 2}, |
| 390 { time3, 1, 1, 0} | 395 { time3, 1, 1, 0} |
| 391 }; | 396 }; |
| 392 | 397 |
| 393 Profile* profile = browser()->profile(); | 398 Profile* profile = browser()->profile(); |
| 394 AutofillCounter counter(profile); | 399 browsing_data::AutofillCounter counter(GetWebDataService()); |
| 395 counter.Init(profile->GetPrefs(), base::Bind(&AutofillCounterTest::Callback, | 400 counter.Init(profile->GetPrefs(), base::Bind(&AutofillCounterTest::Callback, |
| 396 base::Unretained(this))); | 401 base::Unretained(this))); |
| 397 | 402 |
| 398 for (const TestCase& test_case : test_cases) { | 403 for (const TestCase& test_case : test_cases) { |
| 399 counter.SetPeriodStartForTesting(test_case.period_start); | 404 counter.SetPeriodStartForTesting(test_case.period_start); |
| 400 counter.Restart(); | 405 counter.Restart(); |
| 401 WaitForCounting(); | 406 WaitForCounting(); |
| 402 EXPECT_EQ(test_case.expected_num_suggestions, GetNumSuggestions()); | 407 EXPECT_EQ(test_case.expected_num_suggestions, GetNumSuggestions()); |
| 403 EXPECT_EQ(test_case.expected_num_credit_cards, GetNumCreditCards()); | 408 EXPECT_EQ(test_case.expected_num_credit_cards, GetNumCreditCards()); |
| 404 EXPECT_EQ(test_case.expected_num_addresses, GetNumAddresses()); | 409 EXPECT_EQ(test_case.expected_num_addresses, GetNumAddresses()); |
| 405 } | 410 } |
| 406 } | 411 } |
| 407 | 412 |
| 408 } // namespace | 413 } // namespace |
| OLD | NEW |