| 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 <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/test/histogram_tester.h" |
| 12 #include "components/autofill/core/browser/autofill_manager.h" | 13 #include "components/autofill/core/browser/autofill_manager.h" |
| 13 #include "components/autofill/core/browser/popup_item_ids.h" | 14 #include "components/autofill/core/browser/popup_item_ids.h" |
| 14 #include "components/autofill/core/browser/test_autofill_client.h" | 15 #include "components/autofill/core/browser/test_autofill_client.h" |
| 15 #include "components/autofill/core/browser/test_autofill_driver.h" | 16 #include "components/autofill/core/browser/test_autofill_driver.h" |
| 16 #include "components/autofill/core/browser/test_autofill_external_delegate.h" | 17 #include "components/autofill/core/browser/test_autofill_external_delegate.h" |
| 17 #include "components/autofill/core/common/autofill_switches.h" | 18 #include "components/autofill/core/common/autofill_switches.h" |
| 18 #include "components/autofill/core/common/form_data.h" | 19 #include "components/autofill/core/common/form_data.h" |
| 19 #include "components/autofill/core/common/form_field_data.h" | 20 #include "components/autofill/core/common/form_field_data.h" |
| 20 #include "components/autofill/core/common/password_form_fill_data.h" | 21 #include "components/autofill/core/common/password_form_fill_data.h" |
| 21 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 }; | 79 }; |
| 79 | 80 |
| 80 class MockAutofillManager : public AutofillManager { | 81 class MockAutofillManager : public AutofillManager { |
| 81 public: | 82 public: |
| 82 MockAutofillManager(AutofillDriver* driver, MockAutofillClient* client) | 83 MockAutofillManager(AutofillDriver* driver, MockAutofillClient* client) |
| 83 // Force to use the constructor designated for unit test, but we don't | 84 // Force to use the constructor designated for unit test, but we don't |
| 84 // really need personal_data in this test so we pass a NULL pointer. | 85 // really need personal_data in this test so we pass a NULL pointer. |
| 85 : AutofillManager(driver, client, NULL) {} | 86 : AutofillManager(driver, client, NULL) {} |
| 86 virtual ~MockAutofillManager() {} | 87 virtual ~MockAutofillManager() {} |
| 87 | 88 |
| 89 MOCK_METHOD2(ShouldShowScanCreditCard, |
| 90 bool(const FormData& form, const FormFieldData& field)); |
| 91 |
| 88 MOCK_METHOD5(FillOrPreviewForm, | 92 MOCK_METHOD5(FillOrPreviewForm, |
| 89 void(AutofillDriver::RendererFormDataAction action, | 93 void(AutofillDriver::RendererFormDataAction action, |
| 90 int query_id, | 94 int query_id, |
| 91 const FormData& form, | 95 const FormData& form, |
| 92 const FormFieldData& field, | 96 const FormFieldData& field, |
| 93 int unique_id)); | 97 int unique_id)); |
| 94 | 98 |
| 95 MOCK_METHOD4(FillCreditCardForm, | 99 MOCK_METHOD4(FillCreditCardForm, |
| 96 void(int query_id, | 100 void(int query_id, |
| 97 const FormData& form, | 101 const FormData& form, |
| 98 const FormFieldData& field, | 102 const FormFieldData& field, |
| 99 const CreditCard& credit_card)); | 103 const CreditCard& credit_card)); |
| 100 | 104 |
| 101 private: | 105 private: |
| 102 DISALLOW_COPY_AND_ASSIGN(MockAutofillManager); | 106 DISALLOW_COPY_AND_ASSIGN(MockAutofillManager); |
| 103 }; | 107 }; |
| 104 | 108 |
| 105 } // namespace | 109 } // namespace |
| 106 | 110 |
| 107 class AutofillExternalDelegateUnitTest : public testing::Test { | 111 class AutofillExternalDelegateUnitTest : public testing::Test { |
| 108 protected: | 112 protected: |
| 109 void SetUp() override { | 113 void SetUp() override { |
| 110 autofill_driver_.reset(new MockAutofillDriver()); | 114 autofill_driver_.reset(new testing::NiceMock<MockAutofillDriver>()); |
| 111 autofill_manager_.reset( | 115 autofill_manager_.reset( |
| 112 new MockAutofillManager(autofill_driver_.get(), &autofill_client_)); | 116 new MockAutofillManager(autofill_driver_.get(), &autofill_client_)); |
| 113 external_delegate_.reset( | 117 external_delegate_.reset( |
| 114 new AutofillExternalDelegate( | 118 new AutofillExternalDelegate( |
| 115 autofill_manager_.get(), autofill_driver_.get())); | 119 autofill_manager_.get(), autofill_driver_.get())); |
| 116 } | 120 } |
| 117 | 121 |
| 118 void TearDown() override { | 122 void TearDown() override { |
| 119 // Order of destruction is important as AutofillManager relies on | 123 // Order of destruction is important as AutofillManager relies on |
| 120 // PersonalDataManager to be around when it gets destroyed. | 124 // PersonalDataManager to be around when it gets destroyed. |
| 121 autofill_manager_.reset(); | 125 autofill_manager_.reset(); |
| 122 external_delegate_.reset(); | 126 external_delegate_.reset(); |
| 123 autofill_driver_.reset(); | 127 autofill_driver_.reset(); |
| 124 } | 128 } |
| 125 | 129 |
| 126 // Issue an OnQuery call with the given |query_id|. | 130 // Issue an OnQuery call with the given |query_id|. |
| 127 void IssueOnQuery(int query_id) { | 131 void IssueOnQuery(int query_id) { |
| 128 const FormData form; | 132 const FormData form; |
| 129 FormFieldData field; | 133 FormFieldData field; |
| 130 field.is_focusable = true; | 134 field.is_focusable = true; |
| 131 field.should_autocomplete = true; | 135 field.should_autocomplete = true; |
| 132 const gfx::RectF element_bounds; | 136 const gfx::RectF element_bounds; |
| 133 | 137 |
| 134 external_delegate_->OnQuery(query_id, form, field, element_bounds, true); | 138 external_delegate_->OnQuery(query_id, form, field, element_bounds, true); |
| 135 } | 139 } |
| 136 | 140 |
| 137 MockAutofillClient autofill_client_; | 141 void IssueOnSuggestionsReturned() { |
| 138 scoped_ptr<MockAutofillDriver> autofill_driver_; | 142 std::vector<base::string16> autofill_item; |
| 143 autofill_item.push_back(base::string16()); |
| 144 std::vector<int> autofill_ids; |
| 145 autofill_ids.push_back(kAutofillProfileId); |
| 146 external_delegate_->OnSuggestionsReturned( |
| 147 kQueryId, autofill_item, autofill_item, autofill_item, autofill_ids); |
| 148 } |
| 149 |
| 150 testing::NiceMock<MockAutofillClient> autofill_client_; |
| 151 scoped_ptr<testing::NiceMock<MockAutofillDriver>> autofill_driver_; |
| 139 scoped_ptr<MockAutofillManager> autofill_manager_; | 152 scoped_ptr<MockAutofillManager> autofill_manager_; |
| 140 scoped_ptr<AutofillExternalDelegate> external_delegate_; | 153 scoped_ptr<AutofillExternalDelegate> external_delegate_; |
| 141 | 154 |
| 142 base::MessageLoop message_loop_; | 155 base::MessageLoop message_loop_; |
| 143 }; | 156 }; |
| 144 | 157 |
| 145 // Test that our external delegate called the virtual methods at the right time. | 158 // Test that our external delegate called the virtual methods at the right time. |
| 146 TEST_F(AutofillExternalDelegateUnitTest, TestExternalDelegateVirtualCalls) { | 159 TEST_F(AutofillExternalDelegateUnitTest, TestExternalDelegateVirtualCalls) { |
| 147 IssueOnQuery(kQueryId); | 160 IssueOnQuery(kQueryId); |
| 148 | 161 |
| (...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 440 | 453 |
| 441 // Test that autofill client will scan a credit card after use accepted the | 454 // Test that autofill client will scan a credit card after use accepted the |
| 442 // suggestion to scan a credit card. | 455 // suggestion to scan a credit card. |
| 443 TEST_F(AutofillExternalDelegateUnitTest, ScanCreditCardMenuItem) { | 456 TEST_F(AutofillExternalDelegateUnitTest, ScanCreditCardMenuItem) { |
| 444 EXPECT_CALL(autofill_client_, ScanCreditCard(_)); | 457 EXPECT_CALL(autofill_client_, ScanCreditCard(_)); |
| 445 EXPECT_CALL(autofill_client_, HideAutofillPopup()); | 458 EXPECT_CALL(autofill_client_, HideAutofillPopup()); |
| 446 external_delegate_->DidAcceptSuggestion(base::string16(), | 459 external_delegate_->DidAcceptSuggestion(base::string16(), |
| 447 POPUP_ITEM_ID_SCAN_CREDIT_CARD); | 460 POPUP_ITEM_ID_SCAN_CREDIT_CARD); |
| 448 } | 461 } |
| 449 | 462 |
| 463 TEST_F(AutofillExternalDelegateUnitTest, ScanCreditCardPromptMetricsTest) { |
| 464 // Log that the scan card item was shown, although nothing was selected. |
| 465 { |
| 466 EXPECT_CALL(*autofill_manager_, ShouldShowScanCreditCard(_, _)) |
| 467 .WillOnce(testing::Return(true)); |
| 468 base::HistogramTester histogram; |
| 469 IssueOnQuery(kQueryId); |
| 470 IssueOnSuggestionsReturned(); |
| 471 external_delegate_->OnPopupHidden(); |
| 472 histogram.ExpectUniqueSample("Autofill.ScanCreditCardPrompt", |
| 473 AutofillMetrics::SCAN_CARD_ITEM_SHOWN, 1); |
| 474 } |
| 475 // Log that the scan card item was selected. |
| 476 { |
| 477 EXPECT_CALL(*autofill_manager_, ShouldShowScanCreditCard(_, _)) |
| 478 .WillOnce(testing::Return(true)); |
| 479 base::HistogramTester histogram; |
| 480 IssueOnQuery(kQueryId); |
| 481 IssueOnSuggestionsReturned(); |
| 482 external_delegate_->DidAcceptSuggestion(base::string16(), |
| 483 POPUP_ITEM_ID_SCAN_CREDIT_CARD); |
| 484 histogram.ExpectBucketCount("Autofill.ScanCreditCardPrompt", |
| 485 AutofillMetrics::SCAN_CARD_ITEM_SHOWN, 1); |
| 486 histogram.ExpectBucketCount("Autofill.ScanCreditCardPrompt", |
| 487 AutofillMetrics::SCAN_CARD_ITEM_SELECTED, 1); |
| 488 histogram.ExpectBucketCount("Autofill.ScanCreditCardPrompt", |
| 489 AutofillMetrics::SCAN_CARD_OTHER_ITEM_SELECTED, |
| 490 0); |
| 491 } |
| 492 // Log that something else was selected. |
| 493 { |
| 494 EXPECT_CALL(*autofill_manager_, ShouldShowScanCreditCard(_, _)) |
| 495 .WillOnce(testing::Return(true)); |
| 496 base::HistogramTester histogram; |
| 497 IssueOnQuery(kQueryId); |
| 498 IssueOnSuggestionsReturned(); |
| 499 external_delegate_->DidAcceptSuggestion(base::string16(), |
| 500 POPUP_ITEM_ID_CLEAR_FORM); |
| 501 histogram.ExpectBucketCount("Autofill.ScanCreditCardPrompt", |
| 502 AutofillMetrics::SCAN_CARD_ITEM_SHOWN, 1); |
| 503 histogram.ExpectBucketCount("Autofill.ScanCreditCardPrompt", |
| 504 AutofillMetrics::SCAN_CARD_ITEM_SELECTED, 0); |
| 505 histogram.ExpectBucketCount("Autofill.ScanCreditCardPrompt", |
| 506 AutofillMetrics::SCAN_CARD_OTHER_ITEM_SELECTED, |
| 507 1); |
| 508 } |
| 509 // Nothing is logged when the item isn't shown. |
| 510 { |
| 511 EXPECT_CALL(*autofill_manager_, ShouldShowScanCreditCard(_, _)) |
| 512 .WillOnce(testing::Return(false)); |
| 513 base::HistogramTester histogram; |
| 514 IssueOnQuery(kQueryId); |
| 515 IssueOnSuggestionsReturned(); |
| 516 external_delegate_->DidAcceptSuggestion(base::string16(), |
| 517 POPUP_ITEM_ID_CLEAR_FORM); |
| 518 histogram.ExpectTotalCount("Autofill.ScanCreditCardPrompt", 0); |
| 519 } |
| 520 } |
| 521 |
| 450 // Test that autofill manager will fill the credit card form after user scans a | 522 // Test that autofill manager will fill the credit card form after user scans a |
| 451 // credit card. | 523 // credit card. |
| 452 TEST_F(AutofillExternalDelegateUnitTest, FillCreditCardForm) { | 524 TEST_F(AutofillExternalDelegateUnitTest, FillCreditCardForm) { |
| 453 base::string16 card_number = base::ASCIIToUTF16("test"); | 525 base::string16 card_number = base::ASCIIToUTF16("test"); |
| 454 int expiration_month = 1; | 526 int expiration_month = 1; |
| 455 int expiration_year = 3000; | 527 int expiration_year = 3000; |
| 456 EXPECT_CALL( | 528 EXPECT_CALL( |
| 457 *autofill_manager_, | 529 *autofill_manager_, |
| 458 FillCreditCardForm( | 530 FillCreditCardForm( |
| 459 _, _, _, CreditCard(card_number, expiration_month, expiration_year))); | 531 _, _, _, CreditCard(card_number, expiration_month, expiration_year))); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 518 TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateFillFieldWithValue) { | 590 TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateFillFieldWithValue) { |
| 519 EXPECT_CALL(autofill_client_, HideAutofillPopup()); | 591 EXPECT_CALL(autofill_client_, HideAutofillPopup()); |
| 520 base::string16 dummy_string(ASCIIToUTF16("baz foo")); | 592 base::string16 dummy_string(ASCIIToUTF16("baz foo")); |
| 521 EXPECT_CALL(*autofill_driver_, | 593 EXPECT_CALL(*autofill_driver_, |
| 522 RendererShouldFillFieldWithValue(dummy_string)); | 594 RendererShouldFillFieldWithValue(dummy_string)); |
| 523 external_delegate_->DidAcceptSuggestion(dummy_string, | 595 external_delegate_->DidAcceptSuggestion(dummy_string, |
| 524 POPUP_ITEM_ID_AUTOCOMPLETE_ENTRY); | 596 POPUP_ITEM_ID_AUTOCOMPLETE_ENTRY); |
| 525 } | 597 } |
| 526 | 598 |
| 527 } // namespace autofill | 599 } // namespace autofill |
| OLD | NEW |