Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / autofill / autofill_dialog_controller_unittest.cc
blob55b9a399726dbba0c3a8000c698857afe40f9ef8
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/ui/autofill/autofill_dialog_controller_impl.h"
22 #include "chrome/browser/ui/autofill/autofill_dialog_i18n_input.h"
23 #include "chrome/browser/ui/autofill/autofill_dialog_view.h"
24 #include "chrome/browser/ui/autofill/generated_credit_card_bubble_controller.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/ui/autofill/test_generated_credit_card_bubble_controller.h"
28 #include "chrome/browser/webdata/web_data_service_factory.h"
29 #include "chrome/common/chrome_switches.h"
30 #include "chrome/common/pref_names.h"
31 #include "chrome/common/render_messages.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/content/browser/wallet/full_wallet.h"
39 #include "components/autofill/content/browser/wallet/gaia_account.h"
40 #include "components/autofill/content/browser/wallet/instrument.h"
41 #include "components/autofill/content/browser/wallet/mock_wallet_client.h"
42 #include "components/autofill/content/browser/wallet/wallet_address.h"
43 #include "components/autofill/content/browser/wallet/wallet_service_url.h"
44 #include "components/autofill/content/browser/wallet/wallet_test_util.h"
45 #include "components/autofill/core/browser/autofill_metrics.h"
46 #include "components/autofill/core/browser/autofill_test_utils.h"
47 #include "components/autofill/core/browser/test_personal_data_manager.h"
48 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
49 #include "components/autofill/core/common/autofill_switches.h"
50 #include "components/autofill/core/common/form_data.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_field.h"
61 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_problem.h"
62 #include "ui/base/l10n/l10n_util.h"
63 #include "ui/base/resource/resource_bundle.h"
65 #if defined(OS_WIN)
66 #include "ui/base/win/scoped_ole_initializer.h"
67 #endif
69 using base::ASCIIToUTF16;
70 using base::UTF8ToUTF16;
72 namespace autofill {
74 namespace {
76 using ::i18n::addressinput::FieldProblemMap;
77 using testing::AtLeast;
78 using testing::DoAll;
79 using testing::Return;
80 using testing::SetArgPointee;
81 using testing::_;
83 const char kSourceUrl[] = "http://localbike.shop";
84 const char kFakeEmail[] = "user@chromium.org";
85 const char kEditedBillingAddress[] = "123 edited billing address";
86 const char* kFieldsFromPage[] =
87 { "email",
88 "cc-name",
89 "cc-number",
90 "cc-exp-month",
91 "cc-exp-year",
92 "cc-csc",
93 "billing name",
94 "billing address-line1",
95 "billing address-level2",
96 "billing address-level1",
97 "billing postal-code",
98 "billing country",
99 "billing tel",
100 "shipping name",
101 "shipping address-line1",
102 "shipping address-level2",
103 "shipping address-level1",
104 "shipping postal-code",
105 "shipping country",
106 "shipping tel",
108 const char kSettingsOrigin[] = "Chrome settings";
109 const char kTestCCNumberAmex[] = "376200000000002";
110 const char kTestCCNumberVisa[] = "4111111111111111";
111 const char kTestCCNumberMaster[] = "5555555555554444";
112 const char kTestCCNumberDiscover[] = "6011111111111117";
113 const char kTestCCNumberIncomplete[] = "4111111111";
114 // Credit card number fails Luhn check.
115 const char kTestCCNumberInvalid[] = "4111111111111112";
117 // Copies the initial values from |inputs| into |outputs|.
118 void CopyInitialValues(const DetailInputs& inputs, FieldValueMap* outputs) {
119 for (size_t i = 0; i < inputs.size(); ++i) {
120 const DetailInput& input = inputs[i];
121 (*outputs)[input.type] = input.initial_value;
125 scoped_ptr<wallet::WalletItems> CompleteAndValidWalletItems() {
126 scoped_ptr<wallet::WalletItems> items =
127 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
128 items->AddAccount(wallet::GetTestGaiaAccount());
129 items->AddInstrument(wallet::GetTestMaskedInstrument());
130 items->AddAddress(wallet::GetTestShippingAddress());
131 return items.Pass();
134 bool HasAnyError(const ValidityMessages& messages, ServerFieldType field) {
135 return !messages.GetMessageOrDefault(field).text.empty();
138 bool HasUnsureError(const ValidityMessages& messages, ServerFieldType field) {
139 const ValidityMessage& message = messages.GetMessageOrDefault(field);
140 return !message.text.empty() && !message.sure;
143 class TestAutofillDialogView : public AutofillDialogView {
144 public:
145 TestAutofillDialogView()
146 : updates_started_(0), save_details_locally_checked_(true) {}
147 virtual ~TestAutofillDialogView() {}
149 virtual void Show() override {}
150 virtual void Hide() override {}
152 virtual void UpdatesStarted() override {
153 updates_started_++;
156 virtual void UpdatesFinished() override {
157 updates_started_--;
158 EXPECT_GE(updates_started_, 0);
161 virtual void UpdateNotificationArea() override {
162 EXPECT_GE(updates_started_, 1);
165 virtual void UpdateAccountChooser() override {
166 EXPECT_GE(updates_started_, 1);
169 virtual void UpdateButtonStrip() override {
170 EXPECT_GE(updates_started_, 1);
173 virtual void UpdateOverlay() override {
174 EXPECT_GE(updates_started_, 1);
177 virtual void UpdateDetailArea() override {
178 EXPECT_GE(updates_started_, 1);
181 virtual void UpdateSection(DialogSection section) override {
182 section_updates_[section]++;
183 EXPECT_GE(updates_started_, 1);
186 virtual void UpdateErrorBubble() override {
187 EXPECT_GE(updates_started_, 1);
190 virtual void FillSection(DialogSection section,
191 ServerFieldType originating_type) override {}
192 virtual void GetUserInput(DialogSection section, FieldValueMap* output)
193 override {
194 *output = outputs_[section];
197 virtual base::string16 GetCvc() override { return base::string16(); }
199 virtual bool SaveDetailsLocally() override {
200 return save_details_locally_checked_;
203 virtual const content::NavigationController* ShowSignIn(const GURL& url)
204 override {
205 return NULL;
207 virtual void HideSignIn() override {}
209 MOCK_METHOD0(ModelChanged, void());
210 MOCK_METHOD0(UpdateForErrors, void());
212 virtual void OnSignInResize(const gfx::Size& pref_size) override {}
213 virtual void ValidateSection(DialogSection) override {}
215 void SetUserInput(DialogSection section, const FieldValueMap& map) {
216 outputs_[section] = map;
219 void CheckSaveDetailsLocallyCheckbox(bool checked) {
220 save_details_locally_checked_ = checked;
223 void ClearSectionUpdates() {
224 section_updates_.clear();
227 std::map<DialogSection, size_t> section_updates() const {
228 return section_updates_;
231 private:
232 std::map<DialogSection, FieldValueMap> outputs_;
233 std::map<DialogSection, size_t> section_updates_;
235 int updates_started_;
236 bool save_details_locally_checked_;
238 DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogView);
241 class TestAutofillDialogController
242 : public AutofillDialogControllerImpl,
243 public base::SupportsWeakPtr<TestAutofillDialogController> {
244 public:
245 TestAutofillDialogController(
246 content::WebContents* contents,
247 const FormData& form_structure,
248 const GURL& source_url,
249 const AutofillClient::ResultCallback& callback,
250 MockNewCreditCardBubbleController* mock_new_card_bubble_controller)
251 : AutofillDialogControllerImpl(contents,
252 form_structure,
253 source_url,
254 callback),
255 mock_wallet_client_(
256 Profile::FromBrowserContext(contents->GetBrowserContext())
257 ->GetRequestContext(),
258 this,
259 source_url),
260 mock_new_card_bubble_controller_(mock_new_card_bubble_controller),
261 submit_button_delay_count_(0) {}
263 virtual ~TestAutofillDialogController() {}
265 virtual AutofillDialogView* CreateView() override {
266 return new testing::NiceMock<TestAutofillDialogView>();
269 void Init(content::BrowserContext* browser_context) {
270 test_manager_.Init(WebDataServiceFactory::GetAutofillWebDataForProfile(
271 Profile::FromBrowserContext(browser_context),
272 ServiceAccessType::EXPLICIT_ACCESS),
273 user_prefs::UserPrefs::Get(browser_context),
274 browser_context->IsOffTheRecord());
277 TestAutofillDialogView* GetView() {
278 return static_cast<TestAutofillDialogView*>(view());
281 TestPersonalDataManager* GetTestingManager() {
282 return &test_manager_;
285 MockAddressValidator* GetMockValidator() {
286 return &mock_validator_;
289 wallet::MockWalletClient* GetTestingWalletClient() {
290 return &mock_wallet_client_;
293 const GURL& open_tab_url() { return open_tab_url_; }
295 void SimulateSigninError() {
296 OnWalletSigninError();
299 // Skips past the 2 second wait between FinishSubmit and DoFinishSubmit.
300 void ForceFinishSubmit() {
301 DoFinishSubmit();
304 void SimulateSubmitButtonDelayBegin() {
305 AutofillDialogControllerImpl::SubmitButtonDelayBegin();
308 void SimulateSubmitButtonDelayEnd() {
309 AutofillDialogControllerImpl::SubmitButtonDelayEndForTesting();
312 using AutofillDialogControllerImpl::
313 ClearLastWalletItemsFetchTimestampForTesting;
315 // Returns the number of times that the submit button was delayed.
316 int get_submit_button_delay_count() const {
317 return submit_button_delay_count_;
320 MOCK_METHOD0(LoadRiskFingerprintData, void());
321 using AutofillDialogControllerImpl::AccountChooserModelForTesting;
322 using AutofillDialogControllerImpl::OnDidLoadRiskFingerprintData;
323 using AutofillDialogControllerImpl::IsEditingExistingData;
324 using AutofillDialogControllerImpl::IsManuallyEditingSection;
325 using AutofillDialogControllerImpl::IsPayingWithWallet;
326 using AutofillDialogControllerImpl::IsSubmitPausedOn;
327 using AutofillDialogControllerImpl::NOT_CHECKED;
328 using AutofillDialogControllerImpl::popup_input_type;
329 using AutofillDialogControllerImpl::SignedInState;
331 protected:
332 virtual PersonalDataManager* GetManager() const override {
333 return const_cast<TestAutofillDialogController*>(this)->
334 GetTestingManager();
337 virtual AddressValidator* GetValidator() override {
338 return &mock_validator_;
341 virtual wallet::WalletClient* GetWalletClient() override {
342 return &mock_wallet_client_;
345 virtual void OpenTabWithUrl(const GURL& url) override {
346 open_tab_url_ = url;
349 virtual void ShowNewCreditCardBubble(
350 scoped_ptr<CreditCard> new_card,
351 scoped_ptr<AutofillProfile> billing_profile) override {
352 mock_new_card_bubble_controller_->Show(new_card.Pass(),
353 billing_profile.Pass());
356 // AutofillDialogControllerImpl calls this method before showing the dialog
357 // window.
358 virtual void SubmitButtonDelayBegin() override {
359 // Do not delay enabling the submit button in testing.
360 submit_button_delay_count_++;
363 private:
364 TestPersonalDataManager test_manager_;
365 testing::NiceMock<wallet::MockWalletClient> mock_wallet_client_;
367 // A mock validator object to prevent network requests and track when
368 // validation rules are loaded or validation attempts occur.
369 testing::NiceMock<MockAddressValidator> mock_validator_;
371 GURL open_tab_url_;
372 MockNewCreditCardBubbleController* mock_new_card_bubble_controller_;
374 // The number of times that the submit button was delayed.
375 int submit_button_delay_count_;
377 DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogController);
380 class AutofillDialogControllerTest : public ChromeRenderViewHostTestHarness {
381 protected:
382 AutofillDialogControllerTest(): form_structure_(NULL) {}
384 // testing::Test implementation:
385 void SetUp() override {
386 ChromeRenderViewHostTestHarness::SetUp();
387 Reset();
390 void TearDown() override {
391 if (controller_)
392 controller_->ViewClosed();
393 ChromeRenderViewHostTestHarness::TearDown();
396 void Reset() {
397 if (controller_)
398 controller_->ViewClosed();
400 test_generated_bubble_controller_ =
401 new testing::NiceMock<TestGeneratedCreditCardBubbleController>(
402 web_contents());
403 ASSERT_TRUE(test_generated_bubble_controller_->IsInstalled());
405 mock_new_card_bubble_controller_.reset(
406 new MockNewCreditCardBubbleController);
408 profile()->GetPrefs()->ClearPref(::prefs::kAutofillDialogSaveData);
410 // We have to clear the old local state before creating a new one.
411 scoped_local_state_.reset();
412 scoped_local_state_.reset(new ScopedTestingLocalState(
413 TestingBrowserProcess::GetGlobal()));
415 SetUpControllerWithFormData(DefaultFormData());
418 FormData DefaultFormData() {
419 FormData form_data;
420 for (size_t i = 0; i < arraysize(kFieldsFromPage); ++i) {
421 FormFieldData field;
422 field.autocomplete_attribute = kFieldsFromPage[i];
423 form_data.fields.push_back(field);
425 return form_data;
428 // Creates a new controller for |form_data|.
429 void ResetControllerWithFormData(const FormData& form_data) {
430 if (controller_)
431 controller_->ViewClosed();
433 AutofillClient::ResultCallback callback =
434 base::Bind(&AutofillDialogControllerTest::FinishedCallback,
435 base::Unretained(this));
436 controller_ = (new testing::NiceMock<TestAutofillDialogController>(
437 web_contents(),
438 form_data,
439 GURL(kSourceUrl),
440 callback,
441 mock_new_card_bubble_controller_.get()))->AsWeakPtr();
442 controller_->Init(profile());
445 // Creates a new controller for |form_data| and sets up some initial wallet
446 // data for it.
447 void SetUpControllerWithFormData(const FormData& form_data) {
448 ResetControllerWithFormData(form_data);
449 controller()->Show();
450 if (controller() &&
451 !profile()->GetPrefs()->GetBoolean(
452 ::prefs::kAutofillDialogPayWithoutWallet)) {
453 EXPECT_CALL(*controller()->GetTestingWalletClient(),
454 GetWalletItems(_, _));
455 controller()->OnDidFetchWalletCookieValue(std::string());
456 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
460 // Fills the inputs in SECTION_CC with data.
461 void FillCreditCardInputs() {
462 FieldValueMap cc_outputs;
463 const DetailInputs& cc_inputs =
464 controller()->RequestedFieldsForSection(SECTION_CC);
465 for (size_t i = 0; i < cc_inputs.size(); ++i) {
466 cc_outputs[cc_inputs[i].type] = cc_inputs[i].type == CREDIT_CARD_NUMBER ?
467 ASCIIToUTF16(kTestCCNumberVisa) : ASCIIToUTF16("11");
469 controller()->GetView()->SetUserInput(SECTION_CC, cc_outputs);
472 // Fills the inputs in SECTION_CC_BILLING with valid data.
473 void FillCCBillingInputs() {
474 FieldValueMap outputs;
475 const DetailInputs& inputs =
476 controller()->RequestedFieldsForSection(SECTION_CC_BILLING);
477 AutofillProfile full_profile(test::GetVerifiedProfile());
478 CreditCard full_card(test::GetCreditCard());
479 for (size_t i = 0; i < inputs.size(); ++i) {
480 const ServerFieldType type = inputs[i].type;
481 outputs[type] = full_profile.GetInfo(AutofillType(type), "en-US");
483 if (outputs[type].empty())
484 outputs[type] = full_card.GetInfo(AutofillType(type), "en-US");
486 controller()->GetView()->SetUserInput(SECTION_CC_BILLING, outputs);
489 // Activates the 'Add new foo' option from the |section|'s suggestions
490 // dropdown and fills the |section|'s inputs with the data from the
491 // |data_model|. If |section| is SECTION_CC, also fills in '123' for the CVC.
492 void FillInputs(DialogSection section, const AutofillDataModel& data_model) {
493 // Select the 'Add new foo' option.
494 ui::MenuModel* model = GetMenuModelForSection(section);
495 if (model)
496 model->ActivatedAt(model->GetItemCount() - 2);
498 // Fill the inputs.
499 FieldValueMap outputs;
500 const DetailInputs& inputs =
501 controller()->RequestedFieldsForSection(section);
502 for (size_t i = 0; i < inputs.size(); ++i) {
503 ServerFieldType type = inputs[i].type;
504 base::string16 output;
505 if (type == CREDIT_CARD_VERIFICATION_CODE)
506 output = ASCIIToUTF16("123");
507 else
508 output = data_model.GetInfo(AutofillType(type), "en-US");
509 outputs[inputs[i].type] = output;
511 controller()->GetView()->SetUserInput(section, outputs);
514 std::vector<DialogNotification> NotificationsOfType(
515 DialogNotification::Type type) {
516 std::vector<DialogNotification> right_type;
517 const std::vector<DialogNotification>& notifications =
518 controller()->CurrentNotifications();
519 for (size_t i = 0; i < notifications.size(); ++i) {
520 if (notifications[i].type() == type)
521 right_type.push_back(notifications[i]);
523 return right_type;
526 void SwitchToAutofill() {
527 ui::MenuModel* model = controller_->MenuModelForAccountChooser();
528 model->ActivatedAt(model->GetItemCount() - 1);
531 void SwitchToWallet() {
532 controller_->MenuModelForAccountChooser()->ActivatedAt(0);
535 void SimulateSigninError() {
536 controller_->SimulateSigninError();
539 void UseBillingForShipping() {
540 controller()->MenuModelForSection(SECTION_SHIPPING)->ActivatedAt(0);
543 base::string16 ValidateCCNumber(DialogSection section,
544 const std::string& cc_number,
545 bool should_pass) {
546 FieldValueMap outputs;
547 outputs[ADDRESS_BILLING_COUNTRY] = ASCIIToUTF16("United States");
548 outputs[CREDIT_CARD_NUMBER] = UTF8ToUTF16(cc_number);
549 ValidityMessages messages =
550 controller()->InputsAreValid(section, outputs);
551 EXPECT_EQ(should_pass, !messages.HasSureError(CREDIT_CARD_NUMBER));
552 return messages.GetMessageOrDefault(CREDIT_CARD_NUMBER).text;
555 void SubmitWithWalletItems(scoped_ptr<wallet::WalletItems> wallet_items) {
556 controller()->OnDidGetWalletItems(wallet_items.Pass());
557 AcceptAndLoadFakeFingerprint();
560 void AcceptAndLoadFakeFingerprint() {
561 controller()->OnAccept();
562 controller()->OnDidLoadRiskFingerprintData("a");
565 // Returns true if the given |section| contains a field of the given |type|.
566 bool SectionContainsField(DialogSection section, ServerFieldType type) {
567 const DetailInputs& inputs =
568 controller()->RequestedFieldsForSection(section);
569 for (DetailInputs::const_iterator it = inputs.begin(); it != inputs.end();
570 ++it) {
571 if (it->type == type)
572 return true;
574 return false;
577 SuggestionsMenuModel* GetMenuModelForSection(DialogSection section) {
578 ui::MenuModel* model = controller()->MenuModelForSection(section);
579 return static_cast<SuggestionsMenuModel*>(model);
582 void SubmitAndVerifyShippingAndBillingResults() {
583 // Test after setting use billing for shipping.
584 UseBillingForShipping();
586 controller()->OnAccept();
588 ASSERT_EQ(20U, form_structure()->field_count());
589 EXPECT_EQ(ADDRESS_HOME_COUNTRY,
590 form_structure()->field(11)->Type().GetStorableType());
591 EXPECT_EQ(ADDRESS_BILLING, form_structure()->field(11)->Type().group());
592 EXPECT_EQ(ADDRESS_HOME_COUNTRY,
593 form_structure()->field(18)->Type().GetStorableType());
594 EXPECT_EQ(ADDRESS_HOME, form_structure()->field(18)->Type().group());
595 base::string16 billing_country = form_structure()->field(11)->value;
596 EXPECT_EQ(2U, billing_country.size());
597 base::string16 shipping_country = form_structure()->field(18)->value;
598 EXPECT_EQ(2U, shipping_country.size());
599 EXPECT_FALSE(billing_country.empty());
600 EXPECT_FALSE(shipping_country.empty());
601 EXPECT_EQ(billing_country, shipping_country);
603 EXPECT_EQ(CREDIT_CARD_NAME,
604 form_structure()->field(1)->Type().GetStorableType());
605 base::string16 cc_name = form_structure()->field(1)->value;
606 EXPECT_EQ(NAME_FULL, form_structure()->field(6)->Type().GetStorableType());
607 EXPECT_EQ(NAME_BILLING, form_structure()->field(6)->Type().group());
608 base::string16 billing_name = form_structure()->field(6)->value;
609 EXPECT_EQ(NAME_FULL, form_structure()->field(13)->Type().GetStorableType());
610 EXPECT_EQ(NAME, form_structure()->field(13)->Type().group());
611 base::string16 shipping_name = form_structure()->field(13)->value;
613 EXPECT_FALSE(cc_name.empty());
614 EXPECT_FALSE(billing_name.empty());
615 EXPECT_FALSE(shipping_name.empty());
616 EXPECT_EQ(cc_name, billing_name);
617 EXPECT_EQ(cc_name, shipping_name);
620 TestAutofillDialogController* controller() { return controller_.get(); }
622 const FormStructure* form_structure() { return form_structure_; }
624 TestGeneratedCreditCardBubbleController* test_generated_bubble_controller() {
625 return test_generated_bubble_controller_;
628 const MockNewCreditCardBubbleController* mock_new_card_bubble_controller() {
629 return mock_new_card_bubble_controller_.get();
632 private:
633 void FinishedCallback(AutofillClient::RequestAutocompleteResult result,
634 const base::string16& debug_message,
635 const FormStructure* form_structure) {
636 form_structure_ = form_structure;
639 #if defined(OS_WIN)
640 // http://crbug.com/227221
641 ui::ScopedOleInitializer ole_initializer_;
642 #endif
644 // The controller owns itself.
645 base::WeakPtr<TestAutofillDialogController> controller_;
647 // Returned when the dialog closes successfully.
648 const FormStructure* form_structure_;
650 // Used to monitor if the Autofill credit card bubble is shown. Owned by
651 // |web_contents()|.
652 TestGeneratedCreditCardBubbleController* test_generated_bubble_controller_;
654 // Used to record when new card bubbles would show. Created in |Reset()|.
655 scoped_ptr<MockNewCreditCardBubbleController>
656 mock_new_card_bubble_controller_;
658 scoped_ptr<ScopedTestingLocalState> scoped_local_state_;
661 } // namespace
663 TEST_F(AutofillDialogControllerTest, RefuseToShowWithNoAutocompleteAttributes) {
664 FormFieldData email_field;
665 email_field.name = ASCIIToUTF16("email");
666 FormFieldData cc_field;
667 cc_field.name = ASCIIToUTF16("cc");
668 FormFieldData billing_field;
669 billing_field.name = ASCIIToUTF16("billing name");
671 FormData form_data;
672 form_data.fields.push_back(email_field);
673 form_data.fields.push_back(cc_field);
674 form_data.fields.push_back(billing_field);
676 SetUpControllerWithFormData(form_data);
677 EXPECT_FALSE(controller());
680 TEST_F(AutofillDialogControllerTest, RefuseToShowWithNoCcField) {
681 FormFieldData shipping_tel;
682 shipping_tel.autocomplete_attribute = "shipping tel";
684 FormData form_data;
685 form_data.fields.push_back(shipping_tel);
687 SetUpControllerWithFormData(form_data);
688 EXPECT_FALSE(controller());
690 // Any cc- field will do.
691 FormFieldData cc_field;
692 cc_field.autocomplete_attribute = "cc-csc";
693 form_data.fields.push_back(cc_field);
695 SetUpControllerWithFormData(form_data);
696 EXPECT_TRUE(controller());
699 // Ensure the default ValidityMessage has the expected values.
700 TEST_F(AutofillDialogControllerTest, DefaultValidityMessage) {
701 ValidityMessages messages;
702 ValidityMessage message = messages.GetMessageOrDefault(UNKNOWN_TYPE);
703 EXPECT_FALSE(message.sure);
704 EXPECT_TRUE(message.text.empty());
707 // This test makes sure nothing falls over when fields are being validity-
708 // checked.
709 TEST_F(AutofillDialogControllerTest, ValidityCheck) {
710 for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
711 DialogSection section = static_cast<DialogSection>(i);
712 const DetailInputs& shipping_inputs =
713 controller()->RequestedFieldsForSection(section);
714 for (DetailInputs::const_iterator iter = shipping_inputs.begin();
715 iter != shipping_inputs.end(); ++iter) {
716 controller()->InputValidityMessage(section, iter->type, base::string16());
721 // Test for phone number validation.
722 TEST_F(AutofillDialogControllerTest, PhoneNumberValidation) {
723 // Construct FieldValueMap from existing data.
724 SwitchToAutofill();
726 for (size_t i = 0; i < 2; ++i) {
727 ServerFieldType phone = i == 0 ? PHONE_HOME_WHOLE_NUMBER :
728 PHONE_BILLING_WHOLE_NUMBER;
729 ServerFieldType address = i == 0 ? ADDRESS_HOME_COUNTRY :
730 ADDRESS_BILLING_COUNTRY;
731 DialogSection section = i == 0 ? SECTION_SHIPPING : SECTION_BILLING;
733 FieldValueMap outputs;
734 const DetailInputs& inputs =
735 controller()->RequestedFieldsForSection(section);
736 AutofillProfile full_profile(test::GetVerifiedProfile());
737 for (size_t j = 0; j < inputs.size(); ++j) {
738 const ServerFieldType type = inputs[j].type;
739 outputs[type] = full_profile.GetInfo(AutofillType(type), "en-US");
742 // Make sure country is United States.
743 outputs[address] = ASCIIToUTF16("United States");
745 // Existing data should have no errors.
746 ValidityMessages messages = controller()->InputsAreValid(section, outputs);
747 EXPECT_FALSE(HasAnyError(messages, phone));
749 // Input an empty phone number.
750 outputs[phone] = base::string16();
751 messages = controller()->InputsAreValid(section, outputs);
752 EXPECT_TRUE(HasUnsureError(messages, phone));
754 // Input an invalid phone number.
755 outputs[phone] = ASCIIToUTF16("ABC");
756 messages = controller()->InputsAreValid(section, outputs);
757 EXPECT_TRUE(messages.HasSureError(phone));
759 // Input a local phone number.
760 outputs[phone] = ASCIIToUTF16("2155546699");
761 messages = controller()->InputsAreValid(section, outputs);
762 EXPECT_FALSE(HasAnyError(messages, phone));
764 // Input an invalid local phone number.
765 outputs[phone] = ASCIIToUTF16("215554669");
766 messages = controller()->InputsAreValid(section, outputs);
767 EXPECT_TRUE(messages.HasSureError(phone));
769 // Input an international phone number.
770 outputs[phone] = ASCIIToUTF16("+33 892 70 12 39");
771 messages = controller()->InputsAreValid(section, outputs);
772 EXPECT_FALSE(HasAnyError(messages, phone));
774 // Input an invalid international phone number.
775 outputs[phone] = ASCIIToUTF16("+112333 892 70 12 39");
776 messages = controller()->InputsAreValid(section, outputs);
777 EXPECT_TRUE(messages.HasSureError(phone));
779 // Input a valid Canadian number.
780 outputs[phone] = ASCIIToUTF16("+1 506 887 1234");
781 messages = controller()->InputsAreValid(section, outputs);
782 EXPECT_FALSE(HasAnyError(messages, phone));
784 // Input a valid Canadian number without the country code.
785 outputs[phone] = ASCIIToUTF16("506 887 1234");
786 messages = controller()->InputsAreValid(section, outputs);
787 EXPECT_TRUE(HasAnyError(messages, phone));
789 // Input a valid Canadian toll-free number.
790 outputs[phone] = ASCIIToUTF16("310 1234");
791 messages = controller()->InputsAreValid(section, outputs);
792 EXPECT_TRUE(HasAnyError(messages, phone));
796 TEST_F(AutofillDialogControllerTest, ExpirationDateValidity) {
797 ui::ComboboxModel* exp_year_model =
798 controller()->ComboboxModelForAutofillType(CREDIT_CARD_EXP_4_DIGIT_YEAR);
799 ui::ComboboxModel* exp_month_model =
800 controller()->ComboboxModelForAutofillType(CREDIT_CARD_EXP_MONTH);
802 base::string16 default_year_value =
803 exp_year_model->GetItemAt(exp_year_model->GetDefaultIndex());
804 base::string16 default_month_value =
805 exp_month_model->GetItemAt(exp_month_model->GetDefaultIndex());
807 base::string16 other_year_value =
808 exp_year_model->GetItemAt(exp_year_model->GetItemCount() - 1);
809 base::string16 other_month_value =
810 exp_month_model->GetItemAt(exp_month_model->GetItemCount() - 1);
812 FieldValueMap outputs;
813 outputs[ADDRESS_BILLING_COUNTRY] = ASCIIToUTF16("United States");
814 outputs[CREDIT_CARD_EXP_MONTH] = default_month_value;
815 outputs[CREDIT_CARD_EXP_4_DIGIT_YEAR] = default_year_value;
817 // Expiration default values generate unsure validation errors (but not sure).
818 ValidityMessages messages = controller()->InputsAreValid(SECTION_CC_BILLING,
819 outputs);
820 EXPECT_TRUE(HasUnsureError(messages, CREDIT_CARD_EXP_4_DIGIT_YEAR));
821 EXPECT_TRUE(HasUnsureError(messages, CREDIT_CARD_EXP_MONTH));
823 // Expiration date with default month fails.
824 outputs[CREDIT_CARD_EXP_4_DIGIT_YEAR] = other_year_value;
825 messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
826 EXPECT_FALSE(HasUnsureError(messages, CREDIT_CARD_EXP_4_DIGIT_YEAR));
827 EXPECT_TRUE(HasUnsureError(messages, CREDIT_CARD_EXP_MONTH));
829 // Expiration date with default year fails.
830 outputs[CREDIT_CARD_EXP_MONTH] = other_month_value;
831 outputs[CREDIT_CARD_EXP_4_DIGIT_YEAR] = default_year_value;
832 messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
833 EXPECT_TRUE(HasUnsureError(messages, CREDIT_CARD_EXP_4_DIGIT_YEAR));
834 EXPECT_FALSE(HasUnsureError(messages, CREDIT_CARD_EXP_MONTH));
837 TEST_F(AutofillDialogControllerTest, BillingNameValidation) {
838 // Construct FieldValueMap from AutofillProfile data.
839 SwitchToAutofill();
841 FieldValueMap outputs;
842 outputs[ADDRESS_BILLING_COUNTRY] = ASCIIToUTF16("United States");
844 // Input an empty billing name.
845 outputs[NAME_BILLING_FULL] = base::string16();
846 ValidityMessages messages = controller()->InputsAreValid(SECTION_BILLING,
847 outputs);
848 EXPECT_TRUE(HasUnsureError(messages, NAME_BILLING_FULL));
850 // Input a non-empty billing name.
851 outputs[NAME_BILLING_FULL] = ASCIIToUTF16("Bob");
852 messages = controller()->InputsAreValid(SECTION_BILLING, outputs);
853 EXPECT_FALSE(HasAnyError(messages, NAME_BILLING_FULL));
855 // Switch to Wallet which only considers names with with at least two names to
856 // be valid.
857 SwitchToWallet();
859 // Setup some wallet state.
860 scoped_ptr<wallet::WalletItems> wallet_items =
861 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
862 controller()->OnDidGetWalletItems(wallet_items.Pass());
864 // Input an empty billing name. Data source should not change this behavior.
865 outputs[NAME_BILLING_FULL] = base::string16();
866 messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
867 EXPECT_TRUE(HasUnsureError(messages, NAME_BILLING_FULL));
869 // Input a one name billing name. Wallet does not currently support this.
870 outputs[NAME_BILLING_FULL] = ASCIIToUTF16("Bob");
871 messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
872 EXPECT_TRUE(messages.HasSureError(NAME_BILLING_FULL));
874 // Input a two name billing name.
875 outputs[NAME_BILLING_FULL] = ASCIIToUTF16("Bob Barker");
876 messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
877 EXPECT_FALSE(HasAnyError(messages, NAME_BILLING_FULL));
879 // Input a more than two name billing name.
880 outputs[NAME_BILLING_FULL] = ASCIIToUTF16("John Jacob Jingleheimer Schmidt"),
881 messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
882 EXPECT_FALSE(HasAnyError(messages, NAME_BILLING_FULL));
884 // Input a billing name with lots of crazy whitespace.
885 outputs[NAME_BILLING_FULL] =
886 ASCIIToUTF16(" \\n\\r John \\n Jacob Jingleheimer \\t Schmidt "),
887 messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
888 EXPECT_FALSE(HasAnyError(messages, NAME_BILLING_FULL));
891 TEST_F(AutofillDialogControllerTest, CreditCardNumberValidation) {
892 // Construct FieldValueMap from AutofillProfile data.
893 SwitchToAutofill();
895 // Should accept AMEX, Visa, Master and Discover.
896 ValidateCCNumber(SECTION_CC, kTestCCNumberVisa, true);
897 ValidateCCNumber(SECTION_CC, kTestCCNumberMaster, true);
898 ValidateCCNumber(SECTION_CC, kTestCCNumberDiscover, true);
899 ValidateCCNumber(SECTION_CC, kTestCCNumberAmex, true);
900 ValidateCCNumber(SECTION_CC, kTestCCNumberIncomplete, false);
901 ValidateCCNumber(SECTION_CC, kTestCCNumberInvalid, false);
903 // Switch to Wallet which will not accept AMEX.
904 SwitchToWallet();
906 // Setup some wallet state on a merchant for which Wallet doesn't
907 // support AMEX.
908 controller()->OnDidGetWalletItems(
909 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED));
911 // Should accept Visa, Master and Discover, but not AMEX.
912 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberVisa, true);
913 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberMaster, true);
914 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberDiscover, true);
915 EXPECT_EQ(l10n_util::GetStringUTF16(
916 IDS_AUTOFILL_CREDIT_CARD_NOT_SUPPORTED_BY_WALLET_FOR_MERCHANT),
917 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberAmex, false));
918 EXPECT_EQ(
919 l10n_util::GetStringUTF16(
920 IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_NUMBER),
921 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberIncomplete, false));
922 EXPECT_EQ(
923 l10n_util::GetStringUTF16(
924 IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_NUMBER),
925 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberInvalid, false));
927 // Setup some wallet state on a merchant for which Wallet supports AMEX.
928 controller()->OnDidGetWalletItems(
929 wallet::GetTestWalletItems(wallet::AMEX_ALLOWED));
931 // Should accept Visa, Master, Discover, and AMEX.
932 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberVisa, true);
933 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberMaster, true);
934 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberDiscover, true);
935 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberAmex, true);
936 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberIncomplete, false);
937 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberInvalid, false);
940 TEST_F(AutofillDialogControllerTest, AutofillProfiles) {
941 SwitchToAutofill();
942 ui::MenuModel* shipping_model =
943 controller()->MenuModelForSection(SECTION_SHIPPING);
944 // Since the PersonalDataManager is empty, this should only have the
945 // "use billing", "add new" and "manage" menu items.
946 ASSERT_TRUE(shipping_model);
947 EXPECT_EQ(3, shipping_model->GetItemCount());
948 // On the other hand, the other models should be NULL when there's no
949 // suggestion.
950 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC));
951 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_BILLING));
953 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(3);
955 // Empty profiles are ignored.
956 AutofillProfile empty_profile(base::GenerateGUID(), kSettingsOrigin);
957 empty_profile.SetRawInfo(NAME_FULL, ASCIIToUTF16("John Doe"));
958 controller()->GetTestingManager()->AddTestingProfile(&empty_profile);
959 shipping_model = controller()->MenuModelForSection(SECTION_SHIPPING);
960 ASSERT_TRUE(shipping_model);
961 EXPECT_EQ(3, shipping_model->GetItemCount());
963 // An otherwise full but unverified profile should be ignored.
964 AutofillProfile full_profile(test::GetFullProfile());
965 full_profile.set_origin("https://www.example.com");
966 full_profile.SetRawInfo(ADDRESS_HOME_LINE2, base::string16());
967 controller()->GetTestingManager()->AddTestingProfile(&full_profile);
968 shipping_model = controller()->MenuModelForSection(SECTION_SHIPPING);
969 ASSERT_TRUE(shipping_model);
970 EXPECT_EQ(3, shipping_model->GetItemCount());
972 // A full, verified profile should be picked up.
973 AutofillProfile verified_profile(test::GetVerifiedProfile());
974 verified_profile.SetRawInfo(ADDRESS_HOME_LINE2, base::string16());
975 controller()->GetTestingManager()->AddTestingProfile(&verified_profile);
976 shipping_model = controller()->MenuModelForSection(SECTION_SHIPPING);
977 ASSERT_TRUE(shipping_model);
978 EXPECT_EQ(4, shipping_model->GetItemCount());
981 // Checks that a valid profile is selected by default, but if invalid is
982 // popped into edit mode.
983 TEST_F(AutofillDialogControllerTest, AutofillProfilesPopInvalidIntoEdit) {
984 SwitchToAutofill();
985 SuggestionsMenuModel* shipping_model =
986 GetMenuModelForSection(SECTION_SHIPPING);
987 EXPECT_EQ(3, shipping_model->GetItemCount());
988 // "Same as billing" is selected.
989 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_SHIPPING));
990 EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_BILLING));
992 AutofillProfile verified_profile(test::GetVerifiedProfile());
993 controller()->GetTestingManager()->AddTestingProfile(&verified_profile);
994 EXPECT_EQ(4, shipping_model->GetItemCount());
995 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_SHIPPING));
996 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_BILLING));
998 // Now make up a problem and make sure the profile isn't in the list.
999 Reset();
1000 SwitchToAutofill();
1001 FieldProblemMap problems;
1002 problems.insert(std::make_pair(::i18n::addressinput::POSTAL_CODE,
1003 ::i18n::addressinput::MISMATCHING_VALUE));
1004 EXPECT_CALL(*controller()->GetMockValidator(),
1005 ValidateAddress(CountryCodeMatcher("US"), _, _)).
1006 WillRepeatedly(DoAll(SetArgPointee<2>(problems),
1007 Return(AddressValidator::SUCCESS)));
1009 controller()->GetTestingManager()->AddTestingProfile(&verified_profile);
1010 shipping_model = GetMenuModelForSection(SECTION_SHIPPING);
1011 EXPECT_EQ(4, shipping_model->GetItemCount());
1012 EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_SHIPPING));
1013 EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_BILLING));
1016 // Makes sure suggestion profiles are re-validated when validation rules load.
1017 TEST_F(AutofillDialogControllerTest, AutofillProfilesRevalidateAfterRulesLoad) {
1018 SwitchToAutofill();
1019 SuggestionsMenuModel* shipping_model =
1020 GetMenuModelForSection(SECTION_SHIPPING);
1021 EXPECT_EQ(3, shipping_model->GetItemCount());
1022 // "Same as billing" is selected.
1023 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_SHIPPING));
1024 EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_BILLING));
1025 AutofillProfile verified_profile(test::GetVerifiedProfile());
1026 controller()->GetTestingManager()->AddTestingProfile(&verified_profile);
1027 EXPECT_EQ(4, shipping_model->GetItemCount());
1028 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_SHIPPING));
1029 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_BILLING));
1031 FieldProblemMap problems;
1032 problems.insert(std::make_pair(::i18n::addressinput::POSTAL_CODE,
1033 ::i18n::addressinput::MISMATCHING_VALUE));
1034 EXPECT_CALL(*controller()->GetMockValidator(),
1035 ValidateAddress(CountryCodeMatcher("US"), _, _)).
1036 WillRepeatedly(DoAll(SetArgPointee<2>(problems),
1037 Return(AddressValidator::SUCCESS)));
1039 controller()->OnAddressValidationRulesLoaded("US", true);
1040 EXPECT_EQ(4, shipping_model->GetItemCount());
1041 EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_SHIPPING));
1042 EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_BILLING));
1045 // Makes sure that the choice of which Autofill profile to use for each section
1046 // is sticky.
1047 TEST_F(AutofillDialogControllerTest, AutofillProfileDefaults) {
1048 SwitchToAutofill();
1049 AutofillProfile profile(test::GetVerifiedProfile());
1050 AutofillProfile profile2(test::GetVerifiedProfile2());
1051 controller()->GetTestingManager()->AddTestingProfile(&profile);
1052 controller()->GetTestingManager()->AddTestingProfile(&profile2);
1054 // Until a selection has been made, the default shipping suggestion is the
1055 // first one (after "use billing").
1056 SuggestionsMenuModel* shipping_model =
1057 GetMenuModelForSection(SECTION_SHIPPING);
1058 EXPECT_EQ(1, shipping_model->checked_item());
1060 for (int i = 2; i >= 0; --i) {
1061 shipping_model = GetMenuModelForSection(SECTION_SHIPPING);
1062 shipping_model->ExecuteCommand(i, 0);
1063 FillCreditCardInputs();
1064 controller()->OnAccept();
1066 Reset();
1067 controller()->GetTestingManager()->AddTestingProfile(&profile);
1068 controller()->GetTestingManager()->AddTestingProfile(&profile2);
1069 shipping_model = GetMenuModelForSection(SECTION_SHIPPING);
1070 EXPECT_EQ(i, shipping_model->checked_item());
1073 // Try again, but don't add the default profile to the PDM. The dialog
1074 // should fall back to the first profile.
1075 shipping_model->ExecuteCommand(2, 0);
1076 FillCreditCardInputs();
1077 controller()->OnAccept();
1078 Reset();
1079 controller()->GetTestingManager()->AddTestingProfile(&profile);
1080 shipping_model = GetMenuModelForSection(SECTION_SHIPPING);
1081 EXPECT_EQ(1, shipping_model->checked_item());
1084 // Makes sure that a newly added Autofill profile becomes set as the default
1085 // choice for the next run.
1086 TEST_F(AutofillDialogControllerTest, NewAutofillProfileIsDefault) {
1087 SwitchToAutofill();
1089 AutofillProfile profile(test::GetVerifiedProfile());
1090 CreditCard credit_card(test::GetVerifiedCreditCard());
1091 controller()->GetTestingManager()->AddTestingProfile(&profile);
1092 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
1094 // Until a selection has been made, the default suggestion is the first one.
1095 // For the shipping section, this follows the "use billing" suggestion.
1096 EXPECT_EQ(0, GetMenuModelForSection(SECTION_CC)->checked_item());
1097 EXPECT_EQ(1, GetMenuModelForSection(SECTION_SHIPPING)->checked_item());
1099 // Fill in the shipping and credit card sections with new data.
1100 AutofillProfile new_profile(test::GetVerifiedProfile2());
1101 CreditCard new_credit_card(test::GetVerifiedCreditCard2());
1102 FillInputs(SECTION_SHIPPING, new_profile);
1103 FillInputs(SECTION_CC, new_credit_card);
1104 controller()->GetView()->CheckSaveDetailsLocallyCheckbox(true);
1105 controller()->OnAccept();
1107 // Update the |new_profile| and |new_credit_card|'s guids to the saved ones.
1108 new_profile.set_guid(
1109 controller()->GetTestingManager()->imported_profile().guid());
1110 new_credit_card.set_guid(
1111 controller()->GetTestingManager()->imported_credit_card().guid());
1113 // Reload the dialog. The newly added address and credit card should now be
1114 // set as the defaults.
1115 Reset();
1116 controller()->GetTestingManager()->AddTestingProfile(&profile);
1117 controller()->GetTestingManager()->AddTestingProfile(&new_profile);
1118 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
1119 controller()->GetTestingManager()->AddTestingCreditCard(&new_credit_card);
1121 // Until a selection has been made, the default suggestion is the first one.
1122 // For the shipping section, this follows the "use billing" suggestion.
1123 EXPECT_EQ(1, GetMenuModelForSection(SECTION_CC)->checked_item());
1124 EXPECT_EQ(2, GetMenuModelForSection(SECTION_SHIPPING)->checked_item());
1127 TEST_F(AutofillDialogControllerTest, AutofillProfileVariants) {
1128 SwitchToAutofill();
1129 EXPECT_CALL(*controller()->GetView(), ModelChanged());
1130 ui::MenuModel* shipping_model =
1131 controller()->MenuModelForSection(SECTION_SHIPPING);
1132 ASSERT_TRUE(!!shipping_model);
1133 EXPECT_EQ(3, shipping_model->GetItemCount());
1135 // Set up some variant data.
1136 AutofillProfile full_profile(test::GetVerifiedProfile());
1137 std::vector<base::string16> names;
1138 names.push_back(ASCIIToUTF16("John Doe"));
1139 names.push_back(ASCIIToUTF16("Jane Doe"));
1140 full_profile.SetRawMultiInfo(NAME_FULL, names);
1141 std::vector<base::string16> emails;
1142 emails.push_back(ASCIIToUTF16(kFakeEmail));
1143 emails.push_back(ASCIIToUTF16("admin@example.com"));
1144 full_profile.SetRawMultiInfo(EMAIL_ADDRESS, emails);
1146 // Non-default variants are ignored by the dialog.
1147 controller()->GetTestingManager()->AddTestingProfile(&full_profile);
1148 EXPECT_EQ(4, shipping_model->GetItemCount());
1151 TEST_F(AutofillDialogControllerTest, SuggestValidEmail) {
1152 SwitchToAutofill();
1153 AutofillProfile profile(test::GetVerifiedProfile());
1154 const base::string16 kValidEmail = ASCIIToUTF16(kFakeEmail);
1155 profile.SetRawInfo(EMAIL_ADDRESS, kValidEmail);
1156 controller()->GetTestingManager()->AddTestingProfile(&profile);
1158 // "add", "manage", and 1 suggestion.
1159 EXPECT_EQ(
1160 3, controller()->MenuModelForSection(SECTION_BILLING)->GetItemCount());
1161 // "add", "manage", 1 suggestion, and "same as billing".
1162 EXPECT_EQ(
1163 4, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
1166 TEST_F(AutofillDialogControllerTest, DoNotSuggestInvalidEmail) {
1167 SwitchToAutofill();
1168 AutofillProfile profile(test::GetVerifiedProfile());
1169 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16(".!#$%&'*+/=?^_`-@-.."));
1170 controller()->GetTestingManager()->AddTestingProfile(&profile);
1172 EXPECT_FALSE(!!controller()->MenuModelForSection(SECTION_BILLING));
1173 // "add", "manage", 1 suggestion, and "same as billing".
1174 EXPECT_EQ(
1175 4, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
1178 TEST_F(AutofillDialogControllerTest, SuggestValidAddress) {
1179 SwitchToAutofill();
1180 AutofillProfile full_profile(test::GetVerifiedProfile());
1181 full_profile.set_origin(kSettingsOrigin);
1182 controller()->GetTestingManager()->AddTestingProfile(&full_profile);
1183 // "add", "manage", and 1 suggestion.
1184 EXPECT_EQ(
1185 3, controller()->MenuModelForSection(SECTION_BILLING)->GetItemCount());
1188 TEST_F(AutofillDialogControllerTest, DoNotSuggestInvalidAddress) {
1189 SwitchToAutofill();
1190 AutofillProfile full_profile(test::GetVerifiedProfile());
1191 full_profile.set_origin(kSettingsOrigin);
1192 full_profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("C"));
1193 controller()->GetTestingManager()->AddTestingProfile(&full_profile);
1196 TEST_F(AutofillDialogControllerTest, DoNotSuggestIncompleteAddress) {
1197 SwitchToAutofill();
1198 AutofillProfile profile(test::GetVerifiedProfile());
1199 profile.SetRawInfo(ADDRESS_HOME_STATE, base::string16());
1200 controller()->GetTestingManager()->AddTestingProfile(&profile);
1202 // Same as shipping, manage, add new.
1203 EXPECT_EQ(3,
1204 controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
1205 EXPECT_FALSE(!!controller()->MenuModelForSection(SECTION_BILLING));
1208 TEST_F(AutofillDialogControllerTest, DoSuggestShippingAddressWithoutEmail) {
1209 SwitchToAutofill();
1210 AutofillProfile profile(test::GetVerifiedProfile());
1211 profile.SetRawInfo(EMAIL_ADDRESS, base::string16());
1212 controller()->GetTestingManager()->AddTestingProfile(&profile);
1214 // Same as shipping, manage, add new, profile with missing email.
1215 EXPECT_EQ(4,
1216 controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
1217 // Billing addresses require email.
1218 EXPECT_FALSE(!!controller()->MenuModelForSection(SECTION_BILLING));
1221 TEST_F(AutofillDialogControllerTest, AutofillCreditCards) {
1222 SwitchToAutofill();
1223 // Since the PersonalDataManager is empty, this should only have the
1224 // default menu items.
1225 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC));
1227 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(3);
1229 // Empty cards are ignored.
1230 CreditCard empty_card(base::GenerateGUID(), kSettingsOrigin);
1231 empty_card.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("John Doe"));
1232 controller()->GetTestingManager()->AddTestingCreditCard(&empty_card);
1233 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC));
1235 // An otherwise full but unverified card should be ignored.
1236 CreditCard full_card(test::GetCreditCard());
1237 full_card.set_origin("https://www.example.com");
1238 controller()->GetTestingManager()->AddTestingCreditCard(&full_card);
1239 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC));
1241 // A full, verified card should be picked up.
1242 CreditCard verified_card(test::GetCreditCard());
1243 verified_card.set_origin(kSettingsOrigin);
1244 controller()->GetTestingManager()->AddTestingCreditCard(&verified_card);
1245 ui::MenuModel* credit_card_model =
1246 controller()->MenuModelForSection(SECTION_CC);
1247 ASSERT_TRUE(credit_card_model);
1248 EXPECT_EQ(3, credit_card_model->GetItemCount());
1251 // Test selecting a shipping address different from billing as address.
1252 TEST_F(AutofillDialogControllerTest, DontUseBillingAsShipping) {
1253 SwitchToAutofill();
1254 AutofillProfile full_profile(test::GetVerifiedProfile());
1255 AutofillProfile full_profile2(test::GetVerifiedProfile2());
1256 CreditCard credit_card(test::GetVerifiedCreditCard());
1257 controller()->GetTestingManager()->AddTestingProfile(&full_profile);
1258 controller()->GetTestingManager()->AddTestingProfile(&full_profile2);
1259 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
1260 ui::MenuModel* shipping_model =
1261 controller()->MenuModelForSection(SECTION_SHIPPING);
1262 shipping_model->ActivatedAt(2);
1264 controller()->OnAccept();
1265 ASSERT_EQ(20U, form_structure()->field_count());
1266 EXPECT_EQ(ADDRESS_HOME_STATE,
1267 form_structure()->field(9)->Type().GetStorableType());
1268 EXPECT_EQ(ADDRESS_BILLING, form_structure()->field(9)->Type().group());
1269 EXPECT_EQ(ADDRESS_HOME_STATE,
1270 form_structure()->field(16)->Type().GetStorableType());
1271 EXPECT_EQ(ADDRESS_HOME, form_structure()->field(16)->Type().group());
1272 base::string16 billing_state = form_structure()->field(9)->value;
1273 base::string16 shipping_state = form_structure()->field(16)->value;
1274 EXPECT_FALSE(billing_state.empty());
1275 EXPECT_FALSE(shipping_state.empty());
1276 EXPECT_NE(billing_state, shipping_state);
1278 EXPECT_EQ(CREDIT_CARD_NAME,
1279 form_structure()->field(1)->Type().GetStorableType());
1280 base::string16 cc_name = form_structure()->field(1)->value;
1281 EXPECT_EQ(NAME_FULL, form_structure()->field(6)->Type().GetStorableType());
1282 EXPECT_EQ(NAME_BILLING, form_structure()->field(6)->Type().group());
1283 base::string16 billing_name = form_structure()->field(6)->value;
1284 EXPECT_EQ(NAME_FULL, form_structure()->field(13)->Type().GetStorableType());
1285 EXPECT_EQ(NAME, form_structure()->field(13)->Type().group());
1286 base::string16 shipping_name = form_structure()->field(13)->value;
1288 EXPECT_FALSE(cc_name.empty());
1289 EXPECT_FALSE(billing_name.empty());
1290 EXPECT_FALSE(shipping_name.empty());
1291 // Billing name should always be the same as cardholder name.
1292 EXPECT_EQ(cc_name, billing_name);
1293 EXPECT_NE(cc_name, shipping_name);
1296 // Test selecting UseBillingForShipping.
1297 TEST_F(AutofillDialogControllerTest, UseBillingAsShipping) {
1298 SwitchToAutofill();
1300 AutofillProfile full_profile(test::GetVerifiedProfile());
1301 controller()->GetTestingManager()->AddTestingProfile(&full_profile);
1303 AutofillProfile full_profile2(test::GetVerifiedProfile2());
1304 controller()->GetTestingManager()->AddTestingProfile(&full_profile2);
1306 CreditCard credit_card(test::GetVerifiedCreditCard());
1307 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
1309 ASSERT_FALSE(controller()->IsManuallyEditingSection(SECTION_CC));
1310 ASSERT_FALSE(controller()->IsManuallyEditingSection(SECTION_BILLING));
1312 SubmitAndVerifyShippingAndBillingResults();
1315 TEST_F(AutofillDialogControllerTest, UseBillingAsShippingManualInput) {
1316 SwitchToAutofill();
1318 ASSERT_TRUE(controller()->IsManuallyEditingSection(SECTION_CC));
1319 ASSERT_TRUE(controller()->IsManuallyEditingSection(SECTION_BILLING));
1321 CreditCard credit_card(test::GetVerifiedCreditCard());
1322 FillInputs(SECTION_CC, credit_card);
1324 AutofillProfile full_profile(test::GetVerifiedProfile());
1325 FillInputs(SECTION_BILLING, full_profile);
1327 SubmitAndVerifyShippingAndBillingResults();
1330 // Tests that shipping and billing telephone fields are supported, and filled
1331 // in by their respective profiles. http://crbug.com/244515
1332 TEST_F(AutofillDialogControllerTest, BillingVsShippingPhoneNumber) {
1333 FormFieldData shipping_tel;
1334 shipping_tel.autocomplete_attribute = "shipping tel";
1335 FormFieldData billing_tel;
1336 billing_tel.autocomplete_attribute = "billing tel";
1337 FormFieldData cc_field;
1338 cc_field.autocomplete_attribute = "cc-csc";
1340 FormData form_data;
1341 form_data.fields.push_back(shipping_tel);
1342 form_data.fields.push_back(billing_tel);
1343 form_data.fields.push_back(cc_field);
1344 SetUpControllerWithFormData(form_data);
1346 SwitchToAutofill();
1348 // The profile that will be chosen for the shipping section.
1349 AutofillProfile shipping_profile(test::GetVerifiedProfile());
1350 // The profile that will be chosen for the billing section.
1351 AutofillProfile billing_profile(test::GetVerifiedProfile2());
1352 CreditCard credit_card(test::GetVerifiedCreditCard());
1353 controller()->GetTestingManager()->AddTestingProfile(&shipping_profile);
1354 controller()->GetTestingManager()->AddTestingProfile(&billing_profile);
1355 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
1356 ui::MenuModel* billing_model =
1357 controller()->MenuModelForSection(SECTION_BILLING);
1358 billing_model->ActivatedAt(1);
1360 controller()->OnAccept();
1361 ASSERT_EQ(3U, form_structure()->field_count());
1362 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER,
1363 form_structure()->field(0)->Type().GetStorableType());
1364 EXPECT_EQ(PHONE_HOME, form_structure()->field(0)->Type().group());
1365 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER,
1366 form_structure()->field(1)->Type().GetStorableType());
1367 EXPECT_EQ(PHONE_BILLING, form_structure()->field(1)->Type().group());
1368 EXPECT_EQ(shipping_profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER),
1369 form_structure()->field(0)->value);
1370 EXPECT_EQ(billing_profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER),
1371 form_structure()->field(1)->value);
1372 EXPECT_NE(form_structure()->field(1)->value,
1373 form_structure()->field(0)->value);
1376 // Similar to the above, but tests that street-address (i.e. all lines of the
1377 // street address) is successfully filled for both shipping and billing
1378 // sections.
1379 TEST_F(AutofillDialogControllerTest, BillingVsShippingStreetAddress) {
1380 FormFieldData shipping_address;
1381 shipping_address.autocomplete_attribute = "shipping street-address";
1382 FormFieldData billing_address;
1383 billing_address.autocomplete_attribute = "billing street-address";
1384 FormFieldData shipping_address_textarea;
1385 shipping_address_textarea.autocomplete_attribute = "shipping street-address";
1386 shipping_address_textarea.form_control_type = "textarea";
1387 FormFieldData billing_address_textarea;
1388 billing_address_textarea.autocomplete_attribute = "billing street-address";
1389 billing_address_textarea.form_control_type = "textarea";
1390 FormFieldData cc_field;
1391 cc_field.autocomplete_attribute = "cc-csc";
1393 FormData form_data;
1394 form_data.fields.push_back(shipping_address);
1395 form_data.fields.push_back(billing_address);
1396 form_data.fields.push_back(shipping_address_textarea);
1397 form_data.fields.push_back(billing_address_textarea);
1398 form_data.fields.push_back(cc_field);
1399 SetUpControllerWithFormData(form_data);
1401 SwitchToAutofill();
1403 // The profile that will be chosen for the shipping section.
1404 AutofillProfile shipping_profile(test::GetVerifiedProfile());
1405 // The profile that will be chosen for the billing section.
1406 AutofillProfile billing_profile(test::GetVerifiedProfile2());
1407 CreditCard credit_card(test::GetVerifiedCreditCard());
1408 controller()->GetTestingManager()->AddTestingProfile(&shipping_profile);
1409 controller()->GetTestingManager()->AddTestingProfile(&billing_profile);
1410 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
1411 ui::MenuModel* billing_model =
1412 controller()->MenuModelForSection(SECTION_BILLING);
1413 billing_model->ActivatedAt(1);
1415 controller()->OnAccept();
1416 ASSERT_EQ(5U, form_structure()->field_count());
1417 EXPECT_EQ(ADDRESS_HOME_STREET_ADDRESS,
1418 form_structure()->field(0)->Type().GetStorableType());
1419 EXPECT_EQ(ADDRESS_HOME, form_structure()->field(0)->Type().group());
1420 EXPECT_EQ(ADDRESS_HOME_STREET_ADDRESS,
1421 form_structure()->field(1)->Type().GetStorableType());
1422 EXPECT_EQ(ADDRESS_BILLING, form_structure()->field(1)->Type().group());
1423 // Inexact matching; single-line inputs get the address data concatenated but
1424 // separated by commas.
1425 EXPECT_TRUE(StartsWith(form_structure()->field(0)->value,
1426 shipping_profile.GetRawInfo(ADDRESS_HOME_LINE1),
1427 true));
1428 EXPECT_TRUE(EndsWith(form_structure()->field(0)->value,
1429 shipping_profile.GetRawInfo(ADDRESS_HOME_LINE2),
1430 true));
1431 EXPECT_TRUE(StartsWith(form_structure()->field(1)->value,
1432 billing_profile.GetRawInfo(ADDRESS_HOME_LINE1),
1433 true));
1434 EXPECT_TRUE(EndsWith(form_structure()->field(1)->value,
1435 billing_profile.GetRawInfo(ADDRESS_HOME_LINE2),
1436 true));
1437 // The textareas should be an exact match.
1438 EXPECT_EQ(shipping_profile.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS),
1439 form_structure()->field(2)->value);
1440 EXPECT_EQ(billing_profile.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS),
1441 form_structure()->field(3)->value);
1443 EXPECT_NE(form_structure()->field(1)->value,
1444 form_structure()->field(0)->value);
1445 EXPECT_NE(form_structure()->field(3)->value,
1446 form_structure()->field(2)->value);
1449 // Test asking for different pieces of the name.
1450 TEST_F(AutofillDialogControllerTest, NamePieces) {
1451 const char* const attributes[] = {
1452 "shipping name",
1453 "billing name",
1454 "billing given-name",
1455 "billing family-name",
1456 "billing additional-name",
1457 "cc-csc"
1460 FormData form_data;
1461 for (size_t i = 0; i < arraysize(attributes); ++i) {
1462 FormFieldData field;
1463 field.autocomplete_attribute.assign(attributes[i]);
1464 form_data.fields.push_back(field);
1467 SetUpControllerWithFormData(form_data);
1468 SwitchToAutofill();
1470 // Billing.
1471 AutofillProfile test_profile(test::GetVerifiedProfile());
1472 test_profile.SetInfo(AutofillType(NAME_FULL),
1473 ASCIIToUTF16("Fabian Jackson von Nacho"),
1474 "en-US");
1475 controller()->GetTestingManager()->AddTestingProfile(&test_profile);
1477 // Credit card.
1478 CreditCard credit_card(test::GetVerifiedCreditCard());
1479 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
1481 // Make shipping name different from billing.
1482 AutofillProfile test_profile2(test::GetVerifiedProfile2());
1483 test_profile2.SetInfo(AutofillType(NAME_FULL),
1484 ASCIIToUTF16("Don Ford"),
1485 "en-US");
1486 controller()->GetTestingManager()->AddTestingProfile(&test_profile2);
1487 ui::MenuModel* shipping_model =
1488 controller()->MenuModelForSection(SECTION_SHIPPING);
1489 shipping_model->ActivatedAt(2);
1491 controller()->OnAccept();
1493 EXPECT_EQ(NAME_FULL, form_structure()->field(0)->Type().GetStorableType());
1494 EXPECT_EQ(ASCIIToUTF16("Don Ford"),
1495 form_structure()->field(0)->value);
1497 EXPECT_EQ(NAME_FULL, form_structure()->field(1)->Type().GetStorableType());
1498 EXPECT_EQ(ASCIIToUTF16("Fabian Jackson von Nacho"),
1499 form_structure()->field(1)->value);
1501 EXPECT_EQ(NAME_FIRST, form_structure()->field(2)->Type().GetStorableType());
1502 EXPECT_EQ(ASCIIToUTF16("Fabian"),
1503 form_structure()->field(2)->value);
1505 EXPECT_EQ(NAME_LAST, form_structure()->field(3)->Type().GetStorableType());
1506 EXPECT_EQ(ASCIIToUTF16("von Nacho"),
1507 form_structure()->field(3)->value);
1509 EXPECT_EQ(NAME_MIDDLE, form_structure()->field(4)->Type().GetStorableType());
1510 EXPECT_EQ(ASCIIToUTF16("Jackson"),
1511 form_structure()->field(4)->value);
1514 TEST_F(AutofillDialogControllerTest, AcceptLegalDocuments) {
1515 for (size_t i = 0; i < 2; ++i) {
1516 SCOPED_TRACE(testing::Message() << "Case " << i);
1518 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1519 AcceptLegalDocuments(_, _));
1520 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_));
1521 EXPECT_CALL(*controller(), LoadRiskFingerprintData());
1523 EXPECT_TRUE(controller()->LegalDocumentLinks().empty());
1524 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
1525 EXPECT_TRUE(controller()->LegalDocumentLinks().empty());
1527 scoped_ptr<wallet::WalletItems> wallet_items =
1528 CompleteAndValidWalletItems();
1529 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
1530 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
1531 controller()->OnDidGetWalletItems(wallet_items.Pass());
1532 EXPECT_FALSE(controller()->LegalDocumentLinks().empty());
1534 controller()->OnAccept();
1535 controller()->OnDidAcceptLegalDocuments();
1536 controller()->OnDidLoadRiskFingerprintData("a");
1538 // Now try it all over again with the location disclosure already accepted.
1539 // Nothing should change.
1540 Reset();
1541 base::ListValue preexisting_list;
1542 preexisting_list.AppendString(kFakeEmail);
1543 g_browser_process->local_state()->Set(
1544 ::prefs::kAutofillDialogWalletLocationAcceptance,
1545 preexisting_list);
1549 TEST_F(AutofillDialogControllerTest, RejectLegalDocuments) {
1550 for (size_t i = 0; i < 2; ++i) {
1551 SCOPED_TRACE(testing::Message() << "Case " << i);
1553 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1554 AcceptLegalDocuments(_, _)).Times(0);
1556 scoped_ptr<wallet::WalletItems> wallet_items =
1557 CompleteAndValidWalletItems();
1558 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
1559 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
1560 controller()->OnDidGetWalletItems(wallet_items.Pass());
1561 EXPECT_FALSE(controller()->LegalDocumentLinks().empty());
1563 controller()->OnCancel();
1565 // Now try it all over again with the location disclosure already accepted.
1566 // Nothing should change.
1567 Reset();
1568 base::ListValue preexisting_list;
1569 preexisting_list.AppendString(kFakeEmail);
1570 g_browser_process->local_state()->Set(
1571 ::prefs::kAutofillDialogWalletLocationAcceptance,
1572 preexisting_list);
1576 TEST_F(AutofillDialogControllerTest, AcceptLocationDisclosure) {
1577 // Check that accepting the dialog registers the user's name in the list
1578 // of users who have accepted the geolocation terms.
1579 EXPECT_TRUE(g_browser_process->local_state()->GetList(
1580 ::prefs::kAutofillDialogWalletLocationAcceptance)->empty());
1582 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
1583 EXPECT_FALSE(controller()->LegalDocumentsText().empty());
1584 EXPECT_TRUE(controller()->LegalDocumentLinks().empty());
1585 controller()->OnAccept();
1587 const base::ListValue* list = g_browser_process->local_state()->GetList(
1588 ::prefs::kAutofillDialogWalletLocationAcceptance);
1589 ASSERT_EQ(1U, list->GetSize());
1590 std::string accepted_username;
1591 EXPECT_TRUE(list->GetString(0, &accepted_username));
1592 EXPECT_EQ(kFakeEmail, accepted_username);
1594 // Now check it still works if that list starts off with some other username
1595 // in it.
1596 Reset();
1597 list = g_browser_process->local_state()->GetList(
1598 ::prefs::kAutofillDialogWalletLocationAcceptance);
1599 ASSERT_TRUE(list->empty());
1601 std::string kOtherUsername("spouse@example.com");
1602 base::ListValue preexisting_list;
1603 preexisting_list.AppendString(kOtherUsername);
1604 g_browser_process->local_state()->Set(
1605 ::prefs::kAutofillDialogWalletLocationAcceptance,
1606 preexisting_list);
1608 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
1609 EXPECT_FALSE(controller()->LegalDocumentsText().empty());
1610 EXPECT_TRUE(controller()->LegalDocumentLinks().empty());
1611 controller()->OnAccept();
1613 list = g_browser_process->local_state()->GetList(
1614 ::prefs::kAutofillDialogWalletLocationAcceptance);
1615 ASSERT_EQ(2U, list->GetSize());
1616 EXPECT_NE(list->end(), list->Find(base::StringValue(kFakeEmail)));
1617 EXPECT_NE(list->end(), list->Find(base::StringValue(kOtherUsername)));
1619 // Now check the list doesn't change if the user cancels out of the dialog.
1620 Reset();
1621 list = g_browser_process->local_state()->GetList(
1622 ::prefs::kAutofillDialogWalletLocationAcceptance);
1623 ASSERT_TRUE(list->empty());
1625 g_browser_process->local_state()->Set(
1626 ::prefs::kAutofillDialogWalletLocationAcceptance,
1627 preexisting_list);
1629 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
1630 EXPECT_FALSE(controller()->LegalDocumentsText().empty());
1631 EXPECT_TRUE(controller()->LegalDocumentLinks().empty());
1632 controller()->OnCancel();
1634 list = g_browser_process->local_state()->GetList(
1635 ::prefs::kAutofillDialogWalletLocationAcceptance);
1636 ASSERT_EQ(1U, list->GetSize());
1637 EXPECT_NE(list->end(), list->Find(base::StringValue(kOtherUsername)));
1638 EXPECT_EQ(list->end(), list->Find(base::StringValue(kFakeEmail)));
1641 TEST_F(AutofillDialogControllerTest, LegalDocumentOverflow) {
1642 for (size_t number_of_docs = 2; number_of_docs < 11; ++number_of_docs) {
1643 scoped_ptr<wallet::WalletItems> wallet_items =
1644 CompleteAndValidWalletItems();
1645 for (size_t i = 0; i < number_of_docs; ++i)
1646 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
1648 Reset();
1649 controller()->OnDidGetWalletItems(wallet_items.Pass());
1651 // The dialog is only equipped to handle 2-6 legal documents. More than
1652 // 6 errors out.
1653 if (number_of_docs <= 6U) {
1654 EXPECT_FALSE(controller()->LegalDocumentsText().empty());
1655 } else {
1656 EXPECT_TRUE(controller()->LegalDocumentsText().empty());
1657 EXPECT_EQ(1U, NotificationsOfType(
1658 DialogNotification::WALLET_ERROR).size());
1662 controller()->OnCancel();
1665 // Makes sure the default object IDs are respected.
1666 TEST_F(AutofillDialogControllerTest, WalletDefaultItems) {
1667 scoped_ptr<wallet::WalletItems> wallet_items =
1668 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
1669 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1670 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1671 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
1672 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1674 wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
1675 wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
1676 wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
1677 wallet_items->AddAddress(wallet::GetTestShippingAddress());
1678 wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
1680 controller()->OnDidGetWalletItems(wallet_items.Pass());
1681 // "add", "manage", and 4 suggestions.
1682 EXPECT_EQ(6,
1683 controller()->MenuModelForSection(SECTION_CC_BILLING)->GetItemCount());
1684 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING)->
1685 IsItemCheckedAt(2));
1686 ASSERT_FALSE(controller()->IsEditingExistingData(SECTION_CC_BILLING));
1687 // "use billing", "add", "manage", and 5 suggestions.
1688 EXPECT_EQ(8,
1689 controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
1690 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_SHIPPING)->
1691 IsItemCheckedAt(4));
1692 ASSERT_FALSE(controller()->IsEditingExistingData(SECTION_SHIPPING));
1695 // Tests that invalid and AMEX default instruments are ignored.
1696 TEST_F(AutofillDialogControllerTest, SelectInstrument) {
1697 scoped_ptr<wallet::WalletItems> wallet_items =
1698 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
1699 // Tests if default instrument is invalid, then, the first valid instrument is
1700 // selected instead of the default instrument.
1701 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1702 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1703 wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentInvalid());
1704 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1706 controller()->OnDidGetWalletItems(wallet_items.Pass());
1707 // 4 suggestions and "add", "manage".
1708 EXPECT_EQ(6,
1709 controller()->MenuModelForSection(SECTION_CC_BILLING)->GetItemCount());
1710 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING)->
1711 IsItemCheckedAt(0));
1713 // Tests if default instrument is AMEX but Wallet doesn't support
1714 // AMEX on this merchant, then the first valid instrument is
1715 // selected instead of the default instrument.
1716 wallet_items = wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
1717 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1718 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1719 wallet_items->AddInstrument(
1720 wallet::GetTestMaskedInstrumentAmex(wallet::AMEX_DISALLOWED));
1721 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1723 controller()->OnDidGetWalletItems(wallet_items.Pass());
1724 // 4 suggestions and "add", "manage".
1725 EXPECT_EQ(6,
1726 controller()->MenuModelForSection(SECTION_CC_BILLING)->GetItemCount());
1727 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING)->
1728 IsItemCheckedAt(0));
1730 // Tests if default instrument is AMEX and it is allowed on this merchant,
1731 // then it is selected.
1732 wallet_items = wallet::GetTestWalletItems(wallet::AMEX_ALLOWED);
1733 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1734 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1735 wallet_items->AddInstrument(
1736 wallet::GetTestMaskedInstrumentAmex(wallet::AMEX_ALLOWED));
1737 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1739 controller()->OnDidGetWalletItems(wallet_items.Pass());
1740 // 4 suggestions and "add", "manage".
1741 EXPECT_EQ(6,
1742 controller()->MenuModelForSection(SECTION_CC_BILLING)->GetItemCount());
1743 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING)->
1744 IsItemCheckedAt(2));
1746 // Tests if only have AMEX and invalid instrument, then "add" is selected.
1747 wallet_items = wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
1748 wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentInvalid());
1749 wallet_items->AddInstrument(
1750 wallet::GetTestMaskedInstrumentAmex(wallet::AMEX_DISALLOWED));
1752 controller()->OnDidGetWalletItems(wallet_items.Pass());
1753 // 2 suggestions and "add", "manage".
1754 EXPECT_EQ(4,
1755 controller()->MenuModelForSection(SECTION_CC_BILLING)->GetItemCount());
1756 // "add"
1757 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING)->
1758 IsItemCheckedAt(2));
1761 TEST_F(AutofillDialogControllerTest, SaveAddress) {
1762 EXPECT_CALL(*controller()->GetView(), ModelChanged());
1763 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1764 SaveToWalletMock(testing::IsNull(),
1765 testing::NotNull(),
1766 testing::IsNull(),
1767 testing::IsNull()));
1769 scoped_ptr<wallet::WalletItems> wallet_items =
1770 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
1771 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
1772 controller()->OnDidGetWalletItems(wallet_items.Pass());
1773 // If there is no shipping address in wallet, it will default to
1774 // "same-as-billing" instead of "add-new-item". "same-as-billing" is covered
1775 // by the following tests. The penultimate item in the menu is "add-new-item".
1776 ui::MenuModel* shipping_model =
1777 controller()->MenuModelForSection(SECTION_SHIPPING);
1778 shipping_model->ActivatedAt(shipping_model->GetItemCount() - 2);
1780 AutofillProfile test_profile(test::GetVerifiedProfile());
1781 FillInputs(SECTION_SHIPPING, test_profile);
1783 AcceptAndLoadFakeFingerprint();
1786 TEST_F(AutofillDialogControllerTest, SaveInstrument) {
1787 EXPECT_CALL(*controller()->GetView(), ModelChanged());
1788 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1789 SaveToWalletMock(testing::NotNull(),
1790 testing::IsNull(),
1791 testing::IsNull(),
1792 testing::IsNull()));
1794 FillCCBillingInputs();
1795 scoped_ptr<wallet::WalletItems> wallet_items =
1796 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
1797 wallet_items->AddAddress(wallet::GetTestShippingAddress());
1798 SubmitWithWalletItems(wallet_items.Pass());
1801 TEST_F(AutofillDialogControllerTest, SaveInstrumentWithInvalidInstruments) {
1802 EXPECT_CALL(*controller()->GetView(), ModelChanged());
1803 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1804 SaveToWalletMock(testing::NotNull(),
1805 testing::IsNull(),
1806 testing::IsNull(),
1807 testing::IsNull()));
1809 FillCCBillingInputs();
1810 scoped_ptr<wallet::WalletItems> wallet_items =
1811 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
1812 wallet_items->AddAddress(wallet::GetTestShippingAddress());
1813 wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentInvalid());
1814 SubmitWithWalletItems(wallet_items.Pass());
1817 TEST_F(AutofillDialogControllerTest, SaveInstrumentAndAddress) {
1818 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1819 SaveToWalletMock(testing::NotNull(),
1820 testing::NotNull(),
1821 testing::IsNull(),
1822 testing::IsNull()));
1824 FillCCBillingInputs();
1825 scoped_ptr<wallet::WalletItems> wallet_items =
1826 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
1827 SubmitWithWalletItems(wallet_items.Pass());
1830 MATCHER(IsUpdatingExistingData, "updating existing Wallet data") {
1831 return !arg->object_id().empty();
1834 MATCHER(UsesLocalBillingAddress, "uses the local billing address") {
1835 return arg->street_address()[0] == ASCIIToUTF16(kEditedBillingAddress);
1838 // Tests that when using billing address for shipping, and there is no exact
1839 // matched shipping address, then a shipping address should be added.
1840 TEST_F(AutofillDialogControllerTest, BillingForShipping) {
1841 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1842 SaveToWalletMock(testing::IsNull(),
1843 testing::NotNull(),
1844 testing::IsNull(),
1845 testing::IsNull()));
1847 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
1848 // Select "Same as billing" in the address menu.
1849 UseBillingForShipping();
1851 AcceptAndLoadFakeFingerprint();
1854 // Tests that when using billing address for shipping, and there is an exact
1855 // matched shipping address, then a shipping address should not be added.
1856 TEST_F(AutofillDialogControllerTest, BillingForShippingHasMatch) {
1857 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1858 SaveToWalletMock(_, _, _, _)).Times(0);
1860 scoped_ptr<wallet::WalletItems> wallet_items =
1861 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
1862 scoped_ptr<wallet::WalletItems::MaskedInstrument> instrument =
1863 wallet::GetTestMaskedInstrument();
1864 // Copy billing address as shipping address, and assign an id to it.
1865 scoped_ptr<wallet::Address> shipping_address(
1866 new wallet::Address(instrument->address()));
1867 shipping_address->set_object_id("shipping_address_id");
1868 wallet_items->AddAddress(shipping_address.Pass());
1869 wallet_items->AddInstrument(instrument.Pass());
1870 wallet_items->AddAddress(wallet::GetTestShippingAddress());
1872 controller()->OnDidGetWalletItems(wallet_items.Pass());
1873 // Select "Same as billing" in the address menu.
1874 UseBillingForShipping();
1876 AcceptAndLoadFakeFingerprint();
1879 // Test that the local view contents is used when saving a new instrument and
1880 // the user has selected "Same as billing".
1881 TEST_F(AutofillDialogControllerTest, SaveInstrumentSameAsBilling) {
1882 scoped_ptr<wallet::WalletItems> wallet_items =
1883 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
1884 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
1885 controller()->OnDidGetWalletItems(wallet_items.Pass());
1887 ui::MenuModel* model = controller()->MenuModelForSection(SECTION_CC_BILLING);
1888 model->ActivatedAt(model->GetItemCount() - 2);
1890 FieldValueMap outputs;
1891 const DetailInputs& inputs =
1892 controller()->RequestedFieldsForSection(SECTION_CC_BILLING);
1893 AutofillProfile full_profile(test::GetVerifiedProfile());
1894 CreditCard full_card(test::GetCreditCard());
1895 for (size_t i = 0; i < inputs.size(); ++i) {
1896 const ServerFieldType type = inputs[i].type;
1897 if (type == ADDRESS_BILLING_STREET_ADDRESS)
1898 outputs[type] = ASCIIToUTF16(kEditedBillingAddress);
1899 else
1900 outputs[type] = full_profile.GetInfo(AutofillType(type), "en-US");
1902 if (outputs[type].empty())
1903 outputs[type] = full_card.GetInfo(AutofillType(type), "en-US");
1905 controller()->GetView()->SetUserInput(SECTION_CC_BILLING, outputs);
1907 controller()->OnAccept();
1909 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1910 SaveToWalletMock(testing::NotNull(),
1911 UsesLocalBillingAddress(),
1912 testing::IsNull(),
1913 testing::IsNull()));
1914 AcceptAndLoadFakeFingerprint();
1917 TEST_F(AutofillDialogControllerTest, CancelNoSave) {
1918 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1919 SaveToWalletMock(_, _, _, _)).Times(0);
1921 EXPECT_CALL(*controller()->GetView(), ModelChanged());
1923 controller()->OnDidGetWalletItems(
1924 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED));
1925 controller()->OnCancel();
1928 // Checks that clicking the Manage menu item opens a new tab with a different
1929 // URL for Wallet and Autofill.
1930 TEST_F(AutofillDialogControllerTest, ManageItem) {
1931 AutofillProfile full_profile(test::GetVerifiedProfile());
1932 full_profile.set_origin(kSettingsOrigin);
1933 full_profile.SetRawInfo(ADDRESS_HOME_LINE2, base::string16());
1934 controller()->GetTestingManager()->AddTestingProfile(&full_profile);
1935 SwitchToAutofill();
1937 SuggestionsMenuModel* shipping = GetMenuModelForSection(SECTION_SHIPPING);
1938 shipping->ExecuteCommand(shipping->GetItemCount() - 1, 0);
1939 GURL autofill_manage_url = controller()->open_tab_url();
1940 EXPECT_EQ("chrome", autofill_manage_url.scheme());
1942 SwitchToWallet();
1943 scoped_ptr<wallet::WalletItems> wallet_items =
1944 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
1945 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
1946 controller()->OnDidGetWalletItems(wallet_items.Pass());
1948 controller()->SuggestionItemSelected(shipping, shipping->GetItemCount() - 1);
1949 GURL wallet_manage_addresses_url = controller()->open_tab_url();
1950 EXPECT_EQ("https", wallet_manage_addresses_url.scheme());
1952 SuggestionsMenuModel* billing = GetMenuModelForSection(SECTION_CC_BILLING);
1953 controller()->SuggestionItemSelected(billing, billing->GetItemCount() - 1);
1954 GURL wallet_manage_instruments_url = controller()->open_tab_url();
1955 EXPECT_EQ("https", wallet_manage_instruments_url.scheme());
1957 EXPECT_NE(autofill_manage_url, wallet_manage_instruments_url);
1958 EXPECT_NE(wallet_manage_instruments_url, wallet_manage_addresses_url);
1961 // Tests that adding an autofill profile and then submitting works.
1962 TEST_F(AutofillDialogControllerTest, AddAutofillProfile) {
1963 SwitchToAutofill();
1964 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(2);
1966 AutofillProfile full_profile(test::GetVerifiedProfile());
1967 CreditCard credit_card(test::GetVerifiedCreditCard());
1968 controller()->GetTestingManager()->AddTestingProfile(&full_profile);
1969 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
1971 ui::MenuModel* model = controller()->MenuModelForSection(SECTION_BILLING);
1972 // Activate the "Add billing address" menu item.
1973 model->ActivatedAt(model->GetItemCount() - 2);
1975 // Fill in the inputs from the profile.
1976 FieldValueMap outputs;
1977 const DetailInputs& inputs =
1978 controller()->RequestedFieldsForSection(SECTION_BILLING);
1979 AutofillProfile full_profile2(test::GetVerifiedProfile2());
1980 for (size_t i = 0; i < inputs.size(); ++i) {
1981 const ServerFieldType type = inputs[i].type;
1982 outputs[type] = full_profile2.GetInfo(AutofillType(type), "en-US");
1984 controller()->GetView()->SetUserInput(SECTION_BILLING, outputs);
1986 controller()->OnAccept();
1987 const AutofillProfile& added_profile =
1988 controller()->GetTestingManager()->imported_profile();
1990 const DetailInputs& shipping_inputs =
1991 controller()->RequestedFieldsForSection(SECTION_SHIPPING);
1992 for (size_t i = 0; i < shipping_inputs.size(); ++i) {
1993 const ServerFieldType type = shipping_inputs[i].type;
1994 EXPECT_EQ(full_profile2.GetInfo(AutofillType(type), "en-US"),
1995 added_profile.GetInfo(AutofillType(type), "en-US"));
1999 TEST_F(AutofillDialogControllerTest, VerifyCvv) {
2000 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_));
2001 EXPECT_CALL(*controller()->GetTestingWalletClient(),
2002 AuthenticateInstrument(_, _));
2004 SubmitWithWalletItems(CompleteAndValidWalletItems());
2006 EXPECT_TRUE(NotificationsOfType(DialogNotification::REQUIRED_ACTION).empty());
2007 EXPECT_TRUE(controller()->SectionIsActive(SECTION_SHIPPING));
2008 EXPECT_TRUE(controller()->SectionIsActive(SECTION_CC_BILLING));
2009 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2010 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL));
2012 SuggestionState suggestion_state =
2013 controller()->SuggestionStateForSection(SECTION_CC_BILLING);
2014 EXPECT_TRUE(suggestion_state.extra_text.empty());
2016 controller()->OnDidGetFullWallet(
2017 wallet::GetTestFullWalletWithRequiredActions(
2018 std::vector<wallet::RequiredAction>(1, wallet::VERIFY_CVV)));
2019 ASSERT_TRUE(controller()->IsSubmitPausedOn(wallet::VERIFY_CVV));
2021 EXPECT_FALSE(
2022 NotificationsOfType(DialogNotification::REQUIRED_ACTION).empty());
2023 EXPECT_FALSE(controller()->SectionIsActive(SECTION_SHIPPING));
2024 EXPECT_TRUE(controller()->SectionIsActive(SECTION_CC_BILLING));
2026 suggestion_state =
2027 controller()->SuggestionStateForSection(SECTION_CC_BILLING);
2028 EXPECT_FALSE(suggestion_state.extra_text.empty());
2029 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC_BILLING));
2031 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2032 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL));
2034 controller()->OnAccept();
2036 EXPECT_FALSE(controller()->GetDialogOverlay().image.IsEmpty());
2039 TEST_F(AutofillDialogControllerTest, ErrorDuringSubmit) {
2040 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_));
2042 SubmitWithWalletItems(CompleteAndValidWalletItems());
2044 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2045 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL));
2047 controller()->OnWalletError(wallet::WalletClient::UNKNOWN_ERROR);
2049 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2050 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL));
2053 TEST_F(AutofillDialogControllerTest, ErrorDuringVerifyCvv) {
2054 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_));
2056 SubmitWithWalletItems(CompleteAndValidWalletItems());
2057 controller()->OnDidGetFullWallet(
2058 wallet::GetTestFullWalletWithRequiredActions(
2059 std::vector<wallet::RequiredAction>(1, wallet::VERIFY_CVV)));
2061 ASSERT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2062 ASSERT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL));
2064 controller()->OnWalletError(wallet::WalletClient::UNKNOWN_ERROR);
2066 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2067 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL));
2070 // Simulates receiving an INVALID_FORM_FIELD required action while processing a
2071 // |WalletClientDelegate::OnDid{Save,Update}*()| call. This can happen if Online
2072 // Wallet's server validation differs from Chrome's local validation.
2073 TEST_F(AutofillDialogControllerTest, WalletServerSideValidation) {
2074 scoped_ptr<wallet::WalletItems> wallet_items =
2075 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
2076 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
2077 controller()->OnDidGetWalletItems(wallet_items.Pass());
2078 controller()->OnAccept();
2080 std::vector<wallet::RequiredAction> required_actions;
2081 required_actions.push_back(wallet::INVALID_FORM_FIELD);
2083 std::vector<wallet::FormFieldError> form_errors;
2084 form_errors.push_back(
2085 wallet::FormFieldError(wallet::FormFieldError::INVALID_POSTAL_CODE,
2086 wallet::FormFieldError::SHIPPING_ADDRESS));
2088 EXPECT_CALL(*controller()->GetView(), UpdateForErrors());
2089 controller()->OnDidSaveToWallet(std::string(),
2090 std::string(),
2091 required_actions,
2092 form_errors);
2095 // Simulates receiving unrecoverable Wallet server validation errors.
2096 TEST_F(AutofillDialogControllerTest, WalletServerSideValidationUnrecoverable) {
2097 scoped_ptr<wallet::WalletItems> wallet_items =
2098 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
2099 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
2100 controller()->OnDidGetWalletItems(wallet_items.Pass());
2101 controller()->OnAccept();
2103 std::vector<wallet::RequiredAction> required_actions;
2104 required_actions.push_back(wallet::INVALID_FORM_FIELD);
2106 std::vector<wallet::FormFieldError> form_errors;
2107 form_errors.push_back(
2108 wallet::FormFieldError(wallet::FormFieldError::UNKNOWN_ERROR,
2109 wallet::FormFieldError::UNKNOWN_LOCATION));
2111 controller()->OnDidSaveToWallet(std::string(),
2112 std::string(),
2113 required_actions,
2114 form_errors);
2116 EXPECT_EQ(1U, NotificationsOfType(
2117 DialogNotification::REQUIRED_ACTION).size());
2120 // Test Wallet banners are show in the right situations. These banners promote
2121 // saving details into Wallet (i.e. "[x] Save details to Wallet").
2122 TEST_F(AutofillDialogControllerTest, WalletBanners) {
2123 // Simulate non-signed-in case.
2124 SetUpControllerWithFormData(DefaultFormData());
2125 GoogleServiceAuthError error(GoogleServiceAuthError::NONE);
2126 controller()->OnPassiveSigninFailure(error);
2127 EXPECT_EQ(0U, NotificationsOfType(
2128 DialogNotification::WALLET_USAGE_CONFIRMATION).size());
2130 // Sign in a user with a completed account.
2131 SetUpControllerWithFormData(DefaultFormData());
2132 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
2134 // Full account; should show "Details from Wallet" message.
2135 EXPECT_EQ(1U, NotificationsOfType(
2136 DialogNotification::WALLET_USAGE_CONFIRMATION).size());
2137 SwitchToAutofill();
2138 EXPECT_EQ(1U, NotificationsOfType(
2139 DialogNotification::WALLET_USAGE_CONFIRMATION).size());
2141 // Start over and sign in a user with an incomplete account.
2142 SetUpControllerWithFormData(DefaultFormData());
2143 scoped_ptr<wallet::WalletItems> wallet_items =
2144 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
2145 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
2146 controller()->OnDidGetWalletItems(wallet_items.Pass());
2148 // Partial account.
2149 EXPECT_EQ(1U, NotificationsOfType(
2150 DialogNotification::WALLET_USAGE_CONFIRMATION).size());
2152 SwitchToAutofill();
2153 EXPECT_EQ(1U, NotificationsOfType(
2154 DialogNotification::WALLET_USAGE_CONFIRMATION).size());
2156 // A Wallet error should kill any Wallet promos.
2157 controller()->OnWalletError(wallet::WalletClient::UNKNOWN_ERROR);
2159 EXPECT_EQ(1U, NotificationsOfType(
2160 DialogNotification::WALLET_ERROR).size());
2161 EXPECT_EQ(0U, NotificationsOfType(
2162 DialogNotification::WALLET_USAGE_CONFIRMATION).size());
2165 TEST_F(AutofillDialogControllerTest, ViewCancelDoesntSetPref) {
2166 ASSERT_FALSE(profile()->GetPrefs()->HasPrefPath(
2167 ::prefs::kAutofillDialogPayWithoutWallet));
2169 SwitchToAutofill();
2171 controller()->OnCancel();
2172 controller()->ViewClosed();
2174 EXPECT_FALSE(profile()->GetPrefs()->HasPrefPath(
2175 ::prefs::kAutofillDialogPayWithoutWallet));
2178 TEST_F(AutofillDialogControllerTest, SubmitWithSigninErrorDoesntSetPref) {
2179 ASSERT_FALSE(profile()->GetPrefs()->HasPrefPath(
2180 ::prefs::kAutofillDialogPayWithoutWallet));
2182 SimulateSigninError();
2183 FillCreditCardInputs();
2184 controller()->OnAccept();
2186 EXPECT_FALSE(profile()->GetPrefs()->HasPrefPath(
2187 ::prefs::kAutofillDialogPayWithoutWallet));
2190 // Tests that there's an overlay shown while waiting for full wallet items.
2191 TEST_F(AutofillDialogControllerTest, WalletFirstRun) {
2192 EXPECT_TRUE(controller()->GetDialogOverlay().image.IsEmpty());
2194 SubmitWithWalletItems(CompleteAndValidWalletItems());
2195 EXPECT_FALSE(controller()->GetDialogOverlay().image.IsEmpty());
2197 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
2198 EXPECT_FALSE(controller()->GetDialogOverlay().image.IsEmpty());
2199 EXPECT_FALSE(form_structure());
2201 // Don't make the test wait for 2 seconds.
2202 controller()->ForceFinishSubmit();
2203 EXPECT_TRUE(form_structure());
2206 TEST_F(AutofillDialogControllerTest, ViewSubmitSetsPref) {
2207 ASSERT_FALSE(profile()->GetPrefs()->HasPrefPath(
2208 ::prefs::kAutofillDialogPayWithoutWallet));
2210 SwitchToAutofill();
2211 FillCreditCardInputs();
2212 controller()->OnAccept();
2214 EXPECT_TRUE(profile()->GetPrefs()->HasPrefPath(
2215 ::prefs::kAutofillDialogPayWithoutWallet));
2216 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(
2217 ::prefs::kAutofillDialogPayWithoutWallet));
2219 // Try again with a signin error (just leaves the pref alone).
2220 SetUpControllerWithFormData(DefaultFormData());
2222 // Setting up the controller again should not change the pref.
2223 EXPECT_TRUE(profile()->GetPrefs()->HasPrefPath(
2224 ::prefs::kAutofillDialogPayWithoutWallet));
2225 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(
2226 ::prefs::kAutofillDialogPayWithoutWallet));
2228 SimulateSigninError();
2229 FillCreditCardInputs();
2230 controller()->OnAccept();
2231 EXPECT_TRUE(profile()->GetPrefs()->HasPrefPath(
2232 ::prefs::kAutofillDialogPayWithoutWallet));
2233 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(
2234 ::prefs::kAutofillDialogPayWithoutWallet));
2236 // Successfully choosing wallet does set the pref.
2237 // Note that OnDidGetWalletItems sets the account chooser to wallet mode.
2238 SetUpControllerWithFormData(DefaultFormData());
2240 controller()->OnDidFetchWalletCookieValue(std::string());
2241 scoped_ptr<wallet::WalletItems> wallet_items =
2242 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
2243 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
2244 controller()->OnDidGetWalletItems(wallet_items.Pass());
2245 controller()->OnAccept();
2246 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
2247 controller()->ForceFinishSubmit();
2249 EXPECT_TRUE(profile()->GetPrefs()->HasPrefPath(
2250 ::prefs::kAutofillDialogPayWithoutWallet));
2251 EXPECT_FALSE(profile()->GetPrefs()->GetBoolean(
2252 ::prefs::kAutofillDialogPayWithoutWallet));
2255 TEST_F(AutofillDialogControllerTest, HideWalletEmail) {
2256 SwitchToAutofill();
2258 // Email field should be showing when using Autofill.
2259 EXPECT_TRUE(controller()->SectionIsActive(SECTION_BILLING));
2260 EXPECT_FALSE(controller()->SectionIsActive(SECTION_CC_BILLING));
2261 EXPECT_TRUE(SectionContainsField(SECTION_BILLING, EMAIL_ADDRESS));
2263 SwitchToWallet();
2265 // Reset the wallet state.
2266 controller()->OnDidGetWalletItems(scoped_ptr<wallet::WalletItems>());
2268 // Setup some wallet state, submit, and get a full wallet to end the flow.
2269 scoped_ptr<wallet::WalletItems> wallet_items = CompleteAndValidWalletItems();
2271 // Filling |form_structure()| depends on the current username and wallet items
2272 // being fetched. Until both of these have occurred, the user should not be
2273 // able to click Submit if using Wallet. The username fetch happened earlier.
2274 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2275 controller()->OnDidGetWalletItems(wallet_items.Pass());
2276 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2278 // Email field should be absent when using Wallet.
2279 EXPECT_FALSE(controller()->SectionIsActive(SECTION_BILLING));
2280 EXPECT_TRUE(controller()->SectionIsActive(SECTION_CC_BILLING));
2281 EXPECT_FALSE(SectionContainsField(SECTION_CC_BILLING, EMAIL_ADDRESS));
2283 controller()->OnAccept();
2284 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
2285 controller()->ForceFinishSubmit();
2287 ASSERT_TRUE(form_structure());
2288 size_t i = 0;
2289 for (; i < form_structure()->field_count(); ++i) {
2290 if (form_structure()->field(i)->Type().GetStorableType() == EMAIL_ADDRESS) {
2291 EXPECT_EQ(ASCIIToUTF16(kFakeEmail), form_structure()->field(i)->value);
2292 break;
2295 EXPECT_LT(i, form_structure()->field_count());
2298 // Test if autofill types of returned form structure are correct for billing
2299 // entries.
2300 TEST_F(AutofillDialogControllerTest, AutofillTypes) {
2301 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
2302 controller()->OnAccept();
2303 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
2304 controller()->ForceFinishSubmit();
2305 ASSERT_TRUE(form_structure());
2306 ASSERT_EQ(20U, form_structure()->field_count());
2307 EXPECT_EQ(EMAIL_ADDRESS,
2308 form_structure()->field(0)->Type().GetStorableType());
2309 EXPECT_EQ(CREDIT_CARD_NUMBER,
2310 form_structure()->field(2)->Type().GetStorableType());
2311 EXPECT_EQ(ADDRESS_HOME_STATE,
2312 form_structure()->field(9)->Type().GetStorableType());
2313 EXPECT_EQ(ADDRESS_BILLING, form_structure()->field(9)->Type().group());
2314 EXPECT_EQ(ADDRESS_HOME_STATE,
2315 form_structure()->field(16)->Type().GetStorableType());
2316 EXPECT_EQ(ADDRESS_HOME, form_structure()->field(16)->Type().group());
2319 TEST_F(AutofillDialogControllerTest, SaveDetailsInChrome) {
2320 SwitchToAutofill();
2321 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(4);
2323 AutofillProfile full_profile(test::GetVerifiedProfile());
2324 controller()->GetTestingManager()->AddTestingProfile(&full_profile);
2326 CreditCard card(test::GetVerifiedCreditCard());
2327 controller()->GetTestingManager()->AddTestingCreditCard(&card);
2328 EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome());
2330 controller()->MenuModelForSection(SECTION_BILLING)->ActivatedAt(0);
2331 EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome());
2333 controller()->MenuModelForSection(SECTION_BILLING)->ActivatedAt(1);
2334 EXPECT_TRUE(controller()->ShouldOfferToSaveInChrome());
2336 profile()->GetPrefs()->SetBoolean(prefs::kAutofillEnabled, false);
2337 EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome());
2339 profile()->GetPrefs()->SetBoolean(prefs::kAutofillEnabled, true);
2340 controller()->MenuModelForSection(SECTION_BILLING)->ActivatedAt(1);
2341 EXPECT_TRUE(controller()->ShouldOfferToSaveInChrome());
2343 profile()->ForceIncognito(true);
2344 EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome());
2347 TEST_F(AutofillDialogControllerTest, DisabledAutofill) {
2348 SwitchToAutofill();
2349 ASSERT_TRUE(profile()->GetPrefs()->GetBoolean(prefs::kAutofillEnabled));
2351 AutofillProfile verified_profile(test::GetVerifiedProfile());
2352 controller()->GetTestingManager()->AddTestingProfile(&verified_profile);
2354 CreditCard credit_card(test::GetVerifiedCreditCard());
2355 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
2357 // Verify suggestions menus should be showing when Autofill is enabled.
2358 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC));
2359 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_BILLING));
2360 EXPECT_EQ(
2361 4, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
2363 EXPECT_CALL(*controller()->GetView(), ModelChanged());
2364 profile()->GetPrefs()->SetBoolean(prefs::kAutofillEnabled, false);
2366 // Verify billing and credit card suggestions menus are hidden when Autofill
2367 // is disabled.
2368 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC));
2369 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_BILLING));
2370 // And that the shipping suggestions menu has less selections.
2371 EXPECT_EQ(
2372 2, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
2374 // Additionally, editing fields should not show Autofill popups.
2375 ASSERT_NO_FATAL_FAILURE(controller()->UserEditedOrActivatedInput(
2376 SECTION_BILLING,
2377 NAME_BILLING_FULL,
2378 gfx::NativeView(),
2379 gfx::Rect(),
2380 verified_profile.GetInfo(AutofillType(NAME_FULL), "en-US").substr(0, 1),
2381 true));
2382 EXPECT_EQ(UNKNOWN_TYPE, controller()->popup_input_type());
2385 // Tests that user is prompted when using instrument with minimal address.
2386 TEST_F(AutofillDialogControllerTest, UpgradeMinimalAddress) {
2387 // A minimal address being selected should trigger error validation in the
2388 // view. Called once for each incomplete suggestion.
2389 EXPECT_CALL(*controller()->GetView(), UpdateForErrors());
2391 scoped_ptr<wallet::WalletItems> wallet_items =
2392 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
2393 wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentWithIdAndAddress(
2394 "id", wallet::GetTestMinimalAddress()));
2395 scoped_ptr<wallet::Address> address(wallet::GetTestShippingAddress());
2396 address->set_is_complete_address(false);
2397 wallet_items->AddAddress(address.Pass());
2398 controller()->OnDidGetWalletItems(wallet_items.Pass());
2400 // Assert that dialog's SECTION_CC_BILLING section is in edit mode.
2401 ASSERT_TRUE(controller()->IsEditingExistingData(SECTION_CC_BILLING));
2402 // Shipping section should be in edit mode because of
2403 // is_minimal_shipping_address.
2404 ASSERT_TRUE(controller()->IsEditingExistingData(SECTION_SHIPPING));
2407 TEST_F(AutofillDialogControllerTest, RiskNeverLoadsWithPendingLegalDocuments) {
2408 EXPECT_CALL(*controller(), LoadRiskFingerprintData()).Times(0);
2410 scoped_ptr<wallet::WalletItems> wallet_items = CompleteAndValidWalletItems();
2411 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
2412 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
2413 controller()->OnDidGetWalletItems(wallet_items.Pass());
2414 controller()->OnAccept();
2417 TEST_F(AutofillDialogControllerTest, RiskLoadsAfterAcceptingLegalDocuments) {
2418 EXPECT_CALL(*controller(), LoadRiskFingerprintData()).Times(0);
2420 scoped_ptr<wallet::WalletItems> wallet_items = CompleteAndValidWalletItems();
2421 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
2422 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
2423 controller()->OnDidGetWalletItems(wallet_items.Pass());
2425 testing::Mock::VerifyAndClear(controller());
2426 EXPECT_CALL(*controller(), LoadRiskFingerprintData());
2428 controller()->OnAccept();
2430 // Simulate a risk load and verify |GetRiskData()| matches the encoded value.
2431 controller()->OnDidAcceptLegalDocuments();
2432 controller()->OnDidLoadRiskFingerprintData("a");
2433 EXPECT_EQ("a", controller()->GetRiskData());
2436 TEST_F(AutofillDialogControllerTest, NoManageMenuItemForNewWalletUsers) {
2437 // Make sure the menu model item is created for a returning Wallet user.
2438 scoped_ptr<wallet::WalletItems> wallet_items =
2439 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
2440 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
2441 wallet_items->AddAddress(wallet::GetTestShippingAddress());
2442 controller()->OnDidGetWalletItems(wallet_items.Pass());
2444 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING));
2445 // "Same as billing", "123 address", "Add address...", and "Manage addresses".
2446 EXPECT_EQ(
2447 4, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
2449 // Make sure the menu model item is not created for new Wallet users.
2450 base::DictionaryValue dict;
2451 scoped_ptr<base::ListValue> required_actions(new base::ListValue);
2452 required_actions->AppendString("setup_wallet");
2453 dict.Set("required_action", required_actions.release());
2454 controller()->OnDidGetWalletItems(
2455 wallet::WalletItems::CreateWalletItems(dict).Pass());
2457 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC_BILLING));
2458 // "Same as billing" and "Add address...".
2459 EXPECT_EQ(
2460 2, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
2463 TEST_F(AutofillDialogControllerTest, ShippingSectionCanBeHidden) {
2464 FormFieldData email_field;
2465 email_field.autocomplete_attribute = "email";
2466 FormFieldData cc_field;
2467 cc_field.autocomplete_attribute = "cc-number";
2468 FormFieldData billing_field;
2469 billing_field.autocomplete_attribute = "billing address-level1";
2471 FormData form_data;
2472 form_data.fields.push_back(email_field);
2473 form_data.fields.push_back(cc_field);
2474 form_data.fields.push_back(billing_field);
2476 AutofillProfile full_profile(test::GetVerifiedProfile());
2477 controller()->GetTestingManager()->AddTestingProfile(&full_profile);
2478 SetUpControllerWithFormData(form_data);
2480 SwitchToAutofill();
2482 EXPECT_FALSE(controller()->SectionIsActive(SECTION_SHIPPING));
2484 FillCreditCardInputs();
2485 controller()->OnAccept();
2486 EXPECT_TRUE(form_structure());
2489 TEST_F(AutofillDialogControllerTest, ShippingSectionCanBeHiddenForWallet) {
2490 FormFieldData email_field;
2491 email_field.autocomplete_attribute = "email";
2492 FormFieldData cc_field;
2493 cc_field.autocomplete_attribute = "cc-number";
2494 FormFieldData billing_field;
2495 billing_field.autocomplete_attribute = "billing address-level1";
2497 FormData form_data;
2498 form_data.fields.push_back(email_field);
2499 form_data.fields.push_back(cc_field);
2500 form_data.fields.push_back(billing_field);
2502 SetUpControllerWithFormData(form_data);
2503 EXPECT_FALSE(controller()->SectionIsActive(SECTION_SHIPPING));
2504 EXPECT_FALSE(controller()->IsShippingAddressRequired());
2506 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_));
2507 scoped_ptr<wallet::WalletItems> wallet_items =
2508 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
2509 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
2510 SubmitWithWalletItems(wallet_items.Pass());
2511 controller()->OnDidGetFullWallet(wallet::GetTestFullWalletInstrumentOnly());
2512 controller()->ForceFinishSubmit();
2513 EXPECT_TRUE(form_structure());
2516 TEST_F(AutofillDialogControllerTest, NotProdNotification) {
2517 // To make IsPayingWithWallet() true.
2518 controller()->OnDidGetWalletItems(
2519 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED));
2521 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
2522 ASSERT_EQ(
2524 command_line->GetSwitchValueASCII(switches::kWalletServiceUseSandbox));
2526 command_line->AppendSwitchASCII(switches::kWalletServiceUseSandbox, "1");
2527 EXPECT_EQ(1U,
2528 NotificationsOfType(DialogNotification::DEVELOPER_WARNING).size());
2531 TEST_F(AutofillDialogControllerTest, NoNotProdNotification) {
2532 // To make IsPayingWithWallet() true.
2533 controller()->OnDidGetWalletItems(
2534 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED));
2536 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
2537 ASSERT_EQ(
2539 command_line->GetSwitchValueASCII(switches::kWalletServiceUseSandbox));
2541 command_line->AppendSwitchASCII(switches::kWalletServiceUseSandbox, "0");
2542 EXPECT_EQ(0U,
2543 NotificationsOfType(DialogNotification::DEVELOPER_WARNING).size());
2546 // Ensure Wallet instruments marked expired by the server are shown as invalid.
2547 TEST_F(AutofillDialogControllerTest, WalletExpiredCard) {
2548 scoped_ptr<wallet::WalletItems> wallet_items =
2549 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
2550 wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentExpired());
2551 controller()->OnDidGetWalletItems(wallet_items.Pass());
2553 EXPECT_TRUE(controller()->IsEditingExistingData(SECTION_CC_BILLING));
2555 const DetailInputs& inputs =
2556 controller()->RequestedFieldsForSection(SECTION_CC_BILLING);
2557 FieldValueMap outputs;
2558 CopyInitialValues(inputs, &outputs);
2560 // The local inputs are invalid because the server said so. They'll
2561 // stay invalid until they differ from the remotely fetched model.
2562 ValidityMessages messages = controller()->InputsAreValid(SECTION_CC_BILLING,
2563 outputs);
2564 EXPECT_TRUE(messages.HasSureError(CREDIT_CARD_EXP_MONTH));
2565 EXPECT_TRUE(messages.HasSureError(CREDIT_CARD_EXP_4_DIGIT_YEAR));
2567 // Make the local input year differ from the instrument.
2568 CopyInitialValues(inputs, &outputs);
2569 outputs[CREDIT_CARD_EXP_4_DIGIT_YEAR] = ASCIIToUTF16("3002");
2570 messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
2571 EXPECT_FALSE(HasAnyError(messages, CREDIT_CARD_EXP_MONTH));
2572 EXPECT_FALSE(HasAnyError(messages, CREDIT_CARD_EXP_4_DIGIT_YEAR));
2574 // Make the local input month differ from the instrument.
2575 CopyInitialValues(inputs, &outputs);
2576 outputs[CREDIT_CARD_EXP_MONTH] = ASCIIToUTF16("06");
2577 messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
2578 EXPECT_FALSE(HasAnyError(messages, CREDIT_CARD_EXP_MONTH));
2579 EXPECT_FALSE(HasAnyError(messages, CREDIT_CARD_EXP_4_DIGIT_YEAR));
2582 TEST_F(AutofillDialogControllerTest, ChooseAnotherInstrumentOrAddress) {
2583 SubmitWithWalletItems(CompleteAndValidWalletItems());
2585 EXPECT_EQ(0U, NotificationsOfType(
2586 DialogNotification::REQUIRED_ACTION).size());
2587 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems(_, _));
2588 controller()->OnDidGetFullWallet(
2589 wallet::GetTestFullWalletWithRequiredActions(
2590 std::vector<wallet::RequiredAction>(
2591 1, wallet::CHOOSE_ANOTHER_INSTRUMENT_OR_ADDRESS)));
2592 EXPECT_EQ(1U, NotificationsOfType(
2593 DialogNotification::REQUIRED_ACTION).size());
2594 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
2596 controller()->OnAccept();
2597 EXPECT_EQ(0U, NotificationsOfType(
2598 DialogNotification::REQUIRED_ACTION).size());
2601 TEST_F(AutofillDialogControllerTest, NewCardBubbleShown) {
2602 SwitchToAutofill();
2603 FillCreditCardInputs();
2604 controller()->OnAccept();
2605 controller()->ViewClosed();
2607 EXPECT_EQ(1, mock_new_card_bubble_controller()->bubbles_shown());
2608 EXPECT_EQ(0, test_generated_bubble_controller()->bubbles_shown());
2611 TEST_F(AutofillDialogControllerTest, GeneratedCardBubbleShown) {
2612 SubmitWithWalletItems(CompleteAndValidWalletItems());
2613 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
2614 controller()->ForceFinishSubmit();
2615 controller()->ViewClosed();
2617 EXPECT_EQ(0, mock_new_card_bubble_controller()->bubbles_shown());
2618 EXPECT_EQ(1, test_generated_bubble_controller()->bubbles_shown());
2621 // Verify that new Wallet data is fetched when the user switches away from the
2622 // tab hosting the Autofill dialog and back. Also verify that the user's
2623 // selection is preserved across this re-fetch.
2624 TEST_F(AutofillDialogControllerTest, ReloadWalletItemsOnActivation) {
2625 // Initialize some Wallet data.
2626 scoped_ptr<wallet::WalletItems> wallet_items =
2627 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
2628 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
2629 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
2630 wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
2631 wallet_items->AddAddress(wallet::GetTestShippingAddress());
2632 controller()->OnDidGetWalletItems(wallet_items.Pass());
2634 // Initially, the default entries should be selected.
2635 ui::MenuModel* cc_billing_model =
2636 controller()->MenuModelForSection(SECTION_CC_BILLING);
2637 ui::MenuModel* shipping_model =
2638 controller()->MenuModelForSection(SECTION_SHIPPING);
2639 // "add", "manage", and 2 suggestions.
2640 ASSERT_EQ(4, cc_billing_model->GetItemCount());
2641 EXPECT_TRUE(cc_billing_model->IsItemCheckedAt(0));
2642 // "use billing", "add", "manage", and 2 suggestions.
2643 ASSERT_EQ(5, shipping_model->GetItemCount());
2644 EXPECT_TRUE(shipping_model->IsItemCheckedAt(2));
2646 // Select entries other than the defaults.
2647 cc_billing_model->ActivatedAt(1);
2648 shipping_model->ActivatedAt(1);
2649 // 2 suggestions, "add", and "manage".
2650 ASSERT_EQ(4, cc_billing_model->GetItemCount());
2651 EXPECT_TRUE(cc_billing_model->IsItemCheckedAt(1));
2652 // "use billing", 2 suggestions, "add", "manage".
2653 ASSERT_EQ(5, shipping_model->GetItemCount());
2654 EXPECT_TRUE(shipping_model-> IsItemCheckedAt(1));
2656 // Simulate switching away from the tab and back. This should issue a request
2657 // for wallet items.
2658 controller()->ClearLastWalletItemsFetchTimestampForTesting();
2659 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems(_, _));
2660 controller()->TabActivated();
2662 // Simulate a response that includes different items.
2663 wallet_items = wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
2664 wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentExpired());
2665 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
2666 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
2667 wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
2668 controller()->OnDidGetWalletItems(wallet_items.Pass());
2670 // The previously selected entries should still be selected.
2671 // 3 suggestions, "add", and "manage".
2672 ASSERT_EQ(5, cc_billing_model->GetItemCount());
2673 EXPECT_TRUE(cc_billing_model->IsItemCheckedAt(2));
2674 // "use billing", 1 suggestion, "add", and "manage".
2675 ASSERT_EQ(4, shipping_model->GetItemCount());
2676 EXPECT_TRUE(shipping_model->IsItemCheckedAt(1));
2679 // Verify that if the default values change when re-fetching Wallet data, these
2680 // new default values are selected in the dialog.
2681 TEST_F(AutofillDialogControllerTest,
2682 ReloadWalletItemsOnActivationWithNewDefaults) {
2683 // Initialize some Wallet data.
2684 scoped_ptr<wallet::WalletItems> wallet_items =
2685 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
2686 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
2687 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
2688 wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
2689 wallet_items->AddAddress(wallet::GetTestShippingAddress());
2690 controller()->OnDidGetWalletItems(wallet_items.Pass());
2692 // Initially, the default entries should be selected.
2693 ui::MenuModel* cc_billing_model =
2694 controller()->MenuModelForSection(SECTION_CC_BILLING);
2695 ui::MenuModel* shipping_model =
2696 controller()->MenuModelForSection(SECTION_SHIPPING);
2697 // 2 suggestions, "add", and "manage".
2698 ASSERT_EQ(4, cc_billing_model->GetItemCount());
2699 EXPECT_TRUE(cc_billing_model->IsItemCheckedAt(0));
2700 // "use billing", 2 suggestions, "add", and "manage".
2701 ASSERT_EQ(5, shipping_model->GetItemCount());
2702 EXPECT_TRUE(shipping_model->IsItemCheckedAt(2));
2704 // Simulate switching away from the tab and back. This should issue a request
2705 // for wallet items.
2706 controller()->ClearLastWalletItemsFetchTimestampForTesting();
2707 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems(_, _));
2708 controller()->TabActivated();
2710 // Simulate a response that includes different default values.
2711 wallet_items =
2712 wallet::GetTestWalletItemsWithDefaultIds("new_default_instrument_id",
2713 "new_default_address_id",
2714 wallet::AMEX_DISALLOWED);
2715 scoped_ptr<wallet::Address> other_address = wallet::GetTestShippingAddress();
2716 other_address->set_object_id("other_address_id");
2717 scoped_ptr<wallet::Address> new_default_address =
2718 wallet::GetTestNonDefaultShippingAddress();
2719 new_default_address->set_object_id("new_default_address_id");
2721 wallet_items->AddInstrument(
2722 wallet::GetTestMaskedInstrumentWithId("other_instrument_id"));
2723 wallet_items->AddInstrument(
2724 wallet::GetTestMaskedInstrumentWithId("new_default_instrument_id"));
2725 wallet_items->AddAddress(new_default_address.Pass());
2726 wallet_items->AddAddress(other_address.Pass());
2727 controller()->OnDidGetWalletItems(wallet_items.Pass());
2729 // The new default entries should be selected.
2730 // 2 suggestions, "add", and "manage".
2731 ASSERT_EQ(4, cc_billing_model->GetItemCount());
2732 EXPECT_TRUE(cc_billing_model->IsItemCheckedAt(1));
2733 // "use billing", 2 suggestions, "add", and "manage".
2734 ASSERT_EQ(5, shipping_model->GetItemCount());
2735 EXPECT_TRUE(shipping_model->IsItemCheckedAt(1));
2738 TEST_F(AutofillDialogControllerTest, ReloadWithEmptyWalletItems) {
2739 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
2740 controller()->MenuModelForSection(SECTION_CC_BILLING)->ActivatedAt(1);
2741 controller()->MenuModelForSection(SECTION_SHIPPING)->ActivatedAt(1);
2743 controller()->ClearLastWalletItemsFetchTimestampForTesting();
2744 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems(_, _));
2745 controller()->TabActivated();
2747 controller()->OnDidGetWalletItems(
2748 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED));
2750 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC_BILLING));
2751 EXPECT_EQ(
2752 3, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
2755 TEST_F(AutofillDialogControllerTest, SaveInChromeByDefault) {
2756 EXPECT_TRUE(controller()->ShouldSaveInChrome());
2757 SwitchToAutofill();
2758 FillCreditCardInputs();
2759 controller()->OnAccept();
2760 EXPECT_TRUE(controller()->ShouldSaveInChrome());
2763 TEST_F(AutofillDialogControllerTest,
2764 SaveInChromePreferenceNotRememberedOnCancel) {
2765 EXPECT_TRUE(controller()->ShouldSaveInChrome());
2766 SwitchToAutofill();
2767 controller()->GetView()->CheckSaveDetailsLocallyCheckbox(false);
2768 controller()->OnCancel();
2769 EXPECT_TRUE(controller()->ShouldSaveInChrome());
2772 TEST_F(AutofillDialogControllerTest,
2773 SaveInChromePreferenceRememberedOnSuccess) {
2774 EXPECT_TRUE(controller()->ShouldSaveInChrome());
2775 SwitchToAutofill();
2776 FillCreditCardInputs();
2777 controller()->GetView()->CheckSaveDetailsLocallyCheckbox(false);
2778 controller()->OnAccept();
2779 EXPECT_FALSE(controller()->ShouldSaveInChrome());
2782 TEST_F(AutofillDialogControllerTest,
2783 SubmitButtonIsDisabled_SpinnerFinishesBeforeDelay) {
2784 // Reset Wallet state.
2785 controller()->OnDidGetWalletItems(scoped_ptr<wallet::WalletItems>());
2787 EXPECT_EQ(1, controller()->get_submit_button_delay_count());
2789 // Begin the submit button delay.
2790 controller()->SimulateSubmitButtonDelayBegin();
2792 EXPECT_TRUE(controller()->ShouldShowSpinner());
2793 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2795 // Stop the spinner.
2796 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
2798 EXPECT_FALSE(controller()->ShouldShowSpinner());
2799 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2801 // End the submit button delay.
2802 controller()->SimulateSubmitButtonDelayEnd();
2804 EXPECT_FALSE(controller()->ShouldShowSpinner());
2805 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2808 TEST_F(AutofillDialogControllerTest,
2809 SubmitButtonIsDisabled_SpinnerFinishesAfterDelay) {
2810 // Reset Wallet state.
2811 controller()->OnDidGetWalletItems(scoped_ptr<wallet::WalletItems>());
2813 EXPECT_EQ(1, controller()->get_submit_button_delay_count());
2815 // Begin the submit button delay.
2816 controller()->SimulateSubmitButtonDelayBegin();
2818 EXPECT_TRUE(controller()->ShouldShowSpinner());
2819 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2821 // End the submit button delay.
2822 controller()->SimulateSubmitButtonDelayEnd();
2824 EXPECT_TRUE(controller()->ShouldShowSpinner());
2825 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2827 // Stop the spinner.
2828 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
2830 EXPECT_FALSE(controller()->ShouldShowSpinner());
2831 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2834 TEST_F(AutofillDialogControllerTest, SubmitButtonIsDisabled_NoSpinner) {
2835 SwitchToAutofill();
2837 EXPECT_EQ(1, controller()->get_submit_button_delay_count());
2839 // Begin the submit button delay.
2840 controller()->SimulateSubmitButtonDelayBegin();
2842 EXPECT_FALSE(controller()->ShouldShowSpinner());
2843 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2845 // End the submit button delay.
2846 controller()->SimulateSubmitButtonDelayEnd();
2848 EXPECT_FALSE(controller()->ShouldShowSpinner());
2849 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2852 TEST_F(AutofillDialogControllerTest, IconsForFields_NoCreditCard) {
2853 FieldValueMap values;
2854 values[EMAIL_ADDRESS] = ASCIIToUTF16(kFakeEmail);
2855 FieldIconMap icons = controller()->IconsForFields(values);
2856 EXPECT_TRUE(icons.empty());
2859 TEST_F(AutofillDialogControllerTest, IconsForFields_CreditCardNumberOnly) {
2860 FieldValueMap values;
2861 values[EMAIL_ADDRESS] = ASCIIToUTF16(kFakeEmail);
2862 values[CREDIT_CARD_NUMBER] = ASCIIToUTF16(kTestCCNumberVisa);
2863 FieldIconMap icons = controller()->IconsForFields(values);
2864 EXPECT_EQ(1UL, icons.size());
2865 EXPECT_EQ(1UL, icons.count(CREDIT_CARD_NUMBER));
2868 TEST_F(AutofillDialogControllerTest, IconsForFields_CvcOnly) {
2869 FieldValueMap values;
2870 values[EMAIL_ADDRESS] = ASCIIToUTF16(kFakeEmail);
2871 values[CREDIT_CARD_VERIFICATION_CODE] = ASCIIToUTF16("123");
2872 FieldIconMap icons = controller()->IconsForFields(values);
2873 EXPECT_EQ(1UL, icons.size());
2874 EXPECT_EQ(1UL, icons.count(CREDIT_CARD_VERIFICATION_CODE));
2877 TEST_F(AutofillDialogControllerTest, IconsForFields_BothCreditCardAndCvc) {
2878 FieldValueMap values;
2879 values[EMAIL_ADDRESS] = ASCIIToUTF16(kFakeEmail);
2880 values[CREDIT_CARD_NUMBER] = ASCIIToUTF16(kTestCCNumberVisa);
2881 values[CREDIT_CARD_VERIFICATION_CODE] = ASCIIToUTF16("123");
2882 FieldIconMap icons = controller()->IconsForFields(values);
2883 EXPECT_EQ(2UL, icons.size());
2884 EXPECT_EQ(1UL, icons.count(CREDIT_CARD_VERIFICATION_CODE));
2885 EXPECT_EQ(1UL, icons.count(CREDIT_CARD_NUMBER));
2888 TEST_F(AutofillDialogControllerTest, FieldControlsIcons) {
2889 EXPECT_TRUE(controller()->FieldControlsIcons(CREDIT_CARD_NUMBER));
2890 EXPECT_FALSE(controller()->FieldControlsIcons(CREDIT_CARD_VERIFICATION_CODE));
2891 EXPECT_FALSE(controller()->FieldControlsIcons(EMAIL_ADDRESS));
2894 TEST_F(AutofillDialogControllerTest, SaveCreditCardIncludesName_NoBilling) {
2895 SwitchToAutofill();
2897 CreditCard test_credit_card(test::GetVerifiedCreditCard());
2898 FillInputs(SECTION_CC, test_credit_card);
2900 AutofillProfile test_profile(test::GetVerifiedProfile());
2901 FillInputs(SECTION_BILLING, test_profile);
2903 UseBillingForShipping();
2905 controller()->GetView()->CheckSaveDetailsLocallyCheckbox(true);
2906 controller()->OnAccept();
2908 TestPersonalDataManager* test_pdm = controller()->GetTestingManager();
2909 const CreditCard& imported_card = test_pdm->imported_credit_card();
2910 EXPECT_EQ(test_profile.GetInfo(AutofillType(NAME_FULL), "en-US"),
2911 imported_card.GetRawInfo(CREDIT_CARD_NAME));
2914 TEST_F(AutofillDialogControllerTest, SaveCreditCardIncludesName_WithBilling) {
2915 SwitchToAutofill();
2917 TestPersonalDataManager* test_pdm = controller()->GetTestingManager();
2918 AutofillProfile test_profile(test::GetVerifiedProfile());
2920 EXPECT_CALL(*controller()->GetView(), ModelChanged());
2921 test_pdm->AddTestingProfile(&test_profile);
2922 ASSERT_TRUE(controller()->MenuModelForSection(SECTION_BILLING));
2924 CreditCard test_credit_card(test::GetVerifiedCreditCard());
2925 FillInputs(SECTION_CC, test_credit_card);
2927 controller()->GetView()->CheckSaveDetailsLocallyCheckbox(true);
2928 controller()->OnAccept();
2930 const CreditCard& imported_card = test_pdm->imported_credit_card();
2931 EXPECT_EQ(test_profile.GetInfo(AutofillType(NAME_FULL), "en-US"),
2932 imported_card.GetRawInfo(CREDIT_CARD_NAME));
2934 controller()->ViewClosed();
2937 TEST_F(AutofillDialogControllerTest, InputEditability) {
2938 // Empty wallet items: all fields are editable.
2939 scoped_ptr<wallet::WalletItems> items =
2940 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
2941 controller()->OnDidGetWalletItems(items.Pass());
2943 DialogSection sections[] = { SECTION_CC_BILLING, SECTION_SHIPPING };
2944 for (size_t i = 0; i < arraysize(sections); ++i) {
2945 const DetailInputs& inputs =
2946 controller()->RequestedFieldsForSection(sections[i]);
2947 for (size_t j = 0; j < inputs.size(); ++j) {
2948 EXPECT_TRUE(controller()->InputIsEditable(inputs[j], sections[i]));
2952 // Expired instrument: CC number + CVV are not editable.
2953 items = wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
2954 scoped_ptr<wallet::WalletItems::MaskedInstrument> expired_instrument =
2955 wallet::GetTestMaskedInstrumentExpired();
2956 items->AddInstrument(expired_instrument.Pass());
2957 controller()->OnDidGetWalletItems(items.Pass());
2958 EXPECT_TRUE(controller()->IsEditingExistingData(SECTION_CC_BILLING));
2960 FieldValueMap outputs;
2961 CopyInitialValues(
2962 controller()->RequestedFieldsForSection(SECTION_CC_BILLING),
2963 &outputs);
2964 controller()->GetView()->SetUserInput(SECTION_CC_BILLING, outputs);
2966 for (size_t i = 0; i < arraysize(sections); ++i) {
2967 const DetailInputs& inputs =
2968 controller()->RequestedFieldsForSection(sections[i]);
2969 for (size_t j = 0; j < inputs.size(); ++j) {
2970 if (inputs[j].type == CREDIT_CARD_NUMBER ||
2971 inputs[j].type == CREDIT_CARD_VERIFICATION_CODE) {
2972 EXPECT_FALSE(controller()->InputIsEditable(inputs[j], sections[i]));
2973 } else {
2974 EXPECT_TRUE(controller()->InputIsEditable(inputs[j], sections[i]));
2979 // User changes the billing address; same story.
2980 outputs[ADDRESS_BILLING_ZIP] = ASCIIToUTF16("77025");
2981 controller()->GetView()->SetUserInput(SECTION_CC_BILLING, outputs);
2982 for (size_t i = 0; i < arraysize(sections); ++i) {
2983 const DetailInputs& inputs =
2984 controller()->RequestedFieldsForSection(sections[i]);
2985 for (size_t j = 0; j < inputs.size(); ++j) {
2986 if (inputs[j].type == CREDIT_CARD_NUMBER ||
2987 inputs[j].type == CREDIT_CARD_VERIFICATION_CODE) {
2988 EXPECT_FALSE(controller()->InputIsEditable(inputs[j], sections[i]));
2989 } else {
2990 EXPECT_TRUE(controller()->InputIsEditable(inputs[j], sections[i]));
2995 // User changes a detail of the CC itself (expiration date), CVV is now
2996 // editable (and mandatory).
2997 outputs[CREDIT_CARD_EXP_MONTH] = ASCIIToUTF16("06");
2998 controller()->GetView()->SetUserInput(SECTION_CC_BILLING, outputs);
2999 for (size_t i = 0; i < arraysize(sections); ++i) {
3000 const DetailInputs& inputs =
3001 controller()->RequestedFieldsForSection(sections[i]);
3002 for (size_t j = 0; j < inputs.size(); ++j) {
3003 if (inputs[j].type == CREDIT_CARD_NUMBER)
3004 EXPECT_FALSE(controller()->InputIsEditable(inputs[j], sections[i]));
3005 else
3006 EXPECT_TRUE(controller()->InputIsEditable(inputs[j], sections[i]));
3011 // When the default country is something besides US, wallet is not selected
3012 // and the account chooser shouldn't be visible.
3013 TEST_F(AutofillDialogControllerTest, HideWalletInOtherCountries) {
3014 // Addresses from different countries.
3015 AutofillProfile us_profile(base::GenerateGUID(), kSettingsOrigin),
3016 es_profile(base::GenerateGUID(), kSettingsOrigin),
3017 es_profile2(base::GenerateGUID(), kSettingsOrigin);
3018 us_profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
3019 es_profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("ES"));
3020 es_profile2.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("ES"));
3022 // If US is indicated (via timezone), show Wallet.
3023 ResetControllerWithFormData(DefaultFormData());
3024 controller()->GetTestingManager()->set_timezone_country_code("US");
3025 controller()->Show();
3026 EXPECT_TRUE(
3027 controller()->AccountChooserModelForTesting()->WalletIsSelected());
3028 controller()->OnDidFetchWalletCookieValue(std::string());
3029 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
3030 EXPECT_TRUE(controller()->ShouldShowAccountChooser());
3031 EXPECT_TRUE(
3032 controller()->AccountChooserModelForTesting()->WalletIsSelected());
3034 // If US is not indicated, don't show Wallet.
3035 ResetControllerWithFormData(DefaultFormData());
3036 controller()->GetTestingManager()->set_timezone_country_code("ES");
3037 controller()->Show();
3038 controller()->OnDidFetchWalletCookieValue(std::string());
3039 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
3040 EXPECT_FALSE(controller()->ShouldShowAccountChooser());
3042 // If US is indicated (via a profile), show Wallet.
3043 ResetControllerWithFormData(DefaultFormData());
3044 controller()->GetTestingManager()->set_timezone_country_code("ES");
3045 controller()->GetTestingManager()->AddTestingProfile(&us_profile);
3046 controller()->Show();
3047 controller()->OnDidFetchWalletCookieValue(std::string());
3048 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
3049 EXPECT_TRUE(controller()->ShouldShowAccountChooser());
3050 EXPECT_TRUE(
3051 controller()->AccountChooserModelForTesting()->WalletIsSelected());
3053 // Make sure the profile doesn't just override the timezone.
3054 ResetControllerWithFormData(DefaultFormData());
3055 controller()->GetTestingManager()->set_timezone_country_code("US");
3056 controller()->GetTestingManager()->AddTestingProfile(&es_profile);
3057 controller()->Show();
3058 controller()->OnDidFetchWalletCookieValue(std::string());
3059 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
3060 EXPECT_TRUE(controller()->ShouldShowAccountChooser());
3061 EXPECT_TRUE(
3062 controller()->AccountChooserModelForTesting()->WalletIsSelected());
3064 // Only takes one US address to enable Wallet.
3065 ResetControllerWithFormData(DefaultFormData());
3066 controller()->GetTestingManager()->set_timezone_country_code("FR");
3067 controller()->GetTestingManager()->AddTestingProfile(&es_profile);
3068 controller()->GetTestingManager()->AddTestingProfile(&es_profile2);
3069 controller()->GetTestingManager()->AddTestingProfile(&us_profile);
3070 controller()->Show();
3071 controller()->OnDidFetchWalletCookieValue(std::string());
3072 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
3073 EXPECT_TRUE(controller()->ShouldShowAccountChooser());
3074 EXPECT_TRUE(
3075 controller()->AccountChooserModelForTesting()->WalletIsSelected());
3078 TEST_F(AutofillDialogControllerTest, DontGetWalletTillNecessary) {
3079 // When starting on local data mode, the dialog will provide a "Use Google
3080 // Wallet" link.
3081 profile()->GetPrefs()->SetBoolean(
3082 ::prefs::kAutofillDialogPayWithoutWallet, true);
3083 ResetControllerWithFormData(DefaultFormData());
3084 controller()->Show();
3085 base::string16 use_wallet_text = controller()->SignInLinkText();
3086 EXPECT_EQ(TestAutofillDialogController::NOT_CHECKED,
3087 controller()->SignedInState());
3089 // When clicked, this link will ask for wallet items. If there's a signin
3090 // failure, the link will switch to "Sign in to use Google Wallet".
3091 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems(_, _));
3092 controller()->SignInLinkClicked();
3093 EXPECT_NE(TestAutofillDialogController::NOT_CHECKED,
3094 controller()->SignedInState());
3095 controller()->OnDidFetchWalletCookieValue(std::string());
3096 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
3097 controller()->OnPassiveSigninFailure(GoogleServiceAuthError(
3098 GoogleServiceAuthError::CONNECTION_FAILED));
3099 EXPECT_NE(use_wallet_text, controller()->SignInLinkText());
3102 TEST_F(AutofillDialogControllerTest, MultiAccountSwitch) {
3103 std::vector<std::string> users;
3104 users.push_back("user_1@example.com");
3105 users.push_back("user_2@example.com");
3106 controller()->OnDidGetWalletItems(
3107 wallet::GetTestWalletItemsWithUsers(users, 0));
3109 // Items should be: Account 1, account 2, add account, disable wallet.
3110 EXPECT_EQ(4, controller()->MenuModelForAccountChooser()->GetItemCount());
3111 EXPECT_EQ(0U, controller()->GetTestingWalletClient()->user_index());
3113 // GetWalletItems should be called when the user switches accounts.
3114 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems(_, _));
3115 controller()->MenuModelForAccountChooser()->ActivatedAt(1);
3116 // The wallet client should be updated to the new user index.
3117 EXPECT_EQ(1U, controller()->GetTestingWalletClient()->user_index());
3120 TEST_F(AutofillDialogControllerTest, PassiveAuthFailure) {
3121 controller()->OnDidGetWalletItems(
3122 wallet::GetTestWalletItemsWithRequiredAction(
3123 wallet::PASSIVE_GAIA_AUTH));
3124 EXPECT_TRUE(controller()->ShouldShowSpinner());
3125 controller()->OnPassiveSigninFailure(GoogleServiceAuthError(
3126 GoogleServiceAuthError::NONE));
3127 EXPECT_FALSE(controller()->ShouldShowSpinner());
3130 TEST_F(AutofillDialogControllerTest, WalletShippingSameAsBilling) {
3131 // Assert initial state.
3132 ASSERT_FALSE(profile()->GetPrefs()->HasPrefPath(
3133 ::prefs::kAutofillDialogWalletShippingSameAsBilling));
3135 // Verify that false pref defaults to wallet defaults.
3136 scoped_ptr<wallet::WalletItems> wallet_items =
3137 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
3138 wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
3139 wallet_items->AddAddress(wallet::GetTestShippingAddress());
3140 controller()->OnDidGetWalletItems(wallet_items.Pass());
3141 ASSERT_FALSE(profile()->GetPrefs()->GetBoolean(
3142 ::prefs::kAutofillDialogWalletShippingSameAsBilling));
3143 EXPECT_EQ(2, GetMenuModelForSection(SECTION_SHIPPING)->checked_item());
3145 // Set "Same as Billing" for the shipping address and verify it sets the pref
3146 // and selects the appropriate menu item.
3147 UseBillingForShipping();
3148 ASSERT_EQ(0, GetMenuModelForSection(SECTION_SHIPPING)->checked_item());
3149 controller()->ForceFinishSubmit();
3150 ASSERT_TRUE(profile()->GetPrefs()->GetBoolean(
3151 ::prefs::kAutofillDialogWalletShippingSameAsBilling));
3153 // Getting new wallet info shouldn't disrupt the preference and menu should be
3154 // set accordingly.
3155 Reset();
3156 wallet_items = wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
3157 wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
3158 wallet_items->AddAddress(wallet::GetTestShippingAddress());
3159 controller()->OnDidGetWalletItems(wallet_items.Pass());
3160 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(
3161 ::prefs::kAutofillDialogWalletShippingSameAsBilling));
3162 EXPECT_EQ(0, GetMenuModelForSection(SECTION_SHIPPING)->checked_item());
3164 // Choose a different address and ensure pref gets set to false.
3165 controller()->MenuModelForSection(SECTION_SHIPPING)->ActivatedAt(1);
3166 controller()->ForceFinishSubmit();
3167 EXPECT_FALSE(profile()->GetPrefs()->GetBoolean(
3168 ::prefs::kAutofillDialogWalletShippingSameAsBilling));
3171 // Verifies that a call to the IconsForFields() method before the card type is
3172 // known returns a placeholder image that is at least as large as the icons for
3173 // all of the supported major credit card issuers.
3174 TEST_F(AutofillDialogControllerTest, IconReservedForCreditCardField) {
3175 FieldValueMap inputs;
3176 inputs[CREDIT_CARD_NUMBER] = base::string16();
3178 FieldIconMap icons = controller()->IconsForFields(inputs);
3179 EXPECT_EQ(1U, icons.size());
3181 ASSERT_EQ(1U, icons.count(CREDIT_CARD_NUMBER));
3182 gfx::Image placeholder_icon = icons[CREDIT_CARD_NUMBER];
3184 // Verify that the placeholder icon is at least as large as the icons for the
3185 // supported credit card issuers.
3186 const int kSupportedCardIdrs[] = {
3187 IDR_AUTOFILL_CC_AMEX,
3188 IDR_AUTOFILL_CC_DISCOVER,
3189 IDR_AUTOFILL_CC_GENERIC,
3190 IDR_AUTOFILL_CC_MASTERCARD,
3191 IDR_AUTOFILL_CC_VISA,
3193 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
3194 for (size_t i = 0; i < arraysize(kSupportedCardIdrs); ++i) {
3195 SCOPED_TRACE(base::IntToString(i));
3196 gfx::Image supported_card_icon = rb.GetImageNamed(kSupportedCardIdrs[i]);
3197 EXPECT_GE(placeholder_icon.Width(), supported_card_icon.Width());
3198 EXPECT_GE(placeholder_icon.Height(), supported_card_icon.Height());
3202 TEST_F(AutofillDialogControllerTest, CountryChangeUpdatesSection) {
3203 TestAutofillDialogView* view = controller()->GetView();
3204 view->ClearSectionUpdates();
3206 controller()->UserEditedOrActivatedInput(SECTION_SHIPPING,
3207 ADDRESS_HOME_COUNTRY,
3208 gfx::NativeView(),
3209 gfx::Rect(),
3210 ASCIIToUTF16("Belarus"),
3211 true);
3212 std::map<DialogSection, size_t> updates = view->section_updates();
3213 EXPECT_EQ(1U, updates[SECTION_SHIPPING]);
3214 EXPECT_EQ(1U, updates.size());
3216 view->ClearSectionUpdates();
3218 controller()->UserEditedOrActivatedInput(SECTION_CC_BILLING,
3219 ADDRESS_BILLING_COUNTRY,
3220 gfx::NativeView(),
3221 gfx::Rect(),
3222 ASCIIToUTF16("France"),
3223 true);
3224 updates = view->section_updates();
3225 EXPECT_EQ(1U, updates[SECTION_CC_BILLING]);
3226 EXPECT_EQ(1U, updates.size());
3228 SwitchToAutofill();
3229 view->ClearSectionUpdates();
3231 controller()->UserEditedOrActivatedInput(SECTION_BILLING,
3232 ADDRESS_BILLING_COUNTRY,
3233 gfx::NativeView(),
3234 gfx::Rect(),
3235 ASCIIToUTF16("Italy"),
3236 true);
3237 updates = view->section_updates();
3238 EXPECT_EQ(1U, updates[SECTION_BILLING]);
3239 EXPECT_EQ(1U, updates.size());
3242 TEST_F(AutofillDialogControllerTest, CorrectCountryFromInputs) {
3243 EXPECT_CALL(*controller()->GetMockValidator(),
3244 ValidateAddress(CountryCodeMatcher("DE"), _, _));
3246 FieldValueMap billing_inputs;
3247 billing_inputs[ADDRESS_BILLING_COUNTRY] = ASCIIToUTF16("Germany");
3248 controller()->InputsAreValid(SECTION_BILLING, billing_inputs);
3250 EXPECT_CALL(*controller()->GetMockValidator(),
3251 ValidateAddress(CountryCodeMatcher("FR"), _, _));
3253 FieldValueMap shipping_inputs;
3254 shipping_inputs[ADDRESS_HOME_COUNTRY] = ASCIIToUTF16("France");
3255 controller()->InputsAreValid(SECTION_SHIPPING, shipping_inputs);
3258 TEST_F(AutofillDialogControllerTest, ValidationRulesLoadedOnCountryChange) {
3259 ResetControllerWithFormData(DefaultFormData());
3260 EXPECT_CALL(*controller()->GetMockValidator(),
3261 LoadRules("US")).Times(AtLeast(1));
3262 controller()->Show();
3264 EXPECT_CALL(*controller()->GetMockValidator(), LoadRules("FR"));
3265 controller()->UserEditedOrActivatedInput(SECTION_BILLING,
3266 ADDRESS_BILLING_COUNTRY,
3267 gfx::NativeView(),
3268 gfx::Rect(),
3269 ASCIIToUTF16("France"),
3270 true);
3273 TEST_F(AutofillDialogControllerTest, UsValidationRulesLoadedForJpOnlyProfile) {
3274 ResetControllerWithFormData(DefaultFormData());
3275 AutofillProfile jp_profile(base::GenerateGUID(), kSettingsOrigin);
3276 jp_profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("JP"));
3277 controller()->GetTestingManager()->AddTestingProfile(&jp_profile);
3278 EXPECT_CALL(*controller()->GetMockValidator(), LoadRules("US"));
3279 EXPECT_CALL(*controller()->GetMockValidator(),
3280 LoadRules("JP")).Times(AtLeast(1));
3281 controller()->Show();
3284 TEST_F(AutofillDialogControllerTest, InvalidWhenRulesNotReady) {
3285 // Select "Add new shipping address...".
3286 controller()->MenuModelForSection(SECTION_SHIPPING)->ActivatedAt(1);
3288 // If the rules haven't loaded yet, validation errors should show on submit.
3289 EXPECT_CALL(*controller()->GetMockValidator(),
3290 ValidateAddress(CountryCodeMatcher("US"), _, _)).
3291 WillRepeatedly(Return(AddressValidator::RULES_NOT_READY));
3293 FieldValueMap inputs;
3294 inputs[ADDRESS_HOME_ZIP] = ASCIIToUTF16("1234");
3295 inputs[ADDRESS_HOME_COUNTRY] = ASCIIToUTF16("United States");
3297 ValidityMessages messages =
3298 controller()->InputsAreValid(SECTION_SHIPPING, inputs);
3299 EXPECT_FALSE(messages.GetMessageOrDefault(ADDRESS_HOME_ZIP).text.empty());
3300 EXPECT_FALSE(messages.HasSureError(ADDRESS_HOME_ZIP));
3301 // Country should never show an error message as it's always valid.
3302 EXPECT_TRUE(messages.GetMessageOrDefault(ADDRESS_HOME_COUNTRY).text.empty());
3305 TEST_F(AutofillDialogControllerTest, ValidButUnverifiedWhenRulesFail) {
3306 SwitchToAutofill();
3308 // Add suggestions so the credit card and billing sections aren't showing
3309 // their manual inputs (to isolate to just shipping).
3310 AutofillProfile verified_profile(test::GetVerifiedProfile());
3311 controller()->GetTestingManager()->AddTestingProfile(&verified_profile);
3312 CreditCard verified_card(test::GetVerifiedCreditCard());
3313 controller()->GetTestingManager()->AddTestingCreditCard(&verified_card);
3315 // Select "Add new shipping address...".
3316 controller()->MenuModelForSection(SECTION_SHIPPING)->ActivatedAt(2);
3318 // If the rules are unavailable, validation errors should not show.
3319 EXPECT_CALL(*controller()->GetMockValidator(),
3320 ValidateAddress(CountryCodeMatcher("US"), _, _)).
3321 WillRepeatedly(Return(AddressValidator::RULES_UNAVAILABLE));
3323 FieldValueMap outputs;
3324 AutofillProfile full_profile(test::GetFullProfile());
3325 const DetailInputs& inputs =
3326 controller()->RequestedFieldsForSection(SECTION_SHIPPING);
3327 for (size_t i = 0; i < inputs.size(); ++i) {
3328 const ServerFieldType type = inputs[i].type;
3329 outputs[type] = full_profile.GetInfo(AutofillType(type), "en-US");
3331 controller()->GetView()->SetUserInput(SECTION_SHIPPING, outputs);
3332 controller()->GetView()->CheckSaveDetailsLocallyCheckbox(true);
3333 controller()->OnAccept();
3335 // Profiles saved while rules are unavailable shouldn't be verified.
3336 const AutofillProfile& imported_profile =
3337 controller()->GetTestingManager()->imported_profile();
3338 ASSERT_EQ(imported_profile.GetInfo(AutofillType(NAME_FULL), "en-US"),
3339 full_profile.GetInfo(AutofillType(NAME_FULL), "en-US"));
3340 EXPECT_EQ(imported_profile.origin(), GURL(kSourceUrl).GetOrigin().spec());
3341 EXPECT_FALSE(imported_profile.IsVerified());
3344 TEST_F(AutofillDialogControllerTest, LimitedCountryChoices) {
3345 ui::ComboboxModel* shipping_country_model =
3346 controller()->ComboboxModelForAutofillType(ADDRESS_HOME_COUNTRY);
3347 const int default_number_of_countries =
3348 shipping_country_model->GetItemCount();
3349 // We show a lot of countries by default, but the exact number doesn't matter.
3350 EXPECT_GT(default_number_of_countries, 50);
3352 // Create a form data that simulates:
3353 // <select autocomplete="billing country">
3354 // <option value="AU">Down Under</option>
3355 // <option value="">fR</option> <!-- Case doesn't matter -->
3356 // <option value="GRMNY">Germany</option>
3357 // </select>
3358 // Only country codes are respected, whether they're in value or the option's
3359 // text content. Thus the first two options should be recognized.
3360 FormData form_data;
3361 FormFieldData field;
3362 field.autocomplete_attribute = "billing country";
3363 field.option_contents.push_back(ASCIIToUTF16("Down Under"));
3364 field.option_values.push_back(ASCIIToUTF16("AU"));
3365 field.option_contents.push_back(ASCIIToUTF16("Fr"));
3366 field.option_values.push_back(ASCIIToUTF16(""));
3367 field.option_contents.push_back(ASCIIToUTF16("Germany"));
3368 field.option_values.push_back(ASCIIToUTF16("GRMNY"));
3370 FormFieldData cc_field;
3371 cc_field.autocomplete_attribute = "cc-csc";
3373 form_data.fields.push_back(field);
3374 form_data.fields.push_back(cc_field);
3375 ResetControllerWithFormData(form_data);
3376 controller()->Show();
3378 // Shipping model shouldn't have changed.
3379 shipping_country_model =
3380 controller()->ComboboxModelForAutofillType(ADDRESS_HOME_COUNTRY);
3381 EXPECT_EQ(default_number_of_countries,
3382 shipping_country_model->GetItemCount());
3383 // Billing model now only has two items.
3384 ui::ComboboxModel* billing_country_model =
3385 controller()->ComboboxModelForAutofillType(ADDRESS_BILLING_COUNTRY);
3386 ASSERT_EQ(2, billing_country_model->GetItemCount());
3387 EXPECT_EQ(billing_country_model->GetItemAt(0), ASCIIToUTF16("Australia"));
3388 EXPECT_EQ(billing_country_model->GetItemAt(1), ASCIIToUTF16("France"));
3390 // Make sure it also applies to profile suggestions.
3391 AutofillProfile us_profile(test::GetVerifiedProfile());
3392 us_profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
3393 controller()->GetTestingManager()->AddTestingProfile(&us_profile);
3394 // Don't show a suggestion if the only one that exists is disabled.
3395 EXPECT_FALSE(
3396 controller()->SuggestionStateForSection(SECTION_BILLING).visible);
3398 // Add a profile with an acceptable country; suggestion should be shown.
3399 ResetControllerWithFormData(form_data);
3400 controller()->Show();
3401 AutofillProfile au_profile(test::GetVerifiedProfile2());
3402 au_profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("AU"));
3403 controller()->GetTestingManager()->AddTestingProfile(&us_profile);
3404 controller()->GetTestingManager()->AddTestingProfile(&au_profile);
3405 ui::MenuModel* model = controller()->MenuModelForSection(SECTION_BILLING);
3406 ASSERT_TRUE(model);
3407 EXPECT_EQ(4, model->GetItemCount());
3408 EXPECT_FALSE(model->IsEnabledAt(0));
3409 EXPECT_TRUE(model->IsEnabledAt(1));
3411 // Add <input type="text" autocomplete="billing country"></input>
3412 // This should open up selection of all countries again.
3413 FormFieldData field2;
3414 field2.autocomplete_attribute = "billing country";
3415 form_data.fields.push_back(field2);
3416 ResetControllerWithFormData(form_data);
3417 controller()->Show();
3419 billing_country_model =
3420 controller()->ComboboxModelForAutofillType(ADDRESS_BILLING_COUNTRY);
3421 EXPECT_EQ(default_number_of_countries,
3422 billing_country_model->GetItemCount());
3425 TEST_F(AutofillDialogControllerTest, WalletUnsupportedCountries) {
3426 // Create a form data that simulates:
3427 // <select autocomplete="billing country">
3428 // <option value="IQ">Iraq</option>
3429 // <option value="MX">Mexico</option>
3430 // </select>
3431 // i.e. contains a mix of supported and unsupported countries.
3432 FormData form_data;
3433 FormFieldData field;
3434 field.autocomplete_attribute = "shipping country";
3435 field.option_contents.push_back(ASCIIToUTF16("Iraq"));
3436 field.option_values.push_back(ASCIIToUTF16("IQ"));
3437 field.option_contents.push_back(ASCIIToUTF16("Mexico"));
3438 field.option_values.push_back(ASCIIToUTF16("MX"));
3440 FormFieldData cc_field;
3441 cc_field.autocomplete_attribute = "cc-csc";
3443 form_data.fields.push_back(field);
3444 form_data.fields.push_back(cc_field);
3445 ResetControllerWithFormData(form_data);
3446 controller()->Show();
3448 ui::ComboboxModel* shipping_country_model =
3449 controller()->ComboboxModelForAutofillType(ADDRESS_HOME_COUNTRY);
3450 ASSERT_EQ(2, shipping_country_model->GetItemCount());
3451 EXPECT_EQ(shipping_country_model->GetItemAt(0), ASCIIToUTF16("Iraq"));
3452 EXPECT_EQ(shipping_country_model->GetItemAt(1), ASCIIToUTF16("Mexico"));
3454 // Switch to Wallet, add limitations.
3455 SetUpControllerWithFormData(form_data);
3456 SwitchToWallet();
3457 scoped_ptr<wallet::WalletItems> wallet_items =
3458 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
3459 wallet_items->AddAllowedShippingCountry("MX");
3460 wallet_items->AddAllowedShippingCountry("GB");
3461 controller()->OnDidGetWalletItems(wallet_items.Pass());
3463 // Only the intersection is available.
3464 EXPECT_TRUE(controller()->IsPayingWithWallet());
3465 shipping_country_model =
3466 controller()->ComboboxModelForAutofillType(ADDRESS_HOME_COUNTRY);
3467 ASSERT_EQ(1, shipping_country_model->GetItemCount());
3468 EXPECT_EQ(shipping_country_model->GetItemAt(0), ASCIIToUTF16("Mexico"));
3470 // Empty intersection; Wallet's automatically disabled.
3471 wallet_items = wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
3472 wallet_items->AddAllowedShippingCountry("CA");
3473 wallet_items->AddAllowedShippingCountry("GB");
3474 controller()->OnDidGetWalletItems(wallet_items.Pass());
3475 EXPECT_FALSE(controller()->IsPayingWithWallet());
3477 // Since it's disabled, we revert to accepting all the countries.
3478 shipping_country_model =
3479 controller()->ComboboxModelForAutofillType(ADDRESS_HOME_COUNTRY);
3480 ASSERT_EQ(2, shipping_country_model->GetItemCount());
3481 EXPECT_EQ(shipping_country_model->GetItemAt(0), ASCIIToUTF16("Iraq"));
3482 EXPECT_EQ(shipping_country_model->GetItemAt(1), ASCIIToUTF16("Mexico"));
3485 // http://crbug.com/388018
3486 TEST_F(AutofillDialogControllerTest, NoCountryChoices) {
3487 // Create a form data that simulates:
3488 // <select autocomplete="billing country">
3489 // <option value="ATL">Atlantis</option>
3490 // <option value="ELD">Eldorado</option>
3491 // </select>
3492 // i.e. contains a list of no valid countries.
3493 FormData form_data;
3494 FormFieldData field;
3495 field.autocomplete_attribute = "billing country";
3496 field.option_contents.push_back(ASCIIToUTF16("Atlantis"));
3497 field.option_values.push_back(ASCIIToUTF16("ATL"));
3498 field.option_contents.push_back(ASCIIToUTF16("Eldorado"));
3499 field.option_values.push_back(ASCIIToUTF16("ELD"));
3501 FormFieldData cc_field;
3502 cc_field.autocomplete_attribute = "cc-csc";
3504 form_data.fields.push_back(field);
3505 form_data.fields.push_back(cc_field);
3506 ResetControllerWithFormData(form_data);
3507 controller()->Show();
3509 // Controller aborts and self destructs.
3510 EXPECT_EQ(0, controller());
3513 TEST_F(AutofillDialogControllerTest, LimitedCcChoices) {
3514 SwitchToAutofill();
3515 // Typically, MC and Visa are both valid.
3516 ValidateCCNumber(SECTION_CC, kTestCCNumberMaster, true);
3517 ValidateCCNumber(SECTION_CC, kTestCCNumberVisa, true);
3519 FormData form_data;
3520 FormFieldData field;
3521 field.autocomplete_attribute = "billing cc-type";
3522 field.option_contents.push_back(ASCIIToUTF16("Visa"));
3523 field.option_values.push_back(ASCIIToUTF16("V"));
3524 field.option_contents.push_back(ASCIIToUTF16("Amex"));
3525 field.option_values.push_back(ASCIIToUTF16("AX"));
3526 form_data.fields.push_back(field);
3527 ResetControllerWithFormData(form_data);
3528 controller()->Show();
3530 // MC is not valid because it's missing from FormData.
3531 EXPECT_EQ(l10n_util::GetStringUTF16(
3532 IDS_AUTOFILL_DIALOG_VALIDATION_UNACCEPTED_MASTERCARD),
3533 ValidateCCNumber(SECTION_CC, kTestCCNumberMaster, false));
3534 ValidateCCNumber(SECTION_CC, kTestCCNumberVisa, true);
3536 CreditCard visa_card(test::GetVerifiedCreditCard());
3537 CreditCard amex_card(test::GetVerifiedCreditCard2());
3539 CreditCard master_card(base::GenerateGUID(), "chrome settings");
3540 test::SetCreditCardInfo(
3541 &master_card, "Mr Foo", "5105105105105100", "07", "2099");
3543 controller()->GetTestingManager()->AddTestingCreditCard(&visa_card);
3544 controller()->GetTestingManager()->AddTestingCreditCard(&amex_card);
3545 controller()->GetTestingManager()->AddTestingCreditCard(&master_card);
3547 // The stored MC is disabled in the dropdown.
3548 ui::MenuModel* model = controller()->MenuModelForSection(SECTION_CC);
3549 ASSERT_TRUE(model);
3550 ASSERT_EQ(5, model->GetItemCount());
3551 EXPECT_TRUE(model->IsEnabledAt(0));
3552 EXPECT_TRUE(model->IsEnabledAt(1));
3553 EXPECT_FALSE(model->IsEnabledAt(2));
3554 EXPECT_TRUE(model->IsEnabledAt(3));
3555 EXPECT_TRUE(model->IsEnabledAt(4));
3557 // No MC; Wallet is disabled.
3558 SetUpControllerWithFormData(form_data);
3559 EXPECT_FALSE(controller()->IsPayingWithWallet());
3561 // In Autofill mode, Discover is disallowed because it's not in FormData.
3562 ValidateCCNumber(SECTION_CC, kTestCCNumberDiscover, false);
3564 field.option_contents.push_back(ASCIIToUTF16("Mastercard"));
3565 field.option_values.push_back(ASCIIToUTF16("Mastercard"));
3566 form_data.fields[0] = field;
3568 // Add MC to FormData; Wallet is enabled.
3569 SetUpControllerWithFormData(form_data);
3570 EXPECT_TRUE(controller()->IsPayingWithWallet());
3571 // Even though Discover isn't in FormData, it's allowed because Wallet always
3572 // generates a MC Virtual card.
3573 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberDiscover, true);
3576 TEST_F(AutofillDialogControllerTest, SuggestCountrylessProfiles) {
3577 SwitchToAutofill();
3579 FieldValueMap outputs;
3580 outputs[ADDRESS_HOME_COUNTRY] = ASCIIToUTF16("US");
3581 controller()->GetView()->SetUserInput(SECTION_SHIPPING, outputs);
3583 AutofillProfile profile(test::GetVerifiedProfile());
3584 profile.SetRawInfo(NAME_FULL, ASCIIToUTF16("The Man Without a Country"));
3585 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, base::string16());
3586 controller()->GetTestingManager()->AddTestingProfile(&profile);
3588 controller()->UserEditedOrActivatedInput(
3589 SECTION_SHIPPING,
3590 NAME_FULL,
3591 gfx::NativeView(),
3592 gfx::Rect(),
3593 profile.GetRawInfo(NAME_FULL).substr(0, 1),
3594 true);
3595 EXPECT_EQ(NAME_FULL, controller()->popup_input_type());
3598 TEST_F(AutofillDialogControllerTest, SwitchFromWalletWithFirstName) {
3599 controller()->MenuModelForSection(SECTION_CC_BILLING)->ActivatedAt(2);
3601 FieldValueMap outputs;
3602 outputs[NAME_FULL] = ASCIIToUTF16("madonna");
3603 controller()->GetView()->SetUserInput(SECTION_CC_BILLING, outputs);
3605 ASSERT_NO_FATAL_FAILURE(SwitchToAutofill());
3608 // Regression test for http://crbug.com/382777
3609 TEST_F(AutofillDialogControllerTest, WalletBillingCountry) {
3610 FormFieldData cc_field;
3611 cc_field.autocomplete_attribute = "cc-number";
3612 FormFieldData billing_country, billing_country_name, shipping_country,
3613 shipping_country_name;
3614 billing_country.autocomplete_attribute = "billing country";
3615 billing_country_name.autocomplete_attribute = "billing country-name";
3616 shipping_country.autocomplete_attribute = "shipping country";
3617 shipping_country_name.autocomplete_attribute = "shipping country-name";
3619 FormData form_data;
3620 form_data.fields.push_back(cc_field);
3621 form_data.fields.push_back(billing_country);
3622 form_data.fields.push_back(billing_country_name);
3623 form_data.fields.push_back(shipping_country);
3624 form_data.fields.push_back(shipping_country_name);
3626 SetUpControllerWithFormData(form_data);
3627 AcceptAndLoadFakeFingerprint();
3628 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
3629 controller()->ForceFinishSubmit();
3631 ASSERT_EQ(5U, form_structure()->field_count());
3632 EXPECT_EQ(ADDRESS_HOME_COUNTRY,
3633 form_structure()->field(1)->Type().GetStorableType());
3634 EXPECT_EQ(ASCIIToUTF16("US"), form_structure()->field(1)->value);
3635 EXPECT_EQ(ADDRESS_HOME_COUNTRY,
3636 form_structure()->field(2)->Type().GetStorableType());
3637 EXPECT_EQ(ASCIIToUTF16("United States"), form_structure()->field(2)->value);
3638 EXPECT_EQ(ADDRESS_HOME_COUNTRY,
3639 form_structure()->field(3)->Type().GetStorableType());
3640 EXPECT_EQ(ASCIIToUTF16("US"), form_structure()->field(3)->value);
3641 EXPECT_EQ(ADDRESS_HOME_COUNTRY,
3642 form_structure()->field(4)->Type().GetStorableType());
3643 EXPECT_EQ(ASCIIToUTF16("United States"), form_structure()->field(4)->value);
3646 } // namespace autofill