autofill names - dont parse when calling SetRawInfo(FULL_NAME)
[chromium-blink-merge.git] / chrome / browser / ui / autofill / autofill_dialog_controller_unittest.cc
blob3221a70a13abfd5e664f70af508b502090d8f2ad
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <map>
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback.h"
10 #include "base/command_line.h"
11 #include "base/guid.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/prefs/pref_service.h"
15 #include "base/run_loop.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/string_piece.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "base/tuple.h"
20 #include "chrome/browser/ui/autofill/autofill_dialog_controller_impl.h"
21 #include "chrome/browser/ui/autofill/autofill_dialog_i18n_input.h"
22 #include "chrome/browser/ui/autofill/autofill_dialog_view.h"
23 #include "chrome/browser/ui/autofill/generated_credit_card_bubble_controller.h"
24 #include "chrome/browser/ui/autofill/mock_address_validator.h"
25 #include "chrome/browser/ui/autofill/mock_new_credit_card_bubble_controller.h"
26 #include "chrome/browser/ui/autofill/test_generated_credit_card_bubble_controller.h"
27 #include "chrome/browser/webdata/web_data_service_factory.h"
28 #include "chrome/common/chrome_switches.h"
29 #include "chrome/common/pref_names.h"
30 #include "chrome/common/render_messages.h"
31 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
32 #include "chrome/test/base/scoped_testing_local_state.h"
33 #include "chrome/test/base/testing_browser_process.h"
34 #include "chrome/test/base/testing_profile.h"
35 #include "components/autofill/content/browser/risk/proto/fingerprint.pb.h"
36 #include "components/autofill/content/browser/wallet/full_wallet.h"
37 #include "components/autofill/content/browser/wallet/gaia_account.h"
38 #include "components/autofill/content/browser/wallet/instrument.h"
39 #include "components/autofill/content/browser/wallet/mock_wallet_client.h"
40 #include "components/autofill/content/browser/wallet/wallet_address.h"
41 #include "components/autofill/content/browser/wallet/wallet_service_url.h"
42 #include "components/autofill/content/browser/wallet/wallet_test_util.h"
43 #include "components/autofill/core/browser/autofill_metrics.h"
44 #include "components/autofill/core/browser/autofill_test_utils.h"
45 #include "components/autofill/core/browser/test_personal_data_manager.h"
46 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
47 #include "components/autofill/core/common/autofill_switches.h"
48 #include "components/autofill/core/common/form_data.h"
49 #include "components/user_prefs/user_prefs.h"
50 #include "content/public/browser/web_contents.h"
51 #include "content/public/test/mock_render_process_host.h"
52 #include "google_apis/gaia/google_service_auth_error.h"
53 #include "grit/component_scaled_resources.h"
54 #include "grit/components_strings.h"
55 #include "grit/generated_resources.h"
56 #include "testing/gmock/include/gmock/gmock.h"
57 #include "testing/gtest/include/gtest/gtest.h"
58 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/address_data.h"
59 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/address_validator.h"
60 #include "ui/base/l10n/l10n_util.h"
61 #include "ui/base/resource/resource_bundle.h"
63 #if defined(OS_WIN)
64 #include "ui/base/win/scoped_ole_initializer.h"
65 #endif
67 using base::ASCIIToUTF16;
68 using base::UTF8ToUTF16;
70 namespace autofill {
72 namespace {
74 using ::i18n::addressinput::AddressData;
75 using ::i18n::addressinput::AddressProblemFilter;
76 using ::i18n::addressinput::AddressProblem;
77 using ::i18n::addressinput::AddressProblems;
78 using ::i18n::addressinput::AddressValidator;
79 using testing::AtLeast;
80 using testing::DoAll;
81 using testing::Return;
82 using testing::SetArgPointee;
83 using testing::_;
85 const char kSourceUrl[] = "http://localbike.shop";
86 const char kFakeEmail[] = "user@chromium.org";
87 const char kFakeFingerprintEncoded[] = "CgVaAwiACA==";
88 const char kEditedBillingAddress[] = "123 edited billing address";
89 const char* kFieldsFromPage[] =
90 { "email",
91 "cc-name",
92 "cc-number",
93 "cc-exp-month",
94 "cc-exp-year",
95 "cc-csc",
96 "billing name",
97 "billing address-line1",
98 "billing address-level2",
99 "billing address-level1",
100 "billing postal-code",
101 "billing country",
102 "billing tel",
103 "shipping name",
104 "shipping address-line1",
105 "shipping address-level2",
106 "shipping address-level1",
107 "shipping postal-code",
108 "shipping country",
109 "shipping tel",
111 const char kSettingsOrigin[] = "Chrome settings";
112 const char kTestCCNumberAmex[] = "376200000000002";
113 const char kTestCCNumberVisa[] = "4111111111111111";
114 const char kTestCCNumberMaster[] = "5555555555554444";
115 const char kTestCCNumberDiscover[] = "6011111111111117";
116 const char kTestCCNumberIncomplete[] = "4111111111";
117 // Credit card number fails Luhn check.
118 const char kTestCCNumberInvalid[] = "4111111111111112";
120 // Copies the initial values from |inputs| into |outputs|.
121 void CopyInitialValues(const DetailInputs& inputs, FieldValueMap* outputs) {
122 for (size_t i = 0; i < inputs.size(); ++i) {
123 const DetailInput& input = inputs[i];
124 (*outputs)[input.type] = input.initial_value;
128 scoped_ptr<wallet::WalletItems> CompleteAndValidWalletItems() {
129 scoped_ptr<wallet::WalletItems> items =
130 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
131 items->AddAccount(wallet::GetTestGaiaAccount());
132 items->AddInstrument(wallet::GetTestMaskedInstrument());
133 items->AddAddress(wallet::GetTestShippingAddress());
134 return items.Pass();
137 scoped_ptr<risk::Fingerprint> GetFakeFingerprint() {
138 scoped_ptr<risk::Fingerprint> fingerprint(new risk::Fingerprint());
139 // Add some data to the proto, else the encoded content is empty.
140 fingerprint->mutable_machine_characteristics()->mutable_screen_size()->
141 set_width(1024);
142 return fingerprint.Pass();
145 bool HasAnyError(const ValidityMessages& messages, ServerFieldType field) {
146 return !messages.GetMessageOrDefault(field).text.empty();
149 bool HasUnsureError(const ValidityMessages& messages, ServerFieldType field) {
150 const ValidityMessage& message = messages.GetMessageOrDefault(field);
151 return !message.text.empty() && !message.sure;
154 class TestAutofillDialogView : public AutofillDialogView {
155 public:
156 TestAutofillDialogView()
157 : updates_started_(0), save_details_locally_checked_(true) {}
158 virtual ~TestAutofillDialogView() {}
160 virtual void Show() OVERRIDE {}
161 virtual void Hide() OVERRIDE {}
163 virtual void UpdatesStarted() OVERRIDE {
164 updates_started_++;
167 virtual void UpdatesFinished() OVERRIDE {
168 updates_started_--;
169 EXPECT_GE(updates_started_, 0);
172 virtual void UpdateNotificationArea() OVERRIDE {
173 EXPECT_GE(updates_started_, 1);
176 virtual void UpdateAccountChooser() OVERRIDE {
177 EXPECT_GE(updates_started_, 1);
180 virtual void UpdateButtonStrip() OVERRIDE {
181 EXPECT_GE(updates_started_, 1);
184 virtual void UpdateOverlay() OVERRIDE {
185 EXPECT_GE(updates_started_, 1);
188 virtual void UpdateDetailArea() OVERRIDE {
189 EXPECT_GE(updates_started_, 1);
192 virtual void UpdateSection(DialogSection section) OVERRIDE {
193 section_updates_[section]++;
194 EXPECT_GE(updates_started_, 1);
197 virtual void UpdateErrorBubble() OVERRIDE {
198 EXPECT_GE(updates_started_, 1);
201 virtual void FillSection(DialogSection section,
202 ServerFieldType originating_type) OVERRIDE {}
203 virtual void GetUserInput(DialogSection section, FieldValueMap* output)
204 OVERRIDE {
205 *output = outputs_[section];
208 virtual base::string16 GetCvc() OVERRIDE { return base::string16(); }
210 virtual bool SaveDetailsLocally() OVERRIDE {
211 return save_details_locally_checked_;
214 virtual const content::NavigationController* ShowSignIn() OVERRIDE {
215 return NULL;
217 virtual void HideSignIn() OVERRIDE {}
219 MOCK_METHOD0(ModelChanged, void());
220 MOCK_METHOD0(UpdateForErrors, void());
222 virtual void OnSignInResize(const gfx::Size& pref_size) OVERRIDE {}
223 virtual void ValidateSection(DialogSection) OVERRIDE {}
225 void SetUserInput(DialogSection section, const FieldValueMap& map) {
226 outputs_[section] = map;
229 void CheckSaveDetailsLocallyCheckbox(bool checked) {
230 save_details_locally_checked_ = checked;
233 void ClearSectionUpdates() {
234 section_updates_.clear();
237 std::map<DialogSection, size_t> section_updates() const {
238 return section_updates_;
241 private:
242 std::map<DialogSection, FieldValueMap> outputs_;
243 std::map<DialogSection, size_t> section_updates_;
245 int updates_started_;
246 bool save_details_locally_checked_;
248 DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogView);
251 class TestAutofillDialogController
252 : public AutofillDialogControllerImpl,
253 public base::SupportsWeakPtr<TestAutofillDialogController> {
254 public:
255 TestAutofillDialogController(
256 content::WebContents* contents,
257 const FormData& form_structure,
258 const GURL& source_url,
259 const AutofillMetrics& metric_logger,
260 const AutofillClient::ResultCallback& callback,
261 MockNewCreditCardBubbleController* mock_new_card_bubble_controller)
262 : AutofillDialogControllerImpl(contents,
263 form_structure,
264 source_url,
265 callback),
266 metric_logger_(metric_logger),
267 mock_wallet_client_(
268 Profile::FromBrowserContext(contents->GetBrowserContext())
269 ->GetRequestContext(),
270 this,
271 source_url),
272 mock_new_card_bubble_controller_(mock_new_card_bubble_controller),
273 submit_button_delay_count_(0) {}
275 virtual ~TestAutofillDialogController() {}
277 virtual AutofillDialogView* CreateView() OVERRIDE {
278 return new testing::NiceMock<TestAutofillDialogView>();
281 void Init(content::BrowserContext* browser_context) {
282 test_manager_.Init(
283 WebDataServiceFactory::GetAutofillWebDataForProfile(
284 Profile::FromBrowserContext(browser_context),
285 Profile::EXPLICIT_ACCESS),
286 user_prefs::UserPrefs::Get(browser_context),
287 browser_context->IsOffTheRecord());
290 TestAutofillDialogView* GetView() {
291 return static_cast<TestAutofillDialogView*>(view());
294 TestPersonalDataManager* GetTestingManager() {
295 return &test_manager_;
298 MockAddressValidator* GetMockValidator() {
299 return &mock_validator_;
302 wallet::MockWalletClient* GetTestingWalletClient() {
303 return &mock_wallet_client_;
306 const GURL& open_tab_url() { return open_tab_url_; }
308 void SimulateSigninError() {
309 OnWalletSigninError();
312 // Skips past the 2 second wait between FinishSubmit and DoFinishSubmit.
313 void ForceFinishSubmit() {
314 DoFinishSubmit();
317 void SimulateSubmitButtonDelayBegin() {
318 AutofillDialogControllerImpl::SubmitButtonDelayBegin();
321 void SimulateSubmitButtonDelayEnd() {
322 AutofillDialogControllerImpl::SubmitButtonDelayEndForTesting();
325 using AutofillDialogControllerImpl::
326 ClearLastWalletItemsFetchTimestampForTesting;
328 // Returns the number of times that the submit button was delayed.
329 int get_submit_button_delay_count() const {
330 return submit_button_delay_count_;
333 MOCK_METHOD0(LoadRiskFingerprintData, void());
334 using AutofillDialogControllerImpl::AccountChooserModelForTesting;
335 using AutofillDialogControllerImpl::OnDidLoadRiskFingerprintData;
336 using AutofillDialogControllerImpl::IsEditingExistingData;
337 using AutofillDialogControllerImpl::IsManuallyEditingSection;
338 using AutofillDialogControllerImpl::IsPayingWithWallet;
339 using AutofillDialogControllerImpl::IsSubmitPausedOn;
340 using AutofillDialogControllerImpl::NOT_CHECKED;
341 using AutofillDialogControllerImpl::popup_input_type;
342 using AutofillDialogControllerImpl::SignedInState;
344 protected:
345 virtual PersonalDataManager* GetManager() const OVERRIDE {
346 return const_cast<TestAutofillDialogController*>(this)->
347 GetTestingManager();
350 virtual AddressValidator* GetValidator() OVERRIDE {
351 return &mock_validator_;
354 virtual wallet::WalletClient* GetWalletClient() OVERRIDE {
355 return &mock_wallet_client_;
358 virtual void OpenTabWithUrl(const GURL& url) OVERRIDE {
359 open_tab_url_ = url;
362 virtual void ShowNewCreditCardBubble(
363 scoped_ptr<CreditCard> new_card,
364 scoped_ptr<AutofillProfile> billing_profile) OVERRIDE {
365 mock_new_card_bubble_controller_->Show(new_card.Pass(),
366 billing_profile.Pass());
369 // AutofillDialogControllerImpl calls this method before showing the dialog
370 // window.
371 virtual void SubmitButtonDelayBegin() OVERRIDE {
372 // Do not delay enabling the submit button in testing.
373 submit_button_delay_count_++;
376 private:
377 // To specify our own metric logger.
378 virtual const AutofillMetrics& GetMetricLogger() const OVERRIDE {
379 return metric_logger_;
382 const AutofillMetrics& metric_logger_;
383 TestPersonalDataManager test_manager_;
384 testing::NiceMock<wallet::MockWalletClient> mock_wallet_client_;
386 // A mock validator object to prevent network requests and track when
387 // validation rules are loaded or validation attempts occur.
388 testing::NiceMock<MockAddressValidator> mock_validator_;
390 GURL open_tab_url_;
391 MockNewCreditCardBubbleController* mock_new_card_bubble_controller_;
393 // The number of times that the submit button was delayed.
394 int submit_button_delay_count_;
396 DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogController);
399 class AutofillDialogControllerTest : public ChromeRenderViewHostTestHarness {
400 protected:
401 AutofillDialogControllerTest(): form_structure_(NULL) {}
403 // testing::Test implementation:
404 virtual void SetUp() OVERRIDE {
405 ChromeRenderViewHostTestHarness::SetUp();
406 Reset();
409 virtual void TearDown() OVERRIDE {
410 if (controller_)
411 controller_->ViewClosed();
412 ChromeRenderViewHostTestHarness::TearDown();
415 void Reset() {
416 if (controller_)
417 controller_->ViewClosed();
419 test_generated_bubble_controller_ =
420 new testing::NiceMock<TestGeneratedCreditCardBubbleController>(
421 web_contents());
422 ASSERT_TRUE(test_generated_bubble_controller_->IsInstalled());
424 mock_new_card_bubble_controller_.reset(
425 new MockNewCreditCardBubbleController);
427 profile()->GetPrefs()->ClearPref(::prefs::kAutofillDialogSaveData);
429 // We have to clear the old local state before creating a new one.
430 scoped_local_state_.reset();
431 scoped_local_state_.reset(new ScopedTestingLocalState(
432 TestingBrowserProcess::GetGlobal()));
434 SetUpControllerWithFormData(DefaultFormData());
437 FormData DefaultFormData() {
438 FormData form_data;
439 for (size_t i = 0; i < arraysize(kFieldsFromPage); ++i) {
440 FormFieldData field;
441 field.autocomplete_attribute = kFieldsFromPage[i];
442 form_data.fields.push_back(field);
444 return form_data;
447 // Creates a new controller for |form_data|.
448 void ResetControllerWithFormData(const FormData& form_data) {
449 if (controller_)
450 controller_->ViewClosed();
452 AutofillClient::ResultCallback callback =
453 base::Bind(&AutofillDialogControllerTest::FinishedCallback,
454 base::Unretained(this));
455 controller_ = (new testing::NiceMock<TestAutofillDialogController>(
456 web_contents(),
457 form_data,
458 GURL(kSourceUrl),
459 metric_logger_,
460 callback,
461 mock_new_card_bubble_controller_.get()))->AsWeakPtr();
462 controller_->Init(profile());
465 // Creates a new controller for |form_data| and sets up some initial wallet
466 // data for it.
467 void SetUpControllerWithFormData(const FormData& form_data) {
468 ResetControllerWithFormData(form_data);
469 controller()->Show();
470 if (controller() &&
471 !profile()->GetPrefs()->GetBoolean(
472 ::prefs::kAutofillDialogPayWithoutWallet)) {
473 EXPECT_CALL(*controller()->GetTestingWalletClient(),
474 GetWalletItems(_, _));
475 controller()->OnDidFetchWalletCookieValue(std::string());
476 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
480 // Fills the inputs in SECTION_CC with data.
481 void FillCreditCardInputs() {
482 FieldValueMap cc_outputs;
483 const DetailInputs& cc_inputs =
484 controller()->RequestedFieldsForSection(SECTION_CC);
485 for (size_t i = 0; i < cc_inputs.size(); ++i) {
486 cc_outputs[cc_inputs[i].type] = cc_inputs[i].type == CREDIT_CARD_NUMBER ?
487 ASCIIToUTF16(kTestCCNumberVisa) : ASCIIToUTF16("11");
489 controller()->GetView()->SetUserInput(SECTION_CC, cc_outputs);
492 // Fills the inputs in SECTION_CC_BILLING with valid data.
493 void FillCCBillingInputs() {
494 FieldValueMap outputs;
495 const DetailInputs& inputs =
496 controller()->RequestedFieldsForSection(SECTION_CC_BILLING);
497 AutofillProfile full_profile(test::GetVerifiedProfile());
498 CreditCard full_card(test::GetCreditCard());
499 for (size_t i = 0; i < inputs.size(); ++i) {
500 const ServerFieldType type = inputs[i].type;
501 outputs[type] = full_profile.GetInfo(AutofillType(type), "en-US");
503 if (outputs[type].empty())
504 outputs[type] = full_card.GetInfo(AutofillType(type), "en-US");
506 controller()->GetView()->SetUserInput(SECTION_CC_BILLING, outputs);
509 // Activates the 'Add new foo' option from the |section|'s suggestions
510 // dropdown and fills the |section|'s inputs with the data from the
511 // |data_model|. If |section| is SECTION_CC, also fills in '123' for the CVC.
512 void FillInputs(DialogSection section, const AutofillDataModel& data_model) {
513 // Select the 'Add new foo' option.
514 ui::MenuModel* model = GetMenuModelForSection(section);
515 if (model)
516 model->ActivatedAt(model->GetItemCount() - 2);
518 // Fill the inputs.
519 FieldValueMap outputs;
520 const DetailInputs& inputs =
521 controller()->RequestedFieldsForSection(section);
522 for (size_t i = 0; i < inputs.size(); ++i) {
523 ServerFieldType type = inputs[i].type;
524 base::string16 output;
525 if (type == CREDIT_CARD_VERIFICATION_CODE)
526 output = ASCIIToUTF16("123");
527 else
528 output = data_model.GetInfo(AutofillType(type), "en-US");
529 outputs[inputs[i].type] = output;
531 controller()->GetView()->SetUserInput(section, outputs);
534 std::vector<DialogNotification> NotificationsOfType(
535 DialogNotification::Type type) {
536 std::vector<DialogNotification> right_type;
537 const std::vector<DialogNotification>& notifications =
538 controller()->CurrentNotifications();
539 for (size_t i = 0; i < notifications.size(); ++i) {
540 if (notifications[i].type() == type)
541 right_type.push_back(notifications[i]);
543 return right_type;
546 void SwitchToAutofill() {
547 ui::MenuModel* model = controller_->MenuModelForAccountChooser();
548 model->ActivatedAt(model->GetItemCount() - 1);
551 void SwitchToWallet() {
552 controller_->MenuModelForAccountChooser()->ActivatedAt(0);
555 void SimulateSigninError() {
556 controller_->SimulateSigninError();
559 void UseBillingForShipping() {
560 controller()->MenuModelForSection(SECTION_SHIPPING)->ActivatedAt(0);
563 base::string16 ValidateCCNumber(DialogSection section,
564 const std::string& cc_number,
565 bool should_pass) {
566 FieldValueMap outputs;
567 outputs[ADDRESS_BILLING_COUNTRY] = ASCIIToUTF16("United States");
568 outputs[CREDIT_CARD_NUMBER] = UTF8ToUTF16(cc_number);
569 ValidityMessages messages =
570 controller()->InputsAreValid(section, outputs);
571 EXPECT_EQ(should_pass, !messages.HasSureError(CREDIT_CARD_NUMBER));
572 return messages.GetMessageOrDefault(CREDIT_CARD_NUMBER).text;
575 void SubmitWithWalletItems(scoped_ptr<wallet::WalletItems> wallet_items) {
576 controller()->OnDidGetWalletItems(wallet_items.Pass());
577 AcceptAndLoadFakeFingerprint();
580 void AcceptAndLoadFakeFingerprint() {
581 controller()->OnAccept();
582 controller()->OnDidLoadRiskFingerprintData(GetFakeFingerprint().Pass());
585 // Returns true if the given |section| contains a field of the given |type|.
586 bool SectionContainsField(DialogSection section, ServerFieldType type) {
587 const DetailInputs& inputs =
588 controller()->RequestedFieldsForSection(section);
589 for (DetailInputs::const_iterator it = inputs.begin(); it != inputs.end();
590 ++it) {
591 if (it->type == type)
592 return true;
594 return false;
597 SuggestionsMenuModel* GetMenuModelForSection(DialogSection section) {
598 ui::MenuModel* model = controller()->MenuModelForSection(section);
599 return static_cast<SuggestionsMenuModel*>(model);
602 void SubmitAndVerifyShippingAndBillingResults() {
603 // Test after setting use billing for shipping.
604 UseBillingForShipping();
606 controller()->OnAccept();
608 ASSERT_EQ(20U, form_structure()->field_count());
609 EXPECT_EQ(ADDRESS_HOME_COUNTRY,
610 form_structure()->field(11)->Type().GetStorableType());
611 EXPECT_EQ(ADDRESS_BILLING, form_structure()->field(11)->Type().group());
612 EXPECT_EQ(ADDRESS_HOME_COUNTRY,
613 form_structure()->field(18)->Type().GetStorableType());
614 EXPECT_EQ(ADDRESS_HOME, form_structure()->field(18)->Type().group());
615 base::string16 billing_country = form_structure()->field(11)->value;
616 EXPECT_EQ(2U, billing_country.size());
617 base::string16 shipping_country = form_structure()->field(18)->value;
618 EXPECT_EQ(2U, shipping_country.size());
619 EXPECT_FALSE(billing_country.empty());
620 EXPECT_FALSE(shipping_country.empty());
621 EXPECT_EQ(billing_country, shipping_country);
623 EXPECT_EQ(CREDIT_CARD_NAME,
624 form_structure()->field(1)->Type().GetStorableType());
625 base::string16 cc_name = form_structure()->field(1)->value;
626 EXPECT_EQ(NAME_FULL, form_structure()->field(6)->Type().GetStorableType());
627 EXPECT_EQ(NAME_BILLING, form_structure()->field(6)->Type().group());
628 base::string16 billing_name = form_structure()->field(6)->value;
629 EXPECT_EQ(NAME_FULL, form_structure()->field(13)->Type().GetStorableType());
630 EXPECT_EQ(NAME, form_structure()->field(13)->Type().group());
631 base::string16 shipping_name = form_structure()->field(13)->value;
633 EXPECT_FALSE(cc_name.empty());
634 EXPECT_FALSE(billing_name.empty());
635 EXPECT_FALSE(shipping_name.empty());
636 EXPECT_EQ(cc_name, billing_name);
637 EXPECT_EQ(cc_name, shipping_name);
640 TestAutofillDialogController* controller() { return controller_.get(); }
642 const FormStructure* form_structure() { return form_structure_; }
644 TestGeneratedCreditCardBubbleController* test_generated_bubble_controller() {
645 return test_generated_bubble_controller_;
648 const MockNewCreditCardBubbleController* mock_new_card_bubble_controller() {
649 return mock_new_card_bubble_controller_.get();
652 private:
653 void FinishedCallback(AutofillClient::RequestAutocompleteResult result,
654 const base::string16& debug_message,
655 const FormStructure* form_structure) {
656 form_structure_ = form_structure;
659 #if defined(OS_WIN)
660 // http://crbug.com/227221
661 ui::ScopedOleInitializer ole_initializer_;
662 #endif
664 // The controller owns itself.
665 base::WeakPtr<TestAutofillDialogController> controller_;
667 // Must outlive the controller.
668 AutofillMetrics metric_logger_;
670 // Returned when the dialog closes successfully.
671 const FormStructure* form_structure_;
673 // Used to monitor if the Autofill credit card bubble is shown. Owned by
674 // |web_contents()|.
675 TestGeneratedCreditCardBubbleController* test_generated_bubble_controller_;
677 // Used to record when new card bubbles would show. Created in |Reset()|.
678 scoped_ptr<MockNewCreditCardBubbleController>
679 mock_new_card_bubble_controller_;
681 scoped_ptr<ScopedTestingLocalState> scoped_local_state_;
684 } // namespace
686 TEST_F(AutofillDialogControllerTest, RefuseToShowWithNoAutocompleteAttributes) {
687 FormFieldData email_field;
688 email_field.name = ASCIIToUTF16("email");
689 FormFieldData cc_field;
690 cc_field.name = ASCIIToUTF16("cc");
691 FormFieldData billing_field;
692 billing_field.name = ASCIIToUTF16("billing name");
694 FormData form_data;
695 form_data.fields.push_back(email_field);
696 form_data.fields.push_back(cc_field);
697 form_data.fields.push_back(billing_field);
699 SetUpControllerWithFormData(form_data);
700 EXPECT_FALSE(controller());
703 TEST_F(AutofillDialogControllerTest, RefuseToShowWithNoCcField) {
704 FormFieldData shipping_tel;
705 shipping_tel.autocomplete_attribute = "shipping tel";
707 FormData form_data;
708 form_data.fields.push_back(shipping_tel);
710 SetUpControllerWithFormData(form_data);
711 EXPECT_FALSE(controller());
713 // Any cc- field will do.
714 FormFieldData cc_field;
715 cc_field.autocomplete_attribute = "cc-csc";
716 form_data.fields.push_back(cc_field);
718 SetUpControllerWithFormData(form_data);
719 EXPECT_TRUE(controller());
722 // Ensure the default ValidityMessage has the expected values.
723 TEST_F(AutofillDialogControllerTest, DefaultValidityMessage) {
724 ValidityMessages messages;
725 ValidityMessage message = messages.GetMessageOrDefault(UNKNOWN_TYPE);
726 EXPECT_FALSE(message.sure);
727 EXPECT_TRUE(message.text.empty());
730 // This test makes sure nothing falls over when fields are being validity-
731 // checked.
732 TEST_F(AutofillDialogControllerTest, ValidityCheck) {
733 for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
734 DialogSection section = static_cast<DialogSection>(i);
735 const DetailInputs& shipping_inputs =
736 controller()->RequestedFieldsForSection(section);
737 for (DetailInputs::const_iterator iter = shipping_inputs.begin();
738 iter != shipping_inputs.end(); ++iter) {
739 controller()->InputValidityMessage(section, iter->type, base::string16());
744 // Test for phone number validation.
745 TEST_F(AutofillDialogControllerTest, PhoneNumberValidation) {
746 // Construct FieldValueMap from existing data.
747 SwitchToAutofill();
749 for (size_t i = 0; i < 2; ++i) {
750 ServerFieldType phone = i == 0 ? PHONE_HOME_WHOLE_NUMBER :
751 PHONE_BILLING_WHOLE_NUMBER;
752 ServerFieldType address = i == 0 ? ADDRESS_HOME_COUNTRY :
753 ADDRESS_BILLING_COUNTRY;
754 DialogSection section = i == 0 ? SECTION_SHIPPING : SECTION_BILLING;
756 FieldValueMap outputs;
757 const DetailInputs& inputs =
758 controller()->RequestedFieldsForSection(section);
759 AutofillProfile full_profile(test::GetVerifiedProfile());
760 for (size_t i = 0; i < inputs.size(); ++i) {
761 const ServerFieldType type = inputs[i].type;
762 outputs[type] = full_profile.GetInfo(AutofillType(type), "en-US");
765 // Make sure country is United States.
766 outputs[address] = ASCIIToUTF16("United States");
768 // Existing data should have no errors.
769 ValidityMessages messages = controller()->InputsAreValid(section, outputs);
770 EXPECT_FALSE(HasAnyError(messages, phone));
772 // Input an empty phone number.
773 outputs[phone] = base::string16();
774 messages = controller()->InputsAreValid(section, outputs);
775 EXPECT_TRUE(HasUnsureError(messages, phone));
777 // Input an invalid phone number.
778 outputs[phone] = ASCIIToUTF16("ABC");
779 messages = controller()->InputsAreValid(section, outputs);
780 EXPECT_TRUE(messages.HasSureError(phone));
782 // Input a local phone number.
783 outputs[phone] = ASCIIToUTF16("2155546699");
784 messages = controller()->InputsAreValid(section, outputs);
785 EXPECT_FALSE(HasAnyError(messages, phone));
787 // Input an invalid local phone number.
788 outputs[phone] = ASCIIToUTF16("215554669");
789 messages = controller()->InputsAreValid(section, outputs);
790 EXPECT_TRUE(messages.HasSureError(phone));
792 // Input an international phone number.
793 outputs[phone] = ASCIIToUTF16("+33 892 70 12 39");
794 messages = controller()->InputsAreValid(section, outputs);
795 EXPECT_FALSE(HasAnyError(messages, phone));
797 // Input an invalid international phone number.
798 outputs[phone] = ASCIIToUTF16("+112333 892 70 12 39");
799 messages = controller()->InputsAreValid(section, outputs);
800 EXPECT_TRUE(messages.HasSureError(phone));
802 // Input a valid Canadian number.
803 outputs[phone] = ASCIIToUTF16("+1 506 887 1234");
804 messages = controller()->InputsAreValid(section, outputs);
805 EXPECT_FALSE(HasAnyError(messages, phone));
807 // Input a valid Canadian number without the country code.
808 outputs[phone] = ASCIIToUTF16("506 887 1234");
809 messages = controller()->InputsAreValid(section, outputs);
810 EXPECT_TRUE(HasAnyError(messages, phone));
812 // Input a valid Canadian toll-free number.
813 outputs[phone] = ASCIIToUTF16("310 1234");
814 messages = controller()->InputsAreValid(section, outputs);
815 EXPECT_TRUE(HasAnyError(messages, phone));
819 TEST_F(AutofillDialogControllerTest, ExpirationDateValidity) {
820 ui::ComboboxModel* exp_year_model =
821 controller()->ComboboxModelForAutofillType(CREDIT_CARD_EXP_4_DIGIT_YEAR);
822 ui::ComboboxModel* exp_month_model =
823 controller()->ComboboxModelForAutofillType(CREDIT_CARD_EXP_MONTH);
825 base::string16 default_year_value =
826 exp_year_model->GetItemAt(exp_year_model->GetDefaultIndex());
827 base::string16 default_month_value =
828 exp_month_model->GetItemAt(exp_month_model->GetDefaultIndex());
830 base::string16 other_year_value =
831 exp_year_model->GetItemAt(exp_year_model->GetItemCount() - 1);
832 base::string16 other_month_value =
833 exp_month_model->GetItemAt(exp_month_model->GetItemCount() - 1);
835 FieldValueMap outputs;
836 outputs[ADDRESS_BILLING_COUNTRY] = ASCIIToUTF16("United States");
837 outputs[CREDIT_CARD_EXP_MONTH] = default_month_value;
838 outputs[CREDIT_CARD_EXP_4_DIGIT_YEAR] = default_year_value;
840 // Expiration default values generate unsure validation errors (but not sure).
841 ValidityMessages messages = controller()->InputsAreValid(SECTION_CC_BILLING,
842 outputs);
843 EXPECT_TRUE(HasUnsureError(messages, CREDIT_CARD_EXP_4_DIGIT_YEAR));
844 EXPECT_TRUE(HasUnsureError(messages, CREDIT_CARD_EXP_MONTH));
846 // Expiration date with default month fails.
847 outputs[CREDIT_CARD_EXP_4_DIGIT_YEAR] = other_year_value;
848 messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
849 EXPECT_FALSE(HasUnsureError(messages, CREDIT_CARD_EXP_4_DIGIT_YEAR));
850 EXPECT_TRUE(HasUnsureError(messages, CREDIT_CARD_EXP_MONTH));
852 // Expiration date with default year fails.
853 outputs[CREDIT_CARD_EXP_MONTH] = other_month_value;
854 outputs[CREDIT_CARD_EXP_4_DIGIT_YEAR] = default_year_value;
855 messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
856 EXPECT_TRUE(HasUnsureError(messages, CREDIT_CARD_EXP_4_DIGIT_YEAR));
857 EXPECT_FALSE(HasUnsureError(messages, CREDIT_CARD_EXP_MONTH));
860 TEST_F(AutofillDialogControllerTest, BillingNameValidation) {
861 // Construct FieldValueMap from AutofillProfile data.
862 SwitchToAutofill();
864 FieldValueMap outputs;
865 outputs[ADDRESS_BILLING_COUNTRY] = ASCIIToUTF16("United States");
867 // Input an empty billing name.
868 outputs[NAME_BILLING_FULL] = base::string16();
869 ValidityMessages messages = controller()->InputsAreValid(SECTION_BILLING,
870 outputs);
871 EXPECT_TRUE(HasUnsureError(messages, NAME_BILLING_FULL));
873 // Input a non-empty billing name.
874 outputs[NAME_BILLING_FULL] = ASCIIToUTF16("Bob");
875 messages = controller()->InputsAreValid(SECTION_BILLING, outputs);
876 EXPECT_FALSE(HasAnyError(messages, NAME_BILLING_FULL));
878 // Switch to Wallet which only considers names with with at least two names to
879 // be valid.
880 SwitchToWallet();
882 // Setup some wallet state.
883 scoped_ptr<wallet::WalletItems> wallet_items =
884 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
885 controller()->OnDidGetWalletItems(wallet_items.Pass());
887 // Input an empty billing name. Data source should not change this behavior.
888 outputs[NAME_BILLING_FULL] = base::string16();
889 messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
890 EXPECT_TRUE(HasUnsureError(messages, NAME_BILLING_FULL));
892 // Input a one name billing name. Wallet does not currently support this.
893 outputs[NAME_BILLING_FULL] = ASCIIToUTF16("Bob");
894 messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
895 EXPECT_TRUE(messages.HasSureError(NAME_BILLING_FULL));
897 // Input a two name billing name.
898 outputs[NAME_BILLING_FULL] = ASCIIToUTF16("Bob Barker");
899 messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
900 EXPECT_FALSE(HasAnyError(messages, NAME_BILLING_FULL));
902 // Input a more than two name billing name.
903 outputs[NAME_BILLING_FULL] = ASCIIToUTF16("John Jacob Jingleheimer Schmidt"),
904 messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
905 EXPECT_FALSE(HasAnyError(messages, NAME_BILLING_FULL));
907 // Input a billing name with lots of crazy whitespace.
908 outputs[NAME_BILLING_FULL] =
909 ASCIIToUTF16(" \\n\\r John \\n Jacob Jingleheimer \\t Schmidt "),
910 messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
911 EXPECT_FALSE(HasAnyError(messages, NAME_BILLING_FULL));
914 TEST_F(AutofillDialogControllerTest, CreditCardNumberValidation) {
915 // Construct FieldValueMap from AutofillProfile data.
916 SwitchToAutofill();
918 // Should accept AMEX, Visa, Master and Discover.
919 ValidateCCNumber(SECTION_CC, kTestCCNumberVisa, true);
920 ValidateCCNumber(SECTION_CC, kTestCCNumberMaster, true);
921 ValidateCCNumber(SECTION_CC, kTestCCNumberDiscover, true);
922 ValidateCCNumber(SECTION_CC, kTestCCNumberAmex, true);
923 ValidateCCNumber(SECTION_CC, kTestCCNumberIncomplete, false);
924 ValidateCCNumber(SECTION_CC, kTestCCNumberInvalid, false);
926 // Switch to Wallet which will not accept AMEX.
927 SwitchToWallet();
929 // Setup some wallet state on a merchant for which Wallet doesn't
930 // support AMEX.
931 controller()->OnDidGetWalletItems(
932 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED));
934 // Should accept Visa, Master and Discover, but not AMEX.
935 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberVisa, true);
936 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberMaster, true);
937 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberDiscover, true);
938 EXPECT_EQ(l10n_util::GetStringUTF16(
939 IDS_AUTOFILL_CREDIT_CARD_NOT_SUPPORTED_BY_WALLET_FOR_MERCHANT),
940 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberAmex, false));
941 EXPECT_EQ(
942 l10n_util::GetStringUTF16(
943 IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_NUMBER),
944 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberIncomplete, false));
945 EXPECT_EQ(
946 l10n_util::GetStringUTF16(
947 IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_NUMBER),
948 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberInvalid, false));
950 // Setup some wallet state on a merchant for which Wallet supports AMEX.
951 controller()->OnDidGetWalletItems(
952 wallet::GetTestWalletItems(wallet::AMEX_ALLOWED));
954 // Should accept Visa, Master, Discover, and AMEX.
955 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberVisa, true);
956 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberMaster, true);
957 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberDiscover, true);
958 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberAmex, true);
959 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberIncomplete, false);
960 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberInvalid, false);
963 TEST_F(AutofillDialogControllerTest, AutofillProfiles) {
964 SwitchToAutofill();
965 ui::MenuModel* shipping_model =
966 controller()->MenuModelForSection(SECTION_SHIPPING);
967 // Since the PersonalDataManager is empty, this should only have the
968 // "use billing", "add new" and "manage" menu items.
969 ASSERT_TRUE(shipping_model);
970 EXPECT_EQ(3, shipping_model->GetItemCount());
971 // On the other hand, the other models should be NULL when there's no
972 // suggestion.
973 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC));
974 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_BILLING));
976 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(3);
978 // Empty profiles are ignored.
979 AutofillProfile empty_profile(base::GenerateGUID(), kSettingsOrigin);
980 empty_profile.SetRawInfo(NAME_FULL, ASCIIToUTF16("John Doe"));
981 controller()->GetTestingManager()->AddTestingProfile(&empty_profile);
982 shipping_model = controller()->MenuModelForSection(SECTION_SHIPPING);
983 ASSERT_TRUE(shipping_model);
984 EXPECT_EQ(3, shipping_model->GetItemCount());
986 // An otherwise full but unverified profile should be ignored.
987 AutofillProfile full_profile(test::GetFullProfile());
988 full_profile.set_origin("https://www.example.com");
989 full_profile.SetRawInfo(ADDRESS_HOME_LINE2, base::string16());
990 controller()->GetTestingManager()->AddTestingProfile(&full_profile);
991 shipping_model = controller()->MenuModelForSection(SECTION_SHIPPING);
992 ASSERT_TRUE(shipping_model);
993 EXPECT_EQ(3, shipping_model->GetItemCount());
995 // A full, verified profile should be picked up.
996 AutofillProfile verified_profile(test::GetVerifiedProfile());
997 verified_profile.SetRawInfo(ADDRESS_HOME_LINE2, base::string16());
998 controller()->GetTestingManager()->AddTestingProfile(&verified_profile);
999 shipping_model = controller()->MenuModelForSection(SECTION_SHIPPING);
1000 ASSERT_TRUE(shipping_model);
1001 EXPECT_EQ(4, shipping_model->GetItemCount());
1004 // Checks that a valid profile is selected by default, but if invalid is
1005 // popped into edit mode.
1006 TEST_F(AutofillDialogControllerTest, AutofillProfilesPopInvalidIntoEdit) {
1007 SwitchToAutofill();
1008 SuggestionsMenuModel* shipping_model =
1009 GetMenuModelForSection(SECTION_SHIPPING);
1010 EXPECT_EQ(3, shipping_model->GetItemCount());
1011 // "Same as billing" is selected.
1012 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_SHIPPING));
1013 EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_BILLING));
1015 AutofillProfile verified_profile(test::GetVerifiedProfile());
1016 controller()->GetTestingManager()->AddTestingProfile(&verified_profile);
1017 EXPECT_EQ(4, shipping_model->GetItemCount());
1018 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_SHIPPING));
1019 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_BILLING));
1021 // Now make up a problem and make sure the profile isn't in the list.
1022 Reset();
1023 SwitchToAutofill();
1024 AddressProblems problems;
1025 problems.push_back(
1026 AddressProblem(::i18n::addressinput::POSTAL_CODE,
1027 AddressProblem::MISMATCHING_VALUE,
1028 IDS_LEARN_MORE));
1029 EXPECT_CALL(*controller()->GetMockValidator(),
1030 ValidateAddress(CountryCodeMatcher("US"), _, _)).
1031 WillRepeatedly(DoAll(SetArgPointee<2>(problems),
1032 Return(AddressValidator::SUCCESS)));
1034 controller()->GetTestingManager()->AddTestingProfile(&verified_profile);
1035 shipping_model = GetMenuModelForSection(SECTION_SHIPPING);
1036 EXPECT_EQ(4, shipping_model->GetItemCount());
1037 EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_SHIPPING));
1038 EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_BILLING));
1041 // Makes sure suggestion profiles are re-validated when validation rules load.
1042 TEST_F(AutofillDialogControllerTest, AutofillProfilesRevalidateAfterRulesLoad) {
1043 SwitchToAutofill();
1044 SuggestionsMenuModel* shipping_model =
1045 GetMenuModelForSection(SECTION_SHIPPING);
1046 EXPECT_EQ(3, shipping_model->GetItemCount());
1047 // "Same as billing" is selected.
1048 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_SHIPPING));
1049 EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_BILLING));
1050 AutofillProfile verified_profile(test::GetVerifiedProfile());
1051 controller()->GetTestingManager()->AddTestingProfile(&verified_profile);
1052 EXPECT_EQ(4, shipping_model->GetItemCount());
1053 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_SHIPPING));
1054 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_BILLING));
1056 AddressProblems problems;
1057 problems.push_back(
1058 AddressProblem(::i18n::addressinput::POSTAL_CODE,
1059 AddressProblem::MISMATCHING_VALUE,
1060 IDS_LEARN_MORE));
1061 EXPECT_CALL(*controller()->GetMockValidator(),
1062 ValidateAddress(CountryCodeMatcher("US"), _, _)).
1063 WillRepeatedly(DoAll(SetArgPointee<2>(problems),
1064 Return(AddressValidator::SUCCESS)));
1066 controller()->OnAddressValidationRulesLoaded("US", true);
1067 EXPECT_EQ(4, shipping_model->GetItemCount());
1068 EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_SHIPPING));
1069 EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_BILLING));
1072 // Makes sure that the choice of which Autofill profile to use for each section
1073 // is sticky.
1074 TEST_F(AutofillDialogControllerTest, AutofillProfileDefaults) {
1075 SwitchToAutofill();
1076 AutofillProfile profile(test::GetVerifiedProfile());
1077 AutofillProfile profile2(test::GetVerifiedProfile2());
1078 controller()->GetTestingManager()->AddTestingProfile(&profile);
1079 controller()->GetTestingManager()->AddTestingProfile(&profile2);
1081 // Until a selection has been made, the default shipping suggestion is the
1082 // first one (after "use billing").
1083 SuggestionsMenuModel* shipping_model =
1084 GetMenuModelForSection(SECTION_SHIPPING);
1085 EXPECT_EQ(1, shipping_model->checked_item());
1087 for (int i = 2; i >= 0; --i) {
1088 shipping_model = GetMenuModelForSection(SECTION_SHIPPING);
1089 shipping_model->ExecuteCommand(i, 0);
1090 FillCreditCardInputs();
1091 controller()->OnAccept();
1093 Reset();
1094 controller()->GetTestingManager()->AddTestingProfile(&profile);
1095 controller()->GetTestingManager()->AddTestingProfile(&profile2);
1096 shipping_model = GetMenuModelForSection(SECTION_SHIPPING);
1097 EXPECT_EQ(i, shipping_model->checked_item());
1100 // Try again, but don't add the default profile to the PDM. The dialog
1101 // should fall back to the first profile.
1102 shipping_model->ExecuteCommand(2, 0);
1103 FillCreditCardInputs();
1104 controller()->OnAccept();
1105 Reset();
1106 controller()->GetTestingManager()->AddTestingProfile(&profile);
1107 shipping_model = GetMenuModelForSection(SECTION_SHIPPING);
1108 EXPECT_EQ(1, shipping_model->checked_item());
1111 // Makes sure that a newly added Autofill profile becomes set as the default
1112 // choice for the next run.
1113 TEST_F(AutofillDialogControllerTest, NewAutofillProfileIsDefault) {
1114 SwitchToAutofill();
1116 AutofillProfile profile(test::GetVerifiedProfile());
1117 CreditCard credit_card(test::GetVerifiedCreditCard());
1118 controller()->GetTestingManager()->AddTestingProfile(&profile);
1119 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
1121 // Until a selection has been made, the default suggestion is the first one.
1122 // For the shipping section, this follows the "use billing" suggestion.
1123 EXPECT_EQ(0, GetMenuModelForSection(SECTION_CC)->checked_item());
1124 EXPECT_EQ(1, GetMenuModelForSection(SECTION_SHIPPING)->checked_item());
1126 // Fill in the shipping and credit card sections with new data.
1127 AutofillProfile new_profile(test::GetVerifiedProfile2());
1128 CreditCard new_credit_card(test::GetVerifiedCreditCard2());
1129 FillInputs(SECTION_SHIPPING, new_profile);
1130 FillInputs(SECTION_CC, new_credit_card);
1131 controller()->GetView()->CheckSaveDetailsLocallyCheckbox(true);
1132 controller()->OnAccept();
1134 // Update the |new_profile| and |new_credit_card|'s guids to the saved ones.
1135 new_profile.set_guid(
1136 controller()->GetTestingManager()->imported_profile().guid());
1137 new_credit_card.set_guid(
1138 controller()->GetTestingManager()->imported_credit_card().guid());
1140 // Reload the dialog. The newly added address and credit card should now be
1141 // set as the defaults.
1142 Reset();
1143 controller()->GetTestingManager()->AddTestingProfile(&profile);
1144 controller()->GetTestingManager()->AddTestingProfile(&new_profile);
1145 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
1146 controller()->GetTestingManager()->AddTestingCreditCard(&new_credit_card);
1148 // Until a selection has been made, the default suggestion is the first one.
1149 // For the shipping section, this follows the "use billing" suggestion.
1150 EXPECT_EQ(1, GetMenuModelForSection(SECTION_CC)->checked_item());
1151 EXPECT_EQ(2, GetMenuModelForSection(SECTION_SHIPPING)->checked_item());
1154 TEST_F(AutofillDialogControllerTest, AutofillProfileVariants) {
1155 SwitchToAutofill();
1156 EXPECT_CALL(*controller()->GetView(), ModelChanged());
1157 ui::MenuModel* shipping_model =
1158 controller()->MenuModelForSection(SECTION_SHIPPING);
1159 ASSERT_TRUE(!!shipping_model);
1160 EXPECT_EQ(3, shipping_model->GetItemCount());
1162 // Set up some variant data.
1163 AutofillProfile full_profile(test::GetVerifiedProfile());
1164 std::vector<base::string16> names;
1165 names.push_back(ASCIIToUTF16("John Doe"));
1166 names.push_back(ASCIIToUTF16("Jane Doe"));
1167 full_profile.SetRawMultiInfo(NAME_FULL, names);
1168 std::vector<base::string16> emails;
1169 emails.push_back(ASCIIToUTF16(kFakeEmail));
1170 emails.push_back(ASCIIToUTF16("admin@example.com"));
1171 full_profile.SetRawMultiInfo(EMAIL_ADDRESS, emails);
1173 // Non-default variants are ignored by the dialog.
1174 controller()->GetTestingManager()->AddTestingProfile(&full_profile);
1175 EXPECT_EQ(4, shipping_model->GetItemCount());
1178 TEST_F(AutofillDialogControllerTest, SuggestValidEmail) {
1179 SwitchToAutofill();
1180 AutofillProfile profile(test::GetVerifiedProfile());
1181 const base::string16 kValidEmail = ASCIIToUTF16(kFakeEmail);
1182 profile.SetRawInfo(EMAIL_ADDRESS, kValidEmail);
1183 controller()->GetTestingManager()->AddTestingProfile(&profile);
1185 // "add", "manage", and 1 suggestion.
1186 EXPECT_EQ(
1187 3, controller()->MenuModelForSection(SECTION_BILLING)->GetItemCount());
1188 // "add", "manage", 1 suggestion, and "same as billing".
1189 EXPECT_EQ(
1190 4, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
1193 TEST_F(AutofillDialogControllerTest, DoNotSuggestInvalidEmail) {
1194 SwitchToAutofill();
1195 AutofillProfile profile(test::GetVerifiedProfile());
1196 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16(".!#$%&'*+/=?^_`-@-.."));
1197 controller()->GetTestingManager()->AddTestingProfile(&profile);
1199 EXPECT_FALSE(!!controller()->MenuModelForSection(SECTION_BILLING));
1200 // "add", "manage", 1 suggestion, and "same as billing".
1201 EXPECT_EQ(
1202 4, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
1205 TEST_F(AutofillDialogControllerTest, SuggestValidAddress) {
1206 SwitchToAutofill();
1207 AutofillProfile full_profile(test::GetVerifiedProfile());
1208 full_profile.set_origin(kSettingsOrigin);
1209 controller()->GetTestingManager()->AddTestingProfile(&full_profile);
1210 // "add", "manage", and 1 suggestion.
1211 EXPECT_EQ(
1212 3, controller()->MenuModelForSection(SECTION_BILLING)->GetItemCount());
1215 TEST_F(AutofillDialogControllerTest, DoNotSuggestInvalidAddress) {
1216 SwitchToAutofill();
1217 AutofillProfile full_profile(test::GetVerifiedProfile());
1218 full_profile.set_origin(kSettingsOrigin);
1219 full_profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("C"));
1220 controller()->GetTestingManager()->AddTestingProfile(&full_profile);
1223 TEST_F(AutofillDialogControllerTest, DoNotSuggestIncompleteAddress) {
1224 SwitchToAutofill();
1225 AutofillProfile profile(test::GetVerifiedProfile());
1226 profile.SetRawInfo(ADDRESS_HOME_STATE, base::string16());
1227 controller()->GetTestingManager()->AddTestingProfile(&profile);
1229 // Same as shipping, manage, add new.
1230 EXPECT_EQ(3,
1231 controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
1232 EXPECT_FALSE(!!controller()->MenuModelForSection(SECTION_BILLING));
1235 TEST_F(AutofillDialogControllerTest, DoSuggestShippingAddressWithoutEmail) {
1236 SwitchToAutofill();
1237 AutofillProfile profile(test::GetVerifiedProfile());
1238 profile.SetRawInfo(EMAIL_ADDRESS, base::string16());
1239 controller()->GetTestingManager()->AddTestingProfile(&profile);
1241 // Same as shipping, manage, add new, profile with missing email.
1242 EXPECT_EQ(4,
1243 controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
1244 // Billing addresses require email.
1245 EXPECT_FALSE(!!controller()->MenuModelForSection(SECTION_BILLING));
1248 TEST_F(AutofillDialogControllerTest, AutofillCreditCards) {
1249 SwitchToAutofill();
1250 // Since the PersonalDataManager is empty, this should only have the
1251 // default menu items.
1252 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC));
1254 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(3);
1256 // Empty cards are ignored.
1257 CreditCard empty_card(base::GenerateGUID(), kSettingsOrigin);
1258 empty_card.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("John Doe"));
1259 controller()->GetTestingManager()->AddTestingCreditCard(&empty_card);
1260 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC));
1262 // An otherwise full but unverified card should be ignored.
1263 CreditCard full_card(test::GetCreditCard());
1264 full_card.set_origin("https://www.example.com");
1265 controller()->GetTestingManager()->AddTestingCreditCard(&full_card);
1266 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC));
1268 // A full, verified card should be picked up.
1269 CreditCard verified_card(test::GetCreditCard());
1270 verified_card.set_origin(kSettingsOrigin);
1271 controller()->GetTestingManager()->AddTestingCreditCard(&verified_card);
1272 ui::MenuModel* credit_card_model =
1273 controller()->MenuModelForSection(SECTION_CC);
1274 ASSERT_TRUE(credit_card_model);
1275 EXPECT_EQ(3, credit_card_model->GetItemCount());
1278 // Test selecting a shipping address different from billing as address.
1279 TEST_F(AutofillDialogControllerTest, DontUseBillingAsShipping) {
1280 SwitchToAutofill();
1281 AutofillProfile full_profile(test::GetVerifiedProfile());
1282 AutofillProfile full_profile2(test::GetVerifiedProfile2());
1283 CreditCard credit_card(test::GetVerifiedCreditCard());
1284 controller()->GetTestingManager()->AddTestingProfile(&full_profile);
1285 controller()->GetTestingManager()->AddTestingProfile(&full_profile2);
1286 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
1287 ui::MenuModel* shipping_model =
1288 controller()->MenuModelForSection(SECTION_SHIPPING);
1289 shipping_model->ActivatedAt(2);
1291 controller()->OnAccept();
1292 ASSERT_EQ(20U, form_structure()->field_count());
1293 EXPECT_EQ(ADDRESS_HOME_STATE,
1294 form_structure()->field(9)->Type().GetStorableType());
1295 EXPECT_EQ(ADDRESS_BILLING, form_structure()->field(9)->Type().group());
1296 EXPECT_EQ(ADDRESS_HOME_STATE,
1297 form_structure()->field(16)->Type().GetStorableType());
1298 EXPECT_EQ(ADDRESS_HOME, form_structure()->field(16)->Type().group());
1299 base::string16 billing_state = form_structure()->field(9)->value;
1300 base::string16 shipping_state = form_structure()->field(16)->value;
1301 EXPECT_FALSE(billing_state.empty());
1302 EXPECT_FALSE(shipping_state.empty());
1303 EXPECT_NE(billing_state, shipping_state);
1305 EXPECT_EQ(CREDIT_CARD_NAME,
1306 form_structure()->field(1)->Type().GetStorableType());
1307 base::string16 cc_name = form_structure()->field(1)->value;
1308 EXPECT_EQ(NAME_FULL, form_structure()->field(6)->Type().GetStorableType());
1309 EXPECT_EQ(NAME_BILLING, form_structure()->field(6)->Type().group());
1310 base::string16 billing_name = form_structure()->field(6)->value;
1311 EXPECT_EQ(NAME_FULL, form_structure()->field(13)->Type().GetStorableType());
1312 EXPECT_EQ(NAME, form_structure()->field(13)->Type().group());
1313 base::string16 shipping_name = form_structure()->field(13)->value;
1315 EXPECT_FALSE(cc_name.empty());
1316 EXPECT_FALSE(billing_name.empty());
1317 EXPECT_FALSE(shipping_name.empty());
1318 // Billing name should always be the same as cardholder name.
1319 EXPECT_EQ(cc_name, billing_name);
1320 EXPECT_NE(cc_name, shipping_name);
1323 // Test selecting UseBillingForShipping.
1324 TEST_F(AutofillDialogControllerTest, UseBillingAsShipping) {
1325 SwitchToAutofill();
1327 AutofillProfile full_profile(test::GetVerifiedProfile());
1328 controller()->GetTestingManager()->AddTestingProfile(&full_profile);
1330 AutofillProfile full_profile2(test::GetVerifiedProfile2());
1331 controller()->GetTestingManager()->AddTestingProfile(&full_profile2);
1333 CreditCard credit_card(test::GetVerifiedCreditCard());
1334 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
1336 ASSERT_FALSE(controller()->IsManuallyEditingSection(SECTION_CC));
1337 ASSERT_FALSE(controller()->IsManuallyEditingSection(SECTION_BILLING));
1339 SubmitAndVerifyShippingAndBillingResults();
1342 TEST_F(AutofillDialogControllerTest, UseBillingAsShippingManualInput) {
1343 SwitchToAutofill();
1345 ASSERT_TRUE(controller()->IsManuallyEditingSection(SECTION_CC));
1346 ASSERT_TRUE(controller()->IsManuallyEditingSection(SECTION_BILLING));
1348 CreditCard credit_card(test::GetVerifiedCreditCard());
1349 FillInputs(SECTION_CC, credit_card);
1351 AutofillProfile full_profile(test::GetVerifiedProfile());
1352 FillInputs(SECTION_BILLING, full_profile);
1354 SubmitAndVerifyShippingAndBillingResults();
1357 // Tests that shipping and billing telephone fields are supported, and filled
1358 // in by their respective profiles. http://crbug.com/244515
1359 TEST_F(AutofillDialogControllerTest, BillingVsShippingPhoneNumber) {
1360 FormFieldData shipping_tel;
1361 shipping_tel.autocomplete_attribute = "shipping tel";
1362 FormFieldData billing_tel;
1363 billing_tel.autocomplete_attribute = "billing tel";
1364 FormFieldData cc_field;
1365 cc_field.autocomplete_attribute = "cc-csc";
1367 FormData form_data;
1368 form_data.fields.push_back(shipping_tel);
1369 form_data.fields.push_back(billing_tel);
1370 form_data.fields.push_back(cc_field);
1371 SetUpControllerWithFormData(form_data);
1373 SwitchToAutofill();
1375 // The profile that will be chosen for the shipping section.
1376 AutofillProfile shipping_profile(test::GetVerifiedProfile());
1377 // The profile that will be chosen for the billing section.
1378 AutofillProfile billing_profile(test::GetVerifiedProfile2());
1379 CreditCard credit_card(test::GetVerifiedCreditCard());
1380 controller()->GetTestingManager()->AddTestingProfile(&shipping_profile);
1381 controller()->GetTestingManager()->AddTestingProfile(&billing_profile);
1382 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
1383 ui::MenuModel* billing_model =
1384 controller()->MenuModelForSection(SECTION_BILLING);
1385 billing_model->ActivatedAt(1);
1387 controller()->OnAccept();
1388 ASSERT_EQ(3U, form_structure()->field_count());
1389 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER,
1390 form_structure()->field(0)->Type().GetStorableType());
1391 EXPECT_EQ(PHONE_HOME, form_structure()->field(0)->Type().group());
1392 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER,
1393 form_structure()->field(1)->Type().GetStorableType());
1394 EXPECT_EQ(PHONE_BILLING, form_structure()->field(1)->Type().group());
1395 EXPECT_EQ(shipping_profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER),
1396 form_structure()->field(0)->value);
1397 EXPECT_EQ(billing_profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER),
1398 form_structure()->field(1)->value);
1399 EXPECT_NE(form_structure()->field(1)->value,
1400 form_structure()->field(0)->value);
1403 // Similar to the above, but tests that street-address (i.e. all lines of the
1404 // street address) is successfully filled for both shipping and billing
1405 // sections.
1406 TEST_F(AutofillDialogControllerTest, BillingVsShippingStreetAddress) {
1407 FormFieldData shipping_address;
1408 shipping_address.autocomplete_attribute = "shipping street-address";
1409 FormFieldData billing_address;
1410 billing_address.autocomplete_attribute = "billing street-address";
1411 FormFieldData shipping_address_textarea;
1412 shipping_address_textarea.autocomplete_attribute = "shipping street-address";
1413 shipping_address_textarea.form_control_type = "textarea";
1414 FormFieldData billing_address_textarea;
1415 billing_address_textarea.autocomplete_attribute = "billing street-address";
1416 billing_address_textarea.form_control_type = "textarea";
1417 FormFieldData cc_field;
1418 cc_field.autocomplete_attribute = "cc-csc";
1420 FormData form_data;
1421 form_data.fields.push_back(shipping_address);
1422 form_data.fields.push_back(billing_address);
1423 form_data.fields.push_back(shipping_address_textarea);
1424 form_data.fields.push_back(billing_address_textarea);
1425 form_data.fields.push_back(cc_field);
1426 SetUpControllerWithFormData(form_data);
1428 SwitchToAutofill();
1430 // The profile that will be chosen for the shipping section.
1431 AutofillProfile shipping_profile(test::GetVerifiedProfile());
1432 // The profile that will be chosen for the billing section.
1433 AutofillProfile billing_profile(test::GetVerifiedProfile2());
1434 CreditCard credit_card(test::GetVerifiedCreditCard());
1435 controller()->GetTestingManager()->AddTestingProfile(&shipping_profile);
1436 controller()->GetTestingManager()->AddTestingProfile(&billing_profile);
1437 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
1438 ui::MenuModel* billing_model =
1439 controller()->MenuModelForSection(SECTION_BILLING);
1440 billing_model->ActivatedAt(1);
1442 controller()->OnAccept();
1443 ASSERT_EQ(5U, form_structure()->field_count());
1444 EXPECT_EQ(ADDRESS_HOME_STREET_ADDRESS,
1445 form_structure()->field(0)->Type().GetStorableType());
1446 EXPECT_EQ(ADDRESS_HOME, form_structure()->field(0)->Type().group());
1447 EXPECT_EQ(ADDRESS_HOME_STREET_ADDRESS,
1448 form_structure()->field(1)->Type().GetStorableType());
1449 EXPECT_EQ(ADDRESS_BILLING, form_structure()->field(1)->Type().group());
1450 // Inexact matching; single-line inputs get the address data concatenated but
1451 // separated by commas.
1452 EXPECT_TRUE(StartsWith(form_structure()->field(0)->value,
1453 shipping_profile.GetRawInfo(ADDRESS_HOME_LINE1),
1454 true));
1455 EXPECT_TRUE(EndsWith(form_structure()->field(0)->value,
1456 shipping_profile.GetRawInfo(ADDRESS_HOME_LINE2),
1457 true));
1458 EXPECT_TRUE(StartsWith(form_structure()->field(1)->value,
1459 billing_profile.GetRawInfo(ADDRESS_HOME_LINE1),
1460 true));
1461 EXPECT_TRUE(EndsWith(form_structure()->field(1)->value,
1462 billing_profile.GetRawInfo(ADDRESS_HOME_LINE2),
1463 true));
1464 // The textareas should be an exact match.
1465 EXPECT_EQ(shipping_profile.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS),
1466 form_structure()->field(2)->value);
1467 EXPECT_EQ(billing_profile.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS),
1468 form_structure()->field(3)->value);
1470 EXPECT_NE(form_structure()->field(1)->value,
1471 form_structure()->field(0)->value);
1472 EXPECT_NE(form_structure()->field(3)->value,
1473 form_structure()->field(2)->value);
1476 // Test asking for different pieces of the name.
1477 TEST_F(AutofillDialogControllerTest, NamePieces) {
1478 const char* const attributes[] = {
1479 "shipping name",
1480 "billing name",
1481 "billing given-name",
1482 "billing family-name",
1483 "billing additional-name",
1484 "cc-csc"
1487 FormData form_data;
1488 for (size_t i = 0; i < arraysize(attributes); ++i) {
1489 FormFieldData field;
1490 field.autocomplete_attribute.assign(attributes[i]);
1491 form_data.fields.push_back(field);
1494 SetUpControllerWithFormData(form_data);
1495 SwitchToAutofill();
1497 // Billing.
1498 AutofillProfile test_profile(test::GetVerifiedProfile());
1499 test_profile.SetInfo(AutofillType(NAME_FULL),
1500 ASCIIToUTF16("Fabian Jackson von Nacho"),
1501 "en-US");
1502 controller()->GetTestingManager()->AddTestingProfile(&test_profile);
1504 // Credit card.
1505 CreditCard credit_card(test::GetVerifiedCreditCard());
1506 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
1508 // Make shipping name different from billing.
1509 AutofillProfile test_profile2(test::GetVerifiedProfile2());
1510 test_profile2.SetInfo(AutofillType(NAME_FULL),
1511 ASCIIToUTF16("Don Ford"),
1512 "en-US");
1513 controller()->GetTestingManager()->AddTestingProfile(&test_profile2);
1514 ui::MenuModel* shipping_model =
1515 controller()->MenuModelForSection(SECTION_SHIPPING);
1516 shipping_model->ActivatedAt(2);
1518 controller()->OnAccept();
1520 EXPECT_EQ(NAME_FULL, form_structure()->field(0)->Type().GetStorableType());
1521 EXPECT_EQ(ASCIIToUTF16("Don Ford"),
1522 form_structure()->field(0)->value);
1524 EXPECT_EQ(NAME_FULL, form_structure()->field(1)->Type().GetStorableType());
1525 EXPECT_EQ(ASCIIToUTF16("Fabian Jackson von Nacho"),
1526 form_structure()->field(1)->value);
1528 EXPECT_EQ(NAME_FIRST, form_structure()->field(2)->Type().GetStorableType());
1529 EXPECT_EQ(ASCIIToUTF16("Fabian"),
1530 form_structure()->field(2)->value);
1532 EXPECT_EQ(NAME_LAST, form_structure()->field(3)->Type().GetStorableType());
1533 EXPECT_EQ(ASCIIToUTF16("von Nacho"),
1534 form_structure()->field(3)->value);
1536 EXPECT_EQ(NAME_MIDDLE, form_structure()->field(4)->Type().GetStorableType());
1537 EXPECT_EQ(ASCIIToUTF16("Jackson"),
1538 form_structure()->field(4)->value);
1541 TEST_F(AutofillDialogControllerTest, AcceptLegalDocuments) {
1542 for (size_t i = 0; i < 2; ++i) {
1543 SCOPED_TRACE(testing::Message() << "Case " << i);
1545 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1546 AcceptLegalDocuments(_, _));
1547 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_));
1548 EXPECT_CALL(*controller(), LoadRiskFingerprintData());
1550 EXPECT_TRUE(controller()->LegalDocumentLinks().empty());
1551 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
1552 EXPECT_TRUE(controller()->LegalDocumentLinks().empty());
1554 scoped_ptr<wallet::WalletItems> wallet_items =
1555 CompleteAndValidWalletItems();
1556 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
1557 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
1558 controller()->OnDidGetWalletItems(wallet_items.Pass());
1559 EXPECT_FALSE(controller()->LegalDocumentLinks().empty());
1561 controller()->OnAccept();
1562 controller()->OnDidAcceptLegalDocuments();
1563 controller()->OnDidLoadRiskFingerprintData(GetFakeFingerprint().Pass());
1565 // Now try it all over again with the location disclosure already accepted.
1566 // Nothing should change.
1567 Reset();
1568 base::ListValue preexisting_list;
1569 preexisting_list.AppendString(kFakeEmail);
1570 g_browser_process->local_state()->Set(
1571 ::prefs::kAutofillDialogWalletLocationAcceptance,
1572 preexisting_list);
1576 TEST_F(AutofillDialogControllerTest, RejectLegalDocuments) {
1577 for (size_t i = 0; i < 2; ++i) {
1578 SCOPED_TRACE(testing::Message() << "Case " << i);
1580 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1581 AcceptLegalDocuments(_, _)).Times(0);
1583 scoped_ptr<wallet::WalletItems> wallet_items =
1584 CompleteAndValidWalletItems();
1585 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
1586 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
1587 controller()->OnDidGetWalletItems(wallet_items.Pass());
1588 EXPECT_FALSE(controller()->LegalDocumentLinks().empty());
1590 controller()->OnCancel();
1592 // Now try it all over again with the location disclosure already accepted.
1593 // Nothing should change.
1594 Reset();
1595 base::ListValue preexisting_list;
1596 preexisting_list.AppendString(kFakeEmail);
1597 g_browser_process->local_state()->Set(
1598 ::prefs::kAutofillDialogWalletLocationAcceptance,
1599 preexisting_list);
1603 TEST_F(AutofillDialogControllerTest, AcceptLocationDisclosure) {
1604 // Check that accepting the dialog registers the user's name in the list
1605 // of users who have accepted the geolocation terms.
1606 EXPECT_TRUE(g_browser_process->local_state()->GetList(
1607 ::prefs::kAutofillDialogWalletLocationAcceptance)->empty());
1609 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
1610 EXPECT_FALSE(controller()->LegalDocumentsText().empty());
1611 EXPECT_TRUE(controller()->LegalDocumentLinks().empty());
1612 controller()->OnAccept();
1614 const base::ListValue* list = g_browser_process->local_state()->GetList(
1615 ::prefs::kAutofillDialogWalletLocationAcceptance);
1616 ASSERT_EQ(1U, list->GetSize());
1617 std::string accepted_username;
1618 EXPECT_TRUE(list->GetString(0, &accepted_username));
1619 EXPECT_EQ(kFakeEmail, accepted_username);
1621 // Now check it still works if that list starts off with some other username
1622 // in it.
1623 Reset();
1624 list = g_browser_process->local_state()->GetList(
1625 ::prefs::kAutofillDialogWalletLocationAcceptance);
1626 ASSERT_TRUE(list->empty());
1628 std::string kOtherUsername("spouse@example.com");
1629 base::ListValue preexisting_list;
1630 preexisting_list.AppendString(kOtherUsername);
1631 g_browser_process->local_state()->Set(
1632 ::prefs::kAutofillDialogWalletLocationAcceptance,
1633 preexisting_list);
1635 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
1636 EXPECT_FALSE(controller()->LegalDocumentsText().empty());
1637 EXPECT_TRUE(controller()->LegalDocumentLinks().empty());
1638 controller()->OnAccept();
1640 list = g_browser_process->local_state()->GetList(
1641 ::prefs::kAutofillDialogWalletLocationAcceptance);
1642 ASSERT_EQ(2U, list->GetSize());
1643 EXPECT_NE(list->end(), list->Find(base::StringValue(kFakeEmail)));
1644 EXPECT_NE(list->end(), list->Find(base::StringValue(kOtherUsername)));
1646 // Now check the list doesn't change if the user cancels out of the dialog.
1647 Reset();
1648 list = g_browser_process->local_state()->GetList(
1649 ::prefs::kAutofillDialogWalletLocationAcceptance);
1650 ASSERT_TRUE(list->empty());
1652 g_browser_process->local_state()->Set(
1653 ::prefs::kAutofillDialogWalletLocationAcceptance,
1654 preexisting_list);
1656 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
1657 EXPECT_FALSE(controller()->LegalDocumentsText().empty());
1658 EXPECT_TRUE(controller()->LegalDocumentLinks().empty());
1659 controller()->OnCancel();
1661 list = g_browser_process->local_state()->GetList(
1662 ::prefs::kAutofillDialogWalletLocationAcceptance);
1663 ASSERT_EQ(1U, list->GetSize());
1664 EXPECT_NE(list->end(), list->Find(base::StringValue(kOtherUsername)));
1665 EXPECT_EQ(list->end(), list->Find(base::StringValue(kFakeEmail)));
1668 TEST_F(AutofillDialogControllerTest, LegalDocumentOverflow) {
1669 for (size_t number_of_docs = 2; number_of_docs < 11; ++number_of_docs) {
1670 scoped_ptr<wallet::WalletItems> wallet_items =
1671 CompleteAndValidWalletItems();
1672 for (size_t i = 0; i < number_of_docs; ++i)
1673 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
1675 Reset();
1676 controller()->OnDidGetWalletItems(wallet_items.Pass());
1678 // The dialog is only equipped to handle 2-6 legal documents. More than
1679 // 6 errors out.
1680 if (number_of_docs <= 6U) {
1681 EXPECT_FALSE(controller()->LegalDocumentsText().empty());
1682 } else {
1683 EXPECT_TRUE(controller()->LegalDocumentsText().empty());
1684 EXPECT_EQ(1U, NotificationsOfType(
1685 DialogNotification::WALLET_ERROR).size());
1689 controller()->OnCancel();
1692 // Makes sure the default object IDs are respected.
1693 TEST_F(AutofillDialogControllerTest, WalletDefaultItems) {
1694 scoped_ptr<wallet::WalletItems> wallet_items =
1695 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
1696 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1697 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1698 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
1699 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1701 wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
1702 wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
1703 wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
1704 wallet_items->AddAddress(wallet::GetTestShippingAddress());
1705 wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
1707 controller()->OnDidGetWalletItems(wallet_items.Pass());
1708 // "add", "manage", and 4 suggestions.
1709 EXPECT_EQ(6,
1710 controller()->MenuModelForSection(SECTION_CC_BILLING)->GetItemCount());
1711 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING)->
1712 IsItemCheckedAt(2));
1713 ASSERT_FALSE(controller()->IsEditingExistingData(SECTION_CC_BILLING));
1714 // "use billing", "add", "manage", and 5 suggestions.
1715 EXPECT_EQ(8,
1716 controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
1717 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_SHIPPING)->
1718 IsItemCheckedAt(4));
1719 ASSERT_FALSE(controller()->IsEditingExistingData(SECTION_SHIPPING));
1722 // Tests that invalid and AMEX default instruments are ignored.
1723 TEST_F(AutofillDialogControllerTest, SelectInstrument) {
1724 scoped_ptr<wallet::WalletItems> wallet_items =
1725 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
1726 // Tests if default instrument is invalid, then, the first valid instrument is
1727 // selected instead of the default instrument.
1728 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1729 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1730 wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentInvalid());
1731 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1733 controller()->OnDidGetWalletItems(wallet_items.Pass());
1734 // 4 suggestions and "add", "manage".
1735 EXPECT_EQ(6,
1736 controller()->MenuModelForSection(SECTION_CC_BILLING)->GetItemCount());
1737 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING)->
1738 IsItemCheckedAt(0));
1740 // Tests if default instrument is AMEX but Wallet doesn't support
1741 // AMEX on this merchant, then the first valid instrument is
1742 // selected instead of the default instrument.
1743 wallet_items = wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
1744 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1745 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1746 wallet_items->AddInstrument(
1747 wallet::GetTestMaskedInstrumentAmex(wallet::AMEX_DISALLOWED));
1748 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1750 controller()->OnDidGetWalletItems(wallet_items.Pass());
1751 // 4 suggestions and "add", "manage".
1752 EXPECT_EQ(6,
1753 controller()->MenuModelForSection(SECTION_CC_BILLING)->GetItemCount());
1754 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING)->
1755 IsItemCheckedAt(0));
1757 // Tests if default instrument is AMEX and it is allowed on this merchant,
1758 // then it is selected.
1759 wallet_items = wallet::GetTestWalletItems(wallet::AMEX_ALLOWED);
1760 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1761 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1762 wallet_items->AddInstrument(
1763 wallet::GetTestMaskedInstrumentAmex(wallet::AMEX_ALLOWED));
1764 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1766 controller()->OnDidGetWalletItems(wallet_items.Pass());
1767 // 4 suggestions and "add", "manage".
1768 EXPECT_EQ(6,
1769 controller()->MenuModelForSection(SECTION_CC_BILLING)->GetItemCount());
1770 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING)->
1771 IsItemCheckedAt(2));
1773 // Tests if only have AMEX and invalid instrument, then "add" is selected.
1774 wallet_items = wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
1775 wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentInvalid());
1776 wallet_items->AddInstrument(
1777 wallet::GetTestMaskedInstrumentAmex(wallet::AMEX_DISALLOWED));
1779 controller()->OnDidGetWalletItems(wallet_items.Pass());
1780 // 2 suggestions and "add", "manage".
1781 EXPECT_EQ(4,
1782 controller()->MenuModelForSection(SECTION_CC_BILLING)->GetItemCount());
1783 // "add"
1784 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING)->
1785 IsItemCheckedAt(2));
1788 TEST_F(AutofillDialogControllerTest, SaveAddress) {
1789 EXPECT_CALL(*controller()->GetView(), ModelChanged());
1790 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1791 SaveToWalletMock(testing::IsNull(),
1792 testing::NotNull(),
1793 testing::IsNull(),
1794 testing::IsNull()));
1796 scoped_ptr<wallet::WalletItems> wallet_items =
1797 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
1798 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
1799 controller()->OnDidGetWalletItems(wallet_items.Pass());
1800 // If there is no shipping address in wallet, it will default to
1801 // "same-as-billing" instead of "add-new-item". "same-as-billing" is covered
1802 // by the following tests. The penultimate item in the menu is "add-new-item".
1803 ui::MenuModel* shipping_model =
1804 controller()->MenuModelForSection(SECTION_SHIPPING);
1805 shipping_model->ActivatedAt(shipping_model->GetItemCount() - 2);
1807 AutofillProfile test_profile(test::GetVerifiedProfile());
1808 FillInputs(SECTION_SHIPPING, test_profile);
1810 AcceptAndLoadFakeFingerprint();
1813 TEST_F(AutofillDialogControllerTest, SaveInstrument) {
1814 EXPECT_CALL(*controller()->GetView(), ModelChanged());
1815 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1816 SaveToWalletMock(testing::NotNull(),
1817 testing::IsNull(),
1818 testing::IsNull(),
1819 testing::IsNull()));
1821 FillCCBillingInputs();
1822 scoped_ptr<wallet::WalletItems> wallet_items =
1823 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
1824 wallet_items->AddAddress(wallet::GetTestShippingAddress());
1825 SubmitWithWalletItems(wallet_items.Pass());
1828 TEST_F(AutofillDialogControllerTest, SaveInstrumentWithInvalidInstruments) {
1829 EXPECT_CALL(*controller()->GetView(), ModelChanged());
1830 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1831 SaveToWalletMock(testing::NotNull(),
1832 testing::IsNull(),
1833 testing::IsNull(),
1834 testing::IsNull()));
1836 FillCCBillingInputs();
1837 scoped_ptr<wallet::WalletItems> wallet_items =
1838 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
1839 wallet_items->AddAddress(wallet::GetTestShippingAddress());
1840 wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentInvalid());
1841 SubmitWithWalletItems(wallet_items.Pass());
1844 TEST_F(AutofillDialogControllerTest, SaveInstrumentAndAddress) {
1845 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1846 SaveToWalletMock(testing::NotNull(),
1847 testing::NotNull(),
1848 testing::IsNull(),
1849 testing::IsNull()));
1851 FillCCBillingInputs();
1852 scoped_ptr<wallet::WalletItems> wallet_items =
1853 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
1854 SubmitWithWalletItems(wallet_items.Pass());
1857 MATCHER(IsUpdatingExistingData, "updating existing Wallet data") {
1858 return !arg->object_id().empty();
1861 MATCHER(UsesLocalBillingAddress, "uses the local billing address") {
1862 return arg->street_address()[0] == ASCIIToUTF16(kEditedBillingAddress);
1865 // Tests that when using billing address for shipping, and there is no exact
1866 // matched shipping address, then a shipping address should be added.
1867 TEST_F(AutofillDialogControllerTest, BillingForShipping) {
1868 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1869 SaveToWalletMock(testing::IsNull(),
1870 testing::NotNull(),
1871 testing::IsNull(),
1872 testing::IsNull()));
1874 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
1875 // Select "Same as billing" in the address menu.
1876 UseBillingForShipping();
1878 AcceptAndLoadFakeFingerprint();
1881 // Tests that when using billing address for shipping, and there is an exact
1882 // matched shipping address, then a shipping address should not be added.
1883 TEST_F(AutofillDialogControllerTest, BillingForShippingHasMatch) {
1884 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1885 SaveToWalletMock(_, _, _, _)).Times(0);
1887 scoped_ptr<wallet::WalletItems> wallet_items =
1888 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
1889 scoped_ptr<wallet::WalletItems::MaskedInstrument> instrument =
1890 wallet::GetTestMaskedInstrument();
1891 // Copy billing address as shipping address, and assign an id to it.
1892 scoped_ptr<wallet::Address> shipping_address(
1893 new wallet::Address(instrument->address()));
1894 shipping_address->set_object_id("shipping_address_id");
1895 wallet_items->AddAddress(shipping_address.Pass());
1896 wallet_items->AddInstrument(instrument.Pass());
1897 wallet_items->AddAddress(wallet::GetTestShippingAddress());
1899 controller()->OnDidGetWalletItems(wallet_items.Pass());
1900 // Select "Same as billing" in the address menu.
1901 UseBillingForShipping();
1903 AcceptAndLoadFakeFingerprint();
1906 // Test that the local view contents is used when saving a new instrument and
1907 // the user has selected "Same as billing".
1908 TEST_F(AutofillDialogControllerTest, SaveInstrumentSameAsBilling) {
1909 scoped_ptr<wallet::WalletItems> wallet_items =
1910 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
1911 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
1912 controller()->OnDidGetWalletItems(wallet_items.Pass());
1914 ui::MenuModel* model = controller()->MenuModelForSection(SECTION_CC_BILLING);
1915 model->ActivatedAt(model->GetItemCount() - 2);
1917 FieldValueMap outputs;
1918 const DetailInputs& inputs =
1919 controller()->RequestedFieldsForSection(SECTION_CC_BILLING);
1920 AutofillProfile full_profile(test::GetVerifiedProfile());
1921 CreditCard full_card(test::GetCreditCard());
1922 for (size_t i = 0; i < inputs.size(); ++i) {
1923 const ServerFieldType type = inputs[i].type;
1924 if (type == ADDRESS_BILLING_STREET_ADDRESS)
1925 outputs[type] = ASCIIToUTF16(kEditedBillingAddress);
1926 else
1927 outputs[type] = full_profile.GetInfo(AutofillType(type), "en-US");
1929 if (outputs[type].empty())
1930 outputs[type] = full_card.GetInfo(AutofillType(type), "en-US");
1932 controller()->GetView()->SetUserInput(SECTION_CC_BILLING, outputs);
1934 controller()->OnAccept();
1936 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1937 SaveToWalletMock(testing::NotNull(),
1938 UsesLocalBillingAddress(),
1939 testing::IsNull(),
1940 testing::IsNull()));
1941 AcceptAndLoadFakeFingerprint();
1944 TEST_F(AutofillDialogControllerTest, CancelNoSave) {
1945 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1946 SaveToWalletMock(_, _, _, _)).Times(0);
1948 EXPECT_CALL(*controller()->GetView(), ModelChanged());
1950 controller()->OnDidGetWalletItems(
1951 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED));
1952 controller()->OnCancel();
1955 // Checks that clicking the Manage menu item opens a new tab with a different
1956 // URL for Wallet and Autofill.
1957 TEST_F(AutofillDialogControllerTest, ManageItem) {
1958 AutofillProfile full_profile(test::GetVerifiedProfile());
1959 full_profile.set_origin(kSettingsOrigin);
1960 full_profile.SetRawInfo(ADDRESS_HOME_LINE2, base::string16());
1961 controller()->GetTestingManager()->AddTestingProfile(&full_profile);
1962 SwitchToAutofill();
1964 SuggestionsMenuModel* shipping = GetMenuModelForSection(SECTION_SHIPPING);
1965 shipping->ExecuteCommand(shipping->GetItemCount() - 1, 0);
1966 GURL autofill_manage_url = controller()->open_tab_url();
1967 EXPECT_EQ("chrome", autofill_manage_url.scheme());
1969 SwitchToWallet();
1970 scoped_ptr<wallet::WalletItems> wallet_items =
1971 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
1972 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
1973 controller()->OnDidGetWalletItems(wallet_items.Pass());
1975 controller()->SuggestionItemSelected(shipping, shipping->GetItemCount() - 1);
1976 GURL wallet_manage_addresses_url = controller()->open_tab_url();
1977 EXPECT_EQ("https", wallet_manage_addresses_url.scheme());
1979 SuggestionsMenuModel* billing = GetMenuModelForSection(SECTION_CC_BILLING);
1980 controller()->SuggestionItemSelected(billing, billing->GetItemCount() - 1);
1981 GURL wallet_manage_instruments_url = controller()->open_tab_url();
1982 EXPECT_EQ("https", wallet_manage_instruments_url.scheme());
1984 EXPECT_NE(autofill_manage_url, wallet_manage_instruments_url);
1985 EXPECT_NE(wallet_manage_instruments_url, wallet_manage_addresses_url);
1988 // Tests that adding an autofill profile and then submitting works.
1989 TEST_F(AutofillDialogControllerTest, AddAutofillProfile) {
1990 SwitchToAutofill();
1991 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(2);
1993 AutofillProfile full_profile(test::GetVerifiedProfile());
1994 CreditCard credit_card(test::GetVerifiedCreditCard());
1995 controller()->GetTestingManager()->AddTestingProfile(&full_profile);
1996 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
1998 ui::MenuModel* model = controller()->MenuModelForSection(SECTION_BILLING);
1999 // Activate the "Add billing address" menu item.
2000 model->ActivatedAt(model->GetItemCount() - 2);
2002 // Fill in the inputs from the profile.
2003 FieldValueMap outputs;
2004 const DetailInputs& inputs =
2005 controller()->RequestedFieldsForSection(SECTION_BILLING);
2006 AutofillProfile full_profile2(test::GetVerifiedProfile2());
2007 for (size_t i = 0; i < inputs.size(); ++i) {
2008 const ServerFieldType type = inputs[i].type;
2009 outputs[type] = full_profile2.GetInfo(AutofillType(type), "en-US");
2011 controller()->GetView()->SetUserInput(SECTION_BILLING, outputs);
2013 controller()->OnAccept();
2014 const AutofillProfile& added_profile =
2015 controller()->GetTestingManager()->imported_profile();
2017 const DetailInputs& shipping_inputs =
2018 controller()->RequestedFieldsForSection(SECTION_SHIPPING);
2019 for (size_t i = 0; i < shipping_inputs.size(); ++i) {
2020 const ServerFieldType type = shipping_inputs[i].type;
2021 EXPECT_EQ(full_profile2.GetInfo(AutofillType(type), "en-US"),
2022 added_profile.GetInfo(AutofillType(type), "en-US"));
2026 TEST_F(AutofillDialogControllerTest, VerifyCvv) {
2027 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_));
2028 EXPECT_CALL(*controller()->GetTestingWalletClient(),
2029 AuthenticateInstrument(_, _));
2031 SubmitWithWalletItems(CompleteAndValidWalletItems());
2033 EXPECT_TRUE(NotificationsOfType(DialogNotification::REQUIRED_ACTION).empty());
2034 EXPECT_TRUE(controller()->SectionIsActive(SECTION_SHIPPING));
2035 EXPECT_TRUE(controller()->SectionIsActive(SECTION_CC_BILLING));
2036 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2037 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL));
2039 SuggestionState suggestion_state =
2040 controller()->SuggestionStateForSection(SECTION_CC_BILLING);
2041 EXPECT_TRUE(suggestion_state.extra_text.empty());
2043 controller()->OnDidGetFullWallet(
2044 wallet::GetTestFullWalletWithRequiredActions(
2045 std::vector<wallet::RequiredAction>(1, wallet::VERIFY_CVV)));
2046 ASSERT_TRUE(controller()->IsSubmitPausedOn(wallet::VERIFY_CVV));
2048 EXPECT_FALSE(
2049 NotificationsOfType(DialogNotification::REQUIRED_ACTION).empty());
2050 EXPECT_FALSE(controller()->SectionIsActive(SECTION_SHIPPING));
2051 EXPECT_TRUE(controller()->SectionIsActive(SECTION_CC_BILLING));
2053 suggestion_state =
2054 controller()->SuggestionStateForSection(SECTION_CC_BILLING);
2055 EXPECT_FALSE(suggestion_state.extra_text.empty());
2056 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC_BILLING));
2058 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2059 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL));
2061 controller()->OnAccept();
2063 EXPECT_FALSE(controller()->GetDialogOverlay().image.IsEmpty());
2066 TEST_F(AutofillDialogControllerTest, ErrorDuringSubmit) {
2067 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_));
2069 SubmitWithWalletItems(CompleteAndValidWalletItems());
2071 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2072 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL));
2074 controller()->OnWalletError(wallet::WalletClient::UNKNOWN_ERROR);
2076 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2077 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL));
2080 TEST_F(AutofillDialogControllerTest, ErrorDuringVerifyCvv) {
2081 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_));
2083 SubmitWithWalletItems(CompleteAndValidWalletItems());
2084 controller()->OnDidGetFullWallet(
2085 wallet::GetTestFullWalletWithRequiredActions(
2086 std::vector<wallet::RequiredAction>(1, wallet::VERIFY_CVV)));
2088 ASSERT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2089 ASSERT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL));
2091 controller()->OnWalletError(wallet::WalletClient::UNKNOWN_ERROR);
2093 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2094 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL));
2097 // Simulates receiving an INVALID_FORM_FIELD required action while processing a
2098 // |WalletClientDelegate::OnDid{Save,Update}*()| call. This can happen if Online
2099 // Wallet's server validation differs from Chrome's local validation.
2100 TEST_F(AutofillDialogControllerTest, WalletServerSideValidation) {
2101 scoped_ptr<wallet::WalletItems> wallet_items =
2102 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
2103 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
2104 controller()->OnDidGetWalletItems(wallet_items.Pass());
2105 controller()->OnAccept();
2107 std::vector<wallet::RequiredAction> required_actions;
2108 required_actions.push_back(wallet::INVALID_FORM_FIELD);
2110 std::vector<wallet::FormFieldError> form_errors;
2111 form_errors.push_back(
2112 wallet::FormFieldError(wallet::FormFieldError::INVALID_POSTAL_CODE,
2113 wallet::FormFieldError::SHIPPING_ADDRESS));
2115 EXPECT_CALL(*controller()->GetView(), UpdateForErrors());
2116 controller()->OnDidSaveToWallet(std::string(),
2117 std::string(),
2118 required_actions,
2119 form_errors);
2122 // Simulates receiving unrecoverable Wallet server validation errors.
2123 TEST_F(AutofillDialogControllerTest, WalletServerSideValidationUnrecoverable) {
2124 scoped_ptr<wallet::WalletItems> wallet_items =
2125 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
2126 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
2127 controller()->OnDidGetWalletItems(wallet_items.Pass());
2128 controller()->OnAccept();
2130 std::vector<wallet::RequiredAction> required_actions;
2131 required_actions.push_back(wallet::INVALID_FORM_FIELD);
2133 std::vector<wallet::FormFieldError> form_errors;
2134 form_errors.push_back(
2135 wallet::FormFieldError(wallet::FormFieldError::UNKNOWN_ERROR,
2136 wallet::FormFieldError::UNKNOWN_LOCATION));
2138 controller()->OnDidSaveToWallet(std::string(),
2139 std::string(),
2140 required_actions,
2141 form_errors);
2143 EXPECT_EQ(1U, NotificationsOfType(
2144 DialogNotification::REQUIRED_ACTION).size());
2147 // Test Wallet banners are show in the right situations. These banners promote
2148 // saving details into Wallet (i.e. "[x] Save details to Wallet").
2149 TEST_F(AutofillDialogControllerTest, WalletBanners) {
2150 // Simulate non-signed-in case.
2151 SetUpControllerWithFormData(DefaultFormData());
2152 GoogleServiceAuthError error(GoogleServiceAuthError::NONE);
2153 controller()->OnPassiveSigninFailure(error);
2154 EXPECT_EQ(0U, NotificationsOfType(
2155 DialogNotification::WALLET_USAGE_CONFIRMATION).size());
2157 // Sign in a user with a completed account.
2158 SetUpControllerWithFormData(DefaultFormData());
2159 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
2161 // Full account; should show "Details from Wallet" message.
2162 EXPECT_EQ(1U, NotificationsOfType(
2163 DialogNotification::WALLET_USAGE_CONFIRMATION).size());
2164 SwitchToAutofill();
2165 EXPECT_EQ(1U, NotificationsOfType(
2166 DialogNotification::WALLET_USAGE_CONFIRMATION).size());
2168 // Start over and sign in a user with an incomplete account.
2169 SetUpControllerWithFormData(DefaultFormData());
2170 scoped_ptr<wallet::WalletItems> wallet_items =
2171 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
2172 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
2173 controller()->OnDidGetWalletItems(wallet_items.Pass());
2175 // Partial account.
2176 EXPECT_EQ(1U, NotificationsOfType(
2177 DialogNotification::WALLET_USAGE_CONFIRMATION).size());
2179 SwitchToAutofill();
2180 EXPECT_EQ(1U, NotificationsOfType(
2181 DialogNotification::WALLET_USAGE_CONFIRMATION).size());
2183 // A Wallet error should kill any Wallet promos.
2184 controller()->OnWalletError(wallet::WalletClient::UNKNOWN_ERROR);
2186 EXPECT_EQ(1U, NotificationsOfType(
2187 DialogNotification::WALLET_ERROR).size());
2188 EXPECT_EQ(0U, NotificationsOfType(
2189 DialogNotification::WALLET_USAGE_CONFIRMATION).size());
2192 TEST_F(AutofillDialogControllerTest, ViewCancelDoesntSetPref) {
2193 ASSERT_FALSE(profile()->GetPrefs()->HasPrefPath(
2194 ::prefs::kAutofillDialogPayWithoutWallet));
2196 SwitchToAutofill();
2198 controller()->OnCancel();
2199 controller()->ViewClosed();
2201 EXPECT_FALSE(profile()->GetPrefs()->HasPrefPath(
2202 ::prefs::kAutofillDialogPayWithoutWallet));
2205 TEST_F(AutofillDialogControllerTest, SubmitWithSigninErrorDoesntSetPref) {
2206 ASSERT_FALSE(profile()->GetPrefs()->HasPrefPath(
2207 ::prefs::kAutofillDialogPayWithoutWallet));
2209 SimulateSigninError();
2210 FillCreditCardInputs();
2211 controller()->OnAccept();
2213 EXPECT_FALSE(profile()->GetPrefs()->HasPrefPath(
2214 ::prefs::kAutofillDialogPayWithoutWallet));
2217 // Tests that there's an overlay shown while waiting for full wallet items.
2218 TEST_F(AutofillDialogControllerTest, WalletFirstRun) {
2219 EXPECT_TRUE(controller()->GetDialogOverlay().image.IsEmpty());
2221 SubmitWithWalletItems(CompleteAndValidWalletItems());
2222 EXPECT_FALSE(controller()->GetDialogOverlay().image.IsEmpty());
2224 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
2225 EXPECT_FALSE(controller()->GetDialogOverlay().image.IsEmpty());
2226 EXPECT_FALSE(form_structure());
2228 // Don't make the test wait for 2 seconds.
2229 controller()->ForceFinishSubmit();
2230 EXPECT_TRUE(form_structure());
2233 TEST_F(AutofillDialogControllerTest, ViewSubmitSetsPref) {
2234 ASSERT_FALSE(profile()->GetPrefs()->HasPrefPath(
2235 ::prefs::kAutofillDialogPayWithoutWallet));
2237 SwitchToAutofill();
2238 FillCreditCardInputs();
2239 controller()->OnAccept();
2241 EXPECT_TRUE(profile()->GetPrefs()->HasPrefPath(
2242 ::prefs::kAutofillDialogPayWithoutWallet));
2243 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(
2244 ::prefs::kAutofillDialogPayWithoutWallet));
2246 // Try again with a signin error (just leaves the pref alone).
2247 SetUpControllerWithFormData(DefaultFormData());
2249 // Setting up the controller again should not change the pref.
2250 EXPECT_TRUE(profile()->GetPrefs()->HasPrefPath(
2251 ::prefs::kAutofillDialogPayWithoutWallet));
2252 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(
2253 ::prefs::kAutofillDialogPayWithoutWallet));
2255 SimulateSigninError();
2256 FillCreditCardInputs();
2257 controller()->OnAccept();
2258 EXPECT_TRUE(profile()->GetPrefs()->HasPrefPath(
2259 ::prefs::kAutofillDialogPayWithoutWallet));
2260 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(
2261 ::prefs::kAutofillDialogPayWithoutWallet));
2263 // Successfully choosing wallet does set the pref.
2264 // Note that OnDidGetWalletItems sets the account chooser to wallet mode.
2265 SetUpControllerWithFormData(DefaultFormData());
2267 controller()->OnDidFetchWalletCookieValue(std::string());
2268 scoped_ptr<wallet::WalletItems> wallet_items =
2269 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
2270 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
2271 controller()->OnDidGetWalletItems(wallet_items.Pass());
2272 controller()->OnAccept();
2273 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
2274 controller()->ForceFinishSubmit();
2276 EXPECT_TRUE(profile()->GetPrefs()->HasPrefPath(
2277 ::prefs::kAutofillDialogPayWithoutWallet));
2278 EXPECT_FALSE(profile()->GetPrefs()->GetBoolean(
2279 ::prefs::kAutofillDialogPayWithoutWallet));
2282 TEST_F(AutofillDialogControllerTest, HideWalletEmail) {
2283 SwitchToAutofill();
2285 // Email field should be showing when using Autofill.
2286 EXPECT_TRUE(controller()->SectionIsActive(SECTION_BILLING));
2287 EXPECT_FALSE(controller()->SectionIsActive(SECTION_CC_BILLING));
2288 EXPECT_TRUE(SectionContainsField(SECTION_BILLING, EMAIL_ADDRESS));
2290 SwitchToWallet();
2292 // Reset the wallet state.
2293 controller()->OnDidGetWalletItems(scoped_ptr<wallet::WalletItems>());
2295 // Setup some wallet state, submit, and get a full wallet to end the flow.
2296 scoped_ptr<wallet::WalletItems> wallet_items = CompleteAndValidWalletItems();
2298 // Filling |form_structure()| depends on the current username and wallet items
2299 // being fetched. Until both of these have occurred, the user should not be
2300 // able to click Submit if using Wallet. The username fetch happened earlier.
2301 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2302 controller()->OnDidGetWalletItems(wallet_items.Pass());
2303 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2305 // Email field should be absent when using Wallet.
2306 EXPECT_FALSE(controller()->SectionIsActive(SECTION_BILLING));
2307 EXPECT_TRUE(controller()->SectionIsActive(SECTION_CC_BILLING));
2308 EXPECT_FALSE(SectionContainsField(SECTION_CC_BILLING, EMAIL_ADDRESS));
2310 controller()->OnAccept();
2311 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
2312 controller()->ForceFinishSubmit();
2314 ASSERT_TRUE(form_structure());
2315 size_t i = 0;
2316 for (; i < form_structure()->field_count(); ++i) {
2317 if (form_structure()->field(i)->Type().GetStorableType() == EMAIL_ADDRESS) {
2318 EXPECT_EQ(ASCIIToUTF16(kFakeEmail), form_structure()->field(i)->value);
2319 break;
2322 EXPECT_LT(i, form_structure()->field_count());
2325 // Test if autofill types of returned form structure are correct for billing
2326 // entries.
2327 TEST_F(AutofillDialogControllerTest, AutofillTypes) {
2328 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
2329 controller()->OnAccept();
2330 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
2331 controller()->ForceFinishSubmit();
2332 ASSERT_TRUE(form_structure());
2333 ASSERT_EQ(20U, form_structure()->field_count());
2334 EXPECT_EQ(EMAIL_ADDRESS,
2335 form_structure()->field(0)->Type().GetStorableType());
2336 EXPECT_EQ(CREDIT_CARD_NUMBER,
2337 form_structure()->field(2)->Type().GetStorableType());
2338 EXPECT_EQ(ADDRESS_HOME_STATE,
2339 form_structure()->field(9)->Type().GetStorableType());
2340 EXPECT_EQ(ADDRESS_BILLING, form_structure()->field(9)->Type().group());
2341 EXPECT_EQ(ADDRESS_HOME_STATE,
2342 form_structure()->field(16)->Type().GetStorableType());
2343 EXPECT_EQ(ADDRESS_HOME, form_structure()->field(16)->Type().group());
2346 TEST_F(AutofillDialogControllerTest, SaveDetailsInChrome) {
2347 SwitchToAutofill();
2348 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(4);
2350 AutofillProfile full_profile(test::GetVerifiedProfile());
2351 controller()->GetTestingManager()->AddTestingProfile(&full_profile);
2353 CreditCard card(test::GetVerifiedCreditCard());
2354 controller()->GetTestingManager()->AddTestingCreditCard(&card);
2355 EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome());
2357 controller()->MenuModelForSection(SECTION_BILLING)->ActivatedAt(0);
2358 EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome());
2360 controller()->MenuModelForSection(SECTION_BILLING)->ActivatedAt(1);
2361 EXPECT_TRUE(controller()->ShouldOfferToSaveInChrome());
2363 profile()->GetPrefs()->SetBoolean(prefs::kAutofillEnabled, false);
2364 EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome());
2366 profile()->GetPrefs()->SetBoolean(prefs::kAutofillEnabled, true);
2367 controller()->MenuModelForSection(SECTION_BILLING)->ActivatedAt(1);
2368 EXPECT_TRUE(controller()->ShouldOfferToSaveInChrome());
2370 profile()->ForceIncognito(true);
2371 EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome());
2374 TEST_F(AutofillDialogControllerTest, DisabledAutofill) {
2375 SwitchToAutofill();
2376 ASSERT_TRUE(profile()->GetPrefs()->GetBoolean(prefs::kAutofillEnabled));
2378 AutofillProfile verified_profile(test::GetVerifiedProfile());
2379 controller()->GetTestingManager()->AddTestingProfile(&verified_profile);
2381 CreditCard credit_card(test::GetVerifiedCreditCard());
2382 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
2384 // Verify suggestions menus should be showing when Autofill is enabled.
2385 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC));
2386 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_BILLING));
2387 EXPECT_EQ(
2388 4, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
2390 EXPECT_CALL(*controller()->GetView(), ModelChanged());
2391 profile()->GetPrefs()->SetBoolean(prefs::kAutofillEnabled, false);
2393 // Verify billing and credit card suggestions menus are hidden when Autofill
2394 // is disabled.
2395 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC));
2396 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_BILLING));
2397 // And that the shipping suggestions menu has less selections.
2398 EXPECT_EQ(
2399 2, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
2401 // Additionally, editing fields should not show Autofill popups.
2402 ASSERT_NO_FATAL_FAILURE(controller()->UserEditedOrActivatedInput(
2403 SECTION_BILLING,
2404 NAME_BILLING_FULL,
2405 gfx::NativeView(),
2406 gfx::Rect(),
2407 verified_profile.GetInfo(AutofillType(NAME_FULL), "en-US").substr(0, 1),
2408 true));
2409 EXPECT_EQ(UNKNOWN_TYPE, controller()->popup_input_type());
2412 // Tests that user is prompted when using instrument with minimal address.
2413 TEST_F(AutofillDialogControllerTest, UpgradeMinimalAddress) {
2414 // A minimal address being selected should trigger error validation in the
2415 // view. Called once for each incomplete suggestion.
2416 EXPECT_CALL(*controller()->GetView(), UpdateForErrors());
2418 scoped_ptr<wallet::WalletItems> wallet_items =
2419 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
2420 wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentWithIdAndAddress(
2421 "id", wallet::GetTestMinimalAddress()));
2422 scoped_ptr<wallet::Address> address(wallet::GetTestShippingAddress());
2423 address->set_is_complete_address(false);
2424 wallet_items->AddAddress(address.Pass());
2425 controller()->OnDidGetWalletItems(wallet_items.Pass());
2427 // Assert that dialog's SECTION_CC_BILLING section is in edit mode.
2428 ASSERT_TRUE(controller()->IsEditingExistingData(SECTION_CC_BILLING));
2429 // Shipping section should be in edit mode because of
2430 // is_minimal_shipping_address.
2431 ASSERT_TRUE(controller()->IsEditingExistingData(SECTION_SHIPPING));
2434 TEST_F(AutofillDialogControllerTest, RiskNeverLoadsWithPendingLegalDocuments) {
2435 EXPECT_CALL(*controller(), LoadRiskFingerprintData()).Times(0);
2437 scoped_ptr<wallet::WalletItems> wallet_items = CompleteAndValidWalletItems();
2438 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
2439 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
2440 controller()->OnDidGetWalletItems(wallet_items.Pass());
2441 controller()->OnAccept();
2444 TEST_F(AutofillDialogControllerTest, RiskLoadsAfterAcceptingLegalDocuments) {
2445 EXPECT_CALL(*controller(), LoadRiskFingerprintData()).Times(0);
2447 scoped_ptr<wallet::WalletItems> wallet_items = CompleteAndValidWalletItems();
2448 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
2449 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
2450 controller()->OnDidGetWalletItems(wallet_items.Pass());
2452 testing::Mock::VerifyAndClear(controller());
2453 EXPECT_CALL(*controller(), LoadRiskFingerprintData());
2455 controller()->OnAccept();
2457 // Simulate a risk load and verify |GetRiskData()| matches the encoded value.
2458 controller()->OnDidAcceptLegalDocuments();
2459 controller()->OnDidLoadRiskFingerprintData(GetFakeFingerprint().Pass());
2460 EXPECT_EQ(kFakeFingerprintEncoded, controller()->GetRiskData());
2463 TEST_F(AutofillDialogControllerTest, NoManageMenuItemForNewWalletUsers) {
2464 // Make sure the menu model item is created for a returning Wallet user.
2465 scoped_ptr<wallet::WalletItems> wallet_items =
2466 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
2467 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
2468 wallet_items->AddAddress(wallet::GetTestShippingAddress());
2469 controller()->OnDidGetWalletItems(wallet_items.Pass());
2471 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING));
2472 // "Same as billing", "123 address", "Add address...", and "Manage addresses".
2473 EXPECT_EQ(
2474 4, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
2476 // Make sure the menu model item is not created for new Wallet users.
2477 base::DictionaryValue dict;
2478 scoped_ptr<base::ListValue> required_actions(new base::ListValue);
2479 required_actions->AppendString("setup_wallet");
2480 dict.Set("required_action", required_actions.release());
2481 controller()->OnDidGetWalletItems(
2482 wallet::WalletItems::CreateWalletItems(dict).Pass());
2484 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC_BILLING));
2485 // "Same as billing" and "Add address...".
2486 EXPECT_EQ(
2487 2, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
2490 TEST_F(AutofillDialogControllerTest, ShippingSectionCanBeHidden) {
2491 FormFieldData email_field;
2492 email_field.autocomplete_attribute = "email";
2493 FormFieldData cc_field;
2494 cc_field.autocomplete_attribute = "cc-number";
2495 FormFieldData billing_field;
2496 billing_field.autocomplete_attribute = "billing address-level1";
2498 FormData form_data;
2499 form_data.fields.push_back(email_field);
2500 form_data.fields.push_back(cc_field);
2501 form_data.fields.push_back(billing_field);
2503 AutofillProfile full_profile(test::GetVerifiedProfile());
2504 controller()->GetTestingManager()->AddTestingProfile(&full_profile);
2505 SetUpControllerWithFormData(form_data);
2507 SwitchToAutofill();
2509 EXPECT_FALSE(controller()->SectionIsActive(SECTION_SHIPPING));
2511 FillCreditCardInputs();
2512 controller()->OnAccept();
2513 EXPECT_TRUE(form_structure());
2516 TEST_F(AutofillDialogControllerTest, ShippingSectionCanBeHiddenForWallet) {
2517 FormFieldData email_field;
2518 email_field.autocomplete_attribute = "email";
2519 FormFieldData cc_field;
2520 cc_field.autocomplete_attribute = "cc-number";
2521 FormFieldData billing_field;
2522 billing_field.autocomplete_attribute = "billing address-level1";
2524 FormData form_data;
2525 form_data.fields.push_back(email_field);
2526 form_data.fields.push_back(cc_field);
2527 form_data.fields.push_back(billing_field);
2529 SetUpControllerWithFormData(form_data);
2530 EXPECT_FALSE(controller()->SectionIsActive(SECTION_SHIPPING));
2531 EXPECT_FALSE(controller()->IsShippingAddressRequired());
2533 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_));
2534 scoped_ptr<wallet::WalletItems> wallet_items =
2535 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
2536 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
2537 SubmitWithWalletItems(wallet_items.Pass());
2538 controller()->OnDidGetFullWallet(wallet::GetTestFullWalletInstrumentOnly());
2539 controller()->ForceFinishSubmit();
2540 EXPECT_TRUE(form_structure());
2543 TEST_F(AutofillDialogControllerTest, NotProdNotification) {
2544 // To make IsPayingWithWallet() true.
2545 controller()->OnDidGetWalletItems(
2546 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED));
2548 CommandLine* command_line = CommandLine::ForCurrentProcess();
2549 ASSERT_EQ(
2551 command_line->GetSwitchValueASCII(switches::kWalletServiceUseSandbox));
2553 command_line->AppendSwitchASCII(switches::kWalletServiceUseSandbox, "1");
2554 EXPECT_EQ(1U,
2555 NotificationsOfType(DialogNotification::DEVELOPER_WARNING).size());
2558 TEST_F(AutofillDialogControllerTest, NoNotProdNotification) {
2559 // To make IsPayingWithWallet() true.
2560 controller()->OnDidGetWalletItems(
2561 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED));
2563 CommandLine* command_line = CommandLine::ForCurrentProcess();
2564 ASSERT_EQ(
2566 command_line->GetSwitchValueASCII(switches::kWalletServiceUseSandbox));
2568 command_line->AppendSwitchASCII(switches::kWalletServiceUseSandbox, "0");
2569 EXPECT_EQ(0U,
2570 NotificationsOfType(DialogNotification::DEVELOPER_WARNING).size());
2573 // Ensure Wallet instruments marked expired by the server are shown as invalid.
2574 TEST_F(AutofillDialogControllerTest, WalletExpiredCard) {
2575 scoped_ptr<wallet::WalletItems> wallet_items =
2576 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
2577 wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentExpired());
2578 controller()->OnDidGetWalletItems(wallet_items.Pass());
2580 EXPECT_TRUE(controller()->IsEditingExistingData(SECTION_CC_BILLING));
2582 const DetailInputs& inputs =
2583 controller()->RequestedFieldsForSection(SECTION_CC_BILLING);
2584 FieldValueMap outputs;
2585 CopyInitialValues(inputs, &outputs);
2587 // The local inputs are invalid because the server said so. They'll
2588 // stay invalid until they differ from the remotely fetched model.
2589 ValidityMessages messages = controller()->InputsAreValid(SECTION_CC_BILLING,
2590 outputs);
2591 EXPECT_TRUE(messages.HasSureError(CREDIT_CARD_EXP_MONTH));
2592 EXPECT_TRUE(messages.HasSureError(CREDIT_CARD_EXP_4_DIGIT_YEAR));
2594 // Make the local input year differ from the instrument.
2595 CopyInitialValues(inputs, &outputs);
2596 outputs[CREDIT_CARD_EXP_4_DIGIT_YEAR] = ASCIIToUTF16("3002");
2597 messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
2598 EXPECT_FALSE(HasAnyError(messages, CREDIT_CARD_EXP_MONTH));
2599 EXPECT_FALSE(HasAnyError(messages, CREDIT_CARD_EXP_4_DIGIT_YEAR));
2601 // Make the local input month differ from the instrument.
2602 CopyInitialValues(inputs, &outputs);
2603 outputs[CREDIT_CARD_EXP_MONTH] = ASCIIToUTF16("06");
2604 messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
2605 EXPECT_FALSE(HasAnyError(messages, CREDIT_CARD_EXP_MONTH));
2606 EXPECT_FALSE(HasAnyError(messages, CREDIT_CARD_EXP_4_DIGIT_YEAR));
2609 TEST_F(AutofillDialogControllerTest, ChooseAnotherInstrumentOrAddress) {
2610 SubmitWithWalletItems(CompleteAndValidWalletItems());
2612 EXPECT_EQ(0U, NotificationsOfType(
2613 DialogNotification::REQUIRED_ACTION).size());
2614 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems(_, _));
2615 controller()->OnDidGetFullWallet(
2616 wallet::GetTestFullWalletWithRequiredActions(
2617 std::vector<wallet::RequiredAction>(
2618 1, wallet::CHOOSE_ANOTHER_INSTRUMENT_OR_ADDRESS)));
2619 EXPECT_EQ(1U, NotificationsOfType(
2620 DialogNotification::REQUIRED_ACTION).size());
2621 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
2623 controller()->OnAccept();
2624 EXPECT_EQ(0U, NotificationsOfType(
2625 DialogNotification::REQUIRED_ACTION).size());
2628 TEST_F(AutofillDialogControllerTest, NewCardBubbleShown) {
2629 SwitchToAutofill();
2630 FillCreditCardInputs();
2631 controller()->OnAccept();
2632 controller()->ViewClosed();
2634 EXPECT_EQ(1, mock_new_card_bubble_controller()->bubbles_shown());
2635 EXPECT_EQ(0, test_generated_bubble_controller()->bubbles_shown());
2638 TEST_F(AutofillDialogControllerTest, GeneratedCardBubbleShown) {
2639 SubmitWithWalletItems(CompleteAndValidWalletItems());
2640 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
2641 controller()->ForceFinishSubmit();
2642 controller()->ViewClosed();
2644 EXPECT_EQ(0, mock_new_card_bubble_controller()->bubbles_shown());
2645 EXPECT_EQ(1, test_generated_bubble_controller()->bubbles_shown());
2648 // Verify that new Wallet data is fetched when the user switches away from the
2649 // tab hosting the Autofill dialog and back. Also verify that the user's
2650 // selection is preserved across this re-fetch.
2651 TEST_F(AutofillDialogControllerTest, ReloadWalletItemsOnActivation) {
2652 // Initialize some Wallet data.
2653 scoped_ptr<wallet::WalletItems> wallet_items =
2654 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
2655 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
2656 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
2657 wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
2658 wallet_items->AddAddress(wallet::GetTestShippingAddress());
2659 controller()->OnDidGetWalletItems(wallet_items.Pass());
2661 // Initially, the default entries should be selected.
2662 ui::MenuModel* cc_billing_model =
2663 controller()->MenuModelForSection(SECTION_CC_BILLING);
2664 ui::MenuModel* shipping_model =
2665 controller()->MenuModelForSection(SECTION_SHIPPING);
2666 // "add", "manage", and 2 suggestions.
2667 ASSERT_EQ(4, cc_billing_model->GetItemCount());
2668 EXPECT_TRUE(cc_billing_model->IsItemCheckedAt(0));
2669 // "use billing", "add", "manage", and 2 suggestions.
2670 ASSERT_EQ(5, shipping_model->GetItemCount());
2671 EXPECT_TRUE(shipping_model->IsItemCheckedAt(2));
2673 // Select entries other than the defaults.
2674 cc_billing_model->ActivatedAt(1);
2675 shipping_model->ActivatedAt(1);
2676 // 2 suggestions, "add", and "manage".
2677 ASSERT_EQ(4, cc_billing_model->GetItemCount());
2678 EXPECT_TRUE(cc_billing_model->IsItemCheckedAt(1));
2679 // "use billing", 2 suggestions, "add", "manage".
2680 ASSERT_EQ(5, shipping_model->GetItemCount());
2681 EXPECT_TRUE(shipping_model-> IsItemCheckedAt(1));
2683 // Simulate switching away from the tab and back. This should issue a request
2684 // for wallet items.
2685 controller()->ClearLastWalletItemsFetchTimestampForTesting();
2686 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems(_, _));
2687 controller()->TabActivated();
2689 // Simulate a response that includes different items.
2690 wallet_items = wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
2691 wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentExpired());
2692 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
2693 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
2694 wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
2695 controller()->OnDidGetWalletItems(wallet_items.Pass());
2697 // The previously selected entries should still be selected.
2698 // 3 suggestions, "add", and "manage".
2699 ASSERT_EQ(5, cc_billing_model->GetItemCount());
2700 EXPECT_TRUE(cc_billing_model->IsItemCheckedAt(2));
2701 // "use billing", 1 suggestion, "add", and "manage".
2702 ASSERT_EQ(4, shipping_model->GetItemCount());
2703 EXPECT_TRUE(shipping_model->IsItemCheckedAt(1));
2706 // Verify that if the default values change when re-fetching Wallet data, these
2707 // new default values are selected in the dialog.
2708 TEST_F(AutofillDialogControllerTest,
2709 ReloadWalletItemsOnActivationWithNewDefaults) {
2710 // Initialize some Wallet data.
2711 scoped_ptr<wallet::WalletItems> wallet_items =
2712 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
2713 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
2714 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
2715 wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
2716 wallet_items->AddAddress(wallet::GetTestShippingAddress());
2717 controller()->OnDidGetWalletItems(wallet_items.Pass());
2719 // Initially, the default entries should be selected.
2720 ui::MenuModel* cc_billing_model =
2721 controller()->MenuModelForSection(SECTION_CC_BILLING);
2722 ui::MenuModel* shipping_model =
2723 controller()->MenuModelForSection(SECTION_SHIPPING);
2724 // 2 suggestions, "add", and "manage".
2725 ASSERT_EQ(4, cc_billing_model->GetItemCount());
2726 EXPECT_TRUE(cc_billing_model->IsItemCheckedAt(0));
2727 // "use billing", 2 suggestions, "add", and "manage".
2728 ASSERT_EQ(5, shipping_model->GetItemCount());
2729 EXPECT_TRUE(shipping_model->IsItemCheckedAt(2));
2731 // Simulate switching away from the tab and back. This should issue a request
2732 // for wallet items.
2733 controller()->ClearLastWalletItemsFetchTimestampForTesting();
2734 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems(_, _));
2735 controller()->TabActivated();
2737 // Simulate a response that includes different default values.
2738 wallet_items =
2739 wallet::GetTestWalletItemsWithDefaultIds("new_default_instrument_id",
2740 "new_default_address_id",
2741 wallet::AMEX_DISALLOWED);
2742 scoped_ptr<wallet::Address> other_address = wallet::GetTestShippingAddress();
2743 other_address->set_object_id("other_address_id");
2744 scoped_ptr<wallet::Address> new_default_address =
2745 wallet::GetTestNonDefaultShippingAddress();
2746 new_default_address->set_object_id("new_default_address_id");
2748 wallet_items->AddInstrument(
2749 wallet::GetTestMaskedInstrumentWithId("other_instrument_id"));
2750 wallet_items->AddInstrument(
2751 wallet::GetTestMaskedInstrumentWithId("new_default_instrument_id"));
2752 wallet_items->AddAddress(new_default_address.Pass());
2753 wallet_items->AddAddress(other_address.Pass());
2754 controller()->OnDidGetWalletItems(wallet_items.Pass());
2756 // The new default entries should be selected.
2757 // 2 suggestions, "add", and "manage".
2758 ASSERT_EQ(4, cc_billing_model->GetItemCount());
2759 EXPECT_TRUE(cc_billing_model->IsItemCheckedAt(1));
2760 // "use billing", 2 suggestions, "add", and "manage".
2761 ASSERT_EQ(5, shipping_model->GetItemCount());
2762 EXPECT_TRUE(shipping_model->IsItemCheckedAt(1));
2765 TEST_F(AutofillDialogControllerTest, ReloadWithEmptyWalletItems) {
2766 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
2767 controller()->MenuModelForSection(SECTION_CC_BILLING)->ActivatedAt(1);
2768 controller()->MenuModelForSection(SECTION_SHIPPING)->ActivatedAt(1);
2770 controller()->ClearLastWalletItemsFetchTimestampForTesting();
2771 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems(_, _));
2772 controller()->TabActivated();
2774 controller()->OnDidGetWalletItems(
2775 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED));
2777 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC_BILLING));
2778 EXPECT_EQ(
2779 3, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
2782 TEST_F(AutofillDialogControllerTest, SaveInChromeByDefault) {
2783 EXPECT_TRUE(controller()->ShouldSaveInChrome());
2784 SwitchToAutofill();
2785 FillCreditCardInputs();
2786 controller()->OnAccept();
2787 EXPECT_TRUE(controller()->ShouldSaveInChrome());
2790 TEST_F(AutofillDialogControllerTest,
2791 SaveInChromePreferenceNotRememberedOnCancel) {
2792 EXPECT_TRUE(controller()->ShouldSaveInChrome());
2793 SwitchToAutofill();
2794 controller()->GetView()->CheckSaveDetailsLocallyCheckbox(false);
2795 controller()->OnCancel();
2796 EXPECT_TRUE(controller()->ShouldSaveInChrome());
2799 TEST_F(AutofillDialogControllerTest,
2800 SaveInChromePreferenceRememberedOnSuccess) {
2801 EXPECT_TRUE(controller()->ShouldSaveInChrome());
2802 SwitchToAutofill();
2803 FillCreditCardInputs();
2804 controller()->GetView()->CheckSaveDetailsLocallyCheckbox(false);
2805 controller()->OnAccept();
2806 EXPECT_FALSE(controller()->ShouldSaveInChrome());
2809 TEST_F(AutofillDialogControllerTest,
2810 SubmitButtonIsDisabled_SpinnerFinishesBeforeDelay) {
2811 // Reset Wallet state.
2812 controller()->OnDidGetWalletItems(scoped_ptr<wallet::WalletItems>());
2814 EXPECT_EQ(1, controller()->get_submit_button_delay_count());
2816 // Begin the submit button delay.
2817 controller()->SimulateSubmitButtonDelayBegin();
2819 EXPECT_TRUE(controller()->ShouldShowSpinner());
2820 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2822 // Stop the spinner.
2823 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
2825 EXPECT_FALSE(controller()->ShouldShowSpinner());
2826 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2828 // End the submit button delay.
2829 controller()->SimulateSubmitButtonDelayEnd();
2831 EXPECT_FALSE(controller()->ShouldShowSpinner());
2832 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2835 TEST_F(AutofillDialogControllerTest,
2836 SubmitButtonIsDisabled_SpinnerFinishesAfterDelay) {
2837 // Reset Wallet state.
2838 controller()->OnDidGetWalletItems(scoped_ptr<wallet::WalletItems>());
2840 EXPECT_EQ(1, controller()->get_submit_button_delay_count());
2842 // Begin the submit button delay.
2843 controller()->SimulateSubmitButtonDelayBegin();
2845 EXPECT_TRUE(controller()->ShouldShowSpinner());
2846 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2848 // End the submit button delay.
2849 controller()->SimulateSubmitButtonDelayEnd();
2851 EXPECT_TRUE(controller()->ShouldShowSpinner());
2852 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2854 // Stop the spinner.
2855 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
2857 EXPECT_FALSE(controller()->ShouldShowSpinner());
2858 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2861 TEST_F(AutofillDialogControllerTest, SubmitButtonIsDisabled_NoSpinner) {
2862 SwitchToAutofill();
2864 EXPECT_EQ(1, controller()->get_submit_button_delay_count());
2866 // Begin the submit button delay.
2867 controller()->SimulateSubmitButtonDelayBegin();
2869 EXPECT_FALSE(controller()->ShouldShowSpinner());
2870 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2872 // End the submit button delay.
2873 controller()->SimulateSubmitButtonDelayEnd();
2875 EXPECT_FALSE(controller()->ShouldShowSpinner());
2876 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2879 TEST_F(AutofillDialogControllerTest, IconsForFields_NoCreditCard) {
2880 FieldValueMap values;
2881 values[EMAIL_ADDRESS] = ASCIIToUTF16(kFakeEmail);
2882 FieldIconMap icons = controller()->IconsForFields(values);
2883 EXPECT_TRUE(icons.empty());
2886 TEST_F(AutofillDialogControllerTest, IconsForFields_CreditCardNumberOnly) {
2887 FieldValueMap values;
2888 values[EMAIL_ADDRESS] = ASCIIToUTF16(kFakeEmail);
2889 values[CREDIT_CARD_NUMBER] = ASCIIToUTF16(kTestCCNumberVisa);
2890 FieldIconMap icons = controller()->IconsForFields(values);
2891 EXPECT_EQ(1UL, icons.size());
2892 EXPECT_EQ(1UL, icons.count(CREDIT_CARD_NUMBER));
2895 TEST_F(AutofillDialogControllerTest, IconsForFields_CvcOnly) {
2896 FieldValueMap values;
2897 values[EMAIL_ADDRESS] = ASCIIToUTF16(kFakeEmail);
2898 values[CREDIT_CARD_VERIFICATION_CODE] = ASCIIToUTF16("123");
2899 FieldIconMap icons = controller()->IconsForFields(values);
2900 EXPECT_EQ(1UL, icons.size());
2901 EXPECT_EQ(1UL, icons.count(CREDIT_CARD_VERIFICATION_CODE));
2904 TEST_F(AutofillDialogControllerTest, IconsForFields_BothCreditCardAndCvc) {
2905 FieldValueMap values;
2906 values[EMAIL_ADDRESS] = ASCIIToUTF16(kFakeEmail);
2907 values[CREDIT_CARD_NUMBER] = ASCIIToUTF16(kTestCCNumberVisa);
2908 values[CREDIT_CARD_VERIFICATION_CODE] = ASCIIToUTF16("123");
2909 FieldIconMap icons = controller()->IconsForFields(values);
2910 EXPECT_EQ(2UL, icons.size());
2911 EXPECT_EQ(1UL, icons.count(CREDIT_CARD_VERIFICATION_CODE));
2912 EXPECT_EQ(1UL, icons.count(CREDIT_CARD_NUMBER));
2915 TEST_F(AutofillDialogControllerTest, FieldControlsIcons) {
2916 EXPECT_TRUE(controller()->FieldControlsIcons(CREDIT_CARD_NUMBER));
2917 EXPECT_FALSE(controller()->FieldControlsIcons(CREDIT_CARD_VERIFICATION_CODE));
2918 EXPECT_FALSE(controller()->FieldControlsIcons(EMAIL_ADDRESS));
2921 TEST_F(AutofillDialogControllerTest, SaveCreditCardIncludesName_NoBilling) {
2922 SwitchToAutofill();
2924 CreditCard test_credit_card(test::GetVerifiedCreditCard());
2925 FillInputs(SECTION_CC, test_credit_card);
2927 AutofillProfile test_profile(test::GetVerifiedProfile());
2928 FillInputs(SECTION_BILLING, test_profile);
2930 UseBillingForShipping();
2932 controller()->GetView()->CheckSaveDetailsLocallyCheckbox(true);
2933 controller()->OnAccept();
2935 TestPersonalDataManager* test_pdm = controller()->GetTestingManager();
2936 const CreditCard& imported_card = test_pdm->imported_credit_card();
2937 EXPECT_EQ(test_profile.GetInfo(AutofillType(NAME_FULL), "en-US"),
2938 imported_card.GetRawInfo(CREDIT_CARD_NAME));
2941 TEST_F(AutofillDialogControllerTest, SaveCreditCardIncludesName_WithBilling) {
2942 SwitchToAutofill();
2944 TestPersonalDataManager* test_pdm = controller()->GetTestingManager();
2945 AutofillProfile test_profile(test::GetVerifiedProfile());
2947 EXPECT_CALL(*controller()->GetView(), ModelChanged());
2948 test_pdm->AddTestingProfile(&test_profile);
2949 ASSERT_TRUE(controller()->MenuModelForSection(SECTION_BILLING));
2951 CreditCard test_credit_card(test::GetVerifiedCreditCard());
2952 FillInputs(SECTION_CC, test_credit_card);
2954 controller()->GetView()->CheckSaveDetailsLocallyCheckbox(true);
2955 controller()->OnAccept();
2957 const CreditCard& imported_card = test_pdm->imported_credit_card();
2958 EXPECT_EQ(test_profile.GetInfo(AutofillType(NAME_FULL), "en-US"),
2959 imported_card.GetRawInfo(CREDIT_CARD_NAME));
2961 controller()->ViewClosed();
2964 TEST_F(AutofillDialogControllerTest, InputEditability) {
2965 // Empty wallet items: all fields are editable.
2966 scoped_ptr<wallet::WalletItems> items =
2967 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
2968 controller()->OnDidGetWalletItems(items.Pass());
2970 DialogSection sections[] = { SECTION_CC_BILLING, SECTION_SHIPPING };
2971 for (size_t i = 0; i < arraysize(sections); ++i) {
2972 const DetailInputs& inputs =
2973 controller()->RequestedFieldsForSection(sections[i]);
2974 for (size_t j = 0; j < inputs.size(); ++j) {
2975 EXPECT_TRUE(controller()->InputIsEditable(inputs[j], sections[i]));
2979 // Expired instrument: CC number + CVV are not editable.
2980 items = wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
2981 scoped_ptr<wallet::WalletItems::MaskedInstrument> expired_instrument =
2982 wallet::GetTestMaskedInstrumentExpired();
2983 items->AddInstrument(expired_instrument.Pass());
2984 controller()->OnDidGetWalletItems(items.Pass());
2985 EXPECT_TRUE(controller()->IsEditingExistingData(SECTION_CC_BILLING));
2987 const DetailInputs& inputs =
2988 controller()->RequestedFieldsForSection(SECTION_CC_BILLING);
2989 FieldValueMap outputs;
2990 CopyInitialValues(inputs, &outputs);
2991 controller()->GetView()->SetUserInput(SECTION_CC_BILLING, outputs);
2993 for (size_t i = 0; i < arraysize(sections); ++i) {
2994 const DetailInputs& inputs =
2995 controller()->RequestedFieldsForSection(sections[i]);
2996 for (size_t j = 0; j < inputs.size(); ++j) {
2997 if (inputs[j].type == CREDIT_CARD_NUMBER ||
2998 inputs[j].type == CREDIT_CARD_VERIFICATION_CODE) {
2999 EXPECT_FALSE(controller()->InputIsEditable(inputs[j], sections[i]));
3000 } else {
3001 EXPECT_TRUE(controller()->InputIsEditable(inputs[j], sections[i]));
3006 // User changes the billing address; same story.
3007 outputs[ADDRESS_BILLING_ZIP] = ASCIIToUTF16("77025");
3008 controller()->GetView()->SetUserInput(SECTION_CC_BILLING, outputs);
3009 for (size_t i = 0; i < arraysize(sections); ++i) {
3010 const DetailInputs& inputs =
3011 controller()->RequestedFieldsForSection(sections[i]);
3012 for (size_t j = 0; j < inputs.size(); ++j) {
3013 if (inputs[j].type == CREDIT_CARD_NUMBER ||
3014 inputs[j].type == CREDIT_CARD_VERIFICATION_CODE) {
3015 EXPECT_FALSE(controller()->InputIsEditable(inputs[j], sections[i]));
3016 } else {
3017 EXPECT_TRUE(controller()->InputIsEditable(inputs[j], sections[i]));
3022 // User changes a detail of the CC itself (expiration date), CVV is now
3023 // editable (and mandatory).
3024 outputs[CREDIT_CARD_EXP_MONTH] = ASCIIToUTF16("06");
3025 controller()->GetView()->SetUserInput(SECTION_CC_BILLING, outputs);
3026 for (size_t i = 0; i < arraysize(sections); ++i) {
3027 const DetailInputs& inputs =
3028 controller()->RequestedFieldsForSection(sections[i]);
3029 for (size_t j = 0; j < inputs.size(); ++j) {
3030 if (inputs[j].type == CREDIT_CARD_NUMBER)
3031 EXPECT_FALSE(controller()->InputIsEditable(inputs[j], sections[i]));
3032 else
3033 EXPECT_TRUE(controller()->InputIsEditable(inputs[j], sections[i]));
3038 // When the default country is something besides US, wallet is not selected
3039 // and the account chooser shouldn't be visible.
3040 TEST_F(AutofillDialogControllerTest, HideWalletInOtherCountries) {
3041 // Addresses from different countries.
3042 AutofillProfile us_profile(base::GenerateGUID(), kSettingsOrigin),
3043 es_profile(base::GenerateGUID(), kSettingsOrigin),
3044 es_profile2(base::GenerateGUID(), kSettingsOrigin);
3045 us_profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
3046 es_profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("ES"));
3047 es_profile2.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("ES"));
3049 // If US is indicated (via timezone), show Wallet.
3050 ResetControllerWithFormData(DefaultFormData());
3051 controller()->GetTestingManager()->set_timezone_country_code("US");
3052 controller()->Show();
3053 EXPECT_TRUE(
3054 controller()->AccountChooserModelForTesting()->WalletIsSelected());
3055 controller()->OnDidFetchWalletCookieValue(std::string());
3056 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
3057 EXPECT_TRUE(controller()->ShouldShowAccountChooser());
3058 EXPECT_TRUE(
3059 controller()->AccountChooserModelForTesting()->WalletIsSelected());
3061 // If US is not indicated, don't show Wallet.
3062 ResetControllerWithFormData(DefaultFormData());
3063 controller()->GetTestingManager()->set_timezone_country_code("ES");
3064 controller()->Show();
3065 controller()->OnDidFetchWalletCookieValue(std::string());
3066 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
3067 EXPECT_FALSE(controller()->ShouldShowAccountChooser());
3069 // If US is indicated (via a profile), show Wallet.
3070 ResetControllerWithFormData(DefaultFormData());
3071 controller()->GetTestingManager()->set_timezone_country_code("ES");
3072 controller()->GetTestingManager()->AddTestingProfile(&us_profile);
3073 controller()->Show();
3074 controller()->OnDidFetchWalletCookieValue(std::string());
3075 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
3076 EXPECT_TRUE(controller()->ShouldShowAccountChooser());
3077 EXPECT_TRUE(
3078 controller()->AccountChooserModelForTesting()->WalletIsSelected());
3080 // Make sure the profile doesn't just override the timezone.
3081 ResetControllerWithFormData(DefaultFormData());
3082 controller()->GetTestingManager()->set_timezone_country_code("US");
3083 controller()->GetTestingManager()->AddTestingProfile(&es_profile);
3084 controller()->Show();
3085 controller()->OnDidFetchWalletCookieValue(std::string());
3086 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
3087 EXPECT_TRUE(controller()->ShouldShowAccountChooser());
3088 EXPECT_TRUE(
3089 controller()->AccountChooserModelForTesting()->WalletIsSelected());
3091 // Only takes one US address to enable Wallet.
3092 ResetControllerWithFormData(DefaultFormData());
3093 controller()->GetTestingManager()->set_timezone_country_code("FR");
3094 controller()->GetTestingManager()->AddTestingProfile(&es_profile);
3095 controller()->GetTestingManager()->AddTestingProfile(&es_profile2);
3096 controller()->GetTestingManager()->AddTestingProfile(&us_profile);
3097 controller()->Show();
3098 controller()->OnDidFetchWalletCookieValue(std::string());
3099 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
3100 EXPECT_TRUE(controller()->ShouldShowAccountChooser());
3101 EXPECT_TRUE(
3102 controller()->AccountChooserModelForTesting()->WalletIsSelected());
3105 TEST_F(AutofillDialogControllerTest, DontGetWalletTillNecessary) {
3106 // When starting on local data mode, the dialog will provide a "Use Google
3107 // Wallet" link.
3108 profile()->GetPrefs()->SetBoolean(
3109 ::prefs::kAutofillDialogPayWithoutWallet, true);
3110 ResetControllerWithFormData(DefaultFormData());
3111 controller()->Show();
3112 base::string16 use_wallet_text = controller()->SignInLinkText();
3113 EXPECT_EQ(TestAutofillDialogController::NOT_CHECKED,
3114 controller()->SignedInState());
3116 // When clicked, this link will ask for wallet items. If there's a signin
3117 // failure, the link will switch to "Sign in to use Google Wallet".
3118 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems(_, _));
3119 controller()->SignInLinkClicked();
3120 EXPECT_NE(TestAutofillDialogController::NOT_CHECKED,
3121 controller()->SignedInState());
3122 controller()->OnDidFetchWalletCookieValue(std::string());
3123 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
3124 controller()->OnPassiveSigninFailure(GoogleServiceAuthError(
3125 GoogleServiceAuthError::CONNECTION_FAILED));
3126 EXPECT_NE(use_wallet_text, controller()->SignInLinkText());
3129 TEST_F(AutofillDialogControllerTest, MultiAccountSwitch) {
3130 std::vector<std::string> users;
3131 users.push_back("user_1@example.com");
3132 users.push_back("user_2@example.com");
3133 controller()->OnDidGetWalletItems(
3134 wallet::GetTestWalletItemsWithUsers(users, 0));
3136 // Items should be: Account 1, account 2, add account, disable wallet.
3137 EXPECT_EQ(4, controller()->MenuModelForAccountChooser()->GetItemCount());
3138 EXPECT_EQ(0U, controller()->GetTestingWalletClient()->user_index());
3140 // GetWalletItems should be called when the user switches accounts.
3141 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems(_, _));
3142 controller()->MenuModelForAccountChooser()->ActivatedAt(1);
3143 // The wallet client should be updated to the new user index.
3144 EXPECT_EQ(1U, controller()->GetTestingWalletClient()->user_index());
3147 TEST_F(AutofillDialogControllerTest, PassiveAuthFailure) {
3148 controller()->OnDidGetWalletItems(
3149 wallet::GetTestWalletItemsWithRequiredAction(
3150 wallet::PASSIVE_GAIA_AUTH));
3151 EXPECT_TRUE(controller()->ShouldShowSpinner());
3152 controller()->OnPassiveSigninFailure(GoogleServiceAuthError(
3153 GoogleServiceAuthError::NONE));
3154 EXPECT_FALSE(controller()->ShouldShowSpinner());
3157 TEST_F(AutofillDialogControllerTest, WalletShippingSameAsBilling) {
3158 // Assert initial state.
3159 ASSERT_FALSE(profile()->GetPrefs()->HasPrefPath(
3160 ::prefs::kAutofillDialogWalletShippingSameAsBilling));
3162 // Verify that false pref defaults to wallet defaults.
3163 scoped_ptr<wallet::WalletItems> wallet_items =
3164 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
3165 wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
3166 wallet_items->AddAddress(wallet::GetTestShippingAddress());
3167 controller()->OnDidGetWalletItems(wallet_items.Pass());
3168 ASSERT_FALSE(profile()->GetPrefs()->GetBoolean(
3169 ::prefs::kAutofillDialogWalletShippingSameAsBilling));
3170 EXPECT_EQ(2, GetMenuModelForSection(SECTION_SHIPPING)->checked_item());
3172 // Set "Same as Billing" for the shipping address and verify it sets the pref
3173 // and selects the appropriate menu item.
3174 UseBillingForShipping();
3175 ASSERT_EQ(0, GetMenuModelForSection(SECTION_SHIPPING)->checked_item());
3176 controller()->ForceFinishSubmit();
3177 ASSERT_TRUE(profile()->GetPrefs()->GetBoolean(
3178 ::prefs::kAutofillDialogWalletShippingSameAsBilling));
3180 // Getting new wallet info shouldn't disrupt the preference and menu should be
3181 // set accordingly.
3182 Reset();
3183 wallet_items = wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
3184 wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
3185 wallet_items->AddAddress(wallet::GetTestShippingAddress());
3186 controller()->OnDidGetWalletItems(wallet_items.Pass());
3187 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(
3188 ::prefs::kAutofillDialogWalletShippingSameAsBilling));
3189 EXPECT_EQ(0, GetMenuModelForSection(SECTION_SHIPPING)->checked_item());
3191 // Choose a different address and ensure pref gets set to false.
3192 controller()->MenuModelForSection(SECTION_SHIPPING)->ActivatedAt(1);
3193 controller()->ForceFinishSubmit();
3194 EXPECT_FALSE(profile()->GetPrefs()->GetBoolean(
3195 ::prefs::kAutofillDialogWalletShippingSameAsBilling));
3198 // Verifies that a call to the IconsForFields() method before the card type is
3199 // known returns a placeholder image that is at least as large as the icons for
3200 // all of the supported major credit card issuers.
3201 TEST_F(AutofillDialogControllerTest, IconReservedForCreditCardField) {
3202 FieldValueMap inputs;
3203 inputs[CREDIT_CARD_NUMBER] = base::string16();
3205 FieldIconMap icons = controller()->IconsForFields(inputs);
3206 EXPECT_EQ(1U, icons.size());
3208 ASSERT_EQ(1U, icons.count(CREDIT_CARD_NUMBER));
3209 gfx::Image placeholder_icon = icons[CREDIT_CARD_NUMBER];
3211 // Verify that the placeholder icon is at least as large as the icons for the
3212 // supported credit card issuers.
3213 const int kSupportedCardIdrs[] = {
3214 IDR_AUTOFILL_CC_AMEX,
3215 IDR_AUTOFILL_CC_DINERS,
3216 IDR_AUTOFILL_CC_DISCOVER,
3217 IDR_AUTOFILL_CC_GENERIC,
3218 IDR_AUTOFILL_CC_JCB,
3219 IDR_AUTOFILL_CC_MASTERCARD,
3220 IDR_AUTOFILL_CC_VISA,
3222 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
3223 for (size_t i = 0; i < arraysize(kSupportedCardIdrs); ++i) {
3224 SCOPED_TRACE(base::IntToString(i));
3225 gfx::Image supported_card_icon = rb.GetImageNamed(kSupportedCardIdrs[i]);
3226 EXPECT_GE(placeholder_icon.Width(), supported_card_icon.Width());
3227 EXPECT_GE(placeholder_icon.Height(), supported_card_icon.Height());
3231 TEST_F(AutofillDialogControllerTest, CountryChangeUpdatesSection) {
3232 TestAutofillDialogView* view = controller()->GetView();
3233 view->ClearSectionUpdates();
3235 controller()->UserEditedOrActivatedInput(SECTION_SHIPPING,
3236 ADDRESS_HOME_COUNTRY,
3237 gfx::NativeView(),
3238 gfx::Rect(),
3239 ASCIIToUTF16("Belarus"),
3240 true);
3241 std::map<DialogSection, size_t> updates = view->section_updates();
3242 EXPECT_EQ(1U, updates[SECTION_SHIPPING]);
3243 EXPECT_EQ(1U, updates.size());
3245 view->ClearSectionUpdates();
3247 controller()->UserEditedOrActivatedInput(SECTION_CC_BILLING,
3248 ADDRESS_BILLING_COUNTRY,
3249 gfx::NativeView(),
3250 gfx::Rect(),
3251 ASCIIToUTF16("France"),
3252 true);
3253 updates = view->section_updates();
3254 EXPECT_EQ(1U, updates[SECTION_CC_BILLING]);
3255 EXPECT_EQ(1U, updates.size());
3257 SwitchToAutofill();
3258 view->ClearSectionUpdates();
3260 controller()->UserEditedOrActivatedInput(SECTION_BILLING,
3261 ADDRESS_BILLING_COUNTRY,
3262 gfx::NativeView(),
3263 gfx::Rect(),
3264 ASCIIToUTF16("Italy"),
3265 true);
3266 updates = view->section_updates();
3267 EXPECT_EQ(1U, updates[SECTION_BILLING]);
3268 EXPECT_EQ(1U, updates.size());
3271 TEST_F(AutofillDialogControllerTest, CorrectCountryFromInputs) {
3272 EXPECT_CALL(*controller()->GetMockValidator(),
3273 ValidateAddress(CountryCodeMatcher("DE"), _, _));
3275 FieldValueMap billing_inputs;
3276 billing_inputs[ADDRESS_BILLING_COUNTRY] = ASCIIToUTF16("Germany");
3277 controller()->InputsAreValid(SECTION_BILLING, billing_inputs);
3279 EXPECT_CALL(*controller()->GetMockValidator(),
3280 ValidateAddress(CountryCodeMatcher("FR"), _, _));
3282 FieldValueMap shipping_inputs;
3283 shipping_inputs[ADDRESS_HOME_COUNTRY] = ASCIIToUTF16("France");
3284 controller()->InputsAreValid(SECTION_SHIPPING, shipping_inputs);
3287 TEST_F(AutofillDialogControllerTest, ValidationRulesLoadedOnCountryChange) {
3288 ResetControllerWithFormData(DefaultFormData());
3289 EXPECT_CALL(*controller()->GetMockValidator(),
3290 LoadRules("US")).Times(AtLeast(1));
3291 controller()->Show();
3293 EXPECT_CALL(*controller()->GetMockValidator(), LoadRules("FR"));
3294 controller()->UserEditedOrActivatedInput(SECTION_BILLING,
3295 ADDRESS_BILLING_COUNTRY,
3296 gfx::NativeView(),
3297 gfx::Rect(),
3298 ASCIIToUTF16("France"),
3299 true);
3302 TEST_F(AutofillDialogControllerTest, UsValidationRulesLoadedForJpOnlyProfile) {
3303 ResetControllerWithFormData(DefaultFormData());
3304 AutofillProfile jp_profile(base::GenerateGUID(), kSettingsOrigin);
3305 jp_profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("JP"));
3306 controller()->GetTestingManager()->AddTestingProfile(&jp_profile);
3307 EXPECT_CALL(*controller()->GetMockValidator(), LoadRules("US"));
3308 EXPECT_CALL(*controller()->GetMockValidator(),
3309 LoadRules("JP")).Times(AtLeast(1));
3310 controller()->Show();
3313 TEST_F(AutofillDialogControllerTest, InvalidWhenRulesNotReady) {
3314 // Select "Add new shipping address...".
3315 controller()->MenuModelForSection(SECTION_SHIPPING)->ActivatedAt(1);
3317 // If the rules haven't loaded yet, validation errors should show on submit.
3318 EXPECT_CALL(*controller()->GetMockValidator(),
3319 ValidateAddress(CountryCodeMatcher("US"), _, _)).
3320 WillRepeatedly(Return(AddressValidator::RULES_NOT_READY));
3322 FieldValueMap inputs;
3323 inputs[ADDRESS_HOME_ZIP] = ASCIIToUTF16("1234");
3324 inputs[ADDRESS_HOME_COUNTRY] = ASCIIToUTF16("United States");
3326 ValidityMessages messages =
3327 controller()->InputsAreValid(SECTION_SHIPPING, inputs);
3328 EXPECT_FALSE(messages.GetMessageOrDefault(ADDRESS_HOME_ZIP).text.empty());
3329 EXPECT_FALSE(messages.HasSureError(ADDRESS_HOME_ZIP));
3330 // Country should never show an error message as it's always valid.
3331 EXPECT_TRUE(messages.GetMessageOrDefault(ADDRESS_HOME_COUNTRY).text.empty());
3334 TEST_F(AutofillDialogControllerTest, ValidButUnverifiedWhenRulesFail) {
3335 SwitchToAutofill();
3337 // Add suggestions so the credit card and billing sections aren't showing
3338 // their manual inputs (to isolate to just shipping).
3339 AutofillProfile verified_profile(test::GetVerifiedProfile());
3340 controller()->GetTestingManager()->AddTestingProfile(&verified_profile);
3341 CreditCard verified_card(test::GetVerifiedCreditCard());
3342 controller()->GetTestingManager()->AddTestingCreditCard(&verified_card);
3344 // Select "Add new shipping address...".
3345 controller()->MenuModelForSection(SECTION_SHIPPING)->ActivatedAt(2);
3347 // If the rules are unavailable, validation errors should not show.
3348 EXPECT_CALL(*controller()->GetMockValidator(),
3349 ValidateAddress(CountryCodeMatcher("US"), _, _)).
3350 WillRepeatedly(Return(AddressValidator::RULES_UNAVAILABLE));
3352 FieldValueMap outputs;
3353 AutofillProfile full_profile(test::GetFullProfile());
3354 const DetailInputs& inputs =
3355 controller()->RequestedFieldsForSection(SECTION_SHIPPING);
3356 for (size_t i = 0; i < inputs.size(); ++i) {
3357 const ServerFieldType type = inputs[i].type;
3358 outputs[type] = full_profile.GetInfo(AutofillType(type), "en-US");
3360 controller()->GetView()->SetUserInput(SECTION_SHIPPING, outputs);
3361 controller()->GetView()->CheckSaveDetailsLocallyCheckbox(true);
3362 controller()->OnAccept();
3364 // Profiles saved while rules are unavailable shouldn't be verified.
3365 const AutofillProfile& imported_profile =
3366 controller()->GetTestingManager()->imported_profile();
3367 ASSERT_EQ(imported_profile.GetInfo(AutofillType(NAME_FULL), "en-US"),
3368 full_profile.GetInfo(AutofillType(NAME_FULL), "en-US"));
3369 EXPECT_EQ(imported_profile.origin(), GURL(kSourceUrl).GetOrigin().spec());
3370 EXPECT_FALSE(imported_profile.IsVerified());
3373 TEST_F(AutofillDialogControllerTest, LimitedCountryChoices) {
3374 ui::ComboboxModel* shipping_country_model =
3375 controller()->ComboboxModelForAutofillType(ADDRESS_HOME_COUNTRY);
3376 const int default_number_of_countries =
3377 shipping_country_model->GetItemCount();
3378 // We show a lot of countries by default, but the exact number doesn't matter.
3379 EXPECT_GT(default_number_of_countries, 50);
3381 // Create a form data that simulates:
3382 // <select autocomplete="billing country">
3383 // <option value="AU">Down Under</option>
3384 // <option value="">fR</option> <!-- Case doesn't matter -->
3385 // <option value="GRMNY">Germany</option>
3386 // </select>
3387 // Only country codes are respected, whether they're in value or the option's
3388 // text content. Thus the first two options should be recognized.
3389 FormData form_data;
3390 FormFieldData field;
3391 field.autocomplete_attribute = "billing country";
3392 field.option_contents.push_back(ASCIIToUTF16("Down Under"));
3393 field.option_values.push_back(ASCIIToUTF16("AU"));
3394 field.option_contents.push_back(ASCIIToUTF16("Fr"));
3395 field.option_values.push_back(ASCIIToUTF16(""));
3396 field.option_contents.push_back(ASCIIToUTF16("Germany"));
3397 field.option_values.push_back(ASCIIToUTF16("GRMNY"));
3399 FormFieldData cc_field;
3400 cc_field.autocomplete_attribute = "cc-csc";
3402 form_data.fields.push_back(field);
3403 form_data.fields.push_back(cc_field);
3404 ResetControllerWithFormData(form_data);
3405 controller()->Show();
3407 // Shipping model shouldn't have changed.
3408 shipping_country_model =
3409 controller()->ComboboxModelForAutofillType(ADDRESS_HOME_COUNTRY);
3410 EXPECT_EQ(default_number_of_countries,
3411 shipping_country_model->GetItemCount());
3412 // Billing model now only has two items.
3413 ui::ComboboxModel* billing_country_model =
3414 controller()->ComboboxModelForAutofillType(ADDRESS_BILLING_COUNTRY);
3415 ASSERT_EQ(2, billing_country_model->GetItemCount());
3416 EXPECT_EQ(billing_country_model->GetItemAt(0), ASCIIToUTF16("Australia"));
3417 EXPECT_EQ(billing_country_model->GetItemAt(1), ASCIIToUTF16("France"));
3419 // Make sure it also applies to profile suggestions.
3420 AutofillProfile us_profile(test::GetVerifiedProfile());
3421 us_profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
3422 controller()->GetTestingManager()->AddTestingProfile(&us_profile);
3423 // Don't show a suggestion if the only one that exists is disabled.
3424 EXPECT_FALSE(
3425 controller()->SuggestionStateForSection(SECTION_BILLING).visible);
3427 // Add a profile with an acceptable country; suggestion should be shown.
3428 ResetControllerWithFormData(form_data);
3429 controller()->Show();
3430 AutofillProfile au_profile(test::GetVerifiedProfile2());
3431 au_profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("AU"));
3432 controller()->GetTestingManager()->AddTestingProfile(&us_profile);
3433 controller()->GetTestingManager()->AddTestingProfile(&au_profile);
3434 ui::MenuModel* model = controller()->MenuModelForSection(SECTION_BILLING);
3435 ASSERT_TRUE(model);
3436 EXPECT_EQ(4, model->GetItemCount());
3437 EXPECT_FALSE(model->IsEnabledAt(0));
3438 EXPECT_TRUE(model->IsEnabledAt(1));
3440 // Add <input type="text" autocomplete="billing country"></input>
3441 // This should open up selection of all countries again.
3442 FormFieldData field2;
3443 field2.autocomplete_attribute = "billing country";
3444 form_data.fields.push_back(field2);
3445 ResetControllerWithFormData(form_data);
3446 controller()->Show();
3448 billing_country_model =
3449 controller()->ComboboxModelForAutofillType(ADDRESS_BILLING_COUNTRY);
3450 EXPECT_EQ(default_number_of_countries,
3451 billing_country_model->GetItemCount());
3454 TEST_F(AutofillDialogControllerTest, WalletUnsupportedCountries) {
3455 // Create a form data that simulates:
3456 // <select autocomplete="billing country">
3457 // <option value="IQ">Iraq</option>
3458 // <option value="MX">Mexico</option>
3459 // </select>
3460 // i.e. contains a mix of supported and unsupported countries.
3461 FormData form_data;
3462 FormFieldData field;
3463 field.autocomplete_attribute = "shipping country";
3464 field.option_contents.push_back(ASCIIToUTF16("Iraq"));
3465 field.option_values.push_back(ASCIIToUTF16("IQ"));
3466 field.option_contents.push_back(ASCIIToUTF16("Mexico"));
3467 field.option_values.push_back(ASCIIToUTF16("MX"));
3469 FormFieldData cc_field;
3470 cc_field.autocomplete_attribute = "cc-csc";
3472 form_data.fields.push_back(field);
3473 form_data.fields.push_back(cc_field);
3474 ResetControllerWithFormData(form_data);
3475 controller()->Show();
3477 ui::ComboboxModel* shipping_country_model =
3478 controller()->ComboboxModelForAutofillType(ADDRESS_HOME_COUNTRY);
3479 ASSERT_EQ(2, shipping_country_model->GetItemCount());
3480 EXPECT_EQ(shipping_country_model->GetItemAt(0), ASCIIToUTF16("Iraq"));
3481 EXPECT_EQ(shipping_country_model->GetItemAt(1), ASCIIToUTF16("Mexico"));
3483 // Switch to Wallet, add limitations.
3484 SetUpControllerWithFormData(form_data);
3485 SwitchToWallet();
3486 scoped_ptr<wallet::WalletItems> wallet_items =
3487 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
3488 wallet_items->AddAllowedShippingCountry("MX");
3489 wallet_items->AddAllowedShippingCountry("GB");
3490 controller()->OnDidGetWalletItems(wallet_items.Pass());
3492 // Only the intersection is available.
3493 EXPECT_TRUE(controller()->IsPayingWithWallet());
3494 shipping_country_model =
3495 controller()->ComboboxModelForAutofillType(ADDRESS_HOME_COUNTRY);
3496 ASSERT_EQ(1, shipping_country_model->GetItemCount());
3497 EXPECT_EQ(shipping_country_model->GetItemAt(0), ASCIIToUTF16("Mexico"));
3499 // Empty intersection; Wallet's automatically disabled.
3500 wallet_items = wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
3501 wallet_items->AddAllowedShippingCountry("CA");
3502 wallet_items->AddAllowedShippingCountry("GB");
3503 controller()->OnDidGetWalletItems(wallet_items.Pass());
3504 EXPECT_FALSE(controller()->IsPayingWithWallet());
3506 // Since it's disabled, we revert to accepting all the countries.
3507 shipping_country_model =
3508 controller()->ComboboxModelForAutofillType(ADDRESS_HOME_COUNTRY);
3509 ASSERT_EQ(2, shipping_country_model->GetItemCount());
3510 EXPECT_EQ(shipping_country_model->GetItemAt(0), ASCIIToUTF16("Iraq"));
3511 EXPECT_EQ(shipping_country_model->GetItemAt(1), ASCIIToUTF16("Mexico"));
3514 // http://crbug.com/388018
3515 TEST_F(AutofillDialogControllerTest, NoCountryChoices) {
3516 // Create a form data that simulates:
3517 // <select autocomplete="billing country">
3518 // <option value="ATL">Atlantis</option>
3519 // <option value="ELD">Eldorado</option>
3520 // </select>
3521 // i.e. contains a list of no valid countries.
3522 FormData form_data;
3523 FormFieldData field;
3524 field.autocomplete_attribute = "billing country";
3525 field.option_contents.push_back(ASCIIToUTF16("Atlantis"));
3526 field.option_values.push_back(ASCIIToUTF16("ATL"));
3527 field.option_contents.push_back(ASCIIToUTF16("Eldorado"));
3528 field.option_values.push_back(ASCIIToUTF16("ELD"));
3530 FormFieldData cc_field;
3531 cc_field.autocomplete_attribute = "cc-csc";
3533 form_data.fields.push_back(field);
3534 form_data.fields.push_back(cc_field);
3535 ResetControllerWithFormData(form_data);
3536 controller()->Show();
3538 // Controller aborts and self destructs.
3539 EXPECT_EQ(0, controller());
3542 TEST_F(AutofillDialogControllerTest, LimitedCcChoices) {
3543 SwitchToAutofill();
3544 // Typically, MC and Visa are both valid.
3545 ValidateCCNumber(SECTION_CC, kTestCCNumberMaster, true);
3546 ValidateCCNumber(SECTION_CC, kTestCCNumberVisa, true);
3548 FormData form_data;
3549 FormFieldData field;
3550 field.autocomplete_attribute = "billing cc-type";
3551 field.option_contents.push_back(ASCIIToUTF16("Visa"));
3552 field.option_values.push_back(ASCIIToUTF16("V"));
3553 field.option_contents.push_back(ASCIIToUTF16("American Express"));
3554 field.option_values.push_back(ASCIIToUTF16("AX"));
3555 form_data.fields.push_back(field);
3556 ResetControllerWithFormData(form_data);
3557 controller()->Show();
3559 // MC is not valid because it's missing from FormData.
3560 EXPECT_EQ(l10n_util::GetStringUTF16(
3561 IDS_AUTOFILL_DIALOG_VALIDATION_UNACCEPTED_MASTERCARD),
3562 ValidateCCNumber(SECTION_CC, kTestCCNumberMaster, false));
3563 ValidateCCNumber(SECTION_CC, kTestCCNumberVisa, true);
3565 CreditCard visa_card(test::GetVerifiedCreditCard());
3566 CreditCard amex_card(test::GetVerifiedCreditCard2());
3568 CreditCard master_card(base::GenerateGUID(), "chrome settings");
3569 test::SetCreditCardInfo(
3570 &master_card, "Mr Foo", "5105105105105100", "07", "2099");
3572 controller()->GetTestingManager()->AddTestingCreditCard(&visa_card);
3573 controller()->GetTestingManager()->AddTestingCreditCard(&amex_card);
3574 controller()->GetTestingManager()->AddTestingCreditCard(&master_card);
3576 // The stored MC is disabled in the dropdown.
3577 ui::MenuModel* model = controller()->MenuModelForSection(SECTION_CC);
3578 ASSERT_TRUE(model);
3579 ASSERT_EQ(5, model->GetItemCount());
3580 EXPECT_TRUE(model->IsEnabledAt(0));
3581 EXPECT_TRUE(model->IsEnabledAt(1));
3582 EXPECT_FALSE(model->IsEnabledAt(2));
3583 EXPECT_TRUE(model->IsEnabledAt(3));
3584 EXPECT_TRUE(model->IsEnabledAt(4));
3586 // No MC; Wallet is disabled.
3587 SetUpControllerWithFormData(form_data);
3588 EXPECT_FALSE(controller()->IsPayingWithWallet());
3590 // In Autofill mode, Discover is disallowed because it's not in FormData.
3591 ValidateCCNumber(SECTION_CC, kTestCCNumberDiscover, false);
3593 field.option_contents.push_back(ASCIIToUTF16("Mastercard"));
3594 field.option_values.push_back(ASCIIToUTF16("Mastercard"));
3595 form_data.fields[0] = field;
3597 // Add MC to FormData; Wallet is enabled.
3598 SetUpControllerWithFormData(form_data);
3599 EXPECT_TRUE(controller()->IsPayingWithWallet());
3600 // Even though Discover isn't in FormData, it's allowed because Wallet always
3601 // generates a MC Virtual card.
3602 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberDiscover, true);
3605 TEST_F(AutofillDialogControllerTest, SuggestCountrylessProfiles) {
3606 SwitchToAutofill();
3608 FieldValueMap outputs;
3609 outputs[ADDRESS_HOME_COUNTRY] = ASCIIToUTF16("US");
3610 controller()->GetView()->SetUserInput(SECTION_SHIPPING, outputs);
3612 AutofillProfile profile(test::GetVerifiedProfile());
3613 profile.SetRawInfo(NAME_FULL, ASCIIToUTF16("The Man Without a Country"));
3614 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, base::string16());
3615 controller()->GetTestingManager()->AddTestingProfile(&profile);
3617 controller()->UserEditedOrActivatedInput(
3618 SECTION_SHIPPING,
3619 NAME_FULL,
3620 gfx::NativeView(),
3621 gfx::Rect(),
3622 profile.GetRawInfo(NAME_FULL).substr(0, 1),
3623 true);
3624 EXPECT_EQ(NAME_FULL, controller()->popup_input_type());
3627 TEST_F(AutofillDialogControllerTest, SwitchFromWalletWithFirstName) {
3628 controller()->MenuModelForSection(SECTION_CC_BILLING)->ActivatedAt(2);
3630 FieldValueMap outputs;
3631 outputs[NAME_FULL] = ASCIIToUTF16("madonna");
3632 controller()->GetView()->SetUserInput(SECTION_CC_BILLING, outputs);
3634 ASSERT_NO_FATAL_FAILURE(SwitchToAutofill());
3637 // Regression test for http://crbug.com/382777
3638 TEST_F(AutofillDialogControllerTest, WalletBillingCountry) {
3639 FormFieldData cc_field;
3640 cc_field.autocomplete_attribute = "cc-number";
3641 FormFieldData billing_country, billing_country_name, shipping_country,
3642 shipping_country_name;
3643 billing_country.autocomplete_attribute = "billing country";
3644 billing_country_name.autocomplete_attribute = "billing country-name";
3645 shipping_country.autocomplete_attribute = "shipping country";
3646 shipping_country_name.autocomplete_attribute = "shipping country-name";
3648 FormData form_data;
3649 form_data.fields.push_back(cc_field);
3650 form_data.fields.push_back(billing_country);
3651 form_data.fields.push_back(billing_country_name);
3652 form_data.fields.push_back(shipping_country);
3653 form_data.fields.push_back(shipping_country_name);
3655 SetUpControllerWithFormData(form_data);
3656 AcceptAndLoadFakeFingerprint();
3657 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
3658 controller()->ForceFinishSubmit();
3660 ASSERT_EQ(5U, form_structure()->field_count());
3661 EXPECT_EQ(ADDRESS_HOME_COUNTRY,
3662 form_structure()->field(1)->Type().GetStorableType());
3663 EXPECT_EQ(ASCIIToUTF16("US"), form_structure()->field(1)->value);
3664 EXPECT_EQ(ADDRESS_HOME_COUNTRY,
3665 form_structure()->field(2)->Type().GetStorableType());
3666 EXPECT_EQ(ASCIIToUTF16("United States"), form_structure()->field(2)->value);
3667 EXPECT_EQ(ADDRESS_HOME_COUNTRY,
3668 form_structure()->field(3)->Type().GetStorableType());
3669 EXPECT_EQ(ASCIIToUTF16("US"), form_structure()->field(3)->value);
3670 EXPECT_EQ(ADDRESS_HOME_COUNTRY,
3671 form_structure()->field(4)->Type().GetStorableType());
3672 EXPECT_EQ(ASCIIToUTF16("United States"), form_structure()->field(4)->value);
3675 } // namespace autofill