Include all dupe types (event when value is zero) in scan stats.
[chromium-blink-merge.git] / components / autofill / core / browser / autofill_external_delegate_unittest.cc
blobe9bef0665e5e22cae944150580c6ea8e970fa972
1 // Copyright 2013 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.
5 #include <vector>
7 #include "base/command_line.h"
8 #include "base/compiler_specific.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/string16.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/test/histogram_tester.h"
13 #include "components/autofill/core/browser/autofill_manager.h"
14 #include "components/autofill/core/browser/autofill_metrics.h"
15 #include "components/autofill/core/browser/popup_item_ids.h"
16 #include "components/autofill/core/browser/suggestion_test_helpers.h"
17 #include "components/autofill/core/browser/test_autofill_client.h"
18 #include "components/autofill/core/browser/test_autofill_driver.h"
19 #include "components/autofill/core/browser/test_autofill_external_delegate.h"
20 #include "components/autofill/core/common/autofill_switches.h"
21 #include "components/autofill/core/common/form_data.h"
22 #include "components/autofill/core/common/form_field_data.h"
23 #include "components/autofill/core/common/password_form_fill_data.h"
24 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26 #include "ui/gfx/geometry/rect.h"
28 using base::ASCIIToUTF16;
29 using testing::_;
31 namespace autofill {
33 namespace {
35 // A constant value to use as the Autofill query ID.
36 const int kQueryId = 5;
38 // A constant value to use as an Autofill profile ID.
39 const int kAutofillProfileId = 1;
41 class MockAutofillDriver : public TestAutofillDriver {
42 public:
43 MockAutofillDriver() {}
44 // Mock methods to enable testability.
45 MOCK_METHOD1(RendererShouldAcceptDataListSuggestion,
46 void(const base::string16&));
47 MOCK_METHOD0(RendererShouldClearFilledForm, void());
48 MOCK_METHOD0(RendererShouldClearPreviewedForm, void());
49 MOCK_METHOD1(RendererShouldFillFieldWithValue, void(const base::string16&));
50 MOCK_METHOD1(RendererShouldPreviewFieldWithValue,
51 void(const base::string16&));
53 private:
54 DISALLOW_COPY_AND_ASSIGN(MockAutofillDriver);
57 class MockAutofillClient : public autofill::TestAutofillClient {
58 public:
59 MockAutofillClient() {}
61 MOCK_METHOD1(ScanCreditCard,
62 void(const CreditCardScanCallback& callbacK));
64 MOCK_METHOD4(ShowAutofillPopup,
65 void(const gfx::RectF& element_bounds,
66 base::i18n::TextDirection text_direction,
67 const std::vector<Suggestion>& suggestions,
68 base::WeakPtr<AutofillPopupDelegate> delegate));
70 MOCK_METHOD2(UpdateAutofillPopupDataListValues,
71 void(const std::vector<base::string16>& values,
72 const std::vector<base::string16>& lables));
74 MOCK_METHOD0(HideAutofillPopup, void());
76 private:
77 DISALLOW_COPY_AND_ASSIGN(MockAutofillClient);
80 class MockAutofillManager : public AutofillManager {
81 public:
82 MockAutofillManager(AutofillDriver* driver, MockAutofillClient* client)
83 // Force to use the constructor designated for unit test, but we don't
84 // really need personal_data in this test so we pass a NULL pointer.
85 : AutofillManager(driver, client, NULL) {}
86 virtual ~MockAutofillManager() {}
88 MOCK_METHOD2(ShouldShowScanCreditCard,
89 bool(const FormData& form, const FormFieldData& field));
91 MOCK_METHOD5(FillOrPreviewForm,
92 void(AutofillDriver::RendererFormDataAction action,
93 int query_id,
94 const FormData& form,
95 const FormFieldData& field,
96 int unique_id));
98 MOCK_METHOD4(FillCreditCardForm,
99 void(int query_id,
100 const FormData& form,
101 const FormFieldData& field,
102 const CreditCard& credit_card));
104 private:
105 DISALLOW_COPY_AND_ASSIGN(MockAutofillManager);
108 } // namespace
110 class AutofillExternalDelegateUnitTest : public testing::Test {
111 protected:
112 void SetUp() override {
113 autofill_driver_.reset(new testing::NiceMock<MockAutofillDriver>());
114 autofill_manager_.reset(
115 new MockAutofillManager(autofill_driver_.get(), &autofill_client_));
116 external_delegate_.reset(
117 new AutofillExternalDelegate(
118 autofill_manager_.get(), autofill_driver_.get()));
121 void TearDown() override {
122 // Order of destruction is important as AutofillManager relies on
123 // PersonalDataManager to be around when it gets destroyed.
124 autofill_manager_.reset();
125 external_delegate_.reset();
126 autofill_driver_.reset();
129 // Issue an OnQuery call with the given |query_id|.
130 void IssueOnQuery(int query_id) {
131 const FormData form;
132 FormFieldData field;
133 field.is_focusable = true;
134 field.should_autocomplete = true;
135 const gfx::RectF element_bounds;
137 external_delegate_->OnQuery(query_id, form, field, element_bounds);
140 void IssueOnSuggestionsReturned() {
141 std::vector<Suggestion> suggestions;
142 suggestions.push_back(Suggestion());
143 suggestions[0].frontend_id = kAutofillProfileId;
144 external_delegate_->OnSuggestionsReturned(kQueryId, suggestions);
147 testing::NiceMock<MockAutofillClient> autofill_client_;
148 scoped_ptr<testing::NiceMock<MockAutofillDriver>> autofill_driver_;
149 scoped_ptr<MockAutofillManager> autofill_manager_;
150 scoped_ptr<AutofillExternalDelegate> external_delegate_;
152 base::MessageLoop message_loop_;
155 // Test that our external delegate called the virtual methods at the right time.
156 TEST_F(AutofillExternalDelegateUnitTest, TestExternalDelegateVirtualCalls) {
157 IssueOnQuery(kQueryId);
159 // The enums must be cast to ints to prevent compile errors on linux_rel.
160 auto element_ids = testing::ElementsAre(
161 kAutofillProfileId,
162 #if !defined(OS_ANDROID)
163 static_cast<int>(POPUP_ITEM_ID_SEPARATOR),
164 #endif
165 static_cast<int>(POPUP_ITEM_ID_AUTOFILL_OPTIONS));
166 EXPECT_CALL(
167 autofill_client_,
168 ShowAutofillPopup(_, _, SuggestionVectorIdsAre(element_ids), _));
170 // This should call ShowAutofillPopup.
171 std::vector<Suggestion> autofill_item;
172 autofill_item.push_back(Suggestion());
173 autofill_item[0].frontend_id = kAutofillProfileId;
174 external_delegate_->OnSuggestionsReturned(kQueryId, autofill_item);
176 EXPECT_CALL(*autofill_manager_,
177 FillOrPreviewForm(
178 AutofillDriver::FORM_DATA_ACTION_FILL, _, _, _, _));
179 EXPECT_CALL(autofill_client_, HideAutofillPopup());
181 // This should trigger a call to hide the popup since we've selected an
182 // option.
183 external_delegate_->DidAcceptSuggestion(autofill_item[0].value,
184 autofill_item[0].frontend_id);
187 // Test that data list elements for a node will appear in the Autofill popup.
188 TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateDataList) {
189 IssueOnQuery(kQueryId);
191 std::vector<base::string16> data_list_items;
192 data_list_items.push_back(base::string16());
194 EXPECT_CALL(
195 autofill_client_,
196 UpdateAutofillPopupDataListValues(data_list_items, data_list_items));
198 external_delegate_->SetCurrentDataListValues(data_list_items,
199 data_list_items);
201 // The enums must be cast to ints to prevent compile errors on linux_rel.
202 auto element_ids = testing::ElementsAre(
203 static_cast<int>(POPUP_ITEM_ID_DATALIST_ENTRY),
204 #if !defined(OS_ANDROID)
205 static_cast<int>(POPUP_ITEM_ID_SEPARATOR),
206 #endif
207 kAutofillProfileId,
208 #if !defined(OS_ANDROID)
209 static_cast<int>(POPUP_ITEM_ID_SEPARATOR),
210 #endif
211 static_cast<int>(POPUP_ITEM_ID_AUTOFILL_OPTIONS));
212 EXPECT_CALL(
213 autofill_client_,
214 ShowAutofillPopup(_, _, SuggestionVectorIdsAre(element_ids), _));
216 // This should call ShowAutofillPopup.
217 std::vector<Suggestion> autofill_item;
218 autofill_item.push_back(Suggestion());
219 autofill_item[0].frontend_id = kAutofillProfileId;
220 external_delegate_->OnSuggestionsReturned(kQueryId, autofill_item);
222 // Try calling OnSuggestionsReturned with no Autofill values and ensure
223 // the datalist items are still shown.
224 // The enum must be cast to an int to prevent compile errors on linux_rel.
225 EXPECT_CALL(
226 autofill_client_,
227 ShowAutofillPopup(
230 SuggestionVectorIdsAre(testing::ElementsAre(
231 static_cast<int>(POPUP_ITEM_ID_DATALIST_ENTRY))),
232 _));
234 autofill_item.clear();
235 external_delegate_->OnSuggestionsReturned(kQueryId, autofill_item);
238 // Test that datalist values can get updated while a popup is showing.
239 TEST_F(AutofillExternalDelegateUnitTest, UpdateDataListWhileShowingPopup) {
240 IssueOnQuery(kQueryId);
242 EXPECT_CALL(autofill_client_, ShowAutofillPopup(_, _, _, _))
243 .Times(0);
245 // Make sure just setting the data list values doesn't cause the popup to
246 // appear.
247 std::vector<base::string16> data_list_items;
248 data_list_items.push_back(base::string16());
250 EXPECT_CALL(
251 autofill_client_,
252 UpdateAutofillPopupDataListValues(data_list_items, data_list_items));
254 external_delegate_->SetCurrentDataListValues(data_list_items,
255 data_list_items);
257 // The enums must be cast to ints to prevent compile errors on linux_rel.
258 auto element_ids = testing::ElementsAre(
259 static_cast<int>(POPUP_ITEM_ID_DATALIST_ENTRY),
260 #if !defined(OS_ANDROID)
261 static_cast<int>(POPUP_ITEM_ID_SEPARATOR),
262 #endif
263 kAutofillProfileId,
264 #if !defined(OS_ANDROID)
265 static_cast<int>(POPUP_ITEM_ID_SEPARATOR),
266 #endif
267 static_cast<int>(POPUP_ITEM_ID_AUTOFILL_OPTIONS));
268 EXPECT_CALL(
269 autofill_client_,
270 ShowAutofillPopup(_, _, SuggestionVectorIdsAre(element_ids), _));
272 // Ensure the popup is displayed.
273 std::vector<Suggestion> autofill_item;
274 autofill_item.push_back(Suggestion());
275 autofill_item[0].frontend_id = kAutofillProfileId;
276 external_delegate_->OnSuggestionsReturned(kQueryId, autofill_item);
278 // This would normally get called from ShowAutofillPopup, but it is mocked so
279 // we need to call OnPopupShown ourselves.
280 external_delegate_->OnPopupShown();
282 // Update the current data list and ensure the popup is updated.
283 data_list_items.push_back(base::string16());
285 // The enums must be cast to ints to prevent compile errors on linux_rel.
286 EXPECT_CALL(
287 autofill_client_,
288 UpdateAutofillPopupDataListValues(data_list_items, data_list_items));
290 external_delegate_->SetCurrentDataListValues(data_list_items,
291 data_list_items);
294 // Test that the Autofill popup is able to display warnings explaining why
295 // Autofill is disabled for a website.
296 // Regression test for http://crbug.com/247880
297 TEST_F(AutofillExternalDelegateUnitTest, AutofillWarnings) {
298 IssueOnQuery(kQueryId);
300 // The enums must be cast to ints to prevent compile errors on linux_rel.
301 EXPECT_CALL(
302 autofill_client_,
303 ShowAutofillPopup(
306 SuggestionVectorIdsAre(testing::ElementsAre(
307 static_cast<int>(POPUP_ITEM_ID_WARNING_MESSAGE))),
308 _));
310 // This should call ShowAutofillPopup.
311 std::vector<Suggestion> autofill_item;
312 autofill_item.push_back(Suggestion());
313 autofill_item[0].frontend_id = POPUP_ITEM_ID_WARNING_MESSAGE;
314 external_delegate_->OnSuggestionsReturned(kQueryId, autofill_item);
317 // Test that the Autofill delegate doesn't try and fill a form with a
318 // negative unique id.
319 TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateInvalidUniqueId) {
320 // Ensure it doesn't try to preview the negative id.
321 EXPECT_CALL(*autofill_manager_, FillOrPreviewForm(_, _, _, _, _)).Times(0);
322 EXPECT_CALL(*autofill_driver_, RendererShouldClearPreviewedForm()).Times(1);
323 external_delegate_->DidSelectSuggestion(base::string16(), -1);
325 // Ensure it doesn't try to fill the form in with the negative id.
326 EXPECT_CALL(autofill_client_, HideAutofillPopup());
327 EXPECT_CALL(*autofill_manager_, FillOrPreviewForm(_, _, _, _, _)).Times(0);
328 external_delegate_->DidAcceptSuggestion(base::string16(), -1);
331 // Test that the ClearPreview call is only sent if the form was being previewed
332 // (i.e. it isn't autofilling a password).
333 TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateClearPreviewedForm) {
334 // Ensure selecting a new password entries or Autofill entries will
335 // cause any previews to get cleared.
336 EXPECT_CALL(*autofill_driver_, RendererShouldClearPreviewedForm()).Times(1);
337 external_delegate_->DidSelectSuggestion(ASCIIToUTF16("baz foo"),
338 POPUP_ITEM_ID_PASSWORD_ENTRY);
339 EXPECT_CALL(*autofill_driver_, RendererShouldClearPreviewedForm()).Times(1);
340 EXPECT_CALL(*autofill_manager_,
341 FillOrPreviewForm(
342 AutofillDriver::FORM_DATA_ACTION_PREVIEW, _, _, _, _));
343 external_delegate_->DidSelectSuggestion(ASCIIToUTF16("baz foo"), 1);
345 // Ensure selecting an autocomplete entry will cause any previews to
346 // get cleared.
347 EXPECT_CALL(*autofill_driver_, RendererShouldClearPreviewedForm()).Times(1);
348 EXPECT_CALL(*autofill_driver_, RendererShouldPreviewFieldWithValue(
349 ASCIIToUTF16("baz foo")));
350 external_delegate_->DidSelectSuggestion(ASCIIToUTF16("baz foo"),
351 POPUP_ITEM_ID_AUTOCOMPLETE_ENTRY);
354 // Test that the popup is hidden once we are done editing the autofill field.
355 TEST_F(AutofillExternalDelegateUnitTest,
356 ExternalDelegateHidePopupAfterEditing) {
357 EXPECT_CALL(autofill_client_, ShowAutofillPopup(_, _, _, _));
358 autofill::GenerateTestAutofillPopup(external_delegate_.get());
360 EXPECT_CALL(autofill_client_, HideAutofillPopup());
361 external_delegate_->DidEndTextFieldEditing();
364 // Test that the driver is directed to accept the data list after being notified
365 // that the user accepted the data list suggestion.
366 TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateAcceptSuggestion) {
367 EXPECT_CALL(autofill_client_, HideAutofillPopup());
368 base::string16 dummy_string(ASCIIToUTF16("baz qux"));
369 EXPECT_CALL(*autofill_driver_,
370 RendererShouldAcceptDataListSuggestion(dummy_string));
371 external_delegate_->DidAcceptSuggestion(dummy_string,
372 POPUP_ITEM_ID_DATALIST_ENTRY);
375 // Test that the driver is directed to clear the form after being notified that
376 // the user accepted the suggestion to clear the form.
377 TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateClearForm) {
378 EXPECT_CALL(autofill_client_, HideAutofillPopup());
379 EXPECT_CALL(*autofill_driver_, RendererShouldClearFilledForm());
381 external_delegate_->DidAcceptSuggestion(base::string16(),
382 POPUP_ITEM_ID_CLEAR_FORM);
385 // Test that autofill client will scan a credit card after use accepted the
386 // suggestion to scan a credit card.
387 TEST_F(AutofillExternalDelegateUnitTest, ScanCreditCardMenuItem) {
388 EXPECT_CALL(autofill_client_, ScanCreditCard(_));
389 EXPECT_CALL(autofill_client_, HideAutofillPopup());
390 external_delegate_->DidAcceptSuggestion(base::string16(),
391 POPUP_ITEM_ID_SCAN_CREDIT_CARD);
394 TEST_F(AutofillExternalDelegateUnitTest, ScanCreditCardPromptMetricsTest) {
395 // Log that the scan card item was shown, although nothing was selected.
397 EXPECT_CALL(*autofill_manager_, ShouldShowScanCreditCard(_, _))
398 .WillOnce(testing::Return(true));
399 base::HistogramTester histogram;
400 IssueOnQuery(kQueryId);
401 IssueOnSuggestionsReturned();
402 external_delegate_->OnPopupHidden();
403 histogram.ExpectUniqueSample("Autofill.ScanCreditCardPrompt",
404 AutofillMetrics::SCAN_CARD_ITEM_SHOWN, 1);
406 // Log that the scan card item was selected.
408 EXPECT_CALL(*autofill_manager_, ShouldShowScanCreditCard(_, _))
409 .WillOnce(testing::Return(true));
410 base::HistogramTester histogram;
411 IssueOnQuery(kQueryId);
412 IssueOnSuggestionsReturned();
413 external_delegate_->DidAcceptSuggestion(base::string16(),
414 POPUP_ITEM_ID_SCAN_CREDIT_CARD);
415 histogram.ExpectBucketCount("Autofill.ScanCreditCardPrompt",
416 AutofillMetrics::SCAN_CARD_ITEM_SHOWN, 1);
417 histogram.ExpectBucketCount("Autofill.ScanCreditCardPrompt",
418 AutofillMetrics::SCAN_CARD_ITEM_SELECTED, 1);
419 histogram.ExpectBucketCount("Autofill.ScanCreditCardPrompt",
420 AutofillMetrics::SCAN_CARD_OTHER_ITEM_SELECTED,
423 // Log that something else was selected.
425 EXPECT_CALL(*autofill_manager_, ShouldShowScanCreditCard(_, _))
426 .WillOnce(testing::Return(true));
427 base::HistogramTester histogram;
428 IssueOnQuery(kQueryId);
429 IssueOnSuggestionsReturned();
430 external_delegate_->DidAcceptSuggestion(base::string16(),
431 POPUP_ITEM_ID_CLEAR_FORM);
432 histogram.ExpectBucketCount("Autofill.ScanCreditCardPrompt",
433 AutofillMetrics::SCAN_CARD_ITEM_SHOWN, 1);
434 histogram.ExpectBucketCount("Autofill.ScanCreditCardPrompt",
435 AutofillMetrics::SCAN_CARD_ITEM_SELECTED, 0);
436 histogram.ExpectBucketCount("Autofill.ScanCreditCardPrompt",
437 AutofillMetrics::SCAN_CARD_OTHER_ITEM_SELECTED,
440 // Nothing is logged when the item isn't shown.
442 EXPECT_CALL(*autofill_manager_, ShouldShowScanCreditCard(_, _))
443 .WillOnce(testing::Return(false));
444 base::HistogramTester histogram;
445 IssueOnQuery(kQueryId);
446 IssueOnSuggestionsReturned();
447 external_delegate_->DidAcceptSuggestion(base::string16(),
448 POPUP_ITEM_ID_CLEAR_FORM);
449 histogram.ExpectTotalCount("Autofill.ScanCreditCardPrompt", 0);
453 // Test that autofill manager will fill the credit card form after user scans a
454 // credit card.
455 TEST_F(AutofillExternalDelegateUnitTest, FillCreditCardForm) {
456 base::string16 card_number = base::ASCIIToUTF16("test");
457 int expiration_month = 1;
458 int expiration_year = 3000;
459 EXPECT_CALL(
460 *autofill_manager_,
461 FillCreditCardForm(
462 _, _, _, CreditCard(card_number, expiration_month, expiration_year)));
463 external_delegate_->OnCreditCardScanned(card_number, expiration_month,
464 expiration_year);
467 TEST_F(AutofillExternalDelegateUnitTest, IgnoreAutocompleteOffForAutofill) {
468 const FormData form;
469 FormFieldData field;
470 field.is_focusable = true;
471 field.should_autocomplete = false;
472 const gfx::RectF element_bounds;
474 external_delegate_->OnQuery(kQueryId, form, field, element_bounds);
476 std::vector<Suggestion> autofill_items;
477 autofill_items.push_back(Suggestion());
478 autofill_items[0].frontend_id = POPUP_ITEM_ID_AUTOCOMPLETE_ENTRY;
480 // Ensure the popup tries to show itself, despite autocomplete="off".
481 EXPECT_CALL(autofill_client_, ShowAutofillPopup(_, _, _, _));
482 EXPECT_CALL(autofill_client_, HideAutofillPopup()).Times(0);
484 external_delegate_->OnSuggestionsReturned(kQueryId, autofill_items);
487 TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateFillFieldWithValue) {
488 EXPECT_CALL(autofill_client_, HideAutofillPopup());
489 base::string16 dummy_string(ASCIIToUTF16("baz foo"));
490 EXPECT_CALL(*autofill_driver_,
491 RendererShouldFillFieldWithValue(dummy_string));
492 external_delegate_->DidAcceptSuggestion(dummy_string,
493 POPUP_ITEM_ID_AUTOCOMPLETE_ENTRY);
496 } // namespace autofill