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