OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/autofill/core/browser/autofill_assistant.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/callback.h" |
| 10 #include "base/feature_list.h" |
| 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "components/autofill/core/browser/autofill_driver.h" |
| 14 #include "components/autofill/core/browser/autofill_experiments.h" |
| 15 #include "components/autofill/core/browser/autofill_manager.h" |
| 16 #include "components/autofill/core/browser/autofill_test_utils.h" |
| 17 #include "components/autofill/core/browser/credit_card.h" |
| 18 #include "components/autofill/core/browser/form_structure.h" |
| 19 #include "components/autofill/core/browser/test_autofill_client.h" |
| 20 #include "components/autofill/core/browser/test_autofill_driver.h" |
| 21 #include "components/autofill/core/common/autofill_constants.h" |
| 22 #include "testing/gmock/include/gmock/gmock.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 |
| 25 using testing::_; |
| 26 |
| 27 namespace autofill { |
| 28 namespace { |
| 29 |
| 30 class MockAutofillManager : public AutofillManager { |
| 31 public: |
| 32 MockAutofillManager(TestAutofillDriver* driver, TestAutofillClient* client) |
| 33 // Force to use the constructor designated for unit test, but we don't |
| 34 // really need personal_data in this test so we pass a NULL pointer. |
| 35 : AutofillManager(driver, client, NULL) {} |
| 36 virtual ~MockAutofillManager() {} |
| 37 |
| 38 MOCK_METHOD5(FillCreditCardForm, |
| 39 void(int query_id, |
| 40 const FormData& form, |
| 41 const FormFieldData& field, |
| 42 const CreditCard& credit_card, |
| 43 const base::string16& cvc)); |
| 44 |
| 45 private: |
| 46 DISALLOW_COPY_AND_ASSIGN(MockAutofillManager); |
| 47 }; |
| 48 |
| 49 } // namespace |
| 50 |
| 51 class AutofillAssistantTest : public testing::Test { |
| 52 protected: |
| 53 AutofillAssistantTest() |
| 54 : message_loop_(), |
| 55 autofill_client_(), |
| 56 autofill_driver_(), |
| 57 autofill_manager_(&autofill_driver_, &autofill_client_), |
| 58 autofill_assistant_(&autofill_manager_) {} |
| 59 |
| 60 void EnableAutofillCreditCardAssist() { |
| 61 base::FeatureList::ClearInstanceForTesting(); |
| 62 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); |
| 63 feature_list->InitializeFromCommandLine(kAutofillCreditCardAssist.name, |
| 64 std::string()); |
| 65 base::FeatureList::SetInstance(std::move(feature_list)); |
| 66 } |
| 67 |
| 68 // Returns an initialized FormStructure with credit card form data. To be |
| 69 // owned by the caller. |
| 70 std::unique_ptr<FormStructure> CreateValidCreditCardForm() { |
| 71 std::unique_ptr<FormStructure> form_structure; |
| 72 FormData form; |
| 73 |
| 74 FormFieldData field; |
| 75 field.form_control_type = "text"; |
| 76 |
| 77 field.label = base::ASCIIToUTF16("Name on Card"); |
| 78 field.name = base::ASCIIToUTF16("name_on_card"); |
| 79 form.fields.push_back(field); |
| 80 |
| 81 field.label = base::ASCIIToUTF16("Card Number"); |
| 82 field.name = base::ASCIIToUTF16("card_number"); |
| 83 form.fields.push_back(field); |
| 84 |
| 85 field.label = base::ASCIIToUTF16("Exp Month"); |
| 86 field.name = base::ASCIIToUTF16("ccmonth"); |
| 87 form.fields.push_back(field); |
| 88 |
| 89 field.label = base::ASCIIToUTF16("Exp Year"); |
| 90 field.name = base::ASCIIToUTF16("ccyear"); |
| 91 form.fields.push_back(field); |
| 92 |
| 93 field.label = base::ASCIIToUTF16("Verification"); |
| 94 field.name = base::ASCIIToUTF16("verification"); |
| 95 form.fields.push_back(field); |
| 96 |
| 97 form_structure.reset(new FormStructure(form)); |
| 98 form_structure->DetermineHeuristicTypes(); |
| 99 |
| 100 return form_structure; |
| 101 } |
| 102 |
| 103 base::MessageLoop message_loop_; |
| 104 TestAutofillClient autofill_client_; |
| 105 testing::NiceMock<TestAutofillDriver> autofill_driver_; |
| 106 MockAutofillManager autofill_manager_; |
| 107 AutofillAssistant autofill_assistant_; |
| 108 }; |
| 109 |
| 110 MATCHER_P(CreditCardMatches, guid, "") { |
| 111 return arg.guid() == guid; |
| 112 } |
| 113 |
| 114 // If the feature is turned off, CanShowCreditCardAssist() always returns |
| 115 // false. |
| 116 TEST_F(AutofillAssistantTest, CanShowCreditCardAssist_FeatureOff) { |
| 117 std::unique_ptr<FormStructure> form_structure = CreateValidCreditCardForm(); |
| 118 |
| 119 std::vector<FormStructure*> form_structures{form_structure.get()}; |
| 120 EXPECT_FALSE(autofill_assistant_.CanShowCreditCardAssist(form_structures)); |
| 121 } |
| 122 |
| 123 // Tests that with the feature enabled and proper input, |
| 124 // CanShowCreditCardAssist() behaves as expected. |
| 125 TEST_F(AutofillAssistantTest, CanShowCreditCardAssist_FeatureOn) { |
| 126 EnableAutofillCreditCardAssist(); |
| 127 std::unique_ptr<FormStructure> form_structure = CreateValidCreditCardForm(); |
| 128 |
| 129 std::vector<FormStructure*> form_structures; |
| 130 EXPECT_FALSE(autofill_assistant_.CanShowCreditCardAssist(form_structures)); |
| 131 |
| 132 // With valid input, the function extracts the credit card form properly. |
| 133 form_structures.push_back(form_structure.get()); |
| 134 EXPECT_TRUE(autofill_assistant_.CanShowCreditCardAssist(form_structures)); |
| 135 } |
| 136 |
| 137 TEST_F(AutofillAssistantTest, ShowAssistForCreditCard_ValidCard) { |
| 138 EnableAutofillCreditCardAssist(); |
| 139 std::unique_ptr<FormStructure> form_structure = CreateValidCreditCardForm(); |
| 140 |
| 141 // Will extract the credit card form data. |
| 142 std::vector<FormStructure*> form_structures{form_structure.get()}; |
| 143 EXPECT_TRUE(autofill_assistant_.CanShowCreditCardAssist(form_structures)); |
| 144 |
| 145 // Create a valid card for the assist. |
| 146 CreditCard card; |
| 147 test::SetCreditCardInfo(&card, "John Doe", "4111111111111111", "05", "2999"); |
| 148 |
| 149 // FillCreditCardForm ends up being called after user has accepted the |
| 150 // prompt. |
| 151 EXPECT_CALL( |
| 152 autofill_manager_, |
| 153 FillCreditCardForm(kNoQueryId, _, _, CreditCardMatches(card.guid()), |
| 154 /* empty cvc */ base::string16())); |
| 155 |
| 156 autofill_assistant_.ShowAssistForCreditCard(card); |
| 157 } |
| 158 |
| 159 } // namespace autofill |
OLD | NEW |