Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(74)

Side by Side Diff: chrome/browser/ui/autofill/autofill_dialog_controller_unittest.cc

Issue 1931043002: Remove requestAutocomplete (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 <stddef.h>
6
7 #include <map>
8 #include <memory>
9 #include <utility>
10
11 #include "base/bind.h"
12 #include "base/bind_helpers.h"
13 #include "base/callback.h"
14 #include "base/command_line.h"
15 #include "base/guid.h"
16 #include "base/macros.h"
17 #include "base/message_loop/message_loop.h"
18 #include "base/run_loop.h"
19 #include "base/strings/string_number_conversions.h"
20 #include "base/strings/string_piece.h"
21 #include "base/strings/utf_string_conversions.h"
22 #include "build/build_config.h"
23 #include "chrome/browser/signin/account_tracker_service_factory.h"
24 #include "chrome/browser/signin/signin_manager_factory.h"
25 #include "chrome/browser/ui/autofill/autofill_dialog_controller_impl.h"
26 #include "chrome/browser/ui/autofill/autofill_dialog_i18n_input.h"
27 #include "chrome/browser/ui/autofill/autofill_dialog_view.h"
28 #include "chrome/browser/ui/autofill/mock_address_validator.h"
29 #include "chrome/browser/web_data_service_factory.h"
30 #include "chrome/common/chrome_switches.h"
31 #include "chrome/common/pref_names.h"
32 #include "chrome/grit/generated_resources.h"
33 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
34 #include "chrome/test/base/scoped_testing_local_state.h"
35 #include "chrome/test/base/testing_browser_process.h"
36 #include "chrome/test/base/testing_profile.h"
37 #include "components/autofill/content/browser/risk/proto/fingerprint.pb.h"
38 #include "components/autofill/core/browser/autofill_metrics.h"
39 #include "components/autofill/core/browser/autofill_test_utils.h"
40 #include "components/autofill/core/browser/country_names.h"
41 #include "components/autofill/core/browser/test_personal_data_manager.h"
42 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
43 #include "components/autofill/core/common/autofill_constants.h"
44 #include "components/autofill/core/common/autofill_pref_names.h"
45 #include "components/autofill/core/common/autofill_switches.h"
46 #include "components/autofill/core/common/form_data.h"
47 #include "components/bookmarks/common/bookmark_pref_names.h"
48 #include "components/prefs/pref_service.h"
49 #include "components/signin/core/browser/account_tracker_service.h"
50 #include "components/signin/core/browser/signin_manager.h"
51 #include "components/user_prefs/user_prefs.h"
52 #include "content/public/browser/web_contents.h"
53 #include "content/public/test/mock_render_process_host.h"
54 #include "google_apis/gaia/google_service_auth_error.h"
55 #include "grit/components_scaled_resources.h"
56 #include "grit/components_strings.h"
57 #include "testing/gmock/include/gmock/gmock.h"
58 #include "testing/gtest/include/gtest/gtest.h"
59 #include "third_party/libaddressinput/chromium/chrome_address_validator.h"
60 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_fi eld.h"
61 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_pr oblem.h"
62 #include "ui/base/l10n/l10n_util.h"
63 #include "ui/base/resource/resource_bundle.h"
64
65 #if defined(OS_WIN)
66 #include "ui/base/win/scoped_ole_initializer.h"
67 #endif
68
69 using base::ASCIIToUTF16;
70 using base::UTF8ToUTF16;
71
72 namespace autofill {
73
74 namespace {
75
76 using ::i18n::addressinput::FieldProblemMap;
77 using testing::AtLeast;
78 using testing::DoAll;
79 using testing::Return;
80 using testing::SetArgPointee;
81 using testing::_;
82
83 const char kSourceUrl[] = "http://localbike.shop";
84 const char kFakeEmail[] = "user@chromium.org";
85 const char* kFieldsFromPage[] =
86 { "email",
87 "cc-name",
88 "cc-number",
89 "cc-exp-month",
90 "cc-exp-year",
91 "cc-csc",
92 "billing name",
93 "billing address-line1",
94 "billing address-level2",
95 "billing address-level1",
96 "billing postal-code",
97 "billing country",
98 "billing tel",
99 "shipping name",
100 "shipping address-line1",
101 "shipping address-level2",
102 "shipping address-level1",
103 "shipping postal-code",
104 "shipping country",
105 "shipping tel",
106 };
107 const char kTestCCNumberAmex[] = "376200000000002";
108 const char kTestCCNumberVisa[] = "4111111111111111";
109 const char kTestCCNumberMaster[] = "5555555555554444";
110 const char kTestCCNumberDiscover[] = "6011111111111117";
111 const char kTestCCNumberIncomplete[] = "4111111111";
112 // Credit card number fails Luhn check.
113 const char kTestCCNumberInvalid[] = "4111111111111112";
114
115 bool HasAnyError(const ValidityMessages& messages, ServerFieldType field) {
116 return !messages.GetMessageOrDefault(field).text.empty();
117 }
118
119 bool HasUnsureError(const ValidityMessages& messages, ServerFieldType field) {
120 const ValidityMessage& message = messages.GetMessageOrDefault(field);
121 return !message.text.empty() && !message.sure;
122 }
123
124 class TestAutofillDialogView : public AutofillDialogView {
125 public:
126 TestAutofillDialogView()
127 : updates_started_(0), save_details_locally_checked_(true) {}
128 ~TestAutofillDialogView() override {}
129
130 void Show() override {}
131 void Hide() override {}
132
133 void UpdatesStarted() override {
134 updates_started_++;
135 }
136
137 void UpdatesFinished() override {
138 updates_started_--;
139 EXPECT_GE(updates_started_, 0);
140 }
141
142 void UpdateNotificationArea() override {
143 EXPECT_GE(updates_started_, 1);
144 }
145
146 void UpdateButtonStrip() override {
147 EXPECT_GE(updates_started_, 1);
148 }
149
150 void UpdateDetailArea() override {
151 EXPECT_GE(updates_started_, 1);
152 }
153
154 void UpdateSection(DialogSection section) override {
155 section_updates_[section]++;
156 EXPECT_GE(updates_started_, 1);
157 }
158
159 void UpdateErrorBubble() override {
160 EXPECT_GE(updates_started_, 1);
161 }
162
163 void FillSection(DialogSection section,
164 ServerFieldType originating_type) override {}
165 void GetUserInput(DialogSection section, FieldValueMap* output) override {
166 *output = outputs_[section];
167 }
168
169 base::string16 GetCvc() override { return base::string16(); }
170
171 bool SaveDetailsLocally() override { return save_details_locally_checked_; }
172
173 MOCK_METHOD0(ModelChanged, void());
174 MOCK_METHOD0(UpdateForErrors, void());
175
176 void ValidateSection(DialogSection) override {}
177
178 void SetUserInput(DialogSection section, const FieldValueMap& map) {
179 outputs_[section] = map;
180 }
181
182 void CheckSaveDetailsLocallyCheckbox(bool checked) {
183 save_details_locally_checked_ = checked;
184 }
185
186 void ClearSectionUpdates() {
187 section_updates_.clear();
188 }
189
190 std::map<DialogSection, size_t> section_updates() const {
191 return section_updates_;
192 }
193
194 private:
195 std::map<DialogSection, FieldValueMap> outputs_;
196 std::map<DialogSection, size_t> section_updates_;
197
198 int updates_started_;
199 bool save_details_locally_checked_;
200
201 DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogView);
202 };
203
204 class TestAutofillDialogController
205 : public AutofillDialogControllerImpl,
206 public base::SupportsWeakPtr<TestAutofillDialogController> {
207 public:
208 TestAutofillDialogController(
209 content::WebContents* contents,
210 const FormData& form_structure,
211 const GURL& source_url,
212 const AutofillClient::ResultCallback& callback)
213 : AutofillDialogControllerImpl(contents,
214 form_structure,
215 source_url,
216 callback),
217 submit_button_delay_count_(0) {}
218
219 ~TestAutofillDialogController() override {}
220
221 AutofillDialogView* CreateView() override {
222 return new testing::NiceMock<TestAutofillDialogView>();
223 }
224
225 void Init(content::BrowserContext* browser_context) {
226 Profile* profile = Profile::FromBrowserContext(browser_context);
227 test_manager_.Init(WebDataServiceFactory::GetAutofillWebDataForProfile(
228 profile,
229 ServiceAccessType::EXPLICIT_ACCESS),
230 user_prefs::UserPrefs::Get(browser_context),
231 AccountTrackerServiceFactory::GetForProfile(profile),
232 SigninManagerFactory::GetForProfile(profile),
233 browser_context->IsOffTheRecord());
234 }
235
236 TestAutofillDialogView* GetView() {
237 return static_cast<TestAutofillDialogView*>(view());
238 }
239
240 TestPersonalDataManager* GetTestingManager() {
241 return &test_manager_;
242 }
243
244 MockAddressValidator* GetMockValidator() {
245 return &mock_validator_;
246 }
247
248 const GURL& open_tab_url() { return open_tab_url_; }
249
250 void SimulateSubmitButtonDelayBegin() {
251 AutofillDialogControllerImpl::SubmitButtonDelayBegin();
252 }
253
254 void SimulateSubmitButtonDelayEnd() {
255 AutofillDialogControllerImpl::SubmitButtonDelayEndForTesting();
256 }
257
258 // Returns the number of times that the submit button was delayed.
259 int get_submit_button_delay_count() const {
260 return submit_button_delay_count_;
261 }
262
263 MOCK_METHOD0(LoadRiskFingerprintData, void());
264 using AutofillDialogControllerImpl::IsEditingExistingData;
265 using AutofillDialogControllerImpl::IsManuallyEditingSection;
266 using AutofillDialogControllerImpl::popup_input_type;
267
268 protected:
269 PersonalDataManager* GetManager() const override {
270 return const_cast<TestAutofillDialogController*>(this)->
271 GetTestingManager();
272 }
273
274 AddressValidator* GetValidator() override {
275 return &mock_validator_;
276 }
277
278 void OpenTabWithUrl(const GURL& url) override {
279 open_tab_url_ = url;
280 }
281
282 // AutofillDialogControllerImpl calls this method before showing the dialog
283 // window.
284 void SubmitButtonDelayBegin() override {
285 // Do not delay enabling the submit button in testing.
286 submit_button_delay_count_++;
287 }
288
289 private:
290 TestPersonalDataManager test_manager_;
291
292 // A mock validator object to prevent network requests and track when
293 // validation rules are loaded or validation attempts occur.
294 testing::NiceMock<MockAddressValidator> mock_validator_;
295
296 GURL open_tab_url_;
297
298 // The number of times that the submit button was delayed.
299 int submit_button_delay_count_;
300
301 DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogController);
302 };
303
304 class AutofillDialogControllerTest : public ChromeRenderViewHostTestHarness {
305 protected:
306 AutofillDialogControllerTest() : form_structure_(NULL) {
307 CountryNames::SetLocaleString("en-US");
308 }
309
310 // testing::Test implementation:
311 void SetUp() override {
312 ChromeRenderViewHostTestHarness::SetUp();
313 Reset();
314 }
315
316 void TearDown() override {
317 if (controller_)
318 controller_->ViewClosed();
319 ChromeRenderViewHostTestHarness::TearDown();
320 }
321
322 void Reset() {
323 if (controller_)
324 controller_->ViewClosed();
325
326 profile()->GetPrefs()->ClearPref(::prefs::kAutofillDialogSaveData);
327
328 // We have to clear the old local state before creating a new one.
329 scoped_local_state_.reset();
330 scoped_local_state_.reset(new ScopedTestingLocalState(
331 TestingBrowserProcess::GetGlobal()));
332
333 SetUpControllerWithFormData(DefaultFormData());
334 }
335
336 FormData DefaultFormData() {
337 FormData form_data;
338 for (size_t i = 0; i < arraysize(kFieldsFromPage); ++i) {
339 FormFieldData field;
340 field.autocomplete_attribute = kFieldsFromPage[i];
341 form_data.fields.push_back(field);
342 }
343 return form_data;
344 }
345
346 // Creates a new controller for |form_data|.
347 void ResetControllerWithFormData(const FormData& form_data) {
348 if (controller_)
349 controller_->ViewClosed();
350
351 AutofillClient::ResultCallback callback =
352 base::Bind(&AutofillDialogControllerTest::FinishedCallback,
353 base::Unretained(this));
354 controller_ = (new testing::NiceMock<TestAutofillDialogController>(
355 web_contents(),
356 form_data,
357 GURL(kSourceUrl),
358 callback))->AsWeakPtr();
359 controller_->Init(profile());
360 }
361
362 // Creates a new controller for |form_data|.
363 void SetUpControllerWithFormData(const FormData& form_data) {
364 ResetControllerWithFormData(form_data);
365 controller()->Show();
366 }
367
368 // Fills the inputs in SECTION_CC with data.
369 void FillCreditCardInputs() {
370 FieldValueMap cc_outputs;
371 const DetailInputs& cc_inputs =
372 controller()->RequestedFieldsForSection(SECTION_CC);
373 for (size_t i = 0; i < cc_inputs.size(); ++i) {
374 cc_outputs[cc_inputs[i].type] = cc_inputs[i].type == CREDIT_CARD_NUMBER ?
375 ASCIIToUTF16(kTestCCNumberVisa) : ASCIIToUTF16("11");
376 }
377 controller()->GetView()->SetUserInput(SECTION_CC, cc_outputs);
378 }
379
380 // Activates the 'Add new foo' option from the |section|'s suggestions
381 // dropdown and fills the |section|'s inputs with the data from the
382 // |data_model|. If |section| is SECTION_CC, also fills in '123' for the CVC.
383 void FillInputs(DialogSection section, const AutofillDataModel& data_model) {
384 // Select the 'Add new foo' option.
385 ui::MenuModel* model = GetMenuModelForSection(section);
386 if (model)
387 model->ActivatedAt(model->GetItemCount() - 2);
388
389 // Fill the inputs.
390 FieldValueMap outputs;
391 const DetailInputs& inputs =
392 controller()->RequestedFieldsForSection(section);
393 for (size_t i = 0; i < inputs.size(); ++i) {
394 ServerFieldType type = inputs[i].type;
395 base::string16 output;
396 if (type == CREDIT_CARD_VERIFICATION_CODE)
397 output = ASCIIToUTF16("123");
398 else
399 output = data_model.GetInfo(AutofillType(type), "en-US");
400 outputs[inputs[i].type] = output;
401 }
402 controller()->GetView()->SetUserInput(section, outputs);
403 }
404
405 std::vector<DialogNotification> NotificationsOfType(
406 DialogNotification::Type type) {
407 std::vector<DialogNotification> right_type;
408 const std::vector<DialogNotification>& notifications =
409 controller()->CurrentNotifications();
410 for (size_t i = 0; i < notifications.size(); ++i) {
411 if (notifications[i].type() == type)
412 right_type.push_back(notifications[i]);
413 }
414 return right_type;
415 }
416
417 void UseBillingForShipping() {
418 controller()->MenuModelForSection(SECTION_SHIPPING)->ActivatedAt(0);
419 }
420
421 base::string16 ValidateCCNumber(DialogSection section,
422 const std::string& cc_number,
423 bool should_pass) {
424 FieldValueMap outputs;
425 outputs[ADDRESS_BILLING_COUNTRY] = ASCIIToUTF16("United States");
426 outputs[CREDIT_CARD_NUMBER] = UTF8ToUTF16(cc_number);
427 ValidityMessages messages =
428 controller()->InputsAreValid(section, outputs);
429 EXPECT_EQ(should_pass, !messages.HasSureError(CREDIT_CARD_NUMBER));
430 return messages.GetMessageOrDefault(CREDIT_CARD_NUMBER).text;
431 }
432
433 // Returns true if the given |section| contains a field of the given |type|.
434 bool SectionContainsField(DialogSection section, ServerFieldType type) {
435 const DetailInputs& inputs =
436 controller()->RequestedFieldsForSection(section);
437 for (DetailInputs::const_iterator it = inputs.begin(); it != inputs.end();
438 ++it) {
439 if (it->type == type)
440 return true;
441 }
442 return false;
443 }
444
445 SuggestionsMenuModel* GetMenuModelForSection(DialogSection section) {
446 ui::MenuModel* model = controller()->MenuModelForSection(section);
447 return static_cast<SuggestionsMenuModel*>(model);
448 }
449
450 void SubmitAndVerifyShippingAndBillingResults() {
451 // Test after setting use billing for shipping.
452 UseBillingForShipping();
453
454 controller()->OnAccept();
455
456 ASSERT_EQ(20U, form_structure()->field_count());
457 EXPECT_EQ(ADDRESS_HOME_COUNTRY,
458 form_structure()->field(11)->Type().GetStorableType());
459 EXPECT_EQ(ADDRESS_BILLING, form_structure()->field(11)->Type().group());
460 EXPECT_EQ(ADDRESS_HOME_COUNTRY,
461 form_structure()->field(18)->Type().GetStorableType());
462 EXPECT_EQ(ADDRESS_HOME, form_structure()->field(18)->Type().group());
463 base::string16 billing_country = form_structure()->field(11)->value;
464 EXPECT_EQ(2U, billing_country.size());
465 base::string16 shipping_country = form_structure()->field(18)->value;
466 EXPECT_EQ(2U, shipping_country.size());
467 EXPECT_FALSE(billing_country.empty());
468 EXPECT_FALSE(shipping_country.empty());
469 EXPECT_EQ(billing_country, shipping_country);
470
471 EXPECT_EQ(CREDIT_CARD_NAME_FULL,
472 form_structure()->field(1)->Type().GetStorableType());
473 base::string16 cc_name = form_structure()->field(1)->value;
474 EXPECT_EQ(NAME_FULL, form_structure()->field(6)->Type().GetStorableType());
475 EXPECT_EQ(NAME_BILLING, form_structure()->field(6)->Type().group());
476 base::string16 billing_name = form_structure()->field(6)->value;
477 EXPECT_EQ(NAME_FULL, form_structure()->field(13)->Type().GetStorableType());
478 EXPECT_EQ(NAME, form_structure()->field(13)->Type().group());
479 base::string16 shipping_name = form_structure()->field(13)->value;
480
481 EXPECT_FALSE(cc_name.empty());
482 EXPECT_FALSE(billing_name.empty());
483 EXPECT_FALSE(shipping_name.empty());
484 EXPECT_EQ(cc_name, billing_name);
485 EXPECT_EQ(cc_name, shipping_name);
486 }
487
488 TestAutofillDialogController* controller() { return controller_.get(); }
489
490 const FormStructure* form_structure() { return form_structure_; }
491
492 private:
493 void FinishedCallback(AutofillClient::RequestAutocompleteResult result,
494 const base::string16& debug_message,
495 const FormStructure* form_structure) {
496 form_structure_ = form_structure;
497 }
498
499 #if defined(OS_WIN)
500 // http://crbug.com/227221
501 ui::ScopedOleInitializer ole_initializer_;
502 #endif
503
504 // The controller owns itself.
505 base::WeakPtr<TestAutofillDialogController> controller_;
506
507 // Returned when the dialog closes successfully.
508 const FormStructure* form_structure_;
509
510 std::unique_ptr<ScopedTestingLocalState> scoped_local_state_;
511 };
512
513 } // namespace
514
515 TEST_F(AutofillDialogControllerTest, RefuseToShowWithNoAutocompleteAttributes) {
516 FormFieldData email_field;
517 email_field.name = ASCIIToUTF16("email");
518 FormFieldData cc_field;
519 cc_field.name = ASCIIToUTF16("cc");
520 FormFieldData billing_field;
521 billing_field.name = ASCIIToUTF16("billing name");
522
523 FormData form_data;
524 form_data.fields.push_back(email_field);
525 form_data.fields.push_back(cc_field);
526 form_data.fields.push_back(billing_field);
527
528 SetUpControllerWithFormData(form_data);
529 EXPECT_FALSE(controller());
530 }
531
532 TEST_F(AutofillDialogControllerTest, RefuseToShowWithNoCcField) {
533 FormFieldData shipping_tel;
534 shipping_tel.autocomplete_attribute = "shipping tel";
535
536 FormData form_data;
537 form_data.fields.push_back(shipping_tel);
538
539 SetUpControllerWithFormData(form_data);
540 EXPECT_FALSE(controller());
541
542 // Any cc- field will do.
543 FormFieldData cc_field;
544 cc_field.autocomplete_attribute = "cc-csc";
545 form_data.fields.push_back(cc_field);
546
547 SetUpControllerWithFormData(form_data);
548 EXPECT_TRUE(controller());
549 }
550
551 // Ensure the default ValidityMessage has the expected values.
552 TEST_F(AutofillDialogControllerTest, DefaultValidityMessage) {
553 ValidityMessages messages;
554 ValidityMessage message = messages.GetMessageOrDefault(UNKNOWN_TYPE);
555 EXPECT_FALSE(message.sure);
556 EXPECT_TRUE(message.text.empty());
557 }
558
559 // This test makes sure nothing falls over when fields are being validity-
560 // checked.
561 TEST_F(AutofillDialogControllerTest, ValidityCheck) {
562 for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
563 DialogSection section = static_cast<DialogSection>(i);
564 const DetailInputs& shipping_inputs =
565 controller()->RequestedFieldsForSection(section);
566 for (DetailInputs::const_iterator iter = shipping_inputs.begin();
567 iter != shipping_inputs.end(); ++iter) {
568 controller()->InputValidityMessage(section, iter->type, base::string16());
569 }
570 }
571 }
572
573 // Test for phone number validation.
574 TEST_F(AutofillDialogControllerTest, PhoneNumberValidation) {
575 for (size_t i = 0; i < 2; ++i) {
576 ServerFieldType phone = i == 0 ? PHONE_HOME_WHOLE_NUMBER :
577 PHONE_BILLING_WHOLE_NUMBER;
578 ServerFieldType address = i == 0 ? ADDRESS_HOME_COUNTRY :
579 ADDRESS_BILLING_COUNTRY;
580 DialogSection section = i == 0 ? SECTION_SHIPPING : SECTION_BILLING;
581
582 FieldValueMap outputs;
583 const DetailInputs& inputs =
584 controller()->RequestedFieldsForSection(section);
585 AutofillProfile full_profile(test::GetVerifiedProfile());
586 for (size_t j = 0; j < inputs.size(); ++j) {
587 const ServerFieldType type = inputs[j].type;
588 outputs[type] = full_profile.GetInfo(AutofillType(type), "en-US");
589 }
590
591 // Make sure country is United States.
592 outputs[address] = ASCIIToUTF16("United States");
593
594 // Existing data should have no errors.
595 ValidityMessages messages = controller()->InputsAreValid(section, outputs);
596 EXPECT_FALSE(HasAnyError(messages, phone));
597
598 // Input an empty phone number.
599 outputs[phone] = base::string16();
600 messages = controller()->InputsAreValid(section, outputs);
601 EXPECT_TRUE(HasUnsureError(messages, phone));
602
603 // Input an invalid phone number.
604 outputs[phone] = ASCIIToUTF16("ABC");
605 messages = controller()->InputsAreValid(section, outputs);
606 EXPECT_TRUE(messages.HasSureError(phone));
607
608 // Input a local phone number.
609 outputs[phone] = ASCIIToUTF16("2155546699");
610 messages = controller()->InputsAreValid(section, outputs);
611 EXPECT_FALSE(HasAnyError(messages, phone));
612
613 // Input an invalid local phone number.
614 outputs[phone] = ASCIIToUTF16("215554669");
615 messages = controller()->InputsAreValid(section, outputs);
616 EXPECT_TRUE(messages.HasSureError(phone));
617
618 // Input an international phone number.
619 outputs[phone] = ASCIIToUTF16("+33 892 70 12 39");
620 messages = controller()->InputsAreValid(section, outputs);
621 EXPECT_FALSE(HasAnyError(messages, phone));
622
623 // Input an invalid international phone number.
624 outputs[phone] = ASCIIToUTF16("+112333 892 70 12 39");
625 messages = controller()->InputsAreValid(section, outputs);
626 EXPECT_TRUE(messages.HasSureError(phone));
627
628 // Input a valid Canadian number.
629 outputs[phone] = ASCIIToUTF16("+1 506 887 1234");
630 messages = controller()->InputsAreValid(section, outputs);
631 EXPECT_FALSE(HasAnyError(messages, phone));
632
633 // Input a valid Canadian number without the country code.
634 outputs[phone] = ASCIIToUTF16("506 887 1234");
635 messages = controller()->InputsAreValid(section, outputs);
636 EXPECT_TRUE(HasAnyError(messages, phone));
637
638 // Input a valid Canadian toll-free number.
639 outputs[phone] = ASCIIToUTF16("310 1234");
640 messages = controller()->InputsAreValid(section, outputs);
641 EXPECT_TRUE(HasAnyError(messages, phone));
642 }
643 }
644
645 TEST_F(AutofillDialogControllerTest, ExpirationDateValidity) {
646 ui::ComboboxModel* exp_year_model =
647 controller()->ComboboxModelForAutofillType(CREDIT_CARD_EXP_4_DIGIT_YEAR);
648 ui::ComboboxModel* exp_month_model =
649 controller()->ComboboxModelForAutofillType(CREDIT_CARD_EXP_MONTH);
650
651 base::string16 default_year_value =
652 exp_year_model->GetItemAt(exp_year_model->GetDefaultIndex());
653 base::string16 default_month_value =
654 exp_month_model->GetItemAt(exp_month_model->GetDefaultIndex());
655
656 base::string16 other_year_value =
657 exp_year_model->GetItemAt(exp_year_model->GetItemCount() - 1);
658 base::string16 other_month_value =
659 exp_month_model->GetItemAt(exp_month_model->GetItemCount() - 1);
660
661 FieldValueMap outputs;
662 outputs[CREDIT_CARD_EXP_MONTH] = default_month_value;
663 outputs[CREDIT_CARD_EXP_4_DIGIT_YEAR] = default_year_value;
664
665 // Expiration default values generate unsure validation errors (but not sure).
666 ValidityMessages messages = controller()->InputsAreValid(SECTION_CC, outputs);
667 EXPECT_TRUE(HasUnsureError(messages, CREDIT_CARD_EXP_4_DIGIT_YEAR));
668 EXPECT_TRUE(HasUnsureError(messages, CREDIT_CARD_EXP_MONTH));
669
670 // Expiration date with default month fails.
671 outputs[CREDIT_CARD_EXP_4_DIGIT_YEAR] = other_year_value;
672 messages = controller()->InputsAreValid(SECTION_CC, outputs);
673 EXPECT_FALSE(HasUnsureError(messages, CREDIT_CARD_EXP_4_DIGIT_YEAR));
674 EXPECT_TRUE(HasUnsureError(messages, CREDIT_CARD_EXP_MONTH));
675
676 // Expiration date with default year fails.
677 outputs[CREDIT_CARD_EXP_MONTH] = other_month_value;
678 outputs[CREDIT_CARD_EXP_4_DIGIT_YEAR] = default_year_value;
679 messages = controller()->InputsAreValid(SECTION_CC, outputs);
680 EXPECT_TRUE(HasUnsureError(messages, CREDIT_CARD_EXP_4_DIGIT_YEAR));
681 EXPECT_FALSE(HasUnsureError(messages, CREDIT_CARD_EXP_MONTH));
682 }
683
684 TEST_F(AutofillDialogControllerTest, BillingNameValidation) {
685 FieldValueMap outputs;
686 outputs[ADDRESS_BILLING_COUNTRY] = ASCIIToUTF16("United States");
687
688 // Input an empty billing name.
689 outputs[NAME_BILLING_FULL] = base::string16();
690 ValidityMessages messages = controller()->InputsAreValid(SECTION_BILLING,
691 outputs);
692 EXPECT_TRUE(HasUnsureError(messages, NAME_BILLING_FULL));
693
694 // Input a non-empty billing name.
695 outputs[NAME_BILLING_FULL] = ASCIIToUTF16("Bob");
696 messages = controller()->InputsAreValid(SECTION_BILLING, outputs);
697 EXPECT_FALSE(HasAnyError(messages, NAME_BILLING_FULL));
698 }
699
700 TEST_F(AutofillDialogControllerTest, CreditCardNumberValidation) {
701 // Should accept AMEX, Visa, Master and Discover.
702 ValidateCCNumber(SECTION_CC, kTestCCNumberVisa, true);
703 ValidateCCNumber(SECTION_CC, kTestCCNumberMaster, true);
704 ValidateCCNumber(SECTION_CC, kTestCCNumberDiscover, true);
705 ValidateCCNumber(SECTION_CC, kTestCCNumberAmex, true);
706 ValidateCCNumber(SECTION_CC, kTestCCNumberIncomplete, false);
707 ValidateCCNumber(SECTION_CC, kTestCCNumberInvalid, false);
708 }
709
710 TEST_F(AutofillDialogControllerTest, AutofillProfiles) {
711 ui::MenuModel* shipping_model =
712 controller()->MenuModelForSection(SECTION_SHIPPING);
713 // Since the PersonalDataManager is empty, this should only have the
714 // "use billing", "add new" and "manage" menu items.
715 ASSERT_TRUE(shipping_model);
716 EXPECT_EQ(3, shipping_model->GetItemCount());
717 // On the other hand, the other models should be NULL when there's no
718 // suggestion.
719 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC));
720 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_BILLING));
721
722 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(3);
723
724 // Empty profiles are ignored.
725 AutofillProfile empty_profile(base::GenerateGUID(), kSettingsOrigin);
726 empty_profile.SetRawInfo(NAME_FULL, ASCIIToUTF16("John Doe"));
727 controller()->GetTestingManager()->AddTestingProfile(&empty_profile);
728 shipping_model = controller()->MenuModelForSection(SECTION_SHIPPING);
729 ASSERT_TRUE(shipping_model);
730 EXPECT_EQ(3, shipping_model->GetItemCount());
731
732 // An otherwise full but unverified profile should be ignored.
733 AutofillProfile full_profile(test::GetFullProfile());
734 full_profile.set_origin("https://www.example.com");
735 full_profile.SetRawInfo(ADDRESS_HOME_LINE2, base::string16());
736 controller()->GetTestingManager()->AddTestingProfile(&full_profile);
737 shipping_model = controller()->MenuModelForSection(SECTION_SHIPPING);
738 ASSERT_TRUE(shipping_model);
739 EXPECT_EQ(3, shipping_model->GetItemCount());
740
741 // A full, verified profile should be picked up.
742 AutofillProfile verified_profile(test::GetVerifiedProfile());
743 verified_profile.SetRawInfo(ADDRESS_HOME_LINE2, base::string16());
744 controller()->GetTestingManager()->AddTestingProfile(&verified_profile);
745 shipping_model = controller()->MenuModelForSection(SECTION_SHIPPING);
746 ASSERT_TRUE(shipping_model);
747 EXPECT_EQ(4, shipping_model->GetItemCount());
748 }
749
750 // Checks that a valid profile is selected by default, but if invalid is
751 // popped into edit mode.
752 TEST_F(AutofillDialogControllerTest, AutofillProfilesPopInvalidIntoEdit) {
753 SuggestionsMenuModel* shipping_model =
754 GetMenuModelForSection(SECTION_SHIPPING);
755 EXPECT_EQ(3, shipping_model->GetItemCount());
756 // "Same as billing" is selected.
757 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_SHIPPING));
758 EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_BILLING));
759
760 AutofillProfile verified_profile(test::GetVerifiedProfile());
761 controller()->GetTestingManager()->AddTestingProfile(&verified_profile);
762 EXPECT_EQ(4, shipping_model->GetItemCount());
763 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_SHIPPING));
764 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_BILLING));
765
766 // Now make up a problem and make sure the profile isn't in the list.
767 Reset();
768 FieldProblemMap problems;
769 problems.insert(std::make_pair(::i18n::addressinput::POSTAL_CODE,
770 ::i18n::addressinput::MISMATCHING_VALUE));
771 EXPECT_CALL(*controller()->GetMockValidator(),
772 ValidateAddress(CountryCodeMatcher("US"), _, _)).
773 WillRepeatedly(DoAll(SetArgPointee<2>(problems),
774 Return(AddressValidator::SUCCESS)));
775
776 controller()->GetTestingManager()->AddTestingProfile(&verified_profile);
777 shipping_model = GetMenuModelForSection(SECTION_SHIPPING);
778 EXPECT_EQ(4, shipping_model->GetItemCount());
779 EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_SHIPPING));
780 EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_BILLING));
781 }
782
783 // Makes sure suggestion profiles are re-validated when validation rules load.
784 TEST_F(AutofillDialogControllerTest, AutofillProfilesRevalidateAfterRulesLoad) {
785 SuggestionsMenuModel* shipping_model =
786 GetMenuModelForSection(SECTION_SHIPPING);
787 EXPECT_EQ(3, shipping_model->GetItemCount());
788 // "Same as billing" is selected.
789 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_SHIPPING));
790 EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_BILLING));
791 AutofillProfile verified_profile(test::GetVerifiedProfile());
792 controller()->GetTestingManager()->AddTestingProfile(&verified_profile);
793 EXPECT_EQ(4, shipping_model->GetItemCount());
794 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_SHIPPING));
795 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_BILLING));
796
797 FieldProblemMap problems;
798 problems.insert(std::make_pair(::i18n::addressinput::POSTAL_CODE,
799 ::i18n::addressinput::MISMATCHING_VALUE));
800 EXPECT_CALL(*controller()->GetMockValidator(),
801 ValidateAddress(CountryCodeMatcher("US"), _, _)).
802 WillRepeatedly(DoAll(SetArgPointee<2>(problems),
803 Return(AddressValidator::SUCCESS)));
804
805 controller()->OnAddressValidationRulesLoaded("US", true);
806 EXPECT_EQ(4, shipping_model->GetItemCount());
807 EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_SHIPPING));
808 EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_BILLING));
809 }
810
811 // Makes sure that the choice of which Autofill profile to use for each section
812 // is sticky.
813 TEST_F(AutofillDialogControllerTest, AutofillProfileDefaults) {
814 AutofillProfile profile(test::GetVerifiedProfile());
815 AutofillProfile profile2(test::GetVerifiedProfile2());
816 controller()->GetTestingManager()->AddTestingProfile(&profile);
817 controller()->GetTestingManager()->AddTestingProfile(&profile2);
818
819 // Until a selection has been made, the default shipping suggestion is the
820 // first one (after "use billing").
821 SuggestionsMenuModel* shipping_model =
822 GetMenuModelForSection(SECTION_SHIPPING);
823 EXPECT_EQ(1, shipping_model->checked_item());
824
825 for (int i = 2; i >= 0; --i) {
826 shipping_model = GetMenuModelForSection(SECTION_SHIPPING);
827 shipping_model->ExecuteCommand(i, 0);
828 FillCreditCardInputs();
829 controller()->OnAccept();
830
831 Reset();
832 controller()->GetTestingManager()->AddTestingProfile(&profile);
833 controller()->GetTestingManager()->AddTestingProfile(&profile2);
834 shipping_model = GetMenuModelForSection(SECTION_SHIPPING);
835 EXPECT_EQ(i, shipping_model->checked_item());
836 }
837
838 // Try again, but don't add the default profile to the PDM. The dialog
839 // should fall back to the first profile.
840 shipping_model->ExecuteCommand(2, 0);
841 FillCreditCardInputs();
842 controller()->OnAccept();
843 Reset();
844 controller()->GetTestingManager()->AddTestingProfile(&profile);
845 shipping_model = GetMenuModelForSection(SECTION_SHIPPING);
846 EXPECT_EQ(1, shipping_model->checked_item());
847 }
848
849 // Makes sure that a newly added Autofill profile becomes set as the default
850 // choice for the next run.
851 TEST_F(AutofillDialogControllerTest, NewAutofillProfileIsDefault) {
852
853 AutofillProfile profile(test::GetVerifiedProfile());
854 CreditCard credit_card(test::GetVerifiedCreditCard());
855 controller()->GetTestingManager()->AddTestingProfile(&profile);
856 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
857
858 // Until a selection has been made, the default suggestion is the first one.
859 // For the shipping section, this follows the "use billing" suggestion.
860 EXPECT_EQ(0, GetMenuModelForSection(SECTION_CC)->checked_item());
861 EXPECT_EQ(1, GetMenuModelForSection(SECTION_SHIPPING)->checked_item());
862
863 // Fill in the shipping and credit card sections with new data.
864 AutofillProfile new_profile(test::GetVerifiedProfile2());
865 CreditCard new_credit_card(test::GetVerifiedCreditCard2());
866 FillInputs(SECTION_SHIPPING, new_profile);
867 FillInputs(SECTION_CC, new_credit_card);
868 controller()->GetView()->CheckSaveDetailsLocallyCheckbox(true);
869 controller()->OnAccept();
870
871 // Update the |new_profile| and |new_credit_card|'s guids to the saved ones.
872 new_profile.set_guid(
873 controller()->GetTestingManager()->imported_profile().guid());
874 new_credit_card.set_guid(
875 controller()->GetTestingManager()->imported_credit_card().guid());
876
877 // Reload the dialog. The newly added address should now be set as the
878 // default.
879 Reset();
880 controller()->GetTestingManager()->AddTestingProfile(&profile);
881 controller()->GetTestingManager()->AddTestingProfile(&new_profile);
882 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
883
884 // Until a selection has been made, the default suggestion is the first one.
885 // For the shipping section, this follows the "use billing" suggestion.
886 EXPECT_EQ(2, GetMenuModelForSection(SECTION_SHIPPING)->checked_item());
887 }
888
889 TEST_F(AutofillDialogControllerTest, SuggestValidEmail) {
890 AutofillProfile profile(test::GetVerifiedProfile());
891 const base::string16 kValidEmail = ASCIIToUTF16(kFakeEmail);
892 profile.SetRawInfo(EMAIL_ADDRESS, kValidEmail);
893 controller()->GetTestingManager()->AddTestingProfile(&profile);
894
895 // "add", "manage", and 1 suggestion.
896 EXPECT_EQ(
897 3, controller()->MenuModelForSection(SECTION_BILLING)->GetItemCount());
898 // "add", "manage", 1 suggestion, and "same as billing".
899 EXPECT_EQ(
900 4, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
901 }
902
903 TEST_F(AutofillDialogControllerTest, DoNotSuggestInvalidEmail) {
904 AutofillProfile profile(test::GetVerifiedProfile());
905 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16(".!#$%&'*+/=?^_`-@-.."));
906 controller()->GetTestingManager()->AddTestingProfile(&profile);
907
908 EXPECT_FALSE(!!controller()->MenuModelForSection(SECTION_BILLING));
909 // "add", "manage", 1 suggestion, and "same as billing".
910 EXPECT_EQ(
911 4, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
912 }
913
914 TEST_F(AutofillDialogControllerTest, SuggestValidAddress) {
915 AutofillProfile full_profile(test::GetVerifiedProfile());
916 full_profile.set_origin(kSettingsOrigin);
917 controller()->GetTestingManager()->AddTestingProfile(&full_profile);
918 // "add", "manage", and 1 suggestion.
919 EXPECT_EQ(
920 3, controller()->MenuModelForSection(SECTION_BILLING)->GetItemCount());
921 }
922
923 TEST_F(AutofillDialogControllerTest, DoNotSuggestInvalidAddress) {
924 AutofillProfile full_profile(test::GetVerifiedProfile());
925 full_profile.set_origin(kSettingsOrigin);
926 full_profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("C"));
927 controller()->GetTestingManager()->AddTestingProfile(&full_profile);
928 }
929
930 TEST_F(AutofillDialogControllerTest, DoNotSuggestIncompleteAddress) {
931 AutofillProfile profile(test::GetVerifiedProfile());
932 profile.SetRawInfo(ADDRESS_HOME_STATE, base::string16());
933 controller()->GetTestingManager()->AddTestingProfile(&profile);
934
935 // Same as shipping, manage, add new.
936 EXPECT_EQ(3,
937 controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
938 EXPECT_FALSE(!!controller()->MenuModelForSection(SECTION_BILLING));
939 }
940
941 TEST_F(AutofillDialogControllerTest, DoSuggestShippingAddressWithoutEmail) {
942 AutofillProfile profile(test::GetVerifiedProfile());
943 profile.SetRawInfo(EMAIL_ADDRESS, base::string16());
944 controller()->GetTestingManager()->AddTestingProfile(&profile);
945
946 // Same as shipping, manage, add new, profile with missing email.
947 EXPECT_EQ(4,
948 controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
949 // Billing addresses require email.
950 EXPECT_FALSE(!!controller()->MenuModelForSection(SECTION_BILLING));
951 }
952
953 TEST_F(AutofillDialogControllerTest, AutofillCreditCards) {
954 // Since the PersonalDataManager is empty, this should only have the
955 // default menu items.
956 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC));
957
958 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(3);
959
960 // Empty cards are ignored.
961 CreditCard empty_card(base::GenerateGUID(), kSettingsOrigin);
962 empty_card.SetRawInfo(CREDIT_CARD_NAME_FULL, ASCIIToUTF16("John Doe"));
963 controller()->GetTestingManager()->AddTestingCreditCard(&empty_card);
964 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC));
965
966 // An otherwise full but unverified card should be ignored.
967 CreditCard full_card(test::GetCreditCard());
968 full_card.set_origin("https://www.example.com");
969 controller()->GetTestingManager()->AddTestingCreditCard(&full_card);
970 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC));
971
972 // A full, verified card should be picked up.
973 CreditCard verified_card(test::GetCreditCard());
974 verified_card.set_origin(kSettingsOrigin);
975 controller()->GetTestingManager()->AddTestingCreditCard(&verified_card);
976 ui::MenuModel* credit_card_model =
977 controller()->MenuModelForSection(SECTION_CC);
978 ASSERT_TRUE(credit_card_model);
979 EXPECT_EQ(3, credit_card_model->GetItemCount());
980 }
981
982 // Test selecting a shipping address different from billing as address.
983 TEST_F(AutofillDialogControllerTest, DontUseBillingAsShipping) {
984 AutofillProfile full_profile(test::GetVerifiedProfile());
985 AutofillProfile full_profile2(test::GetVerifiedProfile2());
986 CreditCard credit_card(test::GetVerifiedCreditCard());
987 controller()->GetTestingManager()->AddTestingProfile(&full_profile);
988 controller()->GetTestingManager()->AddTestingProfile(&full_profile2);
989 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
990 ui::MenuModel* shipping_model =
991 controller()->MenuModelForSection(SECTION_SHIPPING);
992 shipping_model->ActivatedAt(2);
993
994 controller()->OnAccept();
995 ASSERT_EQ(20U, form_structure()->field_count());
996 EXPECT_EQ(ADDRESS_HOME_STATE,
997 form_structure()->field(9)->Type().GetStorableType());
998 EXPECT_EQ(ADDRESS_BILLING, form_structure()->field(9)->Type().group());
999 EXPECT_EQ(ADDRESS_HOME_STATE,
1000 form_structure()->field(16)->Type().GetStorableType());
1001 EXPECT_EQ(ADDRESS_HOME, form_structure()->field(16)->Type().group());
1002 base::string16 billing_state = form_structure()->field(9)->value;
1003 base::string16 shipping_state = form_structure()->field(16)->value;
1004 EXPECT_FALSE(billing_state.empty());
1005 EXPECT_FALSE(shipping_state.empty());
1006 EXPECT_NE(billing_state, shipping_state);
1007
1008 EXPECT_EQ(CREDIT_CARD_NAME_FULL,
1009 form_structure()->field(1)->Type().GetStorableType());
1010 base::string16 cc_name = form_structure()->field(1)->value;
1011 EXPECT_EQ(NAME_FULL, form_structure()->field(6)->Type().GetStorableType());
1012 EXPECT_EQ(NAME_BILLING, form_structure()->field(6)->Type().group());
1013 base::string16 billing_name = form_structure()->field(6)->value;
1014 EXPECT_EQ(NAME_FULL, form_structure()->field(13)->Type().GetStorableType());
1015 EXPECT_EQ(NAME, form_structure()->field(13)->Type().group());
1016 base::string16 shipping_name = form_structure()->field(13)->value;
1017
1018 EXPECT_FALSE(cc_name.empty());
1019 EXPECT_FALSE(billing_name.empty());
1020 EXPECT_FALSE(shipping_name.empty());
1021 // Billing name should always be the same as cardholder name.
1022 EXPECT_EQ(cc_name, billing_name);
1023 EXPECT_NE(cc_name, shipping_name);
1024 }
1025
1026 // Test selecting UseBillingForShipping.
1027 TEST_F(AutofillDialogControllerTest, UseBillingAsShipping) {
1028 AutofillProfile full_profile(test::GetVerifiedProfile());
1029 controller()->GetTestingManager()->AddTestingProfile(&full_profile);
1030
1031 AutofillProfile full_profile2(test::GetVerifiedProfile2());
1032 controller()->GetTestingManager()->AddTestingProfile(&full_profile2);
1033
1034 CreditCard credit_card(test::GetVerifiedCreditCard());
1035 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
1036
1037 ASSERT_FALSE(controller()->IsManuallyEditingSection(SECTION_CC));
1038 ASSERT_FALSE(controller()->IsManuallyEditingSection(SECTION_BILLING));
1039
1040 SubmitAndVerifyShippingAndBillingResults();
1041 }
1042
1043 TEST_F(AutofillDialogControllerTest, UseBillingAsShippingManualInput) {
1044 ASSERT_TRUE(controller()->IsManuallyEditingSection(SECTION_CC));
1045 ASSERT_TRUE(controller()->IsManuallyEditingSection(SECTION_BILLING));
1046
1047 CreditCard credit_card(test::GetVerifiedCreditCard());
1048 FillInputs(SECTION_CC, credit_card);
1049
1050 AutofillProfile full_profile(test::GetVerifiedProfile());
1051 FillInputs(SECTION_BILLING, full_profile);
1052
1053 SubmitAndVerifyShippingAndBillingResults();
1054 }
1055
1056 // Tests that shipping and billing telephone fields are supported, and filled
1057 // in by their respective profiles. http://crbug.com/244515
1058 TEST_F(AutofillDialogControllerTest, BillingVsShippingPhoneNumber) {
1059 FormFieldData shipping_tel;
1060 shipping_tel.autocomplete_attribute = "shipping tel";
1061 FormFieldData billing_tel;
1062 billing_tel.autocomplete_attribute = "billing tel";
1063 FormFieldData cc_field;
1064 cc_field.autocomplete_attribute = "cc-csc";
1065
1066 FormData form_data;
1067 form_data.fields.push_back(shipping_tel);
1068 form_data.fields.push_back(billing_tel);
1069 form_data.fields.push_back(cc_field);
1070 SetUpControllerWithFormData(form_data);
1071
1072 // The profile that will be chosen for the shipping section.
1073 AutofillProfile shipping_profile(test::GetVerifiedProfile());
1074 // The profile that will be chosen for the billing section.
1075 AutofillProfile billing_profile(test::GetVerifiedProfile2());
1076 CreditCard credit_card(test::GetVerifiedCreditCard());
1077 controller()->GetTestingManager()->AddTestingProfile(&shipping_profile);
1078 controller()->GetTestingManager()->AddTestingProfile(&billing_profile);
1079 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
1080 ui::MenuModel* billing_model =
1081 controller()->MenuModelForSection(SECTION_BILLING);
1082 billing_model->ActivatedAt(1);
1083
1084 controller()->OnAccept();
1085 ASSERT_EQ(3U, form_structure()->field_count());
1086 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER,
1087 form_structure()->field(0)->Type().GetStorableType());
1088 EXPECT_EQ(PHONE_HOME, form_structure()->field(0)->Type().group());
1089 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER,
1090 form_structure()->field(1)->Type().GetStorableType());
1091 EXPECT_EQ(PHONE_BILLING, form_structure()->field(1)->Type().group());
1092 EXPECT_EQ(shipping_profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER),
1093 form_structure()->field(0)->value);
1094 EXPECT_EQ(billing_profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER),
1095 form_structure()->field(1)->value);
1096 EXPECT_NE(form_structure()->field(1)->value,
1097 form_structure()->field(0)->value);
1098 }
1099
1100 // Similar to the above, but tests that street-address (i.e. all lines of the
1101 // street address) is successfully filled for both shipping and billing
1102 // sections.
1103 TEST_F(AutofillDialogControllerTest, BillingVsShippingStreetAddress) {
1104 FormFieldData shipping_address;
1105 shipping_address.autocomplete_attribute = "shipping street-address";
1106 FormFieldData billing_address;
1107 billing_address.autocomplete_attribute = "billing street-address";
1108 FormFieldData shipping_address_textarea;
1109 shipping_address_textarea.autocomplete_attribute = "shipping street-address";
1110 shipping_address_textarea.form_control_type = "textarea";
1111 FormFieldData billing_address_textarea;
1112 billing_address_textarea.autocomplete_attribute = "billing street-address";
1113 billing_address_textarea.form_control_type = "textarea";
1114 FormFieldData cc_field;
1115 cc_field.autocomplete_attribute = "cc-csc";
1116
1117 FormData form_data;
1118 form_data.fields.push_back(shipping_address);
1119 form_data.fields.push_back(billing_address);
1120 form_data.fields.push_back(shipping_address_textarea);
1121 form_data.fields.push_back(billing_address_textarea);
1122 form_data.fields.push_back(cc_field);
1123 SetUpControllerWithFormData(form_data);
1124
1125 // The profile that will be chosen for the shipping section.
1126 AutofillProfile shipping_profile(test::GetVerifiedProfile());
1127 // The profile that will be chosen for the billing section.
1128 AutofillProfile billing_profile(test::GetVerifiedProfile2());
1129 CreditCard credit_card(test::GetVerifiedCreditCard());
1130 controller()->GetTestingManager()->AddTestingProfile(&shipping_profile);
1131 controller()->GetTestingManager()->AddTestingProfile(&billing_profile);
1132 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
1133 ui::MenuModel* billing_model =
1134 controller()->MenuModelForSection(SECTION_BILLING);
1135 billing_model->ActivatedAt(1);
1136
1137 controller()->OnAccept();
1138 ASSERT_EQ(5U, form_structure()->field_count());
1139 EXPECT_EQ(ADDRESS_HOME_STREET_ADDRESS,
1140 form_structure()->field(0)->Type().GetStorableType());
1141 EXPECT_EQ(ADDRESS_HOME, form_structure()->field(0)->Type().group());
1142 EXPECT_EQ(ADDRESS_HOME_STREET_ADDRESS,
1143 form_structure()->field(1)->Type().GetStorableType());
1144 EXPECT_EQ(ADDRESS_BILLING, form_structure()->field(1)->Type().group());
1145 // Inexact matching; single-line inputs get the address data concatenated but
1146 // separated by commas.
1147 EXPECT_TRUE(base::StartsWith(form_structure()->field(0)->value,
1148 shipping_profile.GetRawInfo(ADDRESS_HOME_LINE1),
1149 base::CompareCase::SENSITIVE));
1150 EXPECT_TRUE(base::EndsWith(form_structure()->field(0)->value,
1151 shipping_profile.GetRawInfo(ADDRESS_HOME_LINE2),
1152 base::CompareCase::SENSITIVE));
1153 EXPECT_TRUE(base::StartsWith(form_structure()->field(1)->value,
1154 billing_profile.GetRawInfo(ADDRESS_HOME_LINE1),
1155 base::CompareCase::SENSITIVE));
1156 EXPECT_TRUE(base::EndsWith(form_structure()->field(1)->value,
1157 billing_profile.GetRawInfo(ADDRESS_HOME_LINE2),
1158 base::CompareCase::SENSITIVE));
1159 // The textareas should be an exact match.
1160 EXPECT_EQ(shipping_profile.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS),
1161 form_structure()->field(2)->value);
1162 EXPECT_EQ(billing_profile.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS),
1163 form_structure()->field(3)->value);
1164
1165 EXPECT_NE(form_structure()->field(1)->value,
1166 form_structure()->field(0)->value);
1167 EXPECT_NE(form_structure()->field(3)->value,
1168 form_structure()->field(2)->value);
1169 }
1170
1171 // Test asking for different pieces of the name.
1172 TEST_F(AutofillDialogControllerTest, NamePieces) {
1173 const char* const attributes[] = {
1174 "shipping name",
1175 "billing name",
1176 "billing given-name",
1177 "billing family-name",
1178 "billing additional-name",
1179 "cc-csc"
1180 };
1181
1182 FormData form_data;
1183 for (size_t i = 0; i < arraysize(attributes); ++i) {
1184 FormFieldData field;
1185 field.autocomplete_attribute.assign(attributes[i]);
1186 form_data.fields.push_back(field);
1187 }
1188
1189 SetUpControllerWithFormData(form_data);
1190
1191 // Billing.
1192 AutofillProfile test_profile(test::GetVerifiedProfile());
1193 test_profile.SetInfo(AutofillType(NAME_FULL),
1194 ASCIIToUTF16("Fabian Jackson von Nacho"),
1195 "en-US");
1196 controller()->GetTestingManager()->AddTestingProfile(&test_profile);
1197
1198 // Credit card.
1199 CreditCard credit_card(test::GetVerifiedCreditCard());
1200 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
1201
1202 // Make shipping name different from billing.
1203 AutofillProfile test_profile2(test::GetVerifiedProfile2());
1204 test_profile2.SetInfo(AutofillType(NAME_FULL),
1205 ASCIIToUTF16("Don Ford"),
1206 "en-US");
1207 controller()->GetTestingManager()->AddTestingProfile(&test_profile2);
1208 ui::MenuModel* shipping_model =
1209 controller()->MenuModelForSection(SECTION_SHIPPING);
1210 shipping_model->ActivatedAt(2);
1211
1212 controller()->OnAccept();
1213
1214 EXPECT_EQ(NAME_FULL, form_structure()->field(0)->Type().GetStorableType());
1215 EXPECT_EQ(ASCIIToUTF16("Don Ford"),
1216 form_structure()->field(0)->value);
1217
1218 EXPECT_EQ(NAME_FULL, form_structure()->field(1)->Type().GetStorableType());
1219 EXPECT_EQ(ASCIIToUTF16("Fabian Jackson von Nacho"),
1220 form_structure()->field(1)->value);
1221
1222 EXPECT_EQ(NAME_FIRST, form_structure()->field(2)->Type().GetStorableType());
1223 EXPECT_EQ(ASCIIToUTF16("Fabian"),
1224 form_structure()->field(2)->value);
1225
1226 EXPECT_EQ(NAME_LAST, form_structure()->field(3)->Type().GetStorableType());
1227 EXPECT_EQ(ASCIIToUTF16("von Nacho"),
1228 form_structure()->field(3)->value);
1229
1230 EXPECT_EQ(NAME_MIDDLE, form_structure()->field(4)->Type().GetStorableType());
1231 EXPECT_EQ(ASCIIToUTF16("Jackson"),
1232 form_structure()->field(4)->value);
1233 }
1234
1235 // Tests that adding an autofill profile and then submitting works.
1236 TEST_F(AutofillDialogControllerTest, AddAutofillProfile) {
1237 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(2);
1238
1239 AutofillProfile full_profile(test::GetVerifiedProfile());
1240 CreditCard credit_card(test::GetVerifiedCreditCard());
1241 controller()->GetTestingManager()->AddTestingProfile(&full_profile);
1242 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
1243
1244 ui::MenuModel* model = controller()->MenuModelForSection(SECTION_BILLING);
1245 // Activate the "Add billing address" menu item.
1246 model->ActivatedAt(model->GetItemCount() - 2);
1247
1248 // Fill in the inputs from the profile.
1249 FieldValueMap outputs;
1250 const DetailInputs& inputs =
1251 controller()->RequestedFieldsForSection(SECTION_BILLING);
1252 AutofillProfile full_profile2(test::GetVerifiedProfile2());
1253 for (size_t i = 0; i < inputs.size(); ++i) {
1254 const ServerFieldType type = inputs[i].type;
1255 outputs[type] = full_profile2.GetInfo(AutofillType(type), "en-US");
1256 }
1257 controller()->GetView()->SetUserInput(SECTION_BILLING, outputs);
1258
1259 controller()->OnAccept();
1260 const AutofillProfile& added_profile =
1261 controller()->GetTestingManager()->imported_profile();
1262
1263 const DetailInputs& shipping_inputs =
1264 controller()->RequestedFieldsForSection(SECTION_SHIPPING);
1265 for (size_t i = 0; i < shipping_inputs.size(); ++i) {
1266 const ServerFieldType type = shipping_inputs[i].type;
1267 EXPECT_EQ(full_profile2.GetInfo(AutofillType(type), "en-US"),
1268 added_profile.GetInfo(AutofillType(type), "en-US"));
1269 }
1270 }
1271
1272 TEST_F(AutofillDialogControllerTest, SaveDetailsInChrome) {
1273 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(4);
1274
1275 AutofillProfile full_profile(test::GetVerifiedProfile());
1276 controller()->GetTestingManager()->AddTestingProfile(&full_profile);
1277
1278 CreditCard card(test::GetVerifiedCreditCard());
1279 controller()->GetTestingManager()->AddTestingCreditCard(&card);
1280 EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome());
1281
1282 controller()->MenuModelForSection(SECTION_BILLING)->ActivatedAt(0);
1283 EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome());
1284
1285 controller()->MenuModelForSection(SECTION_BILLING)->ActivatedAt(1);
1286 EXPECT_TRUE(controller()->ShouldOfferToSaveInChrome());
1287
1288 profile()->GetPrefs()->SetBoolean(prefs::kAutofillEnabled, false);
1289 EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome());
1290
1291 profile()->GetPrefs()->SetBoolean(prefs::kAutofillEnabled, true);
1292 controller()->MenuModelForSection(SECTION_BILLING)->ActivatedAt(1);
1293 EXPECT_TRUE(controller()->ShouldOfferToSaveInChrome());
1294
1295 profile()->ForceIncognito(true);
1296 EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome());
1297 }
1298
1299 TEST_F(AutofillDialogControllerTest, DisabledAutofill) {
1300 ASSERT_TRUE(profile()->GetPrefs()->GetBoolean(prefs::kAutofillEnabled));
1301
1302 AutofillProfile verified_profile(test::GetVerifiedProfile());
1303 controller()->GetTestingManager()->AddTestingProfile(&verified_profile);
1304
1305 CreditCard credit_card(test::GetVerifiedCreditCard());
1306 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
1307
1308 // Verify suggestions menus should be showing when Autofill is enabled.
1309 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC));
1310 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_BILLING));
1311 EXPECT_EQ(
1312 4, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
1313
1314 EXPECT_CALL(*controller()->GetView(), ModelChanged());
1315 profile()->GetPrefs()->SetBoolean(prefs::kAutofillEnabled, false);
1316
1317 // Verify billing and credit card suggestions menus are hidden when Autofill
1318 // is disabled.
1319 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC));
1320 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_BILLING));
1321 // And that the shipping suggestions menu has less selections.
1322 EXPECT_EQ(
1323 2, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
1324
1325 // Additionally, editing fields should not show Autofill popups.
1326 ASSERT_NO_FATAL_FAILURE(controller()->UserEditedOrActivatedInput(
1327 SECTION_BILLING, NAME_BILLING_FULL, gfx::NativeView(), gfx::Rect(),
1328 verified_profile.GetInfo(AutofillType(NAME_FULL), "en-US").substr(0, 1),
1329 true));
1330 EXPECT_EQ(UNKNOWN_TYPE, controller()->popup_input_type());
1331 }
1332
1333 TEST_F(AutofillDialogControllerTest, ShippingSectionCanBeHidden) {
1334 FormFieldData email_field;
1335 email_field.autocomplete_attribute = "email";
1336 FormFieldData cc_field;
1337 cc_field.autocomplete_attribute = "cc-number";
1338 FormFieldData billing_field;
1339 billing_field.autocomplete_attribute = "billing address-level1";
1340
1341 FormData form_data;
1342 form_data.fields.push_back(email_field);
1343 form_data.fields.push_back(cc_field);
1344 form_data.fields.push_back(billing_field);
1345
1346 AutofillProfile full_profile(test::GetVerifiedProfile());
1347 controller()->GetTestingManager()->AddTestingProfile(&full_profile);
1348 SetUpControllerWithFormData(form_data);
1349
1350 EXPECT_FALSE(controller()->SectionIsActive(SECTION_SHIPPING));
1351
1352 FillCreditCardInputs();
1353 controller()->OnAccept();
1354 EXPECT_TRUE(form_structure());
1355 }
1356
1357 TEST_F(AutofillDialogControllerTest, SaveInChromeByDefault) {
1358 EXPECT_TRUE(controller()->ShouldSaveInChrome());
1359 FillCreditCardInputs();
1360 controller()->OnAccept();
1361 EXPECT_TRUE(controller()->ShouldSaveInChrome());
1362 }
1363
1364 TEST_F(AutofillDialogControllerTest,
1365 SaveInChromePreferenceNotRememberedOnCancel) {
1366 EXPECT_TRUE(controller()->ShouldSaveInChrome());
1367 controller()->GetView()->CheckSaveDetailsLocallyCheckbox(false);
1368 controller()->OnCancel();
1369 EXPECT_TRUE(controller()->ShouldSaveInChrome());
1370 }
1371
1372 TEST_F(AutofillDialogControllerTest,
1373 SaveInChromePreferenceRememberedOnSuccess) {
1374 EXPECT_TRUE(controller()->ShouldSaveInChrome());
1375 FillCreditCardInputs();
1376 controller()->GetView()->CheckSaveDetailsLocallyCheckbox(false);
1377 controller()->OnAccept();
1378 EXPECT_FALSE(controller()->ShouldSaveInChrome());
1379 }
1380
1381 TEST_F(AutofillDialogControllerTest, SubmitButtonIsDisabled) {
1382 EXPECT_EQ(1, controller()->get_submit_button_delay_count());
1383
1384 // Begin the submit button delay.
1385 controller()->SimulateSubmitButtonDelayBegin();
1386 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
1387
1388 // End the submit button delay.
1389 controller()->SimulateSubmitButtonDelayEnd();
1390 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
1391 }
1392
1393 TEST_F(AutofillDialogControllerTest, IconsForFields_NoCreditCard) {
1394 FieldValueMap values;
1395 values[EMAIL_ADDRESS] = ASCIIToUTF16(kFakeEmail);
1396 FieldIconMap icons = controller()->IconsForFields(values);
1397 EXPECT_TRUE(icons.empty());
1398 }
1399
1400 TEST_F(AutofillDialogControllerTest, IconsForFields_CreditCardNumberOnly) {
1401 FieldValueMap values;
1402 values[EMAIL_ADDRESS] = ASCIIToUTF16(kFakeEmail);
1403 values[CREDIT_CARD_NUMBER] = ASCIIToUTF16(kTestCCNumberVisa);
1404 FieldIconMap icons = controller()->IconsForFields(values);
1405 EXPECT_EQ(1UL, icons.size());
1406 EXPECT_EQ(1UL, icons.count(CREDIT_CARD_NUMBER));
1407 }
1408
1409 TEST_F(AutofillDialogControllerTest, IconsForFields_CvcOnly) {
1410 FieldValueMap values;
1411 values[EMAIL_ADDRESS] = ASCIIToUTF16(kFakeEmail);
1412 values[CREDIT_CARD_VERIFICATION_CODE] = ASCIIToUTF16("123");
1413 FieldIconMap icons = controller()->IconsForFields(values);
1414 EXPECT_EQ(1UL, icons.size());
1415 EXPECT_EQ(1UL, icons.count(CREDIT_CARD_VERIFICATION_CODE));
1416 }
1417
1418 TEST_F(AutofillDialogControllerTest, IconsForFields_BothCreditCardAndCvc) {
1419 FieldValueMap values;
1420 values[EMAIL_ADDRESS] = ASCIIToUTF16(kFakeEmail);
1421 values[CREDIT_CARD_NUMBER] = ASCIIToUTF16(kTestCCNumberVisa);
1422 values[CREDIT_CARD_VERIFICATION_CODE] = ASCIIToUTF16("123");
1423 FieldIconMap icons = controller()->IconsForFields(values);
1424 EXPECT_EQ(2UL, icons.size());
1425 EXPECT_EQ(1UL, icons.count(CREDIT_CARD_VERIFICATION_CODE));
1426 EXPECT_EQ(1UL, icons.count(CREDIT_CARD_NUMBER));
1427 }
1428
1429 TEST_F(AutofillDialogControllerTest, FieldControlsIcons) {
1430 EXPECT_TRUE(controller()->FieldControlsIcons(CREDIT_CARD_NUMBER));
1431 EXPECT_FALSE(controller()->FieldControlsIcons(CREDIT_CARD_VERIFICATION_CODE));
1432 EXPECT_FALSE(controller()->FieldControlsIcons(EMAIL_ADDRESS));
1433 }
1434
1435 // Verifies that a call to the IconsForFields() method before the card type is
1436 // known returns a placeholder image that is at least as large as the icons for
1437 // all of the supported major credit card issuers.
1438 TEST_F(AutofillDialogControllerTest, IconReservedForCreditCardField) {
1439 FieldValueMap inputs;
1440 inputs[CREDIT_CARD_NUMBER] = base::string16();
1441
1442 FieldIconMap icons = controller()->IconsForFields(inputs);
1443 EXPECT_EQ(1U, icons.size());
1444
1445 ASSERT_EQ(1U, icons.count(CREDIT_CARD_NUMBER));
1446 gfx::Image placeholder_icon = icons[CREDIT_CARD_NUMBER];
1447
1448 // Verify that the placeholder icon is at least as large as the icons for the
1449 // supported credit card issuers.
1450 const int kSupportedCardIdrs[] = {
1451 IDR_AUTOFILL_CC_AMEX,
1452 IDR_AUTOFILL_CC_DISCOVER,
1453 IDR_AUTOFILL_CC_GENERIC,
1454 IDR_AUTOFILL_CC_MASTERCARD,
1455 IDR_AUTOFILL_CC_VISA,
1456 };
1457 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
1458 for (size_t i = 0; i < arraysize(kSupportedCardIdrs); ++i) {
1459 SCOPED_TRACE(base::SizeTToString(i));
1460 gfx::Image supported_card_icon = rb.GetImageNamed(kSupportedCardIdrs[i]);
1461 EXPECT_GE(placeholder_icon.Width(), supported_card_icon.Width());
1462 EXPECT_GE(placeholder_icon.Height(), supported_card_icon.Height());
1463 }
1464 }
1465
1466 TEST_F(AutofillDialogControllerTest, CountryChangeUpdatesSection) {
1467 TestAutofillDialogView* view = controller()->GetView();
1468 view->ClearSectionUpdates();
1469
1470 controller()->UserEditedOrActivatedInput(SECTION_SHIPPING,
1471 ADDRESS_HOME_COUNTRY,
1472 gfx::NativeView(),
1473 gfx::Rect(),
1474 ASCIIToUTF16("Belarus"),
1475 true);
1476 std::map<DialogSection, size_t> updates = view->section_updates();
1477 EXPECT_EQ(1U, updates[SECTION_SHIPPING]);
1478 EXPECT_EQ(1U, updates.size());
1479
1480 view->ClearSectionUpdates();
1481
1482 controller()->UserEditedOrActivatedInput(SECTION_BILLING,
1483 ADDRESS_BILLING_COUNTRY,
1484 gfx::NativeView(),
1485 gfx::Rect(),
1486 ASCIIToUTF16("Italy"),
1487 true);
1488 updates = view->section_updates();
1489 EXPECT_EQ(1U, updates[SECTION_BILLING]);
1490 EXPECT_EQ(1U, updates.size());
1491 }
1492
1493 TEST_F(AutofillDialogControllerTest, CorrectCountryFromInputs) {
1494 EXPECT_CALL(*controller()->GetMockValidator(),
1495 ValidateAddress(CountryCodeMatcher("DE"), _, _));
1496
1497 FieldValueMap billing_inputs;
1498 billing_inputs[ADDRESS_BILLING_COUNTRY] = ASCIIToUTF16("Germany");
1499 controller()->InputsAreValid(SECTION_BILLING, billing_inputs);
1500
1501 EXPECT_CALL(*controller()->GetMockValidator(),
1502 ValidateAddress(CountryCodeMatcher("FR"), _, _));
1503
1504 FieldValueMap shipping_inputs;
1505 shipping_inputs[ADDRESS_HOME_COUNTRY] = ASCIIToUTF16("France");
1506 controller()->InputsAreValid(SECTION_SHIPPING, shipping_inputs);
1507 }
1508
1509 TEST_F(AutofillDialogControllerTest, ValidationRulesLoadedOnCountryChange) {
1510 ResetControllerWithFormData(DefaultFormData());
1511 EXPECT_CALL(*controller()->GetMockValidator(),
1512 LoadRules("US")).Times(AtLeast(1));
1513 controller()->Show();
1514
1515 EXPECT_CALL(*controller()->GetMockValidator(), LoadRules("FR"));
1516 controller()->UserEditedOrActivatedInput(SECTION_BILLING,
1517 ADDRESS_BILLING_COUNTRY,
1518 gfx::NativeView(),
1519 gfx::Rect(),
1520 ASCIIToUTF16("France"),
1521 true);
1522 }
1523
1524 TEST_F(AutofillDialogControllerTest, UsValidationRulesLoadedForJpOnlyProfile) {
1525 ResetControllerWithFormData(DefaultFormData());
1526 AutofillProfile jp_profile(base::GenerateGUID(), kSettingsOrigin);
1527 jp_profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("JP"));
1528 controller()->GetTestingManager()->AddTestingProfile(&jp_profile);
1529 EXPECT_CALL(*controller()->GetMockValidator(), LoadRules("US")).Times(0);
1530 EXPECT_CALL(*controller()->GetMockValidator(),
1531 LoadRules("JP")).Times(AtLeast(1));
1532 controller()->Show();
1533 }
1534
1535 TEST_F(AutofillDialogControllerTest, InvalidWhenRulesNotReady) {
1536 // Select "Add new shipping address...".
1537 controller()->MenuModelForSection(SECTION_SHIPPING)->ActivatedAt(1);
1538
1539 // If the rules haven't loaded yet, validation errors should show on submit.
1540 EXPECT_CALL(*controller()->GetMockValidator(),
1541 ValidateAddress(CountryCodeMatcher("US"), _, _)).
1542 WillRepeatedly(Return(AddressValidator::RULES_NOT_READY));
1543
1544 FieldValueMap inputs;
1545 inputs[ADDRESS_HOME_ZIP] = ASCIIToUTF16("1234");
1546 inputs[ADDRESS_HOME_COUNTRY] = ASCIIToUTF16("United States");
1547
1548 ValidityMessages messages =
1549 controller()->InputsAreValid(SECTION_SHIPPING, inputs);
1550 EXPECT_FALSE(messages.GetMessageOrDefault(ADDRESS_HOME_ZIP).text.empty());
1551 EXPECT_FALSE(messages.HasSureError(ADDRESS_HOME_ZIP));
1552 // Country should never show an error message as it's always valid.
1553 EXPECT_TRUE(messages.GetMessageOrDefault(ADDRESS_HOME_COUNTRY).text.empty());
1554 }
1555
1556 TEST_F(AutofillDialogControllerTest, ValidButUnverifiedWhenRulesFail) {
1557 // Add suggestions so the credit card and billing sections aren't showing
1558 // their manual inputs (to isolate to just shipping).
1559 AutofillProfile verified_profile(test::GetVerifiedProfile());
1560 controller()->GetTestingManager()->AddTestingProfile(&verified_profile);
1561 CreditCard verified_card(test::GetVerifiedCreditCard());
1562 controller()->GetTestingManager()->AddTestingCreditCard(&verified_card);
1563
1564 // Select "Add new shipping address...".
1565 controller()->MenuModelForSection(SECTION_SHIPPING)->ActivatedAt(2);
1566
1567 // If the rules are unavailable, validation errors should not show.
1568 EXPECT_CALL(*controller()->GetMockValidator(),
1569 ValidateAddress(CountryCodeMatcher("US"), _, _)).
1570 WillRepeatedly(Return(AddressValidator::RULES_UNAVAILABLE));
1571
1572 FieldValueMap outputs;
1573 AutofillProfile full_profile(test::GetFullProfile());
1574 const DetailInputs& inputs =
1575 controller()->RequestedFieldsForSection(SECTION_SHIPPING);
1576 for (size_t i = 0; i < inputs.size(); ++i) {
1577 const ServerFieldType type = inputs[i].type;
1578 outputs[type] = full_profile.GetInfo(AutofillType(type), "en-US");
1579 }
1580 controller()->GetView()->SetUserInput(SECTION_SHIPPING, outputs);
1581 controller()->GetView()->CheckSaveDetailsLocallyCheckbox(true);
1582 controller()->OnAccept();
1583
1584 // Profiles saved while rules are unavailable shouldn't be verified.
1585 const AutofillProfile& imported_profile =
1586 controller()->GetTestingManager()->imported_profile();
1587 ASSERT_EQ(imported_profile.GetInfo(AutofillType(NAME_FULL), "en-US"),
1588 full_profile.GetInfo(AutofillType(NAME_FULL), "en-US"));
1589 EXPECT_EQ(imported_profile.origin(), GURL(kSourceUrl).GetOrigin().spec());
1590 EXPECT_FALSE(imported_profile.IsVerified());
1591 }
1592
1593 TEST_F(AutofillDialogControllerTest, LimitedCountryChoices) {
1594 ui::ComboboxModel* shipping_country_model =
1595 controller()->ComboboxModelForAutofillType(ADDRESS_HOME_COUNTRY);
1596 const int default_number_of_countries =
1597 shipping_country_model->GetItemCount();
1598 // We show a lot of countries by default, but the exact number doesn't matter.
1599 EXPECT_GT(default_number_of_countries, 50);
1600
1601 // Create a form data that simulates:
1602 // <select autocomplete="billing country">
1603 // <option value="AU">Down Under</option>
1604 // <option value="">fR</option> <!-- Case doesn't matter -->
1605 // <option value="GRMNY">Germany</option>
1606 // </select>
1607 // Only country codes are respected, whether they're in value or the option's
1608 // text content. Thus the first two options should be recognized.
1609 FormData form_data;
1610 FormFieldData field;
1611 field.autocomplete_attribute = "billing country";
1612 field.option_contents.push_back(ASCIIToUTF16("Down Under"));
1613 field.option_values.push_back(ASCIIToUTF16("AU"));
1614 field.option_contents.push_back(ASCIIToUTF16("Fr"));
1615 field.option_values.push_back(ASCIIToUTF16(""));
1616 field.option_contents.push_back(ASCIIToUTF16("Germany"));
1617 field.option_values.push_back(ASCIIToUTF16("GRMNY"));
1618
1619 FormFieldData cc_field;
1620 cc_field.autocomplete_attribute = "cc-csc";
1621
1622 form_data.fields.push_back(field);
1623 form_data.fields.push_back(cc_field);
1624 ResetControllerWithFormData(form_data);
1625 controller()->Show();
1626
1627 // Shipping model shouldn't have changed.
1628 shipping_country_model =
1629 controller()->ComboboxModelForAutofillType(ADDRESS_HOME_COUNTRY);
1630 EXPECT_EQ(default_number_of_countries,
1631 shipping_country_model->GetItemCount());
1632 // Billing model now only has two items.
1633 ui::ComboboxModel* billing_country_model =
1634 controller()->ComboboxModelForAutofillType(ADDRESS_BILLING_COUNTRY);
1635 ASSERT_EQ(2, billing_country_model->GetItemCount());
1636 EXPECT_EQ(billing_country_model->GetItemAt(0), ASCIIToUTF16("Australia"));
1637 EXPECT_EQ(billing_country_model->GetItemAt(1), ASCIIToUTF16("France"));
1638
1639 // Make sure it also applies to profile suggestions.
1640 AutofillProfile us_profile(test::GetVerifiedProfile());
1641 us_profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
1642 controller()->GetTestingManager()->AddTestingProfile(&us_profile);
1643 // Don't show a suggestion if the only one that exists is disabled.
1644 EXPECT_FALSE(
1645 controller()->SuggestionStateForSection(SECTION_BILLING).visible);
1646
1647 // Add a profile with an acceptable country; suggestion should be shown.
1648 ResetControllerWithFormData(form_data);
1649 controller()->Show();
1650 AutofillProfile au_profile(test::GetVerifiedProfile2());
1651 au_profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("AU"));
1652 controller()->GetTestingManager()->AddTestingProfile(&us_profile);
1653 controller()->GetTestingManager()->AddTestingProfile(&au_profile);
1654 ui::MenuModel* model = controller()->MenuModelForSection(SECTION_BILLING);
1655 ASSERT_TRUE(model);
1656 EXPECT_EQ(4, model->GetItemCount());
1657 EXPECT_FALSE(model->IsEnabledAt(0));
1658 EXPECT_TRUE(model->IsEnabledAt(1));
1659
1660 // Add <input type="text" autocomplete="billing country"></input>
1661 // This should open up selection of all countries again.
1662 FormFieldData field2;
1663 field2.autocomplete_attribute = "billing country";
1664 form_data.fields.push_back(field2);
1665 ResetControllerWithFormData(form_data);
1666 controller()->Show();
1667
1668 billing_country_model =
1669 controller()->ComboboxModelForAutofillType(ADDRESS_BILLING_COUNTRY);
1670 EXPECT_EQ(default_number_of_countries,
1671 billing_country_model->GetItemCount());
1672 }
1673
1674 // http://crbug.com/388018
1675 TEST_F(AutofillDialogControllerTest, NoCountryChoices) {
1676 // Create a form data that simulates:
1677 // <select autocomplete="billing country">
1678 // <option value="ATL">Atlantis</option>
1679 // <option value="ELD">Eldorado</option>
1680 // </select>
1681 // i.e. contains a list of no valid countries.
1682 FormData form_data;
1683 FormFieldData field;
1684 field.autocomplete_attribute = "billing country";
1685 field.option_contents.push_back(ASCIIToUTF16("Atlantis"));
1686 field.option_values.push_back(ASCIIToUTF16("ATL"));
1687 field.option_contents.push_back(ASCIIToUTF16("Eldorado"));
1688 field.option_values.push_back(ASCIIToUTF16("ELD"));
1689
1690 FormFieldData cc_field;
1691 cc_field.autocomplete_attribute = "cc-csc";
1692
1693 form_data.fields.push_back(field);
1694 form_data.fields.push_back(cc_field);
1695 ResetControllerWithFormData(form_data);
1696 controller()->Show();
1697
1698 // Controller aborts and self destructs.
1699 EXPECT_EQ(0, controller());
1700 }
1701
1702 TEST_F(AutofillDialogControllerTest, LimitedCcChoices) {
1703 // Typically, MC and Visa are both valid.
1704 ValidateCCNumber(SECTION_CC, kTestCCNumberMaster, true);
1705 ValidateCCNumber(SECTION_CC, kTestCCNumberVisa, true);
1706
1707 FormData form_data;
1708 FormFieldData field;
1709 field.autocomplete_attribute = "billing cc-type";
1710 field.option_contents.push_back(ASCIIToUTF16("Visa"));
1711 field.option_values.push_back(ASCIIToUTF16("V"));
1712 field.option_contents.push_back(ASCIIToUTF16("Amex"));
1713 field.option_values.push_back(ASCIIToUTF16("AX"));
1714 form_data.fields.push_back(field);
1715 ResetControllerWithFormData(form_data);
1716 controller()->Show();
1717
1718 // MC is not valid because it's missing from FormData.
1719 EXPECT_EQ(l10n_util::GetStringUTF16(
1720 IDS_AUTOFILL_DIALOG_VALIDATION_UNACCEPTED_MASTERCARD),
1721 ValidateCCNumber(SECTION_CC, kTestCCNumberMaster, false));
1722 ValidateCCNumber(SECTION_CC, kTestCCNumberVisa, true);
1723
1724 CreditCard visa_card(test::GetVerifiedCreditCard());
1725 CreditCard amex_card(test::GetVerifiedCreditCard2());
1726
1727 CreditCard master_card(base::GenerateGUID(), kSettingsOrigin);
1728 test::SetCreditCardInfo(
1729 &master_card, "Mr Foo", "5105105105105100", "07", "2099");
1730
1731 controller()->GetTestingManager()->AddTestingCreditCard(&visa_card);
1732 controller()->GetTestingManager()->AddTestingCreditCard(&amex_card);
1733 controller()->GetTestingManager()->AddTestingCreditCard(&master_card);
1734
1735 // The stored MC is disabled in the dropdown.
1736 ui::MenuModel* model = controller()->MenuModelForSection(SECTION_CC);
1737 ASSERT_TRUE(model);
1738 ASSERT_EQ(5, model->GetItemCount());
1739 EXPECT_TRUE(model->IsEnabledAt(0));
1740 EXPECT_TRUE(model->IsEnabledAt(1));
1741 EXPECT_FALSE(model->IsEnabledAt(2));
1742 EXPECT_TRUE(model->IsEnabledAt(3));
1743 EXPECT_TRUE(model->IsEnabledAt(4));
1744
1745 SetUpControllerWithFormData(form_data);
1746
1747 // Discover is disallowed because it's not in FormData.
1748 ValidateCCNumber(SECTION_CC, kTestCCNumberDiscover, false);
1749 }
1750
1751 TEST_F(AutofillDialogControllerTest, SuggestCountrylessProfiles) {
1752 FieldValueMap outputs;
1753 outputs[ADDRESS_HOME_COUNTRY] = ASCIIToUTF16("US");
1754 controller()->GetView()->SetUserInput(SECTION_SHIPPING, outputs);
1755
1756 AutofillProfile profile(test::GetVerifiedProfile());
1757 profile.SetRawInfo(NAME_FULL, ASCIIToUTF16("The Man Without a Country"));
1758 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, base::string16());
1759 controller()->GetTestingManager()->AddTestingProfile(&profile);
1760
1761 controller()->UserEditedOrActivatedInput(
1762 SECTION_SHIPPING,
1763 NAME_FULL,
1764 gfx::NativeView(),
1765 gfx::Rect(),
1766 profile.GetRawInfo(NAME_FULL).substr(0, 1),
1767 true);
1768 EXPECT_EQ(NAME_FULL, controller()->popup_input_type());
1769 }
1770
1771 } // namespace autofill
OLDNEW
« no previous file with comments | « chrome/browser/ui/autofill/autofill_dialog_controller_impl.cc ('k') | chrome/browser/ui/autofill/autofill_dialog_i18n_input.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698