| 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 autofill_client_.set_is_context_secure(true); | 66 autofill_client_.set_is_context_secure(true); |
| 64 } | 67 } |
| 65 | 68 |
| 66 // Returns an initialized FormStructure with credit card form data. To be | 69 // Returns an initialized FormStructure with credit card form data. To be |
| 67 // owned by the caller. | 70 // owned by the caller. |
| 68 std::unique_ptr<FormStructure> CreateValidCreditCardForm() { | 71 std::unique_ptr<FormStructure> CreateValidCreditCardForm() { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 94 | 97 |
| 95 form_structure.reset(new FormStructure(form)); | 98 form_structure.reset(new FormStructure(form)); |
| 96 form_structure->DetermineHeuristicTypes(); | 99 form_structure->DetermineHeuristicTypes(); |
| 97 | 100 |
| 98 return form_structure; | 101 return form_structure; |
| 99 } | 102 } |
| 100 | 103 |
| 101 base::MessageLoop message_loop_; | 104 base::MessageLoop message_loop_; |
| 102 TestAutofillClient autofill_client_; | 105 TestAutofillClient autofill_client_; |
| 103 testing::NiceMock<TestAutofillDriver> autofill_driver_; | 106 testing::NiceMock<TestAutofillDriver> autofill_driver_; |
| 107 TestPersonalDataManager pdm_; |
| 104 MockAutofillManager autofill_manager_; | 108 MockAutofillManager autofill_manager_; |
| 105 AutofillAssistant autofill_assistant_; | 109 AutofillAssistant autofill_assistant_; |
| 106 base::test::ScopedFeatureList scoped_feature_list_; | 110 base::test::ScopedFeatureList scoped_feature_list_; |
| 107 }; | 111 }; |
| 108 | 112 |
| 109 MATCHER_P(CreditCardMatches, guid, "") { | 113 MATCHER_P(CreditCardMatches, guid, "") { |
| 110 return arg.guid() == guid; | 114 return arg.guid() == guid; |
| 111 } | 115 } |
| 112 | 116 |
| 113 // If the feature is turned off, CanShowCreditCardAssist() always returns | 117 // If the feature is turned off, CanShowCreditCardAssist() always returns |
| (...skipping 30 matching lines...) Expand all Loading... |
| 144 | 148 |
| 145 // Cannot be shown if the context is not secure. | 149 // Cannot be shown if the context is not secure. |
| 146 autofill_client_.set_is_context_secure(false); | 150 autofill_client_.set_is_context_secure(false); |
| 147 EXPECT_FALSE(autofill_assistant_.CanShowCreditCardAssist(form_structures)); | 151 EXPECT_FALSE(autofill_assistant_.CanShowCreditCardAssist(form_structures)); |
| 148 | 152 |
| 149 // Can be shown if the context is secure. | 153 // Can be shown if the context is secure. |
| 150 autofill_client_.set_is_context_secure(true); | 154 autofill_client_.set_is_context_secure(true); |
| 151 EXPECT_TRUE(autofill_assistant_.CanShowCreditCardAssist(form_structures)); | 155 EXPECT_TRUE(autofill_assistant_.CanShowCreditCardAssist(form_structures)); |
| 152 } | 156 } |
| 153 | 157 |
| 154 TEST_F(AutofillAssistantTest, ShowAssistForCreditCard_ValidCard) { | 158 TEST_F(AutofillAssistantTest, ShowAssistForCreditCard_ValidCard_CancelCvc) { |
| 155 EnableAutofillCreditCardAssist(); | 159 EnableAutofillCreditCardAssist(); |
| 156 std::unique_ptr<FormStructure> form_structure = CreateValidCreditCardForm(); | 160 std::unique_ptr<FormStructure> form_structure = CreateValidCreditCardForm(); |
| 157 | 161 |
| 162 // Will extract the credit card form data. |
| 163 std::vector<FormStructure*> form_structures{form_structure.get()}; |
| 164 EXPECT_TRUE(autofill_assistant_.CanShowCreditCardAssist(form_structures)); |
| 165 |
| 166 // Create a valid card for the assist. |
| 167 CreditCard card; |
| 168 test::SetCreditCardInfo(&card, "John Doe", "4111111111111111", "05", "2999"); |
| 169 |
| 170 // FillCreditCardForm should not be called if the user cancelled the CVC. |
| 171 EXPECT_CALL(autofill_manager_, FillCreditCardForm(_, _, _, _, _)).Times(0); |
| 172 |
| 173 autofill_assistant_.ShowAssistForCreditCard(card); |
| 174 static_cast<CardUnmaskDelegate*>( |
| 175 autofill_manager_.GetOrCreateFullCardRequest()) |
| 176 ->OnUnmaskPromptClosed(); |
| 177 } |
| 178 |
| 179 TEST_F(AutofillAssistantTest, ShowAssistForCreditCard_ValidCard_SubmitCvc) { |
| 180 EnableAutofillCreditCardAssist(); |
| 181 std::unique_ptr<FormStructure> form_structure = CreateValidCreditCardForm(); |
| 182 |
| 158 // Will extract the credit card form data. | 183 // Will extract the credit card form data. |
| 159 std::vector<FormStructure*> form_structures{form_structure.get()}; | 184 std::vector<FormStructure*> form_structures{form_structure.get()}; |
| 160 EXPECT_TRUE(autofill_assistant_.CanShowCreditCardAssist(form_structures)); | 185 EXPECT_TRUE(autofill_assistant_.CanShowCreditCardAssist(form_structures)); |
| 161 | 186 |
| 162 // Create a valid card for the assist. | 187 // Create a valid card for the assist. |
| 163 CreditCard card; | 188 CreditCard card; |
| 164 test::SetCreditCardInfo(&card, "John Doe", "4111111111111111", "05", "2999"); | 189 test::SetCreditCardInfo(&card, "John Doe", "4111111111111111", "05", "2999"); |
| 165 | 190 |
| 166 // FillCreditCardForm ends up being called after user has accepted the | 191 // FillCreditCardForm ends up being called after user has accepted the |
| 167 // prompt. | 192 // prompt. |
| 168 EXPECT_CALL( | 193 EXPECT_CALL( |
| 169 autofill_manager_, | 194 autofill_manager_, |
| 170 FillCreditCardForm(kNoQueryId, _, _, CreditCardMatches(card.guid()), | 195 FillCreditCardForm(kNoQueryId, _, _, CreditCardMatches(card.guid()), |
| 171 /* empty cvc */ base::string16())); | 196 base::ASCIIToUTF16("123"))); |
| 172 | 197 |
| 173 autofill_assistant_.ShowAssistForCreditCard(card); | 198 autofill_assistant_.ShowAssistForCreditCard(card); |
| 199 |
| 200 CardUnmaskDelegate::UnmaskResponse unmask_response; |
| 201 unmask_response.cvc = base::ASCIIToUTF16("123"); |
| 202 static_cast<CardUnmaskDelegate*>( |
| 203 autofill_manager_.GetOrCreateFullCardRequest()) |
| 204 ->OnUnmaskResponse(unmask_response); |
| 174 } | 205 } |
| 175 | 206 |
| 176 } // namespace autofill | 207 } // namespace autofill |
| OLD | NEW |