autofill_dialog_controller_unittest.cc revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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/command_line.h" 9#include "base/guid.h" 10#include "base/memory/scoped_ptr.h" 11#include "base/message_loop/message_loop.h" 12#include "base/prefs/pref_service.h" 13#include "base/run_loop.h" 14#include "base/strings/string_number_conversions.h" 15#include "base/strings/string_piece.h" 16#include "base/strings/utf_string_conversions.h" 17#include "base/tuple.h" 18#include "chrome/browser/ui/autofill/autofill_dialog_controller_impl.h" 19#include "chrome/browser/ui/autofill/autofill_dialog_view.h" 20#include "chrome/browser/ui/autofill/generated_credit_card_bubble_controller.h" 21#include "chrome/browser/ui/autofill/mock_new_credit_card_bubble_controller.h" 22#include "chrome/browser/ui/autofill/test_generated_credit_card_bubble_controller.h" 23#include "chrome/browser/webdata/web_data_service_factory.h" 24#include "chrome/common/pref_names.h" 25#include "chrome/common/render_messages.h" 26#include "chrome/test/base/chrome_render_view_host_test_harness.h" 27#include "chrome/test/base/scoped_testing_local_state.h" 28#include "chrome/test/base/testing_browser_process.h" 29#include "chrome/test/base/testing_profile.h" 30#include "components/autofill/content/browser/risk/proto/fingerprint.pb.h" 31#include "components/autofill/content/browser/wallet/full_wallet.h" 32#include "components/autofill/content/browser/wallet/gaia_account.h" 33#include "components/autofill/content/browser/wallet/instrument.h" 34#include "components/autofill/content/browser/wallet/mock_wallet_client.h" 35#include "components/autofill/content/browser/wallet/wallet_address.h" 36#include "components/autofill/content/browser/wallet/wallet_service_url.h" 37#include "components/autofill/content/browser/wallet/wallet_test_util.h" 38#include "components/autofill/core/browser/autofill_metrics.h" 39#include "components/autofill/core/browser/autofill_test_utils.h" 40#include "components/autofill/core/browser/test_personal_data_manager.h" 41#include "components/autofill/core/browser/webdata/autofill_webdata_service.h" 42#include "components/autofill/core/common/autofill_switches.h" 43#include "components/autofill/core/common/form_data.h" 44#include "components/user_prefs/user_prefs.h" 45#include "content/public/browser/web_contents.h" 46#include "content/public/test/mock_render_process_host.h" 47#include "google_apis/gaia/google_service_auth_error.h" 48#include "testing/gmock/include/gmock/gmock.h" 49#include "testing/gtest/include/gtest/gtest.h" 50 51#if defined(OS_WIN) 52#include "ui/base/win/scoped_ole_initializer.h" 53#endif 54 55namespace autofill { 56 57namespace { 58 59using testing::_; 60 61const char kFakeEmail[] = "user@chromium.org"; 62const char kFakeFingerprintEncoded[] = "CgVaAwiACA=="; 63const char kEditedBillingAddress[] = "123 edited billing address"; 64const char* kFieldsFromPage[] = 65 { "email", 66 "cc-name", 67 "cc-number", 68 "cc-exp-month", 69 "cc-exp-year", 70 "cc-csc", 71 "billing name", 72 "billing address-line1", 73 "billing locality", 74 "billing region", 75 "billing postal-code", 76 "billing country", 77 "billing tel", 78 "shipping name", 79 "shipping address-line1", 80 "shipping locality", 81 "shipping region", 82 "shipping postal-code", 83 "shipping country", 84 "shipping tel", 85 }; 86const char kSettingsOrigin[] = "Chrome settings"; 87const char kTestCCNumberAmex[] = "376200000000002"; 88const char kTestCCNumberVisa[] = "4111111111111111"; 89const char kTestCCNumberMaster[] = "5555555555554444"; 90const char kTestCCNumberDiscover[] = "6011111111111117"; 91const char kTestCCNumberIncomplete[] = "4111111111"; 92// Credit card number fails Luhn check. 93const char kTestCCNumberInvalid[] = "4111111111111112"; 94 95// Copies the initial values from |inputs| into |outputs|. 96void CopyInitialValues(const DetailInputs& inputs, FieldValueMap* outputs) { 97 for (size_t i = 0; i < inputs.size(); ++i) { 98 const DetailInput& input = inputs[i]; 99 (*outputs)[input.type] = input.initial_value; 100 } 101} 102 103scoped_ptr<wallet::WalletItems> CompleteAndValidWalletItems() { 104 scoped_ptr<wallet::WalletItems> items = 105 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED); 106 items->AddAccount(wallet::GetTestGaiaAccount()); 107 items->AddInstrument(wallet::GetTestMaskedInstrument()); 108 items->AddAddress(wallet::GetTestShippingAddress()); 109 return items.Pass(); 110} 111 112scoped_ptr<risk::Fingerprint> GetFakeFingerprint() { 113 scoped_ptr<risk::Fingerprint> fingerprint(new risk::Fingerprint()); 114 // Add some data to the proto, else the encoded content is empty. 115 fingerprint->mutable_machine_characteristics()->mutable_screen_size()-> 116 set_width(1024); 117 return fingerprint.Pass(); 118} 119 120bool HasAnyError(const ValidityMessages& messages, ServerFieldType field) { 121 return !messages.GetMessageOrDefault(field).text.empty(); 122} 123 124bool HasUnsureError(const ValidityMessages& messages, ServerFieldType field) { 125 const ValidityMessage& message = messages.GetMessageOrDefault(field); 126 return !message.text.empty() && !message.sure; 127} 128 129class TestAutofillDialogView : public AutofillDialogView { 130 public: 131 TestAutofillDialogView() 132 : updates_started_(0), save_details_locally_checked_(true) {} 133 virtual ~TestAutofillDialogView() {} 134 135 virtual void Show() OVERRIDE {} 136 virtual void Hide() OVERRIDE {} 137 138 virtual void UpdatesStarted() OVERRIDE { 139 updates_started_++; 140 } 141 142 virtual void UpdatesFinished() OVERRIDE { 143 updates_started_--; 144 EXPECT_GE(updates_started_, 0); 145 } 146 147 virtual void UpdateNotificationArea() OVERRIDE { 148 EXPECT_GE(updates_started_, 1); 149 } 150 151 virtual void UpdateAccountChooser() OVERRIDE { 152 EXPECT_GE(updates_started_, 1); 153 } 154 155 virtual void UpdateButtonStrip() OVERRIDE { 156 EXPECT_GE(updates_started_, 1); 157 } 158 159 virtual void UpdateOverlay() OVERRIDE { 160 EXPECT_GE(updates_started_, 1); 161 } 162 163 virtual void UpdateDetailArea() OVERRIDE { 164 EXPECT_GE(updates_started_, 1); 165 } 166 167 virtual void UpdateSection(DialogSection section) OVERRIDE { 168 EXPECT_GE(updates_started_, 1); 169 } 170 171 virtual void UpdateErrorBubble() OVERRIDE { 172 EXPECT_GE(updates_started_, 1); 173 } 174 175 virtual void FillSection(DialogSection section, 176 const DetailInput& originating_input) OVERRIDE {}; 177 virtual void GetUserInput(DialogSection section, FieldValueMap* output) 178 OVERRIDE { 179 *output = outputs_[section]; 180 } 181 virtual TestableAutofillDialogView* GetTestableView() OVERRIDE { 182 return NULL; 183 } 184 185 virtual base::string16 GetCvc() OVERRIDE { return base::string16(); } 186 virtual bool HitTestInput(const DetailInput& input, 187 const gfx::Point& screen_point) OVERRIDE { 188 return false; 189 } 190 191 virtual bool SaveDetailsLocally() OVERRIDE { 192 return save_details_locally_checked_; 193 } 194 195 virtual const content::NavigationController* ShowSignIn() OVERRIDE { 196 return NULL; 197 } 198 virtual void HideSignIn() OVERRIDE {} 199 200 MOCK_METHOD0(ModelChanged, void()); 201 MOCK_METHOD0(UpdateForErrors, void()); 202 203 virtual void OnSignInResize(const gfx::Size& pref_size) OVERRIDE {} 204 205 void SetUserInput(DialogSection section, const FieldValueMap& map) { 206 outputs_[section] = map; 207 } 208 209 void CheckSaveDetailsLocallyCheckbox(bool checked) { 210 save_details_locally_checked_ = checked; 211 } 212 213 private: 214 std::map<DialogSection, FieldValueMap> outputs_; 215 216 int updates_started_; 217 bool save_details_locally_checked_; 218 219 DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogView); 220}; 221 222class TestAutofillDialogController 223 : public AutofillDialogControllerImpl, 224 public base::SupportsWeakPtr<TestAutofillDialogController> { 225 public: 226 TestAutofillDialogController( 227 content::WebContents* contents, 228 const FormData& form_structure, 229 const GURL& source_url, 230 const AutofillMetrics& metric_logger, 231 const base::Callback<void(const FormStructure*)>& callback, 232 MockNewCreditCardBubbleController* mock_new_card_bubble_controller) 233 : AutofillDialogControllerImpl(contents, 234 form_structure, 235 source_url, 236 callback), 237 metric_logger_(metric_logger), 238 mock_wallet_client_( 239 Profile::FromBrowserContext(contents->GetBrowserContext())-> 240 GetRequestContext(), this, source_url), 241 mock_new_card_bubble_controller_(mock_new_card_bubble_controller), 242 submit_button_delay_count_(0) {} 243 244 virtual ~TestAutofillDialogController() {} 245 246 virtual AutofillDialogView* CreateView() OVERRIDE { 247 return new testing::NiceMock<TestAutofillDialogView>(); 248 } 249 250 void Init(content::BrowserContext* browser_context) { 251 test_manager_.Init( 252 WebDataServiceFactory::GetAutofillWebDataForProfile( 253 Profile::FromBrowserContext(browser_context), 254 Profile::EXPLICIT_ACCESS), 255 user_prefs::UserPrefs::Get(browser_context), 256 browser_context->IsOffTheRecord()); 257 } 258 259 TestAutofillDialogView* GetView() { 260 return static_cast<TestAutofillDialogView*>(view()); 261 } 262 263 TestPersonalDataManager* GetTestingManager() { 264 return &test_manager_; 265 } 266 267 wallet::MockWalletClient* GetTestingWalletClient() { 268 return &mock_wallet_client_; 269 } 270 271 const GURL& open_tab_url() { return open_tab_url_; } 272 273 void SimulateSigninError() { 274 OnWalletSigninError(); 275 } 276 277 // Skips past the 2 second wait between FinishSubmit and DoFinishSubmit. 278 void ForceFinishSubmit() { 279 DoFinishSubmit(); 280 } 281 282 void SimulateSubmitButtonDelayBegin() { 283 AutofillDialogControllerImpl::SubmitButtonDelayBegin(); 284 } 285 286 void SimulateSubmitButtonDelayEnd() { 287 AutofillDialogControllerImpl::SubmitButtonDelayEndForTesting(); 288 } 289 290 using AutofillDialogControllerImpl:: 291 ClearLastWalletItemsFetchTimestampForTesting; 292 293 // Returns the number of times that the submit button was delayed. 294 int get_submit_button_delay_count() const { 295 return submit_button_delay_count_; 296 } 297 298 MOCK_METHOD0(LoadRiskFingerprintData, void()); 299 using AutofillDialogControllerImpl::AccountChooserModelForTesting; 300 using AutofillDialogControllerImpl::OnDidLoadRiskFingerprintData; 301 using AutofillDialogControllerImpl::IsEditingExistingData; 302 using AutofillDialogControllerImpl::IsSubmitPausedOn; 303 using AutofillDialogControllerImpl::NOT_CHECKED; 304 using AutofillDialogControllerImpl::SignedInState; 305 306 protected: 307 virtual PersonalDataManager* GetManager() const OVERRIDE { 308 return const_cast<TestAutofillDialogController*>(this)-> 309 GetTestingManager(); 310 } 311 312 virtual wallet::WalletClient* GetWalletClient() OVERRIDE { 313 return &mock_wallet_client_; 314 } 315 316 virtual void OpenTabWithUrl(const GURL& url) OVERRIDE { 317 open_tab_url_ = url; 318 } 319 320 // Whether the information input in this dialog will be securely transmitted 321 // to the requesting site. 322 virtual bool TransmissionWillBeSecure() const OVERRIDE { 323 return true; 324 } 325 326 virtual void ShowNewCreditCardBubble( 327 scoped_ptr<CreditCard> new_card, 328 scoped_ptr<AutofillProfile> billing_profile) OVERRIDE { 329 mock_new_card_bubble_controller_->Show(new_card.Pass(), 330 billing_profile.Pass()); 331 } 332 333 // AutofillDialogControllerImpl calls this method before showing the dialog 334 // window. 335 virtual void SubmitButtonDelayBegin() OVERRIDE { 336 // Do not delay enabling the submit button in testing. 337 submit_button_delay_count_++; 338 } 339 340 private: 341 // To specify our own metric logger. 342 virtual const AutofillMetrics& GetMetricLogger() const OVERRIDE { 343 return metric_logger_; 344 } 345 346 const AutofillMetrics& metric_logger_; 347 TestPersonalDataManager test_manager_; 348 testing::NiceMock<wallet::MockWalletClient> mock_wallet_client_; 349 GURL open_tab_url_; 350 MockNewCreditCardBubbleController* mock_new_card_bubble_controller_; 351 352 // The number of times that the submit button was delayed. 353 int submit_button_delay_count_; 354 355 DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogController); 356}; 357 358class AutofillDialogControllerTest : public ChromeRenderViewHostTestHarness { 359 protected: 360 AutofillDialogControllerTest(): form_structure_(NULL) {} 361 362 // testing::Test implementation: 363 virtual void SetUp() OVERRIDE { 364 ChromeRenderViewHostTestHarness::SetUp(); 365 Reset(); 366 } 367 368 virtual void TearDown() OVERRIDE { 369 if (controller_) 370 controller_->ViewClosed(); 371 ChromeRenderViewHostTestHarness::TearDown(); 372 } 373 374 void Reset() { 375 if (controller_) 376 controller_->ViewClosed(); 377 378 test_generated_bubble_controller_ = 379 new testing::NiceMock<TestGeneratedCreditCardBubbleController>( 380 web_contents()); 381 ASSERT_TRUE(test_generated_bubble_controller_->IsInstalled()); 382 383 mock_new_card_bubble_controller_.reset( 384 new MockNewCreditCardBubbleController); 385 386 profile()->GetPrefs()->ClearPref(::prefs::kAutofillDialogSaveData); 387 388 // We have to clear the old local state before creating a new one. 389 scoped_local_state_.reset(); 390 scoped_local_state_.reset(new ScopedTestingLocalState( 391 TestingBrowserProcess::GetGlobal())); 392 393 SetUpControllerWithFormData(DefaultFormData()); 394 } 395 396 FormData DefaultFormData() { 397 FormData form_data; 398 for (size_t i = 0; i < arraysize(kFieldsFromPage); ++i) { 399 FormFieldData field; 400 field.autocomplete_attribute = kFieldsFromPage[i]; 401 form_data.fields.push_back(field); 402 } 403 return form_data; 404 } 405 406 // Creates a new controller for |form_data|. 407 void ResetControllerWithFormData(const FormData& form_data) { 408 if (controller_) 409 controller_->ViewClosed(); 410 411 base::Callback<void(const FormStructure*)> callback = 412 base::Bind(&AutofillDialogControllerTest::FinishedCallback, 413 base::Unretained(this)); 414 controller_ = (new testing::NiceMock<TestAutofillDialogController>( 415 web_contents(), 416 form_data, 417 GURL(), 418 metric_logger_, 419 callback, 420 mock_new_card_bubble_controller_.get()))->AsWeakPtr(); 421 controller_->Init(profile()); 422 } 423 424 // Creates a new controller for |form_data| and sets up some initial wallet 425 // data for it. 426 void SetUpControllerWithFormData(const FormData& form_data) { 427 ResetControllerWithFormData(form_data); 428 controller()->Show(); 429 if (!profile()->GetPrefs()->GetBoolean( 430 ::prefs::kAutofillDialogPayWithoutWallet)) { 431 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems()); 432 controller()->OnDidFetchWalletCookieValue(std::string()); 433 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems()); 434 } 435 } 436 437 // Fills the inputs in SECTION_CC with data. 438 void FillCreditCardInputs() { 439 FieldValueMap cc_outputs; 440 const DetailInputs& cc_inputs = 441 controller()->RequestedFieldsForSection(SECTION_CC); 442 for (size_t i = 0; i < cc_inputs.size(); ++i) { 443 cc_outputs[cc_inputs[i].type] = cc_inputs[i].type == CREDIT_CARD_NUMBER ? 444 ASCIIToUTF16(kTestCCNumberVisa) : ASCIIToUTF16("11"); 445 } 446 controller()->GetView()->SetUserInput(SECTION_CC, cc_outputs); 447 } 448 449 // Fills the inputs in SECTION_CC_BILLING with valid data. 450 void FillCCBillingInputs() { 451 FieldValueMap outputs; 452 const DetailInputs& inputs = 453 controller()->RequestedFieldsForSection(SECTION_CC_BILLING); 454 AutofillProfile full_profile(test::GetVerifiedProfile()); 455 CreditCard full_card(test::GetCreditCard()); 456 for (size_t i = 0; i < inputs.size(); ++i) { 457 const ServerFieldType type = inputs[i].type; 458 outputs[type] = full_profile.GetInfo(AutofillType(type), "en-US"); 459 460 if (outputs[type].empty()) 461 outputs[type] = full_card.GetInfo(AutofillType(type), "en-US"); 462 } 463 controller()->GetView()->SetUserInput(SECTION_CC_BILLING, outputs); 464 } 465 466 // Activates the 'Add new foo' option from the |section|'s suggestions 467 // dropdown and fills the |section|'s inputs with the data from the 468 // |data_model|. If |section| is SECTION_CC, also fills in '123' for the CVC. 469 void FillInputs(DialogSection section, const AutofillDataModel& data_model) { 470 // Select the 'Add new foo' option. 471 ui::MenuModel* model = GetMenuModelForSection(section); 472 if (model) 473 model->ActivatedAt(model->GetItemCount() - 2); 474 475 // Fill the inputs. 476 FieldValueMap outputs; 477 const DetailInputs& inputs = 478 controller()->RequestedFieldsForSection(section); 479 for (size_t i = 0; i < inputs.size(); ++i) { 480 ServerFieldType type = inputs[i].type; 481 base::string16 output; 482 if (type == CREDIT_CARD_VERIFICATION_CODE) 483 output = ASCIIToUTF16("123"); 484 else 485 output = data_model.GetInfo(AutofillType(type), "en-US"); 486 outputs[inputs[i].type] = output; 487 } 488 controller()->GetView()->SetUserInput(section, outputs); 489 } 490 491 std::vector<DialogNotification> NotificationsOfType( 492 DialogNotification::Type type) { 493 std::vector<DialogNotification> right_type; 494 const std::vector<DialogNotification>& notifications = 495 controller()->CurrentNotifications(); 496 for (size_t i = 0; i < notifications.size(); ++i) { 497 if (notifications[i].type() == type) 498 right_type.push_back(notifications[i]); 499 } 500 return right_type; 501 } 502 503 void SwitchToAutofill() { 504 ui::MenuModel* model = controller_->MenuModelForAccountChooser(); 505 model->ActivatedAt(model->GetItemCount() - 1); 506 } 507 508 void SwitchToWallet() { 509 controller_->MenuModelForAccountChooser()->ActivatedAt(0); 510 } 511 512 void SimulateSigninError() { 513 controller_->SimulateSigninError(); 514 } 515 516 void UseBillingForShipping() { 517 controller()->MenuModelForSection(SECTION_SHIPPING)->ActivatedAt(0); 518 } 519 520 void ValidateCCNumber(DialogSection section, 521 const std::string& cc_number, 522 bool should_pass) { 523 FieldValueMap outputs; 524 outputs[CREDIT_CARD_NUMBER] = UTF8ToUTF16(cc_number); 525 ValidityMessages messages = 526 controller()->InputsAreValid(section, outputs); 527 EXPECT_EQ(should_pass, !messages.HasSureError(CREDIT_CARD_NUMBER)); 528 } 529 530 void SubmitWithWalletItems(scoped_ptr<wallet::WalletItems> wallet_items) { 531 controller()->OnDidGetWalletItems(wallet_items.Pass()); 532 AcceptAndLoadFakeFingerprint(); 533 } 534 535 void AcceptAndLoadFakeFingerprint() { 536 controller()->OnAccept(); 537 controller()->OnDidLoadRiskFingerprintData(GetFakeFingerprint().Pass()); 538 } 539 540 // Returns true if the given |section| contains a field of the given |type|. 541 bool SectionContainsField(DialogSection section, ServerFieldType type) { 542 const DetailInputs& inputs = 543 controller()->RequestedFieldsForSection(section); 544 for (DetailInputs::const_iterator it = inputs.begin(); it != inputs.end(); 545 ++it) { 546 if (it->type == type) 547 return true; 548 } 549 return false; 550 } 551 552 SuggestionsMenuModel* GetMenuModelForSection(DialogSection section) { 553 ui::MenuModel* model = controller()->MenuModelForSection(section); 554 return static_cast<SuggestionsMenuModel*>(model); 555 } 556 557 TestAutofillDialogController* controller() { return controller_.get(); } 558 559 const FormStructure* form_structure() { return form_structure_; } 560 561 TestGeneratedCreditCardBubbleController* test_generated_bubble_controller() { 562 return test_generated_bubble_controller_; 563 } 564 565 const MockNewCreditCardBubbleController* mock_new_card_bubble_controller() { 566 return mock_new_card_bubble_controller_.get(); 567 } 568 569 private: 570 void FinishedCallback(const FormStructure* form_structure) { 571 form_structure_ = form_structure; 572 } 573 574#if defined(OS_WIN) 575 // http://crbug.com/227221 576 ui::ScopedOleInitializer ole_initializer_; 577#endif 578 579 // The controller owns itself. 580 base::WeakPtr<TestAutofillDialogController> controller_; 581 582 // Must outlive the controller. 583 AutofillMetrics metric_logger_; 584 585 // Returned when the dialog closes successfully. 586 const FormStructure* form_structure_; 587 588 // Used to monitor if the Autofill credit card bubble is shown. Owned by 589 // |web_contents()|. 590 TestGeneratedCreditCardBubbleController* test_generated_bubble_controller_; 591 592 // Used to record when new card bubbles would show. Created in |Reset()|. 593 scoped_ptr<MockNewCreditCardBubbleController> 594 mock_new_card_bubble_controller_; 595 596 scoped_ptr<ScopedTestingLocalState> scoped_local_state_; 597}; 598 599} // namespace 600 601// Ensure the default ValidityMessage has the expected values. 602TEST_F(AutofillDialogControllerTest, DefaultValidityMessage) { 603 ValidityMessages messages; 604 ValidityMessage message = messages.GetMessageOrDefault(UNKNOWN_TYPE); 605 EXPECT_FALSE(message.sure); 606 EXPECT_TRUE(message.text.empty()); 607} 608 609// This test makes sure nothing falls over when fields are being validity- 610// checked. 611TEST_F(AutofillDialogControllerTest, ValidityCheck) { 612 for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) { 613 DialogSection section = static_cast<DialogSection>(i); 614 const DetailInputs& shipping_inputs = 615 controller()->RequestedFieldsForSection(section); 616 for (DetailInputs::const_iterator iter = shipping_inputs.begin(); 617 iter != shipping_inputs.end(); ++iter) { 618 controller()->InputValidityMessage(section, iter->type, base::string16()); 619 } 620 } 621} 622 623// Test for phone number validation. 624TEST_F(AutofillDialogControllerTest, PhoneNumberValidation) { 625 // Construct FieldValueMap from existing data. 626 SwitchToAutofill(); 627 628 for (size_t i = 0; i < 2; ++i) { 629 ServerFieldType phone = i == 0 ? PHONE_HOME_WHOLE_NUMBER : 630 PHONE_BILLING_WHOLE_NUMBER; 631 ServerFieldType address = i == 0 ? ADDRESS_HOME_COUNTRY : 632 ADDRESS_BILLING_COUNTRY; 633 DialogSection section = i == 0 ? SECTION_SHIPPING : SECTION_BILLING; 634 635 FieldValueMap outputs; 636 const DetailInputs& inputs = 637 controller()->RequestedFieldsForSection(section); 638 AutofillProfile full_profile(test::GetVerifiedProfile()); 639 for (size_t i = 0; i < inputs.size(); ++i) { 640 const ServerFieldType type = inputs[i].type; 641 outputs[type] = full_profile.GetInfo(AutofillType(type), "en-US"); 642 } 643 644 // Make sure country is United States. 645 outputs[address] = ASCIIToUTF16("United States"); 646 647 // Existing data should have no errors. 648 ValidityMessages messages = controller()->InputsAreValid(section, outputs); 649 EXPECT_FALSE(HasAnyError(messages, phone)); 650 651 // Input an empty phone number. 652 outputs[phone] = base::string16(); 653 messages = controller()->InputsAreValid(section, outputs); 654 EXPECT_TRUE(HasUnsureError(messages, phone)); 655 656 // Input an invalid phone number. 657 outputs[phone] = ASCIIToUTF16("ABC"); 658 messages = controller()->InputsAreValid(section, outputs); 659 EXPECT_TRUE(messages.HasSureError(phone)); 660 661 // Input a local phone number. 662 outputs[phone] = ASCIIToUTF16("2155546699"); 663 messages = controller()->InputsAreValid(section, outputs); 664 EXPECT_FALSE(HasAnyError(messages, phone)); 665 666 // Input an invalid local phone number. 667 outputs[phone] = ASCIIToUTF16("215554669"); 668 messages = controller()->InputsAreValid(section, outputs); 669 EXPECT_TRUE(messages.HasSureError(phone)); 670 671 // Input an international phone number. 672 outputs[phone] = ASCIIToUTF16("+33 892 70 12 39"); 673 messages = controller()->InputsAreValid(section, outputs); 674 EXPECT_FALSE(HasAnyError(messages, phone)); 675 676 // Input an invalid international phone number. 677 outputs[phone] = ASCIIToUTF16("+112333 892 70 12 39"); 678 messages = controller()->InputsAreValid(section, outputs); 679 EXPECT_TRUE(messages.HasSureError(phone)); 680 } 681} 682 683TEST_F(AutofillDialogControllerTest, ExpirationDateValidity) { 684 ui::ComboboxModel* exp_year_model = 685 controller()->ComboboxModelForAutofillType(CREDIT_CARD_EXP_4_DIGIT_YEAR); 686 ui::ComboboxModel* exp_month_model = 687 controller()->ComboboxModelForAutofillType(CREDIT_CARD_EXP_MONTH); 688 689 base::string16 default_year_value = 690 exp_year_model->GetItemAt(exp_year_model->GetDefaultIndex()); 691 base::string16 default_month_value = 692 exp_month_model->GetItemAt(exp_month_model->GetDefaultIndex()); 693 694 base::string16 other_year_value = 695 exp_year_model->GetItemAt(exp_year_model->GetItemCount() - 1); 696 base::string16 other_month_value = 697 exp_month_model->GetItemAt(exp_month_model->GetItemCount() - 1); 698 699 FieldValueMap outputs; 700 outputs[CREDIT_CARD_EXP_MONTH] = default_month_value; 701 outputs[CREDIT_CARD_EXP_4_DIGIT_YEAR] = default_year_value; 702 703 // Expiration default values generate unsure validation errors (but not sure). 704 ValidityMessages messages = controller()->InputsAreValid(SECTION_CC_BILLING, 705 outputs); 706 EXPECT_TRUE(HasUnsureError(messages, CREDIT_CARD_EXP_4_DIGIT_YEAR)); 707 EXPECT_TRUE(HasUnsureError(messages, CREDIT_CARD_EXP_MONTH)); 708 709 // Expiration date with default month fails. 710 outputs[CREDIT_CARD_EXP_4_DIGIT_YEAR] = other_year_value; 711 messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs); 712 EXPECT_FALSE(HasUnsureError(messages, CREDIT_CARD_EXP_4_DIGIT_YEAR)); 713 EXPECT_TRUE(HasUnsureError(messages, CREDIT_CARD_EXP_MONTH)); 714 715 // Expiration date with default year fails. 716 outputs[CREDIT_CARD_EXP_MONTH] = other_month_value; 717 outputs[CREDIT_CARD_EXP_4_DIGIT_YEAR] = default_year_value; 718 messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs); 719 EXPECT_TRUE(HasUnsureError(messages, CREDIT_CARD_EXP_4_DIGIT_YEAR)); 720 EXPECT_FALSE(HasUnsureError(messages, CREDIT_CARD_EXP_MONTH)); 721} 722 723TEST_F(AutofillDialogControllerTest, BillingNameValidation) { 724 // Construct FieldValueMap from AutofillProfile data. 725 SwitchToAutofill(); 726 727 // Input an empty billing name. 728 FieldValueMap outputs; 729 outputs[NAME_BILLING_FULL] = base::string16(); 730 ValidityMessages messages = controller()->InputsAreValid(SECTION_BILLING, 731 outputs); 732 EXPECT_TRUE(HasUnsureError(messages, NAME_BILLING_FULL)); 733 734 // Input a non-empty billing name. 735 outputs[NAME_BILLING_FULL] = ASCIIToUTF16("Bob"); 736 messages = controller()->InputsAreValid(SECTION_BILLING, outputs); 737 EXPECT_FALSE(HasAnyError(messages, NAME_BILLING_FULL)); 738 739 // Switch to Wallet which only considers names with with at least two names to 740 // be valid. 741 SwitchToWallet(); 742 743 // Setup some wallet state. 744 scoped_ptr<wallet::WalletItems> wallet_items = 745 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED); 746 controller()->OnDidGetWalletItems(wallet_items.Pass()); 747 748 // Input an empty billing name. Data source should not change this behavior. 749 FieldValueMap wallet_outputs; 750 wallet_outputs[NAME_BILLING_FULL] = base::string16(); 751 messages = controller()->InputsAreValid(SECTION_CC_BILLING, wallet_outputs); 752 EXPECT_TRUE(HasUnsureError(messages, NAME_BILLING_FULL)); 753 754 // Input a one name billing name. Wallet does not currently support this. 755 wallet_outputs[NAME_BILLING_FULL] = ASCIIToUTF16("Bob"); 756 messages = controller()->InputsAreValid(SECTION_CC_BILLING, wallet_outputs); 757 EXPECT_TRUE(messages.HasSureError(NAME_BILLING_FULL)); 758 759 // Input a two name billing name. 760 wallet_outputs[NAME_BILLING_FULL] = ASCIIToUTF16("Bob Barker"); 761 messages = controller()->InputsAreValid(SECTION_CC_BILLING, wallet_outputs); 762 EXPECT_FALSE(HasAnyError(messages, NAME_BILLING_FULL)); 763 764 // Input a more than two name billing name. 765 wallet_outputs[NAME_BILLING_FULL] = 766 ASCIIToUTF16("John Jacob Jingleheimer Schmidt"), 767 messages = controller()->InputsAreValid(SECTION_CC_BILLING, wallet_outputs); 768 EXPECT_FALSE(HasAnyError(messages, NAME_BILLING_FULL)); 769 770 // Input a billing name with lots of crazy whitespace. 771 wallet_outputs[NAME_BILLING_FULL] = 772 ASCIIToUTF16(" \\n\\r John \\n Jacob Jingleheimer \\t Schmidt "), 773 messages = controller()->InputsAreValid(SECTION_CC_BILLING, wallet_outputs); 774 EXPECT_FALSE(HasAnyError(messages, NAME_BILLING_FULL)); 775} 776 777TEST_F(AutofillDialogControllerTest, CreditCardNumberValidation) { 778 // Construct FieldValueMap from AutofillProfile data. 779 SwitchToAutofill(); 780 781 // Should accept AMEX, Visa, Master and Discover. 782 ValidateCCNumber(SECTION_CC, kTestCCNumberVisa, true); 783 ValidateCCNumber(SECTION_CC, kTestCCNumberMaster, true); 784 ValidateCCNumber(SECTION_CC, kTestCCNumberDiscover, true); 785 ValidateCCNumber(SECTION_CC, kTestCCNumberAmex, true); 786 ValidateCCNumber(SECTION_CC, kTestCCNumberIncomplete, false); 787 ValidateCCNumber(SECTION_CC, kTestCCNumberInvalid, false); 788 789 // Switch to Wallet which will not accept AMEX. 790 SwitchToWallet(); 791 792 // Setup some wallet state on a merchant for which Wallet doesn't 793 // support AMEX. 794 controller()->OnDidGetWalletItems( 795 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED)); 796 797 // Should accept Visa, Master and Discover, but not AMEX. 798 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberVisa, true); 799 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberMaster, true); 800 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberDiscover, true); 801 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberAmex, false); 802 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberIncomplete, false); 803 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberInvalid, false); 804 805 // Setup some wallet state on a merchant for which Wallet supports AMEX. 806 controller()->OnDidGetWalletItems( 807 wallet::GetTestWalletItems(wallet::AMEX_ALLOWED)); 808 809 // Should accept Visa, Master, Discover, and AMEX. 810 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberVisa, true); 811 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberMaster, true); 812 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberDiscover, true); 813 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberAmex, true); 814 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberIncomplete, false); 815 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberInvalid, false); 816} 817 818TEST_F(AutofillDialogControllerTest, AutofillProfiles) { 819 SwitchToAutofill(); 820 ui::MenuModel* shipping_model = 821 controller()->MenuModelForSection(SECTION_SHIPPING); 822 // Since the PersonalDataManager is empty, this should only have the 823 // "use billing", "add new" and "manage" menu items. 824 ASSERT_TRUE(shipping_model); 825 EXPECT_EQ(3, shipping_model->GetItemCount()); 826 // On the other hand, the other models should be NULL when there's no 827 // suggestion. 828 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC)); 829 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_BILLING)); 830 831 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(3); 832 833 // Empty profiles are ignored. 834 AutofillProfile empty_profile(base::GenerateGUID(), kSettingsOrigin); 835 empty_profile.SetRawInfo(NAME_FULL, ASCIIToUTF16("John Doe")); 836 controller()->GetTestingManager()->AddTestingProfile(&empty_profile); 837 shipping_model = controller()->MenuModelForSection(SECTION_SHIPPING); 838 ASSERT_TRUE(shipping_model); 839 EXPECT_EQ(3, shipping_model->GetItemCount()); 840 841 // An otherwise full but unverified profile should be ignored. 842 AutofillProfile full_profile(test::GetFullProfile()); 843 full_profile.set_origin("https://www.example.com"); 844 full_profile.SetRawInfo(ADDRESS_HOME_LINE2, base::string16()); 845 controller()->GetTestingManager()->AddTestingProfile(&full_profile); 846 shipping_model = controller()->MenuModelForSection(SECTION_SHIPPING); 847 ASSERT_TRUE(shipping_model); 848 EXPECT_EQ(3, shipping_model->GetItemCount()); 849 850 // A full, verified profile should be picked up. 851 AutofillProfile verified_profile(test::GetVerifiedProfile()); 852 verified_profile.SetRawInfo(ADDRESS_HOME_LINE2, base::string16()); 853 controller()->GetTestingManager()->AddTestingProfile(&verified_profile); 854 shipping_model = controller()->MenuModelForSection(SECTION_SHIPPING); 855 ASSERT_TRUE(shipping_model); 856 EXPECT_EQ(4, shipping_model->GetItemCount()); 857} 858 859// Makes sure that the choice of which Autofill profile to use for each section 860// is sticky. 861TEST_F(AutofillDialogControllerTest, AutofillProfileDefaults) { 862 SwitchToAutofill(); 863 AutofillProfile profile(test::GetVerifiedProfile()); 864 AutofillProfile profile2(test::GetVerifiedProfile2()); 865 controller()->GetTestingManager()->AddTestingProfile(&profile); 866 controller()->GetTestingManager()->AddTestingProfile(&profile2); 867 868 // Until a selection has been made, the default shipping suggestion is the 869 // first one (after "use billing"). 870 SuggestionsMenuModel* shipping_model = 871 GetMenuModelForSection(SECTION_SHIPPING); 872 EXPECT_EQ(1, shipping_model->checked_item()); 873 874 for (int i = 2; i >= 0; --i) { 875 shipping_model = GetMenuModelForSection(SECTION_SHIPPING); 876 shipping_model->ExecuteCommand(i, 0); 877 FillCreditCardInputs(); 878 controller()->OnAccept(); 879 880 Reset(); 881 controller()->GetTestingManager()->AddTestingProfile(&profile); 882 controller()->GetTestingManager()->AddTestingProfile(&profile2); 883 shipping_model = GetMenuModelForSection(SECTION_SHIPPING); 884 EXPECT_EQ(i, shipping_model->checked_item()); 885 } 886 887 // Try again, but don't add the default profile to the PDM. The dialog 888 // should fall back to the first profile. 889 shipping_model->ExecuteCommand(2, 0); 890 FillCreditCardInputs(); 891 controller()->OnAccept(); 892 Reset(); 893 controller()->GetTestingManager()->AddTestingProfile(&profile); 894 shipping_model = GetMenuModelForSection(SECTION_SHIPPING); 895 EXPECT_EQ(1, shipping_model->checked_item()); 896} 897 898// Makes sure that a newly added Autofill profile becomes set as the default 899// choice for the next run. 900TEST_F(AutofillDialogControllerTest, NewAutofillProfileIsDefault) { 901 SwitchToAutofill(); 902 903 AutofillProfile profile(test::GetVerifiedProfile()); 904 CreditCard credit_card(test::GetVerifiedCreditCard()); 905 controller()->GetTestingManager()->AddTestingProfile(&profile); 906 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card); 907 908 // Until a selection has been made, the default suggestion is the first one. 909 // For the shipping section, this follows the "use billing" suggestion. 910 EXPECT_EQ(0, GetMenuModelForSection(SECTION_CC)->checked_item()); 911 EXPECT_EQ(1, GetMenuModelForSection(SECTION_SHIPPING)->checked_item()); 912 913 // Fill in the shipping and credit card sections with new data. 914 AutofillProfile new_profile(test::GetVerifiedProfile2()); 915 CreditCard new_credit_card(test::GetVerifiedCreditCard2()); 916 FillInputs(SECTION_SHIPPING, new_profile); 917 FillInputs(SECTION_CC, new_credit_card); 918 controller()->GetView()->CheckSaveDetailsLocallyCheckbox(true); 919 controller()->OnAccept(); 920 921 // Update the |new_profile| and |new_credit_card|'s guids to the saved ones. 922 new_profile.set_guid( 923 controller()->GetTestingManager()->imported_profile().guid()); 924 new_credit_card.set_guid( 925 controller()->GetTestingManager()->imported_credit_card().guid()); 926 927 // Reload the dialog. The newly added address and credit card should now be 928 // set as the defaults. 929 Reset(); 930 controller()->GetTestingManager()->AddTestingProfile(&profile); 931 controller()->GetTestingManager()->AddTestingProfile(&new_profile); 932 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card); 933 controller()->GetTestingManager()->AddTestingCreditCard(&new_credit_card); 934 935 // Until a selection has been made, the default suggestion is the first one. 936 // For the shipping section, this follows the "use billing" suggestion. 937 EXPECT_EQ(1, GetMenuModelForSection(SECTION_CC)->checked_item()); 938 EXPECT_EQ(2, GetMenuModelForSection(SECTION_SHIPPING)->checked_item()); 939} 940 941TEST_F(AutofillDialogControllerTest, AutofillProfileVariants) { 942 SwitchToAutofill(); 943 EXPECT_CALL(*controller()->GetView(), ModelChanged()); 944 ui::MenuModel* shipping_model = 945 controller()->MenuModelForSection(SECTION_SHIPPING); 946 ASSERT_TRUE(!!shipping_model); 947 EXPECT_EQ(3, shipping_model->GetItemCount()); 948 949 // Set up some variant data. 950 AutofillProfile full_profile(test::GetVerifiedProfile()); 951 std::vector<string16> names; 952 names.push_back(ASCIIToUTF16("John Doe")); 953 names.push_back(ASCIIToUTF16("Jane Doe")); 954 full_profile.SetRawMultiInfo(NAME_FULL, names); 955 std::vector<string16> emails; 956 emails.push_back(ASCIIToUTF16(kFakeEmail)); 957 emails.push_back(ASCIIToUTF16("admin@example.com")); 958 full_profile.SetRawMultiInfo(EMAIL_ADDRESS, emails); 959 960 // Non-default variants are ignored by the dialog. 961 controller()->GetTestingManager()->AddTestingProfile(&full_profile); 962 EXPECT_EQ(4, shipping_model->GetItemCount()); 963} 964 965TEST_F(AutofillDialogControllerTest, SuggestValidEmail) { 966 SwitchToAutofill(); 967 AutofillProfile profile(test::GetVerifiedProfile()); 968 const base::string16 kValidEmail = ASCIIToUTF16(kFakeEmail); 969 profile.SetRawInfo(EMAIL_ADDRESS, kValidEmail); 970 controller()->GetTestingManager()->AddTestingProfile(&profile); 971 972 // "add", "manage", and 1 suggestion. 973 EXPECT_EQ( 974 3, controller()->MenuModelForSection(SECTION_BILLING)->GetItemCount()); 975 // "add", "manage", 1 suggestion, and "same as billing". 976 EXPECT_EQ( 977 4, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount()); 978} 979 980TEST_F(AutofillDialogControllerTest, DoNotSuggestInvalidEmail) { 981 SwitchToAutofill(); 982 AutofillProfile profile(test::GetVerifiedProfile()); 983 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16(".!#$%&'*+/=?^_`-@-..")); 984 controller()->GetTestingManager()->AddTestingProfile(&profile); 985 986 EXPECT_FALSE(!!controller()->MenuModelForSection(SECTION_BILLING)); 987 // "add", "manage", 1 suggestion, and "same as billing". 988 EXPECT_EQ( 989 4, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount()); 990} 991 992TEST_F(AutofillDialogControllerTest, SuggestValidAddress) { 993 SwitchToAutofill(); 994 AutofillProfile full_profile(test::GetVerifiedProfile()); 995 full_profile.set_origin(kSettingsOrigin); 996 controller()->GetTestingManager()->AddTestingProfile(&full_profile); 997 // "add", "manage", and 1 suggestion. 998 EXPECT_EQ( 999 3, controller()->MenuModelForSection(SECTION_BILLING)->GetItemCount()); 1000} 1001 1002TEST_F(AutofillDialogControllerTest, DoNotSuggestInvalidAddress) { 1003 SwitchToAutofill(); 1004 AutofillProfile full_profile(test::GetVerifiedProfile()); 1005 full_profile.set_origin(kSettingsOrigin); 1006 full_profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("C")); 1007 controller()->GetTestingManager()->AddTestingProfile(&full_profile); 1008 1009 EXPECT_FALSE(!!controller()->MenuModelForSection(SECTION_BILLING)); 1010} 1011 1012TEST_F(AutofillDialogControllerTest, DoNotSuggestIncompleteAddress) { 1013 SwitchToAutofill(); 1014 AutofillProfile profile(test::GetVerifiedProfile()); 1015 profile.SetRawInfo(ADDRESS_HOME_STATE, base::string16()); 1016 controller()->GetTestingManager()->AddTestingProfile(&profile); 1017 1018 EXPECT_FALSE(!!controller()->MenuModelForSection(SECTION_BILLING)); 1019} 1020 1021TEST_F(AutofillDialogControllerTest, AutofillCreditCards) { 1022 SwitchToAutofill(); 1023 // Since the PersonalDataManager is empty, this should only have the 1024 // default menu items. 1025 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC)); 1026 1027 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(3); 1028 1029 // Empty cards are ignored. 1030 CreditCard empty_card(base::GenerateGUID(), kSettingsOrigin); 1031 empty_card.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("John Doe")); 1032 controller()->GetTestingManager()->AddTestingCreditCard(&empty_card); 1033 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC)); 1034 1035 // An otherwise full but unverified card should be ignored. 1036 CreditCard full_card(test::GetCreditCard()); 1037 full_card.set_origin("https://www.example.com"); 1038 controller()->GetTestingManager()->AddTestingCreditCard(&full_card); 1039 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC)); 1040 1041 // A full, verified card should be picked up. 1042 CreditCard verified_card(test::GetCreditCard()); 1043 verified_card.set_origin(kSettingsOrigin); 1044 controller()->GetTestingManager()->AddTestingCreditCard(&verified_card); 1045 ui::MenuModel* credit_card_model = 1046 controller()->MenuModelForSection(SECTION_CC); 1047 ASSERT_TRUE(credit_card_model); 1048 EXPECT_EQ(3, credit_card_model->GetItemCount()); 1049} 1050 1051// Test selecting a shipping address different from billing as address. 1052TEST_F(AutofillDialogControllerTest, DontUseBillingAsShipping) { 1053 SwitchToAutofill(); 1054 AutofillProfile full_profile(test::GetVerifiedProfile()); 1055 AutofillProfile full_profile2(test::GetVerifiedProfile2()); 1056 CreditCard credit_card(test::GetVerifiedCreditCard()); 1057 controller()->GetTestingManager()->AddTestingProfile(&full_profile); 1058 controller()->GetTestingManager()->AddTestingProfile(&full_profile2); 1059 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card); 1060 ui::MenuModel* shipping_model = 1061 controller()->MenuModelForSection(SECTION_SHIPPING); 1062 shipping_model->ActivatedAt(2); 1063 1064 controller()->OnAccept(); 1065 ASSERT_EQ(20U, form_structure()->field_count()); 1066 EXPECT_EQ(ADDRESS_HOME_STATE, 1067 form_structure()->field(9)->Type().GetStorableType()); 1068 EXPECT_EQ(ADDRESS_BILLING, form_structure()->field(9)->Type().group()); 1069 EXPECT_EQ(ADDRESS_HOME_STATE, 1070 form_structure()->field(16)->Type().GetStorableType()); 1071 EXPECT_EQ(ADDRESS_HOME, form_structure()->field(16)->Type().group()); 1072 base::string16 billing_state = form_structure()->field(9)->value; 1073 base::string16 shipping_state = form_structure()->field(16)->value; 1074 EXPECT_FALSE(billing_state.empty()); 1075 EXPECT_FALSE(shipping_state.empty()); 1076 EXPECT_NE(billing_state, shipping_state); 1077 1078 EXPECT_EQ(CREDIT_CARD_NAME, 1079 form_structure()->field(1)->Type().GetStorableType()); 1080 base::string16 cc_name = form_structure()->field(1)->value; 1081 EXPECT_EQ(NAME_FULL, form_structure()->field(6)->Type().GetStorableType()); 1082 EXPECT_EQ(NAME_BILLING, form_structure()->field(6)->Type().group()); 1083 base::string16 billing_name = form_structure()->field(6)->value; 1084 EXPECT_EQ(NAME_FULL, form_structure()->field(13)->Type().GetStorableType()); 1085 EXPECT_EQ(NAME, form_structure()->field(13)->Type().group()); 1086 base::string16 shipping_name = form_structure()->field(13)->value; 1087 1088 EXPECT_FALSE(cc_name.empty()); 1089 EXPECT_FALSE(billing_name.empty()); 1090 EXPECT_FALSE(shipping_name.empty()); 1091 // Billing name should always be the same as cardholder name. 1092 EXPECT_EQ(cc_name, billing_name); 1093 EXPECT_NE(cc_name, shipping_name); 1094} 1095 1096// Test selecting UseBillingForShipping. 1097TEST_F(AutofillDialogControllerTest, UseBillingAsShipping) { 1098 SwitchToAutofill(); 1099 AutofillProfile full_profile(test::GetVerifiedProfile()); 1100 AutofillProfile full_profile2(test::GetVerifiedProfile2()); 1101 CreditCard credit_card(test::GetVerifiedCreditCard()); 1102 controller()->GetTestingManager()->AddTestingProfile(&full_profile); 1103 controller()->GetTestingManager()->AddTestingProfile(&full_profile2); 1104 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card); 1105 1106 // Test after setting use billing for shipping. 1107 UseBillingForShipping(); 1108 1109 controller()->OnAccept(); 1110 ASSERT_EQ(20U, form_structure()->field_count()); 1111 EXPECT_EQ(ADDRESS_HOME_STATE, 1112 form_structure()->field(9)->Type().GetStorableType()); 1113 EXPECT_EQ(ADDRESS_BILLING, form_structure()->field(9)->Type().group()); 1114 EXPECT_EQ(ADDRESS_HOME_STATE, 1115 form_structure()->field(16)->Type().GetStorableType()); 1116 EXPECT_EQ(ADDRESS_HOME, form_structure()->field(16)->Type().group()); 1117 base::string16 billing_state = form_structure()->field(9)->value; 1118 base::string16 shipping_state = form_structure()->field(16)->value; 1119 EXPECT_FALSE(billing_state.empty()); 1120 EXPECT_FALSE(shipping_state.empty()); 1121 EXPECT_EQ(billing_state, shipping_state); 1122 1123 EXPECT_EQ(CREDIT_CARD_NAME, 1124 form_structure()->field(1)->Type().GetStorableType()); 1125 base::string16 cc_name = form_structure()->field(1)->value; 1126 EXPECT_EQ(NAME_FULL, form_structure()->field(6)->Type().GetStorableType()); 1127 EXPECT_EQ(NAME_BILLING, form_structure()->field(6)->Type().group()); 1128 base::string16 billing_name = form_structure()->field(6)->value; 1129 EXPECT_EQ(NAME_FULL, form_structure()->field(13)->Type().GetStorableType()); 1130 EXPECT_EQ(NAME, form_structure()->field(13)->Type().group()); 1131 base::string16 shipping_name = form_structure()->field(13)->value; 1132 1133 EXPECT_FALSE(cc_name.empty()); 1134 EXPECT_FALSE(billing_name.empty()); 1135 EXPECT_FALSE(shipping_name.empty()); 1136 EXPECT_EQ(cc_name, billing_name); 1137 EXPECT_EQ(cc_name, shipping_name); 1138} 1139 1140// Tests that shipping and billing telephone fields are supported, and filled 1141// in by their respective profiles. http://crbug.com/244515 1142TEST_F(AutofillDialogControllerTest, BillingVsShippingPhoneNumber) { 1143 FormFieldData shipping_tel; 1144 shipping_tel.autocomplete_attribute = "shipping tel"; 1145 FormFieldData billing_tel; 1146 billing_tel.autocomplete_attribute = "billing tel"; 1147 1148 FormData form_data; 1149 form_data.fields.push_back(shipping_tel); 1150 form_data.fields.push_back(billing_tel); 1151 SetUpControllerWithFormData(form_data); 1152 1153 SwitchToAutofill(); 1154 1155 // The profile that will be chosen for the shipping section. 1156 AutofillProfile shipping_profile(test::GetVerifiedProfile()); 1157 // The profile that will be chosen for the billing section. 1158 AutofillProfile billing_profile(test::GetVerifiedProfile2()); 1159 CreditCard credit_card(test::GetVerifiedCreditCard()); 1160 controller()->GetTestingManager()->AddTestingProfile(&shipping_profile); 1161 controller()->GetTestingManager()->AddTestingProfile(&billing_profile); 1162 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card); 1163 ui::MenuModel* billing_model = 1164 controller()->MenuModelForSection(SECTION_BILLING); 1165 billing_model->ActivatedAt(1); 1166 1167 controller()->OnAccept(); 1168 ASSERT_EQ(2U, form_structure()->field_count()); 1169 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER, 1170 form_structure()->field(0)->Type().GetStorableType()); 1171 EXPECT_EQ(PHONE_HOME, form_structure()->field(0)->Type().group()); 1172 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER, 1173 form_structure()->field(1)->Type().GetStorableType()); 1174 EXPECT_EQ(PHONE_BILLING, form_structure()->field(1)->Type().group()); 1175 EXPECT_EQ(shipping_profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER), 1176 form_structure()->field(0)->value); 1177 EXPECT_EQ(billing_profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER), 1178 form_structure()->field(1)->value); 1179 EXPECT_NE(form_structure()->field(1)->value, 1180 form_structure()->field(0)->value); 1181} 1182 1183TEST_F(AutofillDialogControllerTest, AcceptLegalDocuments) { 1184 for (size_t i = 0; i < 2; ++i) { 1185 SCOPED_TRACE(testing::Message() << "Case " << i); 1186 1187 EXPECT_CALL(*controller()->GetTestingWalletClient(), 1188 AcceptLegalDocuments(_, _)); 1189 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_)); 1190 EXPECT_CALL(*controller(), LoadRiskFingerprintData()); 1191 1192 EXPECT_TRUE(controller()->LegalDocumentLinks().empty()); 1193 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems()); 1194 EXPECT_TRUE(controller()->LegalDocumentLinks().empty()); 1195 1196 scoped_ptr<wallet::WalletItems> wallet_items = 1197 CompleteAndValidWalletItems(); 1198 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument()); 1199 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument()); 1200 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1201 EXPECT_FALSE(controller()->LegalDocumentLinks().empty()); 1202 1203 controller()->OnAccept(); 1204 controller()->OnDidAcceptLegalDocuments(); 1205 controller()->OnDidLoadRiskFingerprintData(GetFakeFingerprint().Pass()); 1206 1207 // Now try it all over again with the location disclosure already accepted. 1208 // Nothing should change. 1209 Reset(); 1210 ListValue preexisting_list; 1211 preexisting_list.AppendString(kFakeEmail); 1212 g_browser_process->local_state()->Set( 1213 ::prefs::kAutofillDialogWalletLocationAcceptance, 1214 preexisting_list); 1215 } 1216} 1217 1218TEST_F(AutofillDialogControllerTest, RejectLegalDocuments) { 1219 for (size_t i = 0; i < 2; ++i) { 1220 SCOPED_TRACE(testing::Message() << "Case " << i); 1221 1222 EXPECT_CALL(*controller()->GetTestingWalletClient(), 1223 AcceptLegalDocuments(_, _)).Times(0); 1224 1225 scoped_ptr<wallet::WalletItems> wallet_items = 1226 CompleteAndValidWalletItems(); 1227 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument()); 1228 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument()); 1229 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1230 EXPECT_FALSE(controller()->LegalDocumentLinks().empty()); 1231 1232 controller()->OnCancel(); 1233 1234 // Now try it all over again with the location disclosure already accepted. 1235 // Nothing should change. 1236 Reset(); 1237 ListValue preexisting_list; 1238 preexisting_list.AppendString(kFakeEmail); 1239 g_browser_process->local_state()->Set( 1240 ::prefs::kAutofillDialogWalletLocationAcceptance, 1241 preexisting_list); 1242 } 1243} 1244 1245TEST_F(AutofillDialogControllerTest, AcceptLocationDisclosure) { 1246 // Check that accepting the dialog registers the user's name in the list 1247 // of users who have accepted the geolocation terms. 1248 EXPECT_TRUE(g_browser_process->local_state()->GetList( 1249 ::prefs::kAutofillDialogWalletLocationAcceptance)->empty()); 1250 1251 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems()); 1252 EXPECT_FALSE(controller()->LegalDocumentsText().empty()); 1253 EXPECT_TRUE(controller()->LegalDocumentLinks().empty()); 1254 controller()->OnAccept(); 1255 1256 const ListValue* list = g_browser_process->local_state()->GetList( 1257 ::prefs::kAutofillDialogWalletLocationAcceptance); 1258 ASSERT_EQ(1U, list->GetSize()); 1259 std::string accepted_username; 1260 EXPECT_TRUE(list->GetString(0, &accepted_username)); 1261 EXPECT_EQ(kFakeEmail, accepted_username); 1262 1263 // Now check it still works if that list starts off with some other username 1264 // in it. 1265 Reset(); 1266 list = g_browser_process->local_state()->GetList( 1267 ::prefs::kAutofillDialogWalletLocationAcceptance); 1268 ASSERT_TRUE(list->empty()); 1269 1270 std::string kOtherUsername("spouse@example.com"); 1271 ListValue preexisting_list; 1272 preexisting_list.AppendString(kOtherUsername); 1273 g_browser_process->local_state()->Set( 1274 ::prefs::kAutofillDialogWalletLocationAcceptance, 1275 preexisting_list); 1276 1277 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems()); 1278 EXPECT_FALSE(controller()->LegalDocumentsText().empty()); 1279 EXPECT_TRUE(controller()->LegalDocumentLinks().empty()); 1280 controller()->OnAccept(); 1281 1282 list = g_browser_process->local_state()->GetList( 1283 ::prefs::kAutofillDialogWalletLocationAcceptance); 1284 ASSERT_EQ(2U, list->GetSize()); 1285 EXPECT_NE(list->end(), list->Find(base::StringValue(kFakeEmail))); 1286 EXPECT_NE(list->end(), list->Find(base::StringValue(kOtherUsername))); 1287 1288 // Now check the list doesn't change if the user cancels out of the dialog. 1289 Reset(); 1290 list = g_browser_process->local_state()->GetList( 1291 ::prefs::kAutofillDialogWalletLocationAcceptance); 1292 ASSERT_TRUE(list->empty()); 1293 1294 g_browser_process->local_state()->Set( 1295 ::prefs::kAutofillDialogWalletLocationAcceptance, 1296 preexisting_list); 1297 1298 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems()); 1299 EXPECT_FALSE(controller()->LegalDocumentsText().empty()); 1300 EXPECT_TRUE(controller()->LegalDocumentLinks().empty()); 1301 controller()->OnCancel(); 1302 1303 list = g_browser_process->local_state()->GetList( 1304 ::prefs::kAutofillDialogWalletLocationAcceptance); 1305 ASSERT_EQ(1U, list->GetSize()); 1306 EXPECT_NE(list->end(), list->Find(base::StringValue(kOtherUsername))); 1307 EXPECT_EQ(list->end(), list->Find(base::StringValue(kFakeEmail))); 1308} 1309 1310TEST_F(AutofillDialogControllerTest, LegalDocumentOverflow) { 1311 for (size_t number_of_docs = 2; number_of_docs < 11; ++number_of_docs) { 1312 scoped_ptr<wallet::WalletItems> wallet_items = 1313 CompleteAndValidWalletItems(); 1314 for (size_t i = 0; i < number_of_docs; ++i) 1315 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument()); 1316 1317 Reset(); 1318 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1319 1320 // The dialog is only equipped to handle 2-6 legal documents. More than 1321 // 6 errors out. 1322 if (number_of_docs <= 6U) { 1323 EXPECT_FALSE(controller()->LegalDocumentsText().empty()); 1324 } else { 1325 EXPECT_TRUE(controller()->LegalDocumentsText().empty()); 1326 EXPECT_EQ(1U, NotificationsOfType( 1327 DialogNotification::WALLET_ERROR).size()); 1328 } 1329 } 1330 1331 controller()->OnCancel(); 1332} 1333 1334// Makes sure the default object IDs are respected. 1335TEST_F(AutofillDialogControllerTest, WalletDefaultItems) { 1336 scoped_ptr<wallet::WalletItems> wallet_items = 1337 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED); 1338 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument()); 1339 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument()); 1340 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument()); 1341 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument()); 1342 1343 wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress()); 1344 wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress()); 1345 wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress()); 1346 wallet_items->AddAddress(wallet::GetTestShippingAddress()); 1347 wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress()); 1348 1349 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1350 // "add", "manage", and 4 suggestions. 1351 EXPECT_EQ(6, 1352 controller()->MenuModelForSection(SECTION_CC_BILLING)->GetItemCount()); 1353 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING)-> 1354 IsItemCheckedAt(2)); 1355 ASSERT_FALSE(controller()->IsEditingExistingData(SECTION_CC_BILLING)); 1356 // "use billing", "add", "manage", and 5 suggestions. 1357 EXPECT_EQ(8, 1358 controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount()); 1359 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_SHIPPING)-> 1360 IsItemCheckedAt(4)); 1361 ASSERT_FALSE(controller()->IsEditingExistingData(SECTION_SHIPPING)); 1362} 1363 1364// Tests that invalid and AMEX default instruments are ignored. 1365TEST_F(AutofillDialogControllerTest, SelectInstrument) { 1366 scoped_ptr<wallet::WalletItems> wallet_items = 1367 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED); 1368 // Tests if default instrument is invalid, then, the first valid instrument is 1369 // selected instead of the default instrument. 1370 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument()); 1371 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument()); 1372 wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentInvalid()); 1373 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument()); 1374 1375 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1376 // 4 suggestions and "add", "manage". 1377 EXPECT_EQ(6, 1378 controller()->MenuModelForSection(SECTION_CC_BILLING)->GetItemCount()); 1379 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING)-> 1380 IsItemCheckedAt(0)); 1381 1382 // Tests if default instrument is AMEX but Wallet doesn't support 1383 // AMEX on this merchant, then the first valid instrument is 1384 // selected instead of the default instrument. 1385 wallet_items = wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED); 1386 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument()); 1387 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument()); 1388 wallet_items->AddInstrument( 1389 wallet::GetTestMaskedInstrumentAmex(wallet::AMEX_DISALLOWED)); 1390 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument()); 1391 1392 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1393 // 4 suggestions and "add", "manage". 1394 EXPECT_EQ(6, 1395 controller()->MenuModelForSection(SECTION_CC_BILLING)->GetItemCount()); 1396 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING)-> 1397 IsItemCheckedAt(0)); 1398 1399 // Tests if default instrument is AMEX and it is allowed on this merchant, 1400 // then it is selected. 1401 wallet_items = wallet::GetTestWalletItems(wallet::AMEX_ALLOWED); 1402 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument()); 1403 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument()); 1404 wallet_items->AddInstrument( 1405 wallet::GetTestMaskedInstrumentAmex(wallet::AMEX_ALLOWED)); 1406 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument()); 1407 1408 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1409 // 4 suggestions and "add", "manage". 1410 EXPECT_EQ(6, 1411 controller()->MenuModelForSection(SECTION_CC_BILLING)->GetItemCount()); 1412 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING)-> 1413 IsItemCheckedAt(2)); 1414 1415 // Tests if only have AMEX and invalid instrument, then "add" is selected. 1416 wallet_items = wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED); 1417 wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentInvalid()); 1418 wallet_items->AddInstrument( 1419 wallet::GetTestMaskedInstrumentAmex(wallet::AMEX_DISALLOWED)); 1420 1421 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1422 // 2 suggestions and "add", "manage". 1423 EXPECT_EQ(4, 1424 controller()->MenuModelForSection(SECTION_CC_BILLING)->GetItemCount()); 1425 // "add" 1426 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING)-> 1427 IsItemCheckedAt(2)); 1428} 1429 1430TEST_F(AutofillDialogControllerTest, SaveAddress) { 1431 EXPECT_CALL(*controller()->GetView(), ModelChanged()); 1432 EXPECT_CALL(*controller()->GetTestingWalletClient(), 1433 SaveToWalletMock(testing::IsNull(), testing::NotNull())); 1434 1435 scoped_ptr<wallet::WalletItems> wallet_items = 1436 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED); 1437 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument()); 1438 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1439 // If there is no shipping address in wallet, it will default to 1440 // "same-as-billing" instead of "add-new-item". "same-as-billing" is covered 1441 // by the following tests. The penultimate item in the menu is "add-new-item". 1442 ui::MenuModel* shipping_model = 1443 controller()->MenuModelForSection(SECTION_SHIPPING); 1444 shipping_model->ActivatedAt(shipping_model->GetItemCount() - 2); 1445 1446 AutofillProfile test_profile(test::GetVerifiedProfile()); 1447 FillInputs(SECTION_SHIPPING, test_profile); 1448 1449 AcceptAndLoadFakeFingerprint(); 1450} 1451 1452TEST_F(AutofillDialogControllerTest, SaveInstrument) { 1453 EXPECT_CALL(*controller()->GetView(), ModelChanged()); 1454 EXPECT_CALL(*controller()->GetTestingWalletClient(), 1455 SaveToWalletMock(testing::NotNull(), testing::IsNull())); 1456 1457 FillCCBillingInputs(); 1458 scoped_ptr<wallet::WalletItems> wallet_items = 1459 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED); 1460 wallet_items->AddAddress(wallet::GetTestShippingAddress()); 1461 SubmitWithWalletItems(wallet_items.Pass()); 1462} 1463 1464TEST_F(AutofillDialogControllerTest, SaveInstrumentWithInvalidInstruments) { 1465 EXPECT_CALL(*controller()->GetView(), ModelChanged()); 1466 EXPECT_CALL(*controller()->GetTestingWalletClient(), 1467 SaveToWalletMock(testing::NotNull(), testing::IsNull())); 1468 1469 FillCCBillingInputs(); 1470 scoped_ptr<wallet::WalletItems> wallet_items = 1471 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED); 1472 wallet_items->AddAddress(wallet::GetTestShippingAddress()); 1473 wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentInvalid()); 1474 SubmitWithWalletItems(wallet_items.Pass()); 1475} 1476 1477TEST_F(AutofillDialogControllerTest, SaveInstrumentAndAddress) { 1478 EXPECT_CALL(*controller()->GetTestingWalletClient(), 1479 SaveToWalletMock(testing::NotNull(), testing::NotNull())); 1480 1481 FillCCBillingInputs(); 1482 scoped_ptr<wallet::WalletItems> wallet_items = 1483 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED); 1484 SubmitWithWalletItems(wallet_items.Pass()); 1485} 1486 1487MATCHER(IsUpdatingExistingData, "updating existing Wallet data") { 1488 return !arg->object_id().empty(); 1489} 1490 1491MATCHER(UsesLocalBillingAddress, "uses the local billing address") { 1492 return arg->address_line_1() == ASCIIToUTF16(kEditedBillingAddress); 1493} 1494 1495// Tests that when using billing address for shipping, and there is no exact 1496// matched shipping address, then a shipping address should be added. 1497TEST_F(AutofillDialogControllerTest, BillingForShipping) { 1498 EXPECT_CALL(*controller()->GetTestingWalletClient(), 1499 SaveToWalletMock(testing::IsNull(), testing::NotNull())); 1500 1501 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems()); 1502 // Select "Same as billing" in the address menu. 1503 UseBillingForShipping(); 1504 1505 AcceptAndLoadFakeFingerprint(); 1506} 1507 1508// Tests that when using billing address for shipping, and there is an exact 1509// matched shipping address, then a shipping address should not be added. 1510TEST_F(AutofillDialogControllerTest, BillingForShippingHasMatch) { 1511 EXPECT_CALL(*controller()->GetTestingWalletClient(), 1512 SaveToWalletMock(_, _)).Times(0); 1513 1514 scoped_ptr<wallet::WalletItems> wallet_items = 1515 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED); 1516 scoped_ptr<wallet::WalletItems::MaskedInstrument> instrument = 1517 wallet::GetTestMaskedInstrument(); 1518 // Copy billing address as shipping address, and assign an id to it. 1519 scoped_ptr<wallet::Address> shipping_address( 1520 new wallet::Address(instrument->address())); 1521 shipping_address->set_object_id("shipping_address_id"); 1522 wallet_items->AddAddress(shipping_address.Pass()); 1523 wallet_items->AddInstrument(instrument.Pass()); 1524 wallet_items->AddAddress(wallet::GetTestShippingAddress()); 1525 1526 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1527 // Select "Same as billing" in the address menu. 1528 UseBillingForShipping(); 1529 1530 AcceptAndLoadFakeFingerprint(); 1531} 1532 1533// Test that the local view contents is used when saving a new instrument and 1534// the user has selected "Same as billing". 1535TEST_F(AutofillDialogControllerTest, SaveInstrumentSameAsBilling) { 1536 scoped_ptr<wallet::WalletItems> wallet_items = 1537 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED); 1538 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument()); 1539 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1540 1541 ui::MenuModel* model = controller()->MenuModelForSection(SECTION_CC_BILLING); 1542 model->ActivatedAt(model->GetItemCount() - 2); 1543 1544 FieldValueMap outputs; 1545 const DetailInputs& inputs = 1546 controller()->RequestedFieldsForSection(SECTION_CC_BILLING); 1547 AutofillProfile full_profile(test::GetVerifiedProfile()); 1548 CreditCard full_card(test::GetCreditCard()); 1549 for (size_t i = 0; i < inputs.size(); ++i) { 1550 const ServerFieldType type = inputs[i].type; 1551 if (type == ADDRESS_BILLING_LINE1) 1552 outputs[type] = ASCIIToUTF16(kEditedBillingAddress); 1553 else 1554 outputs[type] = full_profile.GetInfo(AutofillType(type), "en-US"); 1555 1556 if (outputs[type].empty()) 1557 outputs[type] = full_card.GetInfo(AutofillType(type), "en-US"); 1558 } 1559 controller()->GetView()->SetUserInput(SECTION_CC_BILLING, outputs); 1560 1561 controller()->OnAccept(); 1562 1563 EXPECT_CALL(*controller()->GetTestingWalletClient(), 1564 SaveToWalletMock(testing::NotNull(), UsesLocalBillingAddress())); 1565 AcceptAndLoadFakeFingerprint(); 1566} 1567 1568TEST_F(AutofillDialogControllerTest, CancelNoSave) { 1569 EXPECT_CALL(*controller()->GetTestingWalletClient(), 1570 SaveToWalletMock(_, _)).Times(0); 1571 1572 EXPECT_CALL(*controller()->GetView(), ModelChanged()); 1573 1574 controller()->OnDidGetWalletItems( 1575 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED)); 1576 controller()->OnCancel(); 1577} 1578 1579// Checks that clicking the Manage menu item opens a new tab with a different 1580// URL for Wallet and Autofill. 1581TEST_F(AutofillDialogControllerTest, ManageItem) { 1582 AutofillProfile full_profile(test::GetVerifiedProfile()); 1583 full_profile.set_origin(kSettingsOrigin); 1584 full_profile.SetRawInfo(ADDRESS_HOME_LINE2, base::string16()); 1585 controller()->GetTestingManager()->AddTestingProfile(&full_profile); 1586 SwitchToAutofill(); 1587 1588 SuggestionsMenuModel* shipping = GetMenuModelForSection(SECTION_SHIPPING); 1589 shipping->ExecuteCommand(shipping->GetItemCount() - 1, 0); 1590 GURL autofill_manage_url = controller()->open_tab_url(); 1591 EXPECT_EQ("chrome", autofill_manage_url.scheme()); 1592 1593 SwitchToWallet(); 1594 scoped_ptr<wallet::WalletItems> wallet_items = 1595 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED); 1596 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument()); 1597 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1598 1599 controller()->SuggestionItemSelected(shipping, shipping->GetItemCount() - 1); 1600 GURL wallet_manage_addresses_url = controller()->open_tab_url(); 1601 EXPECT_EQ("https", wallet_manage_addresses_url.scheme()); 1602 1603 SuggestionsMenuModel* billing = GetMenuModelForSection(SECTION_CC_BILLING); 1604 controller()->SuggestionItemSelected(billing, billing->GetItemCount() - 1); 1605 GURL wallet_manage_instruments_url = controller()->open_tab_url(); 1606 EXPECT_EQ("https", wallet_manage_instruments_url.scheme()); 1607 1608 EXPECT_NE(autofill_manage_url, wallet_manage_instruments_url); 1609 EXPECT_NE(wallet_manage_instruments_url, wallet_manage_addresses_url); 1610} 1611 1612// Tests that adding an autofill profile and then submitting works. 1613TEST_F(AutofillDialogControllerTest, AddAutofillProfile) { 1614 SwitchToAutofill(); 1615 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(2); 1616 1617 AutofillProfile full_profile(test::GetVerifiedProfile()); 1618 CreditCard credit_card(test::GetVerifiedCreditCard()); 1619 controller()->GetTestingManager()->AddTestingProfile(&full_profile); 1620 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card); 1621 1622 ui::MenuModel* model = controller()->MenuModelForSection(SECTION_BILLING); 1623 // Activate the "Add billing address" menu item. 1624 model->ActivatedAt(model->GetItemCount() - 2); 1625 1626 // Fill in the inputs from the profile. 1627 FieldValueMap outputs; 1628 const DetailInputs& inputs = 1629 controller()->RequestedFieldsForSection(SECTION_BILLING); 1630 AutofillProfile full_profile2(test::GetVerifiedProfile2()); 1631 for (size_t i = 0; i < inputs.size(); ++i) { 1632 const ServerFieldType type = inputs[i].type; 1633 outputs[type] = full_profile2.GetInfo(AutofillType(type), "en-US"); 1634 } 1635 controller()->GetView()->SetUserInput(SECTION_BILLING, outputs); 1636 1637 controller()->OnAccept(); 1638 const AutofillProfile& added_profile = 1639 controller()->GetTestingManager()->imported_profile(); 1640 1641 const DetailInputs& shipping_inputs = 1642 controller()->RequestedFieldsForSection(SECTION_SHIPPING); 1643 for (size_t i = 0; i < shipping_inputs.size(); ++i) { 1644 const ServerFieldType type = shipping_inputs[i].type; 1645 EXPECT_EQ(full_profile2.GetInfo(AutofillType(type), "en-US"), 1646 added_profile.GetInfo(AutofillType(type), "en-US")); 1647 } 1648} 1649 1650TEST_F(AutofillDialogControllerTest, VerifyCvv) { 1651 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_)); 1652 EXPECT_CALL(*controller()->GetTestingWalletClient(), 1653 AuthenticateInstrument(_, _)); 1654 1655 SubmitWithWalletItems(CompleteAndValidWalletItems()); 1656 1657 EXPECT_TRUE(NotificationsOfType(DialogNotification::REQUIRED_ACTION).empty()); 1658 EXPECT_TRUE(controller()->SectionIsActive(SECTION_SHIPPING)); 1659 EXPECT_TRUE(controller()->SectionIsActive(SECTION_CC_BILLING)); 1660 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); 1661 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL)); 1662 1663 SuggestionState suggestion_state = 1664 controller()->SuggestionStateForSection(SECTION_CC_BILLING); 1665 EXPECT_TRUE(suggestion_state.extra_text.empty()); 1666 1667 controller()->OnDidGetFullWallet( 1668 wallet::GetTestFullWalletWithRequiredActions( 1669 std::vector<wallet::RequiredAction>(1, wallet::VERIFY_CVV))); 1670 ASSERT_TRUE(controller()->IsSubmitPausedOn(wallet::VERIFY_CVV)); 1671 1672 EXPECT_FALSE( 1673 NotificationsOfType(DialogNotification::REQUIRED_ACTION).empty()); 1674 EXPECT_FALSE(controller()->SectionIsActive(SECTION_SHIPPING)); 1675 EXPECT_TRUE(controller()->SectionIsActive(SECTION_CC_BILLING)); 1676 1677 suggestion_state = 1678 controller()->SuggestionStateForSection(SECTION_CC_BILLING); 1679 EXPECT_FALSE(suggestion_state.extra_text.empty()); 1680 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC_BILLING)); 1681 1682 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); 1683 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL)); 1684 1685 controller()->OnAccept(); 1686 1687 EXPECT_FALSE(controller()->GetDialogOverlay().image.IsEmpty()); 1688} 1689 1690TEST_F(AutofillDialogControllerTest, ErrorDuringSubmit) { 1691 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_)); 1692 1693 SubmitWithWalletItems(CompleteAndValidWalletItems()); 1694 1695 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); 1696 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL)); 1697 1698 controller()->OnWalletError(wallet::WalletClient::UNKNOWN_ERROR); 1699 1700 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); 1701 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL)); 1702} 1703 1704TEST_F(AutofillDialogControllerTest, ErrorDuringVerifyCvv) { 1705 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_)); 1706 1707 SubmitWithWalletItems(CompleteAndValidWalletItems()); 1708 controller()->OnDidGetFullWallet( 1709 wallet::GetTestFullWalletWithRequiredActions( 1710 std::vector<wallet::RequiredAction>(1, wallet::VERIFY_CVV))); 1711 1712 ASSERT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); 1713 ASSERT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL)); 1714 1715 controller()->OnWalletError(wallet::WalletClient::UNKNOWN_ERROR); 1716 1717 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); 1718 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL)); 1719} 1720 1721// Simulates receiving an INVALID_FORM_FIELD required action while processing a 1722// |WalletClientDelegate::OnDid{Save,Update}*()| call. This can happen if Online 1723// Wallet's server validation differs from Chrome's local validation. 1724TEST_F(AutofillDialogControllerTest, WalletServerSideValidation) { 1725 scoped_ptr<wallet::WalletItems> wallet_items = 1726 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED); 1727 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument()); 1728 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1729 controller()->OnAccept(); 1730 1731 std::vector<wallet::RequiredAction> required_actions; 1732 required_actions.push_back(wallet::INVALID_FORM_FIELD); 1733 1734 std::vector<wallet::FormFieldError> form_errors; 1735 form_errors.push_back( 1736 wallet::FormFieldError(wallet::FormFieldError::INVALID_POSTAL_CODE, 1737 wallet::FormFieldError::SHIPPING_ADDRESS)); 1738 1739 EXPECT_CALL(*controller()->GetView(), UpdateForErrors()); 1740 controller()->OnDidSaveToWallet(std::string(), 1741 std::string(), 1742 required_actions, 1743 form_errors); 1744} 1745 1746// Simulates receiving unrecoverable Wallet server validation errors. 1747TEST_F(AutofillDialogControllerTest, WalletServerSideValidationUnrecoverable) { 1748 scoped_ptr<wallet::WalletItems> wallet_items = 1749 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED); 1750 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument()); 1751 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1752 controller()->OnAccept(); 1753 1754 std::vector<wallet::RequiredAction> required_actions; 1755 required_actions.push_back(wallet::INVALID_FORM_FIELD); 1756 1757 std::vector<wallet::FormFieldError> form_errors; 1758 form_errors.push_back( 1759 wallet::FormFieldError(wallet::FormFieldError::UNKNOWN_ERROR, 1760 wallet::FormFieldError::UNKNOWN_LOCATION)); 1761 1762 controller()->OnDidSaveToWallet(std::string(), 1763 std::string(), 1764 required_actions, 1765 form_errors); 1766 1767 EXPECT_EQ(1U, NotificationsOfType( 1768 DialogNotification::REQUIRED_ACTION).size()); 1769} 1770 1771// Test Wallet banners are show in the right situations. These banners promote 1772// saving details into Wallet (i.e. "[x] Save details to Wallet"). 1773TEST_F(AutofillDialogControllerTest, WalletBanners) { 1774 // Simulate non-signed-in case. 1775 SetUpControllerWithFormData(DefaultFormData()); 1776 GoogleServiceAuthError error(GoogleServiceAuthError::NONE); 1777 controller()->OnPassiveSigninFailure(error); 1778 EXPECT_EQ(0U, NotificationsOfType( 1779 DialogNotification::WALLET_USAGE_CONFIRMATION).size()); 1780 1781 // Sign in a user with a completed account. 1782 SetUpControllerWithFormData(DefaultFormData()); 1783 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems()); 1784 1785 // Full account; should show "Details from Wallet" message. 1786 EXPECT_EQ(1U, NotificationsOfType( 1787 DialogNotification::WALLET_USAGE_CONFIRMATION).size()); 1788 SwitchToAutofill(); 1789 EXPECT_EQ(1U, NotificationsOfType( 1790 DialogNotification::WALLET_USAGE_CONFIRMATION).size()); 1791 1792 // Start over and sign in a user with an incomplete account. 1793 SetUpControllerWithFormData(DefaultFormData()); 1794 scoped_ptr<wallet::WalletItems> wallet_items = 1795 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED); 1796 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument()); 1797 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1798 1799 // Partial account. 1800 EXPECT_EQ(1U, NotificationsOfType( 1801 DialogNotification::WALLET_USAGE_CONFIRMATION).size()); 1802 1803 SwitchToAutofill(); 1804 EXPECT_EQ(1U, NotificationsOfType( 1805 DialogNotification::WALLET_USAGE_CONFIRMATION).size()); 1806 1807 // A Wallet error should kill any Wallet promos. 1808 controller()->OnWalletError(wallet::WalletClient::UNKNOWN_ERROR); 1809 1810 EXPECT_EQ(1U, NotificationsOfType( 1811 DialogNotification::WALLET_ERROR).size()); 1812 EXPECT_EQ(0U, NotificationsOfType( 1813 DialogNotification::WALLET_USAGE_CONFIRMATION).size()); 1814} 1815 1816TEST_F(AutofillDialogControllerTest, ViewCancelDoesntSetPref) { 1817 ASSERT_FALSE(profile()->GetPrefs()->HasPrefPath( 1818 ::prefs::kAutofillDialogPayWithoutWallet)); 1819 1820 SwitchToAutofill(); 1821 1822 controller()->OnCancel(); 1823 controller()->ViewClosed(); 1824 1825 EXPECT_FALSE(profile()->GetPrefs()->HasPrefPath( 1826 ::prefs::kAutofillDialogPayWithoutWallet)); 1827} 1828 1829TEST_F(AutofillDialogControllerTest, SubmitWithSigninErrorDoesntSetPref) { 1830 ASSERT_FALSE(profile()->GetPrefs()->HasPrefPath( 1831 ::prefs::kAutofillDialogPayWithoutWallet)); 1832 1833 SimulateSigninError(); 1834 FillCreditCardInputs(); 1835 controller()->OnAccept(); 1836 1837 EXPECT_FALSE(profile()->GetPrefs()->HasPrefPath( 1838 ::prefs::kAutofillDialogPayWithoutWallet)); 1839} 1840 1841// Tests that there's an overlay shown while waiting for full wallet items. 1842TEST_F(AutofillDialogControllerTest, WalletFirstRun) { 1843 EXPECT_TRUE(controller()->GetDialogOverlay().image.IsEmpty()); 1844 1845 SubmitWithWalletItems(CompleteAndValidWalletItems()); 1846 EXPECT_FALSE(controller()->GetDialogOverlay().image.IsEmpty()); 1847 1848 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet()); 1849 EXPECT_FALSE(controller()->GetDialogOverlay().image.IsEmpty()); 1850 EXPECT_FALSE(form_structure()); 1851 1852 // Don't make the test wait for 2 seconds. 1853 controller()->ForceFinishSubmit(); 1854 EXPECT_TRUE(form_structure()); 1855} 1856 1857TEST_F(AutofillDialogControllerTest, ViewSubmitSetsPref) { 1858 ASSERT_FALSE(profile()->GetPrefs()->HasPrefPath( 1859 ::prefs::kAutofillDialogPayWithoutWallet)); 1860 1861 SwitchToAutofill(); 1862 FillCreditCardInputs(); 1863 controller()->OnAccept(); 1864 1865 EXPECT_TRUE(profile()->GetPrefs()->HasPrefPath( 1866 ::prefs::kAutofillDialogPayWithoutWallet)); 1867 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean( 1868 ::prefs::kAutofillDialogPayWithoutWallet)); 1869 1870 // Try again with a signin error (just leaves the pref alone). 1871 SetUpControllerWithFormData(DefaultFormData()); 1872 1873 // Setting up the controller again should not change the pref. 1874 EXPECT_TRUE(profile()->GetPrefs()->HasPrefPath( 1875 ::prefs::kAutofillDialogPayWithoutWallet)); 1876 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean( 1877 ::prefs::kAutofillDialogPayWithoutWallet)); 1878 1879 SimulateSigninError(); 1880 FillCreditCardInputs(); 1881 controller()->OnAccept(); 1882 EXPECT_TRUE(profile()->GetPrefs()->HasPrefPath( 1883 ::prefs::kAutofillDialogPayWithoutWallet)); 1884 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean( 1885 ::prefs::kAutofillDialogPayWithoutWallet)); 1886 1887 // Successfully choosing wallet does set the pref. 1888 // Note that OnDidGetWalletItems sets the account chooser to wallet mode. 1889 SetUpControllerWithFormData(DefaultFormData()); 1890 1891 controller()->OnDidFetchWalletCookieValue(std::string()); 1892 scoped_ptr<wallet::WalletItems> wallet_items = 1893 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED); 1894 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument()); 1895 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1896 controller()->OnAccept(); 1897 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet()); 1898 controller()->ForceFinishSubmit(); 1899 1900 EXPECT_TRUE(profile()->GetPrefs()->HasPrefPath( 1901 ::prefs::kAutofillDialogPayWithoutWallet)); 1902 EXPECT_FALSE(profile()->GetPrefs()->GetBoolean( 1903 ::prefs::kAutofillDialogPayWithoutWallet)); 1904} 1905 1906TEST_F(AutofillDialogControllerTest, HideWalletEmail) { 1907 SwitchToAutofill(); 1908 1909 // Email field should be showing when using Autofill. 1910 EXPECT_TRUE(controller()->SectionIsActive(SECTION_BILLING)); 1911 EXPECT_FALSE(controller()->SectionIsActive(SECTION_CC_BILLING)); 1912 EXPECT_TRUE(SectionContainsField(SECTION_BILLING, EMAIL_ADDRESS)); 1913 1914 SwitchToWallet(); 1915 1916 // Reset the wallet state. 1917 controller()->OnDidGetWalletItems(scoped_ptr<wallet::WalletItems>()); 1918 1919 // Setup some wallet state, submit, and get a full wallet to end the flow. 1920 scoped_ptr<wallet::WalletItems> wallet_items = CompleteAndValidWalletItems(); 1921 1922 // Filling |form_structure()| depends on the current username and wallet items 1923 // being fetched. Until both of these have occurred, the user should not be 1924 // able to click Submit if using Wallet. The username fetch happened earlier. 1925 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); 1926 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1927 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); 1928 1929 // Email field should be absent when using Wallet. 1930 EXPECT_FALSE(controller()->SectionIsActive(SECTION_BILLING)); 1931 EXPECT_TRUE(controller()->SectionIsActive(SECTION_CC_BILLING)); 1932 EXPECT_FALSE(SectionContainsField(SECTION_CC_BILLING, EMAIL_ADDRESS)); 1933 1934 controller()->OnAccept(); 1935 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet()); 1936 controller()->ForceFinishSubmit(); 1937 1938 ASSERT_TRUE(form_structure()); 1939 size_t i = 0; 1940 for (; i < form_structure()->field_count(); ++i) { 1941 if (form_structure()->field(i)->Type().GetStorableType() == EMAIL_ADDRESS) { 1942 EXPECT_EQ(ASCIIToUTF16(kFakeEmail), form_structure()->field(i)->value); 1943 break; 1944 } 1945 } 1946 EXPECT_LT(i, form_structure()->field_count()); 1947} 1948 1949// Test if autofill types of returned form structure are correct for billing 1950// entries. 1951TEST_F(AutofillDialogControllerTest, AutofillTypes) { 1952 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems()); 1953 controller()->OnAccept(); 1954 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet()); 1955 controller()->ForceFinishSubmit(); 1956 ASSERT_TRUE(form_structure()); 1957 ASSERT_EQ(20U, form_structure()->field_count()); 1958 EXPECT_EQ(EMAIL_ADDRESS, 1959 form_structure()->field(0)->Type().GetStorableType()); 1960 EXPECT_EQ(CREDIT_CARD_NUMBER, 1961 form_structure()->field(2)->Type().GetStorableType()); 1962 EXPECT_EQ(ADDRESS_HOME_STATE, 1963 form_structure()->field(9)->Type().GetStorableType()); 1964 EXPECT_EQ(ADDRESS_BILLING, form_structure()->field(9)->Type().group()); 1965 EXPECT_EQ(ADDRESS_HOME_STATE, 1966 form_structure()->field(16)->Type().GetStorableType()); 1967 EXPECT_EQ(ADDRESS_HOME, form_structure()->field(16)->Type().group()); 1968} 1969 1970TEST_F(AutofillDialogControllerTest, SaveDetailsInChrome) { 1971 SwitchToAutofill(); 1972 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(2); 1973 1974 AutofillProfile full_profile(test::GetVerifiedProfile()); 1975 controller()->GetTestingManager()->AddTestingProfile(&full_profile); 1976 1977 CreditCard card(test::GetVerifiedCreditCard()); 1978 controller()->GetTestingManager()->AddTestingCreditCard(&card); 1979 EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome()); 1980 1981 controller()->MenuModelForSection(SECTION_BILLING)->ActivatedAt(0); 1982 EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome()); 1983 1984 controller()->MenuModelForSection(SECTION_BILLING)->ActivatedAt(1); 1985 EXPECT_TRUE(controller()->ShouldOfferToSaveInChrome()); 1986 1987 profile()->ForceIncognito(true); 1988 EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome()); 1989} 1990 1991// Tests that user is prompted when using instrument with minimal address. 1992TEST_F(AutofillDialogControllerTest, UpgradeMinimalAddress) { 1993 // A minimal address being selected should trigger error validation in the 1994 // view. Called once for each incomplete suggestion. 1995 EXPECT_CALL(*controller()->GetView(), UpdateForErrors()); 1996 1997 scoped_ptr<wallet::WalletItems> wallet_items = 1998 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED); 1999 wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentWithIdAndAddress( 2000 "id", wallet::GetTestMinimalAddress())); 2001 scoped_ptr<wallet::Address> address(wallet::GetTestShippingAddress()); 2002 address->set_is_complete_address(false); 2003 wallet_items->AddAddress(address.Pass()); 2004 controller()->OnDidGetWalletItems(wallet_items.Pass()); 2005 2006 // Assert that dialog's SECTION_CC_BILLING section is in edit mode. 2007 ASSERT_TRUE(controller()->IsEditingExistingData(SECTION_CC_BILLING)); 2008 // Shipping section should be in edit mode because of 2009 // is_minimal_shipping_address. 2010 ASSERT_TRUE(controller()->IsEditingExistingData(SECTION_SHIPPING)); 2011} 2012 2013TEST_F(AutofillDialogControllerTest, RiskNeverLoadsWithPendingLegalDocuments) { 2014 EXPECT_CALL(*controller(), LoadRiskFingerprintData()).Times(0); 2015 2016 scoped_ptr<wallet::WalletItems> wallet_items = CompleteAndValidWalletItems(); 2017 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument()); 2018 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument()); 2019 controller()->OnDidGetWalletItems(wallet_items.Pass()); 2020 controller()->OnAccept(); 2021} 2022 2023TEST_F(AutofillDialogControllerTest, RiskLoadsAfterAcceptingLegalDocuments) { 2024 EXPECT_CALL(*controller(), LoadRiskFingerprintData()).Times(0); 2025 2026 scoped_ptr<wallet::WalletItems> wallet_items = CompleteAndValidWalletItems(); 2027 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument()); 2028 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument()); 2029 controller()->OnDidGetWalletItems(wallet_items.Pass()); 2030 2031 testing::Mock::VerifyAndClear(controller()); 2032 EXPECT_CALL(*controller(), LoadRiskFingerprintData()); 2033 2034 controller()->OnAccept(); 2035 2036 // Simulate a risk load and verify |GetRiskData()| matches the encoded value. 2037 controller()->OnDidAcceptLegalDocuments(); 2038 controller()->OnDidLoadRiskFingerprintData(GetFakeFingerprint().Pass()); 2039 EXPECT_EQ(kFakeFingerprintEncoded, controller()->GetRiskData()); 2040} 2041 2042TEST_F(AutofillDialogControllerTest, NoManageMenuItemForNewWalletUsers) { 2043 // Make sure the menu model item is created for a returning Wallet user. 2044 scoped_ptr<wallet::WalletItems> wallet_items = 2045 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED); 2046 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument()); 2047 wallet_items->AddAddress(wallet::GetTestShippingAddress()); 2048 controller()->OnDidGetWalletItems(wallet_items.Pass()); 2049 2050 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING)); 2051 // "Same as billing", "123 address", "Add address...", and "Manage addresses". 2052 EXPECT_EQ( 2053 4, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount()); 2054 2055 // Make sure the menu model item is not created for new Wallet users. 2056 base::DictionaryValue dict; 2057 scoped_ptr<base::ListValue> required_actions(new base::ListValue); 2058 required_actions->AppendString("setup_wallet"); 2059 dict.Set("required_action", required_actions.release()); 2060 controller()->OnDidGetWalletItems( 2061 wallet::WalletItems::CreateWalletItems(dict).Pass()); 2062 2063 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC_BILLING)); 2064 // "Same as billing" and "Add address...". 2065 EXPECT_EQ( 2066 2, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount()); 2067} 2068 2069TEST_F(AutofillDialogControllerTest, ShippingSectionCanBeHidden) { 2070 FormFieldData email_field; 2071 email_field.autocomplete_attribute = "email"; 2072 FormFieldData cc_field; 2073 cc_field.autocomplete_attribute = "cc-number"; 2074 FormFieldData billing_field; 2075 billing_field.autocomplete_attribute = "billing region"; 2076 2077 FormData form_data; 2078 form_data.fields.push_back(email_field); 2079 form_data.fields.push_back(cc_field); 2080 form_data.fields.push_back(billing_field); 2081 2082 AutofillProfile full_profile(test::GetVerifiedProfile()); 2083 controller()->GetTestingManager()->AddTestingProfile(&full_profile); 2084 SetUpControllerWithFormData(form_data); 2085 2086 SwitchToAutofill(); 2087 2088 EXPECT_FALSE(controller()->SectionIsActive(SECTION_SHIPPING)); 2089 2090 FillCreditCardInputs(); 2091 controller()->OnAccept(); 2092 EXPECT_TRUE(form_structure()); 2093} 2094 2095TEST_F(AutofillDialogControllerTest, ShippingSectionCanBeHiddenForWallet) { 2096 FormFieldData email_field; 2097 email_field.autocomplete_attribute = "email"; 2098 FormFieldData cc_field; 2099 cc_field.autocomplete_attribute = "cc-number"; 2100 FormFieldData billing_field; 2101 billing_field.autocomplete_attribute = "billing region"; 2102 2103 FormData form_data; 2104 form_data.fields.push_back(email_field); 2105 form_data.fields.push_back(cc_field); 2106 form_data.fields.push_back(billing_field); 2107 2108 SetUpControllerWithFormData(form_data); 2109 EXPECT_FALSE(controller()->SectionIsActive(SECTION_SHIPPING)); 2110 EXPECT_FALSE(controller()->IsShippingAddressRequired()); 2111 2112 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_)); 2113 scoped_ptr<wallet::WalletItems> wallet_items = 2114 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED); 2115 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument()); 2116 SubmitWithWalletItems(wallet_items.Pass()); 2117 controller()->OnDidGetFullWallet(wallet::GetTestFullWalletInstrumentOnly()); 2118 controller()->ForceFinishSubmit(); 2119 EXPECT_TRUE(form_structure()); 2120} 2121 2122TEST_F(AutofillDialogControllerTest, NotProdNotification) { 2123 // To make IsPayingWithWallet() true. 2124 controller()->OnDidGetWalletItems( 2125 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED)); 2126 2127 CommandLine* command_line = CommandLine::ForCurrentProcess(); 2128 ASSERT_EQ( 2129 "", 2130 command_line->GetSwitchValueASCII(switches::kWalletServiceUseSandbox)); 2131 2132 // Default everywhere is to use prod (no warning). 2133 EXPECT_EQ(0U, 2134 NotificationsOfType(DialogNotification::DEVELOPER_WARNING).size()); 2135 2136 command_line->AppendSwitchASCII(switches::kWalletServiceUseSandbox, "1"); 2137 EXPECT_EQ(1U, 2138 NotificationsOfType(DialogNotification::DEVELOPER_WARNING).size()); 2139} 2140 2141// Ensure Wallet instruments marked expired by the server are shown as invalid. 2142TEST_F(AutofillDialogControllerTest, WalletExpiredCard) { 2143 scoped_ptr<wallet::WalletItems> wallet_items = 2144 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED); 2145 wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentExpired()); 2146 controller()->OnDidGetWalletItems(wallet_items.Pass()); 2147 2148 EXPECT_TRUE(controller()->IsEditingExistingData(SECTION_CC_BILLING)); 2149 2150 const DetailInputs& inputs = 2151 controller()->RequestedFieldsForSection(SECTION_CC_BILLING); 2152 FieldValueMap outputs; 2153 CopyInitialValues(inputs, &outputs); 2154 2155 // The local inputs are invalid because the server said so. They'll 2156 // stay invalid until they differ from the remotely fetched model. 2157 ValidityMessages messages = controller()->InputsAreValid(SECTION_CC_BILLING, 2158 outputs); 2159 EXPECT_TRUE(messages.HasSureError(CREDIT_CARD_EXP_MONTH)); 2160 EXPECT_TRUE(messages.HasSureError(CREDIT_CARD_EXP_4_DIGIT_YEAR)); 2161 2162 // Make the local input year differ from the instrument. 2163 CopyInitialValues(inputs, &outputs); 2164 outputs[CREDIT_CARD_EXP_4_DIGIT_YEAR] = ASCIIToUTF16("3002"); 2165 messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs); 2166 EXPECT_FALSE(HasAnyError(messages, CREDIT_CARD_EXP_MONTH)); 2167 EXPECT_FALSE(HasAnyError(messages, CREDIT_CARD_EXP_4_DIGIT_YEAR)); 2168 2169 // Make the local input month differ from the instrument. 2170 CopyInitialValues(inputs, &outputs); 2171 outputs[CREDIT_CARD_EXP_MONTH] = ASCIIToUTF16("06"); 2172 messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs); 2173 EXPECT_FALSE(HasAnyError(messages, CREDIT_CARD_EXP_MONTH)); 2174 EXPECT_FALSE(HasAnyError(messages, CREDIT_CARD_EXP_4_DIGIT_YEAR)); 2175} 2176 2177TEST_F(AutofillDialogControllerTest, ChooseAnotherInstrumentOrAddress) { 2178 SubmitWithWalletItems(CompleteAndValidWalletItems()); 2179 2180 EXPECT_EQ(0U, NotificationsOfType( 2181 DialogNotification::REQUIRED_ACTION).size()); 2182 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems()); 2183 controller()->OnDidGetFullWallet( 2184 wallet::GetTestFullWalletWithRequiredActions( 2185 std::vector<wallet::RequiredAction>( 2186 1, wallet::CHOOSE_ANOTHER_INSTRUMENT_OR_ADDRESS))); 2187 EXPECT_EQ(1U, NotificationsOfType( 2188 DialogNotification::REQUIRED_ACTION).size()); 2189 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems()); 2190 2191 controller()->OnAccept(); 2192 EXPECT_EQ(0U, NotificationsOfType( 2193 DialogNotification::REQUIRED_ACTION).size()); 2194} 2195 2196TEST_F(AutofillDialogControllerTest, NewCardBubbleShown) { 2197 SwitchToAutofill(); 2198 FillCreditCardInputs(); 2199 controller()->OnAccept(); 2200 controller()->ViewClosed(); 2201 2202 EXPECT_EQ(1, mock_new_card_bubble_controller()->bubbles_shown()); 2203 EXPECT_EQ(0, test_generated_bubble_controller()->bubbles_shown()); 2204} 2205 2206TEST_F(AutofillDialogControllerTest, GeneratedCardBubbleShown) { 2207 SubmitWithWalletItems(CompleteAndValidWalletItems()); 2208 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet()); 2209 controller()->ForceFinishSubmit(); 2210 controller()->ViewClosed(); 2211 2212 EXPECT_EQ(0, mock_new_card_bubble_controller()->bubbles_shown()); 2213 EXPECT_EQ(1, test_generated_bubble_controller()->bubbles_shown()); 2214} 2215 2216// Verify that new Wallet data is fetched when the user switches away from the 2217// tab hosting the Autofill dialog and back. Also verify that the user's 2218// selection is preserved across this re-fetch. 2219TEST_F(AutofillDialogControllerTest, ReloadWalletItemsOnActivation) { 2220 // Initialize some Wallet data. 2221 scoped_ptr<wallet::WalletItems> wallet_items = 2222 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED); 2223 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument()); 2224 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument()); 2225 wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress()); 2226 wallet_items->AddAddress(wallet::GetTestShippingAddress()); 2227 controller()->OnDidGetWalletItems(wallet_items.Pass()); 2228 2229 // Initially, the default entries should be selected. 2230 ui::MenuModel* cc_billing_model = 2231 controller()->MenuModelForSection(SECTION_CC_BILLING); 2232 ui::MenuModel* shipping_model = 2233 controller()->MenuModelForSection(SECTION_SHIPPING); 2234 // "add", "manage", and 2 suggestions. 2235 ASSERT_EQ(4, cc_billing_model->GetItemCount()); 2236 EXPECT_TRUE(cc_billing_model->IsItemCheckedAt(0)); 2237 // "use billing", "add", "manage", and 2 suggestions. 2238 ASSERT_EQ(5, shipping_model->GetItemCount()); 2239 EXPECT_TRUE(shipping_model->IsItemCheckedAt(2)); 2240 2241 // Select entries other than the defaults. 2242 cc_billing_model->ActivatedAt(1); 2243 shipping_model->ActivatedAt(1); 2244 // 2 suggestions, "add", and "manage". 2245 ASSERT_EQ(4, cc_billing_model->GetItemCount()); 2246 EXPECT_TRUE(cc_billing_model->IsItemCheckedAt(1)); 2247 // "use billing", 2 suggestions, "add", "manage". 2248 ASSERT_EQ(5, shipping_model->GetItemCount()); 2249 EXPECT_TRUE(shipping_model-> IsItemCheckedAt(1)); 2250 2251 // Simulate switching away from the tab and back. This should issue a request 2252 // for wallet items. 2253 controller()->ClearLastWalletItemsFetchTimestampForTesting(); 2254 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems()); 2255 controller()->TabActivated(); 2256 2257 // Simulate a response that includes different items. 2258 wallet_items = wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED); 2259 wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentExpired()); 2260 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument()); 2261 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument()); 2262 wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress()); 2263 controller()->OnDidGetWalletItems(wallet_items.Pass()); 2264 2265 // The previously selected entries should still be selected. 2266 // 3 suggestions, "add", and "manage". 2267 ASSERT_EQ(5, cc_billing_model->GetItemCount()); 2268 EXPECT_TRUE(cc_billing_model->IsItemCheckedAt(2)); 2269 // "use billing", 1 suggestion, "add", and "manage". 2270 ASSERT_EQ(4, shipping_model->GetItemCount()); 2271 EXPECT_TRUE(shipping_model->IsItemCheckedAt(1)); 2272} 2273 2274// Verify that if the default values change when re-fetching Wallet data, these 2275// new default values are selected in the dialog. 2276TEST_F(AutofillDialogControllerTest, 2277 ReloadWalletItemsOnActivationWithNewDefaults) { 2278 // Initialize some Wallet data. 2279 scoped_ptr<wallet::WalletItems> wallet_items = 2280 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED); 2281 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument()); 2282 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument()); 2283 wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress()); 2284 wallet_items->AddAddress(wallet::GetTestShippingAddress()); 2285 controller()->OnDidGetWalletItems(wallet_items.Pass()); 2286 2287 // Initially, the default entries should be selected. 2288 ui::MenuModel* cc_billing_model = 2289 controller()->MenuModelForSection(SECTION_CC_BILLING); 2290 ui::MenuModel* shipping_model = 2291 controller()->MenuModelForSection(SECTION_SHIPPING); 2292 // 2 suggestions, "add", and "manage". 2293 ASSERT_EQ(4, cc_billing_model->GetItemCount()); 2294 EXPECT_TRUE(cc_billing_model->IsItemCheckedAt(0)); 2295 // "use billing", 2 suggestions, "add", and "manage". 2296 ASSERT_EQ(5, shipping_model->GetItemCount()); 2297 EXPECT_TRUE(shipping_model->IsItemCheckedAt(2)); 2298 2299 // Simulate switching away from the tab and back. This should issue a request 2300 // for wallet items. 2301 controller()->ClearLastWalletItemsFetchTimestampForTesting(); 2302 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems()); 2303 controller()->TabActivated(); 2304 2305 // Simulate a response that includes different default values. 2306 wallet_items = 2307 wallet::GetTestWalletItemsWithDefaultIds("new_default_instrument_id", 2308 "new_default_address_id", 2309 wallet::AMEX_DISALLOWED); 2310 scoped_ptr<wallet::Address> other_address = wallet::GetTestShippingAddress(); 2311 other_address->set_object_id("other_address_id"); 2312 scoped_ptr<wallet::Address> new_default_address = 2313 wallet::GetTestNonDefaultShippingAddress(); 2314 new_default_address->set_object_id("new_default_address_id"); 2315 2316 wallet_items->AddInstrument( 2317 wallet::GetTestMaskedInstrumentWithId("other_instrument_id")); 2318 wallet_items->AddInstrument( 2319 wallet::GetTestMaskedInstrumentWithId("new_default_instrument_id")); 2320 wallet_items->AddAddress(new_default_address.Pass()); 2321 wallet_items->AddAddress(other_address.Pass()); 2322 controller()->OnDidGetWalletItems(wallet_items.Pass()); 2323 2324 // The new default entries should be selected. 2325 // 2 suggestions, "add", and "manage". 2326 ASSERT_EQ(4, cc_billing_model->GetItemCount()); 2327 EXPECT_TRUE(cc_billing_model->IsItemCheckedAt(1)); 2328 // "use billing", 2 suggestions, "add", and "manage". 2329 ASSERT_EQ(5, shipping_model->GetItemCount()); 2330 EXPECT_TRUE(shipping_model->IsItemCheckedAt(1)); 2331} 2332 2333TEST_F(AutofillDialogControllerTest, ReloadWithEmptyWalletItems) { 2334 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems()); 2335 controller()->MenuModelForSection(SECTION_CC_BILLING)->ActivatedAt(1); 2336 controller()->MenuModelForSection(SECTION_SHIPPING)->ActivatedAt(1); 2337 2338 controller()->ClearLastWalletItemsFetchTimestampForTesting(); 2339 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems()); 2340 controller()->TabActivated(); 2341 2342 controller()->OnDidGetWalletItems( 2343 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED)); 2344 2345 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC_BILLING)); 2346 EXPECT_EQ( 2347 3, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount()); 2348} 2349 2350TEST_F(AutofillDialogControllerTest, SaveInChromeByDefault) { 2351 EXPECT_TRUE(controller()->ShouldSaveInChrome()); 2352 SwitchToAutofill(); 2353 FillCreditCardInputs(); 2354 controller()->OnAccept(); 2355 EXPECT_TRUE(controller()->ShouldSaveInChrome()); 2356} 2357 2358TEST_F(AutofillDialogControllerTest, 2359 SaveInChromePreferenceNotRememberedOnCancel) { 2360 EXPECT_TRUE(controller()->ShouldSaveInChrome()); 2361 SwitchToAutofill(); 2362 controller()->GetView()->CheckSaveDetailsLocallyCheckbox(false); 2363 controller()->OnCancel(); 2364 EXPECT_TRUE(controller()->ShouldSaveInChrome()); 2365} 2366 2367TEST_F(AutofillDialogControllerTest, 2368 SaveInChromePreferenceRememberedOnSuccess) { 2369 EXPECT_TRUE(controller()->ShouldSaveInChrome()); 2370 SwitchToAutofill(); 2371 FillCreditCardInputs(); 2372 controller()->GetView()->CheckSaveDetailsLocallyCheckbox(false); 2373 controller()->OnAccept(); 2374 EXPECT_FALSE(controller()->ShouldSaveInChrome()); 2375} 2376 2377TEST_F(AutofillDialogControllerTest, 2378 SubmitButtonIsDisabled_SpinnerFinishesBeforeDelay) { 2379 // Reset Wallet state. 2380 controller()->OnDidGetWalletItems(scoped_ptr<wallet::WalletItems>()); 2381 2382 EXPECT_EQ(1, controller()->get_submit_button_delay_count()); 2383 2384 // Begin the submit button delay. 2385 controller()->SimulateSubmitButtonDelayBegin(); 2386 2387 EXPECT_TRUE(controller()->ShouldShowSpinner()); 2388 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); 2389 2390 // Stop the spinner. 2391 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems()); 2392 2393 EXPECT_FALSE(controller()->ShouldShowSpinner()); 2394 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); 2395 2396 // End the submit button delay. 2397 controller()->SimulateSubmitButtonDelayEnd(); 2398 2399 EXPECT_FALSE(controller()->ShouldShowSpinner()); 2400 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); 2401} 2402 2403TEST_F(AutofillDialogControllerTest, 2404 SubmitButtonIsDisabled_SpinnerFinishesAfterDelay) { 2405 // Reset Wallet state. 2406 controller()->OnDidGetWalletItems(scoped_ptr<wallet::WalletItems>()); 2407 2408 EXPECT_EQ(1, controller()->get_submit_button_delay_count()); 2409 2410 // Begin the submit button delay. 2411 controller()->SimulateSubmitButtonDelayBegin(); 2412 2413 EXPECT_TRUE(controller()->ShouldShowSpinner()); 2414 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); 2415 2416 // End the submit button delay. 2417 controller()->SimulateSubmitButtonDelayEnd(); 2418 2419 EXPECT_TRUE(controller()->ShouldShowSpinner()); 2420 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); 2421 2422 // Stop the spinner. 2423 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems()); 2424 2425 EXPECT_FALSE(controller()->ShouldShowSpinner()); 2426 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); 2427} 2428 2429TEST_F(AutofillDialogControllerTest, SubmitButtonIsDisabled_NoSpinner) { 2430 SwitchToAutofill(); 2431 2432 EXPECT_EQ(1, controller()->get_submit_button_delay_count()); 2433 2434 // Begin the submit button delay. 2435 controller()->SimulateSubmitButtonDelayBegin(); 2436 2437 EXPECT_FALSE(controller()->ShouldShowSpinner()); 2438 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); 2439 2440 // End the submit button delay. 2441 controller()->SimulateSubmitButtonDelayEnd(); 2442 2443 EXPECT_FALSE(controller()->ShouldShowSpinner()); 2444 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); 2445} 2446 2447TEST_F(AutofillDialogControllerTest, IconsForFields_NoCreditCard) { 2448 FieldValueMap values; 2449 values[EMAIL_ADDRESS] = ASCIIToUTF16(kFakeEmail); 2450 FieldIconMap icons = controller()->IconsForFields(values); 2451 EXPECT_TRUE(icons.empty()); 2452} 2453 2454TEST_F(AutofillDialogControllerTest, IconsForFields_CreditCardNumberOnly) { 2455 FieldValueMap values; 2456 values[EMAIL_ADDRESS] = ASCIIToUTF16(kFakeEmail); 2457 values[CREDIT_CARD_NUMBER] = ASCIIToUTF16(kTestCCNumberVisa); 2458 FieldIconMap icons = controller()->IconsForFields(values); 2459 EXPECT_EQ(1UL, icons.size()); 2460 EXPECT_EQ(1UL, icons.count(CREDIT_CARD_NUMBER)); 2461} 2462 2463TEST_F(AutofillDialogControllerTest, IconsForFields_CvcOnly) { 2464 FieldValueMap values; 2465 values[EMAIL_ADDRESS] = ASCIIToUTF16(kFakeEmail); 2466 values[CREDIT_CARD_VERIFICATION_CODE] = ASCIIToUTF16("123"); 2467 FieldIconMap icons = controller()->IconsForFields(values); 2468 EXPECT_EQ(1UL, icons.size()); 2469 EXPECT_EQ(1UL, icons.count(CREDIT_CARD_VERIFICATION_CODE)); 2470} 2471 2472TEST_F(AutofillDialogControllerTest, IconsForFields_BothCreditCardAndCvc) { 2473 FieldValueMap values; 2474 values[EMAIL_ADDRESS] = ASCIIToUTF16(kFakeEmail); 2475 values[CREDIT_CARD_NUMBER] = ASCIIToUTF16(kTestCCNumberVisa); 2476 values[CREDIT_CARD_VERIFICATION_CODE] = ASCIIToUTF16("123"); 2477 FieldIconMap icons = controller()->IconsForFields(values); 2478 EXPECT_EQ(2UL, icons.size()); 2479 EXPECT_EQ(1UL, icons.count(CREDIT_CARD_VERIFICATION_CODE)); 2480 EXPECT_EQ(1UL, icons.count(CREDIT_CARD_NUMBER)); 2481} 2482 2483TEST_F(AutofillDialogControllerTest, FieldControlsIcons) { 2484 EXPECT_TRUE(controller()->FieldControlsIcons(CREDIT_CARD_NUMBER)); 2485 EXPECT_FALSE(controller()->FieldControlsIcons(CREDIT_CARD_VERIFICATION_CODE)); 2486 EXPECT_FALSE(controller()->FieldControlsIcons(EMAIL_ADDRESS)); 2487} 2488 2489TEST_F(AutofillDialogControllerTest, SaveCreditCardIncludesName_NoBilling) { 2490 SwitchToAutofill(); 2491 2492 CreditCard test_credit_card(test::GetVerifiedCreditCard()); 2493 FillInputs(SECTION_CC, test_credit_card); 2494 2495 AutofillProfile test_profile(test::GetVerifiedProfile()); 2496 FillInputs(SECTION_BILLING, test_profile); 2497 2498 UseBillingForShipping(); 2499 2500 controller()->GetView()->CheckSaveDetailsLocallyCheckbox(true); 2501 controller()->OnAccept(); 2502 2503 TestPersonalDataManager* test_pdm = controller()->GetTestingManager(); 2504 const CreditCard& imported_card = test_pdm->imported_credit_card(); 2505 EXPECT_EQ(test_profile.GetRawInfo(NAME_FULL), 2506 imported_card.GetRawInfo(CREDIT_CARD_NAME)); 2507} 2508 2509TEST_F(AutofillDialogControllerTest, SaveCreditCardIncludesName_WithBilling) { 2510 SwitchToAutofill(); 2511 2512 TestPersonalDataManager* test_pdm = controller()->GetTestingManager(); 2513 AutofillProfile test_profile(test::GetVerifiedProfile()); 2514 2515 EXPECT_CALL(*controller()->GetView(), ModelChanged()); 2516 test_pdm->AddTestingProfile(&test_profile); 2517 ASSERT_TRUE(controller()->MenuModelForSection(SECTION_BILLING)); 2518 2519 CreditCard test_credit_card(test::GetVerifiedCreditCard()); 2520 FillInputs(SECTION_CC, test_credit_card); 2521 2522 controller()->GetView()->CheckSaveDetailsLocallyCheckbox(true); 2523 controller()->OnAccept(); 2524 2525 const CreditCard& imported_card = test_pdm->imported_credit_card(); 2526 EXPECT_EQ(test_profile.GetRawInfo(NAME_FULL), 2527 imported_card.GetRawInfo(CREDIT_CARD_NAME)); 2528 2529 controller()->ViewClosed(); 2530} 2531 2532TEST_F(AutofillDialogControllerTest, InputEditability) { 2533 // Empty wallet items: all fields are editable. 2534 scoped_ptr<wallet::WalletItems> items = 2535 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED); 2536 controller()->OnDidGetWalletItems(items.Pass()); 2537 2538 DialogSection sections[] = { SECTION_CC_BILLING, SECTION_SHIPPING }; 2539 for (size_t i = 0; i < arraysize(sections); ++i) { 2540 const DetailInputs& inputs = 2541 controller()->RequestedFieldsForSection(sections[i]); 2542 for (size_t j = 0; j < inputs.size(); ++j) { 2543 EXPECT_TRUE(controller()->InputIsEditable(inputs[j], sections[i])); 2544 } 2545 } 2546 2547 // Expired instrument: CC number + CVV are not editable. 2548 items = wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED); 2549 scoped_ptr<wallet::WalletItems::MaskedInstrument> expired_instrument = 2550 wallet::GetTestMaskedInstrumentExpired(); 2551 items->AddInstrument(expired_instrument.Pass()); 2552 controller()->OnDidGetWalletItems(items.Pass()); 2553 EXPECT_TRUE(controller()->IsEditingExistingData(SECTION_CC_BILLING)); 2554 2555 const DetailInputs& inputs = 2556 controller()->RequestedFieldsForSection(SECTION_CC_BILLING); 2557 FieldValueMap outputs; 2558 CopyInitialValues(inputs, &outputs); 2559 controller()->GetView()->SetUserInput(SECTION_CC_BILLING, outputs); 2560 2561 for (size_t i = 0; i < arraysize(sections); ++i) { 2562 const DetailInputs& inputs = 2563 controller()->RequestedFieldsForSection(sections[i]); 2564 for (size_t j = 0; j < inputs.size(); ++j) { 2565 if (inputs[j].type == CREDIT_CARD_NUMBER || 2566 inputs[j].type == CREDIT_CARD_VERIFICATION_CODE) { 2567 EXPECT_FALSE(controller()->InputIsEditable(inputs[j], sections[i])); 2568 } else { 2569 EXPECT_TRUE(controller()->InputIsEditable(inputs[j], sections[i])); 2570 } 2571 } 2572 } 2573 2574 // User changes the billing address; same story. 2575 outputs[ADDRESS_BILLING_ZIP] = ASCIIToUTF16("77025"); 2576 controller()->GetView()->SetUserInput(SECTION_CC_BILLING, outputs); 2577 for (size_t i = 0; i < arraysize(sections); ++i) { 2578 const DetailInputs& inputs = 2579 controller()->RequestedFieldsForSection(sections[i]); 2580 for (size_t j = 0; j < inputs.size(); ++j) { 2581 if (inputs[j].type == CREDIT_CARD_NUMBER || 2582 inputs[j].type == CREDIT_CARD_VERIFICATION_CODE) { 2583 EXPECT_FALSE(controller()->InputIsEditable(inputs[j], sections[i])); 2584 } else { 2585 EXPECT_TRUE(controller()->InputIsEditable(inputs[j], sections[i])); 2586 } 2587 } 2588 } 2589 2590 // User changes a detail of the CC itself (expiration date), CVV is now 2591 // editable (and mandatory). 2592 outputs[CREDIT_CARD_EXP_MONTH] = ASCIIToUTF16("06"); 2593 controller()->GetView()->SetUserInput(SECTION_CC_BILLING, outputs); 2594 for (size_t i = 0; i < arraysize(sections); ++i) { 2595 const DetailInputs& inputs = 2596 controller()->RequestedFieldsForSection(sections[i]); 2597 for (size_t j = 0; j < inputs.size(); ++j) { 2598 if (inputs[j].type == CREDIT_CARD_NUMBER) 2599 EXPECT_FALSE(controller()->InputIsEditable(inputs[j], sections[i])); 2600 else 2601 EXPECT_TRUE(controller()->InputIsEditable(inputs[j], sections[i])); 2602 } 2603 } 2604} 2605 2606// When the default country is something besides US, wallet is not selected 2607// and the account chooser shouldn't be visible. 2608TEST_F(AutofillDialogControllerTest, HideWalletInOtherCountries) { 2609 ResetControllerWithFormData(DefaultFormData()); 2610 controller()->GetTestingManager()->set_default_country_code("US"); 2611 controller()->Show(); 2612 EXPECT_TRUE( 2613 controller()->AccountChooserModelForTesting()->WalletIsSelected()); 2614 controller()->OnDidFetchWalletCookieValue(std::string()); 2615 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems()); 2616 EXPECT_TRUE(controller()->ShouldShowAccountChooser()); 2617 EXPECT_TRUE( 2618 controller()->AccountChooserModelForTesting()->WalletIsSelected()); 2619 2620 ResetControllerWithFormData(DefaultFormData()); 2621 controller()->GetTestingManager()->set_default_country_code("ES"); 2622 controller()->Show(); 2623 EXPECT_FALSE(controller()->ShouldShowAccountChooser()); 2624 EXPECT_FALSE( 2625 controller()->AccountChooserModelForTesting()->WalletIsSelected()); 2626} 2627 2628TEST_F(AutofillDialogControllerTest, DontGetWalletTillNecessary) { 2629 // When starting on local data mode, the dialog will provide a "Use Google 2630 // Wallet" link. 2631 profile()->GetPrefs()->SetBoolean( 2632 ::prefs::kAutofillDialogPayWithoutWallet, true); 2633 ResetControllerWithFormData(DefaultFormData()); 2634 controller()->Show(); 2635 base::string16 use_wallet_text = controller()->SignInLinkText(); 2636 EXPECT_EQ(TestAutofillDialogController::NOT_CHECKED, 2637 controller()->SignedInState()); 2638 2639 // When clicked, this link will ask for wallet items. If there's a signin 2640 // failure, the link will switch to "Sign in to use Google Wallet". 2641 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems()); 2642 controller()->SignInLinkClicked(); 2643 EXPECT_NE(TestAutofillDialogController::NOT_CHECKED, 2644 controller()->SignedInState()); 2645 controller()->OnDidFetchWalletCookieValue(std::string()); 2646 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems()); 2647 controller()->OnPassiveSigninFailure(GoogleServiceAuthError( 2648 GoogleServiceAuthError::CONNECTION_FAILED)); 2649 EXPECT_NE(use_wallet_text, controller()->SignInLinkText()); 2650} 2651 2652TEST_F(AutofillDialogControllerTest, MultiAccountSwitch) { 2653 std::vector<std::string> users; 2654 users.push_back("user_1@example.com"); 2655 users.push_back("user_2@example.com"); 2656 controller()->OnDidGetWalletItems( 2657 wallet::GetTestWalletItemsWithUsers(users, 0)); 2658 2659 // Items should be: Account 1, account 2, add account, disable wallet. 2660 EXPECT_EQ(4, controller()->MenuModelForAccountChooser()->GetItemCount()); 2661 EXPECT_EQ(0U, controller()->GetTestingWalletClient()->user_index()); 2662 2663 // GetWalletItems should be called when the user switches accounts. 2664 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems()); 2665 controller()->MenuModelForAccountChooser()->ActivatedAt(1); 2666 // The wallet client should be updated to the new user index. 2667 EXPECT_EQ(1U, controller()->GetTestingWalletClient()->user_index()); 2668} 2669 2670TEST_F(AutofillDialogControllerTest, PassiveAuthFailure) { 2671 controller()->OnDidGetWalletItems( 2672 wallet::GetTestWalletItemsWithRequiredAction( 2673 wallet::PASSIVE_GAIA_AUTH)); 2674 EXPECT_TRUE(controller()->ShouldShowSpinner()); 2675 controller()->OnPassiveSigninFailure(GoogleServiceAuthError( 2676 GoogleServiceAuthError::NONE)); 2677 EXPECT_FALSE(controller()->ShouldShowSpinner()); 2678} 2679 2680} // namespace autofill 2681