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