| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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/autofill/core/browser/autofill_assistant.h" | 5 #include "components/autofill/core/browser/autofill_assistant.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/feature_list.h" | 10 #include "base/feature_list.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "base/test/scoped_feature_list.h" | 13 #include "base/test/scoped_feature_list.h" |
| 14 #include "components/autofill/core/browser/autofill_driver.h" | 14 #include "components/autofill/core/browser/autofill_driver.h" |
| 15 #include "components/autofill/core/browser/autofill_experiments.h" | 15 #include "components/autofill/core/browser/autofill_experiments.h" |
| 16 #include "components/autofill/core/browser/autofill_manager.h" | 16 #include "components/autofill/core/browser/autofill_manager.h" |
| 17 #include "components/autofill/core/browser/autofill_test_utils.h" | 17 #include "components/autofill/core/browser/autofill_test_utils.h" |
| 18 #include "components/autofill/core/browser/credit_card.h" | 18 #include "components/autofill/core/browser/credit_card.h" |
| 19 #include "components/autofill/core/browser/form_structure.h" | 19 #include "components/autofill/core/browser/form_structure.h" |
| 20 #include "components/autofill/core/browser/test_autofill_client.h" | 20 #include "components/autofill/core/browser/test_autofill_client.h" |
| 21 #include "components/autofill/core/browser/test_autofill_driver.h" | 21 #include "components/autofill/core/browser/test_autofill_driver.h" |
| 22 #include "components/autofill/core/browser/test_personal_data_manager.h" |
| 22 #include "components/autofill/core/common/autofill_constants.h" | 23 #include "components/autofill/core/common/autofill_constants.h" |
| 23 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 26 |
| 26 using testing::_; | 27 using testing::_; |
| 27 | 28 |
| 28 namespace autofill { | 29 namespace autofill { |
| 29 namespace { | 30 namespace { |
| 30 | 31 |
| 31 class MockAutofillManager : public AutofillManager { | 32 class MockAutofillManager : public AutofillManager { |
| 32 public: | 33 public: |
| 33 MockAutofillManager(TestAutofillDriver* driver, TestAutofillClient* client) | 34 MockAutofillManager(TestAutofillDriver* driver, |
| 34 // Force to use the constructor designated for unit test, but we don't | 35 TestAutofillClient* client, |
| 35 // really need personal_data in this test so we pass a NULL pointer. | 36 PersonalDataManager* pdm) |
| 36 : AutofillManager(driver, client, NULL) {} | 37 // Force to use the constructor designated for unit test. |
| 38 : AutofillManager(driver, client, pdm) {} |
| 37 virtual ~MockAutofillManager() {} | 39 virtual ~MockAutofillManager() {} |
| 38 | 40 |
| 39 MOCK_METHOD5(FillCreditCardForm, | 41 MOCK_METHOD5(FillCreditCardForm, |
| 40 void(int query_id, | 42 void(int query_id, |
| 41 const FormData& form, | 43 const FormData& form, |
| 42 const FormFieldData& field, | 44 const FormFieldData& field, |
| 43 const CreditCard& credit_card, | 45 const CreditCard& credit_card, |
| 44 const base::string16& cvc)); | 46 const base::string16& cvc)); |
| 45 | 47 |
| 46 private: | 48 private: |
| 47 DISALLOW_COPY_AND_ASSIGN(MockAutofillManager); | 49 DISALLOW_COPY_AND_ASSIGN(MockAutofillManager); |
| 48 }; | 50 }; |
| 49 | 51 |
| 50 } // namespace | 52 } // namespace |
| 51 | 53 |
| 52 class AutofillAssistantTest : public testing::Test { | 54 class AutofillAssistantTest : public testing::Test { |
| 53 protected: | 55 protected: |
| 54 AutofillAssistantTest() | 56 AutofillAssistantTest() |
| 55 : message_loop_(), | 57 : message_loop_(), |
| 56 autofill_client_(), | 58 autofill_client_(), |
| 57 autofill_driver_(), | 59 autofill_driver_(), |
| 58 autofill_manager_(&autofill_driver_, &autofill_client_), | 60 pdm_(), |
| 61 autofill_manager_(&autofill_driver_, &autofill_client_, &pdm_), |
| 59 autofill_assistant_(&autofill_manager_) {} | 62 autofill_assistant_(&autofill_manager_) {} |
| 60 | 63 |
| 61 void EnableAutofillCreditCardAssist() { | 64 void EnableAutofillCreditCardAssist() { |
| 62 scoped_feature_list_.InitAndEnableFeature(kAutofillCreditCardAssist); | 65 scoped_feature_list_.InitAndEnableFeature(kAutofillCreditCardAssist); |
| 63 } | 66 } |
| 64 | 67 |
| 65 // Returns an initialized FormStructure with credit card form data. To be | 68 // Returns an initialized FormStructure with credit card form data. To be |
| 66 // owned by the caller. | 69 // owned by the caller. |
| 67 std::unique_ptr<FormStructure> CreateValidCreditCardForm() { | 70 std::unique_ptr<FormStructure> CreateValidCreditCardForm() { |
| 68 std::unique_ptr<FormStructure> form_structure; | 71 std::unique_ptr<FormStructure> form_structure; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 93 | 96 |
| 94 form_structure.reset(new FormStructure(form)); | 97 form_structure.reset(new FormStructure(form)); |
| 95 form_structure->DetermineHeuristicTypes(); | 98 form_structure->DetermineHeuristicTypes(); |
| 96 | 99 |
| 97 return form_structure; | 100 return form_structure; |
| 98 } | 101 } |
| 99 | 102 |
| 100 base::MessageLoop message_loop_; | 103 base::MessageLoop message_loop_; |
| 101 TestAutofillClient autofill_client_; | 104 TestAutofillClient autofill_client_; |
| 102 testing::NiceMock<TestAutofillDriver> autofill_driver_; | 105 testing::NiceMock<TestAutofillDriver> autofill_driver_; |
| 106 TestPersonalDataManager pdm_; |
| 103 MockAutofillManager autofill_manager_; | 107 MockAutofillManager autofill_manager_; |
| 104 AutofillAssistant autofill_assistant_; | 108 AutofillAssistant autofill_assistant_; |
| 105 base::test::ScopedFeatureList scoped_feature_list_; | 109 base::test::ScopedFeatureList scoped_feature_list_; |
| 106 }; | 110 }; |
| 107 | 111 |
| 108 MATCHER_P(CreditCardMatches, guid, "") { | 112 MATCHER_P(CreditCardMatches, guid, "") { |
| 109 return arg.guid() == guid; | 113 return arg.guid() == guid; |
| 110 } | 114 } |
| 111 | 115 |
| 112 // If the feature is turned off, CanShowCreditCardAssist() always returns | 116 // If the feature is turned off, CanShowCreditCardAssist() always returns |
| (...skipping 12 matching lines...) Expand all Loading... |
| 125 std::unique_ptr<FormStructure> form_structure = CreateValidCreditCardForm(); | 129 std::unique_ptr<FormStructure> form_structure = CreateValidCreditCardForm(); |
| 126 | 130 |
| 127 std::vector<FormStructure*> form_structures; | 131 std::vector<FormStructure*> form_structures; |
| 128 EXPECT_FALSE(autofill_assistant_.CanShowCreditCardAssist(form_structures)); | 132 EXPECT_FALSE(autofill_assistant_.CanShowCreditCardAssist(form_structures)); |
| 129 | 133 |
| 130 // With valid input, the function extracts the credit card form properly. | 134 // With valid input, the function extracts the credit card form properly. |
| 131 form_structures.push_back(form_structure.get()); | 135 form_structures.push_back(form_structure.get()); |
| 132 EXPECT_TRUE(autofill_assistant_.CanShowCreditCardAssist(form_structures)); | 136 EXPECT_TRUE(autofill_assistant_.CanShowCreditCardAssist(form_structures)); |
| 133 } | 137 } |
| 134 | 138 |
| 135 TEST_F(AutofillAssistantTest, ShowAssistForCreditCard_ValidCard) { | 139 TEST_F(AutofillAssistantTest, ShowAssistForCreditCard_ValidCard_CancelCvc) { |
| 136 EnableAutofillCreditCardAssist(); | 140 EnableAutofillCreditCardAssist(); |
| 137 std::unique_ptr<FormStructure> form_structure = CreateValidCreditCardForm(); | 141 std::unique_ptr<FormStructure> form_structure = CreateValidCreditCardForm(); |
| 138 | 142 |
| 143 // Will extract the credit card form data. |
| 144 std::vector<FormStructure*> form_structures{form_structure.get()}; |
| 145 EXPECT_TRUE(autofill_assistant_.CanShowCreditCardAssist(form_structures)); |
| 146 |
| 147 // Create a valid card for the assist. |
| 148 CreditCard card; |
| 149 test::SetCreditCardInfo(&card, "John Doe", "4111111111111111", "05", "2999"); |
| 150 |
| 151 // FillCreditCardForm should not be called if the user cancelled the CVC. |
| 152 EXPECT_CALL(autofill_manager_, FillCreditCardForm(_, _, _, _, _)).Times(0); |
| 153 |
| 154 autofill_assistant_.ShowAssistForCreditCard(card); |
| 155 static_cast<CardUnmaskDelegate*>( |
| 156 autofill_manager_.GetOrCreateFullCardRequest()) |
| 157 ->OnUnmaskPromptClosed(); |
| 158 } |
| 159 |
| 160 TEST_F(AutofillAssistantTest, ShowAssistForCreditCard_ValidCard_SubmitCvc) { |
| 161 EnableAutofillCreditCardAssist(); |
| 162 std::unique_ptr<FormStructure> form_structure = CreateValidCreditCardForm(); |
| 163 |
| 139 // Will extract the credit card form data. | 164 // Will extract the credit card form data. |
| 140 std::vector<FormStructure*> form_structures{form_structure.get()}; | 165 std::vector<FormStructure*> form_structures{form_structure.get()}; |
| 141 EXPECT_TRUE(autofill_assistant_.CanShowCreditCardAssist(form_structures)); | 166 EXPECT_TRUE(autofill_assistant_.CanShowCreditCardAssist(form_structures)); |
| 142 | 167 |
| 143 // Create a valid card for the assist. | 168 // Create a valid card for the assist. |
| 144 CreditCard card; | 169 CreditCard card; |
| 145 test::SetCreditCardInfo(&card, "John Doe", "4111111111111111", "05", "2999"); | 170 test::SetCreditCardInfo(&card, "John Doe", "4111111111111111", "05", "2999"); |
| 146 | 171 |
| 147 // FillCreditCardForm ends up being called after user has accepted the | 172 // FillCreditCardForm ends up being called after user has accepted the |
| 148 // prompt. | 173 // prompt. |
| 149 EXPECT_CALL( | 174 EXPECT_CALL( |
| 150 autofill_manager_, | 175 autofill_manager_, |
| 151 FillCreditCardForm(kNoQueryId, _, _, CreditCardMatches(card.guid()), | 176 FillCreditCardForm(kNoQueryId, _, _, CreditCardMatches(card.guid()), |
| 152 /* empty cvc */ base::string16())); | 177 base::ASCIIToUTF16("123"))); |
| 153 | 178 |
| 154 autofill_assistant_.ShowAssistForCreditCard(card); | 179 autofill_assistant_.ShowAssistForCreditCard(card); |
| 180 |
| 181 CardUnmaskDelegate::UnmaskResponse unmask_response; |
| 182 unmask_response.cvc = base::ASCIIToUTF16("123"); |
| 183 static_cast<CardUnmaskDelegate*>( |
| 184 autofill_manager_.GetOrCreateFullCardRequest()) |
| 185 ->OnUnmaskResponse(unmask_response); |
| 155 } | 186 } |
| 156 | 187 |
| 157 } // namespace autofill | 188 } // namespace autofill |
| OLD | NEW |