OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <algorithm> | 5 #include <algorithm> |
6 #include <string> | 6 #include <string> |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/command_line.h" |
10 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
11 #include "base/guid.h" | 12 #include "base/guid.h" |
12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
13 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
14 #include "base/prefs/pref_service.h" | 15 #include "base/prefs/pref_service.h" |
15 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
16 #include "base/synchronization/waitable_event.h" | 17 #include "base/synchronization/waitable_event.h" |
17 #include "components/autofill/core/browser/autofill_profile.h" | 18 #include "components/autofill/core/browser/autofill_profile.h" |
18 #include "components/autofill/core/browser/autofill_test_utils.h" | 19 #include "components/autofill/core/browser/autofill_test_utils.h" |
19 #include "components/autofill/core/browser/form_structure.h" | 20 #include "components/autofill/core/browser/form_structure.h" |
20 #include "components/autofill/core/browser/personal_data_manager.h" | 21 #include "components/autofill/core/browser/personal_data_manager.h" |
21 #include "components/autofill/core/browser/personal_data_manager_observer.h" | 22 #include "components/autofill/core/browser/personal_data_manager_observer.h" |
22 #include "components/autofill/core/browser/webdata/autofill_table.h" | 23 #include "components/autofill/core/browser/webdata/autofill_table.h" |
23 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" | 24 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" |
24 #include "components/autofill/core/common/autofill_pref_names.h" | 25 #include "components/autofill/core/common/autofill_pref_names.h" |
| 26 #include "components/autofill/core/common/autofill_switches.h" |
25 #include "components/autofill/core/common/form_data.h" | 27 #include "components/autofill/core/common/form_data.h" |
26 #include "components/webdata/common/web_data_service_base.h" | 28 #include "components/webdata/common/web_data_service_base.h" |
27 #include "components/webdata/common/web_database_service.h" | 29 #include "components/webdata/common/web_database_service.h" |
28 #include "testing/gmock/include/gmock/gmock.h" | 30 #include "testing/gmock/include/gmock/gmock.h" |
29 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
30 | 32 |
31 using base::ASCIIToUTF16; | 33 using base::ASCIIToUTF16; |
32 | 34 |
33 namespace autofill { | 35 namespace autofill { |
34 namespace { | 36 namespace { |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 prefs_.get(), | 115 prefs_.get(), |
114 is_incognito); | 116 is_incognito); |
115 personal_data_->AddObserver(&personal_data_observer_); | 117 personal_data_->AddObserver(&personal_data_observer_); |
116 | 118 |
117 // Verify that the web database has been updated and the notification sent. | 119 // Verify that the web database has been updated and the notification sent. |
118 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) | 120 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) |
119 .WillOnce(QuitMainMessageLoop()); | 121 .WillOnce(QuitMainMessageLoop()); |
120 base::MessageLoop::current()->Run(); | 122 base::MessageLoop::current()->Run(); |
121 } | 123 } |
122 | 124 |
| 125 void EnableWalletCardImport() { |
| 126 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 127 switches::kEnableWalletCardImport); |
| 128 ScopedVector<CreditCard> unused; |
| 129 autofill_table_->GetServerCreditCards(&unused.get()); |
| 130 autofill_table_->SetServerCreditCards(std::vector<CreditCard>()); |
| 131 } |
| 132 |
123 // The temporary directory should be deleted at the end to ensure that | 133 // The temporary directory should be deleted at the end to ensure that |
124 // files are not used anymore and deletion succeeds. | 134 // files are not used anymore and deletion succeeds. |
125 base::ScopedTempDir temp_dir_; | 135 base::ScopedTempDir temp_dir_; |
126 base::MessageLoopForUI message_loop_; | 136 base::MessageLoopForUI message_loop_; |
127 scoped_ptr<PrefService> prefs_; | 137 scoped_ptr<PrefService> prefs_; |
128 scoped_refptr<AutofillWebDataService> autofill_database_service_; | 138 scoped_refptr<AutofillWebDataService> autofill_database_service_; |
129 scoped_refptr<WebDatabaseService> web_database_; | 139 scoped_refptr<WebDatabaseService> web_database_; |
130 AutofillTable* autofill_table_; // weak ref | 140 AutofillTable* autofill_table_; // weak ref |
131 PersonalDataLoadedObserverMock personal_data_observer_; | 141 PersonalDataLoadedObserverMock personal_data_observer_; |
132 scoped_ptr<PersonalDataManager> personal_data_; | 142 scoped_ptr<PersonalDataManager> personal_data_; |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 personal_data_->GetProfiles(); | 373 personal_data_->GetProfiles(); |
364 const std::vector<CreditCard*>& cards3 = personal_data_->GetCreditCards(); | 374 const std::vector<CreditCard*>& cards3 = personal_data_->GetCreditCards(); |
365 ASSERT_EQ(1U, profiles3.size()); | 375 ASSERT_EQ(1U, profiles3.size()); |
366 ASSERT_EQ(1U, cards3.size()); | 376 ASSERT_EQ(1U, cards3.size()); |
367 EXPECT_EQ(0, profile.Compare(*profiles3[0])); | 377 EXPECT_EQ(0, profile.Compare(*profiles3[0])); |
368 EXPECT_EQ(0, credit_card.Compare(*cards3[0])); | 378 EXPECT_EQ(0, credit_card.Compare(*cards3[0])); |
369 EXPECT_EQ(profile.origin(), profiles3[0]->origin()); | 379 EXPECT_EQ(profile.origin(), profiles3[0]->origin()); |
370 EXPECT_EQ(credit_card.origin(), cards3[0]->origin()); | 380 EXPECT_EQ(credit_card.origin(), cards3[0]->origin()); |
371 } | 381 } |
372 | 382 |
| 383 // Tests that server cards are ignored without the flag. |
| 384 TEST_F(PersonalDataManagerTest, ReturnsServerCreditCards) { |
| 385 std::vector<CreditCard> server_cards; |
| 386 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123")); |
| 387 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger", |
| 388 "9012" /* Visa */, "01", "2010"); |
| 389 server_cards.back().SetTypeForMaskedCard(kVisaCard); |
| 390 |
| 391 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "b456")); |
| 392 test::SetCreditCardInfo(&server_cards.back(), "Bonnie Parker", |
| 393 "2109" /* Mastercard */, "12", "2012"); |
| 394 server_cards.back().SetTypeForMaskedCard(kMasterCard); |
| 395 |
| 396 autofill_table_->SetServerCreditCards(server_cards); |
| 397 personal_data_->Refresh(); |
| 398 |
| 399 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) |
| 400 .WillOnce(QuitMainMessageLoop()); |
| 401 base::MessageLoop::current()->Run(); |
| 402 |
| 403 EXPECT_EQ(0U, personal_data_->GetCreditCards().size()); |
| 404 } |
| 405 |
373 // Tests that UpdateCreditCard can be used to mask or unmask server cards. | 406 // Tests that UpdateCreditCard can be used to mask or unmask server cards. |
374 TEST_F(PersonalDataManagerTest, UpdateServerCreditCards) { | 407 TEST_F(PersonalDataManagerTest, UpdateServerCreditCards) { |
| 408 EnableWalletCardImport(); |
| 409 |
375 std::vector<CreditCard> server_cards; | 410 std::vector<CreditCard> server_cards; |
376 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123")); | 411 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123")); |
377 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger", | 412 test::SetCreditCardInfo(&server_cards.back(), "John Dillinger", |
378 "9012" /* Visa */, "01", "2010"); | 413 "9012" /* Visa */, "01", "2010"); |
379 server_cards.back().SetTypeForMaskedCard(kVisaCard); | 414 server_cards.back().SetTypeForMaskedCard(kVisaCard); |
380 | 415 |
381 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "b456")); | 416 server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "b456")); |
382 test::SetCreditCardInfo(&server_cards.back(), "Bonnie Parker", | 417 test::SetCreditCardInfo(&server_cards.back(), "Bonnie Parker", |
383 "2109" /* Mastercard */, "12", "2012"); | 418 "2109" /* Mastercard */, "12", "2012"); |
384 server_cards.back().SetTypeForMaskedCard(kMasterCard); | 419 server_cards.back().SetTypeForMaskedCard(kMasterCard); |
385 | 420 |
386 server_cards.push_back(CreditCard(CreditCard::FULL_SERVER_CARD, "c789")); | 421 server_cards.push_back(CreditCard(CreditCard::FULL_SERVER_CARD, "c789")); |
387 test::SetCreditCardInfo(&server_cards.back(), "Clyde Barrow", | 422 test::SetCreditCardInfo(&server_cards.back(), "Clyde Barrow", |
388 "347666888555" /* American Express */, "04", "2015"); | 423 "347666888555" /* American Express */, "04", "2015"); |
389 | 424 |
390 autofill_table_->SetServerCreditCards(server_cards); | 425 autofill_table_->SetServerCreditCards(server_cards); |
391 personal_data_->Refresh(); | 426 personal_data_->Refresh(); |
392 | 427 |
393 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) | 428 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) |
394 .WillOnce(QuitMainMessageLoop()); | 429 .WillOnce(QuitMainMessageLoop()); |
395 base::MessageLoop::current()->Run(); | 430 base::MessageLoop::current()->Run(); |
396 | 431 |
| 432 ASSERT_EQ(3U, personal_data_->GetCreditCards().size()); |
397 // The GUIDs will be different, so just compare the data. | 433 // The GUIDs will be different, so just compare the data. |
398 for (size_t i = 0; i < 3; ++i) | 434 for (size_t i = 0; i < 3; ++i) |
399 EXPECT_EQ(0, server_cards[i].Compare(*personal_data_->GetCreditCards()[i])); | 435 EXPECT_EQ(0, server_cards[i].Compare(*personal_data_->GetCreditCards()[i])); |
400 | 436 |
401 CreditCard* unmasked_card = &server_cards.front(); | 437 CreditCard* unmasked_card = &server_cards.front(); |
402 unmasked_card->set_record_type(CreditCard::FULL_SERVER_CARD); | 438 unmasked_card->set_record_type(CreditCard::FULL_SERVER_CARD); |
403 unmasked_card->SetNumber(ASCIIToUTF16("423456789012")); | 439 unmasked_card->SetNumber(ASCIIToUTF16("423456789012")); |
404 EXPECT_NE(0, server_cards.front().Compare( | 440 EXPECT_NE(0, server_cards.front().Compare( |
405 *personal_data_->GetCreditCards().front())); | 441 *personal_data_->GetCreditCards().front())); |
406 personal_data_->UpdateServerCreditCard(*unmasked_card); | 442 personal_data_->UpdateServerCreditCard(*unmasked_card); |
(...skipping 2261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2668 AutofillType(ADDRESS_HOME_STREET_ADDRESS), | 2704 AutofillType(ADDRESS_HOME_STREET_ADDRESS), |
2669 base::UTF8ToUTF16("123"), | 2705 base::UTF8ToUTF16("123"), |
2670 false, | 2706 false, |
2671 std::vector<ServerFieldType>()); | 2707 std::vector<ServerFieldType>()); |
2672 ASSERT_FALSE(suggestions.empty()); | 2708 ASSERT_FALSE(suggestions.empty()); |
2673 EXPECT_EQ(suggestions[0].value, | 2709 EXPECT_EQ(suggestions[0].value, |
2674 base::UTF8ToUTF16("123 Zoo St., Second Line, Third line, unit 5")); | 2710 base::UTF8ToUTF16("123 Zoo St., Second Line, Third line, unit 5")); |
2675 } | 2711 } |
2676 | 2712 |
2677 TEST_F(PersonalDataManagerTest, GetCreditCardSuggestions) { | 2713 TEST_F(PersonalDataManagerTest, GetCreditCardSuggestions) { |
| 2714 EnableWalletCardImport(); |
| 2715 |
2678 // These GUIDs are reverse alphabetical to make validating expectations | 2716 // These GUIDs are reverse alphabetical to make validating expectations |
2679 // easier. | 2717 // easier. |
2680 CreditCard credit_card0("287151C8-6AB1-487C-9095-28E80BE5DA15", | 2718 CreditCard credit_card0("287151C8-6AB1-487C-9095-28E80BE5DA15", |
2681 "https://www.example.com"); | 2719 "https://www.example.com"); |
2682 test::SetCreditCardInfo(&credit_card0, | 2720 test::SetCreditCardInfo(&credit_card0, |
2683 "Clyde Barrow", "347666888555" /* American Express */, "04", "2015"); | 2721 "Clyde Barrow", "347666888555" /* American Express */, "04", "2015"); |
2684 personal_data_->AddCreditCard(credit_card0); | 2722 personal_data_->AddCreditCard(credit_card0); |
2685 | 2723 |
2686 CreditCard credit_card1("1141084B-72D7-4B73-90CF-3D6AC154673B", | 2724 CreditCard credit_card1("1141084B-72D7-4B73-90CF-3D6AC154673B", |
2687 "https://www.example.com"); | 2725 "https://www.example.com"); |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2826 | 2864 |
2827 prefs_->SetInteger(prefs::kAutofillMacAddressBookShowedCount, 4); | 2865 prefs_->SetInteger(prefs::kAutofillMacAddressBookShowedCount, 4); |
2828 EXPECT_TRUE(personal_data_->ShouldShowAccessAddressBookSuggestion(type)); | 2866 EXPECT_TRUE(personal_data_->ShouldShowAccessAddressBookSuggestion(type)); |
2829 | 2867 |
2830 prefs_->SetInteger(prefs::kAutofillMacAddressBookShowedCount, 6); | 2868 prefs_->SetInteger(prefs::kAutofillMacAddressBookShowedCount, 6); |
2831 EXPECT_FALSE(personal_data_->ShouldShowAccessAddressBookSuggestion(type)); | 2869 EXPECT_FALSE(personal_data_->ShouldShowAccessAddressBookSuggestion(type)); |
2832 } | 2870 } |
2833 #endif // defined(OS_MACOSX) && !defined(OS_IOS) | 2871 #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
2834 | 2872 |
2835 } // namespace autofill | 2873 } // namespace autofill |
OLD | NEW |