Chromium Code Reviews| 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/assist_manager.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 } | |
|
please use gerrit instead
2016/07/27 17:02:26
} // namespace
Mathieu
2016/07/27 21:35:53
Done.
| |
| 49 | |
| 50 class AssistManagerTest : public testing::Test { | |
| 51 protected: | |
| 52 void SetUp() override { | |
|
please use gerrit instead
2016/07/27 17:02:26
Gtests work like this:
running CanShowCreditCardA
Mathieu
2016/07/27 21:35:52
Done.
| |
| 53 autofill_driver_.reset(new testing::NiceMock<TestAutofillDriver>()); | |
| 54 autofill_manager_.reset( | |
| 55 new MockAutofillManager(autofill_driver_.get(), &autofill_client_)); | |
| 56 assist_manager_.reset(new AssistManager(autofill_manager_.get())); | |
| 57 } | |
| 58 | |
| 59 void TearDown() override { | |
|
please use gerrit instead
2016/07/27 17:02:26
Redundant, because every test case destroys Assist
Mathieu
2016/07/27 21:35:53
Done.
| |
| 60 autofill_manager_.reset(); | |
| 61 autofill_driver_.reset(); | |
| 62 assist_manager_.reset(); | |
| 63 } | |
| 64 | |
| 65 void EnableAutofillCreditCardAssist() { | |
| 66 base::FeatureList::ClearInstanceForTesting(); | |
| 67 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); | |
| 68 feature_list->InitializeFromCommandLine(kAutofillCreditCardAssist.name, | |
| 69 std::string()); | |
| 70 base::FeatureList::SetInstance(std::move(feature_list)); | |
| 71 } | |
| 72 | |
| 73 // Returns an initialized FormStructure with credit card form data. To be | |
| 74 // owned by the caller. | |
| 75 std::unique_ptr<FormStructure> CreateValidCreditCardForm() { | |
| 76 std::unique_ptr<FormStructure> form_structure; | |
| 77 FormData form; | |
| 78 | |
| 79 FormFieldData field; | |
| 80 field.form_control_type = "text"; | |
| 81 | |
| 82 field.label = base::ASCIIToUTF16("Name on Card"); | |
| 83 field.name = base::ASCIIToUTF16("name_on_card"); | |
| 84 form.fields.push_back(field); | |
| 85 | |
| 86 field.label = base::ASCIIToUTF16("Card Number"); | |
| 87 field.name = base::ASCIIToUTF16("card_number"); | |
| 88 form.fields.push_back(field); | |
| 89 | |
| 90 field.label = base::ASCIIToUTF16("Exp Month"); | |
| 91 field.name = base::ASCIIToUTF16("ccmonth"); | |
| 92 form.fields.push_back(field); | |
| 93 | |
| 94 field.label = base::ASCIIToUTF16("Exp Year"); | |
| 95 field.name = base::ASCIIToUTF16("ccyear"); | |
| 96 form.fields.push_back(field); | |
| 97 | |
| 98 field.label = base::ASCIIToUTF16("Verification"); | |
| 99 field.name = base::ASCIIToUTF16("verification"); | |
| 100 form.fields.push_back(field); | |
| 101 | |
| 102 form_structure.reset(new FormStructure(form)); | |
| 103 form_structure->DetermineHeuristicTypes(); | |
| 104 | |
| 105 return std::move(form_structure); | |
| 106 } | |
| 107 | |
| 108 TestAutofillClient autofill_client_; | |
| 109 std::unique_ptr<testing::NiceMock<TestAutofillDriver>> autofill_driver_; | |
| 110 std::unique_ptr<MockAutofillManager> autofill_manager_; | |
| 111 std::unique_ptr<AssistManager> assist_manager_; | |
| 112 | |
| 113 base::MessageLoop message_loop_; | |
| 114 }; | |
| 115 | |
| 116 MATCHER_P(CreditCardMatches, guid, "") { | |
| 117 return arg.guid() == guid; | |
| 118 } | |
| 119 | |
| 120 // If the feature is turned off, CanShowCreditCardAssist() always returns | |
| 121 // false. | |
| 122 TEST_F(AssistManagerTest, CanShowCreditCardAssist_FeatureOff) { | |
| 123 std::unique_ptr<FormStructure> form_structure = CreateValidCreditCardForm(); | |
| 124 | |
| 125 std::vector<FormStructure*> form_structures{form_structure.get()}; | |
| 126 EXPECT_FALSE(assist_manager_->CanShowCreditCardAssist(form_structures)); | |
| 127 } | |
| 128 | |
| 129 // Tests that with the feature enabled and proper input, | |
| 130 // CanShowCreditCardAssist() behaves as expected. | |
| 131 TEST_F(AssistManagerTest, CanShowCreditCardAssist_FeatureOn) { | |
| 132 EnableAutofillCreditCardAssist(); | |
| 133 std::unique_ptr<FormStructure> form_structure = CreateValidCreditCardForm(); | |
| 134 | |
| 135 std::vector<FormStructure*> form_structures; | |
| 136 EXPECT_FALSE(assist_manager_->CanShowCreditCardAssist(form_structures)); | |
| 137 | |
| 138 // With valid input, the function extracts the credit card form properly. | |
| 139 form_structures.push_back(form_structure.get()); | |
| 140 EXPECT_TRUE(assist_manager_->CanShowCreditCardAssist(form_structures)); | |
| 141 } | |
| 142 | |
| 143 TEST_F(AssistManagerTest, ShowAssistForCreditCard_ValidCard) { | |
| 144 EnableAutofillCreditCardAssist(); | |
| 145 std::unique_ptr<FormStructure> form_structure = CreateValidCreditCardForm(); | |
| 146 | |
| 147 // Will extract the credit card form data. | |
| 148 std::vector<FormStructure*> form_structures{form_structure.get()}; | |
| 149 EXPECT_TRUE(assist_manager_->CanShowCreditCardAssist(form_structures)); | |
| 150 | |
| 151 // Create a valid card for the assist. | |
| 152 CreditCard card; | |
| 153 test::SetCreditCardInfo(&card, "John Doe", "4111111111111111", "05", "2999"); | |
| 154 | |
| 155 // FillCreditCardForm ends up being called after user has accepted the | |
| 156 // prompt. | |
| 157 EXPECT_CALL( | |
| 158 *autofill_manager_, | |
| 159 FillCreditCardForm(kNoQueryId, _, _, CreditCardMatches(card.guid()), | |
| 160 /* empty cvc */ base::string16())); | |
| 161 | |
| 162 assist_manager_->ShowAssistForCreditCard(card); | |
| 163 } | |
| 164 | |
| 165 } // namespace autofill | |
| OLD | NEW |