| 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/browser/test_personal_data_manager.h" |
| 23 #include "components/autofill/core/common/autofill_constants.h" | 23 #include "components/autofill/core/common/autofill_constants.h" |
| 24 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 #include "url/gurl.h" |
| 26 | 27 |
| 27 using testing::_; | 28 using testing::_; |
| 28 | 29 |
| 29 namespace autofill { | 30 namespace autofill { |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 class MockAutofillManager : public AutofillManager { | 33 class MockAutofillManager : public AutofillManager { |
| 33 public: | 34 public: |
| 34 MockAutofillManager(TestAutofillDriver* driver, | 35 MockAutofillManager(TestAutofillDriver* driver, |
| 35 TestAutofillClient* client, | 36 TestAutofillClient* client, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 56 AutofillAssistantTest() | 57 AutofillAssistantTest() |
| 57 : message_loop_(), | 58 : message_loop_(), |
| 58 autofill_client_(), | 59 autofill_client_(), |
| 59 autofill_driver_(), | 60 autofill_driver_(), |
| 60 pdm_(), | 61 pdm_(), |
| 61 autofill_manager_(&autofill_driver_, &autofill_client_, &pdm_), | 62 autofill_manager_(&autofill_driver_, &autofill_client_, &pdm_), |
| 62 autofill_assistant_(&autofill_manager_) {} | 63 autofill_assistant_(&autofill_manager_) {} |
| 63 | 64 |
| 64 void EnableAutofillCreditCardAssist() { | 65 void EnableAutofillCreditCardAssist() { |
| 65 scoped_feature_list_.InitAndEnableFeature(kAutofillCreditCardAssist); | 66 scoped_feature_list_.InitAndEnableFeature(kAutofillCreditCardAssist); |
| 66 autofill_client_.set_is_context_secure(true); | |
| 67 } | 67 } |
| 68 | 68 |
| 69 // Returns an initialized FormStructure with credit card form data. To be | 69 // Returns a valid credit card form. |
| 70 // owned by the caller. | 70 FormData CreateValidCreditCardFormData() { |
| 71 std::unique_ptr<FormStructure> CreateValidCreditCardForm() { | |
| 72 std::unique_ptr<FormStructure> form_structure; | |
| 73 FormData form; | 71 FormData form; |
| 72 form.origin = GURL("https://myform.com"); |
| 73 form.action = GURL("https://myform.com/submit"); |
| 74 | 74 |
| 75 FormFieldData field; | 75 FormFieldData field; |
| 76 field.form_control_type = "text"; | 76 field.form_control_type = "text"; |
| 77 | 77 |
| 78 field.label = base::ASCIIToUTF16("Name on Card"); | 78 field.label = base::ASCIIToUTF16("Name on Card"); |
| 79 field.name = base::ASCIIToUTF16("name_on_card"); | 79 field.name = base::ASCIIToUTF16("name_on_card"); |
| 80 form.fields.push_back(field); | 80 form.fields.push_back(field); |
| 81 | 81 |
| 82 field.label = base::ASCIIToUTF16("Card Number"); | 82 field.label = base::ASCIIToUTF16("Card Number"); |
| 83 field.name = base::ASCIIToUTF16("card_number"); | 83 field.name = base::ASCIIToUTF16("card_number"); |
| 84 form.fields.push_back(field); | 84 form.fields.push_back(field); |
| 85 | 85 |
| 86 field.label = base::ASCIIToUTF16("Exp Month"); | 86 field.label = base::ASCIIToUTF16("Exp Month"); |
| 87 field.name = base::ASCIIToUTF16("ccmonth"); | 87 field.name = base::ASCIIToUTF16("ccmonth"); |
| 88 form.fields.push_back(field); | 88 form.fields.push_back(field); |
| 89 | 89 |
| 90 field.label = base::ASCIIToUTF16("Exp Year"); | 90 field.label = base::ASCIIToUTF16("Exp Year"); |
| 91 field.name = base::ASCIIToUTF16("ccyear"); | 91 field.name = base::ASCIIToUTF16("ccyear"); |
| 92 form.fields.push_back(field); | 92 form.fields.push_back(field); |
| 93 | 93 |
| 94 field.label = base::ASCIIToUTF16("Verification"); | 94 field.label = base::ASCIIToUTF16("Verification"); |
| 95 field.name = base::ASCIIToUTF16("verification"); | 95 field.name = base::ASCIIToUTF16("verification"); |
| 96 form.fields.push_back(field); | 96 form.fields.push_back(field); |
| 97 | 97 |
| 98 form_structure.reset(new FormStructure(form)); | 98 return form; |
| 99 } |
| 100 |
| 101 // Returns an initialized FormStructure with credit card form data. To be |
| 102 // owned by the caller. |
| 103 std::unique_ptr<FormStructure> CreateValidCreditCardForm() { |
| 104 std::unique_ptr<FormStructure> form_structure; |
| 105 form_structure.reset(new FormStructure(CreateValidCreditCardFormData())); |
| 99 form_structure->DetermineHeuristicTypes(); | 106 form_structure->DetermineHeuristicTypes(); |
| 100 | |
| 101 return form_structure; | 107 return form_structure; |
| 102 } | 108 } |
| 103 | 109 |
| 104 base::MessageLoop message_loop_; | 110 base::MessageLoop message_loop_; |
| 105 TestAutofillClient autofill_client_; | 111 TestAutofillClient autofill_client_; |
| 106 testing::NiceMock<TestAutofillDriver> autofill_driver_; | 112 testing::NiceMock<TestAutofillDriver> autofill_driver_; |
| 107 TestPersonalDataManager pdm_; | 113 TestPersonalDataManager pdm_; |
| 108 MockAutofillManager autofill_manager_; | 114 MockAutofillManager autofill_manager_; |
| 109 AutofillAssistant autofill_assistant_; | 115 AutofillAssistant autofill_assistant_; |
| 110 base::test::ScopedFeatureList scoped_feature_list_; | 116 base::test::ScopedFeatureList scoped_feature_list_; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 135 // With valid input, the function extracts the credit card form properly. | 141 // With valid input, the function extracts the credit card form properly. |
| 136 form_structures.push_back(form_structure.get()); | 142 form_structures.push_back(form_structure.get()); |
| 137 EXPECT_TRUE(autofill_assistant_.CanShowCreditCardAssist(form_structures)); | 143 EXPECT_TRUE(autofill_assistant_.CanShowCreditCardAssist(form_structures)); |
| 138 } | 144 } |
| 139 | 145 |
| 140 // Tests that with the feature enabled and proper input, | 146 // Tests that with the feature enabled and proper input, |
| 141 // CanShowCreditCardAssist() behaves as expected for secure vs insecure | 147 // CanShowCreditCardAssist() behaves as expected for secure vs insecure |
| 142 // contexts. | 148 // contexts. |
| 143 TEST_F(AutofillAssistantTest, CanShowCreditCardAssist_FeatureOn_NotSecure) { | 149 TEST_F(AutofillAssistantTest, CanShowCreditCardAssist_FeatureOn_NotSecure) { |
| 144 EnableAutofillCreditCardAssist(); | 150 EnableAutofillCreditCardAssist(); |
| 145 std::unique_ptr<FormStructure> form_structure = CreateValidCreditCardForm(); | |
| 146 std::vector<FormStructure*> form_structures; | |
| 147 form_structures.push_back(form_structure.get()); | |
| 148 | 151 |
| 149 // Cannot be shown if the context is not secure. | 152 { |
| 150 autofill_client_.set_is_context_secure(false); | 153 // Cannot be shown if the context is not secure. |
| 151 EXPECT_FALSE(autofill_assistant_.CanShowCreditCardAssist(form_structures)); | 154 FormData form = CreateValidCreditCardFormData(); |
| 155 form.action = GURL("http://myform.com"); |
| 156 form.action = GURL("http://myform.com/submit"); |
| 157 std::unique_ptr<FormStructure> form_structure(new FormStructure(form)); |
| 158 form_structure->DetermineHeuristicTypes(); |
| 152 | 159 |
| 153 // Can be shown if the context is secure. | 160 std::vector<FormStructure*> form_structures; |
| 154 autofill_client_.set_is_context_secure(true); | 161 form_structures.push_back(form_structure.get()); |
| 155 EXPECT_TRUE(autofill_assistant_.CanShowCreditCardAssist(form_structures)); | 162 EXPECT_FALSE(autofill_assistant_.CanShowCreditCardAssist(form_structures)); |
| 163 } |
| 164 |
| 165 { |
| 166 // Can be shown if the context is secure. |
| 167 FormData form = CreateValidCreditCardFormData(); |
| 168 std::unique_ptr<FormStructure> form_structure(new FormStructure(form)); |
| 169 form_structure->DetermineHeuristicTypes(); |
| 170 |
| 171 std::vector<FormStructure*> form_structures; |
| 172 form_structures.push_back(form_structure.get()); |
| 173 EXPECT_TRUE(autofill_assistant_.CanShowCreditCardAssist(form_structures)); |
| 174 } |
| 156 } | 175 } |
| 157 | 176 |
| 158 TEST_F(AutofillAssistantTest, ShowAssistForCreditCard_ValidCard_CancelCvc) { | 177 TEST_F(AutofillAssistantTest, ShowAssistForCreditCard_ValidCard_CancelCvc) { |
| 159 EnableAutofillCreditCardAssist(); | 178 EnableAutofillCreditCardAssist(); |
| 160 std::unique_ptr<FormStructure> form_structure = CreateValidCreditCardForm(); | 179 std::unique_ptr<FormStructure> form_structure = CreateValidCreditCardForm(); |
| 161 | 180 |
| 162 // Will extract the credit card form data. | 181 // Will extract the credit card form data. |
| 163 std::vector<FormStructure*> form_structures{form_structure.get()}; | 182 std::vector<FormStructure*> form_structures{form_structure.get()}; |
| 164 EXPECT_TRUE(autofill_assistant_.CanShowCreditCardAssist(form_structures)); | 183 EXPECT_TRUE(autofill_assistant_.CanShowCreditCardAssist(form_structures)); |
| 165 | 184 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 autofill_assistant_.ShowAssistForCreditCard(card); | 217 autofill_assistant_.ShowAssistForCreditCard(card); |
| 199 | 218 |
| 200 CardUnmaskDelegate::UnmaskResponse unmask_response; | 219 CardUnmaskDelegate::UnmaskResponse unmask_response; |
| 201 unmask_response.cvc = base::ASCIIToUTF16("123"); | 220 unmask_response.cvc = base::ASCIIToUTF16("123"); |
| 202 static_cast<CardUnmaskDelegate*>( | 221 static_cast<CardUnmaskDelegate*>( |
| 203 autofill_manager_.GetOrCreateFullCardRequest()) | 222 autofill_manager_.GetOrCreateFullCardRequest()) |
| 204 ->OnUnmaskResponse(unmask_response); | 223 ->OnUnmaskResponse(unmask_response); |
| 205 } | 224 } |
| 206 | 225 |
| 207 } // namespace autofill | 226 } // namespace autofill |
| OLD | NEW |