1 // Copyright 2017 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/payments/credit_card_save_manager.h"
6
7 #include <stddef.h>
8
9 #include <algorithm>
10 #include <list>
11 #include <map>
12 #include <memory>
13 #include <string>
14 #include <utility>
15 #include <vector>
16
17 #include "base/guid.h"
18 #include "base/metrics/metrics_hashes.h"
19 #include "base/strings/string16.h"
20 #include "base/strings/string_number_conversions.h"
21 #include "base/strings/utf_string_conversions.h"
22 #include "base/test/metrics/histogram_tester.h"
23 #include "base/test/scoped_feature_list.h"
24 #include "base/test/task_environment.h"
25 #include "base/threading/thread_task_runner_handle.h"
26 #include "base/time/time.h"
27 #include "build/build_config.h"
28 #include "components/autofill/core/browser/autofill_experiments.h"
29 #include "components/autofill/core/browser/autofill_metrics.h"
30 #include "components/autofill/core/browser/autofill_test_utils.h"
31 #include "components/autofill/core/browser/data_model/autofill_profile.h"
32 #include "components/autofill/core/browser/data_model/credit_card.h"
33 #include "components/autofill/core/browser/mock_autocomplete_history_manager.h"
34 #include "components/autofill/core/browser/pattern_provider/test_pattern_provider.h"
35 #include "components/autofill/core/browser/payments/payments_customer_data.h"
36 #include "components/autofill/core/browser/payments/test_credit_card_save_manager.h"
37 #include "components/autofill/core/browser/payments/test_credit_card_save_strike_database.h"
38 #include "components/autofill/core/browser/payments/test_payments_client.h"
39 #include "components/autofill/core/browser/personal_data_manager.h"
40 #include "components/autofill/core/browser/test_autofill_client.h"
41 #include "components/autofill/core/browser/test_autofill_clock.h"
42 #include "components/autofill/core/browser/test_autofill_driver.h"
43 #include "components/autofill/core/browser/test_autofill_manager.h"
44 #include "components/autofill/core/browser/test_form_data_importer.h"
45 #include "components/autofill/core/browser/test_personal_data_manager.h"
46 #include "components/autofill/core/browser/validation.h"
47 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
48 #include "components/autofill/core/common/autofill_clock.h"
49 #include "components/autofill/core/common/autofill_features.h"
50 #include "components/autofill/core/common/autofill_payments_features.h"
51 #include "components/autofill/core/common/autofill_prefs.h"
52 #include "components/autofill/core/common/form_data.h"
53 #include "components/autofill/core/common/form_field_data.h"
54 #include "components/infobars/core/infobar_feature.h"
55 #include "components/prefs/pref_service.h"
56 #include "components/sync/driver/test_sync_service.h"
57 #include "components/ukm/test_ukm_recorder.h"
58 #include "services/metrics/public/cpp/ukm_builders.h"
59 #include "services/metrics/public/cpp/ukm_source.h"
60 #include "services/network/public/cpp/shared_url_loader_factory.h"
61 #include "testing/gmock/include/gmock/gmock.h"
62 #include "testing/gtest/include/gtest/gtest.h"
63 #include "url/gurl.h"
64
65 using base::ASCIIToUTF16;
66 using testing::_;
67 using testing::AtLeast;
68 using testing::Return;
69 using testing::SaveArg;
70 using testing::UnorderedElementsAre;
71
72 namespace autofill {
73 namespace {
74
75 using UkmCardUploadDecisionType = ukm::builders::Autofill_CardUploadDecision;
76 using UkmDeveloperEngagementType = ukm::builders::Autofill_DeveloperEngagement;
77
78 // time_t representation of 9th Sep, 2001 01:46:40 GMT
79 const base::Time kArbitraryTime = base::Time::FromTimeT(1000000000);
80 const base::Time kMuchLaterTime = base::Time::FromTimeT(1234567890);
81
82 // Used to configure form for |CreateTestCreditCardFormData|.
83 struct CreditCardFormOptions {
with_is_httpsautofill::__anondc3b9be30111::CreditCardFormOptions84 CreditCardFormOptions& with_is_https(bool b) {
85 is_https = b;
86 return *this;
87 }
88
with_split_namesautofill::__anondc3b9be30111::CreditCardFormOptions89 CreditCardFormOptions& with_split_names(bool b) {
90 split_names = b;
91 return *this;
92 }
93
with_is_from_non_focusable_formautofill::__anondc3b9be30111::CreditCardFormOptions94 CreditCardFormOptions& with_is_from_non_focusable_form(bool b) {
95 is_from_non_focusable_form = b;
96 return *this;
97 }
98
with_is_google_hostautofill::__anondc3b9be30111::CreditCardFormOptions99 CreditCardFormOptions& with_is_google_host(bool b) {
100 is_google_host = b;
101 return *this;
102 }
103 // True if the scheme of a form is https.
104 bool is_https = true;
105 // True if the form is using both first name and last name field.
106 bool split_names = false;
107 // True if the form is a non-focusable form, such as a form that is hidden
108 // after information has been entered into it.
109 bool is_from_non_focusable_form = false;
110 // True if the form is from Google-hosted website, such as payments.google.com
111 // or YouTube.
112 bool is_google_host = false;
113 };
114
115 } // anonymous namespace
116
117 class MockPersonalDataManager : public TestPersonalDataManager {
118 public:
MockPersonalDataManager()119 MockPersonalDataManager() {}
~MockPersonalDataManager()120 ~MockPersonalDataManager() override {}
121 MOCK_METHOD0(OnUserAcceptedUpstreamOffer, void());
122 };
123
124 class CreditCardSaveManagerTest : public testing::Test {
125 public:
SetUp()126 void SetUp() override {
127 autofill_client_.SetPrefs(test::PrefServiceForTesting());
128 std::unique_ptr<TestStrikeDatabase> test_strike_database =
129 std::make_unique<TestStrikeDatabase>();
130 strike_database_ = test_strike_database.get();
131 autofill_client_.set_test_strike_database(std::move(test_strike_database));
132 personal_data_.Init(/*profile_database=*/database_,
133 /*account_database=*/nullptr,
134 /*pref_service=*/autofill_client_.GetPrefs(),
135 /*identity_manager=*/nullptr,
136 /*client_profile_validator=*/nullptr,
137 /*history_service=*/nullptr,
138 /*is_off_the_record=*/false);
139 personal_data_.SetSyncServiceForTest(&sync_service_);
140 autocomplete_history_manager_.Init(
141 /*profile_database=*/database_,
142 /*is_off_the_record=*/false);
143 autofill_driver_.reset(new TestAutofillDriver());
144 payments_client_ = new payments::TestPaymentsClient(
145 autofill_driver_->GetURLLoaderFactory(),
146 autofill_client_.GetIdentityManager(), &personal_data_);
147 autofill_client_.set_test_payments_client(
148 std::unique_ptr<payments::TestPaymentsClient>(payments_client_));
149 credit_card_save_manager_ =
150 new TestCreditCardSaveManager(autofill_driver_.get(), &autofill_client_,
151 payments_client_, &personal_data_);
152 credit_card_save_manager_->SetCreditCardUploadEnabled(true);
153 autofill::TestFormDataImporter* test_form_data_importer =
154 new TestFormDataImporter(
155 &autofill_client_, payments_client_,
156 std::unique_ptr<CreditCardSaveManager>(credit_card_save_manager_),
157 &personal_data_, "en-US");
158 autofill_client_.set_test_form_data_importer(
159 std::unique_ptr<TestFormDataImporter>(test_form_data_importer));
160 autofill_client_.GetStrikeDatabase();
161 autofill_manager_.reset(new TestAutofillManager(
162 autofill_driver_.get(), &autofill_client_, &personal_data_,
163 &autocomplete_history_manager_));
164 autofill_manager_->SetExpectedObservedSubmission(true);
165 }
166
TearDown()167 void TearDown() override {
168 // Order of destruction is important as AutofillManager relies on
169 // PersonalDataManager to be around when it gets destroyed.
170 autofill_manager_.reset();
171 autofill_driver_.reset();
172
173 personal_data_.SetPrefService(nullptr);
174 personal_data_.ClearCreditCards();
175 }
176
FormsSeen(const std::vector<FormData> & forms)177 void FormsSeen(const std::vector<FormData>& forms) {
178 autofill_manager_->OnFormsSeen(forms, base::TimeTicks());
179 }
180
FormSubmitted(const FormData & form)181 void FormSubmitted(const FormData& form) {
182 autofill_manager_->OnFormSubmitted(
183 form, false, mojom::SubmissionSource::FORM_SUBMISSION);
184 }
185
UserHasAcceptedUpload(AutofillClient::UserProvidedCardDetails user_provided_card_details)186 void UserHasAcceptedUpload(
187 AutofillClient::UserProvidedCardDetails user_provided_card_details) {
188 credit_card_save_manager_->OnUserDidDecideOnUploadSave(
189 AutofillClient::ACCEPTED, user_provided_card_details);
190 }
191
192 // Populates |form| with data corresponding to a simple credit card form.
193 // Note that this actually appends fields to the form data, which can be
194 // useful for building up more complex test forms. The |form| can be
195 // configured using the provided |options|.
CreateTestCreditCardFormData(FormData * form,CreditCardFormOptions options)196 void CreateTestCreditCardFormData(FormData* form,
197 CreditCardFormOptions options) {
198 form->name = ASCIIToUTF16("MyForm");
199 base::string16 scheme =
200 options.is_https ? ASCIIToUTF16("https://") : ASCIIToUTF16("http://");
201 base::string16 host = options.is_google_host
202 ? ASCIIToUTF16("pay.google.com")
203 : ASCIIToUTF16("myform.com");
204 base::string16 root_host = options.is_google_host
205 ? ASCIIToUTF16("pay.google.com")
206 : ASCIIToUTF16("myform.root.com");
207 base::string16 form_path = ASCIIToUTF16("/form.html");
208 base::string16 submit_path = ASCIIToUTF16("/submit.html");
209 form->url = GURL(scheme + host + form_path);
210 form->action = GURL(scheme + host + submit_path);
211 form->main_frame_origin =
212 url::Origin::Create(GURL(scheme + root_host + form_path));
213
214 FormFieldData field;
215 if (options.split_names) {
216 test::CreateTestFormField("First Name on Card", "firstnameoncard", "",
217 "text", &field);
218 field.autocomplete_attribute = "cc-given-name";
219 form->fields.push_back(field);
220 test::CreateTestFormField("Last Name on Card", "lastnameoncard", "",
221 "text", &field);
222 field.autocomplete_attribute = "cc-family-name";
223 form->fields.push_back(field);
224 field.autocomplete_attribute = "";
225 } else {
226 test::CreateTestFormField("Name on Card", "nameoncard", "", "text",
227 &field);
228 form->fields.push_back(field);
229 }
230 test::CreateTestFormField("Card Number", "cardnumber", "", "text", &field);
231 field.is_focusable = !options.is_from_non_focusable_form;
232 form->fields.push_back(field);
233 test::CreateTestFormField("Expiration Date", "ccmonth", "", "text", &field);
234 form->fields.push_back(field);
235 test::CreateTestFormField("", "ccyear", "", "text", &field);
236 form->fields.push_back(field);
237 test::CreateTestFormField("CVC", "cvc", "", "text", &field);
238 form->fields.push_back(field);
239 }
240
241 // Fills the fields in |form| with test data.
ManuallyFillAddressForm(const char * first_name,const char * last_name,const char * zip_code,const char * country,FormData * form)242 void ManuallyFillAddressForm(const char* first_name,
243 const char* last_name,
244 const char* zip_code,
245 const char* country,
246 FormData* form) {
247 for (FormFieldData& field : form->fields) {
248 if (base::EqualsASCII(field.name, "firstname"))
249 field.value = ASCIIToUTF16(first_name);
250 else if (base::EqualsASCII(field.name, "lastname"))
251 field.value = ASCIIToUTF16(last_name);
252 else if (base::EqualsASCII(field.name, "addr1"))
253 field.value = ASCIIToUTF16("123 Maple");
254 else if (base::EqualsASCII(field.name, "city"))
255 field.value = ASCIIToUTF16("Dallas");
256 else if (base::EqualsASCII(field.name, "state"))
257 field.value = ASCIIToUTF16("Texas");
258 else if (base::EqualsASCII(field.name, "zipcode"))
259 field.value = ASCIIToUTF16(zip_code);
260 else if (base::EqualsASCII(field.name, "country"))
261 field.value = ASCIIToUTF16(country);
262 }
263 }
264
265 // Tests if credit card data gets saved.
TestSaveCreditCards(bool is_https)266 void TestSaveCreditCards(bool is_https) {
267 // Set up our form data.
268 FormData form;
269 CreateTestCreditCardFormData(
270 &form, CreditCardFormOptions().with_is_https(is_https));
271 std::vector<FormData> forms(1, form);
272 FormsSeen(forms);
273
274 // Edit the data, and submit.
275 form.fields[1].value = ASCIIToUTF16("4111111111111111");
276 form.fields[2].value = ASCIIToUTF16(test::NextMonth());
277 form.fields[3].value = ASCIIToUTF16(test::NextYear());
278 FormSubmitted(form);
279 EXPECT_TRUE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
280 }
281
ExpectUniqueFillableFormParsedUkm()282 void ExpectUniqueFillableFormParsedUkm() {
283 ukm::TestUkmRecorder* test_ukm_recorder =
284 autofill_client_.GetTestUkmRecorder();
285 auto entries = test_ukm_recorder->GetEntriesByName(
286 UkmDeveloperEngagementType::kEntryName);
287 EXPECT_EQ(1u, entries.size());
288 for (const auto* const entry : entries) {
289 test_ukm_recorder->ExpectEntryMetric(
290 entry, UkmDeveloperEngagementType::kDeveloperEngagementName,
291 1 << AutofillMetrics::FILLABLE_FORM_PARSED_WITHOUT_TYPE_HINTS);
292 }
293 }
294
ExpectUniqueCardUploadDecision(const base::HistogramTester & histogram_tester,AutofillMetrics::CardUploadDecisionMetric metric)295 void ExpectUniqueCardUploadDecision(
296 const base::HistogramTester& histogram_tester,
297 AutofillMetrics::CardUploadDecisionMetric metric) {
298 histogram_tester.ExpectUniqueSample("Autofill.CardUploadDecisionMetric",
299 ToHistogramSample(metric), 1);
300 }
301
ExpectCardUploadDecision(const base::HistogramTester & histogram_tester,AutofillMetrics::CardUploadDecisionMetric metric)302 void ExpectCardUploadDecision(
303 const base::HistogramTester& histogram_tester,
304 AutofillMetrics::CardUploadDecisionMetric metric) {
305 histogram_tester.ExpectBucketCount("Autofill.CardUploadDecisionMetric",
306 ToHistogramSample(metric), 1);
307 }
308
ExpectNoCardUploadDecision(const base::HistogramTester & histogram_tester,AutofillMetrics::CardUploadDecisionMetric metric)309 void ExpectNoCardUploadDecision(
310 const base::HistogramTester& histogram_tester,
311 AutofillMetrics::CardUploadDecisionMetric metric) {
312 histogram_tester.ExpectBucketCount("Autofill.CardUploadDecisionMetric",
313 ToHistogramSample(metric), 0);
314 }
315
ExpectCardUploadDecisionUkm(int expected_metric_value)316 void ExpectCardUploadDecisionUkm(int expected_metric_value) {
317 ExpectMetric(UkmCardUploadDecisionType::kUploadDecisionName,
318 UkmCardUploadDecisionType::kEntryName, expected_metric_value,
319 1 /* expected_num_matching_entries */);
320 }
321
ExpectFillableFormParsedUkm(int num_fillable_forms_parsed)322 void ExpectFillableFormParsedUkm(int num_fillable_forms_parsed) {
323 ExpectMetric(UkmDeveloperEngagementType::kDeveloperEngagementName,
324 UkmDeveloperEngagementType::kEntryName,
325 1 << AutofillMetrics::FILLABLE_FORM_PARSED_WITHOUT_TYPE_HINTS,
326 num_fillable_forms_parsed);
327 }
328
ExpectMetric(const char * metric_name,const char * entry_name,int expected_metric_value,size_t expected_num_matching_entries)329 void ExpectMetric(const char* metric_name,
330 const char* entry_name,
331 int expected_metric_value,
332 size_t expected_num_matching_entries) {
333 ukm::TestUkmRecorder* test_ukm_recorder =
334 autofill_client_.GetTestUkmRecorder();
335 auto entries = test_ukm_recorder->GetEntriesByName(entry_name);
336 EXPECT_EQ(expected_num_matching_entries, entries.size());
337 for (const auto* const entry : entries) {
338 test_ukm_recorder->ExpectEntryMetric(entry, metric_name,
339 expected_metric_value);
340 }
341 }
342
343 protected:
344 base::test::TaskEnvironment task_environment_;
345 TestAutofillClient autofill_client_;
346 std::unique_ptr<TestAutofillDriver> autofill_driver_;
347 std::unique_ptr<TestAutofillManager> autofill_manager_;
348 scoped_refptr<AutofillWebDataService> database_;
349 MockPersonalDataManager personal_data_;
350 MockAutocompleteHistoryManager autocomplete_history_manager_;
351 syncer::TestSyncService sync_service_;
352 base::test::ScopedFeatureList scoped_feature_list_;
353 TestPatternProvider test_pattern_provider_;
354 // Ends up getting owned (and destroyed) by TestFormDataImporter:
355 TestCreditCardSaveManager* credit_card_save_manager_;
356 // Ends up getting owned (and destroyed) by TestAutofillClient:
357 payments::TestPaymentsClient* payments_client_;
358 // Ends up getting owned (and destroyed) by TestAutofillClient:
359 TestStrikeDatabase* strike_database_;
360
361 private:
ToHistogramSample(AutofillMetrics::CardUploadDecisionMetric metric)362 int ToHistogramSample(AutofillMetrics::CardUploadDecisionMetric metric) {
363 for (int sample = 0; sample < metric + 1; ++sample)
364 if (metric & (1 << sample))
365 return sample;
366
367 NOTREACHED();
368 return 0;
369 }
370 };
371
372 // Tests that credit card data are saved for forms on https
373 // TODO(crbug.com/666704): Flaky on android_n5x_swarming_rel bot.
374 #if defined(OS_ANDROID)
375 #define MAYBE_ImportFormDataCreditCardHTTPS \
376 DISABLED_ImportFormDataCreditCardHTTPS
377 #else
378 #define MAYBE_ImportFormDataCreditCardHTTPS ImportFormDataCreditCardHTTPS
379 #endif
TEST_F(CreditCardSaveManagerTest,MAYBE_ImportFormDataCreditCardHTTPS)380 TEST_F(CreditCardSaveManagerTest, MAYBE_ImportFormDataCreditCardHTTPS) {
381 credit_card_save_manager_->SetCreditCardUploadEnabled(false);
382 TestSaveCreditCards(true);
383 }
384
385 // Tests that credit card data are saved for forms on http
386 // TODO(crbug.com/666704): Flaky on android_n5x_swarming_rel bot.
387 #if defined(OS_ANDROID)
388 #define MAYBE_ImportFormDataCreditCardHTTP DISABLED_ImportFormDataCreditCardHTTP
389 #else
390 #define MAYBE_ImportFormDataCreditCardHTTP ImportFormDataCreditCardHTTP
391 #endif
TEST_F(CreditCardSaveManagerTest,MAYBE_ImportFormDataCreditCardHTTP)392 TEST_F(CreditCardSaveManagerTest, MAYBE_ImportFormDataCreditCardHTTP) {
393 credit_card_save_manager_->SetCreditCardUploadEnabled(false);
394 TestSaveCreditCards(false);
395 }
396
397 // Tests that credit card data are saved when autocomplete=off for CC field.
398 // TODO(crbug.com/666704): Flaky on android_n5x_swarming_rel bot.
399 #if defined(OS_ANDROID)
400 #define MAYBE_CreditCardSavedWhenAutocompleteOff \
401 DISABLED_CreditCardSavedWhenAutocompleteOff
402 #else
403 #define MAYBE_CreditCardSavedWhenAutocompleteOff \
404 CreditCardSavedWhenAutocompleteOff
405 #endif
TEST_F(CreditCardSaveManagerTest,MAYBE_CreditCardSavedWhenAutocompleteOff)406 TEST_F(CreditCardSaveManagerTest, MAYBE_CreditCardSavedWhenAutocompleteOff) {
407 credit_card_save_manager_->SetCreditCardUploadEnabled(false);
408
409 // Set up our form data.
410 FormData form;
411 CreateTestCreditCardFormData(&form,
412 CreditCardFormOptions().with_is_https(false));
413
414 // Set "autocomplete=off" for cardnumber field.
415 form.fields[1].should_autocomplete = false;
416
417 std::vector<FormData> forms(1, form);
418 FormsSeen(forms);
419
420 // Edit the data, and submit.
421 form.fields[1].value = ASCIIToUTF16("4111111111111111");
422 form.fields[2].value = ASCIIToUTF16(test::NextMonth());
423 form.fields[3].value = ASCIIToUTF16(test::NextYear());
424 FormSubmitted(form);
425 EXPECT_TRUE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
426 }
427
428 // Tests that credit card data are not saved when CC number does not pass the
429 // Luhn test.
TEST_F(CreditCardSaveManagerTest,InvalidCreditCardNumberIsNotSaved)430 TEST_F(CreditCardSaveManagerTest, InvalidCreditCardNumberIsNotSaved) {
431 // Set up our form data.
432 FormData form;
433 CreateTestCreditCardFormData(&form, CreditCardFormOptions());
434 std::vector<FormData> forms(1, form);
435 FormsSeen(forms);
436
437 // Edit the data, and submit.
438 std::string card("4408041234567890");
439 ASSERT_FALSE(autofill::IsValidCreditCardNumber(ASCIIToUTF16(card)));
440 form.fields[1].value = ASCIIToUTF16(card);
441 form.fields[2].value = ASCIIToUTF16(test::NextMonth());
442 form.fields[3].value = ASCIIToUTF16(test::NextYear());
443 FormSubmitted(form);
444 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
445 }
446
TEST_F(CreditCardSaveManagerTest,CreditCardDisabledDoesNotSave)447 TEST_F(CreditCardSaveManagerTest, CreditCardDisabledDoesNotSave) {
448 autofill_manager_->SetAutofillCreditCardEnabled(false);
449
450 // Create, fill and submit an address form in order to establish a recent
451 // profile which can be selected for the upload request.
452 FormData address_form;
453 test::CreateTestAddressFormData(&address_form);
454 FormsSeen(std::vector<FormData>(1, address_form));
455 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
456 FormSubmitted(address_form);
457
458 // Set up our credit card form data.
459 FormData credit_card_form;
460 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
461 FormsSeen(std::vector<FormData>(1, credit_card_form));
462
463 // Edit the data, and submit.
464 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
465 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
466 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
467 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
468 credit_card_form.fields[4].value = ASCIIToUTF16("123");
469
470 base::HistogramTester histogram_tester;
471
472 // The credit card should neither be saved locally or uploaded.
473 FormSubmitted(credit_card_form);
474 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
475 EXPECT_FALSE(credit_card_save_manager_->CreditCardWasUploaded());
476
477 // Verify that no histogram entry was logged.
478 histogram_tester.ExpectTotalCount("Autofill.CardUploadDecisionMetric", 0);
479 }
480
481 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
482 // permanently if the test doesn't apply to iOS flow.
483 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_OnlyCountryInAddresses)484 TEST_F(CreditCardSaveManagerTest, UploadCreditCard_OnlyCountryInAddresses) {
485 // Create, fill and submit an address form in order to establish a recent
486 // profile which can be selected for the upload request.
487 FormData address_form;
488 test::CreateTestAddressFormData(&address_form);
489 FormsSeen(std::vector<FormData>(1, address_form));
490 ExpectUniqueFillableFormParsedUkm();
491
492 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
493 FormSubmitted(address_form);
494
495 // Set up our credit card form data.
496 FormData credit_card_form;
497 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
498 FormsSeen(std::vector<FormData>(1, credit_card_form));
499 ExpectFillableFormParsedUkm(2 /* num_fillable_forms_parsed */);
500
501 // Edit the data, and submit.
502 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
503 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
504 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
505 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
506 credit_card_form.fields[4].value = ASCIIToUTF16("123");
507
508 base::HistogramTester histogram_tester;
509
510 FormSubmitted(credit_card_form);
511 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
512 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
513 EXPECT_TRUE(payments_client_->active_experiments_in_request().empty());
514
515 // Verify that even though the full address profile was saved, only the
516 // country was included in the upload details request to payments.
517 EXPECT_EQ(1U, personal_data_.GetProfiles().size());
518 AutofillProfile only_country;
519 only_country.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"), "en-US");
520 only_country.FinalizeAfterImport();
521 EXPECT_EQ(1U, payments_client_->addresses_in_upload_details().size());
522 // AutofillProfile::Compare will ignore the difference in guid between our
523 // actual profile being sent and the expected one constructed here.
524 EXPECT_EQ(0, payments_client_->addresses_in_upload_details()[0].Compare(
525 only_country));
526
527 // Server did not send a server_id, expect copy of card is not stored.
528 EXPECT_TRUE(personal_data_.GetCreditCards().empty());
529
530 // Verify that the correct histogram entry (and only that) was logged.
531 ExpectUniqueCardUploadDecision(histogram_tester,
532 AutofillMetrics::UPLOAD_OFFERED);
533 // Verify that the correct UKM was logged.
534 ExpectCardUploadDecisionUkm(AutofillMetrics::UPLOAD_OFFERED);
535
536 // Simulate that the user has accepted the upload from the prompt.
537 UserHasAcceptedUpload({});
538 // We should find that full addresses are included in the UploadCard request,
539 // even though only countries were included in GetUploadDetails.
540 EXPECT_THAT(
541 payments_client_->addresses_in_upload_card(),
542 testing::UnorderedElementsAreArray({*personal_data_.GetProfiles()[0]}));
543 }
544 #endif
545
546 // Tests metrics for SaveCardWithFirstAndLastNameComplete for local cards.
TEST_F(CreditCardSaveManagerTest,LocalCreditCard_FirstAndLastName)547 TEST_F(CreditCardSaveManagerTest, LocalCreditCard_FirstAndLastName) {
548 credit_card_save_manager_->SetCreditCardUploadEnabled(false);
549
550 // Create, fill and submit an address form in order to establish a recent
551 // profile which can be selected for the upload request.
552 FormData address_form;
553 test::CreateTestAddressFormData(&address_form);
554 FormsSeen(std::vector<FormData>(1, address_form));
555 ExpectUniqueFillableFormParsedUkm();
556
557 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
558 FormSubmitted(address_form);
559
560 // Set up our credit card form data with credit card first and last name
561 // fields.
562 FormData credit_card_form;
563 CreateTestCreditCardFormData(&credit_card_form,
564 CreditCardFormOptions().with_split_names(true));
565 FormsSeen(std::vector<FormData>(1, credit_card_form));
566
567 // Edit the data, and submit.
568 credit_card_form.fields[0].value = ASCIIToUTF16("Flo");
569 credit_card_form.fields[1].value = ASCIIToUTF16("Master");
570 credit_card_form.fields[2].value = ASCIIToUTF16("4111111111111111");
571 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextMonth());
572 credit_card_form.fields[4].value = ASCIIToUTF16(test::NextYear());
573 credit_card_form.fields[5].value = ASCIIToUTF16("123");
574
575 base::HistogramTester histogram_tester;
576
577 FormSubmitted(credit_card_form);
578 EXPECT_TRUE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
579 EXPECT_FALSE(credit_card_save_manager_->CreditCardWasUploaded());
580
581 // Verify the histogram entry for SaveCardWithFirstAndLastNameOffered.
582 histogram_tester.ExpectTotalCount(
583 "Autofill.SaveCardWithFirstAndLastNameOffered.Local", 1);
584 histogram_tester.ExpectTotalCount(
585 "Autofill.SaveCardWithFirstAndLastNameComplete.Local", 1);
586 histogram_tester.ExpectTotalCount(
587 "Autofill.SaveCardWithFirstAndLastNameOffered.Server", 0);
588 }
589
590 // Tests metrics for SaveCardWithFirstAndLastNameComplete for local cards.
TEST_F(CreditCardSaveManagerTest,LocalCreditCard_LastAndFirstName)591 TEST_F(CreditCardSaveManagerTest, LocalCreditCard_LastAndFirstName) {
592 credit_card_save_manager_->SetCreditCardUploadEnabled(false);
593
594 // Create, fill and submit an address form in order to establish a recent
595 // profile which can be selected for the upload request.
596 FormData address_form;
597 test::CreateTestAddressFormData(&address_form);
598 FormsSeen(std::vector<FormData>(1, address_form));
599 ExpectUniqueFillableFormParsedUkm();
600
601 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
602 FormSubmitted(address_form);
603
604 // Set up our credit card form data with credit card first and last name
605 // fields.
606 FormData credit_card_form;
607 credit_card_form.name = ASCIIToUTF16("MyForm");
608 credit_card_form.url = GURL("https://myform.com/form.html");
609 credit_card_form.action = GURL("https://myform.com/submit.html");
610 credit_card_form.main_frame_origin =
611 url::Origin::Create(GURL("https://myform_root.com/form.html"));
612
613 FormFieldData field;
614 test::CreateTestFormField("Last Name on Card", "lastnameoncard", "", "text",
615 &field);
616 field.autocomplete_attribute = "cc-family-name";
617 credit_card_form.fields.push_back(field);
618 test::CreateTestFormField("First Name on Card", "firstnameoncard", "", "text",
619 &field);
620 field.autocomplete_attribute = "cc-given-name";
621 credit_card_form.fields.push_back(field);
622 field.autocomplete_attribute = "";
623 test::CreateTestFormField("Card Number", "cardnumber", "", "text", &field);
624 credit_card_form.fields.push_back(field);
625 test::CreateTestFormField("Expiration Date", "ccmonth", "", "text", &field);
626 credit_card_form.fields.push_back(field);
627 test::CreateTestFormField("", "ccyear", "", "text", &field);
628 credit_card_form.fields.push_back(field);
629 test::CreateTestFormField("CVC", "cvc", "", "text", &field);
630 credit_card_form.fields.push_back(field);
631 FormsSeen(std::vector<FormData>(1, credit_card_form));
632
633 // Edit the data, and submit.
634 credit_card_form.fields[0].value = ASCIIToUTF16("Master");
635 credit_card_form.fields[1].value = ASCIIToUTF16("Flo");
636 credit_card_form.fields[2].value = ASCIIToUTF16("4111111111111111");
637 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextMonth());
638 credit_card_form.fields[4].value = ASCIIToUTF16(test::NextYear());
639 credit_card_form.fields[5].value = ASCIIToUTF16("123");
640
641 base::HistogramTester histogram_tester;
642
643 FormSubmitted(credit_card_form);
644 EXPECT_TRUE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
645 EXPECT_FALSE(credit_card_save_manager_->CreditCardWasUploaded());
646
647 // Verify the histogram entry for SaveCardWithFirstAndLastNameOffered.
648 histogram_tester.ExpectTotalCount(
649 "Autofill.SaveCardWithFirstAndLastNameOffered.Local", 1);
650 histogram_tester.ExpectTotalCount(
651 "Autofill.SaveCardWithFirstAndLastNameComplete.Local", 1);
652 histogram_tester.ExpectTotalCount(
653 "Autofill.SaveCardWithFirstAndLastNameOffered.Server", 0);
654 }
655
656 // Tests that local save is not called when expiration date is missing.
TEST_F(CreditCardSaveManagerTest,LocalCreditCard_ExpirationDateMissing)657 TEST_F(CreditCardSaveManagerTest, LocalCreditCard_ExpirationDateMissing) {
658 credit_card_save_manager_->SetCreditCardUploadEnabled(false);
659
660 // Create, fill and submit an address form in order to establish a recent
661 // profile which can be selected for the upload request.
662 FormData address_form;
663 test::CreateTestAddressFormData(&address_form);
664 FormsSeen(std::vector<FormData>(1, address_form));
665 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
666 FormSubmitted(address_form);
667
668 // Set up our credit card form.
669 FormData credit_card_form;
670 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
671 FormsSeen(std::vector<FormData>(1, credit_card_form));
672
673 // Edit the data, but don't include a expiration date, and submit.
674 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
675 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
676 credit_card_form.fields[2].value = ASCIIToUTF16("");
677 credit_card_form.fields[3].value = ASCIIToUTF16("");
678 credit_card_form.fields[4].value = ASCIIToUTF16("123");
679 FormSubmitted(credit_card_form);
680
681 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
682 }
683
684 // Tests metrics for supporting unfocused card form.
685 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
686 // permanently if the test doesn't apply to iOS flow.
687 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_WithNonFocusableField)688 TEST_F(CreditCardSaveManagerTest, UploadCreditCard_WithNonFocusableField) {
689 credit_card_save_manager_->SetCreditCardUploadEnabled(true);
690
691 // Create, fill and submit an address form in order to establish a recent
692 // profile which can be selected for the upload request.
693 FormData address_form;
694 test::CreateTestAddressFormData(&address_form);
695 FormsSeen(std::vector<FormData>(1, address_form));
696 ExpectUniqueFillableFormParsedUkm();
697
698 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
699 FormSubmitted(address_form);
700
701 // Set up our credit card form data with non_focusable form field.
702 FormData credit_card_form;
703 CreateTestCreditCardFormData(&credit_card_form,
704 CreditCardFormOptions()
705 .with_split_names(true)
706 .with_is_from_non_focusable_form(true));
707 FormsSeen(std::vector<FormData>(1, credit_card_form));
708
709 // Edit the data, and submit.
710 credit_card_form.fields[0].value = ASCIIToUTF16("Flo");
711 credit_card_form.fields[1].value = ASCIIToUTF16("Master");
712 credit_card_form.fields[2].value = ASCIIToUTF16("4111111111111111");
713 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextMonth());
714 credit_card_form.fields[4].value = ASCIIToUTF16(test::NextYear());
715 credit_card_form.fields[5].value = ASCIIToUTF16("123");
716
717 base::HistogramTester histogram_tester;
718
719 FormSubmitted(credit_card_form);
720 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
721 // Verify that the correct histogram entries were logged.
722 ExpectCardUploadDecision(histogram_tester, AutofillMetrics::UPLOAD_OFFERED);
723 ExpectCardUploadDecision(
724 histogram_tester,
725 AutofillMetrics::UPLOAD_OFFERED_FROM_NON_FOCUSABLE_FIELD);
726 // Verify that the correct UKM was logged.
727 ExpectCardUploadDecisionUkm(
728 AutofillMetrics::UPLOAD_OFFERED |
729 AutofillMetrics::UPLOAD_OFFERED_FROM_NON_FOCUSABLE_FIELD);
730 }
731 #endif
732
733 // Tests local card save will still work as usual when supporting unfocused card
734 // form feature is already on.
TEST_F(CreditCardSaveManagerTest,LocalCreditCard_WithNonFocusableField)735 TEST_F(CreditCardSaveManagerTest, LocalCreditCard_WithNonFocusableField) {
736 credit_card_save_manager_->SetCreditCardUploadEnabled(false);
737
738 // Create, fill and submit an address form in order to establish a recent
739 // profile which can be selected for the upload request.
740 FormData address_form;
741 test::CreateTestAddressFormData(&address_form);
742 FormsSeen(std::vector<FormData>(1, address_form));
743 ExpectUniqueFillableFormParsedUkm();
744
745 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
746 FormSubmitted(address_form);
747
748 // Set up our credit card form data with non_focusable form field.
749 FormData credit_card_form;
750 CreateTestCreditCardFormData(&credit_card_form,
751 CreditCardFormOptions()
752 .with_split_names(true)
753 .with_is_from_non_focusable_form(true));
754 FormsSeen(std::vector<FormData>(1, credit_card_form));
755
756 // Edit the data, and submit.
757 credit_card_form.fields[0].value = ASCIIToUTF16("Flo");
758 credit_card_form.fields[1].value = ASCIIToUTF16("Master");
759 credit_card_form.fields[2].value = ASCIIToUTF16("4111111111111111");
760 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextMonth());
761 credit_card_form.fields[4].value = ASCIIToUTF16(test::NextYear());
762 credit_card_form.fields[5].value = ASCIIToUTF16("123");
763
764 FormSubmitted(credit_card_form);
765 EXPECT_TRUE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
766 EXPECT_FALSE(credit_card_save_manager_->CreditCardWasUploaded());
767 }
768
769 // Tests that |has_non_focusable_field| is correctly sent to AutofillClient when
770 // offering local save.
TEST_F(CreditCardSaveManagerTest,Local_UploadDisabled_SaveCreditCardOptions_WithNonFocusableField)771 TEST_F(CreditCardSaveManagerTest,
772 Local_UploadDisabled_SaveCreditCardOptions_WithNonFocusableField) {
773 credit_card_save_manager_->SetCreditCardUploadEnabled(false);
774
775 // Set up our credit card form data with non_focusable form field.
776 FormData credit_card_form;
777 CreateTestCreditCardFormData(&credit_card_form,
778 CreditCardFormOptions()
779 .with_split_names(true)
780 .with_is_from_non_focusable_form(true));
781 FormsSeen(std::vector<FormData>(1, credit_card_form));
782
783 // Edit the data, and submit.
784 credit_card_form.fields[0].value = ASCIIToUTF16("Flo");
785 credit_card_form.fields[1].value = ASCIIToUTF16("Master");
786 credit_card_form.fields[2].value = ASCIIToUTF16("4111111111111111");
787 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextMonth());
788 credit_card_form.fields[4].value = ASCIIToUTF16(test::NextYear());
789 credit_card_form.fields[5].value = ASCIIToUTF16("123");
790
791 FormSubmitted(credit_card_form);
792 EXPECT_TRUE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
793 EXPECT_FALSE(credit_card_save_manager_->CreditCardWasUploaded());
794 EXPECT_TRUE(
795 autofill_client_.get_save_credit_card_options().has_non_focusable_field);
796 }
797
798 // Tests that |has_non_focusable_field| is correctly sent to AutofillClient when
799 // upload failed because of unsupported bin and falling back to local save.
TEST_F(CreditCardSaveManagerTest,Local_UnsupportedCard_SaveCreditCardOptions_WithNonFocusableField)800 TEST_F(CreditCardSaveManagerTest,
801 Local_UnsupportedCard_SaveCreditCardOptions_WithNonFocusableField) {
802 credit_card_save_manager_->SetCreditCardUploadEnabled(true);
803 // Set supported bin range so that the used card is unsupported.
804 std::vector<std::pair<int, int>> supported_card_bin_ranges{
805 std::make_pair(34, 34), std::make_pair(300, 305)};
806 payments_client_->SetSupportedBINRanges(supported_card_bin_ranges);
807
808 // Set up our credit card form data with non_focusable form field.
809 FormData credit_card_form;
810 CreateTestCreditCardFormData(&credit_card_form,
811 CreditCardFormOptions()
812 .with_split_names(true)
813 .with_is_from_non_focusable_form(true));
814 FormsSeen(std::vector<FormData>(1, credit_card_form));
815
816 // Edit the data, and submit.
817 credit_card_form.fields[0].value = ASCIIToUTF16("Flo");
818 credit_card_form.fields[1].value = ASCIIToUTF16("Master");
819 credit_card_form.fields[2].value = ASCIIToUTF16("4111111111111111");
820 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextMonth());
821 credit_card_form.fields[4].value = ASCIIToUTF16(test::NextYear());
822 credit_card_form.fields[5].value = ASCIIToUTF16("123");
823
824 FormSubmitted(credit_card_form);
825 EXPECT_TRUE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
826 EXPECT_FALSE(credit_card_save_manager_->CreditCardWasUploaded());
827 EXPECT_TRUE(
828 autofill_client_.get_save_credit_card_options().has_non_focusable_field);
829 }
830
831 // Tests that |has_non_focusable_field| is correctly sent to AutofillClient when
832 // GetDetailsForSaveCard failed and falling back to local save.
TEST_F(CreditCardSaveManagerTest,Local_GetDetailsForSaveCardFails_SaveCreditCardOptions_WithNonFocusableField)833 TEST_F(
834 CreditCardSaveManagerTest,
835 Local_GetDetailsForSaveCardFails_SaveCreditCardOptions_WithNonFocusableField) {
836 credit_card_save_manager_->SetCreditCardUploadEnabled(true);
837 // Anything other than "en-US" will cause GetUploadDetails to return a failure
838 // response.
839 credit_card_save_manager_->SetAppLocale("pt-BR");
840
841 // Create, fill and submit an address form in order to establish a recent
842 // profile which can be selected for the upload request.
843 FormData address_form;
844 test::CreateTestAddressFormData(&address_form);
845 FormsSeen(std::vector<FormData>(1, address_form));
846 ExpectUniqueFillableFormParsedUkm();
847
848 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
849 FormSubmitted(address_form);
850
851 // Set up our credit card form data with non_focusable form field.
852 FormData credit_card_form;
853 CreateTestCreditCardFormData(&credit_card_form,
854 CreditCardFormOptions()
855 .with_split_names(true)
856 .with_is_from_non_focusable_form(true));
857 FormsSeen(std::vector<FormData>(1, credit_card_form));
858
859 // Edit the data, and submit.
860 credit_card_form.fields[0].value = ASCIIToUTF16("Flo");
861 credit_card_form.fields[1].value = ASCIIToUTF16("Master");
862 credit_card_form.fields[2].value = ASCIIToUTF16("4111111111111111");
863 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextMonth());
864 credit_card_form.fields[4].value = ASCIIToUTF16(test::NextYear());
865 credit_card_form.fields[5].value = ASCIIToUTF16("123");
866
867 FormSubmitted(credit_card_form);
868 EXPECT_TRUE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
869 EXPECT_FALSE(credit_card_save_manager_->CreditCardWasUploaded());
870 EXPECT_TRUE(
871 autofill_client_.get_save_credit_card_options().has_non_focusable_field);
872 }
873
874 // Tests that |has_non_focusable_field| is correctly sent to AutofillClient when
875 // offering upload save.
TEST_F(CreditCardSaveManagerTest,Upload_SaveCreditCardOptions_WithNonFocusableField)876 TEST_F(CreditCardSaveManagerTest,
877 Upload_SaveCreditCardOptions_WithNonFocusableField) {
878 credit_card_save_manager_->SetCreditCardUploadEnabled(true);
879
880 // Create, fill and submit an address form in order to establish a recent
881 // profile which can be selected for the upload request.
882 FormData address_form;
883 test::CreateTestAddressFormData(&address_form);
884 FormsSeen(std::vector<FormData>(1, address_form));
885 ExpectUniqueFillableFormParsedUkm();
886
887 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
888 FormSubmitted(address_form);
889
890 // Set up our credit card form data with non_focusable form field.
891 FormData credit_card_form;
892 CreateTestCreditCardFormData(&credit_card_form,
893 CreditCardFormOptions()
894 .with_split_names(true)
895 .with_is_from_non_focusable_form(true));
896 FormsSeen(std::vector<FormData>(1, credit_card_form));
897
898 // Edit the data, and submit.
899 credit_card_form.fields[0].value = ASCIIToUTF16("Flo");
900 credit_card_form.fields[1].value = ASCIIToUTF16("Master");
901 credit_card_form.fields[2].value = ASCIIToUTF16("4111111111111111");
902 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextMonth());
903 credit_card_form.fields[4].value = ASCIIToUTF16(test::NextYear());
904 credit_card_form.fields[5].value = ASCIIToUTF16("123");
905
906 FormSubmitted(credit_card_form);
907 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
908 EXPECT_TRUE(
909 autofill_client_.get_save_credit_card_options().has_non_focusable_field);
910 }
911
912 // Tests that |has_non_focusable_field| is not sent to AutofillClient when the
913 // form does not have any non-focusable fields.
TEST_F(CreditCardSaveManagerTest,SaveCreditCardOptions_WithoutNonFocusableField)914 TEST_F(CreditCardSaveManagerTest,
915 SaveCreditCardOptions_WithoutNonFocusableField) {
916 credit_card_save_manager_->SetCreditCardUploadEnabled(false);
917
918 // Set up our credit card form data without any non_focusable form field.
919 FormData credit_card_form;
920 CreateTestCreditCardFormData(&credit_card_form,
921 CreditCardFormOptions().with_split_names(true));
922 FormsSeen(std::vector<FormData>(1, credit_card_form));
923
924 // Edit the data, and submit.
925 credit_card_form.fields[0].value = ASCIIToUTF16("Flo");
926 credit_card_form.fields[1].value = ASCIIToUTF16("Master");
927 credit_card_form.fields[2].value = ASCIIToUTF16("4111111111111111");
928 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextMonth());
929 credit_card_form.fields[4].value = ASCIIToUTF16(test::NextYear());
930 credit_card_form.fields[5].value = ASCIIToUTF16("123");
931
932 FormSubmitted(credit_card_form);
933 EXPECT_TRUE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
934 EXPECT_FALSE(credit_card_save_manager_->CreditCardWasUploaded());
935 EXPECT_FALSE(
936 autofill_client_.get_save_credit_card_options().has_non_focusable_field);
937 }
938
939 // Tests that |from_dynamic_change_form| is correctly sent to AutofillClient
940 // when offering local save.
TEST_F(CreditCardSaveManagerTest,Local_UploadDisabled_SaveCreditCardOptions_WithDynamicForms)941 TEST_F(CreditCardSaveManagerTest,
942 Local_UploadDisabled_SaveCreditCardOptions_WithDynamicForms) {
943 credit_card_save_manager_->SetCreditCardUploadEnabled(false);
944
945 // Set up our credit card form data without any non_focusable form field.
946 FormData credit_card_form;
947 CreateTestCreditCardFormData(&credit_card_form,
948 CreditCardFormOptions().with_split_names(true));
949 // Use the two same forms for FormsSeen to mock the dynamic change forms.
950 FormsSeen(std::vector<FormData>(2, credit_card_form));
951
952 // Edit the data, and submit.
953 credit_card_form.fields[0].value = ASCIIToUTF16("Flo");
954 credit_card_form.fields[1].value = ASCIIToUTF16("Master");
955 credit_card_form.fields[2].value = ASCIIToUTF16("4111111111111111");
956 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextMonth());
957 credit_card_form.fields[4].value = ASCIIToUTF16(test::NextYear());
958 credit_card_form.fields[5].value = ASCIIToUTF16("123");
959
960 FormSubmitted(credit_card_form);
961 EXPECT_TRUE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
962 EXPECT_FALSE(credit_card_save_manager_->CreditCardWasUploaded());
963 EXPECT_TRUE(
964 autofill_client_.get_save_credit_card_options().from_dynamic_change_form);
965 }
966
967 // Tests that |from_dynamic_change_form| is correctly sent to AutofillClient
968 // when upload failed because of unsupported bin and falling back to local save.
TEST_F(CreditCardSaveManagerTest,Local_UnsupportedCard_SaveCreditCardOptions_WithDynamicForms)969 TEST_F(CreditCardSaveManagerTest,
970 Local_UnsupportedCard_SaveCreditCardOptions_WithDynamicForms) {
971 credit_card_save_manager_->SetCreditCardUploadEnabled(true);
972 // Set supported bin range so that the used card is unsupported.
973 std::vector<std::pair<int, int>> supported_card_bin_ranges{
974 std::make_pair(34, 34), std::make_pair(300, 305)};
975 payments_client_->SetSupportedBINRanges(supported_card_bin_ranges);
976
977 // Set up our credit card form data without any non_focusable form field.
978 FormData credit_card_form;
979 CreateTestCreditCardFormData(&credit_card_form,
980 CreditCardFormOptions().with_split_names(true));
981 // Use the two same forms for FormsSeen to mock the dynamic change forms.
982 FormsSeen(std::vector<FormData>(2, credit_card_form));
983
984 // Edit the data, and submit.
985 credit_card_form.fields[0].value = ASCIIToUTF16("Flo");
986 credit_card_form.fields[1].value = ASCIIToUTF16("Master");
987 credit_card_form.fields[2].value = ASCIIToUTF16("4111111111111111");
988 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextMonth());
989 credit_card_form.fields[4].value = ASCIIToUTF16(test::NextYear());
990 credit_card_form.fields[5].value = ASCIIToUTF16("123");
991
992 FormSubmitted(credit_card_form);
993 EXPECT_TRUE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
994 EXPECT_FALSE(credit_card_save_manager_->CreditCardWasUploaded());
995 EXPECT_TRUE(
996 autofill_client_.get_save_credit_card_options().from_dynamic_change_form);
997 }
998
999 // Tests that |from_dynamic_change_form| is correctly sent to AutofillClient
1000 // when GetDetailsForSaveCard failed and falling back to local save.
TEST_F(CreditCardSaveManagerTest,Local_GetDetailsForSaveCardFails_SaveCreditCardOptions_WithDynamicForms)1001 TEST_F(
1002 CreditCardSaveManagerTest,
1003 Local_GetDetailsForSaveCardFails_SaveCreditCardOptions_WithDynamicForms) {
1004 credit_card_save_manager_->SetCreditCardUploadEnabled(true);
1005 // Anything other than "en-US" will cause GetUploadDetails to return a failure
1006 // response.
1007 credit_card_save_manager_->SetAppLocale("pt-BR");
1008
1009 // Create, fill and submit an address form in order to establish a recent
1010 // profile which can be selected for the upload request.
1011 FormData address_form;
1012 test::CreateTestAddressFormData(&address_form);
1013 FormsSeen(std::vector<FormData>(1, address_form));
1014 ExpectUniqueFillableFormParsedUkm();
1015
1016 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
1017 FormSubmitted(address_form);
1018
1019 // Set up our credit card form data without any non_focusable form field.
1020 FormData credit_card_form;
1021 CreateTestCreditCardFormData(&credit_card_form,
1022 CreditCardFormOptions().with_split_names(true));
1023 // Use the two same forms for FormsSeen to mock the dynamic change forms.
1024 FormsSeen(std::vector<FormData>(2, credit_card_form));
1025
1026 // Edit the data, and submit.
1027 credit_card_form.fields[0].value = ASCIIToUTF16("Flo");
1028 credit_card_form.fields[1].value = ASCIIToUTF16("Master");
1029 credit_card_form.fields[2].value = ASCIIToUTF16("4111111111111111");
1030 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextMonth());
1031 credit_card_form.fields[4].value = ASCIIToUTF16(test::NextYear());
1032 credit_card_form.fields[5].value = ASCIIToUTF16("123");
1033
1034 FormSubmitted(credit_card_form);
1035 EXPECT_TRUE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
1036 EXPECT_FALSE(credit_card_save_manager_->CreditCardWasUploaded());
1037 EXPECT_TRUE(
1038 autofill_client_.get_save_credit_card_options().from_dynamic_change_form);
1039 }
1040
1041 // Tests that |from_dynamic_change_form| is correctly sent to AutofillClient
1042 // when offering upload save.
TEST_F(CreditCardSaveManagerTest,Upload_SaveCreditCardOptions_WithDynamicForms)1043 TEST_F(CreditCardSaveManagerTest,
1044 Upload_SaveCreditCardOptions_WithDynamicForms) {
1045 credit_card_save_manager_->SetCreditCardUploadEnabled(true);
1046
1047 // Create, fill and submit an address form in order to establish a recent
1048 // profile which can be selected for the upload request.
1049 FormData address_form;
1050 test::CreateTestAddressFormData(&address_form);
1051 FormsSeen(std::vector<FormData>(1, address_form));
1052 ExpectUniqueFillableFormParsedUkm();
1053
1054 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
1055 FormSubmitted(address_form);
1056
1057 // Set up our credit card form data without any non_focusable form field.
1058 FormData credit_card_form;
1059 CreateTestCreditCardFormData(&credit_card_form,
1060 CreditCardFormOptions().with_split_names(true));
1061 // Use the two same forms for FormsSeen to mock the dynamic change forms.
1062 FormsSeen(std::vector<FormData>(2, credit_card_form));
1063
1064 // Edit the data, and submit.
1065 credit_card_form.fields[0].value = ASCIIToUTF16("Flo");
1066 credit_card_form.fields[1].value = ASCIIToUTF16("Master");
1067 credit_card_form.fields[2].value = ASCIIToUTF16("4111111111111111");
1068 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextMonth());
1069 credit_card_form.fields[4].value = ASCIIToUTF16(test::NextYear());
1070 credit_card_form.fields[5].value = ASCIIToUTF16("123");
1071
1072 FormSubmitted(credit_card_form);
1073 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
1074 EXPECT_TRUE(
1075 autofill_client_.get_save_credit_card_options().from_dynamic_change_form);
1076 }
1077
1078 // Tests that |from_dynamic_change_form| is not sent to AutofillClient when the
1079 // form is not dynamically changing.
TEST_F(CreditCardSaveManagerTest,SaveCreditCardOptions_WithoutDynamicForms)1080 TEST_F(CreditCardSaveManagerTest, SaveCreditCardOptions_WithoutDynamicForms) {
1081 credit_card_save_manager_->SetCreditCardUploadEnabled(false);
1082
1083 // Set up our credit card form data without any non_focusable form field.
1084 FormData credit_card_form;
1085 CreateTestCreditCardFormData(&credit_card_form,
1086 CreditCardFormOptions().with_split_names(true));
1087 // Only using one form for FormsSeen will not be treated as dynamic change
1088 // form.
1089 FormsSeen(std::vector<FormData>(1, credit_card_form));
1090
1091 // Edit the data, and submit.
1092 credit_card_form.fields[0].value = ASCIIToUTF16("Flo");
1093 credit_card_form.fields[1].value = ASCIIToUTF16("Master");
1094 credit_card_form.fields[2].value = ASCIIToUTF16("4111111111111111");
1095 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextMonth());
1096 credit_card_form.fields[4].value = ASCIIToUTF16(test::NextYear());
1097 credit_card_form.fields[5].value = ASCIIToUTF16("123");
1098
1099 FormSubmitted(credit_card_form);
1100 EXPECT_TRUE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
1101 EXPECT_FALSE(credit_card_save_manager_->CreditCardWasUploaded());
1102 EXPECT_FALSE(
1103 autofill_client_.get_save_credit_card_options().from_dynamic_change_form);
1104 }
1105
1106 // Tests that a credit card inferred from a form with a credit card first and
1107 // last name can be uploaded.
1108 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
1109 // permanently if the test doesn't apply to iOS flow.
1110 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_FirstAndLastName)1111 TEST_F(CreditCardSaveManagerTest, UploadCreditCard_FirstAndLastName) {
1112 // Create, fill and submit an address form in order to establish a recent
1113 // profile which can be selected for the upload request.
1114 FormData address_form;
1115 test::CreateTestAddressFormData(&address_form);
1116 FormsSeen(std::vector<FormData>(1, address_form));
1117 ExpectUniqueFillableFormParsedUkm();
1118
1119 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
1120 FormSubmitted(address_form);
1121
1122 // Set up our credit card form data with credit card first and last name
1123 // fields.
1124 FormData credit_card_form;
1125 CreateTestCreditCardFormData(&credit_card_form,
1126 CreditCardFormOptions().with_split_names(true));
1127 FormsSeen(std::vector<FormData>(1, credit_card_form));
1128
1129 // Edit the data, and submit.
1130 credit_card_form.fields[0].value = ASCIIToUTF16("Flo");
1131 credit_card_form.fields[1].value = ASCIIToUTF16("Master");
1132 credit_card_form.fields[2].value = ASCIIToUTF16("4111111111111111");
1133 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextMonth());
1134 credit_card_form.fields[4].value = ASCIIToUTF16(test::NextYear());
1135 credit_card_form.fields[5].value = ASCIIToUTF16("123");
1136
1137 base::HistogramTester histogram_tester;
1138
1139 FormSubmitted(credit_card_form);
1140 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
1141 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
1142 EXPECT_TRUE(payments_client_->active_experiments_in_request().empty());
1143
1144 // Server did not send a server_id, expect copy of card is not stored.
1145 EXPECT_TRUE(personal_data_.GetCreditCards().empty());
1146 // Verify that the correct histogram entry (and only that) was logged.
1147 ExpectUniqueCardUploadDecision(histogram_tester,
1148 AutofillMetrics::UPLOAD_OFFERED);
1149 // Verify that the correct UKM was logged.
1150 ExpectCardUploadDecisionUkm(AutofillMetrics::UPLOAD_OFFERED);
1151
1152 histogram_tester.ExpectTotalCount(
1153 "Autofill.SaveCardWithFirstAndLastNameOffered.Local", 0);
1154 histogram_tester.ExpectTotalCount(
1155 "Autofill.SaveCardWithFirstAndLastNameOffered.Server", 1);
1156 histogram_tester.ExpectTotalCount(
1157 "Autofill.SaveCardWithFirstAndLastNameComplete.Server", 1);
1158 }
1159 #endif
1160
1161 // Tests that a credit card inferred from a form with a credit card first and
1162 // last name can be uploaded when the last name comes before first name on the
1163 // form.
1164 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
1165 // permanently if the test doesn't apply to iOS flow.
1166 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_LastAndFirstName)1167 TEST_F(CreditCardSaveManagerTest, UploadCreditCard_LastAndFirstName) {
1168 // Create, fill and submit an address form in order to establish a recent
1169 // profile which can be selected for the upload request.
1170 FormData address_form;
1171 test::CreateTestAddressFormData(&address_form);
1172 FormsSeen(std::vector<FormData>(1, address_form));
1173 ExpectUniqueFillableFormParsedUkm();
1174
1175 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
1176 FormSubmitted(address_form);
1177
1178 // Set up our credit card form data with credit card first and last name
1179 // fields.
1180 FormData credit_card_form;
1181 credit_card_form.name = ASCIIToUTF16("MyForm");
1182 credit_card_form.url = GURL("https://myform.com/form.html");
1183 credit_card_form.action = GURL("https://myform.com/submit.html");
1184 credit_card_form.main_frame_origin =
1185 url::Origin::Create(GURL("https://myform_root.com/form.html"));
1186
1187 FormFieldData field;
1188 test::CreateTestFormField("Last Name on Card", "lastnameoncard", "", "text",
1189 &field);
1190 field.autocomplete_attribute = "cc-family-name";
1191 credit_card_form.fields.push_back(field);
1192 test::CreateTestFormField("First Name on Card", "firstnameoncard", "", "text",
1193 &field);
1194 field.autocomplete_attribute = "cc-given-name";
1195 credit_card_form.fields.push_back(field);
1196 field.autocomplete_attribute = "";
1197 test::CreateTestFormField("Card Number", "cardnumber", "", "text", &field);
1198 credit_card_form.fields.push_back(field);
1199 test::CreateTestFormField("Expiration Date", "ccmonth", "", "text", &field);
1200 credit_card_form.fields.push_back(field);
1201 test::CreateTestFormField("", "ccyear", "", "text", &field);
1202 credit_card_form.fields.push_back(field);
1203 test::CreateTestFormField("CVC", "cvc", "", "text", &field);
1204 credit_card_form.fields.push_back(field);
1205 FormsSeen(std::vector<FormData>(1, credit_card_form));
1206
1207 // Edit the data, and submit.
1208 credit_card_form.fields[0].value = ASCIIToUTF16("Master");
1209 credit_card_form.fields[1].value = ASCIIToUTF16("Flo");
1210 credit_card_form.fields[2].value = ASCIIToUTF16("4111111111111111");
1211 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextMonth());
1212 credit_card_form.fields[4].value = ASCIIToUTF16(test::NextYear());
1213 credit_card_form.fields[5].value = ASCIIToUTF16("123");
1214
1215 base::HistogramTester histogram_tester;
1216
1217 FormSubmitted(credit_card_form);
1218 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
1219 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
1220 EXPECT_TRUE(payments_client_->active_experiments_in_request().empty());
1221
1222 // Server did not send a server_id, expect copy of card is not stored.
1223 EXPECT_TRUE(personal_data_.GetCreditCards().empty());
1224 // Verify that the correct histogram entry (and only that) was logged.
1225 ExpectUniqueCardUploadDecision(histogram_tester,
1226 AutofillMetrics::UPLOAD_OFFERED);
1227 // Verify that the correct UKM was logged.
1228 ExpectCardUploadDecisionUkm(AutofillMetrics::UPLOAD_OFFERED);
1229
1230 histogram_tester.ExpectTotalCount(
1231 "Autofill.SaveCardWithFirstAndLastNameOffered.Local", 0);
1232 histogram_tester.ExpectTotalCount(
1233 "Autofill.SaveCardWithFirstAndLastNameOffered.Server", 1);
1234 histogram_tester.ExpectTotalCount(
1235 "Autofill.SaveCardWithFirstAndLastNameComplete.Server", 1);
1236 }
1237 #endif
1238
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_NotSavedLocally)1239 TEST_F(CreditCardSaveManagerTest, UploadCreditCard_NotSavedLocally) {
1240 personal_data_.ClearCreditCards();
1241 personal_data_.ClearProfiles();
1242
1243 credit_card_save_manager_->SetCreditCardUploadEnabled(true);
1244
1245 const char* const server_id = "InstrumentData:1234";
1246 payments_client_->SetServerIdForCardUpload(server_id);
1247
1248 // Create, fill and submit an address form in order to establish a recent
1249 // profile which can be selected for the upload request.
1250 FormData address_form;
1251 test::CreateTestAddressFormData(&address_form);
1252 FormsSeen(std::vector<FormData>(1, address_form));
1253
1254 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
1255 FormSubmitted(address_form);
1256
1257 // Set up our credit card form data.
1258 FormData credit_card_form;
1259 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
1260 FormsSeen(std::vector<FormData>(1, credit_card_form));
1261
1262 // Edit the data, and submit.
1263 const char* const card_number = "4111111111111111";
1264 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
1265 credit_card_form.fields[1].value = ASCIIToUTF16(card_number);
1266 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
1267 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
1268 credit_card_form.fields[4].value = ASCIIToUTF16("123");
1269
1270 FormSubmitted(credit_card_form);
1271 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
1272
1273 // Don't keep a copy of the card on this device.
1274 EXPECT_TRUE(personal_data_.GetCreditCards().empty());
1275 }
1276
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_FeatureNotEnabled)1277 TEST_F(CreditCardSaveManagerTest, UploadCreditCard_FeatureNotEnabled) {
1278 credit_card_save_manager_->SetCreditCardUploadEnabled(false);
1279
1280 // Create, fill and submit an address form in order to establish a recent
1281 // profile which can be selected for the upload request.
1282 FormData address_form;
1283 test::CreateTestAddressFormData(&address_form);
1284 FormsSeen(std::vector<FormData>(1, address_form));
1285 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
1286 FormSubmitted(address_form);
1287
1288 // Set up our credit card form data.
1289 FormData credit_card_form;
1290 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
1291 FormsSeen(std::vector<FormData>(1, credit_card_form));
1292
1293 // Edit the data, and submit.
1294 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
1295 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
1296 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
1297 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
1298 credit_card_form.fields[4].value = ASCIIToUTF16("123");
1299
1300 base::HistogramTester histogram_tester;
1301
1302 // The save prompt should be shown instead of doing an upload.
1303 FormSubmitted(credit_card_form);
1304 EXPECT_TRUE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
1305 EXPECT_FALSE(credit_card_save_manager_->CreditCardWasUploaded());
1306
1307 // Verify that no histogram entry was logged.
1308 histogram_tester.ExpectTotalCount("Autofill.CardUploadDecisionMetric", 0);
1309 }
1310
1311 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
1312 // permanently if the test doesn't apply to iOS flow.
1313 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_CvcUnavailable)1314 TEST_F(CreditCardSaveManagerTest, UploadCreditCard_CvcUnavailable) {
1315 // Create, fill and submit an address form in order to establish a recent
1316 // profile which can be selected for the upload request.
1317 FormData address_form;
1318 test::CreateTestAddressFormData(&address_form);
1319 FormsSeen(std::vector<FormData>(1, address_form));
1320 ExpectUniqueFillableFormParsedUkm();
1321
1322 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
1323 FormSubmitted(address_form);
1324
1325 // Set up our credit card form data.
1326 FormData credit_card_form;
1327 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
1328 FormsSeen(std::vector<FormData>(1, credit_card_form));
1329 ExpectFillableFormParsedUkm(2 /* num_fillable_forms_parsed */);
1330
1331 // Edit the data, and submit.
1332 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
1333 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
1334 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
1335 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
1336 credit_card_form.fields[4].value = ASCIIToUTF16(""); // CVC MISSING
1337
1338 base::HistogramTester histogram_tester;
1339
1340 // With the offer-to-save decision deferred to Google Payments, Payments can
1341 // still decide to allow saving despite the missing CVC value.
1342 FormSubmitted(credit_card_form);
1343 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
1344 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
1345
1346 // Verify that the correct histogram entries were logged.
1347 ExpectCardUploadDecision(histogram_tester, AutofillMetrics::UPLOAD_OFFERED);
1348 ExpectCardUploadDecision(histogram_tester,
1349 AutofillMetrics::CVC_VALUE_NOT_FOUND);
1350 // Verify that the correct UKM was logged.
1351 ExpectCardUploadDecisionUkm(AutofillMetrics::UPLOAD_OFFERED |
1352 AutofillMetrics::CVC_VALUE_NOT_FOUND);
1353 }
1354 #endif
1355
1356 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
1357 // permanently if the test doesn't apply to iOS flow.
1358 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_CvcInvalidLength)1359 TEST_F(CreditCardSaveManagerTest, UploadCreditCard_CvcInvalidLength) {
1360 // Create, fill and submit an address form in order to establish a recent
1361 // profile which can be selected for the upload request.
1362 FormData address_form;
1363 test::CreateTestAddressFormData(&address_form);
1364 FormsSeen(std::vector<FormData>(1, address_form));
1365 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
1366 FormSubmitted(address_form);
1367
1368 // Set up our credit card form data.
1369 FormData credit_card_form;
1370 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
1371 FormsSeen(std::vector<FormData>(1, credit_card_form));
1372
1373 // Edit the data, and submit.
1374 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
1375 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
1376 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
1377 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
1378 credit_card_form.fields[4].value = ASCIIToUTF16("1234");
1379
1380 base::HistogramTester histogram_tester;
1381
1382 // With the offer-to-save decision deferred to Google Payments, Payments can
1383 // still decide to allow saving despite the invalid CVC value.
1384 FormSubmitted(credit_card_form);
1385 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
1386 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
1387
1388 // Verify that the correct histogram entries were logged.
1389 ExpectCardUploadDecision(histogram_tester, AutofillMetrics::UPLOAD_OFFERED);
1390 ExpectCardUploadDecision(histogram_tester,
1391 AutofillMetrics::INVALID_CVC_VALUE);
1392 // Verify that the correct UKM was logged.
1393 ExpectCardUploadDecisionUkm(AutofillMetrics::UPLOAD_OFFERED |
1394 AutofillMetrics::INVALID_CVC_VALUE);
1395 }
1396 #endif
1397
1398 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
1399 // permanently if the test doesn't apply to iOS flow.
1400 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_MultipleCvcFields)1401 TEST_F(CreditCardSaveManagerTest, UploadCreditCard_MultipleCvcFields) {
1402 // Create, fill and submit an address form in order to establish a recent
1403 // profile which can be selected for the upload request.
1404 FormData address_form;
1405 test::CreateTestAddressFormData(&address_form);
1406 FormsSeen(std::vector<FormData>(1, address_form));
1407 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
1408 FormSubmitted(address_form);
1409
1410 // Set up our credit card form data.
1411 FormData credit_card_form;
1412 credit_card_form.name = ASCIIToUTF16("MyForm");
1413 credit_card_form.url = GURL("https://myform.com/form.html");
1414 credit_card_form.action = GURL("https://myform.com/submit.html");
1415 credit_card_form.main_frame_origin =
1416 url::Origin::Create(GURL("http://myform_root.com/form.html"));
1417
1418 FormFieldData field;
1419 test::CreateTestFormField("Card Name", "cardname", "", "text", &field);
1420 credit_card_form.fields.push_back(field);
1421 test::CreateTestFormField("Card Number", "cardnumber", "", "text", &field);
1422 credit_card_form.fields.push_back(field);
1423 test::CreateTestFormField("Expiration Month", "ccmonth", "", "text", &field);
1424 credit_card_form.fields.push_back(field);
1425 test::CreateTestFormField("Expiration Year", "ccyear", "", "text", &field);
1426 credit_card_form.fields.push_back(field);
1427 test::CreateTestFormField("CVC (hidden)", "cvc1", "", "text", &field);
1428 credit_card_form.fields.push_back(field);
1429 test::CreateTestFormField("CVC", "cvc2", "", "text", &field);
1430 credit_card_form.fields.push_back(field);
1431
1432 FormsSeen(std::vector<FormData>(1, credit_card_form));
1433
1434 // Edit the data, and submit.
1435 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
1436 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
1437 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
1438 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
1439 credit_card_form.fields[4].value = ASCIIToUTF16(""); // CVC MISSING
1440 credit_card_form.fields[5].value = ASCIIToUTF16("123");
1441
1442 base::HistogramTester histogram_tester;
1443
1444 // A CVC value appeared in one of the two CVC fields, upload should happen.
1445 FormSubmitted(credit_card_form);
1446 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
1447 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
1448
1449 // Verify that the correct histogram entry (and only that) was logged.
1450 ExpectUniqueCardUploadDecision(histogram_tester,
1451 AutofillMetrics::UPLOAD_OFFERED);
1452 // Verify that the correct UKM was logged.
1453 ExpectCardUploadDecisionUkm(AutofillMetrics::UPLOAD_OFFERED);
1454 }
1455 #endif
1456
1457 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
1458 // permanently if the test doesn't apply to iOS flow.
1459 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_NoCvcFieldOnForm)1460 TEST_F(CreditCardSaveManagerTest, UploadCreditCard_NoCvcFieldOnForm) {
1461 // Create, fill and submit an address form in order to establish a recent
1462 // profile which can be selected for the upload request.
1463 FormData address_form;
1464 test::CreateTestAddressFormData(&address_form);
1465 FormsSeen(std::vector<FormData>(1, address_form));
1466 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
1467 FormSubmitted(address_form);
1468
1469 // Set up our credit card form data. Note that CVC field is missing.
1470 FormData credit_card_form;
1471 credit_card_form.name = ASCIIToUTF16("MyForm");
1472 credit_card_form.url = GURL("https://myform.com/form.html");
1473 credit_card_form.action = GURL("https://myform.com/submit.html");
1474 credit_card_form.main_frame_origin =
1475 url::Origin::Create(GURL("http://myform_root.com/form.html"));
1476
1477 FormFieldData field;
1478 test::CreateTestFormField("Card Name", "cardname", "", "text", &field);
1479 credit_card_form.fields.push_back(field);
1480 test::CreateTestFormField("Card Number", "cardnumber", "", "text", &field);
1481 credit_card_form.fields.push_back(field);
1482 test::CreateTestFormField("Expiration Month", "ccmonth", "", "text", &field);
1483 credit_card_form.fields.push_back(field);
1484 test::CreateTestFormField("Expiration Year", "ccyear", "", "text", &field);
1485 credit_card_form.fields.push_back(field);
1486
1487 FormsSeen(std::vector<FormData>(1, credit_card_form));
1488
1489 // Edit the data, and submit.
1490 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
1491 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
1492 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
1493 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
1494
1495 base::HistogramTester histogram_tester;
1496
1497 // With the offer-to-save decision deferred to Google Payments, Payments can
1498 // still decide to allow saving despite the missing CVC value.
1499 FormSubmitted(credit_card_form);
1500 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
1501 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
1502
1503 // Verify that the correct histogram entries were logged.
1504 ExpectCardUploadDecision(histogram_tester, AutofillMetrics::UPLOAD_OFFERED);
1505 ExpectCardUploadDecision(histogram_tester,
1506 AutofillMetrics::CVC_FIELD_NOT_FOUND);
1507 // Verify that the correct UKM was logged.
1508 ExpectCardUploadDecisionUkm(AutofillMetrics::UPLOAD_OFFERED |
1509 AutofillMetrics::CVC_FIELD_NOT_FOUND);
1510 }
1511 #endif
1512
1513 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
1514 // permanently if the test doesn't apply to iOS flow.
1515 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_NoCvcFieldOnForm_InvalidCvcInNonCvcField)1516 TEST_F(CreditCardSaveManagerTest,
1517 UploadCreditCard_NoCvcFieldOnForm_InvalidCvcInNonCvcField) {
1518 // Create, fill and submit an address form in order to establish a recent
1519 // profile which can be selected for the upload request.
1520 FormData address_form;
1521 test::CreateTestAddressFormData(&address_form);
1522 FormsSeen({address_form});
1523 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
1524 FormSubmitted(address_form);
1525
1526 // Set up our credit card form data. Note that CVC field is missing.
1527 FormData credit_card_form;
1528 credit_card_form.name = ASCIIToUTF16("MyForm");
1529 credit_card_form.url = GURL("https://myform.com/form.html");
1530 credit_card_form.action = GURL("https://myform.com/submit.html");
1531 credit_card_form.main_frame_origin =
1532 url::Origin::Create(GURL("http://myform_root.com/form.html"));
1533
1534 FormFieldData field;
1535 test::CreateTestFormField("Card Name", "cardname", "", "text", &field);
1536 credit_card_form.fields.push_back(field);
1537 test::CreateTestFormField("Card Number", "cardnumber", "", "text", &field);
1538 credit_card_form.fields.push_back(field);
1539 test::CreateTestFormField("Expiration Month", "ccmonth", "", "text", &field);
1540 credit_card_form.fields.push_back(field);
1541 test::CreateTestFormField("Expiration Year", "ccyear", "", "text", &field);
1542 credit_card_form.fields.push_back(field);
1543 test::CreateTestFormField("Random Field", "random", "", "text", &field);
1544 credit_card_form.fields.push_back(field);
1545
1546 FormsSeen({credit_card_form});
1547
1548 // Enter an invalid cvc in "Random Field" and submit.
1549 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
1550 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
1551 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
1552 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
1553 credit_card_form.fields[4].value = ASCIIToUTF16("1234");
1554
1555 base::HistogramTester histogram_tester;
1556
1557 // With the offer-to-save decision deferred to Google Payments, Payments can
1558 // still decide to allow saving despite the invalid CVC value.
1559 FormSubmitted(credit_card_form);
1560 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
1561 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
1562
1563 // Verify that the correct histogram entries were logged.
1564 ExpectCardUploadDecision(histogram_tester, AutofillMetrics::UPLOAD_OFFERED);
1565 ExpectCardUploadDecision(histogram_tester,
1566 AutofillMetrics::CVC_FIELD_NOT_FOUND);
1567 // Verify that the correct UKM was logged.
1568 ExpectCardUploadDecisionUkm(AutofillMetrics::UPLOAD_OFFERED |
1569 AutofillMetrics::CVC_FIELD_NOT_FOUND);
1570 }
1571 #endif
1572
1573 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
1574 // permanently if the test doesn't apply to iOS flow.
1575 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_NoCvcFieldOnForm_CvcInNonCvcField)1576 TEST_F(CreditCardSaveManagerTest,
1577 UploadCreditCard_NoCvcFieldOnForm_CvcInNonCvcField) {
1578 // Create, fill and submit an address form in order to establish a recent
1579 // profile which can be selected for the upload request.
1580 FormData address_form;
1581 test::CreateTestAddressFormData(&address_form);
1582 FormsSeen({address_form});
1583 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
1584 FormSubmitted(address_form);
1585
1586 // Set up our credit card form data. Note that CVC field is missing.
1587 FormData credit_card_form;
1588 credit_card_form.name = ASCIIToUTF16("MyForm");
1589 credit_card_form.url = GURL("https://myform.com/form.html");
1590 credit_card_form.action = GURL("https://myform.com/submit.html");
1591 credit_card_form.main_frame_origin =
1592 url::Origin::Create(GURL("http://myform_root.com/form.html"));
1593
1594 FormFieldData field;
1595 test::CreateTestFormField("Card Name", "cardname", "", "text", &field);
1596 credit_card_form.fields.push_back(field);
1597 test::CreateTestFormField("Card Number", "cardnumber", "", "text", &field);
1598 credit_card_form.fields.push_back(field);
1599 test::CreateTestFormField("Expiration Month", "ccmonth", "", "text", &field);
1600 credit_card_form.fields.push_back(field);
1601 test::CreateTestFormField("Expiration Year", "ccyear", "", "text", &field);
1602 credit_card_form.fields.push_back(field);
1603 test::CreateTestFormField("Random Field", "random", "", "text", &field);
1604 credit_card_form.fields.push_back(field);
1605
1606 FormsSeen({credit_card_form});
1607
1608 // Enter a valid cvc in "Random Field" and submit.
1609 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
1610 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
1611 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
1612 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
1613 credit_card_form.fields[4].value = ASCIIToUTF16("123");
1614
1615 base::HistogramTester histogram_tester;
1616
1617 // With the offer-to-save decision deferred to Google Payments, Payments can
1618 // still decide to allow saving despite the missing CVC value.
1619 FormSubmitted(credit_card_form);
1620 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
1621 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
1622
1623 // Verify that the correct histogram entries were logged.
1624 ExpectCardUploadDecision(histogram_tester, AutofillMetrics::UPLOAD_OFFERED);
1625 ExpectCardUploadDecision(
1626 histogram_tester,
1627 AutofillMetrics::FOUND_POSSIBLE_CVC_VALUE_IN_NON_CVC_FIELD);
1628 // Verify that the correct UKM was logged.
1629 ExpectCardUploadDecisionUkm(
1630 AutofillMetrics::UPLOAD_OFFERED |
1631 AutofillMetrics::FOUND_POSSIBLE_CVC_VALUE_IN_NON_CVC_FIELD);
1632 }
1633 #endif
1634
1635 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
1636 // permanently if the test doesn't apply to iOS flow.
1637 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_NoCvcFieldOnForm_CvcInAddressField)1638 TEST_F(CreditCardSaveManagerTest,
1639 UploadCreditCard_NoCvcFieldOnForm_CvcInAddressField) {
1640 // Create, fill and submit an address form in order to establish a recent
1641 // profile which can be selected for the upload request.
1642 FormData address_form;
1643 test::CreateTestAddressFormData(&address_form);
1644 FormsSeen({address_form});
1645 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
1646 FormSubmitted(address_form);
1647
1648 // Set up our credit card form data. Note that CVC field is missing.
1649 FormData credit_card_form;
1650 credit_card_form.name = ASCIIToUTF16("MyForm");
1651 credit_card_form.url = GURL("https://myform.com/form.html");
1652 credit_card_form.action = GURL("https://myform.com/submit.html");
1653 credit_card_form.main_frame_origin =
1654 url::Origin::Create(GURL("http://myform_root.com/form.html"));
1655
1656 FormFieldData field;
1657 test::CreateTestFormField("Card Name", "cardname", "", "text", &field);
1658 credit_card_form.fields.push_back(field);
1659 test::CreateTestFormField("Card Number", "cardnumber", "", "text", &field);
1660 credit_card_form.fields.push_back(field);
1661 test::CreateTestFormField("Expiration Month", "ccmonth", "", "text", &field);
1662 credit_card_form.fields.push_back(field);
1663 test::CreateTestFormField("Expiration Year", "ccyear", "", "text", &field);
1664 credit_card_form.fields.push_back(field);
1665 test::CreateTestFormField("Address Line 1", "addr1", "", "text", &field);
1666 credit_card_form.fields.push_back(field);
1667
1668 FormsSeen({credit_card_form});
1669
1670 // Enter a valid cvc in "Random Field" and submit.
1671 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
1672 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
1673 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
1674 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
1675 credit_card_form.fields[4].value = ASCIIToUTF16("123");
1676
1677 base::HistogramTester histogram_tester;
1678
1679 // With the offer-to-save decision deferred to Google Payments, Payments can
1680 // still decide to allow saving despite the missing CVC value.
1681 FormSubmitted(credit_card_form);
1682 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
1683 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
1684
1685 // Verify that the correct histogram entries were logged.
1686 ExpectCardUploadDecision(histogram_tester, AutofillMetrics::UPLOAD_OFFERED);
1687 ExpectCardUploadDecision(histogram_tester,
1688 AutofillMetrics::CVC_FIELD_NOT_FOUND);
1689 // Verify that the correct UKM was logged.
1690 ExpectCardUploadDecisionUkm(AutofillMetrics::UPLOAD_OFFERED |
1691 AutofillMetrics::CVC_FIELD_NOT_FOUND);
1692 }
1693 #endif
1694
1695 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
1696 // permanently if the test doesn't apply to iOS flow.
1697 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_NoProfileAvailable)1698 TEST_F(CreditCardSaveManagerTest, UploadCreditCard_NoProfileAvailable) {
1699 // Don't fill or submit an address form.
1700
1701 // Set up our credit card form data.
1702 FormData credit_card_form;
1703 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
1704 FormsSeen(std::vector<FormData>(1, credit_card_form));
1705
1706 // Edit the data, and submit.
1707 credit_card_form.fields[0].value = ASCIIToUTF16("Bob Master");
1708 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
1709 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
1710 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
1711 credit_card_form.fields[4].value = ASCIIToUTF16("123");
1712
1713 base::HistogramTester histogram_tester;
1714
1715 // With the offer-to-save decision deferred to Google Payments, Payments can
1716 // still decide to allow saving despite the missing name/address.
1717 FormSubmitted(credit_card_form);
1718 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
1719 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
1720
1721 // Verify that the correct histogram entries were logged.
1722 ExpectCardUploadDecision(histogram_tester, AutofillMetrics::UPLOAD_OFFERED);
1723 ExpectCardUploadDecision(
1724 histogram_tester, AutofillMetrics::UPLOAD_NOT_OFFERED_NO_ADDRESS_PROFILE);
1725 // Verify that the correct UKM was logged.
1726 ExpectCardUploadDecisionUkm(
1727 AutofillMetrics::UPLOAD_OFFERED |
1728 AutofillMetrics::UPLOAD_NOT_OFFERED_NO_ADDRESS_PROFILE);
1729 }
1730 #endif
1731
1732 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
1733 // permanently if the test doesn't apply to iOS flow.
1734 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_NoRecentlyUsedProfile)1735 TEST_F(CreditCardSaveManagerTest, UploadCreditCard_NoRecentlyUsedProfile) {
1736 // Create the test clock and set the time to a specific value.
1737 TestAutofillClock test_clock;
1738 test_clock.SetNow(kArbitraryTime);
1739
1740 // Create, fill and submit an address form in order to establish a profile.
1741 FormData address_form;
1742 test::CreateTestAddressFormData(&address_form);
1743 FormsSeen({address_form});
1744
1745 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
1746 FormSubmitted(address_form);
1747
1748 // Set the current time to another value.
1749 test_clock.SetNow(kMuchLaterTime);
1750
1751 // Set up our credit card form data.
1752 FormData credit_card_form;
1753 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
1754 FormsSeen(std::vector<FormData>(1, credit_card_form));
1755
1756 // Edit the data, and submit.
1757 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
1758 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
1759 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
1760 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
1761 credit_card_form.fields[4].value = ASCIIToUTF16("123");
1762
1763 base::HistogramTester histogram_tester;
1764
1765 // With the offer-to-save decision deferred to Google Payments, Payments can
1766 // still decide to allow saving despite the missing name/address.
1767 FormSubmitted(credit_card_form);
1768 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
1769 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
1770
1771 // Verify that the correct histogram entries were logged.
1772 ExpectCardUploadDecision(histogram_tester, AutofillMetrics::UPLOAD_OFFERED);
1773 ExpectCardUploadDecision(
1774 histogram_tester,
1775 AutofillMetrics::UPLOAD_NOT_OFFERED_NO_RECENTLY_USED_ADDRESS);
1776 // Verify that the correct UKM was logged.
1777 ExpectCardUploadDecisionUkm(
1778 AutofillMetrics::UPLOAD_OFFERED |
1779 AutofillMetrics::UPLOAD_NOT_OFFERED_NO_RECENTLY_USED_ADDRESS);
1780 }
1781 #endif
1782
1783 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
1784 // permanently if the test doesn't apply to iOS flow.
1785 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_CvcUnavailableAndNoProfileAvailable)1786 TEST_F(CreditCardSaveManagerTest,
1787 UploadCreditCard_CvcUnavailableAndNoProfileAvailable) {
1788 // Don't fill or submit an address form.
1789
1790 // Set up our credit card form data.
1791 FormData credit_card_form;
1792 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
1793 FormsSeen(std::vector<FormData>(1, credit_card_form));
1794
1795 // Edit the data, and submit.
1796 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
1797 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
1798 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
1799 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
1800 credit_card_form.fields[4].value = ASCIIToUTF16(""); // CVC MISSING
1801
1802 base::HistogramTester histogram_tester;
1803
1804 // With the offer-to-save decision deferred to Google Payments, Payments can
1805 // still decide to allow saving despite the missing CVC, name, and address.
1806 FormSubmitted(credit_card_form);
1807 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
1808 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
1809
1810 // Verify that the correct histogram entries were logged.
1811 ExpectCardUploadDecision(histogram_tester, AutofillMetrics::UPLOAD_OFFERED);
1812 ExpectCardUploadDecision(histogram_tester,
1813 AutofillMetrics::CVC_VALUE_NOT_FOUND);
1814 ExpectCardUploadDecision(
1815 histogram_tester, AutofillMetrics::UPLOAD_NOT_OFFERED_NO_ADDRESS_PROFILE);
1816 // Verify that the correct UKM was logged.
1817 ExpectCardUploadDecisionUkm(
1818 AutofillMetrics::UPLOAD_OFFERED | AutofillMetrics::CVC_VALUE_NOT_FOUND |
1819 AutofillMetrics::UPLOAD_NOT_OFFERED_NO_ADDRESS_PROFILE);
1820 }
1821 #endif
1822
1823 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
1824 // permanently if the test doesn't apply to iOS flow.
1825 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_NoNameAvailable)1826 TEST_F(CreditCardSaveManagerTest, UploadCreditCard_NoNameAvailable) {
1827 // Create, fill and submit an address form in order to establish a recent
1828 // profile which can be selected for the upload request.
1829 FormData address_form;
1830 test::CreateTestAddressFormData(&address_form);
1831 FormsSeen(std::vector<FormData>(1, address_form));
1832 // But omit the name:
1833 ManuallyFillAddressForm("", "", "77401", "US", &address_form);
1834 FormSubmitted(address_form);
1835
1836 // Set up our credit card form data.
1837 FormData credit_card_form;
1838 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
1839 FormsSeen(std::vector<FormData>(1, credit_card_form));
1840
1841 // Edit the data, but don't include a name, and submit.
1842 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
1843 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
1844 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
1845 credit_card_form.fields[4].value = ASCIIToUTF16("123");
1846
1847 base::HistogramTester histogram_tester;
1848
1849 // With the offer-to-save decision deferred to Google Payments, Payments can
1850 // still decide to allow saving despite the missing name.
1851 FormSubmitted(credit_card_form);
1852 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
1853 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
1854
1855 // Verify that the correct histogram entries were logged.
1856 ExpectCardUploadDecision(histogram_tester, AutofillMetrics::UPLOAD_OFFERED);
1857 ExpectCardUploadDecision(histogram_tester,
1858 AutofillMetrics::UPLOAD_NOT_OFFERED_NO_NAME);
1859 ExpectCardUploadDecision(
1860 histogram_tester,
1861 AutofillMetrics::USER_REQUESTED_TO_PROVIDE_CARDHOLDER_NAME);
1862 // Verify that the correct UKM was logged.
1863 ExpectCardUploadDecisionUkm(
1864 AutofillMetrics::UPLOAD_OFFERED |
1865 AutofillMetrics::UPLOAD_NOT_OFFERED_NO_NAME |
1866 AutofillMetrics::USER_REQUESTED_TO_PROVIDE_CARDHOLDER_NAME);
1867 }
1868 #endif
1869
1870 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
1871 // permanently if the test doesn't apply to iOS flow.
1872 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_NoNameAvailableAndNoProfileAvailable)1873 TEST_F(CreditCardSaveManagerTest,
1874 UploadCreditCard_NoNameAvailableAndNoProfileAvailable) {
1875 // Don't fill or submit an address form.
1876
1877 // Set up our credit card form data.
1878 FormData credit_card_form;
1879 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
1880 FormsSeen(std::vector<FormData>(1, credit_card_form));
1881
1882 // Edit the data, but don't include a name, and submit.
1883 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
1884 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
1885 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
1886 credit_card_form.fields[4].value = ASCIIToUTF16("123");
1887
1888 base::HistogramTester histogram_tester;
1889
1890 // With the offer-to-save decision deferred to Google Payments, Payments can
1891 // still decide to allow saving despite the missing names/address.
1892 FormSubmitted(credit_card_form);
1893 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
1894 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
1895
1896 // Verify that the correct histogram entries were logged.
1897 ExpectCardUploadDecision(histogram_tester, AutofillMetrics::UPLOAD_OFFERED);
1898 ExpectCardUploadDecision(
1899 histogram_tester, AutofillMetrics::UPLOAD_NOT_OFFERED_NO_ADDRESS_PROFILE);
1900 ExpectCardUploadDecision(histogram_tester,
1901 AutofillMetrics::UPLOAD_NOT_OFFERED_NO_NAME);
1902 ExpectCardUploadDecision(
1903 histogram_tester,
1904 AutofillMetrics::USER_REQUESTED_TO_PROVIDE_CARDHOLDER_NAME);
1905 // Verify that the correct UKM was logged.
1906 ExpectCardUploadDecisionUkm(
1907 AutofillMetrics::UPLOAD_OFFERED |
1908 AutofillMetrics::UPLOAD_NOT_OFFERED_NO_ADDRESS_PROFILE |
1909 AutofillMetrics::UPLOAD_NOT_OFFERED_NO_NAME |
1910 AutofillMetrics::USER_REQUESTED_TO_PROVIDE_CARDHOLDER_NAME);
1911 }
1912 #endif
1913
1914 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
1915 // permanently if the test doesn't apply to iOS flow.
1916 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_ZipCodesConflict)1917 TEST_F(CreditCardSaveManagerTest, UploadCreditCard_ZipCodesConflict) {
1918 // Create, fill and submit two address forms with different zip codes.
1919 FormData address_form1, address_form2;
1920 test::CreateTestAddressFormData(&address_form1, "1");
1921 test::CreateTestAddressFormData(&address_form2, "2");
1922
1923 std::vector<FormData> address_forms;
1924 address_forms.push_back(address_form1);
1925 address_forms.push_back(address_form2);
1926 FormsSeen(address_forms);
1927 ExpectFillableFormParsedUkm(2 /* num_fillable_forms_parsed */);
1928
1929 ManuallyFillAddressForm("Flo", "Master", "77401-8294", "US", &address_form1);
1930 FormSubmitted(address_form1);
1931
1932 ManuallyFillAddressForm("Flo", "Master", "77401-1234", "US", &address_form2);
1933 FormSubmitted(address_form2);
1934
1935 // Set up our credit card form data.
1936 FormData credit_card_form;
1937 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
1938 FormsSeen(std::vector<FormData>(1, credit_card_form));
1939 ExpectFillableFormParsedUkm(3 /* num_fillable_forms_parsed */);
1940
1941 // Edit the data and submit.
1942 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
1943 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
1944 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
1945 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
1946 credit_card_form.fields[4].value = ASCIIToUTF16("123");
1947
1948 base::HistogramTester histogram_tester;
1949
1950 // With the offer-to-save decision deferred to Google Payments, Payments can
1951 // still decide to allow saving despite the conflicting zip codes.
1952 FormSubmitted(credit_card_form);
1953 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
1954 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
1955
1956 // Verify that the correct histogram entries were logged.
1957 ExpectCardUploadDecision(histogram_tester, AutofillMetrics::UPLOAD_OFFERED);
1958 ExpectCardUploadDecision(
1959 histogram_tester, AutofillMetrics::UPLOAD_NOT_OFFERED_CONFLICTING_ZIPS);
1960 // Verify that the correct UKM was logged.
1961 ExpectCardUploadDecisionUkm(
1962 AutofillMetrics::UPLOAD_OFFERED |
1963 AutofillMetrics::UPLOAD_NOT_OFFERED_CONFLICTING_ZIPS);
1964 }
1965 #endif
1966
1967 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
1968 // permanently if the test doesn't apply to iOS flow.
1969 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_ZipCodesDoNotDiscardWhitespace)1970 TEST_F(CreditCardSaveManagerTest,
1971 UploadCreditCard_ZipCodesDoNotDiscardWhitespace) {
1972 // Create two separate profiles with different zip codes. Must directly add
1973 // instead of submitting a form, because they're deduped on form submit.
1974 AutofillProfile profile1;
1975 profile1.set_guid("00000000-0000-0000-0000-000000000001");
1976 profile1.SetInfo(NAME_FULL, ASCIIToUTF16("Flo Master"), "en-US");
1977 profile1.SetInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("H3B2Y5"), "en-US");
1978 profile1.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("CA"), "en-US");
1979 personal_data_.AddProfile(profile1);
1980
1981 AutofillProfile profile2;
1982 profile2.set_guid("00000000-0000-0000-0000-000000000002");
1983 profile2.SetInfo(NAME_FULL, ASCIIToUTF16("Flo Master"), "en-US");
1984 profile2.SetInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("h3b 2y5"), "en-US");
1985 profile2.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("CA"), "en-US");
1986 personal_data_.AddProfile(profile2);
1987
1988 // Set up our credit card form data.
1989 FormData credit_card_form;
1990 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
1991 FormsSeen({credit_card_form});
1992
1993 // Edit the data and submit.
1994 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
1995 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
1996 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
1997 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
1998 credit_card_form.fields[4].value = ASCIIToUTF16("123");
1999
2000 base::HistogramTester histogram_tester;
2001
2002 // With the offer-to-save decision deferred to Google Payments, Payments can
2003 // still decide to allow saving despite the conflicting zip codes.
2004 FormSubmitted(credit_card_form);
2005 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
2006 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
2007
2008 // Verify that the correct histogram entries were logged.
2009 ExpectCardUploadDecision(histogram_tester, AutofillMetrics::UPLOAD_OFFERED);
2010 ExpectCardUploadDecision(
2011 histogram_tester, AutofillMetrics::UPLOAD_NOT_OFFERED_CONFLICTING_ZIPS);
2012 // Verify that the correct UKM was logged.
2013 ExpectCardUploadDecisionUkm(
2014 AutofillMetrics::UPLOAD_OFFERED |
2015 AutofillMetrics::UPLOAD_NOT_OFFERED_CONFLICTING_ZIPS);
2016 }
2017 #endif
2018
2019 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
2020 // permanently if the test doesn't apply to iOS flow.
2021 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_ZipCodesHavePrefixMatch)2022 TEST_F(CreditCardSaveManagerTest, UploadCreditCard_ZipCodesHavePrefixMatch) {
2023 // Create, fill and submit two address forms with different zip codes.
2024 FormData address_form1, address_form2;
2025 test::CreateTestAddressFormData(&address_form1);
2026 test::CreateTestAddressFormData(&address_form2);
2027
2028 std::vector<FormData> address_forms;
2029 address_forms.push_back(address_form1);
2030 address_forms.push_back(address_form2);
2031 FormsSeen(address_forms);
2032
2033 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form1);
2034 FormSubmitted(address_form1);
2035
2036 ManuallyFillAddressForm("Flo", "Master", "77401-8294", "US", &address_form2);
2037 FormSubmitted(address_form2);
2038
2039 // Set up our credit card form data.
2040 FormData credit_card_form;
2041 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
2042 FormsSeen(std::vector<FormData>(1, credit_card_form));
2043
2044 // Edit the data and submit.
2045 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
2046 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
2047 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
2048 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
2049 credit_card_form.fields[4].value = ASCIIToUTF16("123");
2050
2051 base::HistogramTester histogram_tester;
2052
2053 // One zip is a prefix of the other, upload should happen.
2054 FormSubmitted(credit_card_form);
2055 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
2056 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
2057
2058 // Verify that the correct histogram entry (and only that) was logged.
2059 ExpectUniqueCardUploadDecision(histogram_tester,
2060 AutofillMetrics::UPLOAD_OFFERED);
2061 // Verify that the correct UKM was logged.
2062 ExpectCardUploadDecisionUkm(AutofillMetrics::UPLOAD_OFFERED);
2063 }
2064 #endif
2065
2066 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
2067 // permanently if the test doesn't apply to iOS flow.
2068 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_NoZipCodeAvailable)2069 TEST_F(CreditCardSaveManagerTest, UploadCreditCard_NoZipCodeAvailable) {
2070 // Create, fill and submit an address form in order to establish a recent
2071 // profile which can be selected for the upload request.
2072 FormData address_form;
2073 test::CreateTestAddressFormData(&address_form);
2074 FormsSeen(std::vector<FormData>(1, address_form));
2075 // Autofill's validation requirements for Venezuala ("VE", see
2076 // src/components/autofill/core/browser/geo/country_data.cc) do not require
2077 // zip codes. We use Venezuala here because to use the US (or one of many
2078 // other countries which autofill requires a zip code for) would result in no
2079 // address being imported at all, and then we never reach the check for
2080 // missing zip code in the upload code.
2081 ManuallyFillAddressForm("Flo", "Master", "" /* zip_code */, "Venezuela",
2082 &address_form);
2083 FormSubmitted(address_form);
2084
2085 // Set up our credit card form data.
2086 FormData credit_card_form;
2087 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
2088 FormsSeen(std::vector<FormData>(1, credit_card_form));
2089
2090 // Edit the data, and submit.
2091 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
2092 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
2093 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
2094 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
2095 credit_card_form.fields[4].value = ASCIIToUTF16("123");
2096
2097 base::HistogramTester histogram_tester;
2098
2099 // With the offer-to-save decision deferred to Google Payments, Payments can
2100 // still decide to allow saving despite the missing zip code.
2101 FormSubmitted(credit_card_form);
2102 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
2103 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
2104
2105 // Verify that the correct histogram entries were logged.
2106 ExpectCardUploadDecision(histogram_tester, AutofillMetrics::UPLOAD_OFFERED);
2107 ExpectCardUploadDecision(histogram_tester,
2108 AutofillMetrics::UPLOAD_NOT_OFFERED_NO_ZIP_CODE);
2109 // Verify that the correct UKM was logged.
2110 ExpectCardUploadDecisionUkm(AutofillMetrics::UPLOAD_OFFERED |
2111 AutofillMetrics::UPLOAD_NOT_OFFERED_NO_ZIP_CODE);
2112 }
2113 #endif
2114
2115 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
2116 // permanently if the test doesn't apply to iOS flow.
2117 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_CCFormHasMiddleInitial)2118 TEST_F(CreditCardSaveManagerTest, UploadCreditCard_CCFormHasMiddleInitial) {
2119 // Create, fill and submit two address forms with different names.
2120 FormData address_form1, address_form2;
2121 test::CreateTestAddressFormData(&address_form1);
2122 test::CreateTestAddressFormData(&address_form2);
2123 FormsSeen({address_form1, address_form2});
2124
2125 // Names can be different case.
2126 ManuallyFillAddressForm("flo", "master", "77401", "US", &address_form1);
2127 FormSubmitted(address_form1);
2128
2129 // And they can have a middle initial even if the other names don't.
2130 ManuallyFillAddressForm("Flo W", "Master", "77401", "US", &address_form2);
2131 FormSubmitted(address_form2);
2132
2133 // Set up our credit card form data.
2134 FormData credit_card_form;
2135 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
2136 FormsSeen({credit_card_form});
2137
2138 // Edit the data, but use the name with a middle initial *and* period, and
2139 // submit.
2140 credit_card_form.fields[0].value = ASCIIToUTF16("Flo W. Master");
2141 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
2142 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
2143 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
2144 credit_card_form.fields[4].value = ASCIIToUTF16("123");
2145
2146 base::HistogramTester histogram_tester;
2147
2148 // Names match loosely, upload should happen.
2149 FormSubmitted(credit_card_form);
2150 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
2151 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
2152
2153 // Verify that the correct histogram entry (and only that) was logged.
2154 ExpectUniqueCardUploadDecision(histogram_tester,
2155 AutofillMetrics::UPLOAD_OFFERED);
2156 // Verify that the correct UKM was logged.
2157 ExpectCardUploadDecisionUkm(AutofillMetrics::UPLOAD_OFFERED);
2158 }
2159 #endif
2160
2161 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
2162 // permanently if the test doesn't apply to iOS flow.
2163 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_NoMiddleInitialInCCForm)2164 TEST_F(CreditCardSaveManagerTest, UploadCreditCard_NoMiddleInitialInCCForm) {
2165 // Create, fill and submit two address forms with different names.
2166 FormData address_form1, address_form2;
2167 test::CreateTestAddressFormData(&address_form1);
2168 test::CreateTestAddressFormData(&address_form2);
2169 FormsSeen({address_form1, address_form2});
2170
2171 // Names can have different variations of middle initials.
2172 ManuallyFillAddressForm("flo w.", "master", "77401", "US", &address_form1);
2173 FormSubmitted(address_form1);
2174 ManuallyFillAddressForm("Flo W", "Master", "77401", "US", &address_form2);
2175 FormSubmitted(address_form2);
2176
2177 // Set up our credit card form data.
2178 FormData credit_card_form;
2179 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
2180 FormsSeen({credit_card_form});
2181
2182 // Edit the data, but do not use middle initial.
2183 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
2184 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
2185 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
2186 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
2187 credit_card_form.fields[4].value = ASCIIToUTF16("123");
2188
2189 base::HistogramTester histogram_tester;
2190
2191 // Names match loosely, upload should happen.
2192 FormSubmitted(credit_card_form);
2193 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
2194 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
2195
2196 // Verify that the correct histogram entry (and only that) was logged.
2197 ExpectUniqueCardUploadDecision(histogram_tester,
2198 AutofillMetrics::UPLOAD_OFFERED);
2199 // Verify that the correct UKM was logged.
2200 ExpectCardUploadDecisionUkm(AutofillMetrics::UPLOAD_OFFERED);
2201 }
2202 #endif
2203
2204 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
2205 // permanently if the test doesn't apply to iOS flow.
2206 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_CCFormHasCardholderMiddleName)2207 TEST_F(CreditCardSaveManagerTest,
2208 UploadCreditCard_CCFormHasCardholderMiddleName) {
2209 // Create, fill and submit address form without middle name.
2210 FormData address_form;
2211 test::CreateTestAddressFormData(&address_form);
2212 FormsSeen({address_form});
2213 ManuallyFillAddressForm("John", "Adams", "77401", "US", &address_form);
2214 FormSubmitted(address_form);
2215
2216 // Set up our credit card form data.
2217 FormData credit_card_form;
2218 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
2219 FormsSeen({credit_card_form});
2220
2221 // Edit the name by adding a middle name.
2222 credit_card_form.fields[0].value = ASCIIToUTF16("John Quincy Adams");
2223 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
2224 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
2225 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
2226 credit_card_form.fields[4].value = ASCIIToUTF16("123");
2227
2228 base::HistogramTester histogram_tester;
2229
2230 // With the offer-to-save decision deferred to Google Payments, Payments can
2231 // still decide to allow saving despite the mismatching names.
2232 FormSubmitted(credit_card_form);
2233 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
2234 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
2235
2236 // Verify that the correct histogram entries were logged.
2237 ExpectCardUploadDecision(histogram_tester, AutofillMetrics::UPLOAD_OFFERED);
2238 ExpectCardUploadDecision(
2239 histogram_tester, AutofillMetrics::UPLOAD_NOT_OFFERED_CONFLICTING_NAMES);
2240 ExpectCardUploadDecision(
2241 histogram_tester,
2242 AutofillMetrics::USER_REQUESTED_TO_PROVIDE_CARDHOLDER_NAME);
2243 // Verify that the correct UKM was logged.
2244 ExpectCardUploadDecisionUkm(
2245 AutofillMetrics::UPLOAD_OFFERED |
2246 AutofillMetrics::UPLOAD_NOT_OFFERED_CONFLICTING_NAMES |
2247 AutofillMetrics::USER_REQUESTED_TO_PROVIDE_CARDHOLDER_NAME);
2248 }
2249 #endif
2250
2251 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
2252 // permanently if the test doesn't apply to iOS flow.
2253 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_CCFormHasAddressMiddleName)2254 TEST_F(CreditCardSaveManagerTest, UploadCreditCard_CCFormHasAddressMiddleName) {
2255 // Create, fill and submit address form with middle name.
2256 FormData address_form;
2257 test::CreateTestAddressFormData(&address_form);
2258 FormsSeen({address_form});
2259 ManuallyFillAddressForm("John Quincy", "Adams", "77401", "US", &address_form);
2260 FormSubmitted(address_form);
2261
2262 // Set up our credit card form data.
2263 FormData credit_card_form;
2264 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
2265 FormsSeen({credit_card_form});
2266
2267 // Edit the name by removing middle name.
2268 credit_card_form.fields[0].value = ASCIIToUTF16("John Adams");
2269 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
2270 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
2271 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
2272 credit_card_form.fields[4].value = ASCIIToUTF16("123");
2273
2274 base::HistogramTester histogram_tester;
2275
2276 // With the offer-to-save decision deferred to Google Payments, Payments can
2277 // still decide to allow saving despite the mismatching names.
2278 FormSubmitted(credit_card_form);
2279 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
2280 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
2281
2282 // Verify that the correct histogram entries were logged.
2283 ExpectCardUploadDecision(histogram_tester, AutofillMetrics::UPLOAD_OFFERED);
2284 ExpectCardUploadDecision(
2285 histogram_tester, AutofillMetrics::UPLOAD_NOT_OFFERED_CONFLICTING_NAMES);
2286 ExpectCardUploadDecision(
2287 histogram_tester,
2288 AutofillMetrics::USER_REQUESTED_TO_PROVIDE_CARDHOLDER_NAME);
2289 // Verify that the correct UKM was logged.
2290 ExpectCardUploadDecisionUkm(
2291 AutofillMetrics::UPLOAD_OFFERED |
2292 AutofillMetrics::UPLOAD_NOT_OFFERED_CONFLICTING_NAMES |
2293 AutofillMetrics::USER_REQUESTED_TO_PROVIDE_CARDHOLDER_NAME);
2294 }
2295 #endif
2296
2297 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
2298 // permanently if the test doesn't apply to iOS flow.
2299 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_NamesCanMismatch)2300 TEST_F(CreditCardSaveManagerTest, UploadCreditCard_NamesCanMismatch) {
2301 // Create, fill and submit two address forms with different names.
2302 FormData address_form1, address_form2;
2303 test::CreateTestAddressFormData(&address_form1);
2304 test::CreateTestAddressFormData(&address_form2);
2305
2306 std::vector<FormData> address_forms;
2307 address_forms.push_back(address_form1);
2308 address_forms.push_back(address_form2);
2309 FormsSeen(address_forms);
2310
2311 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form1);
2312 FormSubmitted(address_form1);
2313
2314 ManuallyFillAddressForm("Master", "Blaster", "77401", "US", &address_form2);
2315 FormSubmitted(address_form2);
2316
2317 // Set up our credit card form data.
2318 FormData credit_card_form;
2319 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
2320 FormsSeen(std::vector<FormData>(1, credit_card_form));
2321
2322 // Edit the data, but use yet another name, and submit.
2323 credit_card_form.fields[0].value = ASCIIToUTF16("Bob Master");
2324 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
2325 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
2326 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
2327 credit_card_form.fields[4].value = ASCIIToUTF16("123");
2328
2329 base::HistogramTester histogram_tester;
2330
2331 // With the offer-to-save decision deferred to Google Payments, Payments can
2332 // still decide to allow saving despite the mismatching names.
2333 FormSubmitted(credit_card_form);
2334 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
2335 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
2336
2337 // Verify that the correct histogram entries were logged.
2338 ExpectCardUploadDecision(histogram_tester, AutofillMetrics::UPLOAD_OFFERED);
2339 ExpectCardUploadDecision(
2340 histogram_tester, AutofillMetrics::UPLOAD_NOT_OFFERED_CONFLICTING_NAMES);
2341 ExpectCardUploadDecision(
2342 histogram_tester,
2343 AutofillMetrics::USER_REQUESTED_TO_PROVIDE_CARDHOLDER_NAME);
2344 // Verify that the correct UKM was logged.
2345 ExpectCardUploadDecisionUkm(
2346 AutofillMetrics::UPLOAD_OFFERED |
2347 AutofillMetrics::UPLOAD_NOT_OFFERED_CONFLICTING_NAMES |
2348 AutofillMetrics::USER_REQUESTED_TO_PROVIDE_CARDHOLDER_NAME);
2349 }
2350 #endif
2351
2352 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
2353 // permanently if the test doesn't apply to iOS flow.
2354 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_IgnoreOldProfiles)2355 TEST_F(CreditCardSaveManagerTest, UploadCreditCard_IgnoreOldProfiles) {
2356 // Create the test clock and set the time to a specific value.
2357 TestAutofillClock test_clock;
2358 test_clock.SetNow(kArbitraryTime);
2359
2360 // Create, fill and submit two address forms with different names.
2361 FormData address_form1, address_form2;
2362 test::CreateTestAddressFormData(&address_form1);
2363 test::CreateTestAddressFormData(&address_form2);
2364 FormsSeen({address_form1, address_form2});
2365
2366 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form1);
2367 FormSubmitted(address_form1);
2368
2369 // Advance the current time. Since |address_form1| will not be a recently
2370 // used address profile, we will not include it in the candidate profiles.
2371 test_clock.SetNow(kMuchLaterTime);
2372
2373 ManuallyFillAddressForm("Master", "Blaster", "77401", "US", &address_form2);
2374 FormSubmitted(address_form2);
2375
2376 // Set up our credit card form data.
2377 FormData credit_card_form;
2378 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
2379 FormsSeen(std::vector<FormData>(1, credit_card_form));
2380
2381 // Edit the data, but use yet another name, and submit.
2382 credit_card_form.fields[0].value = ASCIIToUTF16("Master Blaster");
2383 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
2384 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
2385 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
2386 credit_card_form.fields[4].value = ASCIIToUTF16("123");
2387
2388 base::HistogramTester histogram_tester;
2389
2390 // Name matches recently used profile, should offer upload.
2391 FormSubmitted(credit_card_form);
2392 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
2393 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
2394
2395 // Verify that the correct histogram entry (and only that) was logged.
2396 ExpectUniqueCardUploadDecision(histogram_tester,
2397 AutofillMetrics::UPLOAD_OFFERED);
2398 }
2399 #endif
2400
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_RequestCardholderNameIfNameMissingAndNoPaymentsCustomer)2401 TEST_F(
2402 CreditCardSaveManagerTest,
2403 UploadCreditCard_RequestCardholderNameIfNameMissingAndNoPaymentsCustomer) {
2404 // Create, fill and submit an address form in order to establish a recent
2405 // profile which can be selected for the upload request.
2406 FormData address_form;
2407 test::CreateTestAddressFormData(&address_form);
2408 FormsSeen(std::vector<FormData>(1, address_form));
2409 // But omit the name:
2410 ManuallyFillAddressForm("", "", "77401", "US", &address_form);
2411 FormSubmitted(address_form);
2412
2413 // Set up our credit card form data.
2414 FormData credit_card_form;
2415 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
2416 FormsSeen(std::vector<FormData>(1, credit_card_form));
2417
2418 // Edit the data, but don't include a name, and submit.
2419 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
2420 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
2421 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
2422 credit_card_form.fields[4].value = ASCIIToUTF16("123");
2423
2424 base::HistogramTester histogram_tester;
2425
2426 // With the offer-to-save decision deferred to Google Payments, Payments can
2427 // still decide to allow saving despite the missing name.
2428 FormSubmitted(credit_card_form);
2429 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
2430 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
2431
2432 // Verify that the correct histogram entry and DetectedValue for "Cardholder
2433 // name explicitly requested" was logged.
2434 ExpectCardUploadDecision(
2435 histogram_tester,
2436 AutofillMetrics::USER_REQUESTED_TO_PROVIDE_CARDHOLDER_NAME);
2437 EXPECT_TRUE(payments_client_->detected_values_in_upload_details() &
2438 CreditCardSaveManager::DetectedValue::USER_PROVIDED_NAME);
2439 }
2440
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_RequestCardholderNameIfNameConflictingAndNoPaymentsCustomer)2441 TEST_F(
2442 CreditCardSaveManagerTest,
2443 UploadCreditCard_RequestCardholderNameIfNameConflictingAndNoPaymentsCustomer) {
2444 // Create, fill and submit an address form in order to establish a recent
2445 // profile which can be selected for the upload request.
2446 FormData address_form;
2447 test::CreateTestAddressFormData(&address_form);
2448 FormsSeen(std::vector<FormData>(1, address_form));
2449 ManuallyFillAddressForm("John", "Smith", "77401", "US", &address_form);
2450 FormSubmitted(address_form);
2451
2452 // Set up our credit card form data.
2453 FormData credit_card_form;
2454 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
2455 FormsSeen(std::vector<FormData>(1, credit_card_form));
2456
2457 // Edit the data, but include a conflicting name, and submit.
2458 credit_card_form.fields[0].value = ASCIIToUTF16("Jane Doe");
2459 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
2460 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
2461 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
2462 credit_card_form.fields[4].value = ASCIIToUTF16("123");
2463
2464 base::HistogramTester histogram_tester;
2465
2466 // With the offer-to-save decision deferred to Google Payments, Payments can
2467 // still decide to allow saving despite the missing name.
2468 FormSubmitted(credit_card_form);
2469 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
2470 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
2471
2472 // Verify that the correct histogram entry and DetectedValue for "Cardholder
2473 // name explicitly requested" was logged.
2474 ExpectCardUploadDecision(
2475 histogram_tester,
2476 AutofillMetrics::USER_REQUESTED_TO_PROVIDE_CARDHOLDER_NAME);
2477 EXPECT_TRUE(payments_client_->detected_values_in_upload_details() &
2478 CreditCardSaveManager::DetectedValue::USER_PROVIDED_NAME);
2479 }
2480
TEST_F(CreditCardSaveManagerTest,GoogleHostSite_ShouldNotOfferSaveIfUploadEnabled)2481 TEST_F(CreditCardSaveManagerTest,
2482 GoogleHostSite_ShouldNotOfferSaveIfUploadEnabled) {
2483 // Create, fill and submit an address form in order to establish a recent
2484 // profile which can be selected for the upload request.
2485 FormData address_form;
2486 test::CreateTestAddressFormData(&address_form);
2487 FormsSeen(std::vector<FormData>(1, address_form));
2488 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
2489 FormSubmitted(address_form);
2490 // Set up our credit card form data.
2491 FormData credit_card_form;
2492 CreateTestCreditCardFormData(
2493 &credit_card_form, CreditCardFormOptions().with_is_google_host(true));
2494 FormsSeen(std::vector<FormData>(1, credit_card_form));
2495
2496 // Edit the data, and submit.
2497 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
2498 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
2499 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
2500 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
2501 credit_card_form.fields[4].value = ASCIIToUTF16("123");
2502
2503 base::HistogramTester histogram_tester;
2504
2505 // The credit card should neither be saved locally or uploaded.
2506 FormSubmitted(credit_card_form);
2507 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
2508 EXPECT_FALSE(credit_card_save_manager_->CreditCardWasUploaded());
2509
2510 // Verify that no histogram entry was logged.
2511 histogram_tester.ExpectTotalCount("Autofill.CardUploadDecisionMetric", 0);
2512 }
2513
TEST_F(CreditCardSaveManagerTest,GoogleHostSite_ShouldOfferSaveIfUploadDisabled)2514 TEST_F(CreditCardSaveManagerTest,
2515 GoogleHostSite_ShouldOfferSaveIfUploadDisabled) {
2516 credit_card_save_manager_->SetCreditCardUploadEnabled(false);
2517
2518 // Create, fill and submit an address form in order to establish a recent
2519 // profile which can be selected for the upload request.
2520 FormData address_form;
2521 test::CreateTestAddressFormData(&address_form);
2522 FormsSeen(std::vector<FormData>(1, address_form));
2523 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
2524 FormSubmitted(address_form);
2525 // Set up our credit card form data.
2526 FormData credit_card_form;
2527 CreateTestCreditCardFormData(
2528 &credit_card_form, CreditCardFormOptions().with_is_google_host(true));
2529
2530 FormsSeen(std::vector<FormData>(1, credit_card_form));
2531
2532 // Edit the data, and submit.
2533 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
2534 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
2535 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
2536 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
2537 credit_card_form.fields[4].value = ASCIIToUTF16("123");
2538
2539 base::HistogramTester histogram_tester;
2540
2541 // The credit card should be saved locally.
2542 FormSubmitted(credit_card_form);
2543 EXPECT_TRUE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
2544 EXPECT_FALSE(credit_card_save_manager_->CreditCardWasUploaded());
2545 }
2546
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_DoNotRequestCardholderNameIfNameExistsAndNoPaymentsCustomer)2547 TEST_F(
2548 CreditCardSaveManagerTest,
2549 UploadCreditCard_DoNotRequestCardholderNameIfNameExistsAndNoPaymentsCustomer) {
2550 // On iOS the cardholder name fix flow and expiration date fix flow no longer
2551 // apply since the user is forced to always set correct data before submitting.
2552 #if !defined(OS_IOS)
2553 // Create, fill and submit an address form in order to establish a recent
2554 // profile which can be selected for the upload request.
2555 FormData address_form;
2556 test::CreateTestAddressFormData(&address_form);
2557 FormsSeen(std::vector<FormData>(1, address_form));
2558 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
2559 FormSubmitted(address_form);
2560
2561 // Set up our credit card form data.
2562 FormData credit_card_form;
2563 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
2564 FormsSeen(std::vector<FormData>(1, credit_card_form));
2565
2566 // Edit the data, and submit.
2567 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
2568 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
2569 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
2570 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
2571 credit_card_form.fields[4].value = ASCIIToUTF16("123");
2572
2573 base::HistogramTester histogram_tester;
2574
2575 FormSubmitted(credit_card_form);
2576 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
2577 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
2578
2579 // Because everything went smoothly, verify that there was no histogram entry
2580 // or DetectedValue for "Cardholder name explicitly requested" logged.
2581 ExpectNoCardUploadDecision(
2582 histogram_tester,
2583 AutofillMetrics::USER_REQUESTED_TO_PROVIDE_CARDHOLDER_NAME);
2584 EXPECT_FALSE(payments_client_->detected_values_in_upload_details() &
2585 CreditCardSaveManager::DetectedValue::USER_PROVIDED_NAME);
2586 #endif
2587 }
2588
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_DoNotRequestCardholderNameIfNameMissingAndPaymentsCustomer)2589 TEST_F(
2590 CreditCardSaveManagerTest,
2591 UploadCreditCard_DoNotRequestCardholderNameIfNameMissingAndPaymentsCustomer) {
2592 // On iOS the cardholder name fix flow and expiration date fix flow no longer
2593 // apply since the user is forced to always set correct data before
2594 // submitting.
2595 #if !defined(OS_IOS)
2596 // Set the billing_customer_number to designate existence of a Payments
2597 // account.
2598 personal_data_.SetPaymentsCustomerData(
2599 std::make_unique<PaymentsCustomerData>(/*customer_id=*/"123456"));
2600
2601 // Create, fill and submit an address form in order to establish a recent
2602 // profile which can be selected for the upload request.
2603 FormData address_form;
2604 test::CreateTestAddressFormData(&address_form);
2605 FormsSeen(std::vector<FormData>(1, address_form));
2606 // But omit the name:
2607 ManuallyFillAddressForm("", "", "77401", "US", &address_form);
2608 FormSubmitted(address_form);
2609
2610 // Set up our credit card form data.
2611 FormData credit_card_form;
2612 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
2613 FormsSeen(std::vector<FormData>(1, credit_card_form));
2614
2615 // Edit the data, but don't include a name, and submit.
2616 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
2617 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
2618 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
2619 credit_card_form.fields[4].value = ASCIIToUTF16("123");
2620
2621 base::HistogramTester histogram_tester;
2622
2623 // With the offer-to-save decision deferred to Google Payments, Payments can
2624 // still decide to allow saving despite the missing name.
2625 FormSubmitted(credit_card_form);
2626 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
2627 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
2628
2629 // Verify that there was no histogram entry or DetectedValue for "Cardholder
2630 // name explicitly requested" logged.
2631 ExpectNoCardUploadDecision(
2632 histogram_tester,
2633 AutofillMetrics::USER_REQUESTED_TO_PROVIDE_CARDHOLDER_NAME);
2634 EXPECT_FALSE(payments_client_->detected_values_in_upload_details() &
2635 CreditCardSaveManager::DetectedValue::USER_PROVIDED_NAME);
2636 #endif
2637 }
2638
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_DoNotRequestCardholderNameIfNameConflictingAndPaymentsCustomer)2639 TEST_F(
2640 CreditCardSaveManagerTest,
2641 UploadCreditCard_DoNotRequestCardholderNameIfNameConflictingAndPaymentsCustomer) {
2642 // On iOS the cardholder name fix flow and expiration date fix flow no longer
2643 // apply since the user is forced to always set correct data before
2644 // submitting.
2645 #if !defined(OS_IOS)
2646 // Set the billing_customer_number to designate existence of a Payments
2647 // account.
2648 personal_data_.SetPaymentsCustomerData(
2649 std::make_unique<PaymentsCustomerData>(/*customer_id=*/"123456"));
2650
2651 // Create, fill and submit an address form in order to establish a recent
2652 // profile which can be selected for the upload request.
2653 FormData address_form;
2654 test::CreateTestAddressFormData(&address_form);
2655 FormsSeen(std::vector<FormData>(1, address_form));
2656 ManuallyFillAddressForm("John", "Smith", "77401", "US", &address_form);
2657 FormSubmitted(address_form);
2658
2659 // Set up our credit card form data.
2660 FormData credit_card_form;
2661 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
2662 FormsSeen(std::vector<FormData>(1, credit_card_form));
2663
2664 // Edit the data, but include a conflicting name, and submit.
2665 credit_card_form.fields[0].value = ASCIIToUTF16("Jane Doe");
2666 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
2667 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
2668 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
2669 credit_card_form.fields[4].value = ASCIIToUTF16("123");
2670
2671 base::HistogramTester histogram_tester;
2672
2673 // With the offer-to-save decision deferred to Google Payments, Payments can
2674 // still decide to allow saving despite the missing name.
2675 FormSubmitted(credit_card_form);
2676 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
2677 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
2678
2679 // Verify that there was no histogram entry or DetectedValue for "Cardholder
2680 // name explicitly requested" logged.
2681 ExpectNoCardUploadDecision(
2682 histogram_tester,
2683 AutofillMetrics::USER_REQUESTED_TO_PROVIDE_CARDHOLDER_NAME);
2684 EXPECT_FALSE(payments_client_->detected_values_in_upload_details() &
2685 CreditCardSaveManager::DetectedValue::USER_PROVIDED_NAME);
2686 #endif
2687 }
2688
2689 // This test ensures |should_request_name_from_user_| is reset between offers to
2690 // save.
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_ShouldRequestCardholderName_ResetBetweenConsecutiveSaves)2691 TEST_F(
2692 CreditCardSaveManagerTest,
2693 UploadCreditCard_ShouldRequestCardholderName_ResetBetweenConsecutiveSaves) {
2694 // On iOS the cardholder name fix flow and expiration date fix flow no longer
2695 // apply since the user is forced to always set correct data before
2696 // submitting.
2697 #if !defined(OS_IOS)
2698 // Create, fill and submit an address form in order to establish a recent
2699 // profile which can be selected for the upload request.
2700 FormData address_form;
2701 test::CreateTestAddressFormData(&address_form);
2702 FormsSeen(std::vector<FormData>(1, address_form));
2703 // But omit the name:
2704 ManuallyFillAddressForm("", "", "77401", "US", &address_form);
2705 FormSubmitted(address_form);
2706
2707 // Set up our credit card form data.
2708 FormData credit_card_form;
2709 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
2710 FormsSeen(std::vector<FormData>(1, credit_card_form));
2711
2712 // Edit the data, but don't include a name, and submit.
2713 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
2714 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
2715 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
2716 credit_card_form.fields[4].value = ASCIIToUTF16("123");
2717
2718 // With the offer-to-save decision deferred to Google Payments, Payments can
2719 // still decide to allow saving despite the missing name.
2720 FormSubmitted(credit_card_form);
2721 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
2722 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
2723
2724 // Verify the |credit_card_save_manager_| is requesting cardholder name.
2725 EXPECT_TRUE(credit_card_save_manager_->should_request_name_from_user_);
2726
2727 // Set the billing_customer_number to designate existence of a Payments
2728 // account.
2729 personal_data_.SetPaymentsCustomerData(
2730 std::make_unique<PaymentsCustomerData>(/*customer_id=*/"123456"));
2731
2732 // Run through the form submit in exactly the same way (but now Chrome knows
2733 // that the user is a Google Payments customer).
2734 personal_data_.ClearCreditCards();
2735 personal_data_.ClearProfiles();
2736 FormSubmitted(credit_card_form);
2737
2738 // Verify the |credit_card_save_manager_| is NOT requesting cardholder name.
2739 EXPECT_FALSE(credit_card_save_manager_->should_request_name_from_user_);
2740 #endif
2741 }
2742
2743 // This test ensures |should_request_expiration_date_from_user_|
2744 // is reset between offers to save.
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_ShouldRequestExpirationDate_ResetBetweenConsecutiveSaves)2745 TEST_F(
2746 CreditCardSaveManagerTest,
2747 UploadCreditCard_ShouldRequestExpirationDate_ResetBetweenConsecutiveSaves) {
2748 // On iOS the cardholder name fix flow and expiration date fix flow no longer
2749 // apply since the user is forced to always set correct data before
2750 // submitting.
2751 #if !defined(OS_IOS)
2752 // Create, fill and submit an address form in order to establish a recent
2753 // profile which can be selected for the upload request.
2754 FormData address_form;
2755 test::CreateTestAddressFormData(&address_form);
2756 FormsSeen(std::vector<FormData>(1, address_form));
2757 ManuallyFillAddressForm("Jane", "Doe", "77401", "US", &address_form);
2758 FormSubmitted(address_form);
2759
2760 // Set up our credit card form data.
2761 FormData credit_card_form;
2762 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
2763 FormsSeen(std::vector<FormData>(1, credit_card_form));
2764
2765 // Edit the data, but don't include a expiration date, and submit.
2766 credit_card_form.fields[0].value = ASCIIToUTF16("Jane Doe");
2767 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
2768 credit_card_form.fields[2].value = ASCIIToUTF16("");
2769 credit_card_form.fields[3].value = ASCIIToUTF16("");
2770 credit_card_form.fields[4].value = ASCIIToUTF16("123");
2771
2772 // With the offer-to-save decision deferred to Google Payments, Payments can
2773 // still decide to allow saving despite the missing expiration date.
2774 FormSubmitted(credit_card_form);
2775 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
2776 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
2777
2778 // Verify the |credit_card_save_manager_| is requesting expiration date.
2779 EXPECT_TRUE(
2780 credit_card_save_manager_->should_request_expiration_date_from_user_);
2781
2782 // Edit the data, include a expiration date, and submit this time.
2783 credit_card_form.fields[0].value = ASCIIToUTF16("Jane Doe");
2784 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
2785 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
2786 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
2787 credit_card_form.fields[4].value = ASCIIToUTF16("123");
2788 FormSubmitted(credit_card_form);
2789
2790 // Verify the |credit_card_save_manager_| is NOT requesting expiration date.
2791 EXPECT_FALSE(
2792 credit_card_save_manager_->should_request_expiration_date_from_user_);
2793 #endif
2794 }
2795
2796 // This test ensures |should_request_expiration_date_from_user_|
2797 // is false when Wallet Sync Transport is enabled.
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_WalletSyncTransportEnabled_ShouldNotRequestExpirationDate)2798 TEST_F(
2799 CreditCardSaveManagerTest,
2800 UploadCreditCard_WalletSyncTransportEnabled_ShouldNotRequestExpirationDate) {
2801 // On iOS the cardholder name fix flow and expiration date fix flow no longer
2802 // apply since the user is forced to always set correct data before
2803 // submitting.
2804 #if !defined(OS_IOS)
2805 // Wallet Sync Transport is enabled.
2806 personal_data_.SetSyncAndSignInState(
2807 AutofillSyncSigninState::kSignedInAndWalletSyncTransportEnabled);
2808
2809 // Create, fill and submit an address form in order to establish a recent
2810 // profile which can be selected for the upload request.
2811 FormData address_form;
2812 test::CreateTestAddressFormData(&address_form);
2813 FormsSeen(std::vector<FormData>(1, address_form));
2814 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
2815 FormSubmitted(address_form);
2816
2817 // Set up our credit card form data.
2818 FormData credit_card_form;
2819 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
2820 FormsSeen(std::vector<FormData>(1, credit_card_form));
2821
2822 // Edit the data, but don't include a expiration date, and submit.
2823 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
2824 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
2825 credit_card_form.fields[2].value = ASCIIToUTF16("");
2826 credit_card_form.fields[3].value = ASCIIToUTF16("");
2827 credit_card_form.fields[4].value = ASCIIToUTF16("123");
2828
2829 FormSubmitted(credit_card_form);
2830
2831 // Save should not be offered because implicit Sync + Expiration date fix flow
2832 // aborts offering save
2833 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
2834 EXPECT_FALSE(credit_card_save_manager_->CreditCardWasUploaded());
2835 #endif
2836 }
2837
2838 // This test ensures |should_request_expiration_date_from_user_|
2839 // is true when Wallet Sync Transport is not enabled.
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_WalletSyncTransportNotEnabled_ShouldRequestExpirationDate)2840 TEST_F(
2841 CreditCardSaveManagerTest,
2842 UploadCreditCard_WalletSyncTransportNotEnabled_ShouldRequestExpirationDate) {
2843 // On iOS the cardholder name fix flow and expiration date fix flow no longer
2844 // apply since the user is forced to always set correct data before
2845 // submitting.
2846 #if !defined(OS_IOS)
2847 // Wallet Sync Transport is not enabled.
2848 personal_data_.SetSyncAndSignInState(
2849 AutofillSyncSigninState::kSignedInAndSyncFeatureEnabled);
2850
2851 // Create, fill and submit an address form in order to establish a recent
2852 // profile which can be selected for the upload request.
2853 FormData address_form;
2854 test::CreateTestAddressFormData(&address_form);
2855 FormsSeen(std::vector<FormData>(1, address_form));
2856 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
2857 FormSubmitted(address_form);
2858
2859 // Set up our credit card form data.
2860 FormData credit_card_form;
2861 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
2862 FormsSeen(std::vector<FormData>(1, credit_card_form));
2863
2864 // Edit the data, but don't include a expiration date, and submit.
2865 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
2866 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
2867 credit_card_form.fields[2].value = ASCIIToUTF16("");
2868 credit_card_form.fields[3].value = ASCIIToUTF16("");
2869 credit_card_form.fields[4].value = ASCIIToUTF16("123");
2870
2871 // With the offer-to-save decision deferred to Google Payments, Payments can
2872 // still decide to allow saving despite the missing expiration date.
2873 FormSubmitted(credit_card_form);
2874 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
2875 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
2876
2877 // Verify the |credit_card_save_manager_| is requesting expiration date.
2878 EXPECT_TRUE(
2879 credit_card_save_manager_->should_request_expiration_date_from_user_);
2880 #endif
2881 }
2882
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_DoNotRequestExpirationDateIfMissingNameAndExpirationDate)2883 TEST_F(
2884 CreditCardSaveManagerTest,
2885 UploadCreditCard_DoNotRequestExpirationDateIfMissingNameAndExpirationDate) {
2886 // On iOS the cardholder name fix flow and expiration date fix flow no longer
2887 // apply since the user is forced to always set correct data before
2888 // submitting.
2889 #if !defined(OS_IOS)
2890 // Create, fill and submit an address form in order to establish a recent
2891 // profile which can be selected for the upload request.
2892 FormData address_form;
2893 test::CreateTestAddressFormData(&address_form);
2894 FormsSeen(std::vector<FormData>(1, address_form));
2895 // But omit the name:
2896 ManuallyFillAddressForm("", "", "77401", "US", &address_form);
2897 FormSubmitted(address_form);
2898
2899 // Set up our credit card form data.
2900 FormData credit_card_form;
2901 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
2902 FormsSeen(std::vector<FormData>(1, credit_card_form));
2903
2904 // Edit the data, and submit.
2905 credit_card_form.fields[0].value = ASCIIToUTF16("");
2906 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
2907 credit_card_form.fields[2].value = ASCIIToUTF16("");
2908 credit_card_form.fields[3].value = ASCIIToUTF16("");
2909 credit_card_form.fields[4].value = ASCIIToUTF16("123");
2910
2911 base::HistogramTester histogram_tester;
2912
2913 FormSubmitted(credit_card_form);
2914 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
2915 EXPECT_FALSE(credit_card_save_manager_->CreditCardWasUploaded());
2916 #endif
2917 }
2918
2919 // iOS should always provide a valid expiration date when attempting to
2920 // upload a Saved Card due to the Messages SaveCard modal.
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_AlwaysRequestCardholderNameAndExpirationDateOnIOS)2921 TEST_F(CreditCardSaveManagerTest,
2922 UploadCreditCard_AlwaysRequestCardholderNameAndExpirationDateOnIOS) {
2923 #if defined(OS_IOS)
2924 // Create, fill and submit an address form in order to establish a recent
2925 // profile which can be selected for the upload request.
2926 FormData address_form;
2927 test::CreateTestAddressFormData(&address_form);
2928 FormsSeen(std::vector<FormData>(1, address_form));
2929 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
2930 FormSubmitted(address_form);
2931
2932 // Set up our credit card form data.
2933 FormData credit_card_form;
2934 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
2935 FormsSeen(std::vector<FormData>(1, credit_card_form));
2936
2937 // Edit the data, and submit.
2938 credit_card_form.fields[0].value = ASCIIToUTF16("");
2939 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
2940 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
2941 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
2942 credit_card_form.fields[4].value = ASCIIToUTF16("123");
2943
2944 base::HistogramTester histogram_tester;
2945
2946 FormSubmitted(credit_card_form);
2947 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
2948 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
2949 EXPECT_TRUE(
2950 payments_client_->detected_values_in_upload_details() &
2951 CreditCardSaveManager::DetectedValue::USER_PROVIDED_EXPIRATION_DATE);
2952 EXPECT_TRUE(payments_client_->detected_values_in_upload_details() &
2953 CreditCardSaveManager::DetectedValue::USER_PROVIDED_NAME);
2954
2955 #endif
2956 }
2957
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_RequestExpirationDateViaExpDateFixFlow)2958 TEST_F(CreditCardSaveManagerTest,
2959 UploadCreditCard_RequestExpirationDateViaExpDateFixFlow) {
2960 #if defined(OS_IOS)
2961 // iOS should always provide a valid expiration date when attempting to
2962 // upload a Saved Card due to the Messages SaveCard modal. The manager
2963 // shouldn't handle expired dates.
2964 if ((base::FeatureList::IsEnabled(
2965 features::kAutofillSaveCardInfobarEditSupport) &&
2966 base::FeatureList::IsEnabled(kIOSInfobarUIReboot))) {
2967 return;
2968 }
2969 #endif
2970
2971 // Create, fill and submit an address form in order to establish a recent
2972 // profile which can be selected for the upload request.
2973 FormData address_form;
2974 test::CreateTestAddressFormData(&address_form);
2975 FormsSeen(std::vector<FormData>(1, address_form));
2976 ManuallyFillAddressForm("John", "Smith", "77401", "US", &address_form);
2977 FormSubmitted(address_form);
2978
2979 // Set up our credit card form data.
2980 FormData credit_card_form;
2981 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
2982 FormsSeen(std::vector<FormData>(1, credit_card_form));
2983
2984 // Edit the data, and submit.
2985 credit_card_form.fields[0].value = ASCIIToUTF16("John Smith");
2986 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
2987 credit_card_form.fields[2].value = ASCIIToUTF16("");
2988 credit_card_form.fields[3].value = ASCIIToUTF16("");
2989 credit_card_form.fields[4].value = ASCIIToUTF16("123");
2990
2991 base::HistogramTester histogram_tester;
2992 FormSubmitted(credit_card_form);
2993 // Verify that the correct histogram entry and DetectedValue for "Expiration
2994 // date explicitly requested" was logged.
2995 ExpectCardUploadDecision(
2996 histogram_tester,
2997 AutofillMetrics::USER_REQUESTED_TO_PROVIDE_EXPIRATION_DATE);
2998 histogram_tester.ExpectUniqueSample(
2999 "Autofill.SaveCardRequestExpirationDateReason",
3000 AutofillMetrics::SaveCardRequestExpirationDateReasonMetric::
3001 kMonthAndYearMissing,
3002 1);
3003 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
3004 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
3005 EXPECT_TRUE(
3006 payments_client_->detected_values_in_upload_details() &
3007 CreditCardSaveManager::DetectedValue::USER_PROVIDED_EXPIRATION_DATE);
3008 }
3009
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_RequestExpirationDateIfOnlyMonthMissing)3010 TEST_F(CreditCardSaveManagerTest,
3011 UploadCreditCard_RequestExpirationDateIfOnlyMonthMissing) {
3012 #if defined(OS_IOS)
3013 // iOS should always provide a valid expiration date when attempting to
3014 // upload a Saved Card due to the Messages SaveCard modal. The manager
3015 // shouldn't handle expired dates.
3016 if ((base::FeatureList::IsEnabled(
3017 features::kAutofillSaveCardInfobarEditSupport) &&
3018 base::FeatureList::IsEnabled(kIOSInfobarUIReboot))) {
3019 return;
3020 }
3021 #endif
3022
3023 // Create, fill and submit an address form in order to establish a recent
3024 // profile which can be selected for the upload request.
3025 FormData address_form;
3026 test::CreateTestAddressFormData(&address_form);
3027 FormsSeen(std::vector<FormData>(1, address_form));
3028 ManuallyFillAddressForm("John", "Smith", "77401", "US", &address_form);
3029 FormSubmitted(address_form);
3030
3031 // Set up our credit card form data.
3032 FormData credit_card_form;
3033 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
3034 FormsSeen(std::vector<FormData>(1, credit_card_form));
3035
3036 // Edit the data, and submit.
3037 credit_card_form.fields[0].value = ASCIIToUTF16("John Smith");
3038 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
3039 credit_card_form.fields[2].value = ASCIIToUTF16("");
3040 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
3041 credit_card_form.fields[4].value = ASCIIToUTF16("123");
3042
3043 base::HistogramTester histogram_tester;
3044 FormSubmitted(credit_card_form);
3045 // Verify that the correct histogram entry and DetectedValue for "Expiration
3046 // date explicitly requested" was logged.
3047 ExpectCardUploadDecision(
3048 histogram_tester,
3049 AutofillMetrics::USER_REQUESTED_TO_PROVIDE_EXPIRATION_DATE);
3050 histogram_tester.ExpectUniqueSample(
3051 "Autofill.SaveCardRequestExpirationDateReason",
3052 AutofillMetrics::SaveCardRequestExpirationDateReasonMetric::
3053 kMonthMissingOnly,
3054 1);
3055 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
3056 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
3057 EXPECT_TRUE(
3058 payments_client_->detected_values_in_upload_details() &
3059 CreditCardSaveManager::DetectedValue::USER_PROVIDED_EXPIRATION_DATE);
3060 }
3061
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_RequestExpirationDateIfOnlyYearMissing)3062 TEST_F(CreditCardSaveManagerTest,
3063 UploadCreditCard_RequestExpirationDateIfOnlyYearMissing) {
3064 #if defined(OS_IOS)
3065 // iOS should always provide a valid expiration date when attempting to
3066 // upload a Saved Card due to the Messages SaveCard modal. The manager
3067 // shouldn't handle expired dates.
3068 if ((base::FeatureList::IsEnabled(
3069 features::kAutofillSaveCardInfobarEditSupport) &&
3070 base::FeatureList::IsEnabled(kIOSInfobarUIReboot))) {
3071 return;
3072 }
3073 #endif
3074
3075 // Create, fill and submit an address form in order to establish a recent
3076 // profile which can be selected for the upload request.
3077 FormData address_form;
3078 test::CreateTestAddressFormData(&address_form);
3079 FormsSeen(std::vector<FormData>(1, address_form));
3080 ManuallyFillAddressForm("John", "Smith", "77401", "US", &address_form);
3081 FormSubmitted(address_form);
3082
3083 // Set up our credit card form data.
3084 FormData credit_card_form;
3085 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
3086 FormsSeen(std::vector<FormData>(1, credit_card_form));
3087
3088 // Edit the data, and submit.
3089 credit_card_form.fields[0].value = ASCIIToUTF16("John Smith");
3090 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
3091 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
3092 credit_card_form.fields[3].value = ASCIIToUTF16("");
3093 credit_card_form.fields[4].value = ASCIIToUTF16("123");
3094
3095 base::HistogramTester histogram_tester;
3096 FormSubmitted(credit_card_form);
3097 // Verify that the correct histogram entry and DetectedValue for "Expiration
3098 // date explicitly requested" was logged.
3099 ExpectCardUploadDecision(
3100 histogram_tester,
3101 AutofillMetrics::USER_REQUESTED_TO_PROVIDE_EXPIRATION_DATE);
3102 histogram_tester.ExpectUniqueSample(
3103 "Autofill.SaveCardRequestExpirationDateReason",
3104 AutofillMetrics::SaveCardRequestExpirationDateReasonMetric::
3105 kYearMissingOnly,
3106 1);
3107 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
3108 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
3109 EXPECT_TRUE(
3110 payments_client_->detected_values_in_upload_details() &
3111 CreditCardSaveManager::DetectedValue::USER_PROVIDED_EXPIRATION_DATE);
3112 }
3113
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_RequestExpirationDateIfExpirationDateInputIsExpired)3114 TEST_F(CreditCardSaveManagerTest,
3115 UploadCreditCard_RequestExpirationDateIfExpirationDateInputIsExpired) {
3116 #if defined(OS_IOS)
3117 // iOS should always provide a valid expiration date when attempting to
3118 // upload a Saved Card due to the Messages SaveCard modal. The manager
3119 // shouldn't handle expired dates.
3120 if ((base::FeatureList::IsEnabled(
3121 features::kAutofillSaveCardInfobarEditSupport) &&
3122 base::FeatureList::IsEnabled(kIOSInfobarUIReboot))) {
3123 return;
3124 }
3125 #endif
3126
3127 // Create, fill and submit an address form in order to establish a recent
3128 // profile which can be selected for the upload request.
3129 FormData address_form;
3130 test::CreateTestAddressFormData(&address_form);
3131 FormsSeen(std::vector<FormData>(1, address_form));
3132 ManuallyFillAddressForm("John", "Smith", "77401", "US", &address_form);
3133 FormSubmitted(address_form);
3134
3135 // Set up our credit card form data.
3136 FormData credit_card_form;
3137 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
3138 FormsSeen(std::vector<FormData>(1, credit_card_form));
3139
3140 // Edit the data, and submit.
3141 credit_card_form.fields[0].value = ASCIIToUTF16("John Smith");
3142 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
3143 credit_card_form.fields[2].value = ASCIIToUTF16("09");
3144 credit_card_form.fields[3].value = ASCIIToUTF16("2000");
3145 credit_card_form.fields[4].value = ASCIIToUTF16("123");
3146
3147 base::HistogramTester histogram_tester;
3148 FormSubmitted(credit_card_form);
3149 // Verify that the correct histogram entry and DetectedValue for "Expiration
3150 // date explicitly requested" was logged.
3151 ExpectCardUploadDecision(
3152 histogram_tester,
3153 AutofillMetrics::USER_REQUESTED_TO_PROVIDE_EXPIRATION_DATE);
3154 histogram_tester.ExpectUniqueSample(
3155 "Autofill.SaveCardRequestExpirationDateReason",
3156 AutofillMetrics::SaveCardRequestExpirationDateReasonMetric::
3157 kExpirationDatePresentButExpired,
3158 1);
3159 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
3160 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
3161 EXPECT_TRUE(
3162 payments_client_->detected_values_in_upload_details() &
3163 CreditCardSaveManager::DetectedValue::USER_PROVIDED_EXPIRATION_DATE);
3164 }
3165
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_RequestExpirationDateIfExpirationDateInputIsTwoDigitAndExpired)3166 TEST_F(
3167 CreditCardSaveManagerTest,
3168 UploadCreditCard_RequestExpirationDateIfExpirationDateInputIsTwoDigitAndExpired) {
3169 #if defined(OS_IOS)
3170 // iOS should always provide a valid expiration date when attempting to
3171 // upload a Saved Card due to the Messages SaveCard modal. The manager
3172 // shouldn't handle expired dates.
3173 if ((base::FeatureList::IsEnabled(
3174 features::kAutofillSaveCardInfobarEditSupport) &&
3175 base::FeatureList::IsEnabled(kIOSInfobarUIReboot))) {
3176 return;
3177 }
3178 #endif
3179
3180 // Create, fill and submit an address form in order to establish a recent
3181 // profile which can be selected for the upload request.
3182 FormData address_form;
3183 test::CreateTestAddressFormData(&address_form);
3184 FormsSeen(std::vector<FormData>(1, address_form));
3185 ManuallyFillAddressForm("John", "Smith", "77401", "US", &address_form);
3186 FormSubmitted(address_form);
3187
3188 // Set up our credit card form data.
3189 FormData credit_card_form;
3190 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
3191 FormsSeen(std::vector<FormData>(1, credit_card_form));
3192
3193 // Edit the data with 2 digit year and submit.
3194 credit_card_form.fields[0].value = ASCIIToUTF16("John Smith");
3195 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
3196 credit_card_form.fields[2].value = ASCIIToUTF16("01");
3197 credit_card_form.fields[3].value = ASCIIToUTF16("10");
3198 credit_card_form.fields[4].value = ASCIIToUTF16("123");
3199
3200 base::HistogramTester histogram_tester;
3201 FormSubmitted(credit_card_form);
3202 // Verify that the correct histogram entry and DetectedValue for "Expiration
3203 // date explicitly requested" was logged.
3204 ExpectCardUploadDecision(
3205 histogram_tester,
3206 AutofillMetrics::USER_REQUESTED_TO_PROVIDE_EXPIRATION_DATE);
3207 histogram_tester.ExpectUniqueSample(
3208 "Autofill.SaveCardRequestExpirationDateReason",
3209 AutofillMetrics::SaveCardRequestExpirationDateReasonMetric::
3210 kExpirationDatePresentButExpired,
3211 1);
3212 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
3213 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
3214 EXPECT_TRUE(
3215 payments_client_->detected_values_in_upload_details() &
3216 CreditCardSaveManager::DetectedValue::USER_PROVIDED_EXPIRATION_DATE);
3217 }
3218
3219 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
3220 // permanently if the test doesn't apply to iOS flow.
3221 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_UploadDetailsFails)3222 TEST_F(CreditCardSaveManagerTest, UploadCreditCard_UploadDetailsFails) {
3223 // Anything other than "en-US" will cause GetUploadDetails to return a failure
3224 // response.
3225 credit_card_save_manager_->SetAppLocale("pt-BR");
3226
3227 // Create, fill and submit an address form in order to establish a recent
3228 // profile which can be selected for the upload request.
3229 FormData address_form;
3230 test::CreateTestAddressFormData(&address_form);
3231 FormsSeen(std::vector<FormData>(1, address_form));
3232 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
3233 FormSubmitted(address_form);
3234
3235 // Set up our credit card form data.
3236 FormData credit_card_form;
3237 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
3238 FormsSeen(std::vector<FormData>(1, credit_card_form));
3239
3240 // Edit the data, and submit.
3241 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
3242 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
3243 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
3244 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
3245 credit_card_form.fields[4].value = ASCIIToUTF16("123");
3246
3247 base::HistogramTester histogram_tester;
3248
3249 // The save prompt should be shown instead of doing an upload.
3250 FormSubmitted(credit_card_form);
3251 EXPECT_TRUE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
3252 EXPECT_FALSE(credit_card_save_manager_->CreditCardWasUploaded());
3253
3254 // Verify that the correct histogram entry (and only that) was logged.
3255 ExpectUniqueCardUploadDecision(
3256 histogram_tester,
3257 AutofillMetrics::UPLOAD_NOT_OFFERED_GET_UPLOAD_DETAILS_FAILED);
3258 // Verify that the correct UKM was logged.
3259 ExpectCardUploadDecisionUkm(
3260 AutofillMetrics::UPLOAD_NOT_OFFERED_GET_UPLOAD_DETAILS_FAILED);
3261 }
3262 #endif
3263
TEST_F(CreditCardSaveManagerTest,DuplicateMaskedCreditCard_NoUpload)3264 TEST_F(CreditCardSaveManagerTest, DuplicateMaskedCreditCard_NoUpload) {
3265 // Create, fill and submit an address form in order to establish a recent
3266 // profile which can be selected for the upload request.
3267 FormData address_form;
3268 test::CreateTestAddressFormData(&address_form);
3269 FormsSeen(std::vector<FormData>(1, address_form));
3270 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
3271 FormSubmitted(address_form);
3272
3273 // Add a masked credit card whose |TypeAndLastFourDigits| matches what we will
3274 // enter below.
3275 CreditCard credit_card(CreditCard::MASKED_SERVER_CARD, "a123");
3276 test::SetCreditCardInfo(&credit_card, "Flo Master", "1111",
3277 test::NextMonth().c_str(), test::NextYear().c_str(),
3278 "1");
3279 credit_card.SetNetworkForMaskedCard(kVisaCard);
3280 personal_data_.AddServerCreditCard(credit_card);
3281
3282 // Set up our credit card form data.
3283 FormData credit_card_form;
3284 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
3285 FormsSeen(std::vector<FormData>(1, credit_card_form));
3286
3287 // Edit the data, and submit.
3288 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
3289 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
3290 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
3291 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
3292 credit_card_form.fields[4].value = ASCIIToUTF16("123");
3293
3294 // Local save prompt should not be shown as there is alredy masked
3295 // card with same |TypeAndLastFourDigits|.
3296 FormSubmitted(credit_card_form);
3297 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
3298 EXPECT_FALSE(credit_card_save_manager_->CreditCardWasUploaded());
3299 }
3300
TEST_F(CreditCardSaveManagerTest,NothingIfNothingFound)3301 TEST_F(CreditCardSaveManagerTest, NothingIfNothingFound) {
3302 // Set up our credit card form data.
3303 FormData credit_card_form;
3304 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
3305 FormsSeen(std::vector<FormData>(1, credit_card_form));
3306
3307 // Edit the data, and submit.
3308 credit_card_form.fields[0].value = ASCIIToUTF16(""); // No name set
3309 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
3310 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
3311 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
3312 credit_card_form.fields[4].value = ASCIIToUTF16(""); // No CVC set
3313
3314 // Submit the form and check what detected_values for an upload save would be.
3315 FormSubmitted(credit_card_form);
3316 int detected_values = payments_client_->detected_values_in_upload_details();
3317 EXPECT_FALSE(detected_values & CreditCardSaveManager::DetectedValue::CVC);
3318 EXPECT_FALSE(detected_values &
3319 CreditCardSaveManager::DetectedValue::CARDHOLDER_NAME);
3320 EXPECT_FALSE(detected_values &
3321 CreditCardSaveManager::DetectedValue::ADDRESS_NAME);
3322 EXPECT_FALSE(detected_values &
3323 CreditCardSaveManager::DetectedValue::POSTAL_CODE);
3324 EXPECT_FALSE(detected_values &
3325 CreditCardSaveManager::DetectedValue::COUNTRY_CODE);
3326 EXPECT_FALSE(
3327 detected_values &
3328 CreditCardSaveManager::DetectedValue::HAS_GOOGLE_PAYMENTS_ACCOUNT);
3329 }
3330
TEST_F(CreditCardSaveManagerTest,DetectCvc)3331 TEST_F(CreditCardSaveManagerTest, DetectCvc) {
3332 // Set up our credit card form data.
3333 FormData credit_card_form;
3334 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
3335 FormsSeen(std::vector<FormData>(1, credit_card_form));
3336
3337 // Edit the data, and submit.
3338 credit_card_form.fields[0].value = ASCIIToUTF16(""); // No name set
3339 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
3340 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
3341 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
3342 credit_card_form.fields[4].value = ASCIIToUTF16("123");
3343
3344 // Submit the form and ensure the detected_values for an upload save contained
3345 // the expected bit.
3346 FormSubmitted(credit_card_form);
3347 int expected_detected_value = CreditCardSaveManager::DetectedValue::CVC;
3348 EXPECT_EQ(payments_client_->detected_values_in_upload_details() &
3349 expected_detected_value,
3350 expected_detected_value);
3351 }
3352
TEST_F(CreditCardSaveManagerTest,DetectCardholderName)3353 TEST_F(CreditCardSaveManagerTest, DetectCardholderName) {
3354 // Set up our credit card form data.
3355 FormData credit_card_form;
3356 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
3357 FormsSeen(std::vector<FormData>(1, credit_card_form));
3358
3359 // Edit the data, and submit.
3360 credit_card_form.fields[0].value = ASCIIToUTF16("John Smith");
3361 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
3362 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
3363 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
3364 credit_card_form.fields[4].value = ASCIIToUTF16(""); // No CVC set
3365
3366 // Submit the form and ensure the detected_values for an upload save contained
3367 // the expected bit.
3368 FormSubmitted(credit_card_form);
3369 int expected_detected_value =
3370 CreditCardSaveManager::DetectedValue::CARDHOLDER_NAME;
3371 EXPECT_EQ(payments_client_->detected_values_in_upload_details() &
3372 expected_detected_value,
3373 expected_detected_value);
3374 }
3375
TEST_F(CreditCardSaveManagerTest,DetectAddressName)3376 TEST_F(CreditCardSaveManagerTest, DetectAddressName) {
3377 // Set up a new address profile.
3378 AutofillProfile profile;
3379 profile.set_guid("00000000-0000-0000-0000-000000000200");
3380 profile.SetInfo(NAME_FULL, ASCIIToUTF16("John Smith"), "en-US");
3381 personal_data_.AddProfile(profile);
3382
3383 // Set up our credit card form data.
3384 FormData credit_card_form;
3385 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
3386 FormsSeen(std::vector<FormData>(1, credit_card_form));
3387
3388 // Edit the data, and submit.
3389 credit_card_form.fields[0].value = ASCIIToUTF16(""); // No name set
3390 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
3391 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
3392 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
3393 credit_card_form.fields[4].value = ASCIIToUTF16(""); // No CVC set
3394
3395 // Submit the form and ensure the detected_values for an upload save contained
3396 // the expected bit.
3397 FormSubmitted(credit_card_form);
3398 int expected_detected_value =
3399 CreditCardSaveManager::DetectedValue::ADDRESS_NAME;
3400 EXPECT_EQ(payments_client_->detected_values_in_upload_details() &
3401 expected_detected_value,
3402 expected_detected_value);
3403 }
3404
TEST_F(CreditCardSaveManagerTest,DetectCardholderAndAddressNameIfMatching)3405 TEST_F(CreditCardSaveManagerTest, DetectCardholderAndAddressNameIfMatching) {
3406 // Set up a new address profile.
3407 AutofillProfile profile;
3408 profile.set_guid("00000000-0000-0000-0000-000000000200");
3409 profile.SetInfo(NAME_FULL, ASCIIToUTF16("John Smith"), "en-US");
3410 personal_data_.AddProfile(profile);
3411
3412 // Set up our credit card form data.
3413 FormData credit_card_form;
3414 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
3415 FormsSeen(std::vector<FormData>(1, credit_card_form));
3416
3417 // Edit the data, and submit.
3418 credit_card_form.fields[0].value = ASCIIToUTF16("John Smith");
3419 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
3420 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
3421 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
3422 credit_card_form.fields[4].value = ASCIIToUTF16(""); // No CVC set
3423
3424 // Submit the form and ensure the detected_values for an upload save contained
3425 // the expected bits.
3426 FormSubmitted(credit_card_form);
3427 int expected_detected_values =
3428 CreditCardSaveManager::DetectedValue::CARDHOLDER_NAME |
3429 CreditCardSaveManager::DetectedValue::ADDRESS_NAME;
3430 EXPECT_EQ(payments_client_->detected_values_in_upload_details() &
3431 expected_detected_values,
3432 expected_detected_values);
3433 }
3434
TEST_F(CreditCardSaveManagerTest,DetectNoUniqueNameIfNamesConflict)3435 TEST_F(CreditCardSaveManagerTest, DetectNoUniqueNameIfNamesConflict) {
3436 // Set up a new address profile.
3437 AutofillProfile profile;
3438 profile.set_guid("00000000-0000-0000-0000-000000000200");
3439 profile.SetInfo(NAME_FULL, ASCIIToUTF16("John Smith"), "en-US");
3440 personal_data_.AddProfile(profile);
3441
3442 // Set up our credit card form data.
3443 FormData credit_card_form;
3444 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
3445 FormsSeen(std::vector<FormData>(1, credit_card_form));
3446
3447 // Edit the data, and submit.
3448 credit_card_form.fields[0].value = ASCIIToUTF16("Miles Prower"); // Conflict!
3449 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
3450 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
3451 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
3452 credit_card_form.fields[4].value = ASCIIToUTF16(""); // No CVC set
3453
3454 // Submit the form and check what detected_values for an upload save would be.
3455 FormSubmitted(credit_card_form);
3456 int detected_values = payments_client_->detected_values_in_upload_details();
3457 EXPECT_FALSE(detected_values &
3458 CreditCardSaveManager::DetectedValue::CARDHOLDER_NAME);
3459 EXPECT_FALSE(detected_values &
3460 CreditCardSaveManager::DetectedValue::ADDRESS_NAME);
3461 }
3462
TEST_F(CreditCardSaveManagerTest,DetectPostalCode)3463 TEST_F(CreditCardSaveManagerTest, DetectPostalCode) {
3464 // Set up a new address profile.
3465 AutofillProfile profile;
3466 profile.set_guid("00000000-0000-0000-0000-000000000200");
3467 profile.SetInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("94043"), "en-US");
3468 personal_data_.AddProfile(profile);
3469
3470 // Set up our credit card form data.
3471 FormData credit_card_form;
3472 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
3473 FormsSeen(std::vector<FormData>(1, credit_card_form));
3474
3475 // Edit the data, and submit.
3476 credit_card_form.fields[0].value = ASCIIToUTF16(""); // No name set
3477 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
3478 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
3479 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
3480 credit_card_form.fields[4].value = ASCIIToUTF16(""); // No CVC set
3481
3482 // Submit the form and ensure the detected_values for an upload save contained
3483 // the expected bit.
3484 FormSubmitted(credit_card_form);
3485 int expected_detected_value =
3486 CreditCardSaveManager::DetectedValue::POSTAL_CODE;
3487 EXPECT_EQ(payments_client_->detected_values_in_upload_details() &
3488 expected_detected_value,
3489 expected_detected_value);
3490 }
3491
TEST_F(CreditCardSaveManagerTest,DetectNoUniquePostalCodeIfZipsConflict)3492 TEST_F(CreditCardSaveManagerTest, DetectNoUniquePostalCodeIfZipsConflict) {
3493 // Set up two new address profiles with conflicting postal codes.
3494 AutofillProfile profile1;
3495 profile1.set_guid("00000000-0000-0000-0000-000000000200");
3496 profile1.SetInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("94043"), "en-US");
3497 personal_data_.AddProfile(profile1);
3498 AutofillProfile profile2;
3499 profile2.set_guid("00000000-0000-0000-0000-000000000201");
3500 profile2.SetInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("95051"), "en-US");
3501 personal_data_.AddProfile(profile2);
3502
3503 // Set up our credit card form data.
3504 FormData credit_card_form;
3505 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
3506 FormsSeen(std::vector<FormData>(1, credit_card_form));
3507
3508 // Edit the data, and submit.
3509 credit_card_form.fields[0].value = ASCIIToUTF16(""); // No name set
3510 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
3511 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
3512 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
3513 credit_card_form.fields[4].value = ASCIIToUTF16(""); // No CVC set
3514
3515 // Submit the form and check what detected_values for an upload save would be.
3516 FormSubmitted(credit_card_form);
3517 EXPECT_FALSE(payments_client_->detected_values_in_upload_details() &
3518 CreditCardSaveManager::DetectedValue::POSTAL_CODE);
3519 }
3520
TEST_F(CreditCardSaveManagerTest,DetectAddressLine)3521 TEST_F(CreditCardSaveManagerTest, DetectAddressLine) {
3522 // Set up a new address profile.
3523 AutofillProfile profile;
3524 profile.set_guid("00000000-0000-0000-0000-000000000200");
3525 profile.SetInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("123 Testing St."), "en-US");
3526 personal_data_.AddProfile(profile);
3527
3528 // Set up our credit card form data.
3529 FormData credit_card_form;
3530 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
3531 FormsSeen(std::vector<FormData>(1, credit_card_form));
3532
3533 // Edit the data, and submit.
3534 credit_card_form.fields[0].value = ASCIIToUTF16(""); // No name set
3535 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
3536 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
3537 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
3538 credit_card_form.fields[4].value = ASCIIToUTF16(""); // No CVC set
3539
3540 // Submit the form and ensure the detected_values for an upload save contained
3541 // the expected bit.
3542 FormSubmitted(credit_card_form);
3543 int expected_detected_value =
3544 CreditCardSaveManager::DetectedValue::ADDRESS_LINE;
3545 EXPECT_EQ(payments_client_->detected_values_in_upload_details() &
3546 expected_detected_value,
3547 expected_detected_value);
3548 }
3549
TEST_F(CreditCardSaveManagerTest,DetectLocality)3550 TEST_F(CreditCardSaveManagerTest, DetectLocality) {
3551 // Set up a new address profile.
3552 AutofillProfile profile;
3553 profile.set_guid("00000000-0000-0000-0000-000000000200");
3554 profile.SetInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("Mountain View"), "en-US");
3555 personal_data_.AddProfile(profile);
3556
3557 // Set up our credit card form data.
3558 FormData credit_card_form;
3559 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
3560 FormsSeen(std::vector<FormData>(1, credit_card_form));
3561
3562 // Edit the data, and submit.
3563 credit_card_form.fields[0].value = ASCIIToUTF16(""); // No name set
3564 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
3565 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
3566 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
3567 credit_card_form.fields[4].value = ASCIIToUTF16(""); // No CVC set
3568
3569 // Submit the form and ensure the detected_values for an upload save contained
3570 // the expected bit.
3571 FormSubmitted(credit_card_form);
3572 int expected_detected_value = CreditCardSaveManager::DetectedValue::LOCALITY;
3573 EXPECT_EQ(payments_client_->detected_values_in_upload_details() &
3574 expected_detected_value,
3575 expected_detected_value);
3576 }
3577
TEST_F(CreditCardSaveManagerTest,DetectAdministrativeArea)3578 TEST_F(CreditCardSaveManagerTest, DetectAdministrativeArea) {
3579 // Set up a new address profile.
3580 AutofillProfile profile;
3581 profile.set_guid("00000000-0000-0000-0000-000000000200");
3582 profile.SetInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("California"), "en-US");
3583 personal_data_.AddProfile(profile);
3584
3585 // Set up our credit card form data.
3586 FormData credit_card_form;
3587 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
3588 FormsSeen(std::vector<FormData>(1, credit_card_form));
3589
3590 // Edit the data, and submit.
3591 credit_card_form.fields[0].value = ASCIIToUTF16(""); // No name set
3592 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
3593 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
3594 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
3595 credit_card_form.fields[4].value = ASCIIToUTF16(""); // No CVC set
3596
3597 // Submit the form and ensure the detected_values for an upload save contained
3598 // the expected bit.
3599 FormSubmitted(credit_card_form);
3600 int expected_detected_value =
3601 CreditCardSaveManager::DetectedValue::ADMINISTRATIVE_AREA;
3602 EXPECT_EQ(payments_client_->detected_values_in_upload_details() &
3603 expected_detected_value,
3604 expected_detected_value);
3605 }
3606
TEST_F(CreditCardSaveManagerTest,DetectCountryCode)3607 TEST_F(CreditCardSaveManagerTest, DetectCountryCode) {
3608 // Set up a new address profile.
3609 AutofillProfile profile;
3610 profile.set_guid("00000000-0000-0000-0000-000000000200");
3611 profile.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"), "en-US");
3612 personal_data_.AddProfile(profile);
3613
3614 // Set up our credit card form data.
3615 FormData credit_card_form;
3616 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
3617 FormsSeen(std::vector<FormData>(1, credit_card_form));
3618
3619 // Edit the data, and submit.
3620 credit_card_form.fields[0].value = ASCIIToUTF16(""); // No name set
3621 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
3622 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
3623 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
3624 credit_card_form.fields[4].value = ASCIIToUTF16(""); // No CVC set
3625
3626 // Submit the form and ensure the detected_values for an upload save contained
3627 // the expected bit.
3628 FormSubmitted(credit_card_form);
3629 int expected_detected_value =
3630 CreditCardSaveManager::DetectedValue::COUNTRY_CODE;
3631 EXPECT_EQ(payments_client_->detected_values_in_upload_details() &
3632 expected_detected_value,
3633 expected_detected_value);
3634 }
3635
TEST_F(CreditCardSaveManagerTest,DetectHasGooglePaymentAccount)3636 TEST_F(CreditCardSaveManagerTest, DetectHasGooglePaymentAccount) {
3637 // Set the billing_customer_number to designate existence of a Payments
3638 // account.
3639 personal_data_.SetPaymentsCustomerData(
3640 std::make_unique<PaymentsCustomerData>(/*customer_id=*/"123456"));
3641
3642 // Set up our credit card form data.
3643 FormData credit_card_form;
3644 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
3645 FormsSeen(std::vector<FormData>(1, credit_card_form));
3646
3647 // Edit the data, and submit.
3648 credit_card_form.fields[0].value = ASCIIToUTF16(""); // No name set
3649 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
3650 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
3651 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
3652 credit_card_form.fields[4].value = ASCIIToUTF16(""); // No CVC set
3653
3654 // Submit the form and ensure the detected_values for an upload save contained
3655 // the expected bit.
3656 FormSubmitted(credit_card_form);
3657 int expected_detected_value =
3658 CreditCardSaveManager::DetectedValue::HAS_GOOGLE_PAYMENTS_ACCOUNT;
3659 EXPECT_EQ(payments_client_->detected_values_in_upload_details() &
3660 expected_detected_value,
3661 expected_detected_value);
3662 }
3663
TEST_F(CreditCardSaveManagerTest,DetectEverythingAtOnce)3664 TEST_F(CreditCardSaveManagerTest, DetectEverythingAtOnce) {
3665 // Set up a new address profile.
3666 AutofillProfile profile;
3667 profile.set_guid("00000000-0000-0000-0000-000000000200");
3668 profile.SetInfo(NAME_FULL, ASCIIToUTF16("John Smith"), "en-US");
3669 profile.SetInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("123 Testing St."), "en-US");
3670 profile.SetInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("Mountain View"), "en-US");
3671 profile.SetInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("California"), "en-US");
3672 profile.SetInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("94043"), "en-US");
3673 profile.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"), "en-US");
3674 personal_data_.AddProfile(profile);
3675
3676 // Set up our credit card form data.
3677 FormData credit_card_form;
3678 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
3679 FormsSeen(std::vector<FormData>(1, credit_card_form));
3680
3681 // Edit the data, and submit.
3682 credit_card_form.fields[0].value = ASCIIToUTF16("John Smith");
3683 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
3684 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
3685 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
3686 credit_card_form.fields[4].value = ASCIIToUTF16("123");
3687
3688 // Submit the form and ensure the detected_values for an upload save contained
3689 // the expected bits.
3690 FormSubmitted(credit_card_form);
3691 int expected_detected_values =
3692 CreditCardSaveManager::DetectedValue::CVC |
3693 CreditCardSaveManager::DetectedValue::CARDHOLDER_NAME |
3694 CreditCardSaveManager::DetectedValue::ADDRESS_NAME |
3695 CreditCardSaveManager::DetectedValue::ADDRESS_LINE |
3696 CreditCardSaveManager::DetectedValue::LOCALITY |
3697 CreditCardSaveManager::DetectedValue::ADMINISTRATIVE_AREA |
3698 CreditCardSaveManager::DetectedValue::POSTAL_CODE |
3699 CreditCardSaveManager::DetectedValue::COUNTRY_CODE;
3700 EXPECT_EQ(payments_client_->detected_values_in_upload_details() &
3701 expected_detected_values,
3702 expected_detected_values);
3703 }
3704
TEST_F(CreditCardSaveManagerTest,DetectSubsetOfPossibleFields)3705 TEST_F(CreditCardSaveManagerTest, DetectSubsetOfPossibleFields) {
3706 // Set up a new address profile, taking out address line and state.
3707 AutofillProfile profile;
3708 profile.set_guid("00000000-0000-0000-0000-000000000200");
3709 profile.SetInfo(NAME_FULL, ASCIIToUTF16("John Smith"), "en-US");
3710 profile.SetInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("Mountain View"), "en-US");
3711 profile.SetInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("94043"), "en-US");
3712 profile.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"), "en-US");
3713 personal_data_.AddProfile(profile);
3714
3715 // Set up our credit card form data.
3716 FormData credit_card_form;
3717 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
3718 FormsSeen(std::vector<FormData>(1, credit_card_form));
3719
3720 // Edit the data, and submit.
3721 credit_card_form.fields[0].value = ASCIIToUTF16("Miles Prower"); // Conflict!
3722 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
3723 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
3724 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
3725 credit_card_form.fields[4].value = ASCIIToUTF16("123");
3726
3727 // Submit the form and ensure the detected_values for an upload save contained
3728 // the expected bits.
3729 FormSubmitted(credit_card_form);
3730 int expected_detected_values =
3731 CreditCardSaveManager::DetectedValue::CVC |
3732 CreditCardSaveManager::DetectedValue::LOCALITY |
3733 CreditCardSaveManager::DetectedValue::POSTAL_CODE |
3734 CreditCardSaveManager::DetectedValue::COUNTRY_CODE;
3735 EXPECT_EQ(payments_client_->detected_values_in_upload_details() &
3736 expected_detected_values,
3737 expected_detected_values);
3738 }
3739
3740 // This test checks that ADDRESS_LINE, LOCALITY, ADMINISTRATIVE_AREA, and
3741 // COUNTRY_CODE don't care about possible conflicts or consistency and are
3742 // populated if even one address profile contains it.
TEST_F(CreditCardSaveManagerTest,DetectAddressComponentsAcrossProfiles)3743 TEST_F(CreditCardSaveManagerTest, DetectAddressComponentsAcrossProfiles) {
3744 // Set up four new address profiles, each with a different address component.
3745 AutofillProfile profile1;
3746 profile1.set_guid("00000000-0000-0000-0000-000000000200");
3747 profile1.SetInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("123 Testing St."),
3748 "en-US");
3749 personal_data_.AddProfile(profile1);
3750 AutofillProfile profile2;
3751 profile2.set_guid("00000000-0000-0000-0000-000000000201");
3752 profile2.SetInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("Mountain View"), "en-US");
3753 personal_data_.AddProfile(profile2);
3754 AutofillProfile profile3;
3755 profile3.set_guid("00000000-0000-0000-0000-000000000202");
3756 profile3.SetInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("California"), "en-US");
3757 personal_data_.AddProfile(profile3);
3758 AutofillProfile profile4;
3759 profile4.set_guid("00000000-0000-0000-0000-000000000203");
3760 profile4.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"), "en-US");
3761 personal_data_.AddProfile(profile4);
3762
3763 // Set up our credit card form data.
3764 FormData credit_card_form;
3765 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
3766 FormsSeen(std::vector<FormData>(1, credit_card_form));
3767
3768 // Edit the data, and submit.
3769 credit_card_form.fields[0].value = ASCIIToUTF16(""); // No name set
3770 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
3771 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
3772 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
3773 credit_card_form.fields[4].value = ASCIIToUTF16(""); // No CVC set
3774
3775 // Submit the form and ensure the detected_values for an upload save contained
3776 // the expected bits.
3777 FormSubmitted(credit_card_form);
3778 int expected_detected_values =
3779 CreditCardSaveManager::DetectedValue::ADDRESS_LINE |
3780 CreditCardSaveManager::DetectedValue::LOCALITY |
3781 CreditCardSaveManager::DetectedValue::ADMINISTRATIVE_AREA |
3782 CreditCardSaveManager::DetectedValue::COUNTRY_CODE;
3783 EXPECT_EQ(payments_client_->detected_values_in_upload_details() &
3784 expected_detected_values,
3785 expected_detected_values);
3786 }
3787
3788 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
3789 // permanently if the test doesn't apply to iOS flow.
3790 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_LogAdditionalErrorsWithUploadDetailsFailure)3791 TEST_F(CreditCardSaveManagerTest,
3792 UploadCreditCard_LogAdditionalErrorsWithUploadDetailsFailure) {
3793 // Anything other than "en-US" will cause GetUploadDetails to return a failure
3794 // response.
3795 credit_card_save_manager_->SetAppLocale("pt-BR");
3796
3797 // Set up a new address profile without a name or postal code.
3798 AutofillProfile profile;
3799 profile.set_guid("00000000-0000-0000-0000-000000000200");
3800 profile.SetInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("123 Testing St."), "en-US");
3801 profile.SetInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("Mountain View"), "en-US");
3802 profile.SetInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("California"), "en-US");
3803 profile.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"), "en-US");
3804 personal_data_.AddProfile(profile);
3805
3806 // Set up our credit card form data.
3807 FormData credit_card_form;
3808 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
3809 FormsSeen(std::vector<FormData>(1, credit_card_form));
3810
3811 // Edit the data, and submit.
3812 credit_card_form.fields[0].value = ASCIIToUTF16(""); // No name!
3813 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
3814 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
3815 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
3816 credit_card_form.fields[4].value = ASCIIToUTF16(""); // No CVC!
3817
3818 base::HistogramTester histogram_tester;
3819 FormSubmitted(credit_card_form);
3820
3821 // Verify that the correct histogram entries were logged.
3822 ExpectCardUploadDecision(
3823 histogram_tester,
3824 AutofillMetrics::UPLOAD_NOT_OFFERED_GET_UPLOAD_DETAILS_FAILED);
3825 ExpectCardUploadDecision(histogram_tester,
3826 AutofillMetrics::UPLOAD_NOT_OFFERED_NO_NAME);
3827 ExpectCardUploadDecision(histogram_tester,
3828 AutofillMetrics::UPLOAD_NOT_OFFERED_NO_ZIP_CODE);
3829 ExpectCardUploadDecision(histogram_tester,
3830 AutofillMetrics::CVC_VALUE_NOT_FOUND);
3831 ExpectCardUploadDecision(
3832 histogram_tester,
3833 AutofillMetrics::USER_REQUESTED_TO_PROVIDE_CARDHOLDER_NAME);
3834 // Verify that the correct UKM was logged.
3835 int upload_decision =
3836 AutofillMetrics::UPLOAD_NOT_OFFERED_GET_UPLOAD_DETAILS_FAILED |
3837 AutofillMetrics::UPLOAD_NOT_OFFERED_NO_NAME |
3838 AutofillMetrics::UPLOAD_NOT_OFFERED_NO_ZIP_CODE |
3839 AutofillMetrics::CVC_VALUE_NOT_FOUND |
3840 AutofillMetrics::USER_REQUESTED_TO_PROVIDE_CARDHOLDER_NAME;
3841 ExpectMetric(UkmCardUploadDecisionType::kUploadDecisionName,
3842 UkmCardUploadDecisionType::kEntryName, upload_decision,
3843 1 /* expected_num_matching_entries */);
3844 }
3845 #endif
3846
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_ShouldOfferLocalSaveIfEverythingDetectedAndPaymentsDeclines)3847 TEST_F(
3848 CreditCardSaveManagerTest,
3849 UploadCreditCard_ShouldOfferLocalSaveIfEverythingDetectedAndPaymentsDeclines) {
3850 // Anything other than "en-US" will cause GetUploadDetails to return a failure
3851 // response.
3852 credit_card_save_manager_->SetAppLocale("pt-BR");
3853
3854 // Set up a new address profile.
3855 AutofillProfile profile;
3856 profile.set_guid("00000000-0000-0000-0000-000000000200");
3857 profile.SetInfo(NAME_FULL, ASCIIToUTF16("John Smith"), "en-US");
3858 profile.SetInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("123 Testing St."), "en-US");
3859 profile.SetInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("Mountain View"), "en-US");
3860 profile.SetInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("California"), "en-US");
3861 profile.SetInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("94043"), "en-US");
3862 profile.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"), "en-US");
3863 personal_data_.AddProfile(profile);
3864
3865 // Set up our credit card form data.
3866 FormData credit_card_form;
3867 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
3868 FormsSeen(std::vector<FormData>(1, credit_card_form));
3869
3870 // Edit the data, and submit.
3871 credit_card_form.fields[0].value = ASCIIToUTF16("John Smith");
3872 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
3873 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
3874 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
3875 credit_card_form.fields[4].value = ASCIIToUTF16("123");
3876
3877 base::HistogramTester histogram_tester;
3878
3879 // Because Payments rejects the offer to upload save but CVC + name + address
3880 // were all found, the local save prompt should be shown instead of the upload
3881 // prompt.
3882 FormSubmitted(credit_card_form);
3883 EXPECT_TRUE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
3884 EXPECT_FALSE(credit_card_save_manager_->CreditCardWasUploaded());
3885 }
3886
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_ShouldOfferLocalSaveIfEverythingDetectedAndPaymentsDeclines_WithFirstAndLastName)3887 TEST_F(
3888 CreditCardSaveManagerTest,
3889 UploadCreditCard_ShouldOfferLocalSaveIfEverythingDetectedAndPaymentsDeclines_WithFirstAndLastName) {
3890 // Anything other than "en-US" will cause GetUploadDetails to return a failure
3891 // response.
3892 credit_card_save_manager_->SetAppLocale("pt-BR");
3893
3894 // Set up a new address profile.
3895 AutofillProfile profile;
3896 profile.set_guid("00000000-0000-0000-0000-000000000200");
3897 profile.SetInfo(NAME_FULL, ASCIIToUTF16("John Smith"), "en-US");
3898 profile.SetInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("123 Testing St."), "en-US");
3899 profile.SetInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("Mountain View"), "en-US");
3900 profile.SetInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("California"), "en-US");
3901 profile.SetInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("94043"), "en-US");
3902 profile.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"), "en-US");
3903 personal_data_.AddProfile(profile);
3904
3905 // Set up our credit card form data with credit card first and last name
3906 // fields.
3907 FormData credit_card_form;
3908 CreateTestCreditCardFormData(&credit_card_form,
3909 CreditCardFormOptions().with_split_names(true));
3910 FormsSeen(std::vector<FormData>(1, credit_card_form));
3911
3912 // Edit the data, and submit.
3913 credit_card_form.fields[0].value = ASCIIToUTF16("John");
3914 credit_card_form.fields[1].value = ASCIIToUTF16("Smith");
3915 credit_card_form.fields[2].value = ASCIIToUTF16("4111111111111111");
3916 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextMonth());
3917 credit_card_form.fields[4].value = ASCIIToUTF16(test::NextYear());
3918 credit_card_form.fields[5].value = ASCIIToUTF16("123");
3919
3920 base::HistogramTester histogram_tester;
3921
3922 // Because Payments rejects the offer to upload save but CVC + name + address
3923 // were all found, the local save prompt should be shown instead of the upload
3924 // prompt.
3925 FormSubmitted(credit_card_form);
3926 EXPECT_TRUE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
3927 EXPECT_FALSE(credit_card_save_manager_->CreditCardWasUploaded());
3928
3929 histogram_tester.ExpectTotalCount(
3930 "Autofill.SaveCardWithFirstAndLastNameOffered.Local", 1);
3931 histogram_tester.ExpectTotalCount(
3932 "Autofill.SaveCardWithFirstAndLastNameOffered.Server", 0);
3933 histogram_tester.ExpectTotalCount(
3934 "Autofill.SaveCardWithFirstAndLastNameComplete.Server", 0);
3935 }
3936
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_ShouldNotOfferLocalSaveIfSomethingNotDetectedAndPaymentsDeclines)3937 TEST_F(
3938 CreditCardSaveManagerTest,
3939 UploadCreditCard_ShouldNotOfferLocalSaveIfSomethingNotDetectedAndPaymentsDeclines) {
3940 // Anything other than "en-US" will cause GetUploadDetails to return a failure
3941 // response.
3942 credit_card_save_manager_->SetAppLocale("pt-BR");
3943
3944 // Set up a new address profile without a name or postal code.
3945 AutofillProfile profile;
3946 profile.set_guid("00000000-0000-0000-0000-000000000200");
3947 profile.SetInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("123 Testing St."), "en-US");
3948 profile.SetInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("Mountain View"), "en-US");
3949 profile.SetInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("California"), "en-US");
3950 profile.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"), "en-US");
3951 personal_data_.AddProfile(profile);
3952
3953 // Set up our credit card form data.
3954 FormData credit_card_form;
3955 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
3956 FormsSeen(std::vector<FormData>(1, credit_card_form));
3957
3958 // Edit the data, and submit.
3959 credit_card_form.fields[0].value = ASCIIToUTF16(""); // No name!
3960 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
3961 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
3962 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
3963 credit_card_form.fields[4].value = ASCIIToUTF16(""); // No CVC!
3964
3965 base::HistogramTester histogram_tester;
3966
3967 // Because Payments rejects the offer to upload save but not all of CVC + name
3968 // + address were detected, the local save prompt should not be shown either.
3969 FormSubmitted(credit_card_form);
3970 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
3971 EXPECT_FALSE(credit_card_save_manager_->CreditCardWasUploaded());
3972 }
3973
3974 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
3975 // permanently if the test doesn't apply to iOS flow.
3976 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_PaymentsDecidesOfferToSaveIfNoCvc)3977 TEST_F(CreditCardSaveManagerTest,
3978 UploadCreditCard_PaymentsDecidesOfferToSaveIfNoCvc) {
3979 // Create, fill and submit an address form in order to establish a recent
3980 // profile which can be selected for the upload request.
3981 FormData address_form;
3982 test::CreateTestAddressFormData(&address_form);
3983 FormsSeen(std::vector<FormData>(1, address_form));
3984 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
3985 FormSubmitted(address_form);
3986
3987 // Set up our credit card form data.
3988 FormData credit_card_form;
3989 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
3990 FormsSeen(std::vector<FormData>(1, credit_card_form));
3991
3992 // Edit the data, and submit.
3993 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
3994 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
3995 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
3996 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
3997 credit_card_form.fields[4].value = ASCIIToUTF16(""); // No CVC!
3998
3999 base::HistogramTester histogram_tester;
4000
4001 // Payments should be asked whether upload save can be offered.
4002 // (Unit tests assume they reply yes and save is successful.)
4003 FormSubmitted(credit_card_form);
4004 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
4005 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
4006
4007 // Verify that the correct histogram entries were logged.
4008 ExpectCardUploadDecision(histogram_tester, AutofillMetrics::UPLOAD_OFFERED);
4009 ExpectCardUploadDecision(histogram_tester,
4010 AutofillMetrics::CVC_VALUE_NOT_FOUND);
4011 // Verify that the correct UKM was logged.
4012 ExpectMetric(
4013 UkmCardUploadDecisionType::kUploadDecisionName,
4014 UkmCardUploadDecisionType::kEntryName,
4015 AutofillMetrics::UPLOAD_OFFERED | AutofillMetrics::CVC_VALUE_NOT_FOUND,
4016 1 /* expected_num_matching_entries */);
4017 }
4018 #endif
4019
4020 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
4021 // permanently if the test doesn't apply to iOS flow.
4022 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_PaymentsDecidesOfferToSaveIfNoName)4023 TEST_F(CreditCardSaveManagerTest,
4024 UploadCreditCard_PaymentsDecidesOfferToSaveIfNoName) {
4025 // Create, fill and submit an address form in order to establish a recent
4026 // profile which can be selected for the upload request.
4027 FormData address_form;
4028 test::CreateTestAddressFormData(&address_form);
4029 FormsSeen(std::vector<FormData>(1, address_form));
4030 // But omit the name:
4031 ManuallyFillAddressForm("", "", "77401", "US", &address_form);
4032 FormSubmitted(address_form);
4033
4034 // Set up our credit card form data.
4035 FormData credit_card_form;
4036 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
4037 FormsSeen(std::vector<FormData>(1, credit_card_form));
4038
4039 // Edit the data, and submit.
4040 credit_card_form.fields[0].value = ASCIIToUTF16(""); // No name!
4041 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
4042 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
4043 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
4044 credit_card_form.fields[4].value = ASCIIToUTF16("123");
4045
4046 base::HistogramTester histogram_tester;
4047
4048 // Payments should be asked whether upload save can be offered.
4049 // (Unit tests assume they reply yes and save is successful.)
4050 FormSubmitted(credit_card_form);
4051 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
4052 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
4053
4054 // Verify that the correct histogram entries were logged.
4055 ExpectCardUploadDecision(histogram_tester, AutofillMetrics::UPLOAD_OFFERED);
4056 ExpectCardUploadDecision(histogram_tester,
4057 AutofillMetrics::UPLOAD_NOT_OFFERED_NO_NAME);
4058 ExpectCardUploadDecision(
4059 histogram_tester,
4060 AutofillMetrics::USER_REQUESTED_TO_PROVIDE_CARDHOLDER_NAME);
4061 // Verify that the correct UKM was logged.
4062 int upload_decision =
4063 AutofillMetrics::UPLOAD_OFFERED |
4064 AutofillMetrics::UPLOAD_NOT_OFFERED_NO_NAME |
4065 AutofillMetrics::USER_REQUESTED_TO_PROVIDE_CARDHOLDER_NAME;
4066 ExpectMetric(UkmCardUploadDecisionType::kUploadDecisionName,
4067 UkmCardUploadDecisionType::kEntryName, upload_decision,
4068 1 /* expected_num_matching_entries */);
4069 }
4070 #endif
4071
4072 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
4073 // permanently if the test doesn't apply to iOS flow.
4074 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_PaymentsDecidesOfferToSaveIfConflictingNames)4075 TEST_F(CreditCardSaveManagerTest,
4076 UploadCreditCard_PaymentsDecidesOfferToSaveIfConflictingNames) {
4077 // Create, fill and submit an address form in order to establish a recent
4078 // profile which can be selected for the upload request.
4079 FormData address_form;
4080 test::CreateTestAddressFormData(&address_form);
4081 FormsSeen(std::vector<FormData>(1, address_form));
4082 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
4083 FormSubmitted(address_form);
4084
4085 // Set up our credit card form data.
4086 FormData credit_card_form;
4087 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
4088 FormsSeen(std::vector<FormData>(1, credit_card_form));
4089
4090 // Edit the data, and submit.
4091 credit_card_form.fields[0].value = ASCIIToUTF16("Miles Prower"); // Conflict!
4092 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
4093 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
4094 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
4095 credit_card_form.fields[4].value = ASCIIToUTF16("123");
4096
4097 base::HistogramTester histogram_tester;
4098
4099 // Payments should be asked whether upload save can be offered.
4100 // (Unit tests assume they reply yes and save is successful.)
4101 FormSubmitted(credit_card_form);
4102 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
4103 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
4104
4105 // Verify that the correct histogram entries were logged.
4106 ExpectCardUploadDecision(histogram_tester, AutofillMetrics::UPLOAD_OFFERED);
4107 ExpectCardUploadDecision(
4108 histogram_tester, AutofillMetrics::UPLOAD_NOT_OFFERED_CONFLICTING_NAMES);
4109 ExpectCardUploadDecision(
4110 histogram_tester,
4111 AutofillMetrics::USER_REQUESTED_TO_PROVIDE_CARDHOLDER_NAME);
4112 // Verify that the correct UKM was logged.
4113 int upload_decision =
4114 AutofillMetrics::UPLOAD_OFFERED |
4115 AutofillMetrics::UPLOAD_NOT_OFFERED_CONFLICTING_NAMES |
4116 AutofillMetrics::USER_REQUESTED_TO_PROVIDE_CARDHOLDER_NAME;
4117 ExpectMetric(UkmCardUploadDecisionType::kUploadDecisionName,
4118 UkmCardUploadDecisionType::kEntryName, upload_decision,
4119 1 /* expected_num_matching_entries */);
4120 }
4121 #endif
4122
4123 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
4124 // permanently if the test doesn't apply to iOS flow.
4125 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_PaymentsDecidesOfferToSaveIfNoZip)4126 TEST_F(CreditCardSaveManagerTest,
4127 UploadCreditCard_PaymentsDecidesOfferToSaveIfNoZip) {
4128 // Set up a new address profile without a postal code.
4129 AutofillProfile profile;
4130 profile.set_guid("00000000-0000-0000-0000-000000000200");
4131 profile.SetInfo(NAME_FULL, ASCIIToUTF16("Flo Master"), "en-US");
4132 profile.SetInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("123 Testing St."), "en-US");
4133 profile.SetInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("Mountain View"), "en-US");
4134 profile.SetInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("California"), "en-US");
4135 profile.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"), "en-US");
4136 personal_data_.AddProfile(profile);
4137
4138 // Set up our credit card form data.
4139 FormData credit_card_form;
4140 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
4141 FormsSeen(std::vector<FormData>(1, credit_card_form));
4142
4143 // Edit the data, and submit.
4144 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
4145 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
4146 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
4147 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
4148 credit_card_form.fields[4].value = ASCIIToUTF16("123");
4149
4150 base::HistogramTester histogram_tester;
4151
4152 // Payments should be asked whether upload save can be offered.
4153 // (Unit tests assume they reply yes and save is successful.)
4154 FormSubmitted(credit_card_form);
4155 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
4156 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
4157
4158 // Verify that the correct histogram entries were logged.
4159 ExpectCardUploadDecision(histogram_tester, AutofillMetrics::UPLOAD_OFFERED);
4160 ExpectCardUploadDecision(histogram_tester,
4161 AutofillMetrics::UPLOAD_NOT_OFFERED_NO_ZIP_CODE);
4162 // Verify that the correct UKM was logged.
4163 ExpectMetric(UkmCardUploadDecisionType::kUploadDecisionName,
4164 UkmCardUploadDecisionType::kEntryName,
4165 AutofillMetrics::UPLOAD_OFFERED |
4166 AutofillMetrics::UPLOAD_NOT_OFFERED_NO_ZIP_CODE,
4167 1 /* expected_num_matching_entries */);
4168 }
4169 #endif
4170
4171 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
4172 // permanently if the test doesn't apply to iOS flow.
4173 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_PaymentsDecidesOfferToSaveIfConflictingZips)4174 TEST_F(CreditCardSaveManagerTest,
4175 UploadCreditCard_PaymentsDecidesOfferToSaveIfConflictingZips) {
4176 // Set up two new address profiles with conflicting postal codes.
4177 AutofillProfile profile1;
4178 profile1.set_guid("00000000-0000-0000-0000-000000000200");
4179 profile1.SetInfo(NAME_FULL, ASCIIToUTF16("Flo Master"), "en-US");
4180 profile1.SetInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("123 Testing St."),
4181 "en-US");
4182 profile1.SetInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("Mountain View"), "en-US");
4183 profile1.SetInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("California"), "en-US");
4184 profile1.SetInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("94043"), "en-US");
4185 profile1.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"), "en-US");
4186 personal_data_.AddProfile(profile1);
4187 AutofillProfile profile2;
4188 profile2.set_guid("00000000-0000-0000-0000-000000000201");
4189 profile2.SetInfo(NAME_FULL, ASCIIToUTF16("Flo Master"), "en-US");
4190 profile2.SetInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("234 Other Place"),
4191 "en-US");
4192 profile2.SetInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("Fake City"), "en-US");
4193 profile2.SetInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("Stateland"), "en-US");
4194 profile2.SetInfo(ADDRESS_HOME_ZIP, ASCIIToUTF16("12345"), "en-US");
4195 profile2.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"), "en-US");
4196 personal_data_.AddProfile(profile2);
4197
4198 // Set up our credit card form data.
4199 FormData credit_card_form;
4200 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
4201 FormsSeen(std::vector<FormData>(1, credit_card_form));
4202
4203 // Edit the data, and submit.
4204 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
4205 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
4206 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
4207 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
4208 credit_card_form.fields[4].value = ASCIIToUTF16("123");
4209
4210 base::HistogramTester histogram_tester;
4211
4212 // Payments should be asked whether upload save can be offered.
4213 // (Unit tests assume they reply yes and save is successful.)
4214 FormSubmitted(credit_card_form);
4215 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
4216 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
4217
4218 // Verify that the correct histogram entries were logged.
4219 ExpectCardUploadDecision(histogram_tester, AutofillMetrics::UPLOAD_OFFERED);
4220 ExpectCardUploadDecision(
4221 histogram_tester, AutofillMetrics::UPLOAD_NOT_OFFERED_CONFLICTING_ZIPS);
4222 // Verify that the correct UKM was logged.
4223 ExpectMetric(UkmCardUploadDecisionType::kUploadDecisionName,
4224 UkmCardUploadDecisionType::kEntryName,
4225 AutofillMetrics::UPLOAD_OFFERED |
4226 AutofillMetrics::UPLOAD_NOT_OFFERED_CONFLICTING_ZIPS,
4227 1 /* expected_num_matching_entries */);
4228 }
4229 #endif
4230
4231 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
4232 // permanently if the test doesn't apply to iOS flow.
4233 #if !defined(OS_IOS)
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_PaymentsDecidesOfferToSaveIfNothingFound)4234 TEST_F(CreditCardSaveManagerTest,
4235 UploadCreditCard_PaymentsDecidesOfferToSaveIfNothingFound) {
4236 // Set up a new address profile without a name or postal code.
4237 AutofillProfile profile;
4238 profile.set_guid("00000000-0000-0000-0000-000000000200");
4239 profile.SetInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16("123 Testing St."), "en-US");
4240 profile.SetInfo(ADDRESS_HOME_CITY, ASCIIToUTF16("Mountain View"), "en-US");
4241 profile.SetInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("California"), "en-US");
4242 profile.SetInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"), "en-US");
4243 personal_data_.AddProfile(profile);
4244
4245 // Set up our credit card form data.
4246 FormData credit_card_form;
4247 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
4248 FormsSeen(std::vector<FormData>(1, credit_card_form));
4249
4250 // Edit the data, and submit.
4251 credit_card_form.fields[0].value = ASCIIToUTF16(""); // No name!
4252 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
4253 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
4254 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
4255 credit_card_form.fields[4].value = ASCIIToUTF16(""); // No CVC!
4256
4257 base::HistogramTester histogram_tester;
4258
4259 // Payments should be asked whether upload save can be offered.
4260 // (Unit tests assume they reply yes and save is successful.)
4261 FormSubmitted(credit_card_form);
4262 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
4263 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
4264
4265 // Verify that the correct histogram entries were logged.
4266 ExpectCardUploadDecision(histogram_tester, AutofillMetrics::UPLOAD_OFFERED);
4267 ExpectCardUploadDecision(histogram_tester,
4268 AutofillMetrics::CVC_VALUE_NOT_FOUND);
4269 ExpectCardUploadDecision(histogram_tester,
4270 AutofillMetrics::UPLOAD_NOT_OFFERED_NO_NAME);
4271 ExpectCardUploadDecision(histogram_tester,
4272 AutofillMetrics::UPLOAD_NOT_OFFERED_NO_ZIP_CODE);
4273 ExpectCardUploadDecision(
4274 histogram_tester,
4275 AutofillMetrics::USER_REQUESTED_TO_PROVIDE_CARDHOLDER_NAME);
4276 // Verify that the correct UKM was logged.
4277 int upload_decision =
4278 AutofillMetrics::UPLOAD_OFFERED | AutofillMetrics::CVC_VALUE_NOT_FOUND |
4279 AutofillMetrics::UPLOAD_NOT_OFFERED_NO_NAME |
4280 AutofillMetrics::UPLOAD_NOT_OFFERED_NO_ZIP_CODE |
4281 AutofillMetrics::USER_REQUESTED_TO_PROVIDE_CARDHOLDER_NAME;
4282 ExpectMetric(UkmCardUploadDecisionType::kUploadDecisionName,
4283 UkmCardUploadDecisionType::kEntryName, upload_decision,
4284 1 /* expected_num_matching_entries */);
4285 }
4286 #endif
4287
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_UploadOfLocalCard)4288 TEST_F(CreditCardSaveManagerTest, UploadCreditCard_UploadOfLocalCard) {
4289 // Add a local credit card whose |TypeAndLastFourDigits| matches what we will
4290 // enter below.
4291 CreditCard local_card;
4292 test::SetCreditCardInfo(&local_card, "Flo Master", "4111111111111111",
4293 test::NextMonth().c_str(), test::NextYear().c_str(),
4294 "1");
4295 local_card.set_record_type(CreditCard::LOCAL_CARD);
4296 personal_data_.AddCreditCard(local_card);
4297
4298 // Create, fill and submit an address form in order to establish a recent
4299 // profile which can be selected for the upload request.
4300 FormData address_form;
4301 test::CreateTestAddressFormData(&address_form);
4302 FormsSeen(std::vector<FormData>(1, address_form));
4303 ExpectUniqueFillableFormParsedUkm();
4304
4305 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
4306 FormSubmitted(address_form);
4307
4308 // Set up our credit card form data.
4309 FormData credit_card_form;
4310 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
4311 FormsSeen(std::vector<FormData>(1, credit_card_form));
4312 ExpectFillableFormParsedUkm(2 /* num_fillable_forms_parsed */);
4313
4314 // Edit the data, and submit.
4315 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
4316 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
4317 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
4318 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
4319 credit_card_form.fields[4].value = ASCIIToUTF16("123");
4320
4321 base::HistogramTester histogram_tester;
4322
4323 FormSubmitted(credit_card_form);
4324 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
4325 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
4326
4327 // Verify that metrics noted it was an existing local card for which credit
4328 // card upload was offered and accepted.
4329 histogram_tester.ExpectUniqueSample(
4330 "Autofill.UploadOfferedCardOrigin",
4331 AutofillMetrics::OFFERING_UPLOAD_OF_LOCAL_CARD, 1);
4332 histogram_tester.ExpectUniqueSample(
4333 "Autofill.UploadAcceptedCardOrigin",
4334 AutofillMetrics::USER_ACCEPTED_UPLOAD_OF_LOCAL_CARD, 1);
4335 }
4336
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_UploadOfNewCard)4337 TEST_F(CreditCardSaveManagerTest, UploadCreditCard_UploadOfNewCard) {
4338 // Create, fill and submit an address form in order to establish a recent
4339 // profile which can be selected for the upload request.
4340 FormData address_form;
4341 test::CreateTestAddressFormData(&address_form);
4342 FormsSeen(std::vector<FormData>(1, address_form));
4343 ExpectUniqueFillableFormParsedUkm();
4344
4345 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
4346 FormSubmitted(address_form);
4347
4348 // Set up our credit card form data.
4349 FormData credit_card_form;
4350 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
4351 FormsSeen(std::vector<FormData>(1, credit_card_form));
4352 ExpectFillableFormParsedUkm(2 /* num_fillable_forms_parsed */);
4353
4354 // Edit the data, and submit.
4355 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
4356 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
4357 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
4358 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
4359 credit_card_form.fields[4].value = ASCIIToUTF16("123");
4360
4361 base::HistogramTester histogram_tester;
4362
4363 FormSubmitted(credit_card_form);
4364 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
4365 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
4366
4367 // Verify that metrics noted it was a brand new card for which credit card
4368 // upload was offered and accepted.
4369 histogram_tester.ExpectUniqueSample(
4370 "Autofill.UploadOfferedCardOrigin",
4371 AutofillMetrics::OFFERING_UPLOAD_OF_NEW_CARD, 1);
4372 histogram_tester.ExpectUniqueSample(
4373 "Autofill.UploadAcceptedCardOrigin",
4374 AutofillMetrics::USER_ACCEPTED_UPLOAD_OF_NEW_CARD, 1);
4375 }
4376
4377 // This test ensures that if offer-to-upload is denied by Google Payments, local
4378 // save is not offered if the card is already a local card.
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_DenyingUploadOfLocalCardShouldNotOfferLocalSave)4379 TEST_F(CreditCardSaveManagerTest,
4380 UploadCreditCard_DenyingUploadOfLocalCardShouldNotOfferLocalSave) {
4381 // Anything other than "en-US" will cause GetUploadDetails to return a failure
4382 // response.
4383 credit_card_save_manager_->SetAppLocale("pt-BR");
4384
4385 // Add a local credit card whose |TypeAndLastFourDigits| matches what we will
4386 // enter below.
4387 CreditCard local_card;
4388 test::SetCreditCardInfo(&local_card, "Flo Master", "4111111111111111",
4389 test::NextMonth().c_str(), test::NextYear().c_str(),
4390 "1");
4391 local_card.set_record_type(CreditCard::LOCAL_CARD);
4392 personal_data_.AddCreditCard(local_card);
4393
4394 // Create, fill and submit an address form in order to establish a recent
4395 // profile which can be selected for the upload request.
4396 FormData address_form;
4397 test::CreateTestAddressFormData(&address_form);
4398 FormsSeen(std::vector<FormData>(1, address_form));
4399 ExpectUniqueFillableFormParsedUkm();
4400
4401 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
4402 FormSubmitted(address_form);
4403
4404 // Set up our credit card form data.
4405 FormData credit_card_form;
4406 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
4407 FormsSeen(std::vector<FormData>(1, credit_card_form));
4408 ExpectFillableFormParsedUkm(2 /* num_fillable_forms_parsed */);
4409
4410 // Edit the data, and submit.
4411 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
4412 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
4413 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
4414 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
4415 credit_card_form.fields[4].value = ASCIIToUTF16("123");
4416
4417 base::HistogramTester histogram_tester;
4418
4419 // Neither local or upload save should be offered in this case.
4420 FormSubmitted(credit_card_form);
4421 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
4422 EXPECT_FALSE(credit_card_save_manager_->CreditCardWasUploaded());
4423
4424 // Verify that metrics noted it was an existing local card for which credit
4425 // card upload was offered and accepted.
4426 histogram_tester.ExpectTotalCount("Autofill.UploadOfferedCardOrigin", 0);
4427 histogram_tester.ExpectTotalCount("Autofill.UploadAcceptedCardOrigin", 0);
4428 }
4429
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_DoNotAddAnyFlagStatesToRequestIfExperimentsOff)4430 TEST_F(CreditCardSaveManagerTest,
4431 UploadCreditCard_DoNotAddAnyFlagStatesToRequestIfExperimentsOff) {
4432 // Create, fill and submit an address form in order to establish a recent
4433 // profile which can be selected for the upload request.
4434 FormData address_form;
4435 test::CreateTestAddressFormData(&address_form);
4436 FormsSeen(std::vector<FormData>(1, address_form));
4437 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
4438 FormSubmitted(address_form);
4439
4440 // Set up our credit card form data.
4441 FormData credit_card_form;
4442 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
4443 FormsSeen(std::vector<FormData>(1, credit_card_form));
4444
4445 // Edit the data, and submit.
4446 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
4447 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
4448 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
4449 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
4450 credit_card_form.fields[4].value = ASCIIToUTF16("123");
4451
4452 // Confirm that upload happened and that no experiment flag state was sent in
4453 // the request.
4454 FormSubmitted(credit_card_form);
4455 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
4456 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
4457 EXPECT_TRUE(payments_client_->active_experiments_in_request().empty());
4458 }
4459
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_ShouldAddUploadCardBillableServiceNumberInRequest)4460 TEST_F(CreditCardSaveManagerTest,
4461 UploadCreditCard_ShouldAddUploadCardBillableServiceNumberInRequest) {
4462 // Create, fill and submit an address form in order to establish a recent
4463 // profile which can be selected for the upload request.
4464 FormData address_form;
4465 test::CreateTestAddressFormData(&address_form);
4466 FormsSeen(std::vector<FormData>(1, address_form));
4467 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
4468 FormSubmitted(address_form);
4469
4470 // Set up our credit card form data.
4471 FormData credit_card_form;
4472 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
4473 FormsSeen(std::vector<FormData>(1, credit_card_form));
4474
4475 // Edit the data, and submit.
4476 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
4477 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
4478 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
4479 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
4480 credit_card_form.fields[4].value = ASCIIToUTF16("123");
4481
4482 // Confirm that the preflight request contained
4483 // kUploadCardBillableServiceNumber in the request.
4484 FormSubmitted(credit_card_form);
4485 EXPECT_EQ(payments::kUploadCardBillableServiceNumber,
4486 payments_client_->billable_service_number_in_request());
4487 }
4488
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_ShouldAddUploadCardSourceInRequest)4489 TEST_F(CreditCardSaveManagerTest,
4490 UploadCreditCard_ShouldAddUploadCardSourceInRequest) {
4491 // Create, fill and submit an address form in order to establish a recent
4492 // profile which can be selected for the upload request.
4493 FormData address_form;
4494 test::CreateTestAddressFormData(&address_form);
4495 FormsSeen(std::vector<FormData>(1, address_form));
4496 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
4497 FormSubmitted(address_form);
4498
4499 // Set up our credit card form data.
4500 FormData credit_card_form;
4501 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
4502 FormsSeen(std::vector<FormData>(1, credit_card_form));
4503
4504 // Edit the data, and submit.
4505 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
4506 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
4507 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
4508 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
4509 credit_card_form.fields[4].value = ASCIIToUTF16("123");
4510
4511 // Confirm that the preflight request contained the correct UploadCardSource.
4512 FormSubmitted(credit_card_form);
4513 EXPECT_EQ(payments::PaymentsClient::UploadCardSource::UPSTREAM_CHECKOUT_FLOW,
4514 payments_client_->upload_card_source_in_request());
4515 }
4516
4517 // Tests that a card with some strikes (but not max strikes) should still show
4518 // the save bubble/infobar.
TEST_F(CreditCardSaveManagerTest,LocallySaveCreditCard_NotEnoughStrikesStillShowsOfferToSave)4519 TEST_F(CreditCardSaveManagerTest,
4520 LocallySaveCreditCard_NotEnoughStrikesStillShowsOfferToSave) {
4521 credit_card_save_manager_->SetCreditCardUploadEnabled(false);
4522 TestCreditCardSaveStrikeDatabase credit_card_save_strike_database =
4523 TestCreditCardSaveStrikeDatabase(strike_database_);
4524
4525 // Add a single strike for the card to be added.
4526 credit_card_save_strike_database.AddStrike("1111");
4527 EXPECT_EQ(1, credit_card_save_strike_database.GetStrikes("1111"));
4528
4529 // Set up our credit card form data.
4530 FormData credit_card_form;
4531 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
4532 FormsSeen(std::vector<FormData>(1, credit_card_form));
4533 ExpectFillableFormParsedUkm(1 /* num_fillable_forms_parsed */);
4534
4535 // Edit the data, and submit.
4536 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
4537 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
4538 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
4539 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
4540 credit_card_form.fields[4].value = ASCIIToUTF16("123");
4541
4542 base::HistogramTester histogram_tester;
4543
4544 FormSubmitted(credit_card_form);
4545 EXPECT_TRUE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
4546 EXPECT_FALSE(credit_card_save_manager_->CreditCardWasUploaded());
4547
4548 // Verify that the offer-to-save bubble was still shown because the card did
4549 // not have too many strikes.
4550 EXPECT_TRUE(
4551 autofill_client_.get_offer_to_save_credit_card_bubble_was_shown());
4552 // Verify that no histogram entry was logged.
4553 histogram_tester.ExpectTotalCount(
4554 "Autofill.StrikeDatabase.CreditCardSaveNotOfferedDueToMaxStrikes", 0);
4555 }
4556
4557 // Tests that a card with some strikes (but not max strikes) should still show
4558 // the save bubble/infobar.
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_NotEnoughStrikesStillShowsOfferToSave)4559 TEST_F(CreditCardSaveManagerTest,
4560 UploadCreditCard_NotEnoughStrikesStillShowsOfferToSave) {
4561 TestCreditCardSaveStrikeDatabase credit_card_save_strike_database =
4562 TestCreditCardSaveStrikeDatabase(strike_database_);
4563
4564 // Add a single strike for the card to be added.
4565 credit_card_save_strike_database.AddStrike("1111");
4566 EXPECT_EQ(1, credit_card_save_strike_database.GetStrikes("1111"));
4567
4568 // Create, fill and submit an address form in order to establish a recent
4569 // profile which can be selected for the upload request.
4570 FormData address_form;
4571 test::CreateTestAddressFormData(&address_form);
4572 FormsSeen(std::vector<FormData>(1, address_form));
4573 ExpectUniqueFillableFormParsedUkm();
4574
4575 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
4576 FormSubmitted(address_form);
4577
4578 // Set up our credit card form data.
4579 FormData credit_card_form;
4580 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
4581 FormsSeen(std::vector<FormData>(1, credit_card_form));
4582 ExpectFillableFormParsedUkm(2 /* num_fillable_forms_parsed */);
4583
4584 // Edit the data, and submit.
4585 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
4586 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
4587 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
4588 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
4589 credit_card_form.fields[4].value = ASCIIToUTF16("123");
4590
4591 base::HistogramTester histogram_tester;
4592
4593 FormSubmitted(credit_card_form);
4594 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
4595 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
4596
4597 // Verify that the offer-to-save bubble was still shown because the card did
4598 // not have too many strikes.
4599 EXPECT_TRUE(
4600 autofill_client_.get_offer_to_save_credit_card_bubble_was_shown());
4601 // Verify that no histogram entry was logged.
4602 histogram_tester.ExpectTotalCount(
4603 "Autofill.StrikeDatabase.CreditCardSaveNotOfferedDueToMaxStrikes", 0);
4604 }
4605
4606 #if defined(OS_ANDROID) || defined(OS_IOS)
4607 // Tests that a card with max strikes does not offer save on mobile at all.
TEST_F(CreditCardSaveManagerTest,LocallySaveCreditCard_MaxStrikesDisallowsSave)4608 TEST_F(CreditCardSaveManagerTest,
4609 LocallySaveCreditCard_MaxStrikesDisallowsSave) {
4610 credit_card_save_manager_->SetCreditCardUploadEnabled(false);
4611 TestCreditCardSaveStrikeDatabase credit_card_save_strike_database =
4612 TestCreditCardSaveStrikeDatabase(strike_database_);
4613
4614 // Max out strikes for the card to be added.
4615 credit_card_save_strike_database.AddStrike("1111");
4616 credit_card_save_strike_database.AddStrike("1111");
4617 credit_card_save_strike_database.AddStrike("1111");
4618 EXPECT_EQ(3, credit_card_save_strike_database.GetStrikes("1111"));
4619
4620 // Set up our credit card form data.
4621 FormData credit_card_form;
4622 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
4623 FormsSeen(std::vector<FormData>(1, credit_card_form));
4624 ExpectFillableFormParsedUkm(1 /* num_fillable_forms_parsed */);
4625
4626 // Edit the data, and submit.
4627 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
4628 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
4629 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
4630 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
4631 credit_card_form.fields[4].value = ASCIIToUTF16("123");
4632
4633 base::HistogramTester histogram_tester;
4634
4635 // No form of credit card save should be shown.
4636 FormSubmitted(credit_card_form);
4637 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
4638 EXPECT_FALSE(credit_card_save_manager_->CreditCardWasUploaded());
4639
4640 // Verify that the correct histogram entry was logged.
4641 histogram_tester.ExpectBucketCount(
4642 "Autofill.StrikeDatabase.CreditCardSaveNotOfferedDueToMaxStrikes",
4643 AutofillMetrics::SaveTypeMetric::LOCAL, 1);
4644 }
4645
4646 // TODO(crbug.com/1113034): Create an equivalent test for iOS, or skip
4647 // permanently if the test doesn't apply to iOS flow.
4648 #if !defined(OS_IOS)
4649 // Tests that a card with max strikes does not offer save on mobile at all.
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_MaxStrikesDisallowsSave)4650 TEST_F(CreditCardSaveManagerTest, UploadCreditCard_MaxStrikesDisallowsSave) {
4651 TestCreditCardSaveStrikeDatabase credit_card_save_strike_database =
4652 TestCreditCardSaveStrikeDatabase(strike_database_);
4653
4654 // Max out strikes for the card to be added.
4655 credit_card_save_strike_database.AddStrike("1111");
4656 credit_card_save_strike_database.AddStrike("1111");
4657 credit_card_save_strike_database.AddStrike("1111");
4658 EXPECT_EQ(3, credit_card_save_strike_database.GetStrikes("1111"));
4659
4660 // Create, fill and submit an address form in order to establish a recent
4661 // profile which can be selected for the upload request.
4662 FormData address_form;
4663 test::CreateTestAddressFormData(&address_form);
4664 FormsSeen(std::vector<FormData>(1, address_form));
4665 ExpectUniqueFillableFormParsedUkm();
4666
4667 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
4668 FormSubmitted(address_form);
4669
4670 // Set up our credit card form data.
4671 FormData credit_card_form;
4672 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
4673 FormsSeen(std::vector<FormData>(1, credit_card_form));
4674 ExpectFillableFormParsedUkm(2 /* num_fillable_forms_parsed */);
4675
4676 // Edit the data, and submit.
4677 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
4678 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
4679 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
4680 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
4681 credit_card_form.fields[4].value = ASCIIToUTF16("123");
4682
4683 base::HistogramTester histogram_tester;
4684
4685 // No form of credit card save should be shown.
4686 FormSubmitted(credit_card_form);
4687 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
4688 EXPECT_FALSE(credit_card_save_manager_->CreditCardWasUploaded());
4689
4690 // Verify that the correct histogram entries were logged.
4691 ExpectCardUploadDecision(
4692 histogram_tester,
4693 AutofillMetrics::UPLOAD_NOT_OFFERED_MAX_STRIKES_ON_MOBILE);
4694 histogram_tester.ExpectBucketCount(
4695 "Autofill.StrikeDatabase.CreditCardSaveNotOfferedDueToMaxStrikes",
4696 AutofillMetrics::SaveTypeMetric::SERVER, 1);
4697 // Verify that the correct UKM was logged.
4698 ExpectCardUploadDecisionUkm(
4699 AutofillMetrics::UPLOAD_NOT_OFFERED_MAX_STRIKES_ON_MOBILE);
4700 }
4701 #endif
4702
4703 #else // !defined(OS_ANDROID) && !defined(OS_IOS)
4704 // Tests that a card with max strikes should still offer to save on Desktop via
4705 // the omnibox icon, but that the offer-to-save bubble itself is not shown.
TEST_F(CreditCardSaveManagerTest,LocallySaveCreditCard_MaxStrikesStillAllowsSave)4706 TEST_F(CreditCardSaveManagerTest,
4707 LocallySaveCreditCard_MaxStrikesStillAllowsSave) {
4708 credit_card_save_manager_->SetCreditCardUploadEnabled(false);
4709 TestCreditCardSaveStrikeDatabase credit_card_save_strike_database =
4710 TestCreditCardSaveStrikeDatabase(strike_database_);
4711
4712 // Max out strikes for the card to be added.
4713 credit_card_save_strike_database.AddStrike("1111");
4714 credit_card_save_strike_database.AddStrike("1111");
4715 credit_card_save_strike_database.AddStrike("1111");
4716 EXPECT_EQ(3, credit_card_save_strike_database.GetStrikes("1111"));
4717
4718 // Set up our credit card form data.
4719 FormData credit_card_form;
4720 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
4721 FormsSeen(std::vector<FormData>(1, credit_card_form));
4722 ExpectFillableFormParsedUkm(1 /* num_fillable_forms_parsed */);
4723
4724 // Edit the data, and submit.
4725 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
4726 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
4727 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
4728 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
4729 credit_card_form.fields[4].value = ASCIIToUTF16("123");
4730
4731 base::HistogramTester histogram_tester;
4732
4733 FormSubmitted(credit_card_form);
4734 EXPECT_TRUE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
4735 EXPECT_FALSE(credit_card_save_manager_->CreditCardWasUploaded());
4736
4737 // Verify that the offer-to-save bubble was not shown because the card had too
4738 // many strikes.
4739 EXPECT_FALSE(
4740 autofill_client_.get_offer_to_save_credit_card_bubble_was_shown());
4741 // Verify that the correct histogram entry was logged.
4742 histogram_tester.ExpectBucketCount(
4743 "Autofill.StrikeDatabase.CreditCardSaveNotOfferedDueToMaxStrikes",
4744 AutofillMetrics::SaveTypeMetric::LOCAL, 1);
4745 }
4746
4747 // Tests that a card with max strikes should still offer to save on Desktop via
4748 // the omnibox icon, but that the offer-to-save bubble itself is not shown.
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_MaxStrikesStillAllowsSave)4749 TEST_F(CreditCardSaveManagerTest, UploadCreditCard_MaxStrikesStillAllowsSave) {
4750 TestCreditCardSaveStrikeDatabase credit_card_save_strike_database =
4751 TestCreditCardSaveStrikeDatabase(strike_database_);
4752
4753 // Max out strikes for the card to be added.
4754 credit_card_save_strike_database.AddStrike("1111");
4755 credit_card_save_strike_database.AddStrike("1111");
4756 credit_card_save_strike_database.AddStrike("1111");
4757 EXPECT_EQ(3, credit_card_save_strike_database.GetStrikes("1111"));
4758
4759 // Create, fill and submit an address form in order to establish a recent
4760 // profile which can be selected for the upload request.
4761 FormData address_form;
4762 test::CreateTestAddressFormData(&address_form);
4763 FormsSeen(std::vector<FormData>(1, address_form));
4764 ExpectUniqueFillableFormParsedUkm();
4765
4766 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
4767 FormSubmitted(address_form);
4768
4769 // Set up our credit card form data.
4770 FormData credit_card_form;
4771 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
4772 FormsSeen(std::vector<FormData>(1, credit_card_form));
4773 ExpectFillableFormParsedUkm(2 /* num_fillable_forms_parsed */);
4774
4775 // Edit the data, and submit.
4776 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
4777 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
4778 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
4779 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
4780 credit_card_form.fields[4].value = ASCIIToUTF16("123");
4781
4782 base::HistogramTester histogram_tester;
4783
4784 FormSubmitted(credit_card_form);
4785 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
4786 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
4787
4788 // Verify that the offer-to-save bubble was not shown because the card had too
4789 // many strikes.
4790 EXPECT_FALSE(
4791 autofill_client_.get_offer_to_save_credit_card_bubble_was_shown());
4792 // Verify that the correct histogram entry was logged.
4793 histogram_tester.ExpectBucketCount(
4794 "Autofill.StrikeDatabase.CreditCardSaveNotOfferedDueToMaxStrikes",
4795 AutofillMetrics::SaveTypeMetric::SERVER, 1);
4796 }
4797
4798 // Tests that 2 LocalCardMigrationStrikes are removed when cards are saved
4799 // locally.
TEST_F(CreditCardSaveManagerTest,LocalCreditCard_LocalCardMigrationStrikesRemovedOnLocalSave)4800 TEST_F(CreditCardSaveManagerTest,
4801 LocalCreditCard_LocalCardMigrationStrikesRemovedOnLocalSave) {
4802 LocalCardMigrationStrikeDatabase local_card_migration_strike_database =
4803 LocalCardMigrationStrikeDatabase(strike_database_);
4804
4805 // Start with 3 strikes in |local_card_migration_strike_database|.
4806 local_card_migration_strike_database.AddStrikes(3);
4807 EXPECT_EQ(local_card_migration_strike_database.GetStrikes(), 3);
4808
4809 credit_card_save_manager_->SetCreditCardUploadEnabled(false);
4810
4811 // Create, fill and submit an address form in order to establish a recent
4812 // profile which can be selected for the upload request.
4813 FormData address_form;
4814 test::CreateTestAddressFormData(&address_form);
4815 FormsSeen(std::vector<FormData>(1, address_form));
4816 ExpectUniqueFillableFormParsedUkm();
4817
4818 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
4819 FormSubmitted(address_form);
4820
4821 // Set up our credit card form data with credit card first and last name
4822 // fields.
4823 FormData credit_card_form;
4824 CreateTestCreditCardFormData(&credit_card_form,
4825 CreditCardFormOptions().with_split_names(true));
4826 FormsSeen(std::vector<FormData>(1, credit_card_form));
4827
4828 // Edit the data, and submit.
4829 credit_card_form.fields[0].value = ASCIIToUTF16("Flo");
4830 credit_card_form.fields[1].value = ASCIIToUTF16("Master");
4831 credit_card_form.fields[2].value = ASCIIToUTF16("4111111111111111");
4832 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextMonth());
4833 credit_card_form.fields[4].value = ASCIIToUTF16(test::NextYear());
4834 credit_card_form.fields[5].value = ASCIIToUTF16("123");
4835
4836 base::HistogramTester histogram_tester;
4837
4838 FormSubmitted(credit_card_form);
4839 EXPECT_TRUE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
4840 EXPECT_FALSE(credit_card_save_manager_->CreditCardWasUploaded());
4841
4842 // 2 strikes should be removed when card was saved locally.
4843 EXPECT_EQ(local_card_migration_strike_database.GetStrikes(), 1);
4844 }
4845
4846 // Tests that no LocalCardMigrationStrikes get removed due to cards being
4847 // uploaded.
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_NoLocalSaveMigrationStrikesRemovedOnUpload)4848 TEST_F(CreditCardSaveManagerTest,
4849 UploadCreditCard_NoLocalSaveMigrationStrikesRemovedOnUpload) {
4850 LocalCardMigrationStrikeDatabase local_card_migration_strike_database =
4851 LocalCardMigrationStrikeDatabase(strike_database_);
4852
4853 // Start with 3 strikes in |local_card_migration_strike_database|.
4854 local_card_migration_strike_database.AddStrikes(3);
4855 EXPECT_EQ(local_card_migration_strike_database.GetStrikes(), 3);
4856
4857 credit_card_save_manager_->SetCreditCardUploadEnabled(true);
4858
4859 // Create, fill and submit an address form in order to establish a recent
4860 // profile which can be selected for the upload request.
4861 FormData address_form;
4862 test::CreateTestAddressFormData(&address_form);
4863 FormsSeen(std::vector<FormData>(1, address_form));
4864 ExpectUniqueFillableFormParsedUkm();
4865
4866 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
4867 FormSubmitted(address_form);
4868
4869 // Set up our credit card form data with credit card first and last name
4870 // fields.
4871 FormData credit_card_form;
4872 CreateTestCreditCardFormData(&credit_card_form,
4873 CreditCardFormOptions().with_split_names(true));
4874 FormsSeen(std::vector<FormData>(1, credit_card_form));
4875
4876 // Edit the data, and submit.
4877 credit_card_form.fields[0].value = ASCIIToUTF16("Flo");
4878 credit_card_form.fields[1].value = ASCIIToUTF16("Master");
4879 credit_card_form.fields[2].value = ASCIIToUTF16("4111111111111111");
4880 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextMonth());
4881 credit_card_form.fields[4].value = ASCIIToUTF16(test::NextYear());
4882 credit_card_form.fields[5].value = ASCIIToUTF16("123");
4883
4884 base::HistogramTester histogram_tester;
4885
4886 FormSubmitted(credit_card_form);
4887 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
4888 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
4889
4890 // Strike count shouldn't change.
4891 EXPECT_EQ(local_card_migration_strike_database.GetStrikes(), 3);
4892 }
4893 #endif
4894
4895 // Tests that adding a card clears all strikes for that card.
TEST_F(CreditCardSaveManagerTest,LocallySaveCreditCard_ClearStrikesOnAdd)4896 TEST_F(CreditCardSaveManagerTest, LocallySaveCreditCard_ClearStrikesOnAdd) {
4897 credit_card_save_manager_->SetCreditCardUploadEnabled(false);
4898 TestCreditCardSaveStrikeDatabase credit_card_save_strike_database =
4899 TestCreditCardSaveStrikeDatabase(strike_database_);
4900
4901 // Add two strikes for the card to be added.
4902 credit_card_save_strike_database.AddStrike("1111");
4903 credit_card_save_strike_database.AddStrike("1111");
4904 EXPECT_EQ(2, credit_card_save_strike_database.GetStrikes("1111"));
4905
4906 // Set up our credit card form data.
4907 FormData credit_card_form;
4908 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
4909 FormsSeen(std::vector<FormData>(1, credit_card_form));
4910 ExpectFillableFormParsedUkm(1 /* num_fillable_forms_parsed */);
4911
4912 // Edit the data, and submit.
4913 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
4914 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
4915 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
4916 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
4917 credit_card_form.fields[4].value = ASCIIToUTF16("123");
4918
4919 FormSubmitted(credit_card_form);
4920 EXPECT_TRUE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
4921 EXPECT_FALSE(credit_card_save_manager_->CreditCardWasUploaded());
4922
4923 // Verify that adding the card reset the strike count for that card.
4924 EXPECT_EQ(0, credit_card_save_strike_database.GetStrikes("1111"));
4925 }
4926
4927 // Tests that adding a card clears all strikes for that card.
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_ClearStrikesOnAdd)4928 TEST_F(CreditCardSaveManagerTest, UploadCreditCard_ClearStrikesOnAdd) {
4929 TestCreditCardSaveStrikeDatabase credit_card_save_strike_database =
4930 TestCreditCardSaveStrikeDatabase(strike_database_);
4931
4932 // Add two strikes for the card to be added.
4933 credit_card_save_strike_database.AddStrike("1111");
4934 credit_card_save_strike_database.AddStrike("1111");
4935 EXPECT_EQ(2, credit_card_save_strike_database.GetStrikes("1111"));
4936
4937 // Create, fill and submit an address form in order to establish a recent
4938 // profile which can be selected for the upload request.
4939 FormData address_form;
4940 test::CreateTestAddressFormData(&address_form);
4941 FormsSeen(std::vector<FormData>(1, address_form));
4942 ExpectUniqueFillableFormParsedUkm();
4943
4944 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
4945 FormSubmitted(address_form);
4946
4947 // Set up our credit card form data.
4948 FormData credit_card_form;
4949 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
4950 FormsSeen(std::vector<FormData>(1, credit_card_form));
4951 ExpectFillableFormParsedUkm(2 /* num_fillable_forms_parsed */);
4952
4953 // Edit the data, and submit.
4954 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
4955 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
4956 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
4957 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
4958 credit_card_form.fields[4].value = ASCIIToUTF16("123");
4959
4960 FormSubmitted(credit_card_form);
4961 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
4962 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
4963
4964 // Verify that adding the card reset the strike count for that card.
4965 EXPECT_EQ(0, credit_card_save_strike_database.GetStrikes("1111"));
4966 }
4967
4968 // Tests that adding a card clears all strikes for that card.
TEST_F(CreditCardSaveManagerTest,LocallySaveCreditCard_NumStrikesLoggedOnAdd)4969 TEST_F(CreditCardSaveManagerTest, LocallySaveCreditCard_NumStrikesLoggedOnAdd) {
4970 credit_card_save_manager_->SetCreditCardUploadEnabled(false);
4971
4972 TestCreditCardSaveStrikeDatabase credit_card_save_strike_database =
4973 TestCreditCardSaveStrikeDatabase(strike_database_);
4974
4975 // Add two strikes for the card to be added.
4976 credit_card_save_strike_database.AddStrike("1111");
4977 credit_card_save_strike_database.AddStrike("1111");
4978 EXPECT_EQ(2, credit_card_save_strike_database.GetStrikes("1111"));
4979
4980 // Set up our credit card form data.
4981 FormData credit_card_form;
4982 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
4983 FormsSeen(std::vector<FormData>(1, credit_card_form));
4984 ExpectFillableFormParsedUkm(1 /* num_fillable_forms_parsed */);
4985
4986 // Edit the data, and submit.
4987 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
4988 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
4989 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
4990 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
4991 credit_card_form.fields[4].value = ASCIIToUTF16("123");
4992
4993 base::HistogramTester histogram_tester;
4994
4995 FormSubmitted(credit_card_form);
4996 EXPECT_TRUE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
4997 EXPECT_FALSE(credit_card_save_manager_->CreditCardWasUploaded());
4998
4999 // Verify that adding the card logged the number of strikes it had previously.
5000 histogram_tester.ExpectUniqueSample(
5001 "Autofill.StrikeDatabase.StrikesPresentWhenLocalCardSaved",
5002 /*sample=*/2, /*count=*/1);
5003 }
5004
5005 // Tests that adding a card clears all strikes for that card.
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_NumStrikesLoggedOnAdd)5006 TEST_F(CreditCardSaveManagerTest, UploadCreditCard_NumStrikesLoggedOnAdd) {
5007 TestCreditCardSaveStrikeDatabase credit_card_save_strike_database =
5008 TestCreditCardSaveStrikeDatabase(strike_database_);
5009
5010 // Add two strikes for the card to be added.
5011 credit_card_save_strike_database.AddStrike("1111");
5012 credit_card_save_strike_database.AddStrike("1111");
5013 EXPECT_EQ(2, credit_card_save_strike_database.GetStrikes("1111"));
5014
5015 // Create, fill and submit an address form in order to establish a recent
5016 // profile which can be selected for the upload request.
5017 FormData address_form;
5018 test::CreateTestAddressFormData(&address_form);
5019 FormsSeen(std::vector<FormData>(1, address_form));
5020 ExpectUniqueFillableFormParsedUkm();
5021
5022 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
5023 FormSubmitted(address_form);
5024
5025 // Set up our credit card form data.
5026 FormData credit_card_form;
5027 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
5028 FormsSeen(std::vector<FormData>(1, credit_card_form));
5029 ExpectFillableFormParsedUkm(2 /* num_fillable_forms_parsed */);
5030
5031 // Edit the data, and submit.
5032 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
5033 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
5034 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
5035 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
5036 credit_card_form.fields[4].value = ASCIIToUTF16("123");
5037
5038 base::HistogramTester histogram_tester;
5039
5040 FormSubmitted(credit_card_form);
5041 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
5042 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
5043
5044 // Verify that adding the card logged the number of strikes it had previously.
5045 histogram_tester.ExpectUniqueSample(
5046 "Autofill.StrikeDatabase.StrikesPresentWhenServerCardSaved",
5047 /*sample=*/2, /*count=*/1);
5048 }
5049
5050 // Tests that one strike is added when upload failed and
5051 // bubble is shown.
TEST_F(CreditCardSaveManagerTest,UploadCreditCard_NumStrikesLoggedOnUploadNotSuccess)5052 TEST_F(CreditCardSaveManagerTest,
5053 UploadCreditCard_NumStrikesLoggedOnUploadNotSuccess) {
5054 const char* const server_id = "InstrumentData:1234";
5055 payments_client_->SetServerIdForCardUpload(server_id);
5056 TestCreditCardSaveStrikeDatabase credit_card_save_strike_database =
5057 TestCreditCardSaveStrikeDatabase(strike_database_);
5058 EXPECT_EQ(0, credit_card_save_strike_database.GetStrikes("1111"));
5059
5060 // If upload failed and the bubble was shown, strike count should increase
5061 // by 1.
5062 credit_card_save_manager_->set_show_save_prompt(true);
5063 credit_card_save_manager_->set_upload_request_card_number(
5064 ASCIIToUTF16("4111111111111111"));
5065 credit_card_save_manager_->OnDidUploadCard(AutofillClient::TRY_AGAIN_FAILURE,
5066 server_id);
5067 EXPECT_EQ(1, credit_card_save_strike_database.GetStrikes("1111"));
5068 }
5069
5070 // Make sure that the PersonalDataManager gets notified when the user accepts
5071 // an upload offer.
TEST_F(CreditCardSaveManagerTest,OnUserDidAcceptUpload_NotifiesPDM)5072 TEST_F(CreditCardSaveManagerTest, OnUserDidAcceptUpload_NotifiesPDM) {
5073 EXPECT_CALL(personal_data_, OnUserAcceptedUpstreamOffer);
5074
5075 // Simulate that the user has accepted the upload from the prompt.
5076 UserHasAcceptedUpload({});
5077 }
5078
5079 // Tests that if a card doesn't fall in any of the supported bin ranges, local
5080 // save is offered rather than upload save.
TEST_F(CreditCardSaveManagerTest,UploadSaveNotOfferedForUnsupportedCard)5081 TEST_F(CreditCardSaveManagerTest, UploadSaveNotOfferedForUnsupportedCard) {
5082 std::vector<std::pair<int, int>> supported_card_bin_ranges{
5083 std::make_pair(4111, 4113), std::make_pair(34, 34),
5084 std::make_pair(300, 305)};
5085 payments_client_->SetSupportedBINRanges(supported_card_bin_ranges);
5086 // Set up our credit card form data.
5087 FormData credit_card_form;
5088 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
5089 FormsSeen(std::vector<FormData>(1, credit_card_form));
5090
5091 // Edit the data, and submit.
5092 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
5093 credit_card_form.fields[1].value = ASCIIToUTF16("5454545454545454");
5094 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
5095 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
5096 credit_card_form.fields[4].value = ASCIIToUTF16("123");
5097
5098 // Since card isn't in any of the supported ranges, local save should be
5099 // offered and upload save should not.
5100 FormSubmitted(credit_card_form);
5101 EXPECT_TRUE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
5102 EXPECT_FALSE(credit_card_save_manager_->CreditCardWasUploaded());
5103 }
5104
5105 // Tests that if a card doesn't fall in any of the supported bin ranges, but is
5106 // already saved, then local save is not offered.
TEST_F(CreditCardSaveManagerTest,LocalSaveNotOfferedForSavedUnsupportedCard)5107 TEST_F(CreditCardSaveManagerTest, LocalSaveNotOfferedForSavedUnsupportedCard) {
5108 std::vector<std::pair<int, int>> supported_card_bin_ranges{
5109 std::make_pair(4111, 4113), std::make_pair(34, 34),
5110 std::make_pair(300, 305)};
5111 payments_client_->SetSupportedBINRanges(supported_card_bin_ranges);
5112 // Set up our credit card form data.
5113 FormData credit_card_form;
5114 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
5115 FormsSeen(std::vector<FormData>(1, credit_card_form));
5116
5117 // Add a local credit card whose number matches what we will
5118 // enter below.
5119 CreditCard local_card;
5120 test::SetCreditCardInfo(&local_card, "Flo Master", "5454545454545454",
5121 test::NextMonth().c_str(), test::NextYear().c_str(),
5122 "1");
5123 local_card.set_record_type(CreditCard::LOCAL_CARD);
5124 personal_data_.AddCreditCard(local_card);
5125
5126 // Edit the data, and submit.
5127 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
5128 credit_card_form.fields[1].value = ASCIIToUTF16("5454545454545454");
5129 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
5130 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
5131 credit_card_form.fields[4].value = ASCIIToUTF16("123");
5132
5133 // Since card is already saved, local save should not be offered.
5134 FormSubmitted(credit_card_form);
5135 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
5136 }
5137
5138 // Tests that if a card falls in one of the supported bin ranges, upload save
5139 // is offered.
TEST_F(CreditCardSaveManagerTest,UploadSaveOfferedForSupportedCard)5140 TEST_F(CreditCardSaveManagerTest, UploadSaveOfferedForSupportedCard) {
5141 // Set supported BIN ranges.
5142 std::vector<std::pair<int, int>> supported_card_bin_ranges{
5143 std::make_pair(4111, 4113)};
5144 payments_client_->SetSupportedBINRanges(supported_card_bin_ranges);
5145
5146 // Set up our credit card form data.
5147 FormData credit_card_form;
5148 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
5149 FormsSeen(std::vector<FormData>(1, credit_card_form));
5150
5151 // Edit the data, and submit.
5152 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
5153 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
5154 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
5155 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
5156 credit_card_form.fields[4].value = ASCIIToUTF16("123");
5157
5158 // Since card is in one of the supported ranges(4111-4113), upload save should
5159 // be offered.
5160 FormSubmitted(credit_card_form);
5161 EXPECT_FALSE(autofill_client_.ConfirmSaveCardLocallyWasCalled());
5162 EXPECT_TRUE(credit_card_save_manager_->CreditCardWasUploaded());
5163 }
5164
5165 // Tests that if payment client returns an invalid legal message upload should
5166 // not be offered.
TEST_F(CreditCardSaveManagerTest,InvalidLegalMessageInOnDidGetUploadDetails)5167 TEST_F(CreditCardSaveManagerTest, InvalidLegalMessageInOnDidGetUploadDetails) {
5168 payments_client_->SetUseInvalidLegalMessageInGetUploadDetails(true);
5169
5170 // Create, fill and submit an address form in order to establish a recent
5171 // profile which can be selected for the upload request.
5172 FormData address_form;
5173 test::CreateTestAddressFormData(&address_form);
5174 FormsSeen(std::vector<FormData>(1, address_form));
5175
5176 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
5177 FormSubmitted(address_form);
5178
5179 // Set up our credit card form data.
5180 FormData credit_card_form;
5181 CreateTestCreditCardFormData(&credit_card_form, CreditCardFormOptions());
5182 FormsSeen(std::vector<FormData>(1, credit_card_form));
5183
5184 // Edit the data, and submit.
5185 const char* const card_number = "4111111111111111";
5186 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
5187 credit_card_form.fields[1].value = ASCIIToUTF16(card_number);
5188 credit_card_form.fields[2].value = ASCIIToUTF16(test::NextMonth());
5189 credit_card_form.fields[3].value = ASCIIToUTF16(test::NextYear());
5190 credit_card_form.fields[4].value = ASCIIToUTF16("123");
5191
5192 base::HistogramTester histogram_tester;
5193 FormSubmitted(credit_card_form);
5194
5195 // Verify that the correct histogram entries were logged.
5196 ExpectCardUploadDecision(
5197 histogram_tester,
5198 AutofillMetrics::UPLOAD_NOT_OFFERED_INVALID_LEGAL_MESSAGE);
5199 }
5200
5201 } // namespace autofill
5202