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.
8 #include "base/command_line.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/run_loop.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_piece.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/tuple.h"
18 #include "chrome/browser/ui/autofill/autofill_dialog_controller_impl.h"
19 #include "chrome/browser/ui/autofill/autofill_dialog_view.h"
20 #include "chrome/browser/ui/autofill/generated_credit_card_bubble_controller.h"
21 #include "chrome/browser/ui/autofill/mock_new_credit_card_bubble_controller.h"
22 #include "chrome/browser/ui/autofill/test_generated_credit_card_bubble_controller.h"
23 #include "chrome/browser/webdata/web_data_service_factory.h"
24 #include "chrome/common/chrome_switches.h"
25 #include "chrome/common/pref_names.h"
26 #include "chrome/common/render_messages.h"
27 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
28 #include "chrome/test/base/scoped_testing_local_state.h"
29 #include "chrome/test/base/testing_browser_process.h"
30 #include "chrome/test/base/testing_profile.h"
31 #include "components/autofill/content/browser/risk/proto/fingerprint.pb.h"
32 #include "components/autofill/content/browser/wallet/full_wallet.h"
33 #include "components/autofill/content/browser/wallet/gaia_account.h"
34 #include "components/autofill/content/browser/wallet/instrument.h"
35 #include "components/autofill/content/browser/wallet/mock_wallet_client.h"
36 #include "components/autofill/content/browser/wallet/wallet_address.h"
37 #include "components/autofill/content/browser/wallet/wallet_service_url.h"
38 #include "components/autofill/content/browser/wallet/wallet_test_util.h"
39 #include "components/autofill/core/browser/autofill_metrics.h"
40 #include "components/autofill/core/browser/autofill_test_utils.h"
41 #include "components/autofill/core/browser/test_personal_data_manager.h"
42 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
43 #include "components/autofill/core/common/autofill_switches.h"
44 #include "components/autofill/core/common/form_data.h"
45 #include "components/user_prefs/user_prefs.h"
46 #include "content/public/browser/web_contents.h"
47 #include "content/public/test/mock_render_process_host.h"
48 #include "google_apis/gaia/google_service_auth_error.h"
49 #include "grit/webkit_resources.h"
50 #include "testing/gmock/include/gmock/gmock.h"
51 #include "testing/gtest/include/gtest/gtest.h"
52 #include "ui/base/resource/resource_bundle.h"
55 #include "ui/base/win/scoped_ole_initializer.h"
58 using base::ASCIIToUTF16
;
59 using base::UTF8ToUTF16
;
67 const char kFakeEmail
[] = "user@chromium.org";
68 const char kFakeFingerprintEncoded
[] = "CgVaAwiACA==";
69 const char kEditedBillingAddress
[] = "123 edited billing address";
70 const char* kFieldsFromPage
[] =
78 "billing address-line1",
81 "billing postal-code",
85 "shipping address-line1",
88 "shipping postal-code",
92 const char kSettingsOrigin
[] = "Chrome settings";
93 const char kTestCCNumberAmex
[] = "376200000000002";
94 const char kTestCCNumberVisa
[] = "4111111111111111";
95 const char kTestCCNumberMaster
[] = "5555555555554444";
96 const char kTestCCNumberDiscover
[] = "6011111111111117";
97 const char kTestCCNumberIncomplete
[] = "4111111111";
98 // Credit card number fails Luhn check.
99 const char kTestCCNumberInvalid
[] = "4111111111111112";
101 // Copies the initial values from |inputs| into |outputs|.
102 void CopyInitialValues(const DetailInputs
& inputs
, FieldValueMap
* outputs
) {
103 for (size_t i
= 0; i
< inputs
.size(); ++i
) {
104 const DetailInput
& input
= inputs
[i
];
105 (*outputs
)[input
.type
] = input
.initial_value
;
109 scoped_ptr
<wallet::WalletItems
> CompleteAndValidWalletItems() {
110 scoped_ptr
<wallet::WalletItems
> items
=
111 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
112 items
->AddAccount(wallet::GetTestGaiaAccount());
113 items
->AddInstrument(wallet::GetTestMaskedInstrument());
114 items
->AddAddress(wallet::GetTestShippingAddress());
118 scoped_ptr
<risk::Fingerprint
> GetFakeFingerprint() {
119 scoped_ptr
<risk::Fingerprint
> fingerprint(new risk::Fingerprint());
120 // Add some data to the proto, else the encoded content is empty.
121 fingerprint
->mutable_machine_characteristics()->mutable_screen_size()->
123 return fingerprint
.Pass();
126 bool HasAnyError(const ValidityMessages
& messages
, ServerFieldType field
) {
127 return !messages
.GetMessageOrDefault(field
).text
.empty();
130 bool HasUnsureError(const ValidityMessages
& messages
, ServerFieldType field
) {
131 const ValidityMessage
& message
= messages
.GetMessageOrDefault(field
);
132 return !message
.text
.empty() && !message
.sure
;
135 class TestAutofillDialogView
: public AutofillDialogView
{
137 TestAutofillDialogView()
138 : updates_started_(0), save_details_locally_checked_(true) {}
139 virtual ~TestAutofillDialogView() {}
141 virtual void Show() OVERRIDE
{}
142 virtual void Hide() OVERRIDE
{}
144 virtual void UpdatesStarted() OVERRIDE
{
148 virtual void UpdatesFinished() OVERRIDE
{
150 EXPECT_GE(updates_started_
, 0);
153 virtual void UpdateNotificationArea() OVERRIDE
{
154 EXPECT_GE(updates_started_
, 1);
157 virtual void UpdateAccountChooser() OVERRIDE
{
158 EXPECT_GE(updates_started_
, 1);
161 virtual void UpdateButtonStrip() OVERRIDE
{
162 EXPECT_GE(updates_started_
, 1);
165 virtual void UpdateOverlay() OVERRIDE
{
166 EXPECT_GE(updates_started_
, 1);
169 virtual void UpdateDetailArea() OVERRIDE
{
170 EXPECT_GE(updates_started_
, 1);
173 virtual void UpdateSection(DialogSection section
) OVERRIDE
{
174 section_updates_
[section
]++;
175 EXPECT_GE(updates_started_
, 1);
178 virtual void UpdateErrorBubble() OVERRIDE
{
179 EXPECT_GE(updates_started_
, 1);
182 virtual void FillSection(DialogSection section
,
183 ServerFieldType originating_type
) OVERRIDE
{}
184 virtual void GetUserInput(DialogSection section
, FieldValueMap
* output
)
186 *output
= outputs_
[section
];
188 virtual TestableAutofillDialogView
* GetTestableView() OVERRIDE
{
192 virtual base::string16
GetCvc() OVERRIDE
{ return base::string16(); }
193 virtual bool HitTestInput(ServerFieldType type
,
194 const gfx::Point
& screen_point
) OVERRIDE
{
198 virtual bool SaveDetailsLocally() OVERRIDE
{
199 return save_details_locally_checked_
;
202 virtual const content::NavigationController
* ShowSignIn() OVERRIDE
{
205 virtual void HideSignIn() OVERRIDE
{}
207 MOCK_METHOD0(ModelChanged
, void());
208 MOCK_METHOD0(UpdateForErrors
, void());
210 virtual void OnSignInResize(const gfx::Size
& pref_size
) OVERRIDE
{}
212 void SetUserInput(DialogSection section
, const FieldValueMap
& map
) {
213 outputs_
[section
] = map
;
216 void CheckSaveDetailsLocallyCheckbox(bool checked
) {
217 save_details_locally_checked_
= checked
;
220 void ClearSectionUpdates() {
221 section_updates_
.clear();
224 std::map
<DialogSection
, size_t> section_updates() const {
225 return section_updates_
;
229 std::map
<DialogSection
, FieldValueMap
> outputs_
;
230 std::map
<DialogSection
, size_t> section_updates_
;
232 int updates_started_
;
233 bool save_details_locally_checked_
;
235 DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogView
);
238 class TestAutofillDialogController
239 : public AutofillDialogControllerImpl
,
240 public base::SupportsWeakPtr
<TestAutofillDialogController
> {
242 TestAutofillDialogController(
243 content::WebContents
* contents
,
244 const FormData
& form_structure
,
245 const GURL
& source_url
,
246 const AutofillMetrics
& metric_logger
,
247 const base::Callback
<void(const FormStructure
*)>& callback
,
248 MockNewCreditCardBubbleController
* mock_new_card_bubble_controller
)
249 : AutofillDialogControllerImpl(contents
,
253 metric_logger_(metric_logger
),
255 Profile::FromBrowserContext(contents
->GetBrowserContext())->
256 GetRequestContext(), this, source_url
),
257 mock_new_card_bubble_controller_(mock_new_card_bubble_controller
),
258 submit_button_delay_count_(0) {}
260 virtual ~TestAutofillDialogController() {}
262 virtual AutofillDialogView
* CreateView() OVERRIDE
{
263 return new testing::NiceMock
<TestAutofillDialogView
>();
266 void Init(content::BrowserContext
* browser_context
) {
268 WebDataServiceFactory::GetAutofillWebDataForProfile(
269 Profile::FromBrowserContext(browser_context
),
270 Profile::EXPLICIT_ACCESS
),
271 user_prefs::UserPrefs::Get(browser_context
),
272 browser_context
->IsOffTheRecord());
275 TestAutofillDialogView
* GetView() {
276 return static_cast<TestAutofillDialogView
*>(view());
279 TestPersonalDataManager
* GetTestingManager() {
280 return &test_manager_
;
283 wallet::MockWalletClient
* GetTestingWalletClient() {
284 return &mock_wallet_client_
;
287 const GURL
& open_tab_url() { return open_tab_url_
; }
289 void SimulateSigninError() {
290 OnWalletSigninError();
293 // Skips past the 2 second wait between FinishSubmit and DoFinishSubmit.
294 void ForceFinishSubmit() {
298 void SimulateSubmitButtonDelayBegin() {
299 AutofillDialogControllerImpl::SubmitButtonDelayBegin();
302 void SimulateSubmitButtonDelayEnd() {
303 AutofillDialogControllerImpl::SubmitButtonDelayEndForTesting();
306 using AutofillDialogControllerImpl::
307 ClearLastWalletItemsFetchTimestampForTesting
;
309 // Returns the number of times that the submit button was delayed.
310 int get_submit_button_delay_count() const {
311 return submit_button_delay_count_
;
314 MOCK_METHOD0(LoadRiskFingerprintData
, void());
315 using AutofillDialogControllerImpl::AccountChooserModelForTesting
;
316 using AutofillDialogControllerImpl::OnDidLoadRiskFingerprintData
;
317 using AutofillDialogControllerImpl::IsEditingExistingData
;
318 using AutofillDialogControllerImpl::IsManuallyEditingSection
;
319 using AutofillDialogControllerImpl::IsSubmitPausedOn
;
320 using AutofillDialogControllerImpl::NOT_CHECKED
;
321 using AutofillDialogControllerImpl::SignedInState
;
324 virtual PersonalDataManager
* GetManager() const OVERRIDE
{
325 return const_cast<TestAutofillDialogController
*>(this)->
329 virtual wallet::WalletClient
* GetWalletClient() OVERRIDE
{
330 return &mock_wallet_client_
;
333 virtual void OpenTabWithUrl(const GURL
& url
) OVERRIDE
{
337 virtual void ShowNewCreditCardBubble(
338 scoped_ptr
<CreditCard
> new_card
,
339 scoped_ptr
<AutofillProfile
> billing_profile
) OVERRIDE
{
340 mock_new_card_bubble_controller_
->Show(new_card
.Pass(),
341 billing_profile
.Pass());
344 // AutofillDialogControllerImpl calls this method before showing the dialog
346 virtual void SubmitButtonDelayBegin() OVERRIDE
{
347 // Do not delay enabling the submit button in testing.
348 submit_button_delay_count_
++;
352 // To specify our own metric logger.
353 virtual const AutofillMetrics
& GetMetricLogger() const OVERRIDE
{
354 return metric_logger_
;
357 const AutofillMetrics
& metric_logger_
;
358 TestPersonalDataManager test_manager_
;
359 testing::NiceMock
<wallet::MockWalletClient
> mock_wallet_client_
;
361 MockNewCreditCardBubbleController
* mock_new_card_bubble_controller_
;
363 // The number of times that the submit button was delayed.
364 int submit_button_delay_count_
;
366 DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogController
);
369 class AutofillDialogControllerTest
: public ChromeRenderViewHostTestHarness
{
371 AutofillDialogControllerTest(): form_structure_(NULL
) {}
373 // testing::Test implementation:
374 virtual void SetUp() OVERRIDE
{
375 ChromeRenderViewHostTestHarness::SetUp();
379 virtual void TearDown() OVERRIDE
{
381 controller_
->ViewClosed();
382 ChromeRenderViewHostTestHarness::TearDown();
387 controller_
->ViewClosed();
389 test_generated_bubble_controller_
=
390 new testing::NiceMock
<TestGeneratedCreditCardBubbleController
>(
392 ASSERT_TRUE(test_generated_bubble_controller_
->IsInstalled());
394 mock_new_card_bubble_controller_
.reset(
395 new MockNewCreditCardBubbleController
);
397 profile()->GetPrefs()->ClearPref(::prefs::kAutofillDialogSaveData
);
399 // We have to clear the old local state before creating a new one.
400 scoped_local_state_
.reset();
401 scoped_local_state_
.reset(new ScopedTestingLocalState(
402 TestingBrowserProcess::GetGlobal()));
404 SetUpControllerWithFormData(DefaultFormData());
407 FormData
DefaultFormData() {
409 for (size_t i
= 0; i
< arraysize(kFieldsFromPage
); ++i
) {
411 field
.autocomplete_attribute
= kFieldsFromPage
[i
];
412 form_data
.fields
.push_back(field
);
417 // Creates a new controller for |form_data|.
418 void ResetControllerWithFormData(const FormData
& form_data
) {
420 controller_
->ViewClosed();
422 base::Callback
<void(const FormStructure
*)> callback
=
423 base::Bind(&AutofillDialogControllerTest::FinishedCallback
,
424 base::Unretained(this));
425 controller_
= (new testing::NiceMock
<TestAutofillDialogController
>(
431 mock_new_card_bubble_controller_
.get()))->AsWeakPtr();
432 controller_
->Init(profile());
435 // Creates a new controller for |form_data| and sets up some initial wallet
437 void SetUpControllerWithFormData(const FormData
& form_data
) {
438 ResetControllerWithFormData(form_data
);
439 controller()->Show();
440 if (!profile()->GetPrefs()->GetBoolean(
441 ::prefs::kAutofillDialogPayWithoutWallet
)) {
442 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems());
443 controller()->OnDidFetchWalletCookieValue(std::string());
444 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
448 // Fills the inputs in SECTION_CC with data.
449 void FillCreditCardInputs() {
450 FieldValueMap cc_outputs
;
451 const DetailInputs
& cc_inputs
=
452 controller()->RequestedFieldsForSection(SECTION_CC
);
453 for (size_t i
= 0; i
< cc_inputs
.size(); ++i
) {
454 cc_outputs
[cc_inputs
[i
].type
] = cc_inputs
[i
].type
== CREDIT_CARD_NUMBER
?
455 ASCIIToUTF16(kTestCCNumberVisa
) : ASCIIToUTF16("11");
457 controller()->GetView()->SetUserInput(SECTION_CC
, cc_outputs
);
460 // Fills the inputs in SECTION_CC_BILLING with valid data.
461 void FillCCBillingInputs() {
462 FieldValueMap outputs
;
463 const DetailInputs
& inputs
=
464 controller()->RequestedFieldsForSection(SECTION_CC_BILLING
);
465 AutofillProfile
full_profile(test::GetVerifiedProfile());
466 CreditCard
full_card(test::GetCreditCard());
467 for (size_t i
= 0; i
< inputs
.size(); ++i
) {
468 const ServerFieldType type
= inputs
[i
].type
;
469 outputs
[type
] = full_profile
.GetInfo(AutofillType(type
), "en-US");
471 if (outputs
[type
].empty())
472 outputs
[type
] = full_card
.GetInfo(AutofillType(type
), "en-US");
474 controller()->GetView()->SetUserInput(SECTION_CC_BILLING
, outputs
);
477 // Activates the 'Add new foo' option from the |section|'s suggestions
478 // dropdown and fills the |section|'s inputs with the data from the
479 // |data_model|. If |section| is SECTION_CC, also fills in '123' for the CVC.
480 void FillInputs(DialogSection section
, const AutofillDataModel
& data_model
) {
481 // Select the 'Add new foo' option.
482 ui::MenuModel
* model
= GetMenuModelForSection(section
);
484 model
->ActivatedAt(model
->GetItemCount() - 2);
487 FieldValueMap outputs
;
488 const DetailInputs
& inputs
=
489 controller()->RequestedFieldsForSection(section
);
490 for (size_t i
= 0; i
< inputs
.size(); ++i
) {
491 ServerFieldType type
= inputs
[i
].type
;
492 base::string16 output
;
493 if (type
== CREDIT_CARD_VERIFICATION_CODE
)
494 output
= ASCIIToUTF16("123");
496 output
= data_model
.GetInfo(AutofillType(type
), "en-US");
497 outputs
[inputs
[i
].type
] = output
;
499 controller()->GetView()->SetUserInput(section
, outputs
);
502 std::vector
<DialogNotification
> NotificationsOfType(
503 DialogNotification::Type type
) {
504 std::vector
<DialogNotification
> right_type
;
505 const std::vector
<DialogNotification
>& notifications
=
506 controller()->CurrentNotifications();
507 for (size_t i
= 0; i
< notifications
.size(); ++i
) {
508 if (notifications
[i
].type() == type
)
509 right_type
.push_back(notifications
[i
]);
514 void SwitchToAutofill() {
515 ui::MenuModel
* model
= controller_
->MenuModelForAccountChooser();
516 model
->ActivatedAt(model
->GetItemCount() - 1);
519 void SwitchToWallet() {
520 controller_
->MenuModelForAccountChooser()->ActivatedAt(0);
523 void SimulateSigninError() {
524 controller_
->SimulateSigninError();
527 void UseBillingForShipping() {
528 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(0);
531 void ValidateCCNumber(DialogSection section
,
532 const std::string
& cc_number
,
534 FieldValueMap outputs
;
535 outputs
[CREDIT_CARD_NUMBER
] = UTF8ToUTF16(cc_number
);
536 ValidityMessages messages
=
537 controller()->InputsAreValid(section
, outputs
);
538 EXPECT_EQ(should_pass
, !messages
.HasSureError(CREDIT_CARD_NUMBER
));
541 void SubmitWithWalletItems(scoped_ptr
<wallet::WalletItems
> wallet_items
) {
542 controller()->OnDidGetWalletItems(wallet_items
.Pass());
543 AcceptAndLoadFakeFingerprint();
546 void AcceptAndLoadFakeFingerprint() {
547 controller()->OnAccept();
548 controller()->OnDidLoadRiskFingerprintData(GetFakeFingerprint().Pass());
551 // Returns true if the given |section| contains a field of the given |type|.
552 bool SectionContainsField(DialogSection section
, ServerFieldType type
) {
553 const DetailInputs
& inputs
=
554 controller()->RequestedFieldsForSection(section
);
555 for (DetailInputs::const_iterator it
= inputs
.begin(); it
!= inputs
.end();
557 if (it
->type
== type
)
563 SuggestionsMenuModel
* GetMenuModelForSection(DialogSection section
) {
564 ui::MenuModel
* model
= controller()->MenuModelForSection(section
);
565 return static_cast<SuggestionsMenuModel
*>(model
);
568 void SubmitAndVerifyShippingAndBillingResults() {
569 // Test after setting use billing for shipping.
570 UseBillingForShipping();
572 controller()->OnAccept();
574 ASSERT_EQ(20U, form_structure()->field_count());
575 EXPECT_EQ(ADDRESS_HOME_COUNTRY
,
576 form_structure()->field(11)->Type().GetStorableType());
577 EXPECT_EQ(ADDRESS_BILLING
, form_structure()->field(11)->Type().group());
578 EXPECT_EQ(ADDRESS_HOME_COUNTRY
,
579 form_structure()->field(18)->Type().GetStorableType());
580 EXPECT_EQ(ADDRESS_HOME
, form_structure()->field(18)->Type().group());
581 base::string16 billing_country
= form_structure()->field(11)->value
;
582 EXPECT_EQ(2U, billing_country
.size());
583 base::string16 shipping_country
= form_structure()->field(18)->value
;
584 EXPECT_EQ(2U, shipping_country
.size());
585 EXPECT_FALSE(billing_country
.empty());
586 EXPECT_FALSE(shipping_country
.empty());
587 EXPECT_EQ(billing_country
, shipping_country
);
589 EXPECT_EQ(CREDIT_CARD_NAME
,
590 form_structure()->field(1)->Type().GetStorableType());
591 base::string16 cc_name
= form_structure()->field(1)->value
;
592 EXPECT_EQ(NAME_FULL
, form_structure()->field(6)->Type().GetStorableType());
593 EXPECT_EQ(NAME_BILLING
, form_structure()->field(6)->Type().group());
594 base::string16 billing_name
= form_structure()->field(6)->value
;
595 EXPECT_EQ(NAME_FULL
, form_structure()->field(13)->Type().GetStorableType());
596 EXPECT_EQ(NAME
, form_structure()->field(13)->Type().group());
597 base::string16 shipping_name
= form_structure()->field(13)->value
;
599 EXPECT_FALSE(cc_name
.empty());
600 EXPECT_FALSE(billing_name
.empty());
601 EXPECT_FALSE(shipping_name
.empty());
602 EXPECT_EQ(cc_name
, billing_name
);
603 EXPECT_EQ(cc_name
, shipping_name
);
606 TestAutofillDialogController
* controller() { return controller_
.get(); }
608 const FormStructure
* form_structure() { return form_structure_
; }
610 TestGeneratedCreditCardBubbleController
* test_generated_bubble_controller() {
611 return test_generated_bubble_controller_
;
614 const MockNewCreditCardBubbleController
* mock_new_card_bubble_controller() {
615 return mock_new_card_bubble_controller_
.get();
619 void FinishedCallback(const FormStructure
* form_structure
) {
620 form_structure_
= form_structure
;
624 // http://crbug.com/227221
625 ui::ScopedOleInitializer ole_initializer_
;
628 // The controller owns itself.
629 base::WeakPtr
<TestAutofillDialogController
> controller_
;
631 // Must outlive the controller.
632 AutofillMetrics metric_logger_
;
634 // Returned when the dialog closes successfully.
635 const FormStructure
* form_structure_
;
637 // Used to monitor if the Autofill credit card bubble is shown. Owned by
639 TestGeneratedCreditCardBubbleController
* test_generated_bubble_controller_
;
641 // Used to record when new card bubbles would show. Created in |Reset()|.
642 scoped_ptr
<MockNewCreditCardBubbleController
>
643 mock_new_card_bubble_controller_
;
645 scoped_ptr
<ScopedTestingLocalState
> scoped_local_state_
;
650 // Ensure the default ValidityMessage has the expected values.
651 TEST_F(AutofillDialogControllerTest
, DefaultValidityMessage
) {
652 ValidityMessages messages
;
653 ValidityMessage message
= messages
.GetMessageOrDefault(UNKNOWN_TYPE
);
654 EXPECT_FALSE(message
.sure
);
655 EXPECT_TRUE(message
.text
.empty());
658 // This test makes sure nothing falls over when fields are being validity-
660 TEST_F(AutofillDialogControllerTest
, ValidityCheck
) {
661 for (size_t i
= SECTION_MIN
; i
<= SECTION_MAX
; ++i
) {
662 DialogSection section
= static_cast<DialogSection
>(i
);
663 const DetailInputs
& shipping_inputs
=
664 controller()->RequestedFieldsForSection(section
);
665 for (DetailInputs::const_iterator iter
= shipping_inputs
.begin();
666 iter
!= shipping_inputs
.end(); ++iter
) {
667 controller()->InputValidityMessage(section
, iter
->type
, base::string16());
672 // Test for phone number validation.
673 TEST_F(AutofillDialogControllerTest
, PhoneNumberValidation
) {
674 // Construct FieldValueMap from existing data.
677 for (size_t i
= 0; i
< 2; ++i
) {
678 ServerFieldType phone
= i
== 0 ? PHONE_HOME_WHOLE_NUMBER
:
679 PHONE_BILLING_WHOLE_NUMBER
;
680 ServerFieldType address
= i
== 0 ? ADDRESS_HOME_COUNTRY
:
681 ADDRESS_BILLING_COUNTRY
;
682 DialogSection section
= i
== 0 ? SECTION_SHIPPING
: SECTION_BILLING
;
684 FieldValueMap outputs
;
685 const DetailInputs
& inputs
=
686 controller()->RequestedFieldsForSection(section
);
687 AutofillProfile
full_profile(test::GetVerifiedProfile());
688 for (size_t i
= 0; i
< inputs
.size(); ++i
) {
689 const ServerFieldType type
= inputs
[i
].type
;
690 outputs
[type
] = full_profile
.GetInfo(AutofillType(type
), "en-US");
693 // Make sure country is United States.
694 outputs
[address
] = ASCIIToUTF16("United States");
696 // Existing data should have no errors.
697 ValidityMessages messages
= controller()->InputsAreValid(section
, outputs
);
698 EXPECT_FALSE(HasAnyError(messages
, phone
));
700 // Input an empty phone number.
701 outputs
[phone
] = base::string16();
702 messages
= controller()->InputsAreValid(section
, outputs
);
703 EXPECT_TRUE(HasUnsureError(messages
, phone
));
705 // Input an invalid phone number.
706 outputs
[phone
] = ASCIIToUTF16("ABC");
707 messages
= controller()->InputsAreValid(section
, outputs
);
708 EXPECT_TRUE(messages
.HasSureError(phone
));
710 // Input a local phone number.
711 outputs
[phone
] = ASCIIToUTF16("2155546699");
712 messages
= controller()->InputsAreValid(section
, outputs
);
713 EXPECT_FALSE(HasAnyError(messages
, phone
));
715 // Input an invalid local phone number.
716 outputs
[phone
] = ASCIIToUTF16("215554669");
717 messages
= controller()->InputsAreValid(section
, outputs
);
718 EXPECT_TRUE(messages
.HasSureError(phone
));
720 // Input an international phone number.
721 outputs
[phone
] = ASCIIToUTF16("+33 892 70 12 39");
722 messages
= controller()->InputsAreValid(section
, outputs
);
723 EXPECT_FALSE(HasAnyError(messages
, phone
));
725 // Input an invalid international phone number.
726 outputs
[phone
] = ASCIIToUTF16("+112333 892 70 12 39");
727 messages
= controller()->InputsAreValid(section
, outputs
);
728 EXPECT_TRUE(messages
.HasSureError(phone
));
732 TEST_F(AutofillDialogControllerTest
, ExpirationDateValidity
) {
733 ui::ComboboxModel
* exp_year_model
=
734 controller()->ComboboxModelForAutofillType(CREDIT_CARD_EXP_4_DIGIT_YEAR
);
735 ui::ComboboxModel
* exp_month_model
=
736 controller()->ComboboxModelForAutofillType(CREDIT_CARD_EXP_MONTH
);
738 base::string16 default_year_value
=
739 exp_year_model
->GetItemAt(exp_year_model
->GetDefaultIndex());
740 base::string16 default_month_value
=
741 exp_month_model
->GetItemAt(exp_month_model
->GetDefaultIndex());
743 base::string16 other_year_value
=
744 exp_year_model
->GetItemAt(exp_year_model
->GetItemCount() - 1);
745 base::string16 other_month_value
=
746 exp_month_model
->GetItemAt(exp_month_model
->GetItemCount() - 1);
748 FieldValueMap outputs
;
749 outputs
[CREDIT_CARD_EXP_MONTH
] = default_month_value
;
750 outputs
[CREDIT_CARD_EXP_4_DIGIT_YEAR
] = default_year_value
;
752 // Expiration default values generate unsure validation errors (but not sure).
753 ValidityMessages messages
= controller()->InputsAreValid(SECTION_CC_BILLING
,
755 EXPECT_TRUE(HasUnsureError(messages
, CREDIT_CARD_EXP_4_DIGIT_YEAR
));
756 EXPECT_TRUE(HasUnsureError(messages
, CREDIT_CARD_EXP_MONTH
));
758 // Expiration date with default month fails.
759 outputs
[CREDIT_CARD_EXP_4_DIGIT_YEAR
] = other_year_value
;
760 messages
= controller()->InputsAreValid(SECTION_CC_BILLING
, outputs
);
761 EXPECT_FALSE(HasUnsureError(messages
, CREDIT_CARD_EXP_4_DIGIT_YEAR
));
762 EXPECT_TRUE(HasUnsureError(messages
, CREDIT_CARD_EXP_MONTH
));
764 // Expiration date with default year fails.
765 outputs
[CREDIT_CARD_EXP_MONTH
] = other_month_value
;
766 outputs
[CREDIT_CARD_EXP_4_DIGIT_YEAR
] = default_year_value
;
767 messages
= controller()->InputsAreValid(SECTION_CC_BILLING
, outputs
);
768 EXPECT_TRUE(HasUnsureError(messages
, CREDIT_CARD_EXP_4_DIGIT_YEAR
));
769 EXPECT_FALSE(HasUnsureError(messages
, CREDIT_CARD_EXP_MONTH
));
772 TEST_F(AutofillDialogControllerTest
, BillingNameValidation
) {
773 // Construct FieldValueMap from AutofillProfile data.
776 // Input an empty billing name.
777 FieldValueMap outputs
;
778 outputs
[NAME_BILLING_FULL
] = base::string16();
779 ValidityMessages messages
= controller()->InputsAreValid(SECTION_BILLING
,
781 EXPECT_TRUE(HasUnsureError(messages
, NAME_BILLING_FULL
));
783 // Input a non-empty billing name.
784 outputs
[NAME_BILLING_FULL
] = ASCIIToUTF16("Bob");
785 messages
= controller()->InputsAreValid(SECTION_BILLING
, outputs
);
786 EXPECT_FALSE(HasAnyError(messages
, NAME_BILLING_FULL
));
788 // Switch to Wallet which only considers names with with at least two names to
792 // Setup some wallet state.
793 scoped_ptr
<wallet::WalletItems
> wallet_items
=
794 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
795 controller()->OnDidGetWalletItems(wallet_items
.Pass());
797 // Input an empty billing name. Data source should not change this behavior.
798 FieldValueMap wallet_outputs
;
799 wallet_outputs
[NAME_BILLING_FULL
] = base::string16();
800 messages
= controller()->InputsAreValid(SECTION_CC_BILLING
, wallet_outputs
);
801 EXPECT_TRUE(HasUnsureError(messages
, NAME_BILLING_FULL
));
803 // Input a one name billing name. Wallet does not currently support this.
804 wallet_outputs
[NAME_BILLING_FULL
] = ASCIIToUTF16("Bob");
805 messages
= controller()->InputsAreValid(SECTION_CC_BILLING
, wallet_outputs
);
806 EXPECT_TRUE(messages
.HasSureError(NAME_BILLING_FULL
));
808 // Input a two name billing name.
809 wallet_outputs
[NAME_BILLING_FULL
] = ASCIIToUTF16("Bob Barker");
810 messages
= controller()->InputsAreValid(SECTION_CC_BILLING
, wallet_outputs
);
811 EXPECT_FALSE(HasAnyError(messages
, NAME_BILLING_FULL
));
813 // Input a more than two name billing name.
814 wallet_outputs
[NAME_BILLING_FULL
] =
815 ASCIIToUTF16("John Jacob Jingleheimer Schmidt"),
816 messages
= controller()->InputsAreValid(SECTION_CC_BILLING
, wallet_outputs
);
817 EXPECT_FALSE(HasAnyError(messages
, NAME_BILLING_FULL
));
819 // Input a billing name with lots of crazy whitespace.
820 wallet_outputs
[NAME_BILLING_FULL
] =
821 ASCIIToUTF16(" \\n\\r John \\n Jacob Jingleheimer \\t Schmidt "),
822 messages
= controller()->InputsAreValid(SECTION_CC_BILLING
, wallet_outputs
);
823 EXPECT_FALSE(HasAnyError(messages
, NAME_BILLING_FULL
));
826 TEST_F(AutofillDialogControllerTest
, CreditCardNumberValidation
) {
827 // Construct FieldValueMap from AutofillProfile data.
830 // Should accept AMEX, Visa, Master and Discover.
831 ValidateCCNumber(SECTION_CC
, kTestCCNumberVisa
, true);
832 ValidateCCNumber(SECTION_CC
, kTestCCNumberMaster
, true);
833 ValidateCCNumber(SECTION_CC
, kTestCCNumberDiscover
, true);
834 ValidateCCNumber(SECTION_CC
, kTestCCNumberAmex
, true);
835 ValidateCCNumber(SECTION_CC
, kTestCCNumberIncomplete
, false);
836 ValidateCCNumber(SECTION_CC
, kTestCCNumberInvalid
, false);
838 // Switch to Wallet which will not accept AMEX.
841 // Setup some wallet state on a merchant for which Wallet doesn't
843 controller()->OnDidGetWalletItems(
844 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
));
846 // Should accept Visa, Master and Discover, but not AMEX.
847 ValidateCCNumber(SECTION_CC_BILLING
, kTestCCNumberVisa
, true);
848 ValidateCCNumber(SECTION_CC_BILLING
, kTestCCNumberMaster
, true);
849 ValidateCCNumber(SECTION_CC_BILLING
, kTestCCNumberDiscover
, true);
850 ValidateCCNumber(SECTION_CC_BILLING
, kTestCCNumberAmex
, false);
851 ValidateCCNumber(SECTION_CC_BILLING
, kTestCCNumberIncomplete
, false);
852 ValidateCCNumber(SECTION_CC_BILLING
, kTestCCNumberInvalid
, false);
854 // Setup some wallet state on a merchant for which Wallet supports AMEX.
855 controller()->OnDidGetWalletItems(
856 wallet::GetTestWalletItems(wallet::AMEX_ALLOWED
));
858 // Should accept Visa, Master, Discover, and AMEX.
859 ValidateCCNumber(SECTION_CC_BILLING
, kTestCCNumberVisa
, true);
860 ValidateCCNumber(SECTION_CC_BILLING
, kTestCCNumberMaster
, true);
861 ValidateCCNumber(SECTION_CC_BILLING
, kTestCCNumberDiscover
, true);
862 ValidateCCNumber(SECTION_CC_BILLING
, kTestCCNumberAmex
, true);
863 ValidateCCNumber(SECTION_CC_BILLING
, kTestCCNumberIncomplete
, false);
864 ValidateCCNumber(SECTION_CC_BILLING
, kTestCCNumberInvalid
, false);
867 TEST_F(AutofillDialogControllerTest
, AutofillProfiles
) {
869 ui::MenuModel
* shipping_model
=
870 controller()->MenuModelForSection(SECTION_SHIPPING
);
871 // Since the PersonalDataManager is empty, this should only have the
872 // "use billing", "add new" and "manage" menu items.
873 ASSERT_TRUE(shipping_model
);
874 EXPECT_EQ(3, shipping_model
->GetItemCount());
875 // On the other hand, the other models should be NULL when there's no
877 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC
));
878 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_BILLING
));
880 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(3);
882 // Empty profiles are ignored.
883 AutofillProfile
empty_profile(base::GenerateGUID(), kSettingsOrigin
);
884 empty_profile
.SetRawInfo(NAME_FULL
, ASCIIToUTF16("John Doe"));
885 controller()->GetTestingManager()->AddTestingProfile(&empty_profile
);
886 shipping_model
= controller()->MenuModelForSection(SECTION_SHIPPING
);
887 ASSERT_TRUE(shipping_model
);
888 EXPECT_EQ(3, shipping_model
->GetItemCount());
890 // An otherwise full but unverified profile should be ignored.
891 AutofillProfile
full_profile(test::GetFullProfile());
892 full_profile
.set_origin("https://www.example.com");
893 full_profile
.SetRawInfo(ADDRESS_HOME_LINE2
, base::string16());
894 controller()->GetTestingManager()->AddTestingProfile(&full_profile
);
895 shipping_model
= controller()->MenuModelForSection(SECTION_SHIPPING
);
896 ASSERT_TRUE(shipping_model
);
897 EXPECT_EQ(3, shipping_model
->GetItemCount());
899 // A full, verified profile should be picked up.
900 AutofillProfile
verified_profile(test::GetVerifiedProfile());
901 verified_profile
.SetRawInfo(ADDRESS_HOME_LINE2
, base::string16());
902 controller()->GetTestingManager()->AddTestingProfile(&verified_profile
);
903 shipping_model
= controller()->MenuModelForSection(SECTION_SHIPPING
);
904 ASSERT_TRUE(shipping_model
);
905 EXPECT_EQ(4, shipping_model
->GetItemCount());
908 // Makes sure that the choice of which Autofill profile to use for each section
910 TEST_F(AutofillDialogControllerTest
, AutofillProfileDefaults
) {
912 AutofillProfile
profile(test::GetVerifiedProfile());
913 AutofillProfile
profile2(test::GetVerifiedProfile2());
914 controller()->GetTestingManager()->AddTestingProfile(&profile
);
915 controller()->GetTestingManager()->AddTestingProfile(&profile2
);
917 // Until a selection has been made, the default shipping suggestion is the
918 // first one (after "use billing").
919 SuggestionsMenuModel
* shipping_model
=
920 GetMenuModelForSection(SECTION_SHIPPING
);
921 EXPECT_EQ(1, shipping_model
->checked_item());
923 for (int i
= 2; i
>= 0; --i
) {
924 shipping_model
= GetMenuModelForSection(SECTION_SHIPPING
);
925 shipping_model
->ExecuteCommand(i
, 0);
926 FillCreditCardInputs();
927 controller()->OnAccept();
930 controller()->GetTestingManager()->AddTestingProfile(&profile
);
931 controller()->GetTestingManager()->AddTestingProfile(&profile2
);
932 shipping_model
= GetMenuModelForSection(SECTION_SHIPPING
);
933 EXPECT_EQ(i
, shipping_model
->checked_item());
936 // Try again, but don't add the default profile to the PDM. The dialog
937 // should fall back to the first profile.
938 shipping_model
->ExecuteCommand(2, 0);
939 FillCreditCardInputs();
940 controller()->OnAccept();
942 controller()->GetTestingManager()->AddTestingProfile(&profile
);
943 shipping_model
= GetMenuModelForSection(SECTION_SHIPPING
);
944 EXPECT_EQ(1, shipping_model
->checked_item());
947 // Makes sure that a newly added Autofill profile becomes set as the default
948 // choice for the next run.
949 TEST_F(AutofillDialogControllerTest
, NewAutofillProfileIsDefault
) {
952 AutofillProfile
profile(test::GetVerifiedProfile());
953 CreditCard
credit_card(test::GetVerifiedCreditCard());
954 controller()->GetTestingManager()->AddTestingProfile(&profile
);
955 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card
);
957 // Until a selection has been made, the default suggestion is the first one.
958 // For the shipping section, this follows the "use billing" suggestion.
959 EXPECT_EQ(0, GetMenuModelForSection(SECTION_CC
)->checked_item());
960 EXPECT_EQ(1, GetMenuModelForSection(SECTION_SHIPPING
)->checked_item());
962 // Fill in the shipping and credit card sections with new data.
963 AutofillProfile
new_profile(test::GetVerifiedProfile2());
964 CreditCard
new_credit_card(test::GetVerifiedCreditCard2());
965 FillInputs(SECTION_SHIPPING
, new_profile
);
966 FillInputs(SECTION_CC
, new_credit_card
);
967 controller()->GetView()->CheckSaveDetailsLocallyCheckbox(true);
968 controller()->OnAccept();
970 // Update the |new_profile| and |new_credit_card|'s guids to the saved ones.
971 new_profile
.set_guid(
972 controller()->GetTestingManager()->imported_profile().guid());
973 new_credit_card
.set_guid(
974 controller()->GetTestingManager()->imported_credit_card().guid());
976 // Reload the dialog. The newly added address and credit card should now be
977 // set as the defaults.
979 controller()->GetTestingManager()->AddTestingProfile(&profile
);
980 controller()->GetTestingManager()->AddTestingProfile(&new_profile
);
981 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card
);
982 controller()->GetTestingManager()->AddTestingCreditCard(&new_credit_card
);
984 // Until a selection has been made, the default suggestion is the first one.
985 // For the shipping section, this follows the "use billing" suggestion.
986 EXPECT_EQ(1, GetMenuModelForSection(SECTION_CC
)->checked_item());
987 EXPECT_EQ(2, GetMenuModelForSection(SECTION_SHIPPING
)->checked_item());
990 TEST_F(AutofillDialogControllerTest
, AutofillProfileVariants
) {
992 EXPECT_CALL(*controller()->GetView(), ModelChanged());
993 ui::MenuModel
* shipping_model
=
994 controller()->MenuModelForSection(SECTION_SHIPPING
);
995 ASSERT_TRUE(!!shipping_model
);
996 EXPECT_EQ(3, shipping_model
->GetItemCount());
998 // Set up some variant data.
999 AutofillProfile
full_profile(test::GetVerifiedProfile());
1000 std::vector
<base::string16
> names
;
1001 names
.push_back(ASCIIToUTF16("John Doe"));
1002 names
.push_back(ASCIIToUTF16("Jane Doe"));
1003 full_profile
.SetRawMultiInfo(NAME_FULL
, names
);
1004 std::vector
<base::string16
> emails
;
1005 emails
.push_back(ASCIIToUTF16(kFakeEmail
));
1006 emails
.push_back(ASCIIToUTF16("admin@example.com"));
1007 full_profile
.SetRawMultiInfo(EMAIL_ADDRESS
, emails
);
1009 // Non-default variants are ignored by the dialog.
1010 controller()->GetTestingManager()->AddTestingProfile(&full_profile
);
1011 EXPECT_EQ(4, shipping_model
->GetItemCount());
1014 TEST_F(AutofillDialogControllerTest
, SuggestValidEmail
) {
1016 AutofillProfile
profile(test::GetVerifiedProfile());
1017 const base::string16 kValidEmail
= ASCIIToUTF16(kFakeEmail
);
1018 profile
.SetRawInfo(EMAIL_ADDRESS
, kValidEmail
);
1019 controller()->GetTestingManager()->AddTestingProfile(&profile
);
1021 // "add", "manage", and 1 suggestion.
1023 3, controller()->MenuModelForSection(SECTION_BILLING
)->GetItemCount());
1024 // "add", "manage", 1 suggestion, and "same as billing".
1026 4, controller()->MenuModelForSection(SECTION_SHIPPING
)->GetItemCount());
1029 TEST_F(AutofillDialogControllerTest
, DoNotSuggestInvalidEmail
) {
1031 AutofillProfile
profile(test::GetVerifiedProfile());
1032 profile
.SetRawInfo(EMAIL_ADDRESS
, ASCIIToUTF16(".!#$%&'*+/=?^_`-@-.."));
1033 controller()->GetTestingManager()->AddTestingProfile(&profile
);
1035 EXPECT_FALSE(!!controller()->MenuModelForSection(SECTION_BILLING
));
1036 // "add", "manage", 1 suggestion, and "same as billing".
1038 4, controller()->MenuModelForSection(SECTION_SHIPPING
)->GetItemCount());
1041 TEST_F(AutofillDialogControllerTest
, SuggestValidAddress
) {
1043 AutofillProfile
full_profile(test::GetVerifiedProfile());
1044 full_profile
.set_origin(kSettingsOrigin
);
1045 controller()->GetTestingManager()->AddTestingProfile(&full_profile
);
1046 // "add", "manage", and 1 suggestion.
1048 3, controller()->MenuModelForSection(SECTION_BILLING
)->GetItemCount());
1051 TEST_F(AutofillDialogControllerTest
, DoNotSuggestInvalidAddress
) {
1053 AutofillProfile
full_profile(test::GetVerifiedProfile());
1054 full_profile
.set_origin(kSettingsOrigin
);
1055 full_profile
.SetRawInfo(ADDRESS_HOME_STATE
, ASCIIToUTF16("C"));
1056 controller()->GetTestingManager()->AddTestingProfile(&full_profile
);
1058 EXPECT_FALSE(!!controller()->MenuModelForSection(SECTION_BILLING
));
1061 TEST_F(AutofillDialogControllerTest
, DoNotSuggestIncompleteAddress
) {
1063 AutofillProfile
profile(test::GetVerifiedProfile());
1064 profile
.SetRawInfo(ADDRESS_HOME_STATE
, base::string16());
1065 controller()->GetTestingManager()->AddTestingProfile(&profile
);
1067 EXPECT_FALSE(!!controller()->MenuModelForSection(SECTION_BILLING
));
1070 TEST_F(AutofillDialogControllerTest
, AutofillCreditCards
) {
1072 // Since the PersonalDataManager is empty, this should only have the
1073 // default menu items.
1074 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC
));
1076 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(3);
1078 // Empty cards are ignored.
1079 CreditCard
empty_card(base::GenerateGUID(), kSettingsOrigin
);
1080 empty_card
.SetRawInfo(CREDIT_CARD_NAME
, ASCIIToUTF16("John Doe"));
1081 controller()->GetTestingManager()->AddTestingCreditCard(&empty_card
);
1082 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC
));
1084 // An otherwise full but unverified card should be ignored.
1085 CreditCard
full_card(test::GetCreditCard());
1086 full_card
.set_origin("https://www.example.com");
1087 controller()->GetTestingManager()->AddTestingCreditCard(&full_card
);
1088 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC
));
1090 // A full, verified card should be picked up.
1091 CreditCard
verified_card(test::GetCreditCard());
1092 verified_card
.set_origin(kSettingsOrigin
);
1093 controller()->GetTestingManager()->AddTestingCreditCard(&verified_card
);
1094 ui::MenuModel
* credit_card_model
=
1095 controller()->MenuModelForSection(SECTION_CC
);
1096 ASSERT_TRUE(credit_card_model
);
1097 EXPECT_EQ(3, credit_card_model
->GetItemCount());
1100 // Test selecting a shipping address different from billing as address.
1101 TEST_F(AutofillDialogControllerTest
, DontUseBillingAsShipping
) {
1103 AutofillProfile
full_profile(test::GetVerifiedProfile());
1104 AutofillProfile
full_profile2(test::GetVerifiedProfile2());
1105 CreditCard
credit_card(test::GetVerifiedCreditCard());
1106 controller()->GetTestingManager()->AddTestingProfile(&full_profile
);
1107 controller()->GetTestingManager()->AddTestingProfile(&full_profile2
);
1108 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card
);
1109 ui::MenuModel
* shipping_model
=
1110 controller()->MenuModelForSection(SECTION_SHIPPING
);
1111 shipping_model
->ActivatedAt(2);
1113 controller()->OnAccept();
1114 ASSERT_EQ(20U, form_structure()->field_count());
1115 EXPECT_EQ(ADDRESS_HOME_STATE
,
1116 form_structure()->field(9)->Type().GetStorableType());
1117 EXPECT_EQ(ADDRESS_BILLING
, form_structure()->field(9)->Type().group());
1118 EXPECT_EQ(ADDRESS_HOME_STATE
,
1119 form_structure()->field(16)->Type().GetStorableType());
1120 EXPECT_EQ(ADDRESS_HOME
, form_structure()->field(16)->Type().group());
1121 base::string16 billing_state
= form_structure()->field(9)->value
;
1122 base::string16 shipping_state
= form_structure()->field(16)->value
;
1123 EXPECT_FALSE(billing_state
.empty());
1124 EXPECT_FALSE(shipping_state
.empty());
1125 EXPECT_NE(billing_state
, shipping_state
);
1127 EXPECT_EQ(CREDIT_CARD_NAME
,
1128 form_structure()->field(1)->Type().GetStorableType());
1129 base::string16 cc_name
= form_structure()->field(1)->value
;
1130 EXPECT_EQ(NAME_FULL
, form_structure()->field(6)->Type().GetStorableType());
1131 EXPECT_EQ(NAME_BILLING
, form_structure()->field(6)->Type().group());
1132 base::string16 billing_name
= form_structure()->field(6)->value
;
1133 EXPECT_EQ(NAME_FULL
, form_structure()->field(13)->Type().GetStorableType());
1134 EXPECT_EQ(NAME
, form_structure()->field(13)->Type().group());
1135 base::string16 shipping_name
= form_structure()->field(13)->value
;
1137 EXPECT_FALSE(cc_name
.empty());
1138 EXPECT_FALSE(billing_name
.empty());
1139 EXPECT_FALSE(shipping_name
.empty());
1140 // Billing name should always be the same as cardholder name.
1141 EXPECT_EQ(cc_name
, billing_name
);
1142 EXPECT_NE(cc_name
, shipping_name
);
1145 // Test selecting UseBillingForShipping.
1146 TEST_F(AutofillDialogControllerTest
, UseBillingAsShipping
) {
1149 AutofillProfile
full_profile(test::GetVerifiedProfile());
1150 controller()->GetTestingManager()->AddTestingProfile(&full_profile
);
1152 AutofillProfile
full_profile2(test::GetVerifiedProfile2());
1153 controller()->GetTestingManager()->AddTestingProfile(&full_profile2
);
1155 CreditCard
credit_card(test::GetVerifiedCreditCard());
1156 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card
);
1158 ASSERT_FALSE(controller()->IsManuallyEditingSection(SECTION_CC
));
1159 ASSERT_FALSE(controller()->IsManuallyEditingSection(SECTION_BILLING
));
1161 SubmitAndVerifyShippingAndBillingResults();
1164 TEST_F(AutofillDialogControllerTest
, UseBillingAsShippingManualInput
) {
1167 ASSERT_TRUE(controller()->IsManuallyEditingSection(SECTION_CC
));
1168 ASSERT_TRUE(controller()->IsManuallyEditingSection(SECTION_BILLING
));
1170 CreditCard
credit_card(test::GetVerifiedCreditCard());
1171 FillInputs(SECTION_CC
, credit_card
);
1173 AutofillProfile
full_profile(test::GetVerifiedProfile());
1174 FillInputs(SECTION_BILLING
, full_profile
);
1176 SubmitAndVerifyShippingAndBillingResults();
1179 // Tests that shipping and billing telephone fields are supported, and filled
1180 // in by their respective profiles. http://crbug.com/244515
1181 TEST_F(AutofillDialogControllerTest
, BillingVsShippingPhoneNumber
) {
1182 FormFieldData shipping_tel
;
1183 shipping_tel
.autocomplete_attribute
= "shipping tel";
1184 FormFieldData billing_tel
;
1185 billing_tel
.autocomplete_attribute
= "billing tel";
1188 form_data
.fields
.push_back(shipping_tel
);
1189 form_data
.fields
.push_back(billing_tel
);
1190 SetUpControllerWithFormData(form_data
);
1194 // The profile that will be chosen for the shipping section.
1195 AutofillProfile
shipping_profile(test::GetVerifiedProfile());
1196 // The profile that will be chosen for the billing section.
1197 AutofillProfile
billing_profile(test::GetVerifiedProfile2());
1198 CreditCard
credit_card(test::GetVerifiedCreditCard());
1199 controller()->GetTestingManager()->AddTestingProfile(&shipping_profile
);
1200 controller()->GetTestingManager()->AddTestingProfile(&billing_profile
);
1201 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card
);
1202 ui::MenuModel
* billing_model
=
1203 controller()->MenuModelForSection(SECTION_BILLING
);
1204 billing_model
->ActivatedAt(1);
1206 controller()->OnAccept();
1207 ASSERT_EQ(2U, form_structure()->field_count());
1208 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER
,
1209 form_structure()->field(0)->Type().GetStorableType());
1210 EXPECT_EQ(PHONE_HOME
, form_structure()->field(0)->Type().group());
1211 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER
,
1212 form_structure()->field(1)->Type().GetStorableType());
1213 EXPECT_EQ(PHONE_BILLING
, form_structure()->field(1)->Type().group());
1214 EXPECT_EQ(shipping_profile
.GetRawInfo(PHONE_HOME_WHOLE_NUMBER
),
1215 form_structure()->field(0)->value
);
1216 EXPECT_EQ(billing_profile
.GetRawInfo(PHONE_HOME_WHOLE_NUMBER
),
1217 form_structure()->field(1)->value
);
1218 EXPECT_NE(form_structure()->field(1)->value
,
1219 form_structure()->field(0)->value
);
1222 // Similar to the above, but tests that street-address (i.e. all lines of the
1223 // street address) is successfully filled for both shipping and billing
1225 TEST_F(AutofillDialogControllerTest
, BillingVsShippingStreetAddress
) {
1226 FormFieldData shipping_address
;
1227 shipping_address
.autocomplete_attribute
= "shipping street-address";
1228 FormFieldData billing_address
;
1229 billing_address
.autocomplete_attribute
= "billing street-address";
1230 FormFieldData shipping_address_textarea
;
1231 shipping_address_textarea
.autocomplete_attribute
= "shipping street-address";
1232 shipping_address_textarea
.form_control_type
= "textarea";
1233 FormFieldData billing_address_textarea
;
1234 billing_address_textarea
.autocomplete_attribute
= "billing street-address";
1235 billing_address_textarea
.form_control_type
= "textarea";
1238 form_data
.fields
.push_back(shipping_address
);
1239 form_data
.fields
.push_back(billing_address
);
1240 form_data
.fields
.push_back(shipping_address_textarea
);
1241 form_data
.fields
.push_back(billing_address_textarea
);
1242 SetUpControllerWithFormData(form_data
);
1246 // The profile that will be chosen for the shipping section.
1247 AutofillProfile
shipping_profile(test::GetVerifiedProfile());
1248 // The profile that will be chosen for the billing section.
1249 AutofillProfile
billing_profile(test::GetVerifiedProfile2());
1250 CreditCard
credit_card(test::GetVerifiedCreditCard());
1251 controller()->GetTestingManager()->AddTestingProfile(&shipping_profile
);
1252 controller()->GetTestingManager()->AddTestingProfile(&billing_profile
);
1253 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card
);
1254 ui::MenuModel
* billing_model
=
1255 controller()->MenuModelForSection(SECTION_BILLING
);
1256 billing_model
->ActivatedAt(1);
1258 controller()->OnAccept();
1259 ASSERT_EQ(4U, form_structure()->field_count());
1260 EXPECT_EQ(ADDRESS_HOME_STREET_ADDRESS
,
1261 form_structure()->field(0)->Type().GetStorableType());
1262 EXPECT_EQ(ADDRESS_HOME
, form_structure()->field(0)->Type().group());
1263 EXPECT_EQ(ADDRESS_HOME_STREET_ADDRESS
,
1264 form_structure()->field(1)->Type().GetStorableType());
1265 EXPECT_EQ(ADDRESS_BILLING
, form_structure()->field(1)->Type().group());
1266 // Inexact matching; single-line inputs get the address data concatenated but
1267 // separated by commas.
1268 EXPECT_TRUE(StartsWith(form_structure()->field(0)->value
,
1269 shipping_profile
.GetRawInfo(ADDRESS_HOME_LINE1
),
1271 EXPECT_TRUE(EndsWith(form_structure()->field(0)->value
,
1272 shipping_profile
.GetRawInfo(ADDRESS_HOME_LINE2
),
1274 EXPECT_TRUE(StartsWith(form_structure()->field(1)->value
,
1275 billing_profile
.GetRawInfo(ADDRESS_HOME_LINE1
),
1277 EXPECT_TRUE(EndsWith(form_structure()->field(1)->value
,
1278 billing_profile
.GetRawInfo(ADDRESS_HOME_LINE2
),
1280 // The textareas should be an exact match.
1281 EXPECT_EQ(shipping_profile
.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS
),
1282 form_structure()->field(2)->value
);
1283 EXPECT_EQ(billing_profile
.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS
),
1284 form_structure()->field(3)->value
);
1286 EXPECT_NE(form_structure()->field(1)->value
,
1287 form_structure()->field(0)->value
);
1288 EXPECT_NE(form_structure()->field(3)->value
,
1289 form_structure()->field(2)->value
);
1292 TEST_F(AutofillDialogControllerTest
, AcceptLegalDocuments
) {
1293 for (size_t i
= 0; i
< 2; ++i
) {
1294 SCOPED_TRACE(testing::Message() << "Case " << i
);
1296 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1297 AcceptLegalDocuments(_
, _
));
1298 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_
));
1299 EXPECT_CALL(*controller(), LoadRiskFingerprintData());
1301 EXPECT_TRUE(controller()->LegalDocumentLinks().empty());
1302 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
1303 EXPECT_TRUE(controller()->LegalDocumentLinks().empty());
1305 scoped_ptr
<wallet::WalletItems
> wallet_items
=
1306 CompleteAndValidWalletItems();
1307 wallet_items
->AddLegalDocument(wallet::GetTestLegalDocument());
1308 wallet_items
->AddLegalDocument(wallet::GetTestLegalDocument());
1309 controller()->OnDidGetWalletItems(wallet_items
.Pass());
1310 EXPECT_FALSE(controller()->LegalDocumentLinks().empty());
1312 controller()->OnAccept();
1313 controller()->OnDidAcceptLegalDocuments();
1314 controller()->OnDidLoadRiskFingerprintData(GetFakeFingerprint().Pass());
1316 // Now try it all over again with the location disclosure already accepted.
1317 // Nothing should change.
1319 base::ListValue preexisting_list
;
1320 preexisting_list
.AppendString(kFakeEmail
);
1321 g_browser_process
->local_state()->Set(
1322 ::prefs::kAutofillDialogWalletLocationAcceptance
,
1327 TEST_F(AutofillDialogControllerTest
, RejectLegalDocuments
) {
1328 for (size_t i
= 0; i
< 2; ++i
) {
1329 SCOPED_TRACE(testing::Message() << "Case " << i
);
1331 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1332 AcceptLegalDocuments(_
, _
)).Times(0);
1334 scoped_ptr
<wallet::WalletItems
> wallet_items
=
1335 CompleteAndValidWalletItems();
1336 wallet_items
->AddLegalDocument(wallet::GetTestLegalDocument());
1337 wallet_items
->AddLegalDocument(wallet::GetTestLegalDocument());
1338 controller()->OnDidGetWalletItems(wallet_items
.Pass());
1339 EXPECT_FALSE(controller()->LegalDocumentLinks().empty());
1341 controller()->OnCancel();
1343 // Now try it all over again with the location disclosure already accepted.
1344 // Nothing should change.
1346 base::ListValue preexisting_list
;
1347 preexisting_list
.AppendString(kFakeEmail
);
1348 g_browser_process
->local_state()->Set(
1349 ::prefs::kAutofillDialogWalletLocationAcceptance
,
1354 TEST_F(AutofillDialogControllerTest
, AcceptLocationDisclosure
) {
1355 // Check that accepting the dialog registers the user's name in the list
1356 // of users who have accepted the geolocation terms.
1357 EXPECT_TRUE(g_browser_process
->local_state()->GetList(
1358 ::prefs::kAutofillDialogWalletLocationAcceptance
)->empty());
1360 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
1361 EXPECT_FALSE(controller()->LegalDocumentsText().empty());
1362 EXPECT_TRUE(controller()->LegalDocumentLinks().empty());
1363 controller()->OnAccept();
1365 const base::ListValue
* list
= g_browser_process
->local_state()->GetList(
1366 ::prefs::kAutofillDialogWalletLocationAcceptance
);
1367 ASSERT_EQ(1U, list
->GetSize());
1368 std::string accepted_username
;
1369 EXPECT_TRUE(list
->GetString(0, &accepted_username
));
1370 EXPECT_EQ(kFakeEmail
, accepted_username
);
1372 // Now check it still works if that list starts off with some other username
1375 list
= g_browser_process
->local_state()->GetList(
1376 ::prefs::kAutofillDialogWalletLocationAcceptance
);
1377 ASSERT_TRUE(list
->empty());
1379 std::string
kOtherUsername("spouse@example.com");
1380 base::ListValue preexisting_list
;
1381 preexisting_list
.AppendString(kOtherUsername
);
1382 g_browser_process
->local_state()->Set(
1383 ::prefs::kAutofillDialogWalletLocationAcceptance
,
1386 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
1387 EXPECT_FALSE(controller()->LegalDocumentsText().empty());
1388 EXPECT_TRUE(controller()->LegalDocumentLinks().empty());
1389 controller()->OnAccept();
1391 list
= g_browser_process
->local_state()->GetList(
1392 ::prefs::kAutofillDialogWalletLocationAcceptance
);
1393 ASSERT_EQ(2U, list
->GetSize());
1394 EXPECT_NE(list
->end(), list
->Find(base::StringValue(kFakeEmail
)));
1395 EXPECT_NE(list
->end(), list
->Find(base::StringValue(kOtherUsername
)));
1397 // Now check the list doesn't change if the user cancels out of the dialog.
1399 list
= g_browser_process
->local_state()->GetList(
1400 ::prefs::kAutofillDialogWalletLocationAcceptance
);
1401 ASSERT_TRUE(list
->empty());
1403 g_browser_process
->local_state()->Set(
1404 ::prefs::kAutofillDialogWalletLocationAcceptance
,
1407 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
1408 EXPECT_FALSE(controller()->LegalDocumentsText().empty());
1409 EXPECT_TRUE(controller()->LegalDocumentLinks().empty());
1410 controller()->OnCancel();
1412 list
= g_browser_process
->local_state()->GetList(
1413 ::prefs::kAutofillDialogWalletLocationAcceptance
);
1414 ASSERT_EQ(1U, list
->GetSize());
1415 EXPECT_NE(list
->end(), list
->Find(base::StringValue(kOtherUsername
)));
1416 EXPECT_EQ(list
->end(), list
->Find(base::StringValue(kFakeEmail
)));
1419 TEST_F(AutofillDialogControllerTest
, LegalDocumentOverflow
) {
1420 for (size_t number_of_docs
= 2; number_of_docs
< 11; ++number_of_docs
) {
1421 scoped_ptr
<wallet::WalletItems
> wallet_items
=
1422 CompleteAndValidWalletItems();
1423 for (size_t i
= 0; i
< number_of_docs
; ++i
)
1424 wallet_items
->AddLegalDocument(wallet::GetTestLegalDocument());
1427 controller()->OnDidGetWalletItems(wallet_items
.Pass());
1429 // The dialog is only equipped to handle 2-6 legal documents. More than
1431 if (number_of_docs
<= 6U) {
1432 EXPECT_FALSE(controller()->LegalDocumentsText().empty());
1434 EXPECT_TRUE(controller()->LegalDocumentsText().empty());
1435 EXPECT_EQ(1U, NotificationsOfType(
1436 DialogNotification::WALLET_ERROR
).size());
1440 controller()->OnCancel();
1443 // Makes sure the default object IDs are respected.
1444 TEST_F(AutofillDialogControllerTest
, WalletDefaultItems
) {
1445 scoped_ptr
<wallet::WalletItems
> wallet_items
=
1446 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
1447 wallet_items
->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1448 wallet_items
->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1449 wallet_items
->AddInstrument(wallet::GetTestMaskedInstrument());
1450 wallet_items
->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1452 wallet_items
->AddAddress(wallet::GetTestNonDefaultShippingAddress());
1453 wallet_items
->AddAddress(wallet::GetTestNonDefaultShippingAddress());
1454 wallet_items
->AddAddress(wallet::GetTestNonDefaultShippingAddress());
1455 wallet_items
->AddAddress(wallet::GetTestShippingAddress());
1456 wallet_items
->AddAddress(wallet::GetTestNonDefaultShippingAddress());
1458 controller()->OnDidGetWalletItems(wallet_items
.Pass());
1459 // "add", "manage", and 4 suggestions.
1461 controller()->MenuModelForSection(SECTION_CC_BILLING
)->GetItemCount());
1462 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING
)->
1463 IsItemCheckedAt(2));
1464 ASSERT_FALSE(controller()->IsEditingExistingData(SECTION_CC_BILLING
));
1465 // "use billing", "add", "manage", and 5 suggestions.
1467 controller()->MenuModelForSection(SECTION_SHIPPING
)->GetItemCount());
1468 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_SHIPPING
)->
1469 IsItemCheckedAt(4));
1470 ASSERT_FALSE(controller()->IsEditingExistingData(SECTION_SHIPPING
));
1473 // Tests that invalid and AMEX default instruments are ignored.
1474 TEST_F(AutofillDialogControllerTest
, SelectInstrument
) {
1475 scoped_ptr
<wallet::WalletItems
> wallet_items
=
1476 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
1477 // Tests if default instrument is invalid, then, the first valid instrument is
1478 // selected instead of the default instrument.
1479 wallet_items
->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1480 wallet_items
->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1481 wallet_items
->AddInstrument(wallet::GetTestMaskedInstrumentInvalid());
1482 wallet_items
->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1484 controller()->OnDidGetWalletItems(wallet_items
.Pass());
1485 // 4 suggestions and "add", "manage".
1487 controller()->MenuModelForSection(SECTION_CC_BILLING
)->GetItemCount());
1488 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING
)->
1489 IsItemCheckedAt(0));
1491 // Tests if default instrument is AMEX but Wallet doesn't support
1492 // AMEX on this merchant, then the first valid instrument is
1493 // selected instead of the default instrument.
1494 wallet_items
= wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
1495 wallet_items
->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1496 wallet_items
->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1497 wallet_items
->AddInstrument(
1498 wallet::GetTestMaskedInstrumentAmex(wallet::AMEX_DISALLOWED
));
1499 wallet_items
->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1501 controller()->OnDidGetWalletItems(wallet_items
.Pass());
1502 // 4 suggestions and "add", "manage".
1504 controller()->MenuModelForSection(SECTION_CC_BILLING
)->GetItemCount());
1505 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING
)->
1506 IsItemCheckedAt(0));
1508 // Tests if default instrument is AMEX and it is allowed on this merchant,
1509 // then it is selected.
1510 wallet_items
= wallet::GetTestWalletItems(wallet::AMEX_ALLOWED
);
1511 wallet_items
->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1512 wallet_items
->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1513 wallet_items
->AddInstrument(
1514 wallet::GetTestMaskedInstrumentAmex(wallet::AMEX_ALLOWED
));
1515 wallet_items
->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1517 controller()->OnDidGetWalletItems(wallet_items
.Pass());
1518 // 4 suggestions and "add", "manage".
1520 controller()->MenuModelForSection(SECTION_CC_BILLING
)->GetItemCount());
1521 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING
)->
1522 IsItemCheckedAt(2));
1524 // Tests if only have AMEX and invalid instrument, then "add" is selected.
1525 wallet_items
= wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
1526 wallet_items
->AddInstrument(wallet::GetTestMaskedInstrumentInvalid());
1527 wallet_items
->AddInstrument(
1528 wallet::GetTestMaskedInstrumentAmex(wallet::AMEX_DISALLOWED
));
1530 controller()->OnDidGetWalletItems(wallet_items
.Pass());
1531 // 2 suggestions and "add", "manage".
1533 controller()->MenuModelForSection(SECTION_CC_BILLING
)->GetItemCount());
1535 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING
)->
1536 IsItemCheckedAt(2));
1539 TEST_F(AutofillDialogControllerTest
, SaveAddress
) {
1540 EXPECT_CALL(*controller()->GetView(), ModelChanged());
1541 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1542 SaveToWalletMock(testing::IsNull(),
1545 testing::IsNull()));
1547 scoped_ptr
<wallet::WalletItems
> wallet_items
=
1548 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
1549 wallet_items
->AddInstrument(wallet::GetTestMaskedInstrument());
1550 controller()->OnDidGetWalletItems(wallet_items
.Pass());
1551 // If there is no shipping address in wallet, it will default to
1552 // "same-as-billing" instead of "add-new-item". "same-as-billing" is covered
1553 // by the following tests. The penultimate item in the menu is "add-new-item".
1554 ui::MenuModel
* shipping_model
=
1555 controller()->MenuModelForSection(SECTION_SHIPPING
);
1556 shipping_model
->ActivatedAt(shipping_model
->GetItemCount() - 2);
1558 AutofillProfile
test_profile(test::GetVerifiedProfile());
1559 FillInputs(SECTION_SHIPPING
, test_profile
);
1561 AcceptAndLoadFakeFingerprint();
1564 TEST_F(AutofillDialogControllerTest
, SaveInstrument
) {
1565 EXPECT_CALL(*controller()->GetView(), ModelChanged());
1566 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1567 SaveToWalletMock(testing::NotNull(),
1570 testing::IsNull()));
1572 FillCCBillingInputs();
1573 scoped_ptr
<wallet::WalletItems
> wallet_items
=
1574 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
1575 wallet_items
->AddAddress(wallet::GetTestShippingAddress());
1576 SubmitWithWalletItems(wallet_items
.Pass());
1579 TEST_F(AutofillDialogControllerTest
, SaveInstrumentWithInvalidInstruments
) {
1580 EXPECT_CALL(*controller()->GetView(), ModelChanged());
1581 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1582 SaveToWalletMock(testing::NotNull(),
1585 testing::IsNull()));
1587 FillCCBillingInputs();
1588 scoped_ptr
<wallet::WalletItems
> wallet_items
=
1589 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
1590 wallet_items
->AddAddress(wallet::GetTestShippingAddress());
1591 wallet_items
->AddInstrument(wallet::GetTestMaskedInstrumentInvalid());
1592 SubmitWithWalletItems(wallet_items
.Pass());
1595 TEST_F(AutofillDialogControllerTest
, SaveInstrumentAndAddress
) {
1596 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1597 SaveToWalletMock(testing::NotNull(),
1600 testing::IsNull()));
1602 FillCCBillingInputs();
1603 scoped_ptr
<wallet::WalletItems
> wallet_items
=
1604 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
1605 SubmitWithWalletItems(wallet_items
.Pass());
1608 MATCHER(IsUpdatingExistingData
, "updating existing Wallet data") {
1609 return !arg
->object_id().empty();
1612 MATCHER(UsesLocalBillingAddress
, "uses the local billing address") {
1613 return arg
->address_line_1() == ASCIIToUTF16(kEditedBillingAddress
);
1616 // Tests that when using billing address for shipping, and there is no exact
1617 // matched shipping address, then a shipping address should be added.
1618 TEST_F(AutofillDialogControllerTest
, BillingForShipping
) {
1619 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1620 SaveToWalletMock(testing::IsNull(),
1623 testing::IsNull()));
1625 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
1626 // Select "Same as billing" in the address menu.
1627 UseBillingForShipping();
1629 AcceptAndLoadFakeFingerprint();
1632 // Tests that when using billing address for shipping, and there is an exact
1633 // matched shipping address, then a shipping address should not be added.
1634 TEST_F(AutofillDialogControllerTest
, BillingForShippingHasMatch
) {
1635 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1636 SaveToWalletMock(_
, _
, _
, _
)).Times(0);
1638 scoped_ptr
<wallet::WalletItems
> wallet_items
=
1639 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
1640 scoped_ptr
<wallet::WalletItems::MaskedInstrument
> instrument
=
1641 wallet::GetTestMaskedInstrument();
1642 // Copy billing address as shipping address, and assign an id to it.
1643 scoped_ptr
<wallet::Address
> shipping_address(
1644 new wallet::Address(instrument
->address()));
1645 shipping_address
->set_object_id("shipping_address_id");
1646 wallet_items
->AddAddress(shipping_address
.Pass());
1647 wallet_items
->AddInstrument(instrument
.Pass());
1648 wallet_items
->AddAddress(wallet::GetTestShippingAddress());
1650 controller()->OnDidGetWalletItems(wallet_items
.Pass());
1651 // Select "Same as billing" in the address menu.
1652 UseBillingForShipping();
1654 AcceptAndLoadFakeFingerprint();
1657 // Test that the local view contents is used when saving a new instrument and
1658 // the user has selected "Same as billing".
1659 TEST_F(AutofillDialogControllerTest
, SaveInstrumentSameAsBilling
) {
1660 scoped_ptr
<wallet::WalletItems
> wallet_items
=
1661 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
1662 wallet_items
->AddInstrument(wallet::GetTestMaskedInstrument());
1663 controller()->OnDidGetWalletItems(wallet_items
.Pass());
1665 ui::MenuModel
* model
= controller()->MenuModelForSection(SECTION_CC_BILLING
);
1666 model
->ActivatedAt(model
->GetItemCount() - 2);
1668 FieldValueMap outputs
;
1669 const DetailInputs
& inputs
=
1670 controller()->RequestedFieldsForSection(SECTION_CC_BILLING
);
1671 AutofillProfile
full_profile(test::GetVerifiedProfile());
1672 CreditCard
full_card(test::GetCreditCard());
1673 for (size_t i
= 0; i
< inputs
.size(); ++i
) {
1674 const ServerFieldType type
= inputs
[i
].type
;
1675 if (type
== ADDRESS_BILLING_LINE1
)
1676 outputs
[type
] = ASCIIToUTF16(kEditedBillingAddress
);
1678 outputs
[type
] = full_profile
.GetInfo(AutofillType(type
), "en-US");
1680 if (outputs
[type
].empty())
1681 outputs
[type
] = full_card
.GetInfo(AutofillType(type
), "en-US");
1683 controller()->GetView()->SetUserInput(SECTION_CC_BILLING
, outputs
);
1685 controller()->OnAccept();
1687 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1688 SaveToWalletMock(testing::NotNull(),
1689 UsesLocalBillingAddress(),
1691 testing::IsNull()));
1692 AcceptAndLoadFakeFingerprint();
1695 TEST_F(AutofillDialogControllerTest
, CancelNoSave
) {
1696 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1697 SaveToWalletMock(_
, _
, _
, _
)).Times(0);
1699 EXPECT_CALL(*controller()->GetView(), ModelChanged());
1701 controller()->OnDidGetWalletItems(
1702 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
));
1703 controller()->OnCancel();
1706 // Checks that clicking the Manage menu item opens a new tab with a different
1707 // URL for Wallet and Autofill.
1708 TEST_F(AutofillDialogControllerTest
, ManageItem
) {
1709 AutofillProfile
full_profile(test::GetVerifiedProfile());
1710 full_profile
.set_origin(kSettingsOrigin
);
1711 full_profile
.SetRawInfo(ADDRESS_HOME_LINE2
, base::string16());
1712 controller()->GetTestingManager()->AddTestingProfile(&full_profile
);
1715 SuggestionsMenuModel
* shipping
= GetMenuModelForSection(SECTION_SHIPPING
);
1716 shipping
->ExecuteCommand(shipping
->GetItemCount() - 1, 0);
1717 GURL autofill_manage_url
= controller()->open_tab_url();
1718 EXPECT_EQ("chrome", autofill_manage_url
.scheme());
1721 scoped_ptr
<wallet::WalletItems
> wallet_items
=
1722 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
1723 wallet_items
->AddInstrument(wallet::GetTestMaskedInstrument());
1724 controller()->OnDidGetWalletItems(wallet_items
.Pass());
1726 controller()->SuggestionItemSelected(shipping
, shipping
->GetItemCount() - 1);
1727 GURL wallet_manage_addresses_url
= controller()->open_tab_url();
1728 EXPECT_EQ("https", wallet_manage_addresses_url
.scheme());
1730 SuggestionsMenuModel
* billing
= GetMenuModelForSection(SECTION_CC_BILLING
);
1731 controller()->SuggestionItemSelected(billing
, billing
->GetItemCount() - 1);
1732 GURL wallet_manage_instruments_url
= controller()->open_tab_url();
1733 EXPECT_EQ("https", wallet_manage_instruments_url
.scheme());
1735 EXPECT_NE(autofill_manage_url
, wallet_manage_instruments_url
);
1736 EXPECT_NE(wallet_manage_instruments_url
, wallet_manage_addresses_url
);
1739 // Tests that adding an autofill profile and then submitting works.
1740 TEST_F(AutofillDialogControllerTest
, AddAutofillProfile
) {
1742 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(2);
1744 AutofillProfile
full_profile(test::GetVerifiedProfile());
1745 CreditCard
credit_card(test::GetVerifiedCreditCard());
1746 controller()->GetTestingManager()->AddTestingProfile(&full_profile
);
1747 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card
);
1749 ui::MenuModel
* model
= controller()->MenuModelForSection(SECTION_BILLING
);
1750 // Activate the "Add billing address" menu item.
1751 model
->ActivatedAt(model
->GetItemCount() - 2);
1753 // Fill in the inputs from the profile.
1754 FieldValueMap outputs
;
1755 const DetailInputs
& inputs
=
1756 controller()->RequestedFieldsForSection(SECTION_BILLING
);
1757 AutofillProfile
full_profile2(test::GetVerifiedProfile2());
1758 for (size_t i
= 0; i
< inputs
.size(); ++i
) {
1759 const ServerFieldType type
= inputs
[i
].type
;
1760 outputs
[type
] = full_profile2
.GetInfo(AutofillType(type
), "en-US");
1762 controller()->GetView()->SetUserInput(SECTION_BILLING
, outputs
);
1764 controller()->OnAccept();
1765 const AutofillProfile
& added_profile
=
1766 controller()->GetTestingManager()->imported_profile();
1768 const DetailInputs
& shipping_inputs
=
1769 controller()->RequestedFieldsForSection(SECTION_SHIPPING
);
1770 for (size_t i
= 0; i
< shipping_inputs
.size(); ++i
) {
1771 const ServerFieldType type
= shipping_inputs
[i
].type
;
1772 EXPECT_EQ(full_profile2
.GetInfo(AutofillType(type
), "en-US"),
1773 added_profile
.GetInfo(AutofillType(type
), "en-US"));
1777 TEST_F(AutofillDialogControllerTest
, VerifyCvv
) {
1778 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_
));
1779 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1780 AuthenticateInstrument(_
, _
));
1782 SubmitWithWalletItems(CompleteAndValidWalletItems());
1784 EXPECT_TRUE(NotificationsOfType(DialogNotification::REQUIRED_ACTION
).empty());
1785 EXPECT_TRUE(controller()->SectionIsActive(SECTION_SHIPPING
));
1786 EXPECT_TRUE(controller()->SectionIsActive(SECTION_CC_BILLING
));
1787 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK
));
1788 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL
));
1790 SuggestionState suggestion_state
=
1791 controller()->SuggestionStateForSection(SECTION_CC_BILLING
);
1792 EXPECT_TRUE(suggestion_state
.extra_text
.empty());
1794 controller()->OnDidGetFullWallet(
1795 wallet::GetTestFullWalletWithRequiredActions(
1796 std::vector
<wallet::RequiredAction
>(1, wallet::VERIFY_CVV
)));
1797 ASSERT_TRUE(controller()->IsSubmitPausedOn(wallet::VERIFY_CVV
));
1800 NotificationsOfType(DialogNotification::REQUIRED_ACTION
).empty());
1801 EXPECT_FALSE(controller()->SectionIsActive(SECTION_SHIPPING
));
1802 EXPECT_TRUE(controller()->SectionIsActive(SECTION_CC_BILLING
));
1805 controller()->SuggestionStateForSection(SECTION_CC_BILLING
);
1806 EXPECT_FALSE(suggestion_state
.extra_text
.empty());
1807 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC_BILLING
));
1809 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK
));
1810 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL
));
1812 controller()->OnAccept();
1814 EXPECT_FALSE(controller()->GetDialogOverlay().image
.IsEmpty());
1817 TEST_F(AutofillDialogControllerTest
, ErrorDuringSubmit
) {
1818 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_
));
1820 SubmitWithWalletItems(CompleteAndValidWalletItems());
1822 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK
));
1823 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL
));
1825 controller()->OnWalletError(wallet::WalletClient::UNKNOWN_ERROR
);
1827 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK
));
1828 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL
));
1831 TEST_F(AutofillDialogControllerTest
, ErrorDuringVerifyCvv
) {
1832 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_
));
1834 SubmitWithWalletItems(CompleteAndValidWalletItems());
1835 controller()->OnDidGetFullWallet(
1836 wallet::GetTestFullWalletWithRequiredActions(
1837 std::vector
<wallet::RequiredAction
>(1, wallet::VERIFY_CVV
)));
1839 ASSERT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK
));
1840 ASSERT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL
));
1842 controller()->OnWalletError(wallet::WalletClient::UNKNOWN_ERROR
);
1844 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK
));
1845 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL
));
1848 // Simulates receiving an INVALID_FORM_FIELD required action while processing a
1849 // |WalletClientDelegate::OnDid{Save,Update}*()| call. This can happen if Online
1850 // Wallet's server validation differs from Chrome's local validation.
1851 TEST_F(AutofillDialogControllerTest
, WalletServerSideValidation
) {
1852 scoped_ptr
<wallet::WalletItems
> wallet_items
=
1853 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
1854 wallet_items
->AddInstrument(wallet::GetTestMaskedInstrument());
1855 controller()->OnDidGetWalletItems(wallet_items
.Pass());
1856 controller()->OnAccept();
1858 std::vector
<wallet::RequiredAction
> required_actions
;
1859 required_actions
.push_back(wallet::INVALID_FORM_FIELD
);
1861 std::vector
<wallet::FormFieldError
> form_errors
;
1862 form_errors
.push_back(
1863 wallet::FormFieldError(wallet::FormFieldError::INVALID_POSTAL_CODE
,
1864 wallet::FormFieldError::SHIPPING_ADDRESS
));
1866 EXPECT_CALL(*controller()->GetView(), UpdateForErrors());
1867 controller()->OnDidSaveToWallet(std::string(),
1873 // Simulates receiving unrecoverable Wallet server validation errors.
1874 TEST_F(AutofillDialogControllerTest
, WalletServerSideValidationUnrecoverable
) {
1875 scoped_ptr
<wallet::WalletItems
> wallet_items
=
1876 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
1877 wallet_items
->AddInstrument(wallet::GetTestMaskedInstrument());
1878 controller()->OnDidGetWalletItems(wallet_items
.Pass());
1879 controller()->OnAccept();
1881 std::vector
<wallet::RequiredAction
> required_actions
;
1882 required_actions
.push_back(wallet::INVALID_FORM_FIELD
);
1884 std::vector
<wallet::FormFieldError
> form_errors
;
1885 form_errors
.push_back(
1886 wallet::FormFieldError(wallet::FormFieldError::UNKNOWN_ERROR
,
1887 wallet::FormFieldError::UNKNOWN_LOCATION
));
1889 controller()->OnDidSaveToWallet(std::string(),
1894 EXPECT_EQ(1U, NotificationsOfType(
1895 DialogNotification::REQUIRED_ACTION
).size());
1898 // Test Wallet banners are show in the right situations. These banners promote
1899 // saving details into Wallet (i.e. "[x] Save details to Wallet").
1900 TEST_F(AutofillDialogControllerTest
, WalletBanners
) {
1901 // Simulate non-signed-in case.
1902 SetUpControllerWithFormData(DefaultFormData());
1903 GoogleServiceAuthError
error(GoogleServiceAuthError::NONE
);
1904 controller()->OnPassiveSigninFailure(error
);
1905 EXPECT_EQ(0U, NotificationsOfType(
1906 DialogNotification::WALLET_USAGE_CONFIRMATION
).size());
1908 // Sign in a user with a completed account.
1909 SetUpControllerWithFormData(DefaultFormData());
1910 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
1912 // Full account; should show "Details from Wallet" message.
1913 EXPECT_EQ(1U, NotificationsOfType(
1914 DialogNotification::WALLET_USAGE_CONFIRMATION
).size());
1916 EXPECT_EQ(1U, NotificationsOfType(
1917 DialogNotification::WALLET_USAGE_CONFIRMATION
).size());
1919 // Start over and sign in a user with an incomplete account.
1920 SetUpControllerWithFormData(DefaultFormData());
1921 scoped_ptr
<wallet::WalletItems
> wallet_items
=
1922 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
1923 wallet_items
->AddInstrument(wallet::GetTestMaskedInstrument());
1924 controller()->OnDidGetWalletItems(wallet_items
.Pass());
1927 EXPECT_EQ(1U, NotificationsOfType(
1928 DialogNotification::WALLET_USAGE_CONFIRMATION
).size());
1931 EXPECT_EQ(1U, NotificationsOfType(
1932 DialogNotification::WALLET_USAGE_CONFIRMATION
).size());
1934 // A Wallet error should kill any Wallet promos.
1935 controller()->OnWalletError(wallet::WalletClient::UNKNOWN_ERROR
);
1937 EXPECT_EQ(1U, NotificationsOfType(
1938 DialogNotification::WALLET_ERROR
).size());
1939 EXPECT_EQ(0U, NotificationsOfType(
1940 DialogNotification::WALLET_USAGE_CONFIRMATION
).size());
1943 TEST_F(AutofillDialogControllerTest
, ViewCancelDoesntSetPref
) {
1944 ASSERT_FALSE(profile()->GetPrefs()->HasPrefPath(
1945 ::prefs::kAutofillDialogPayWithoutWallet
));
1949 controller()->OnCancel();
1950 controller()->ViewClosed();
1952 EXPECT_FALSE(profile()->GetPrefs()->HasPrefPath(
1953 ::prefs::kAutofillDialogPayWithoutWallet
));
1956 TEST_F(AutofillDialogControllerTest
, SubmitWithSigninErrorDoesntSetPref
) {
1957 ASSERT_FALSE(profile()->GetPrefs()->HasPrefPath(
1958 ::prefs::kAutofillDialogPayWithoutWallet
));
1960 SimulateSigninError();
1961 FillCreditCardInputs();
1962 controller()->OnAccept();
1964 EXPECT_FALSE(profile()->GetPrefs()->HasPrefPath(
1965 ::prefs::kAutofillDialogPayWithoutWallet
));
1968 // Tests that there's an overlay shown while waiting for full wallet items.
1969 TEST_F(AutofillDialogControllerTest
, WalletFirstRun
) {
1970 EXPECT_TRUE(controller()->GetDialogOverlay().image
.IsEmpty());
1972 SubmitWithWalletItems(CompleteAndValidWalletItems());
1973 EXPECT_FALSE(controller()->GetDialogOverlay().image
.IsEmpty());
1975 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
1976 EXPECT_FALSE(controller()->GetDialogOverlay().image
.IsEmpty());
1977 EXPECT_FALSE(form_structure());
1979 // Don't make the test wait for 2 seconds.
1980 controller()->ForceFinishSubmit();
1981 EXPECT_TRUE(form_structure());
1984 TEST_F(AutofillDialogControllerTest
, ViewSubmitSetsPref
) {
1985 ASSERT_FALSE(profile()->GetPrefs()->HasPrefPath(
1986 ::prefs::kAutofillDialogPayWithoutWallet
));
1989 FillCreditCardInputs();
1990 controller()->OnAccept();
1992 EXPECT_TRUE(profile()->GetPrefs()->HasPrefPath(
1993 ::prefs::kAutofillDialogPayWithoutWallet
));
1994 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(
1995 ::prefs::kAutofillDialogPayWithoutWallet
));
1997 // Try again with a signin error (just leaves the pref alone).
1998 SetUpControllerWithFormData(DefaultFormData());
2000 // Setting up the controller again should not change the pref.
2001 EXPECT_TRUE(profile()->GetPrefs()->HasPrefPath(
2002 ::prefs::kAutofillDialogPayWithoutWallet
));
2003 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(
2004 ::prefs::kAutofillDialogPayWithoutWallet
));
2006 SimulateSigninError();
2007 FillCreditCardInputs();
2008 controller()->OnAccept();
2009 EXPECT_TRUE(profile()->GetPrefs()->HasPrefPath(
2010 ::prefs::kAutofillDialogPayWithoutWallet
));
2011 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(
2012 ::prefs::kAutofillDialogPayWithoutWallet
));
2014 // Successfully choosing wallet does set the pref.
2015 // Note that OnDidGetWalletItems sets the account chooser to wallet mode.
2016 SetUpControllerWithFormData(DefaultFormData());
2018 controller()->OnDidFetchWalletCookieValue(std::string());
2019 scoped_ptr
<wallet::WalletItems
> wallet_items
=
2020 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
2021 wallet_items
->AddInstrument(wallet::GetTestMaskedInstrument());
2022 controller()->OnDidGetWalletItems(wallet_items
.Pass());
2023 controller()->OnAccept();
2024 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
2025 controller()->ForceFinishSubmit();
2027 EXPECT_TRUE(profile()->GetPrefs()->HasPrefPath(
2028 ::prefs::kAutofillDialogPayWithoutWallet
));
2029 EXPECT_FALSE(profile()->GetPrefs()->GetBoolean(
2030 ::prefs::kAutofillDialogPayWithoutWallet
));
2033 TEST_F(AutofillDialogControllerTest
, HideWalletEmail
) {
2036 // Email field should be showing when using Autofill.
2037 EXPECT_TRUE(controller()->SectionIsActive(SECTION_BILLING
));
2038 EXPECT_FALSE(controller()->SectionIsActive(SECTION_CC_BILLING
));
2039 EXPECT_TRUE(SectionContainsField(SECTION_BILLING
, EMAIL_ADDRESS
));
2043 // Reset the wallet state.
2044 controller()->OnDidGetWalletItems(scoped_ptr
<wallet::WalletItems
>());
2046 // Setup some wallet state, submit, and get a full wallet to end the flow.
2047 scoped_ptr
<wallet::WalletItems
> wallet_items
= CompleteAndValidWalletItems();
2049 // Filling |form_structure()| depends on the current username and wallet items
2050 // being fetched. Until both of these have occurred, the user should not be
2051 // able to click Submit if using Wallet. The username fetch happened earlier.
2052 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK
));
2053 controller()->OnDidGetWalletItems(wallet_items
.Pass());
2054 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK
));
2056 // Email field should be absent when using Wallet.
2057 EXPECT_FALSE(controller()->SectionIsActive(SECTION_BILLING
));
2058 EXPECT_TRUE(controller()->SectionIsActive(SECTION_CC_BILLING
));
2059 EXPECT_FALSE(SectionContainsField(SECTION_CC_BILLING
, EMAIL_ADDRESS
));
2061 controller()->OnAccept();
2062 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
2063 controller()->ForceFinishSubmit();
2065 ASSERT_TRUE(form_structure());
2067 for (; i
< form_structure()->field_count(); ++i
) {
2068 if (form_structure()->field(i
)->Type().GetStorableType() == EMAIL_ADDRESS
) {
2069 EXPECT_EQ(ASCIIToUTF16(kFakeEmail
), form_structure()->field(i
)->value
);
2073 EXPECT_LT(i
, form_structure()->field_count());
2076 // Test if autofill types of returned form structure are correct for billing
2078 TEST_F(AutofillDialogControllerTest
, AutofillTypes
) {
2079 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
2080 controller()->OnAccept();
2081 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
2082 controller()->ForceFinishSubmit();
2083 ASSERT_TRUE(form_structure());
2084 ASSERT_EQ(20U, form_structure()->field_count());
2085 EXPECT_EQ(EMAIL_ADDRESS
,
2086 form_structure()->field(0)->Type().GetStorableType());
2087 EXPECT_EQ(CREDIT_CARD_NUMBER
,
2088 form_structure()->field(2)->Type().GetStorableType());
2089 EXPECT_EQ(ADDRESS_HOME_STATE
,
2090 form_structure()->field(9)->Type().GetStorableType());
2091 EXPECT_EQ(ADDRESS_BILLING
, form_structure()->field(9)->Type().group());
2092 EXPECT_EQ(ADDRESS_HOME_STATE
,
2093 form_structure()->field(16)->Type().GetStorableType());
2094 EXPECT_EQ(ADDRESS_HOME
, form_structure()->field(16)->Type().group());
2097 TEST_F(AutofillDialogControllerTest
, SaveDetailsInChrome
) {
2099 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(2);
2101 AutofillProfile
full_profile(test::GetVerifiedProfile());
2102 controller()->GetTestingManager()->AddTestingProfile(&full_profile
);
2104 CreditCard
card(test::GetVerifiedCreditCard());
2105 controller()->GetTestingManager()->AddTestingCreditCard(&card
);
2106 EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome());
2108 controller()->MenuModelForSection(SECTION_BILLING
)->ActivatedAt(0);
2109 EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome());
2111 controller()->MenuModelForSection(SECTION_BILLING
)->ActivatedAt(1);
2112 EXPECT_TRUE(controller()->ShouldOfferToSaveInChrome());
2114 profile()->ForceIncognito(true);
2115 EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome());
2118 // Tests that user is prompted when using instrument with minimal address.
2119 TEST_F(AutofillDialogControllerTest
, UpgradeMinimalAddress
) {
2120 // A minimal address being selected should trigger error validation in the
2121 // view. Called once for each incomplete suggestion.
2122 EXPECT_CALL(*controller()->GetView(), UpdateForErrors());
2124 scoped_ptr
<wallet::WalletItems
> wallet_items
=
2125 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
2126 wallet_items
->AddInstrument(wallet::GetTestMaskedInstrumentWithIdAndAddress(
2127 "id", wallet::GetTestMinimalAddress()));
2128 scoped_ptr
<wallet::Address
> address(wallet::GetTestShippingAddress());
2129 address
->set_is_complete_address(false);
2130 wallet_items
->AddAddress(address
.Pass());
2131 controller()->OnDidGetWalletItems(wallet_items
.Pass());
2133 // Assert that dialog's SECTION_CC_BILLING section is in edit mode.
2134 ASSERT_TRUE(controller()->IsEditingExistingData(SECTION_CC_BILLING
));
2135 // Shipping section should be in edit mode because of
2136 // is_minimal_shipping_address.
2137 ASSERT_TRUE(controller()->IsEditingExistingData(SECTION_SHIPPING
));
2140 TEST_F(AutofillDialogControllerTest
, RiskNeverLoadsWithPendingLegalDocuments
) {
2141 EXPECT_CALL(*controller(), LoadRiskFingerprintData()).Times(0);
2143 scoped_ptr
<wallet::WalletItems
> wallet_items
= CompleteAndValidWalletItems();
2144 wallet_items
->AddLegalDocument(wallet::GetTestLegalDocument());
2145 wallet_items
->AddLegalDocument(wallet::GetTestLegalDocument());
2146 controller()->OnDidGetWalletItems(wallet_items
.Pass());
2147 controller()->OnAccept();
2150 TEST_F(AutofillDialogControllerTest
, RiskLoadsAfterAcceptingLegalDocuments
) {
2151 EXPECT_CALL(*controller(), LoadRiskFingerprintData()).Times(0);
2153 scoped_ptr
<wallet::WalletItems
> wallet_items
= CompleteAndValidWalletItems();
2154 wallet_items
->AddLegalDocument(wallet::GetTestLegalDocument());
2155 wallet_items
->AddLegalDocument(wallet::GetTestLegalDocument());
2156 controller()->OnDidGetWalletItems(wallet_items
.Pass());
2158 testing::Mock::VerifyAndClear(controller());
2159 EXPECT_CALL(*controller(), LoadRiskFingerprintData());
2161 controller()->OnAccept();
2163 // Simulate a risk load and verify |GetRiskData()| matches the encoded value.
2164 controller()->OnDidAcceptLegalDocuments();
2165 controller()->OnDidLoadRiskFingerprintData(GetFakeFingerprint().Pass());
2166 EXPECT_EQ(kFakeFingerprintEncoded
, controller()->GetRiskData());
2169 TEST_F(AutofillDialogControllerTest
, NoManageMenuItemForNewWalletUsers
) {
2170 // Make sure the menu model item is created for a returning Wallet user.
2171 scoped_ptr
<wallet::WalletItems
> wallet_items
=
2172 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
2173 wallet_items
->AddInstrument(wallet::GetTestMaskedInstrument());
2174 wallet_items
->AddAddress(wallet::GetTestShippingAddress());
2175 controller()->OnDidGetWalletItems(wallet_items
.Pass());
2177 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING
));
2178 // "Same as billing", "123 address", "Add address...", and "Manage addresses".
2180 4, controller()->MenuModelForSection(SECTION_SHIPPING
)->GetItemCount());
2182 // Make sure the menu model item is not created for new Wallet users.
2183 base::DictionaryValue dict
;
2184 scoped_ptr
<base::ListValue
> required_actions(new base::ListValue
);
2185 required_actions
->AppendString("setup_wallet");
2186 dict
.Set("required_action", required_actions
.release());
2187 controller()->OnDidGetWalletItems(
2188 wallet::WalletItems::CreateWalletItems(dict
).Pass());
2190 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC_BILLING
));
2191 // "Same as billing" and "Add address...".
2193 2, controller()->MenuModelForSection(SECTION_SHIPPING
)->GetItemCount());
2196 TEST_F(AutofillDialogControllerTest
, ShippingSectionCanBeHidden
) {
2197 FormFieldData email_field
;
2198 email_field
.autocomplete_attribute
= "email";
2199 FormFieldData cc_field
;
2200 cc_field
.autocomplete_attribute
= "cc-number";
2201 FormFieldData billing_field
;
2202 billing_field
.autocomplete_attribute
= "billing region";
2205 form_data
.fields
.push_back(email_field
);
2206 form_data
.fields
.push_back(cc_field
);
2207 form_data
.fields
.push_back(billing_field
);
2209 AutofillProfile
full_profile(test::GetVerifiedProfile());
2210 controller()->GetTestingManager()->AddTestingProfile(&full_profile
);
2211 SetUpControllerWithFormData(form_data
);
2215 EXPECT_FALSE(controller()->SectionIsActive(SECTION_SHIPPING
));
2217 FillCreditCardInputs();
2218 controller()->OnAccept();
2219 EXPECT_TRUE(form_structure());
2222 TEST_F(AutofillDialogControllerTest
, ShippingSectionCanBeHiddenForWallet
) {
2223 FormFieldData email_field
;
2224 email_field
.autocomplete_attribute
= "email";
2225 FormFieldData cc_field
;
2226 cc_field
.autocomplete_attribute
= "cc-number";
2227 FormFieldData billing_field
;
2228 billing_field
.autocomplete_attribute
= "billing region";
2231 form_data
.fields
.push_back(email_field
);
2232 form_data
.fields
.push_back(cc_field
);
2233 form_data
.fields
.push_back(billing_field
);
2235 SetUpControllerWithFormData(form_data
);
2236 EXPECT_FALSE(controller()->SectionIsActive(SECTION_SHIPPING
));
2237 EXPECT_FALSE(controller()->IsShippingAddressRequired());
2239 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_
));
2240 scoped_ptr
<wallet::WalletItems
> wallet_items
=
2241 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
2242 wallet_items
->AddInstrument(wallet::GetTestMaskedInstrument());
2243 SubmitWithWalletItems(wallet_items
.Pass());
2244 controller()->OnDidGetFullWallet(wallet::GetTestFullWalletInstrumentOnly());
2245 controller()->ForceFinishSubmit();
2246 EXPECT_TRUE(form_structure());
2249 TEST_F(AutofillDialogControllerTest
, NotProdNotification
) {
2250 // To make IsPayingWithWallet() true.
2251 controller()->OnDidGetWalletItems(
2252 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
));
2254 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
2257 command_line
->GetSwitchValueASCII(switches::kWalletServiceUseSandbox
));
2259 command_line
->AppendSwitchASCII(switches::kWalletServiceUseSandbox
, "1");
2261 NotificationsOfType(DialogNotification::DEVELOPER_WARNING
).size());
2264 TEST_F(AutofillDialogControllerTest
, NoNotProdNotification
) {
2265 // To make IsPayingWithWallet() true.
2266 controller()->OnDidGetWalletItems(
2267 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
));
2269 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
2272 command_line
->GetSwitchValueASCII(switches::kWalletServiceUseSandbox
));
2274 command_line
->AppendSwitchASCII(switches::kWalletServiceUseSandbox
, "0");
2276 NotificationsOfType(DialogNotification::DEVELOPER_WARNING
).size());
2279 // Ensure Wallet instruments marked expired by the server are shown as invalid.
2280 TEST_F(AutofillDialogControllerTest
, WalletExpiredCard
) {
2281 scoped_ptr
<wallet::WalletItems
> wallet_items
=
2282 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
2283 wallet_items
->AddInstrument(wallet::GetTestMaskedInstrumentExpired());
2284 controller()->OnDidGetWalletItems(wallet_items
.Pass());
2286 EXPECT_TRUE(controller()->IsEditingExistingData(SECTION_CC_BILLING
));
2288 const DetailInputs
& inputs
=
2289 controller()->RequestedFieldsForSection(SECTION_CC_BILLING
);
2290 FieldValueMap outputs
;
2291 CopyInitialValues(inputs
, &outputs
);
2293 // The local inputs are invalid because the server said so. They'll
2294 // stay invalid until they differ from the remotely fetched model.
2295 ValidityMessages messages
= controller()->InputsAreValid(SECTION_CC_BILLING
,
2297 EXPECT_TRUE(messages
.HasSureError(CREDIT_CARD_EXP_MONTH
));
2298 EXPECT_TRUE(messages
.HasSureError(CREDIT_CARD_EXP_4_DIGIT_YEAR
));
2300 // Make the local input year differ from the instrument.
2301 CopyInitialValues(inputs
, &outputs
);
2302 outputs
[CREDIT_CARD_EXP_4_DIGIT_YEAR
] = ASCIIToUTF16("3002");
2303 messages
= controller()->InputsAreValid(SECTION_CC_BILLING
, outputs
);
2304 EXPECT_FALSE(HasAnyError(messages
, CREDIT_CARD_EXP_MONTH
));
2305 EXPECT_FALSE(HasAnyError(messages
, CREDIT_CARD_EXP_4_DIGIT_YEAR
));
2307 // Make the local input month differ from the instrument.
2308 CopyInitialValues(inputs
, &outputs
);
2309 outputs
[CREDIT_CARD_EXP_MONTH
] = ASCIIToUTF16("06");
2310 messages
= controller()->InputsAreValid(SECTION_CC_BILLING
, outputs
);
2311 EXPECT_FALSE(HasAnyError(messages
, CREDIT_CARD_EXP_MONTH
));
2312 EXPECT_FALSE(HasAnyError(messages
, CREDIT_CARD_EXP_4_DIGIT_YEAR
));
2315 TEST_F(AutofillDialogControllerTest
, ChooseAnotherInstrumentOrAddress
) {
2316 SubmitWithWalletItems(CompleteAndValidWalletItems());
2318 EXPECT_EQ(0U, NotificationsOfType(
2319 DialogNotification::REQUIRED_ACTION
).size());
2320 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems());
2321 controller()->OnDidGetFullWallet(
2322 wallet::GetTestFullWalletWithRequiredActions(
2323 std::vector
<wallet::RequiredAction
>(
2324 1, wallet::CHOOSE_ANOTHER_INSTRUMENT_OR_ADDRESS
)));
2325 EXPECT_EQ(1U, NotificationsOfType(
2326 DialogNotification::REQUIRED_ACTION
).size());
2327 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
2329 controller()->OnAccept();
2330 EXPECT_EQ(0U, NotificationsOfType(
2331 DialogNotification::REQUIRED_ACTION
).size());
2334 TEST_F(AutofillDialogControllerTest
, NewCardBubbleShown
) {
2336 FillCreditCardInputs();
2337 controller()->OnAccept();
2338 controller()->ViewClosed();
2340 EXPECT_EQ(1, mock_new_card_bubble_controller()->bubbles_shown());
2341 EXPECT_EQ(0, test_generated_bubble_controller()->bubbles_shown());
2344 TEST_F(AutofillDialogControllerTest
, GeneratedCardBubbleShown
) {
2345 SubmitWithWalletItems(CompleteAndValidWalletItems());
2346 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
2347 controller()->ForceFinishSubmit();
2348 controller()->ViewClosed();
2350 EXPECT_EQ(0, mock_new_card_bubble_controller()->bubbles_shown());
2351 EXPECT_EQ(1, test_generated_bubble_controller()->bubbles_shown());
2354 // Verify that new Wallet data is fetched when the user switches away from the
2355 // tab hosting the Autofill dialog and back. Also verify that the user's
2356 // selection is preserved across this re-fetch.
2357 TEST_F(AutofillDialogControllerTest
, ReloadWalletItemsOnActivation
) {
2358 // Initialize some Wallet data.
2359 scoped_ptr
<wallet::WalletItems
> wallet_items
=
2360 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
2361 wallet_items
->AddInstrument(wallet::GetTestMaskedInstrument());
2362 wallet_items
->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
2363 wallet_items
->AddAddress(wallet::GetTestNonDefaultShippingAddress());
2364 wallet_items
->AddAddress(wallet::GetTestShippingAddress());
2365 controller()->OnDidGetWalletItems(wallet_items
.Pass());
2367 // Initially, the default entries should be selected.
2368 ui::MenuModel
* cc_billing_model
=
2369 controller()->MenuModelForSection(SECTION_CC_BILLING
);
2370 ui::MenuModel
* shipping_model
=
2371 controller()->MenuModelForSection(SECTION_SHIPPING
);
2372 // "add", "manage", and 2 suggestions.
2373 ASSERT_EQ(4, cc_billing_model
->GetItemCount());
2374 EXPECT_TRUE(cc_billing_model
->IsItemCheckedAt(0));
2375 // "use billing", "add", "manage", and 2 suggestions.
2376 ASSERT_EQ(5, shipping_model
->GetItemCount());
2377 EXPECT_TRUE(shipping_model
->IsItemCheckedAt(2));
2379 // Select entries other than the defaults.
2380 cc_billing_model
->ActivatedAt(1);
2381 shipping_model
->ActivatedAt(1);
2382 // 2 suggestions, "add", and "manage".
2383 ASSERT_EQ(4, cc_billing_model
->GetItemCount());
2384 EXPECT_TRUE(cc_billing_model
->IsItemCheckedAt(1));
2385 // "use billing", 2 suggestions, "add", "manage".
2386 ASSERT_EQ(5, shipping_model
->GetItemCount());
2387 EXPECT_TRUE(shipping_model
-> IsItemCheckedAt(1));
2389 // Simulate switching away from the tab and back. This should issue a request
2390 // for wallet items.
2391 controller()->ClearLastWalletItemsFetchTimestampForTesting();
2392 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems());
2393 controller()->TabActivated();
2395 // Simulate a response that includes different items.
2396 wallet_items
= wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
2397 wallet_items
->AddInstrument(wallet::GetTestMaskedInstrumentExpired());
2398 wallet_items
->AddInstrument(wallet::GetTestMaskedInstrument());
2399 wallet_items
->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
2400 wallet_items
->AddAddress(wallet::GetTestNonDefaultShippingAddress());
2401 controller()->OnDidGetWalletItems(wallet_items
.Pass());
2403 // The previously selected entries should still be selected.
2404 // 3 suggestions, "add", and "manage".
2405 ASSERT_EQ(5, cc_billing_model
->GetItemCount());
2406 EXPECT_TRUE(cc_billing_model
->IsItemCheckedAt(2));
2407 // "use billing", 1 suggestion, "add", and "manage".
2408 ASSERT_EQ(4, shipping_model
->GetItemCount());
2409 EXPECT_TRUE(shipping_model
->IsItemCheckedAt(1));
2412 // Verify that if the default values change when re-fetching Wallet data, these
2413 // new default values are selected in the dialog.
2414 TEST_F(AutofillDialogControllerTest
,
2415 ReloadWalletItemsOnActivationWithNewDefaults
) {
2416 // Initialize some Wallet data.
2417 scoped_ptr
<wallet::WalletItems
> wallet_items
=
2418 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
2419 wallet_items
->AddInstrument(wallet::GetTestMaskedInstrument());
2420 wallet_items
->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
2421 wallet_items
->AddAddress(wallet::GetTestNonDefaultShippingAddress());
2422 wallet_items
->AddAddress(wallet::GetTestShippingAddress());
2423 controller()->OnDidGetWalletItems(wallet_items
.Pass());
2425 // Initially, the default entries should be selected.
2426 ui::MenuModel
* cc_billing_model
=
2427 controller()->MenuModelForSection(SECTION_CC_BILLING
);
2428 ui::MenuModel
* shipping_model
=
2429 controller()->MenuModelForSection(SECTION_SHIPPING
);
2430 // 2 suggestions, "add", and "manage".
2431 ASSERT_EQ(4, cc_billing_model
->GetItemCount());
2432 EXPECT_TRUE(cc_billing_model
->IsItemCheckedAt(0));
2433 // "use billing", 2 suggestions, "add", and "manage".
2434 ASSERT_EQ(5, shipping_model
->GetItemCount());
2435 EXPECT_TRUE(shipping_model
->IsItemCheckedAt(2));
2437 // Simulate switching away from the tab and back. This should issue a request
2438 // for wallet items.
2439 controller()->ClearLastWalletItemsFetchTimestampForTesting();
2440 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems());
2441 controller()->TabActivated();
2443 // Simulate a response that includes different default values.
2445 wallet::GetTestWalletItemsWithDefaultIds("new_default_instrument_id",
2446 "new_default_address_id",
2447 wallet::AMEX_DISALLOWED
);
2448 scoped_ptr
<wallet::Address
> other_address
= wallet::GetTestShippingAddress();
2449 other_address
->set_object_id("other_address_id");
2450 scoped_ptr
<wallet::Address
> new_default_address
=
2451 wallet::GetTestNonDefaultShippingAddress();
2452 new_default_address
->set_object_id("new_default_address_id");
2454 wallet_items
->AddInstrument(
2455 wallet::GetTestMaskedInstrumentWithId("other_instrument_id"));
2456 wallet_items
->AddInstrument(
2457 wallet::GetTestMaskedInstrumentWithId("new_default_instrument_id"));
2458 wallet_items
->AddAddress(new_default_address
.Pass());
2459 wallet_items
->AddAddress(other_address
.Pass());
2460 controller()->OnDidGetWalletItems(wallet_items
.Pass());
2462 // The new default entries should be selected.
2463 // 2 suggestions, "add", and "manage".
2464 ASSERT_EQ(4, cc_billing_model
->GetItemCount());
2465 EXPECT_TRUE(cc_billing_model
->IsItemCheckedAt(1));
2466 // "use billing", 2 suggestions, "add", and "manage".
2467 ASSERT_EQ(5, shipping_model
->GetItemCount());
2468 EXPECT_TRUE(shipping_model
->IsItemCheckedAt(1));
2471 TEST_F(AutofillDialogControllerTest
, ReloadWithEmptyWalletItems
) {
2472 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
2473 controller()->MenuModelForSection(SECTION_CC_BILLING
)->ActivatedAt(1);
2474 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(1);
2476 controller()->ClearLastWalletItemsFetchTimestampForTesting();
2477 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems());
2478 controller()->TabActivated();
2480 controller()->OnDidGetWalletItems(
2481 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
));
2483 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC_BILLING
));
2485 3, controller()->MenuModelForSection(SECTION_SHIPPING
)->GetItemCount());
2488 TEST_F(AutofillDialogControllerTest
, SaveInChromeByDefault
) {
2489 EXPECT_TRUE(controller()->ShouldSaveInChrome());
2491 FillCreditCardInputs();
2492 controller()->OnAccept();
2493 EXPECT_TRUE(controller()->ShouldSaveInChrome());
2496 TEST_F(AutofillDialogControllerTest
,
2497 SaveInChromePreferenceNotRememberedOnCancel
) {
2498 EXPECT_TRUE(controller()->ShouldSaveInChrome());
2500 controller()->GetView()->CheckSaveDetailsLocallyCheckbox(false);
2501 controller()->OnCancel();
2502 EXPECT_TRUE(controller()->ShouldSaveInChrome());
2505 TEST_F(AutofillDialogControllerTest
,
2506 SaveInChromePreferenceRememberedOnSuccess
) {
2507 EXPECT_TRUE(controller()->ShouldSaveInChrome());
2509 FillCreditCardInputs();
2510 controller()->GetView()->CheckSaveDetailsLocallyCheckbox(false);
2511 controller()->OnAccept();
2512 EXPECT_FALSE(controller()->ShouldSaveInChrome());
2515 TEST_F(AutofillDialogControllerTest
,
2516 SubmitButtonIsDisabled_SpinnerFinishesBeforeDelay
) {
2517 // Reset Wallet state.
2518 controller()->OnDidGetWalletItems(scoped_ptr
<wallet::WalletItems
>());
2520 EXPECT_EQ(1, controller()->get_submit_button_delay_count());
2522 // Begin the submit button delay.
2523 controller()->SimulateSubmitButtonDelayBegin();
2525 EXPECT_TRUE(controller()->ShouldShowSpinner());
2526 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK
));
2528 // Stop the spinner.
2529 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
2531 EXPECT_FALSE(controller()->ShouldShowSpinner());
2532 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK
));
2534 // End the submit button delay.
2535 controller()->SimulateSubmitButtonDelayEnd();
2537 EXPECT_FALSE(controller()->ShouldShowSpinner());
2538 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK
));
2541 TEST_F(AutofillDialogControllerTest
,
2542 SubmitButtonIsDisabled_SpinnerFinishesAfterDelay
) {
2543 // Reset Wallet state.
2544 controller()->OnDidGetWalletItems(scoped_ptr
<wallet::WalletItems
>());
2546 EXPECT_EQ(1, controller()->get_submit_button_delay_count());
2548 // Begin the submit button delay.
2549 controller()->SimulateSubmitButtonDelayBegin();
2551 EXPECT_TRUE(controller()->ShouldShowSpinner());
2552 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK
));
2554 // End the submit button delay.
2555 controller()->SimulateSubmitButtonDelayEnd();
2557 EXPECT_TRUE(controller()->ShouldShowSpinner());
2558 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK
));
2560 // Stop the spinner.
2561 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
2563 EXPECT_FALSE(controller()->ShouldShowSpinner());
2564 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK
));
2567 TEST_F(AutofillDialogControllerTest
, SubmitButtonIsDisabled_NoSpinner
) {
2570 EXPECT_EQ(1, controller()->get_submit_button_delay_count());
2572 // Begin the submit button delay.
2573 controller()->SimulateSubmitButtonDelayBegin();
2575 EXPECT_FALSE(controller()->ShouldShowSpinner());
2576 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK
));
2578 // End the submit button delay.
2579 controller()->SimulateSubmitButtonDelayEnd();
2581 EXPECT_FALSE(controller()->ShouldShowSpinner());
2582 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK
));
2585 TEST_F(AutofillDialogControllerTest
, IconsForFields_NoCreditCard
) {
2586 FieldValueMap values
;
2587 values
[EMAIL_ADDRESS
] = ASCIIToUTF16(kFakeEmail
);
2588 FieldIconMap icons
= controller()->IconsForFields(values
);
2589 EXPECT_TRUE(icons
.empty());
2592 TEST_F(AutofillDialogControllerTest
, IconsForFields_CreditCardNumberOnly
) {
2593 FieldValueMap values
;
2594 values
[EMAIL_ADDRESS
] = ASCIIToUTF16(kFakeEmail
);
2595 values
[CREDIT_CARD_NUMBER
] = ASCIIToUTF16(kTestCCNumberVisa
);
2596 FieldIconMap icons
= controller()->IconsForFields(values
);
2597 EXPECT_EQ(1UL, icons
.size());
2598 EXPECT_EQ(1UL, icons
.count(CREDIT_CARD_NUMBER
));
2601 TEST_F(AutofillDialogControllerTest
, IconsForFields_CvcOnly
) {
2602 FieldValueMap values
;
2603 values
[EMAIL_ADDRESS
] = ASCIIToUTF16(kFakeEmail
);
2604 values
[CREDIT_CARD_VERIFICATION_CODE
] = ASCIIToUTF16("123");
2605 FieldIconMap icons
= controller()->IconsForFields(values
);
2606 EXPECT_EQ(1UL, icons
.size());
2607 EXPECT_EQ(1UL, icons
.count(CREDIT_CARD_VERIFICATION_CODE
));
2610 TEST_F(AutofillDialogControllerTest
, IconsForFields_BothCreditCardAndCvc
) {
2611 FieldValueMap values
;
2612 values
[EMAIL_ADDRESS
] = ASCIIToUTF16(kFakeEmail
);
2613 values
[CREDIT_CARD_NUMBER
] = ASCIIToUTF16(kTestCCNumberVisa
);
2614 values
[CREDIT_CARD_VERIFICATION_CODE
] = ASCIIToUTF16("123");
2615 FieldIconMap icons
= controller()->IconsForFields(values
);
2616 EXPECT_EQ(2UL, icons
.size());
2617 EXPECT_EQ(1UL, icons
.count(CREDIT_CARD_VERIFICATION_CODE
));
2618 EXPECT_EQ(1UL, icons
.count(CREDIT_CARD_NUMBER
));
2621 TEST_F(AutofillDialogControllerTest
, FieldControlsIcons
) {
2622 EXPECT_TRUE(controller()->FieldControlsIcons(CREDIT_CARD_NUMBER
));
2623 EXPECT_FALSE(controller()->FieldControlsIcons(CREDIT_CARD_VERIFICATION_CODE
));
2624 EXPECT_FALSE(controller()->FieldControlsIcons(EMAIL_ADDRESS
));
2627 TEST_F(AutofillDialogControllerTest
, SaveCreditCardIncludesName_NoBilling
) {
2630 CreditCard
test_credit_card(test::GetVerifiedCreditCard());
2631 FillInputs(SECTION_CC
, test_credit_card
);
2633 AutofillProfile
test_profile(test::GetVerifiedProfile());
2634 FillInputs(SECTION_BILLING
, test_profile
);
2636 UseBillingForShipping();
2638 controller()->GetView()->CheckSaveDetailsLocallyCheckbox(true);
2639 controller()->OnAccept();
2641 TestPersonalDataManager
* test_pdm
= controller()->GetTestingManager();
2642 const CreditCard
& imported_card
= test_pdm
->imported_credit_card();
2643 EXPECT_EQ(test_profile
.GetRawInfo(NAME_FULL
),
2644 imported_card
.GetRawInfo(CREDIT_CARD_NAME
));
2647 TEST_F(AutofillDialogControllerTest
, SaveCreditCardIncludesName_WithBilling
) {
2650 TestPersonalDataManager
* test_pdm
= controller()->GetTestingManager();
2651 AutofillProfile
test_profile(test::GetVerifiedProfile());
2653 EXPECT_CALL(*controller()->GetView(), ModelChanged());
2654 test_pdm
->AddTestingProfile(&test_profile
);
2655 ASSERT_TRUE(controller()->MenuModelForSection(SECTION_BILLING
));
2657 CreditCard
test_credit_card(test::GetVerifiedCreditCard());
2658 FillInputs(SECTION_CC
, test_credit_card
);
2660 controller()->GetView()->CheckSaveDetailsLocallyCheckbox(true);
2661 controller()->OnAccept();
2663 const CreditCard
& imported_card
= test_pdm
->imported_credit_card();
2664 EXPECT_EQ(test_profile
.GetRawInfo(NAME_FULL
),
2665 imported_card
.GetRawInfo(CREDIT_CARD_NAME
));
2667 controller()->ViewClosed();
2670 TEST_F(AutofillDialogControllerTest
, InputEditability
) {
2671 // Empty wallet items: all fields are editable.
2672 scoped_ptr
<wallet::WalletItems
> items
=
2673 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
2674 controller()->OnDidGetWalletItems(items
.Pass());
2676 DialogSection sections
[] = { SECTION_CC_BILLING
, SECTION_SHIPPING
};
2677 for (size_t i
= 0; i
< arraysize(sections
); ++i
) {
2678 const DetailInputs
& inputs
=
2679 controller()->RequestedFieldsForSection(sections
[i
]);
2680 for (size_t j
= 0; j
< inputs
.size(); ++j
) {
2681 EXPECT_TRUE(controller()->InputIsEditable(inputs
[j
], sections
[i
]));
2685 // Expired instrument: CC number + CVV are not editable.
2686 items
= wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
2687 scoped_ptr
<wallet::WalletItems::MaskedInstrument
> expired_instrument
=
2688 wallet::GetTestMaskedInstrumentExpired();
2689 items
->AddInstrument(expired_instrument
.Pass());
2690 controller()->OnDidGetWalletItems(items
.Pass());
2691 EXPECT_TRUE(controller()->IsEditingExistingData(SECTION_CC_BILLING
));
2693 const DetailInputs
& inputs
=
2694 controller()->RequestedFieldsForSection(SECTION_CC_BILLING
);
2695 FieldValueMap outputs
;
2696 CopyInitialValues(inputs
, &outputs
);
2697 controller()->GetView()->SetUserInput(SECTION_CC_BILLING
, outputs
);
2699 for (size_t i
= 0; i
< arraysize(sections
); ++i
) {
2700 const DetailInputs
& inputs
=
2701 controller()->RequestedFieldsForSection(sections
[i
]);
2702 for (size_t j
= 0; j
< inputs
.size(); ++j
) {
2703 if (inputs
[j
].type
== CREDIT_CARD_NUMBER
||
2704 inputs
[j
].type
== CREDIT_CARD_VERIFICATION_CODE
) {
2705 EXPECT_FALSE(controller()->InputIsEditable(inputs
[j
], sections
[i
]));
2707 EXPECT_TRUE(controller()->InputIsEditable(inputs
[j
], sections
[i
]));
2712 // User changes the billing address; same story.
2713 outputs
[ADDRESS_BILLING_ZIP
] = ASCIIToUTF16("77025");
2714 controller()->GetView()->SetUserInput(SECTION_CC_BILLING
, outputs
);
2715 for (size_t i
= 0; i
< arraysize(sections
); ++i
) {
2716 const DetailInputs
& inputs
=
2717 controller()->RequestedFieldsForSection(sections
[i
]);
2718 for (size_t j
= 0; j
< inputs
.size(); ++j
) {
2719 if (inputs
[j
].type
== CREDIT_CARD_NUMBER
||
2720 inputs
[j
].type
== CREDIT_CARD_VERIFICATION_CODE
) {
2721 EXPECT_FALSE(controller()->InputIsEditable(inputs
[j
], sections
[i
]));
2723 EXPECT_TRUE(controller()->InputIsEditable(inputs
[j
], sections
[i
]));
2728 // User changes a detail of the CC itself (expiration date), CVV is now
2729 // editable (and mandatory).
2730 outputs
[CREDIT_CARD_EXP_MONTH
] = ASCIIToUTF16("06");
2731 controller()->GetView()->SetUserInput(SECTION_CC_BILLING
, outputs
);
2732 for (size_t i
= 0; i
< arraysize(sections
); ++i
) {
2733 const DetailInputs
& inputs
=
2734 controller()->RequestedFieldsForSection(sections
[i
]);
2735 for (size_t j
= 0; j
< inputs
.size(); ++j
) {
2736 if (inputs
[j
].type
== CREDIT_CARD_NUMBER
)
2737 EXPECT_FALSE(controller()->InputIsEditable(inputs
[j
], sections
[i
]));
2739 EXPECT_TRUE(controller()->InputIsEditable(inputs
[j
], sections
[i
]));
2744 // When the default country is something besides US, wallet is not selected
2745 // and the account chooser shouldn't be visible.
2746 TEST_F(AutofillDialogControllerTest
, HideWalletInOtherCountries
) {
2747 // Addresses from different countries.
2748 AutofillProfile
us_profile(base::GenerateGUID(), kSettingsOrigin
),
2749 es_profile(base::GenerateGUID(), kSettingsOrigin
),
2750 es_profile2(base::GenerateGUID(), kSettingsOrigin
);
2751 us_profile
.SetRawInfo(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("US"));
2752 es_profile
.SetRawInfo(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("ES"));
2753 es_profile2
.SetRawInfo(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("ES"));
2755 // If US is indicated (via timezone), show Wallet.
2756 ResetControllerWithFormData(DefaultFormData());
2757 controller()->GetTestingManager()->set_timezone_country_code("US");
2758 controller()->Show();
2760 controller()->AccountChooserModelForTesting()->WalletIsSelected());
2761 controller()->OnDidFetchWalletCookieValue(std::string());
2762 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
2763 EXPECT_TRUE(controller()->ShouldShowAccountChooser());
2765 controller()->AccountChooserModelForTesting()->WalletIsSelected());
2767 // If US is not indicated, don't show Wallet.
2768 ResetControllerWithFormData(DefaultFormData());
2769 controller()->GetTestingManager()->set_timezone_country_code("ES");
2770 controller()->Show();
2771 controller()->OnDidFetchWalletCookieValue(std::string());
2772 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
2773 EXPECT_FALSE(controller()->ShouldShowAccountChooser());
2775 // If US is indicated (via a profile), show Wallet.
2776 ResetControllerWithFormData(DefaultFormData());
2777 controller()->GetTestingManager()->set_timezone_country_code("ES");
2778 controller()->GetTestingManager()->AddTestingProfile(&us_profile
);
2779 controller()->Show();
2780 controller()->OnDidFetchWalletCookieValue(std::string());
2781 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
2782 EXPECT_TRUE(controller()->ShouldShowAccountChooser());
2784 controller()->AccountChooserModelForTesting()->WalletIsSelected());
2786 // Make sure the profile doesn't just override the timezone.
2787 ResetControllerWithFormData(DefaultFormData());
2788 controller()->GetTestingManager()->set_timezone_country_code("US");
2789 controller()->GetTestingManager()->AddTestingProfile(&es_profile
);
2790 controller()->Show();
2791 controller()->OnDidFetchWalletCookieValue(std::string());
2792 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
2793 EXPECT_TRUE(controller()->ShouldShowAccountChooser());
2795 controller()->AccountChooserModelForTesting()->WalletIsSelected());
2797 // Only takes one US address to enable Wallet.
2798 ResetControllerWithFormData(DefaultFormData());
2799 controller()->GetTestingManager()->set_timezone_country_code("FR");
2800 controller()->GetTestingManager()->AddTestingProfile(&es_profile
);
2801 controller()->GetTestingManager()->AddTestingProfile(&es_profile2
);
2802 controller()->GetTestingManager()->AddTestingProfile(&us_profile
);
2803 controller()->Show();
2804 controller()->OnDidFetchWalletCookieValue(std::string());
2805 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
2806 EXPECT_TRUE(controller()->ShouldShowAccountChooser());
2808 controller()->AccountChooserModelForTesting()->WalletIsSelected());
2811 TEST_F(AutofillDialogControllerTest
, DontGetWalletTillNecessary
) {
2812 // When starting on local data mode, the dialog will provide a "Use Google
2814 profile()->GetPrefs()->SetBoolean(
2815 ::prefs::kAutofillDialogPayWithoutWallet
, true);
2816 ResetControllerWithFormData(DefaultFormData());
2817 controller()->Show();
2818 base::string16 use_wallet_text
= controller()->SignInLinkText();
2819 EXPECT_EQ(TestAutofillDialogController::NOT_CHECKED
,
2820 controller()->SignedInState());
2822 // When clicked, this link will ask for wallet items. If there's a signin
2823 // failure, the link will switch to "Sign in to use Google Wallet".
2824 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems());
2825 controller()->SignInLinkClicked();
2826 EXPECT_NE(TestAutofillDialogController::NOT_CHECKED
,
2827 controller()->SignedInState());
2828 controller()->OnDidFetchWalletCookieValue(std::string());
2829 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
2830 controller()->OnPassiveSigninFailure(GoogleServiceAuthError(
2831 GoogleServiceAuthError::CONNECTION_FAILED
));
2832 EXPECT_NE(use_wallet_text
, controller()->SignInLinkText());
2835 TEST_F(AutofillDialogControllerTest
, MultiAccountSwitch
) {
2836 std::vector
<std::string
> users
;
2837 users
.push_back("user_1@example.com");
2838 users
.push_back("user_2@example.com");
2839 controller()->OnDidGetWalletItems(
2840 wallet::GetTestWalletItemsWithUsers(users
, 0));
2842 // Items should be: Account 1, account 2, add account, disable wallet.
2843 EXPECT_EQ(4, controller()->MenuModelForAccountChooser()->GetItemCount());
2844 EXPECT_EQ(0U, controller()->GetTestingWalletClient()->user_index());
2846 // GetWalletItems should be called when the user switches accounts.
2847 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems());
2848 controller()->MenuModelForAccountChooser()->ActivatedAt(1);
2849 // The wallet client should be updated to the new user index.
2850 EXPECT_EQ(1U, controller()->GetTestingWalletClient()->user_index());
2853 TEST_F(AutofillDialogControllerTest
, PassiveAuthFailure
) {
2854 controller()->OnDidGetWalletItems(
2855 wallet::GetTestWalletItemsWithRequiredAction(
2856 wallet::PASSIVE_GAIA_AUTH
));
2857 EXPECT_TRUE(controller()->ShouldShowSpinner());
2858 controller()->OnPassiveSigninFailure(GoogleServiceAuthError(
2859 GoogleServiceAuthError::NONE
));
2860 EXPECT_FALSE(controller()->ShouldShowSpinner());
2863 TEST_F(AutofillDialogControllerTest
, WalletShippingSameAsBilling
) {
2864 // Assert initial state.
2865 ASSERT_FALSE(profile()->GetPrefs()->HasPrefPath(
2866 ::prefs::kAutofillDialogWalletShippingSameAsBilling
));
2868 // Verify that false pref defaults to wallet defaults.
2869 scoped_ptr
<wallet::WalletItems
> wallet_items
=
2870 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
2871 wallet_items
->AddAddress(wallet::GetTestNonDefaultShippingAddress());
2872 wallet_items
->AddAddress(wallet::GetTestShippingAddress());
2873 controller()->OnDidGetWalletItems(wallet_items
.Pass());
2874 ASSERT_FALSE(profile()->GetPrefs()->GetBoolean(
2875 ::prefs::kAutofillDialogWalletShippingSameAsBilling
));
2876 EXPECT_EQ(2, GetMenuModelForSection(SECTION_SHIPPING
)->checked_item());
2878 // Set "Same as Billing" for the shipping address and verify it sets the pref
2879 // and selects the appropriate menu item.
2880 UseBillingForShipping();
2881 ASSERT_EQ(0, GetMenuModelForSection(SECTION_SHIPPING
)->checked_item());
2882 controller()->ForceFinishSubmit();
2883 ASSERT_TRUE(profile()->GetPrefs()->GetBoolean(
2884 ::prefs::kAutofillDialogWalletShippingSameAsBilling
));
2886 // Getting new wallet info shouldn't disrupt the preference and menu should be
2889 wallet_items
= wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
2890 wallet_items
->AddAddress(wallet::GetTestNonDefaultShippingAddress());
2891 wallet_items
->AddAddress(wallet::GetTestShippingAddress());
2892 controller()->OnDidGetWalletItems(wallet_items
.Pass());
2893 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(
2894 ::prefs::kAutofillDialogWalletShippingSameAsBilling
));
2895 EXPECT_EQ(0, GetMenuModelForSection(SECTION_SHIPPING
)->checked_item());
2897 // Choose a different address and ensure pref gets set to false.
2898 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(1);
2899 controller()->ForceFinishSubmit();
2900 EXPECT_FALSE(profile()->GetPrefs()->GetBoolean(
2901 ::prefs::kAutofillDialogWalletShippingSameAsBilling
));
2904 // Verifies that a call to the IconsForFields() method before the card type is
2905 // known returns a placeholder image that is at least as large as the icons for
2906 // all of the supported major credit card issuers.
2907 TEST_F(AutofillDialogControllerTest
, IconReservedForCreditCardField
) {
2908 FieldValueMap inputs
;
2909 inputs
[CREDIT_CARD_NUMBER
] = base::string16();
2911 FieldIconMap icons
= controller()->IconsForFields(inputs
);
2912 EXPECT_EQ(1U, icons
.size());
2914 ASSERT_EQ(1U, icons
.count(CREDIT_CARD_NUMBER
));
2915 gfx::Image placeholder_icon
= icons
[CREDIT_CARD_NUMBER
];
2917 // Verify that the placeholder icon is at least as large as the icons for the
2918 // supported credit card issuers.
2919 const int kSupportedCardIdrs
[] = {
2920 IDR_AUTOFILL_CC_AMEX
,
2921 IDR_AUTOFILL_CC_DINERS
,
2922 IDR_AUTOFILL_CC_DISCOVER
,
2923 IDR_AUTOFILL_CC_GENERIC
,
2924 IDR_AUTOFILL_CC_JCB
,
2925 IDR_AUTOFILL_CC_MASTERCARD
,
2926 IDR_AUTOFILL_CC_VISA
,
2928 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
2929 for (size_t i
= 0; i
< arraysize(kSupportedCardIdrs
); ++i
) {
2930 SCOPED_TRACE(base::IntToString(i
));
2931 gfx::Image supported_card_icon
= rb
.GetImageNamed(kSupportedCardIdrs
[i
]);
2932 EXPECT_GE(placeholder_icon
.Width(), supported_card_icon
.Width());
2933 EXPECT_GE(placeholder_icon
.Height(), supported_card_icon
.Height());
2937 TEST_F(AutofillDialogControllerTest
, CountryChangeUpdatesSection
) {
2938 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
2939 command_line
->AppendSwitch(::switches::kEnableAutofillAddressI18n
);
2943 TestAutofillDialogView
* view
= controller()->GetView();
2944 view
->ClearSectionUpdates();
2946 controller()->UserEditedOrActivatedInput(SECTION_SHIPPING
,
2947 ADDRESS_HOME_COUNTRY
,
2950 ASCIIToUTF16("China"),
2952 std::map
<DialogSection
, size_t> updates
= view
->section_updates();
2953 EXPECT_EQ(1U, updates
[SECTION_SHIPPING
]);
2954 EXPECT_EQ(1U, updates
.size());
2956 view
->ClearSectionUpdates();
2958 controller()->UserEditedOrActivatedInput(SECTION_CC_BILLING
,
2959 ADDRESS_BILLING_COUNTRY
,
2962 ASCIIToUTF16("France"),
2964 updates
= view
->section_updates();
2965 EXPECT_EQ(1U, updates
[SECTION_CC_BILLING
]);
2966 EXPECT_EQ(1U, updates
.size());
2969 view
->ClearSectionUpdates();
2971 controller()->UserEditedOrActivatedInput(SECTION_BILLING
,
2972 ADDRESS_BILLING_COUNTRY
,
2975 ASCIIToUTF16("Italy"),
2977 updates
= view
->section_updates();
2978 EXPECT_EQ(1U, updates
[SECTION_BILLING
]);
2979 EXPECT_EQ(1U, updates
.size());
2982 } // namespace autofill