| 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 "components/browsing_data/core/counters/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" |
| 16 #include "chrome/browser/web_data_service_factory.h" | 16 #include "chrome/browser/web_data_service_factory.h" |
| 17 #include "chrome/test/base/in_process_browser_test.h" | 17 #include "chrome/test/base/in_process_browser_test.h" |
| 18 #include "components/autofill/core/browser/autofill_profile.h" | 18 #include "components/autofill/core/browser/autofill_profile.h" |
| 19 #include "components/autofill/core/browser/autofill_test_utils.h" |
| 19 #include "components/autofill/core/browser/autofill_type.h" | 20 #include "components/autofill/core/browser/autofill_type.h" |
| 20 #include "components/autofill/core/browser/credit_card.h" | 21 #include "components/autofill/core/browser/credit_card.h" |
| 21 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" | 22 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" |
| 22 #include "components/browsing_data/core/browsing_data_utils.h" | 23 #include "components/browsing_data/core/browsing_data_utils.h" |
| 23 #include "components/browsing_data/core/pref_names.h" | 24 #include "components/browsing_data/core/pref_names.h" |
| 24 #include "components/prefs/pref_service.h" | 25 #include "components/prefs/pref_service.h" |
| 25 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
| 26 | 27 |
| 27 namespace { | 28 namespace { |
| 28 | 29 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 59 base::ASCIIToUTF16(value)); | 60 base::ASCIIToUTF16(value)); |
| 60 } | 61 } |
| 61 | 62 |
| 62 void ClearAutocompleteSuggestions() { | 63 void ClearAutocompleteSuggestions() { |
| 63 web_data_service_->RemoveFormElementsAddedBetween( | 64 web_data_service_->RemoveFormElementsAddedBetween( |
| 64 base::Time(), base::Time::Max()); | 65 base::Time(), base::Time::Max()); |
| 65 } | 66 } |
| 66 | 67 |
| 67 // Credit cards -------------------------------------------------------------- | 68 // Credit cards -------------------------------------------------------------- |
| 68 | 69 |
| 69 void AddCreditCard(const std::string& card_number, | 70 void AddCreditCard(const char* card_number, |
| 70 int exp_month, | 71 const char* exp_month, |
| 71 int exp_year) { | 72 const char* exp_year) { |
| 72 autofill::CreditCard card( | 73 autofill::CreditCard card; |
| 73 base::ASCIIToUTF16(card_number), exp_month, exp_year); | 74 autofill::test::SetCreditCardInfo(&card, nullptr, card_number, exp_month, |
| 74 std::string id = base::GenerateGUID(); | 75 exp_year); |
| 75 credit_card_ids_.push_back(id); | 76 credit_card_ids_.push_back(card.guid()); |
| 76 card.set_guid(id); | |
| 77 web_data_service_->AddCreditCard(card); | 77 web_data_service_->AddCreditCard(card); |
| 78 } | 78 } |
| 79 | 79 |
| 80 void RemoveLastCreditCard() { | 80 void RemoveLastCreditCard() { |
| 81 web_data_service_->RemoveCreditCard(credit_card_ids_.back()); | 81 web_data_service_->RemoveCreditCard(credit_card_ids_.back()); |
| 82 credit_card_ids_.pop_back(); | 82 credit_card_ids_.pop_back(); |
| 83 } | 83 } |
| 84 | 84 |
| 85 // Addresses ----------------------------------------------------------------- | 85 // Addresses ----------------------------------------------------------------- |
| 86 | 86 |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 // Tests that we count the correct number of credit cards. | 255 // Tests that we count the correct number of credit cards. |
| 256 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, CreditCards) { | 256 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, CreditCards) { |
| 257 Profile* profile = browser()->profile(); | 257 Profile* profile = browser()->profile(); |
| 258 browsing_data::AutofillCounter counter(GetWebDataService()); | 258 browsing_data::AutofillCounter counter(GetWebDataService()); |
| 259 counter.Init(profile->GetPrefs(), base::Bind(&AutofillCounterTest::Callback, | 259 counter.Init(profile->GetPrefs(), base::Bind(&AutofillCounterTest::Callback, |
| 260 base::Unretained(this))); | 260 base::Unretained(this))); |
| 261 counter.Restart(); | 261 counter.Restart(); |
| 262 WaitForCounting(); | 262 WaitForCounting(); |
| 263 EXPECT_EQ(0, GetNumCreditCards()); | 263 EXPECT_EQ(0, GetNumCreditCards()); |
| 264 | 264 |
| 265 AddCreditCard("0000-0000-0000-0000", 1, 2015); | 265 AddCreditCard("0000-0000-0000-0000", "1", "2015"); |
| 266 counter.Restart(); | 266 counter.Restart(); |
| 267 WaitForCounting(); | 267 WaitForCounting(); |
| 268 EXPECT_EQ(1, GetNumCreditCards()); | 268 EXPECT_EQ(1, GetNumCreditCards()); |
| 269 | 269 |
| 270 AddCreditCard("0123-4567-8910-1112", 10, 2015); | 270 AddCreditCard("0123-4567-8910-1112", "10", "2015"); |
| 271 counter.Restart(); | 271 counter.Restart(); |
| 272 WaitForCounting(); | 272 WaitForCounting(); |
| 273 EXPECT_EQ(2, GetNumCreditCards()); | 273 EXPECT_EQ(2, GetNumCreditCards()); |
| 274 | 274 |
| 275 AddCreditCard("1211-1098-7654-3210", 10, 2030); | 275 AddCreditCard("1211-1098-7654-3210", "10", "2030"); |
| 276 counter.Restart(); | 276 counter.Restart(); |
| 277 WaitForCounting(); | 277 WaitForCounting(); |
| 278 EXPECT_EQ(3, GetNumCreditCards()); | 278 EXPECT_EQ(3, GetNumCreditCards()); |
| 279 | 279 |
| 280 RemoveLastCreditCard(); | 280 RemoveLastCreditCard(); |
| 281 counter.Restart(); | 281 counter.Restart(); |
| 282 WaitForCounting(); | 282 WaitForCounting(); |
| 283 EXPECT_EQ(2, GetNumCreditCards()); | 283 EXPECT_EQ(2, GetNumCreditCards()); |
| 284 | 284 |
| 285 ClearCreditCardsAndAddresses(); | 285 ClearCreditCardsAndAddresses(); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 | 326 |
| 327 // Tests that we return the correct complex result when counting more than | 327 // Tests that we return the correct complex result when counting more than |
| 328 // one type of items. | 328 // one type of items. |
| 329 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, ComplexResult) { | 329 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, ComplexResult) { |
| 330 AddAutocompleteSuggestion("email", "example@example.com"); | 330 AddAutocompleteSuggestion("email", "example@example.com"); |
| 331 AddAutocompleteSuggestion("zip", "12345"); | 331 AddAutocompleteSuggestion("zip", "12345"); |
| 332 AddAutocompleteSuggestion("tel", "+123456789"); | 332 AddAutocompleteSuggestion("tel", "+123456789"); |
| 333 AddAutocompleteSuggestion("tel", "+987654321"); | 333 AddAutocompleteSuggestion("tel", "+987654321"); |
| 334 AddAutocompleteSuggestion("city", "Munich"); | 334 AddAutocompleteSuggestion("city", "Munich"); |
| 335 | 335 |
| 336 AddCreditCard("0000-0000-0000-0000", 1, 2015); | 336 AddCreditCard("0000-0000-0000-0000", "1", "2015"); |
| 337 AddCreditCard("1211-1098-7654-3210", 10, 2030); | 337 AddCreditCard("1211-1098-7654-3210", "10", "2030"); |
| 338 | 338 |
| 339 AddAddress("John", "Doe", "Main Street 12345"); | 339 AddAddress("John", "Doe", "Main Street 12345"); |
| 340 AddAddress("Jane", "Smith", "Main Street 12346"); | 340 AddAddress("Jane", "Smith", "Main Street 12346"); |
| 341 AddAddress("John", "Smith", "Side Street 47"); | 341 AddAddress("John", "Smith", "Side Street 47"); |
| 342 | 342 |
| 343 Profile* profile = browser()->profile(); | 343 Profile* profile = browser()->profile(); |
| 344 browsing_data::AutofillCounter counter(GetWebDataService()); | 344 browsing_data::AutofillCounter counter(GetWebDataService()); |
| 345 counter.Init(profile->GetPrefs(), base::Bind(&AutofillCounterTest::Callback, | 345 counter.Init(profile->GetPrefs(), base::Bind(&AutofillCounterTest::Callback, |
| 346 base::Unretained(this))); | 346 base::Unretained(this))); |
| 347 counter.Restart(); | 347 counter.Restart(); |
| 348 WaitForCounting(); | 348 WaitForCounting(); |
| 349 EXPECT_EQ(5, GetNumSuggestions()); | 349 EXPECT_EQ(5, GetNumSuggestions()); |
| 350 EXPECT_EQ(2, GetNumCreditCards()); | 350 EXPECT_EQ(2, GetNumCreditCards()); |
| 351 EXPECT_EQ(3, GetNumAddresses()); | 351 EXPECT_EQ(3, GetNumAddresses()); |
| 352 } | 352 } |
| 353 | 353 |
| 354 // Tests that the counting respects time ranges. | 354 // Tests that the counting respects time ranges. |
| 355 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, TimeRanges) { | 355 IN_PROC_BROWSER_TEST_F(AutofillCounterTest, TimeRanges) { |
| 356 // This test makes time comparisons that are precise to a microsecond, but the | 356 // This test makes time comparisons that are precise to a microsecond, but the |
| 357 // database uses the time_t format which is only precise to a second. | 357 // database uses the time_t format which is only precise to a second. |
| 358 // Make sure we use timestamps rounded to a second. | 358 // Make sure we use timestamps rounded to a second. |
| 359 base::Time time1 = base::Time::FromTimeT(base::Time::Now().ToTimeT()); | 359 base::Time time1 = base::Time::FromTimeT(base::Time::Now().ToTimeT()); |
| 360 | 360 |
| 361 AddAutocompleteSuggestion("email", "example@example.com"); | 361 AddAutocompleteSuggestion("email", "example@example.com"); |
| 362 AddCreditCard("0000-0000-0000-0000", 1, 2015); | 362 AddCreditCard("0000-0000-0000-0000", "1", "2015"); |
| 363 AddAddress("John", "Doe", "Main Street 12345"); | 363 AddAddress("John", "Doe", "Main Street 12345"); |
| 364 WaitForDBThread(); | 364 WaitForDBThread(); |
| 365 | 365 |
| 366 // Skip at least a second has passed and add another batch. | 366 // Skip at least a second has passed and add another batch. |
| 367 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1)); | 367 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1)); |
| 368 base::Time time2 = base::Time::FromTimeT(base::Time::Now().ToTimeT()); | 368 base::Time time2 = base::Time::FromTimeT(base::Time::Now().ToTimeT()); |
| 369 | 369 |
| 370 AddCreditCard("0123-4567-8910-1112", 10, 2015); | 370 AddCreditCard("0123-4567-8910-1112", "10", "2015"); |
| 371 AddAddress("Jane", "Smith", "Main Street 12346"); | 371 AddAddress("Jane", "Smith", "Main Street 12346"); |
| 372 AddAddress("John", "Smith", "Side Street 47"); | 372 AddAddress("John", "Smith", "Side Street 47"); |
| 373 WaitForDBThread(); | 373 WaitForDBThread(); |
| 374 | 374 |
| 375 // Skip at least a second has passed and add another batch. | 375 // Skip at least a second has passed and add another batch. |
| 376 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1)); | 376 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1)); |
| 377 base::Time time3 = base::Time::FromTimeT(base::Time::Now().ToTimeT()); | 377 base::Time time3 = base::Time::FromTimeT(base::Time::Now().ToTimeT()); |
| 378 | 378 |
| 379 AddAutocompleteSuggestion("tel", "+987654321"); | 379 AddAutocompleteSuggestion("tel", "+987654321"); |
| 380 AddCreditCard("1211-1098-7654-3210", 10, 2030); | 380 AddCreditCard("1211-1098-7654-3210", "10", "2030"); |
| 381 WaitForDBThread(); | 381 WaitForDBThread(); |
| 382 | 382 |
| 383 // Test the results for different starting points. | 383 // Test the results for different starting points. |
| 384 struct TestCase { | 384 struct TestCase { |
| 385 const base::Time period_start; | 385 const base::Time period_start; |
| 386 const browsing_data::BrowsingDataCounter::ResultInt | 386 const browsing_data::BrowsingDataCounter::ResultInt |
| 387 expected_num_suggestions; | 387 expected_num_suggestions; |
| 388 const browsing_data::BrowsingDataCounter::ResultInt | 388 const browsing_data::BrowsingDataCounter::ResultInt |
| 389 expected_num_credit_cards; | 389 expected_num_credit_cards; |
| 390 const browsing_data::BrowsingDataCounter::ResultInt expected_num_addresses; | 390 const browsing_data::BrowsingDataCounter::ResultInt expected_num_addresses; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 404 counter.SetPeriodStartForTesting(test_case.period_start); | 404 counter.SetPeriodStartForTesting(test_case.period_start); |
| 405 counter.Restart(); | 405 counter.Restart(); |
| 406 WaitForCounting(); | 406 WaitForCounting(); |
| 407 EXPECT_EQ(test_case.expected_num_suggestions, GetNumSuggestions()); | 407 EXPECT_EQ(test_case.expected_num_suggestions, GetNumSuggestions()); |
| 408 EXPECT_EQ(test_case.expected_num_credit_cards, GetNumCreditCards()); | 408 EXPECT_EQ(test_case.expected_num_credit_cards, GetNumCreditCards()); |
| 409 EXPECT_EQ(test_case.expected_num_addresses, GetNumAddresses()); | 409 EXPECT_EQ(test_case.expected_num_addresses, GetNumAddresses()); |
| 410 } | 410 } |
| 411 } | 411 } |
| 412 | 412 |
| 413 } // namespace | 413 } // namespace |
| OLD | NEW |