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 | |
49 } // namespace | |
50 | |
51 class AssistManagerTest : public testing::Test { | |
52 protected: | |
53 AssistManagerTest() | |
54 : autofill_driver_(new testing::NiceMock<TestAutofillDriver>()) { | |
55 autofill_manager_.reset( | |
56 new MockAutofillManager(autofill_driver_.get(), &autofill_client_)); | |
57 assist_manager_.reset(new AssistManager(autofill_manager_.get())); | |
58 } | |
please use gerrit instead
2016/07/29 16:33:20
Nit: I have not tested this, but this should work
Mathieu
2016/07/29 20:19:17
Done.
| |
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 std::move(form_structure); | |
101 } | |
102 | |
103 TestAutofillClient autofill_client_; | |
104 std::unique_ptr<testing::NiceMock<TestAutofillDriver>> autofill_driver_; | |
105 std::unique_ptr<MockAutofillManager> autofill_manager_; | |
106 std::unique_ptr<AssistManager> assist_manager_; | |
107 | |
108 base::MessageLoop message_loop_; | |
109 }; | |
110 | |
111 MATCHER_P(CreditCardMatches, guid, "") { | |
112 return arg.guid() == guid; | |
113 } | |
114 | |
115 // If the feature is turned off, CanShowCreditCardAssist() always returns | |
116 // false. | |
117 TEST_F(AssistManagerTest, CanShowCreditCardAssist_FeatureOff) { | |
118 std::unique_ptr<FormStructure> form_structure = CreateValidCreditCardForm(); | |
119 | |
120 std::vector<FormStructure*> form_structures{form_structure.get()}; | |
121 EXPECT_FALSE(assist_manager_->CanShowCreditCardAssist(form_structures)); | |
122 } | |
123 | |
124 // Tests that with the feature enabled and proper input, | |
125 // CanShowCreditCardAssist() behaves as expected. | |
126 TEST_F(AssistManagerTest, CanShowCreditCardAssist_FeatureOn) { | |
127 EnableAutofillCreditCardAssist(); | |
128 std::unique_ptr<FormStructure> form_structure = CreateValidCreditCardForm(); | |
129 | |
130 std::vector<FormStructure*> form_structures; | |
131 EXPECT_FALSE(assist_manager_->CanShowCreditCardAssist(form_structures)); | |
132 | |
133 // With valid input, the function extracts the credit card form properly. | |
134 form_structures.push_back(form_structure.get()); | |
135 EXPECT_TRUE(assist_manager_->CanShowCreditCardAssist(form_structures)); | |
136 } | |
137 | |
138 TEST_F(AssistManagerTest, ShowAssistForCreditCard_ValidCard) { | |
139 EnableAutofillCreditCardAssist(); | |
140 std::unique_ptr<FormStructure> form_structure = CreateValidCreditCardForm(); | |
141 | |
142 // Will extract the credit card form data. | |
143 std::vector<FormStructure*> form_structures{form_structure.get()}; | |
144 EXPECT_TRUE(assist_manager_->CanShowCreditCardAssist(form_structures)); | |
145 | |
146 // Create a valid card for the assist. | |
147 CreditCard card; | |
148 test::SetCreditCardInfo(&card, "John Doe", "4111111111111111", "05", "2999"); | |
149 | |
150 // FillCreditCardForm ends up being called after user has accepted the | |
151 // prompt. | |
152 EXPECT_CALL( | |
153 *autofill_manager_, | |
154 FillCreditCardForm(kNoQueryId, _, _, CreditCardMatches(card.guid()), | |
155 /* empty cvc */ base::string16())); | |
156 | |
157 assist_manager_->ShowAssistForCreditCard(card); | |
158 } | |
159 | |
160 } // namespace autofill | |
OLD | NEW |