autofill_dialog_controller_unittest.cc revision ca12bfac764ba476d6cd062bf1dde12cc64c3f40
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/utf_string_conversions.h" 16#include "base/tuple.h" 17#include "chrome/browser/ui/autofill/autofill_credit_card_bubble_controller.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/test_autofill_credit_card_bubble.h" 21#include "chrome/common/pref_names.h" 22#include "chrome/common/render_messages.h" 23#include "chrome/test/base/chrome_render_view_host_test_harness.h" 24#include "chrome/test/base/testing_profile.h" 25#include "components/autofill/content/browser/risk/proto/fingerprint.pb.h" 26#include "components/autofill/content/browser/wallet/full_wallet.h" 27#include "components/autofill/content/browser/wallet/instrument.h" 28#include "components/autofill/content/browser/wallet/mock_wallet_client.h" 29#include "components/autofill/content/browser/wallet/wallet_address.h" 30#include "components/autofill/content/browser/wallet/wallet_service_url.h" 31#include "components/autofill/content/browser/wallet/wallet_test_util.h" 32#include "components/autofill/core/browser/autofill_common_test.h" 33#include "components/autofill/core/browser/autofill_metrics.h" 34#include "components/autofill/core/browser/test_personal_data_manager.h" 35#include "components/autofill/core/common/autofill_switches.h" 36#include "components/autofill/core/common/form_data.h" 37#include "content/public/browser/web_contents.h" 38#include "content/public/test/mock_render_process_host.h" 39#include "testing/gmock/include/gmock/gmock.h" 40#include "testing/gtest/include/gtest/gtest.h" 41 42#if defined(OS_WIN) 43#include "ui/base/win/scoped_ole_initializer.h" 44#endif 45 46using testing::_; 47 48namespace autofill { 49 50namespace { 51 52const char kFakeEmail[] = "user@example.com"; 53const char kFakeFingerprintEncoded[] = "CgVaAwiACA=="; 54const char kEditedBillingAddress[] = "123 edited billing address"; 55const char* kFieldsFromPage[] = 56 { "email", 57 "cc-name", 58 "cc-number", 59 "cc-exp-month", 60 "cc-exp-year", 61 "cc-csc", 62 "billing name", 63 "billing address-line1", 64 "billing locality", 65 "billing region", 66 "billing postal-code", 67 "billing country", 68 "billing tel", 69 "shipping name", 70 "shipping address-line1", 71 "shipping locality", 72 "shipping region", 73 "shipping postal-code", 74 "shipping country", 75 "shipping tel", 76 }; 77const char kSettingsOrigin[] = "Chrome settings"; 78const char kTestCCNumberAmex[] = "376200000000002"; 79const char kTestCCNumberVisa[] = "4111111111111111"; 80const char kTestCCNumberMaster[] = "5555555555554444"; 81const char kTestCCNumberDiscover[] = "6011111111111117"; 82const char kTestCCNumberIncomplete[] = "4111111111"; 83// Credit card number fails Luhn check. 84const char kTestCCNumberInvalid[] = "4111111111111112"; 85 86 87void SetOutputValue(const DetailInputs& inputs, 88 DetailOutputMap* outputs, 89 AutofillFieldType type, 90 const std::string& value) { 91 for (size_t i = 0; i < inputs.size(); ++i) { 92 const DetailInput& input = inputs[i]; 93 (*outputs)[&input] = input.type == type ? 94 ASCIIToUTF16(value) : 95 input.initial_value; 96 } 97} 98 99scoped_ptr<wallet::WalletItems> CompleteAndValidWalletItems() { 100 scoped_ptr<wallet::WalletItems> items = wallet::GetTestWalletItems(); 101 items->AddInstrument(wallet::GetTestMaskedInstrument()); 102 items->AddAddress(wallet::GetTestShippingAddress()); 103 return items.Pass(); 104} 105 106scoped_ptr<wallet::FullWallet> CreateFullWallet(const char* required_action) { 107 base::DictionaryValue dict; 108 scoped_ptr<base::ListValue> list(new base::ListValue()); 109 list->AppendString(required_action); 110 dict.Set("required_action", list.release()); 111 return wallet::FullWallet::CreateFullWallet(dict); 112} 113 114scoped_ptr<risk::Fingerprint> GetFakeFingerprint() { 115 scoped_ptr<risk::Fingerprint> fingerprint(new risk::Fingerprint()); 116 // Add some data to the proto, else the encoded content is empty. 117 fingerprint->mutable_machine_characteristics()->mutable_screen_size()-> 118 set_width(1024); 119 return fingerprint.Pass(); 120} 121 122class TestAutofillDialogView : public AutofillDialogView { 123 public: 124 TestAutofillDialogView() {} 125 virtual ~TestAutofillDialogView() {} 126 127 virtual void Show() OVERRIDE {} 128 virtual void Hide() OVERRIDE {} 129 virtual void UpdateNotificationArea() OVERRIDE {} 130 virtual void UpdateAccountChooser() OVERRIDE {} 131 virtual void UpdateButtonStrip() OVERRIDE {} 132 virtual void UpdateDetailArea() OVERRIDE {} 133 virtual void UpdateAutocheckoutStepsArea() OVERRIDE {} 134 virtual void UpdateSection(DialogSection section) OVERRIDE {} 135 virtual void FillSection(DialogSection section, 136 const DetailInput& originating_input) OVERRIDE {}; 137 virtual void GetUserInput(DialogSection section, DetailOutputMap* output) 138 OVERRIDE { 139 *output = outputs_[section]; 140 } 141 virtual TestableAutofillDialogView* GetTestableView() OVERRIDE { 142 return NULL; 143 } 144 145 virtual string16 GetCvc() OVERRIDE { return string16(); } 146 virtual bool SaveDetailsLocally() OVERRIDE { return true; } 147 virtual const content::NavigationController* ShowSignIn() OVERRIDE { 148 return NULL; 149 } 150 virtual void HideSignIn() OVERRIDE {} 151 virtual void UpdateProgressBar(double value) OVERRIDE {} 152 153 MOCK_METHOD0(ModelChanged, void()); 154 MOCK_METHOD0(UpdateForErrors, void()); 155 156 virtual void OnSignInResize(const gfx::Size& pref_size) OVERRIDE {} 157 158 void SetUserInput(DialogSection section, const DetailOutputMap& map) { 159 outputs_[section] = map; 160 } 161 162 private: 163 std::map<DialogSection, DetailOutputMap> outputs_; 164 165 DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogView); 166}; 167 168// Bring over command-ids from AccountChooserModel. 169class TestAccountChooserModel : public AccountChooserModel { 170 public: 171 TestAccountChooserModel(AccountChooserModelDelegate* delegate, 172 PrefService* prefs, 173 const AutofillMetrics& metric_logger) 174 : AccountChooserModel(delegate, prefs, metric_logger, 175 DIALOG_TYPE_REQUEST_AUTOCOMPLETE) {} 176 virtual ~TestAccountChooserModel() {} 177 178 using AccountChooserModel::kActiveWalletItemId; 179 using AccountChooserModel::kAutofillItemId; 180 181 private: 182 DISALLOW_COPY_AND_ASSIGN(TestAccountChooserModel); 183}; 184 185class TestAutofillDialogController 186 : public AutofillDialogControllerImpl, 187 public base::SupportsWeakPtr<TestAutofillDialogController> { 188 public: 189 TestAutofillDialogController( 190 content::WebContents* contents, 191 const FormData& form_structure, 192 const GURL& source_url, 193 const AutofillMetrics& metric_logger, 194 const DialogType dialog_type, 195 const base::Callback<void(const FormStructure*, 196 const std::string&)>& callback) 197 : AutofillDialogControllerImpl(contents, 198 form_structure, 199 source_url, 200 dialog_type, 201 callback), 202 metric_logger_(metric_logger), 203 mock_wallet_client_( 204 Profile::FromBrowserContext(contents->GetBrowserContext())-> 205 GetRequestContext(), this), 206 dialog_type_(dialog_type) {} 207 virtual ~TestAutofillDialogController() {} 208 209 virtual AutofillDialogView* CreateView() OVERRIDE { 210 return new testing::NiceMock<TestAutofillDialogView>(); 211 } 212 213 void Init(content::BrowserContext* browser_context) { 214 test_manager_.Init(browser_context); 215 } 216 217 TestAutofillDialogView* GetView() { 218 return static_cast<TestAutofillDialogView*>(view()); 219 } 220 221 TestPersonalDataManager* GetTestingManager() { 222 return &test_manager_; 223 } 224 225 wallet::MockWalletClient* GetTestingWalletClient() { 226 return &mock_wallet_client_; 227 } 228 229 const GURL& open_tab_url() { return open_tab_url_; } 230 231 virtual DialogType GetDialogType() const OVERRIDE { 232 return dialog_type_; 233 } 234 235 void set_dialog_type(DialogType dialog_type) { dialog_type_ = dialog_type; } 236 237 void SimulateSigninError() { 238 OnWalletSigninError(); 239 } 240 241 bool AutocheckoutIsRunning() const { 242 return AUTOCHECKOUT_IN_PROGRESS == autocheckout_state(); 243 } 244 245 MOCK_METHOD0(LoadRiskFingerprintData, void()); 246 using AutofillDialogControllerImpl::OnDidLoadRiskFingerprintData; 247 using AutofillDialogControllerImpl::IsEditingExistingData; 248 249 protected: 250 virtual PersonalDataManager* GetManager() OVERRIDE { 251 return &test_manager_; 252 } 253 254 virtual wallet::WalletClient* GetWalletClient() OVERRIDE { 255 return &mock_wallet_client_; 256 } 257 258 virtual void OpenTabWithUrl(const GURL& url) OVERRIDE { 259 open_tab_url_ = url; 260 } 261 262 // Whether the information input in this dialog will be securely transmitted 263 // to the requesting site. 264 virtual bool TransmissionWillBeSecure() const OVERRIDE { 265 return true; 266 } 267 268 private: 269 // To specify our own metric logger. 270 virtual const AutofillMetrics& GetMetricLogger() const OVERRIDE { 271 return metric_logger_; 272 } 273 274 const AutofillMetrics& metric_logger_; 275 TestPersonalDataManager test_manager_; 276 testing::NiceMock<wallet::MockWalletClient> mock_wallet_client_; 277 GURL open_tab_url_; 278 DialogType dialog_type_; 279 280 DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogController); 281}; 282 283class TestAutofillCreditCardBubbleController : 284 public AutofillCreditCardBubbleController { 285 public: 286 explicit TestAutofillCreditCardBubbleController( 287 content::WebContents* contents) 288 : AutofillCreditCardBubbleController(contents), 289 bubble_(GetWeakPtr()) { 290 contents->SetUserData(UserDataKey(), this); 291 EXPECT_EQ(contents->GetUserData(UserDataKey()), this); 292 } 293 virtual ~TestAutofillCreditCardBubbleController() {} 294 295 MOCK_METHOD2(ShowAsGeneratedCardBubble, 296 void(const base::string16& backing_card_name, 297 const base::string16& fronting_card_name)); 298 MOCK_METHOD1(ShowAsNewCardSavedBubble, 299 void(const base::string16& newly_saved_card_name)); 300 301 protected: 302 virtual base::WeakPtr<AutofillCreditCardBubble> CreateBubble() OVERRIDE { 303 return bubble_.GetWeakPtr(); 304 } 305 306 virtual bool CanShow() const OVERRIDE { 307 return true; 308 } 309 310 private: 311 TestAutofillCreditCardBubble bubble_; 312 313 DISALLOW_COPY_AND_ASSIGN(TestAutofillCreditCardBubbleController); 314}; 315 316class AutofillDialogControllerTest : public ChromeRenderViewHostTestHarness { 317 protected: 318 AutofillDialogControllerTest(): form_structure_(NULL) {} 319 320 // testing::Test implementation: 321 virtual void SetUp() OVERRIDE { 322 ChromeRenderViewHostTestHarness::SetUp(); 323 profile()->CreateRequestContext(); 324 325 test_bubble_controller_ = 326 new testing::NiceMock<TestAutofillCreditCardBubbleController>( 327 web_contents()); 328 329 // Don't get stuck on the first run wallet interstitial. 330 profile()->GetPrefs()->SetBoolean(::prefs::kAutofillDialogHasPaidWithWallet, 331 true); 332 333 SetUpControllerWithFormData(DefaultFormData()); 334 } 335 336 virtual void TearDown() OVERRIDE { 337 if (controller_) 338 controller_->ViewClosed(); 339 ChromeRenderViewHostTestHarness::TearDown(); 340 } 341 342 void Reset() { 343 if (controller_) 344 controller_->ViewClosed(); 345 profile()->CreateRequestContext(); 346 347 test_bubble_controller_ = 348 new testing::NiceMock<TestAutofillCreditCardBubbleController>( 349 web_contents()); 350 351 // Don't get stuck on the first run wallet interstitial. 352 profile()->GetPrefs()->SetBoolean(::prefs::kAutofillDialogHasPaidWithWallet, 353 true); 354 355 SetUpControllerWithFormData(DefaultFormData()); 356 } 357 358 FormData DefaultFormData() { 359 FormData form_data; 360 for (size_t i = 0; i < arraysize(kFieldsFromPage); ++i) { 361 FormFieldData field; 362 field.autocomplete_attribute = kFieldsFromPage[i]; 363 form_data.fields.push_back(field); 364 } 365 return form_data; 366 } 367 368 void SetUpControllerWithFormData(const FormData& form_data) { 369 if (controller_) 370 controller_->ViewClosed(); 371 372 base::Callback<void(const FormStructure*, const std::string&)> callback = 373 base::Bind(&AutofillDialogControllerTest::FinishedCallback, 374 base::Unretained(this)); 375 controller_ = (new testing::NiceMock<TestAutofillDialogController>( 376 web_contents(), 377 form_data, 378 GURL(), 379 metric_logger_, 380 DIALOG_TYPE_REQUEST_AUTOCOMPLETE, 381 callback))->AsWeakPtr(); 382 controller_->Init(profile()); 383 controller_->Show(); 384 controller_->OnUserNameFetchSuccess(kFakeEmail); 385 } 386 387 void FillCreditCardInputs() { 388 DetailOutputMap cc_outputs; 389 const DetailInputs& cc_inputs = 390 controller()->RequestedFieldsForSection(SECTION_CC); 391 for (size_t i = 0; i < cc_inputs.size(); ++i) { 392 cc_outputs[&cc_inputs[i]] = cc_inputs[i].type == CREDIT_CARD_NUMBER ? 393 ASCIIToUTF16(kTestCCNumberVisa) : ASCIIToUTF16("11"); 394 } 395 controller()->GetView()->SetUserInput(SECTION_CC, cc_outputs); 396 } 397 398 std::vector<DialogNotification> NotificationsOfType( 399 DialogNotification::Type type) { 400 std::vector<DialogNotification> right_type; 401 const std::vector<DialogNotification>& notifications = 402 controller()->CurrentNotifications(); 403 for (size_t i = 0; i < notifications.size(); ++i) { 404 if (notifications[i].type() == type) 405 right_type.push_back(notifications[i]); 406 } 407 return right_type; 408 } 409 410 void SwitchToAutofill() { 411 controller_->MenuModelForAccountChooser()->ActivatedAt( 412 TestAccountChooserModel::kAutofillItemId); 413 } 414 415 void SwitchToWallet() { 416 controller_->MenuModelForAccountChooser()->ActivatedAt( 417 TestAccountChooserModel::kActiveWalletItemId); 418 } 419 420 void SimulateSigninError() { 421 controller_->SimulateSigninError(); 422 } 423 424 void UseBillingForShipping() { 425 controller()->MenuModelForSection(SECTION_SHIPPING)->ActivatedAt(0); 426 } 427 428 void ValidateCCNumber(DialogSection section, 429 const std::string& cc_number, 430 bool should_pass) { 431 DetailOutputMap outputs; 432 const DetailInputs& inputs = 433 controller()->RequestedFieldsForSection(section); 434 435 SetOutputValue(inputs, &outputs, CREDIT_CARD_NUMBER, cc_number); 436 ValidityData validity_data = 437 controller()->InputsAreValid(section, outputs, VALIDATE_FINAL); 438 EXPECT_EQ(should_pass ? 0U : 1U, validity_data.count(CREDIT_CARD_NUMBER)); 439 } 440 441 void SubmitWithWalletItems(scoped_ptr<wallet::WalletItems> wallet_items) { 442 controller()->OnDidGetWalletItems(wallet_items.Pass()); 443 AcceptAndLoadFakeFingerprint(); 444 } 445 446 void AcceptAndLoadFakeFingerprint() { 447 controller()->OnAccept(); 448 controller()->OnDidLoadRiskFingerprintData(GetFakeFingerprint().Pass()); 449 } 450 451 bool ReadSetVisuallyDeemphasizedIpc() { 452 EXPECT_EQ(1U, process()->sink().message_count()); 453 uint32 kMsgID = ChromeViewMsg_SetVisuallyDeemphasized::ID; 454 const IPC::Message* message = 455 process()->sink().GetFirstMessageMatching(kMsgID); 456 EXPECT_TRUE(message); 457 Tuple1<bool> payload; 458 ChromeViewMsg_SetVisuallyDeemphasized::Read(message, &payload); 459 process()->sink().ClearMessages(); 460 return payload.a; 461 } 462 463 TestAutofillDialogController* controller() { return controller_.get(); } 464 465 const FormStructure* form_structure() { return form_structure_; } 466 467 TestAutofillCreditCardBubbleController* test_bubble_controller() { 468 return test_bubble_controller_; 469 } 470 471 private: 472 void FinishedCallback(const FormStructure* form_structure, 473 const std::string& google_transaction_id) { 474 form_structure_ = form_structure; 475 if (controller()->GetDialogType() == DIALOG_TYPE_AUTOCHECKOUT) 476 EXPECT_TRUE(controller()->AutocheckoutIsRunning()); 477 } 478 479#if defined(OS_WIN) 480 // http://crbug.com/227221 481 ui::ScopedOleInitializer ole_initializer_; 482#endif 483 484 // The controller owns itself. 485 base::WeakPtr<TestAutofillDialogController> controller_; 486 487 // Must outlive the controller. 488 AutofillMetrics metric_logger_; 489 490 // Returned when the dialog closes successfully. 491 const FormStructure* form_structure_; 492 493 // Used to monitor if the Autofill credit card bubble is shown. Owned by 494 // |web_contents()|. 495 TestAutofillCreditCardBubbleController* test_bubble_controller_; 496}; 497 498} // namespace 499 500// This test makes sure nothing falls over when fields are being validity- 501// checked. 502TEST_F(AutofillDialogControllerTest, ValidityCheck) { 503 for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) { 504 DialogSection section = static_cast<DialogSection>(i); 505 const DetailInputs& shipping_inputs = 506 controller()->RequestedFieldsForSection(section); 507 for (DetailInputs::const_iterator iter = shipping_inputs.begin(); 508 iter != shipping_inputs.end(); ++iter) { 509 controller()->InputValidityMessage(section, iter->type, string16()); 510 } 511 } 512} 513 514// Test for phone number validation. 515TEST_F(AutofillDialogControllerTest, PhoneNumberValidation) { 516 // Construct DetailOutputMap from existing data. 517 SwitchToAutofill(); 518 519 AutofillProfile full_profile(test::GetVerifiedProfile()); 520 controller()->GetTestingManager()->AddTestingProfile(&full_profile); 521 522 for (size_t i = 0; i < 2; ++i) { 523 AutofillFieldType phone = i == 0 ? PHONE_HOME_WHOLE_NUMBER : 524 PHONE_BILLING_WHOLE_NUMBER; 525 AutofillFieldType address = i == 0 ? ADDRESS_HOME_COUNTRY : 526 ADDRESS_BILLING_COUNTRY; 527 DialogSection section = i == 0 ? SECTION_SHIPPING : SECTION_BILLING; 528 529 controller()->EditClickedForSection(section); 530 531 DetailOutputMap outputs; 532 const DetailInputs& inputs = 533 controller()->RequestedFieldsForSection(section); 534 // Make sure country is United States. 535 SetOutputValue(inputs, &outputs, address, "United States"); 536 537 // Existing data should have no errors. 538 ValidityData validity_data = 539 controller()->InputsAreValid(section, outputs, VALIDATE_FINAL); 540 EXPECT_EQ(0U, validity_data.count(phone)); 541 542 // Input an empty phone number with VALIDATE_FINAL. 543 SetOutputValue( inputs, &outputs, phone, ""); 544 validity_data = 545 controller()->InputsAreValid(section, outputs, VALIDATE_FINAL); 546 EXPECT_EQ(1U, validity_data.count(phone)); 547 548 // Input an empty phone number with VALIDATE_EDIT. 549 validity_data = 550 controller()->InputsAreValid(section, outputs, VALIDATE_EDIT); 551 EXPECT_EQ(0U, validity_data.count(phone)); 552 553 // Input an invalid phone number. 554 SetOutputValue(inputs, &outputs, phone, "ABC"); 555 validity_data = 556 controller()->InputsAreValid(section, outputs, VALIDATE_EDIT); 557 EXPECT_EQ(1U, validity_data.count(phone)); 558 559 // Input a local phone number. 560 SetOutputValue(inputs, &outputs, phone, "2155546699"); 561 validity_data = 562 controller()->InputsAreValid(section, outputs, VALIDATE_EDIT); 563 EXPECT_EQ(0U, validity_data.count(phone)); 564 565 // Input an invalid local phone number. 566 SetOutputValue(inputs, &outputs, phone, "215554669"); 567 validity_data = 568 controller()->InputsAreValid(section, outputs, VALIDATE_EDIT); 569 EXPECT_EQ(1U, validity_data.count(phone)); 570 571 // Input an international phone number. 572 SetOutputValue(inputs, &outputs, phone, "+33 892 70 12 39"); 573 validity_data = 574 controller()->InputsAreValid(section, outputs, VALIDATE_EDIT); 575 EXPECT_EQ(0U, validity_data.count(phone)); 576 577 // Input an invalid international phone number. 578 SetOutputValue(inputs, &outputs, phone, "+112333 892 70 12 39"); 579 validity_data = 580 controller()->InputsAreValid(section, outputs, VALIDATE_EDIT); 581 EXPECT_EQ(1U, validity_data.count(phone)); 582 } 583} 584 585TEST_F(AutofillDialogControllerTest, BillingNameValidation) { 586 // Construct DetailOutputMap from AutofillProfile data. 587 SwitchToAutofill(); 588 589 DetailOutputMap outputs; 590 const DetailInputs& inputs = 591 controller()->RequestedFieldsForSection(SECTION_BILLING); 592 593 // Input an empty billing name with VALIDATE_FINAL. 594 SetOutputValue(inputs, &outputs, NAME_FULL, ""); 595 ValidityData validity_data = 596 controller()->InputsAreValid(SECTION_BILLING, outputs, VALIDATE_FINAL); 597 EXPECT_EQ(1U, validity_data.count(NAME_FULL)); 598 599 // Input an empty billing name with VALIDATE_EDIT. 600 validity_data = 601 controller()->InputsAreValid(SECTION_BILLING, outputs, VALIDATE_EDIT); 602 EXPECT_EQ(0U, validity_data.count(NAME_FULL)); 603 604 // Input a non-empty billing name. 605 SetOutputValue(inputs, &outputs, NAME_FULL, "Bob"); 606 validity_data = 607 controller()->InputsAreValid(SECTION_BILLING, outputs, VALIDATE_FINAL); 608 EXPECT_EQ(0U, validity_data.count(NAME_FULL)); 609 610 // Switch to Wallet which only considers names with with at least two names to 611 // be valid. 612 SwitchToWallet(); 613 614 // Setup some wallet state. 615 scoped_ptr<wallet::WalletItems> wallet_items = wallet::GetTestWalletItems(); 616 controller()->OnDidGetWalletItems(wallet_items.Pass()); 617 618 DetailOutputMap wallet_outputs; 619 const DetailInputs& wallet_inputs = 620 controller()->RequestedFieldsForSection(SECTION_CC_BILLING); 621 622 // Input an empty billing name with VALIDATE_FINAL. Data source should not 623 // change this behavior. 624 SetOutputValue(wallet_inputs, &wallet_outputs, NAME_FULL, ""); 625 validity_data = 626 controller()->InputsAreValid( 627 SECTION_CC_BILLING, wallet_outputs, VALIDATE_FINAL); 628 EXPECT_EQ(1U, validity_data.count(NAME_FULL)); 629 630 // Input an empty billing name with VALIDATE_EDIT. Data source should not 631 // change this behavior. 632 validity_data = 633 controller()->InputsAreValid( 634 SECTION_CC_BILLING, wallet_outputs, VALIDATE_EDIT); 635 EXPECT_EQ(0U, validity_data.count(NAME_FULL)); 636 637 // Input a one name billing name. Wallet does not currently support this. 638 SetOutputValue(wallet_inputs, &wallet_outputs, NAME_FULL, "Bob"); 639 validity_data = 640 controller()->InputsAreValid( 641 SECTION_CC_BILLING, wallet_outputs, VALIDATE_FINAL); 642 EXPECT_EQ(1U, validity_data.count(NAME_FULL)); 643 644 // Input a two name billing name. 645 SetOutputValue(wallet_inputs, &wallet_outputs, NAME_FULL, 646 "Bob Barker"); 647 validity_data = 648 controller()->InputsAreValid( 649 SECTION_CC_BILLING, wallet_outputs, VALIDATE_FINAL); 650 EXPECT_EQ(0U, validity_data.count(NAME_FULL)); 651 652 // Input a more than two name billing name. 653 SetOutputValue(wallet_inputs, &wallet_outputs, NAME_FULL, 654 "John Jacob Jingleheimer Schmidt"); 655 validity_data = 656 controller()->InputsAreValid( 657 SECTION_CC_BILLING, wallet_outputs, VALIDATE_FINAL); 658 EXPECT_EQ(0U, validity_data.count(NAME_FULL)); 659 660 // Input a billing name with lots of crazy whitespace. 661 SetOutputValue(wallet_inputs, &wallet_outputs, NAME_FULL, 662 " \\n\\r John \\n Jacob Jingleheimer \\t Schmidt "); 663 validity_data = 664 controller()->InputsAreValid( 665 SECTION_CC_BILLING, wallet_outputs, VALIDATE_FINAL); 666 EXPECT_EQ(0U, validity_data.count(NAME_FULL)); 667} 668 669TEST_F(AutofillDialogControllerTest, CreditCardNumberValidation) { 670 // Construct DetailOutputMap from AutofillProfile data. 671 SwitchToAutofill(); 672 673 // Should accept AMEX, Visa, Master and Discover. 674 ValidateCCNumber(SECTION_CC, kTestCCNumberVisa, true); 675 ValidateCCNumber(SECTION_CC, kTestCCNumberMaster, true); 676 ValidateCCNumber(SECTION_CC, kTestCCNumberDiscover, true); 677 ValidateCCNumber(SECTION_CC, kTestCCNumberAmex, true); 678 679 ValidateCCNumber(SECTION_CC, kTestCCNumberIncomplete, false); 680 ValidateCCNumber(SECTION_CC, kTestCCNumberInvalid, false); 681 682 // Switch to Wallet which will not accept AMEX. 683 SwitchToWallet(); 684 685 // Setup some wallet state. 686 controller()->OnDidGetWalletItems(wallet::GetTestWalletItems()); 687 688 // Should accept Visa, Master and Discover, but not AMEX. 689 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberVisa, true); 690 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberMaster, true); 691 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberDiscover, true); 692 693 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberAmex, false); 694 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberIncomplete, false); 695 ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberInvalid, false); 696} 697 698TEST_F(AutofillDialogControllerTest, AutofillProfiles) { 699 ui::MenuModel* shipping_model = 700 controller()->MenuModelForSection(SECTION_SHIPPING); 701 // Since the PersonalDataManager is empty, this should only have the 702 // "use billing", "add new" and "manage" menu items. 703 ASSERT_TRUE(shipping_model); 704 EXPECT_EQ(3, shipping_model->GetItemCount()); 705 // On the other hand, the other models should be NULL when there's no 706 // suggestion. 707 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC)); 708 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_BILLING)); 709 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_EMAIL)); 710 711 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(3); 712 713 // Empty profiles are ignored. 714 AutofillProfile empty_profile(base::GenerateGUID(), kSettingsOrigin); 715 empty_profile.SetRawInfo(NAME_FULL, ASCIIToUTF16("John Doe")); 716 controller()->GetTestingManager()->AddTestingProfile(&empty_profile); 717 shipping_model = controller()->MenuModelForSection(SECTION_SHIPPING); 718 ASSERT_TRUE(shipping_model); 719 EXPECT_EQ(3, shipping_model->GetItemCount()); 720 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_EMAIL)); 721 722 // An otherwise full but unverified profile should be ignored. 723 AutofillProfile full_profile(test::GetFullProfile()); 724 full_profile.set_origin("https://www.example.com"); 725 full_profile.SetRawInfo(ADDRESS_HOME_LINE2, string16()); 726 controller()->GetTestingManager()->AddTestingProfile(&full_profile); 727 shipping_model = controller()->MenuModelForSection(SECTION_SHIPPING); 728 ASSERT_TRUE(shipping_model); 729 EXPECT_EQ(3, shipping_model->GetItemCount()); 730 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_EMAIL)); 731 732 // A full, verified profile should be picked up. 733 AutofillProfile verified_profile(test::GetVerifiedProfile()); 734 verified_profile.SetRawInfo(ADDRESS_HOME_LINE2, string16()); 735 controller()->GetTestingManager()->AddTestingProfile(&verified_profile); 736 shipping_model = controller()->MenuModelForSection(SECTION_SHIPPING); 737 ASSERT_TRUE(shipping_model); 738 EXPECT_EQ(4, shipping_model->GetItemCount()); 739 EXPECT_TRUE(!!controller()->MenuModelForSection(SECTION_EMAIL)); 740} 741 742// Makes sure that the choice of which Autofill profile to use for each section 743// is sticky. 744TEST_F(AutofillDialogControllerTest, AutofillProfileDefaults) { 745 AutofillProfile full_profile(test::GetFullProfile()); 746 full_profile.set_origin(kSettingsOrigin); 747 controller()->GetTestingManager()->AddTestingProfile(&full_profile); 748 AutofillProfile full_profile2(test::GetFullProfile2()); 749 full_profile2.set_origin(kSettingsOrigin); 750 controller()->GetTestingManager()->AddTestingProfile(&full_profile2); 751 752 // Until a selection has been made, the default shipping suggestion is the 753 // first one (after "use billing"). 754 SuggestionsMenuModel* shipping_model = static_cast<SuggestionsMenuModel*>( 755 controller()->MenuModelForSection(SECTION_SHIPPING)); 756 EXPECT_EQ(1, shipping_model->checked_item()); 757 758 for (int i = 2; i >= 0; --i) { 759 shipping_model = static_cast<SuggestionsMenuModel*>( 760 controller()->MenuModelForSection(SECTION_SHIPPING)); 761 shipping_model->ExecuteCommand(i, 0); 762 FillCreditCardInputs(); 763 controller()->OnAccept(); 764 765 Reset(); 766 controller()->GetTestingManager()->AddTestingProfile(&full_profile); 767 controller()->GetTestingManager()->AddTestingProfile(&full_profile2); 768 shipping_model = static_cast<SuggestionsMenuModel*>( 769 controller()->MenuModelForSection(SECTION_SHIPPING)); 770 EXPECT_EQ(i, shipping_model->checked_item()); 771 } 772 773 // Try again, but don't add the default profile to the PDM. The dialog 774 // should fall back to the first profile. 775 shipping_model->ExecuteCommand(2, 0); 776 FillCreditCardInputs(); 777 controller()->OnAccept(); 778 Reset(); 779 controller()->GetTestingManager()->AddTestingProfile(&full_profile); 780 shipping_model = static_cast<SuggestionsMenuModel*>( 781 controller()->MenuModelForSection(SECTION_SHIPPING)); 782 EXPECT_EQ(1, shipping_model->checked_item()); 783} 784 785TEST_F(AutofillDialogControllerTest, AutofillProfileVariants) { 786 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(1); 787 ui::MenuModel* email_model = 788 controller()->MenuModelForSection(SECTION_EMAIL); 789 EXPECT_FALSE(email_model); 790 791 // Set up some variant data. 792 AutofillProfile full_profile(test::GetVerifiedProfile()); 793 std::vector<string16> names; 794 names.push_back(ASCIIToUTF16("John Doe")); 795 names.push_back(ASCIIToUTF16("Jane Doe")); 796 full_profile.SetRawMultiInfo(EMAIL_ADDRESS, names); 797 const string16 kEmail1 = ASCIIToUTF16(kFakeEmail); 798 const string16 kEmail2 = ASCIIToUTF16("admin@example.com"); 799 std::vector<string16> emails; 800 emails.push_back(kEmail1); 801 emails.push_back(kEmail2); 802 full_profile.SetRawMultiInfo(EMAIL_ADDRESS, emails); 803 804 // Respect variants for the email address field only. 805 controller()->GetTestingManager()->AddTestingProfile(&full_profile); 806 ui::MenuModel* shipping_model = 807 controller()->MenuModelForSection(SECTION_SHIPPING); 808 EXPECT_EQ(4, shipping_model->GetItemCount()); 809 email_model = controller()->MenuModelForSection(SECTION_EMAIL); 810 ASSERT_TRUE(!!email_model); 811 EXPECT_EQ(4, email_model->GetItemCount()); 812 813 // The first one is the default. 814 SuggestionsMenuModel* email_suggestions = static_cast<SuggestionsMenuModel*>( 815 controller()->MenuModelForSection(SECTION_EMAIL)); 816 EXPECT_EQ(0, email_suggestions->checked_item()); 817 818 email_model->ActivatedAt(0); 819 EXPECT_EQ(kEmail1, 820 controller()->SuggestionStateForSection(SECTION_EMAIL).text); 821 email_model->ActivatedAt(1); 822 EXPECT_EQ(kEmail2, 823 controller()->SuggestionStateForSection(SECTION_EMAIL).text); 824 825 controller()->EditClickedForSection(SECTION_EMAIL); 826 const DetailInputs& inputs = 827 controller()->RequestedFieldsForSection(SECTION_EMAIL); 828 EXPECT_EQ(kEmail2, inputs[0].initial_value); 829 830 // The choice of variant is persisted across runs of the dialog. 831 email_model->ActivatedAt(0); 832 email_model->ActivatedAt(1); 833 FillCreditCardInputs(); 834 controller()->OnAccept(); 835 836 Reset(); 837 controller()->GetTestingManager()->AddTestingProfile(&full_profile); 838 email_suggestions = static_cast<SuggestionsMenuModel*>( 839 controller()->MenuModelForSection(SECTION_EMAIL)); 840 EXPECT_EQ(1, email_suggestions->checked_item()); 841} 842 843TEST_F(AutofillDialogControllerTest, SuggestValidEmail) { 844 AutofillProfile profile(test::GetVerifiedProfile()); 845 const string16 kValidEmail = ASCIIToUTF16(kFakeEmail); 846 profile.SetRawInfo(EMAIL_ADDRESS, kValidEmail); 847 controller()->GetTestingManager()->AddTestingProfile(&profile); 848 849 controller()->MenuModelForSection(SECTION_EMAIL)->ActivatedAt(0); 850 EXPECT_EQ(kValidEmail, 851 controller()->SuggestionStateForSection(SECTION_EMAIL).text); 852} 853 854TEST_F(AutofillDialogControllerTest, DoNotSuggestInvalidEmail) { 855 AutofillProfile profile(test::GetVerifiedProfile()); 856 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16(".!#$%&'*+/=?^_`-@-..")); 857 controller()->GetTestingManager()->AddTestingProfile(&profile); 858 EXPECT_EQ(static_cast<ui::MenuModel*>(NULL), 859 controller()->MenuModelForSection(SECTION_EMAIL)); 860} 861 862TEST_F(AutofillDialogControllerTest, DoNotSuggestEmailFromIncompleteProfile) { 863 AutofillProfile profile(test::GetVerifiedProfile()); 864 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16(kFakeEmail)); 865 profile.SetRawInfo(ADDRESS_HOME_STATE, base::string16()); 866 controller()->GetTestingManager()->AddTestingProfile(&profile); 867 EXPECT_EQ(static_cast<ui::MenuModel*>(NULL), 868 controller()->MenuModelForSection(SECTION_EMAIL)); 869} 870 871TEST_F(AutofillDialogControllerTest, DoNotSuggestEmailFromInvalidProfile) { 872 AutofillProfile profile(test::GetVerifiedProfile()); 873 profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16(kFakeEmail)); 874 profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("C")); 875 controller()->GetTestingManager()->AddTestingProfile(&profile); 876 EXPECT_EQ(static_cast<ui::MenuModel*>(NULL), 877 controller()->MenuModelForSection(SECTION_EMAIL)); 878} 879 880TEST_F(AutofillDialogControllerTest, SuggestValidAddress) { 881 AutofillProfile full_profile(test::GetVerifiedProfile()); 882 full_profile.set_origin(kSettingsOrigin); 883 controller()->GetTestingManager()->AddTestingProfile(&full_profile); 884 EXPECT_EQ( 885 4, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount()); 886} 887 888TEST_F(AutofillDialogControllerTest, DoNotSuggestInvalidAddress) { 889 AutofillProfile full_profile(test::GetVerifiedProfile()); 890 full_profile.set_origin(kSettingsOrigin); 891 full_profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("C")); 892 controller()->GetTestingManager()->AddTestingProfile(&full_profile); 893 EXPECT_EQ( 894 3, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount()); 895} 896 897TEST_F(AutofillDialogControllerTest, AutofillCreditCards) { 898 // Since the PersonalDataManager is empty, this should only have the 899 // default menu items. 900 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC)); 901 902 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(3); 903 904 // Empty cards are ignored. 905 CreditCard empty_card(base::GenerateGUID(), kSettingsOrigin); 906 empty_card.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("John Doe")); 907 controller()->GetTestingManager()->AddTestingCreditCard(&empty_card); 908 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC)); 909 910 // An otherwise full but unverified card should be ignored. 911 CreditCard full_card(test::GetCreditCard()); 912 full_card.set_origin("https://www.example.com"); 913 controller()->GetTestingManager()->AddTestingCreditCard(&full_card); 914 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC)); 915 916 // A full, verified card should be picked up. 917 CreditCard verified_card(test::GetCreditCard()); 918 verified_card.set_origin(kSettingsOrigin); 919 controller()->GetTestingManager()->AddTestingCreditCard(&verified_card); 920 ui::MenuModel* credit_card_model = 921 controller()->MenuModelForSection(SECTION_CC); 922 ASSERT_TRUE(credit_card_model); 923 EXPECT_EQ(3, credit_card_model->GetItemCount()); 924} 925 926// Test selecting a shipping address different from billing as address. 927TEST_F(AutofillDialogControllerTest, DontUseBillingAsShipping) { 928 AutofillProfile full_profile(test::GetVerifiedProfile()); 929 AutofillProfile full_profile2(test::GetVerifiedProfile2()); 930 CreditCard credit_card(test::GetVerifiedCreditCard()); 931 controller()->GetTestingManager()->AddTestingProfile(&full_profile); 932 controller()->GetTestingManager()->AddTestingProfile(&full_profile2); 933 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card); 934 ui::MenuModel* shipping_model = 935 controller()->MenuModelForSection(SECTION_SHIPPING); 936 shipping_model->ActivatedAt(2); 937 938 controller()->OnAccept(); 939 ASSERT_EQ(20U, form_structure()->field_count()); 940 EXPECT_EQ(ADDRESS_BILLING_STATE, form_structure()->field(9)->type()); 941 EXPECT_EQ(ADDRESS_HOME_STATE, form_structure()->field(16)->type()); 942 string16 billing_state = form_structure()->field(9)->value; 943 string16 shipping_state = form_structure()->field(16)->value; 944 EXPECT_FALSE(billing_state.empty()); 945 EXPECT_FALSE(shipping_state.empty()); 946 EXPECT_NE(billing_state, shipping_state); 947 948 EXPECT_EQ(CREDIT_CARD_NAME, form_structure()->field(1)->type()); 949 string16 cc_name = form_structure()->field(1)->value; 950 EXPECT_EQ(CREDIT_CARD_NAME, form_structure()->field(6)->type()); 951 string16 billing_name = form_structure()->field(6)->value; 952 EXPECT_EQ(NAME_FULL, form_structure()->field(13)->type()); 953 string16 shipping_name = form_structure()->field(13)->value; 954 955 EXPECT_FALSE(cc_name.empty()); 956 EXPECT_FALSE(billing_name.empty()); 957 EXPECT_FALSE(shipping_name.empty()); 958 // Billing name should always be the same as cardholder name. 959 EXPECT_EQ(cc_name, billing_name); 960 EXPECT_NE(cc_name, shipping_name); 961} 962 963// Test selecting UseBillingForShipping. 964TEST_F(AutofillDialogControllerTest, UseBillingAsShipping) { 965 AutofillProfile full_profile(test::GetVerifiedProfile()); 966 AutofillProfile full_profile2(test::GetVerifiedProfile2()); 967 CreditCard credit_card(test::GetVerifiedCreditCard()); 968 controller()->GetTestingManager()->AddTestingProfile(&full_profile); 969 controller()->GetTestingManager()->AddTestingProfile(&full_profile2); 970 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card); 971 972 // Test after setting use billing for shipping. 973 UseBillingForShipping(); 974 975 controller()->OnAccept(); 976 ASSERT_EQ(20U, form_structure()->field_count()); 977 EXPECT_EQ(ADDRESS_BILLING_STATE, form_structure()->field(9)->type()); 978 EXPECT_EQ(ADDRESS_HOME_STATE, form_structure()->field(16)->type()); 979 string16 billing_state = form_structure()->field(9)->value; 980 string16 shipping_state = form_structure()->field(16)->value; 981 EXPECT_FALSE(billing_state.empty()); 982 EXPECT_FALSE(shipping_state.empty()); 983 EXPECT_EQ(billing_state, shipping_state); 984 985 EXPECT_EQ(CREDIT_CARD_NAME, form_structure()->field(1)->type()); 986 string16 cc_name = form_structure()->field(1)->value; 987 EXPECT_EQ(CREDIT_CARD_NAME, form_structure()->field(6)->type()); 988 string16 billing_name = form_structure()->field(6)->value; 989 EXPECT_EQ(NAME_FULL, form_structure()->field(13)->type()); 990 string16 shipping_name = form_structure()->field(13)->value; 991 992 EXPECT_FALSE(cc_name.empty()); 993 EXPECT_FALSE(billing_name.empty()); 994 EXPECT_FALSE(shipping_name.empty()); 995 EXPECT_EQ(cc_name, billing_name); 996 EXPECT_EQ(cc_name, shipping_name); 997} 998 999// Tests that shipping and billing telephone fields are supported, and filled 1000// in by their respective profiles. http://crbug.com/244515 1001TEST_F(AutofillDialogControllerTest, BillingVsShippingPhoneNumber) { 1002 FormFieldData shipping_tel; 1003 shipping_tel.autocomplete_attribute = "shipping tel"; 1004 FormFieldData billing_tel; 1005 billing_tel.autocomplete_attribute = "billing tel"; 1006 1007 FormData form_data; 1008 form_data.fields.push_back(shipping_tel); 1009 form_data.fields.push_back(billing_tel); 1010 SetUpControllerWithFormData(form_data); 1011 1012 // The profile that will be chosen for the shipping section. 1013 AutofillProfile shipping_profile(test::GetVerifiedProfile()); 1014 // The profile that will be chosen for the billing section. 1015 AutofillProfile billing_profile(test::GetVerifiedProfile2()); 1016 CreditCard credit_card(test::GetVerifiedCreditCard()); 1017 controller()->GetTestingManager()->AddTestingProfile(&shipping_profile); 1018 controller()->GetTestingManager()->AddTestingProfile(&billing_profile); 1019 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card); 1020 ui::MenuModel* billing_model = 1021 controller()->MenuModelForSection(SECTION_BILLING); 1022 billing_model->ActivatedAt(1); 1023 1024 controller()->OnAccept(); 1025 ASSERT_EQ(2U, form_structure()->field_count()); 1026 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER, form_structure()->field(0)->type()); 1027 EXPECT_EQ(PHONE_BILLING_WHOLE_NUMBER, form_structure()->field(1)->type()); 1028 EXPECT_EQ(shipping_profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER), 1029 form_structure()->field(0)->value); 1030 EXPECT_EQ(billing_profile.GetRawInfo(PHONE_BILLING_WHOLE_NUMBER), 1031 form_structure()->field(1)->value); 1032 EXPECT_NE(form_structure()->field(1)->value, 1033 form_structure()->field(0)->value); 1034} 1035 1036TEST_F(AutofillDialogControllerTest, AcceptLegalDocuments) { 1037 EXPECT_CALL(*controller()->GetTestingWalletClient(), 1038 AcceptLegalDocuments(_, _, _)).Times(1); 1039 EXPECT_CALL(*controller()->GetTestingWalletClient(), 1040 GetFullWallet(_)).Times(1); 1041 EXPECT_CALL(*controller(), LoadRiskFingerprintData()).Times(1); 1042 1043 scoped_ptr<wallet::WalletItems> wallet_items = CompleteAndValidWalletItems(); 1044 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument()); 1045 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1046 controller()->OnAccept(); 1047 controller()->OnDidAcceptLegalDocuments(); 1048 controller()->OnDidLoadRiskFingerprintData(GetFakeFingerprint().Pass()); 1049} 1050 1051// Makes sure the default object IDs are respected. 1052TEST_F(AutofillDialogControllerTest, WalletDefaultItems) { 1053 scoped_ptr<wallet::WalletItems> wallet_items = wallet::GetTestWalletItems(); 1054 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument()); 1055 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument()); 1056 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument()); 1057 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument()); 1058 1059 wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress()); 1060 wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress()); 1061 wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress()); 1062 wallet_items->AddAddress(wallet::GetTestShippingAddress()); 1063 wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress()); 1064 1065 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1066 // "add", "manage", and 4 suggestions. 1067 EXPECT_EQ(6, 1068 controller()->MenuModelForSection(SECTION_CC_BILLING)->GetItemCount()); 1069 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING)-> 1070 IsItemCheckedAt(2)); 1071 ASSERT_FALSE(controller()->IsEditingExistingData(SECTION_CC_BILLING)); 1072 // "use billing", "add", "manage", and 5 suggestions. 1073 EXPECT_EQ(8, 1074 controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount()); 1075 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_SHIPPING)-> 1076 IsItemCheckedAt(4)); 1077 ASSERT_FALSE(controller()->IsEditingExistingData(SECTION_SHIPPING)); 1078} 1079 1080// Tests that invalid and AMEX default instruments are ignored. 1081TEST_F(AutofillDialogControllerTest, SelectInstrument) { 1082 scoped_ptr<wallet::WalletItems> wallet_items = wallet::GetTestWalletItems(); 1083 // Tests if default instrument is invalid, then, the first valid instrument is 1084 // selected instead of the default instrument. 1085 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument()); 1086 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument()); 1087 wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentInvalid()); 1088 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument()); 1089 1090 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1091 // 4 suggestions and "add", "manage". 1092 EXPECT_EQ(6, 1093 controller()->MenuModelForSection(SECTION_CC_BILLING)->GetItemCount()); 1094 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING)-> 1095 IsItemCheckedAt(0)); 1096 1097 // Tests if default instrument is AMEX, then, the first valid instrument is 1098 // selected instead of the default instrument. 1099 wallet_items = wallet::GetTestWalletItems(); 1100 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument()); 1101 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument()); 1102 wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentAmex()); 1103 wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument()); 1104 1105 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1106 // 4 suggestions and "add", "manage". 1107 EXPECT_EQ(6, 1108 controller()->MenuModelForSection(SECTION_CC_BILLING)->GetItemCount()); 1109 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING)-> 1110 IsItemCheckedAt(0)); 1111 1112 // Tests if only have AMEX and invalid instrument, then "add" is selected. 1113 wallet_items = wallet::GetTestWalletItems(); 1114 wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentInvalid()); 1115 wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentAmex()); 1116 1117 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1118 // 2 suggestions and "add", "manage". 1119 EXPECT_EQ(4, 1120 controller()->MenuModelForSection(SECTION_CC_BILLING)->GetItemCount()); 1121 // "add" 1122 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING)-> 1123 IsItemCheckedAt(2)); 1124} 1125 1126TEST_F(AutofillDialogControllerTest, SaveAddress) { 1127 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(1); 1128 EXPECT_CALL(*controller()->GetTestingWalletClient(), 1129 SaveToWalletMock(testing::IsNull(), 1130 testing::NotNull(), 1131 _)).Times(1); 1132 1133 scoped_ptr<wallet::WalletItems> wallet_items = wallet::GetTestWalletItems(); 1134 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument()); 1135 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1136 // If there is no shipping address in wallet, it will default to 1137 // "same-as-billing" instead of "add-new-item". "same-as-billing" is covered 1138 // by the following tests. The last item in the menu is "add-new-item". 1139 ui::MenuModel* shipping_model = 1140 controller()->MenuModelForSection(SECTION_SHIPPING); 1141 shipping_model->ActivatedAt(shipping_model->GetItemCount() - 1); 1142 AcceptAndLoadFakeFingerprint(); 1143} 1144 1145TEST_F(AutofillDialogControllerTest, SaveInstrument) { 1146 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(1); 1147 EXPECT_CALL(*controller()->GetTestingWalletClient(), 1148 SaveToWalletMock(testing::NotNull(), 1149 testing::IsNull(), 1150 _)).Times(1); 1151 1152 scoped_ptr<wallet::WalletItems> wallet_items = wallet::GetTestWalletItems(); 1153 wallet_items->AddAddress(wallet::GetTestShippingAddress()); 1154 SubmitWithWalletItems(wallet_items.Pass()); 1155} 1156 1157TEST_F(AutofillDialogControllerTest, SaveInstrumentWithInvalidInstruments) { 1158 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(1); 1159 EXPECT_CALL(*controller()->GetTestingWalletClient(), 1160 SaveToWalletMock(testing::NotNull(), 1161 testing::IsNull(), 1162 _)).Times(1); 1163 1164 scoped_ptr<wallet::WalletItems> wallet_items = wallet::GetTestWalletItems(); 1165 wallet_items->AddAddress(wallet::GetTestShippingAddress()); 1166 wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentInvalid()); 1167 SubmitWithWalletItems(wallet_items.Pass()); 1168} 1169 1170TEST_F(AutofillDialogControllerTest, SaveInstrumentAndAddress) { 1171 EXPECT_CALL(*controller()->GetTestingWalletClient(), 1172 SaveToWalletMock(testing::NotNull(), 1173 testing::NotNull(), 1174 _)).Times(1); 1175 1176 controller()->OnDidGetWalletItems(wallet::GetTestWalletItems()); 1177 AcceptAndLoadFakeFingerprint(); 1178} 1179 1180MATCHER(IsUpdatingExistingData, "updating existing Wallet data") { 1181 return !arg->object_id().empty(); 1182} 1183 1184// Tests that editing an address (in wallet mode0 and submitting the dialog 1185// should update the existing address on the server via WalletClient. 1186TEST_F(AutofillDialogControllerTest, UpdateAddress) { 1187 EXPECT_CALL(*controller()->GetTestingWalletClient(), 1188 SaveToWalletMock(testing::IsNull(), 1189 IsUpdatingExistingData(), 1190 _)).Times(1); 1191 1192 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems()); 1193 1194 controller()->EditClickedForSection(SECTION_SHIPPING); 1195 AcceptAndLoadFakeFingerprint(); 1196} 1197 1198// Tests that editing an instrument (CC + address) in wallet mode updates an 1199// existing instrument on the server via WalletClient. 1200TEST_F(AutofillDialogControllerTest, UpdateInstrument) { 1201 EXPECT_CALL(*controller()->GetTestingWalletClient(), 1202 SaveToWalletMock(IsUpdatingExistingData(), 1203 testing::IsNull(), 1204 _)).Times(1); 1205 1206 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems()); 1207 1208 controller()->EditClickedForSection(SECTION_CC_BILLING); 1209 AcceptAndLoadFakeFingerprint(); 1210} 1211 1212// Test that a user is able to edit their instrument and add a new address in 1213// the same submission. 1214TEST_F(AutofillDialogControllerTest, UpdateInstrumentSaveAddress) { 1215 EXPECT_CALL(*controller()->GetTestingWalletClient(), 1216 SaveToWalletMock(IsUpdatingExistingData(), 1217 testing::NotNull(), 1218 _)).Times(1); 1219 1220 scoped_ptr<wallet::WalletItems> wallet_items = wallet::GetTestWalletItems(); 1221 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument()); 1222 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1223 1224 controller()->EditClickedForSection(SECTION_CC_BILLING); 1225 AcceptAndLoadFakeFingerprint(); 1226} 1227 1228// Test that saving a new instrument and editing an address works. 1229TEST_F(AutofillDialogControllerTest, SaveInstrumentUpdateAddress) { 1230 EXPECT_CALL(*controller()->GetTestingWalletClient(), 1231 SaveToWalletMock(testing::NotNull(), 1232 IsUpdatingExistingData(), 1233 _)).Times(1); 1234 1235 scoped_ptr<wallet::WalletItems> wallet_items = wallet::GetTestWalletItems(); 1236 wallet_items->AddAddress(wallet::GetTestShippingAddress()); 1237 1238 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1239 1240 controller()->EditClickedForSection(SECTION_SHIPPING); 1241 AcceptAndLoadFakeFingerprint(); 1242} 1243 1244MATCHER(UsesLocalBillingAddress, "uses the local billing address") { 1245 return arg->address_line_1() == ASCIIToUTF16(kEditedBillingAddress); 1246} 1247 1248// Tests that when using billing address for shipping, and there is no exact 1249// matched shipping address, then a shipping address should be added. 1250TEST_F(AutofillDialogControllerTest, BillingForShipping) { 1251 EXPECT_CALL(*controller()->GetTestingWalletClient(), 1252 SaveToWalletMock(testing::IsNull(), 1253 testing::NotNull(), 1254 _)).Times(1); 1255 1256 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems()); 1257 // Select "Same as billing" in the address menu. 1258 UseBillingForShipping(); 1259 1260 AcceptAndLoadFakeFingerprint(); 1261} 1262 1263// Tests that when using billing address for shipping, and there is an exact 1264// matched shipping address, then a shipping address should not be added. 1265TEST_F(AutofillDialogControllerTest, BillingForShippingHasMatch) { 1266 EXPECT_CALL(*controller()->GetTestingWalletClient(), 1267 SaveToWalletMock(_, _, _)).Times(0); 1268 1269 scoped_ptr<wallet::WalletItems> wallet_items = wallet::GetTestWalletItems(); 1270 scoped_ptr<wallet::WalletItems::MaskedInstrument> instrument = 1271 wallet::GetTestMaskedInstrument(); 1272 // Copy billing address as shipping address, and assign an id to it. 1273 scoped_ptr<wallet::Address> shipping_address( 1274 new wallet::Address(instrument->address())); 1275 shipping_address->set_object_id("shipping_address_id"); 1276 wallet_items->AddAddress(shipping_address.Pass()); 1277 wallet_items->AddInstrument(instrument.Pass()); 1278 wallet_items->AddAddress(wallet::GetTestShippingAddress()); 1279 1280 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1281 // Select "Same as billing" in the address menu. 1282 UseBillingForShipping(); 1283 1284 AcceptAndLoadFakeFingerprint(); 1285} 1286 1287// Test that the local view contents is used when saving a new instrument and 1288// the user has selected "Same as billing". 1289TEST_F(AutofillDialogControllerTest, SaveInstrumentSameAsBilling) { 1290 scoped_ptr<wallet::WalletItems> wallet_items = wallet::GetTestWalletItems(); 1291 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument()); 1292 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1293 1294 controller()->EditClickedForSection(SECTION_CC_BILLING); 1295 controller()->OnAccept(); 1296 1297 DetailOutputMap outputs; 1298 const DetailInputs& inputs = 1299 controller()->RequestedFieldsForSection(SECTION_CC_BILLING); 1300 for (size_t i = 0; i < inputs.size(); ++i) { 1301 const DetailInput& input = inputs[i]; 1302 outputs[&input] = input.type == ADDRESS_BILLING_LINE1 ? 1303 ASCIIToUTF16(kEditedBillingAddress) : input.initial_value; 1304 } 1305 controller()->GetView()->SetUserInput(SECTION_CC_BILLING, outputs); 1306 1307 EXPECT_CALL(*controller()->GetTestingWalletClient(), 1308 SaveToWalletMock(testing::NotNull(), 1309 UsesLocalBillingAddress(), 1310 _)).Times(1); 1311 AcceptAndLoadFakeFingerprint(); 1312} 1313 1314TEST_F(AutofillDialogControllerTest, CancelNoSave) { 1315 EXPECT_CALL(*controller()->GetTestingWalletClient(), 1316 SaveToWalletMock(_, _, _)).Times(0); 1317 1318 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(1); 1319 1320 controller()->OnDidGetWalletItems(wallet::GetTestWalletItems()); 1321 controller()->OnCancel(); 1322} 1323 1324// Checks that clicking the Manage menu item opens a new tab with a different 1325// URL for Wallet and Autofill. 1326TEST_F(AutofillDialogControllerTest, ManageItem) { 1327 AutofillProfile full_profile(test::GetVerifiedProfile()); 1328 full_profile.set_origin(kSettingsOrigin); 1329 full_profile.SetRawInfo(ADDRESS_HOME_LINE2, string16()); 1330 controller()->GetTestingManager()->AddTestingProfile(&full_profile); 1331 SwitchToAutofill(); 1332 1333 SuggestionsMenuModel* shipping = static_cast<SuggestionsMenuModel*>( 1334 controller()->MenuModelForSection(SECTION_SHIPPING)); 1335 shipping->ExecuteCommand(shipping->GetItemCount() - 1, 0); 1336 GURL autofill_manage_url = controller()->open_tab_url(); 1337 EXPECT_EQ("chrome", autofill_manage_url.scheme()); 1338 1339 SwitchToWallet(); 1340 scoped_ptr<wallet::WalletItems> wallet_items = wallet::GetTestWalletItems(); 1341 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument()); 1342 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1343 1344 controller()->SuggestionItemSelected(shipping, shipping->GetItemCount() - 1); 1345 GURL wallet_manage_addresses_url = controller()->open_tab_url(); 1346 EXPECT_EQ("https", wallet_manage_addresses_url.scheme()); 1347 1348 SuggestionsMenuModel* billing = static_cast<SuggestionsMenuModel*>( 1349 controller()->MenuModelForSection(SECTION_CC_BILLING)); 1350 controller()->SuggestionItemSelected(billing, billing->GetItemCount() - 1); 1351 GURL wallet_manage_instruments_url = controller()->open_tab_url(); 1352 EXPECT_EQ("https", wallet_manage_instruments_url.scheme()); 1353 1354 EXPECT_NE(autofill_manage_url, wallet_manage_instruments_url); 1355 EXPECT_NE(wallet_manage_instruments_url, wallet_manage_addresses_url); 1356} 1357 1358TEST_F(AutofillDialogControllerTest, EditClickedCancelled) { 1359 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(1); 1360 1361 AutofillProfile full_profile(test::GetVerifiedProfile()); 1362 const string16 kEmail = ASCIIToUTF16("first@johndoe.com"); 1363 full_profile.SetRawInfo(EMAIL_ADDRESS, kEmail); 1364 controller()->GetTestingManager()->AddTestingProfile(&full_profile); 1365 1366 ui::MenuModel* email_model = 1367 controller()->MenuModelForSection(SECTION_EMAIL); 1368 EXPECT_EQ(3, email_model->GetItemCount()); 1369 1370 // When unedited, the initial_value should be empty. 1371 email_model->ActivatedAt(0); 1372 const DetailInputs& inputs0 = 1373 controller()->RequestedFieldsForSection(SECTION_EMAIL); 1374 EXPECT_EQ(string16(), inputs0[0].initial_value); 1375 EXPECT_EQ(kEmail, 1376 controller()->SuggestionStateForSection(SECTION_EMAIL).text); 1377 1378 // When edited, the initial_value should contain the value. 1379 controller()->EditClickedForSection(SECTION_EMAIL); 1380 const DetailInputs& inputs1 = 1381 controller()->RequestedFieldsForSection(SECTION_EMAIL); 1382 EXPECT_EQ(kEmail, inputs1[0].initial_value); 1383 EXPECT_EQ(string16(), 1384 controller()->SuggestionStateForSection(SECTION_EMAIL).text); 1385 1386 // When edit is cancelled, the initial_value should be empty. 1387 controller()->EditCancelledForSection(SECTION_EMAIL); 1388 const DetailInputs& inputs2 = 1389 controller()->RequestedFieldsForSection(SECTION_EMAIL); 1390 EXPECT_EQ(kEmail, 1391 controller()->SuggestionStateForSection(SECTION_EMAIL).text); 1392 EXPECT_EQ(string16(), inputs2[0].initial_value); 1393} 1394 1395// Tests that editing an autofill profile and then submitting works. 1396TEST_F(AutofillDialogControllerTest, EditAutofillProfile) { 1397 SwitchToAutofill(); 1398 1399 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(1); 1400 1401 AutofillProfile full_profile(test::GetVerifiedProfile()); 1402 controller()->GetTestingManager()->AddTestingProfile(&full_profile); 1403 controller()->EditClickedForSection(SECTION_SHIPPING); 1404 1405 DetailOutputMap outputs; 1406 const DetailInputs& inputs = 1407 controller()->RequestedFieldsForSection(SECTION_SHIPPING); 1408 for (size_t i = 0; i < inputs.size(); ++i) { 1409 const DetailInput& input = inputs[i]; 1410 outputs[&input] = input.type == NAME_FULL ? ASCIIToUTF16("Edited Name") : 1411 input.initial_value; 1412 } 1413 controller()->GetView()->SetUserInput(SECTION_SHIPPING, outputs); 1414 1415 // We also have to simulate CC inputs to keep the controller happy. 1416 FillCreditCardInputs(); 1417 1418 controller()->OnAccept(); 1419 const AutofillProfile& edited_profile = 1420 controller()->GetTestingManager()->imported_profile(); 1421 1422 for (size_t i = 0; i < inputs.size(); ++i) { 1423 const DetailInput& input = inputs[i]; 1424 EXPECT_EQ(input.type == NAME_FULL ? ASCIIToUTF16("Edited Name") : 1425 input.initial_value, 1426 edited_profile.GetInfo(input.type, "en-US")); 1427 } 1428} 1429 1430// Tests that adding an autofill profile and then submitting works. 1431TEST_F(AutofillDialogControllerTest, AddAutofillProfile) { 1432 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(2); 1433 1434 AutofillProfile full_profile(test::GetVerifiedProfile()); 1435 CreditCard credit_card(test::GetVerifiedCreditCard()); 1436 controller()->GetTestingManager()->AddTestingProfile(&full_profile); 1437 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card); 1438 1439 ui::MenuModel* model = controller()->MenuModelForSection(SECTION_BILLING); 1440 // Activate the "Add billing address" menu item. 1441 model->ActivatedAt(model->GetItemCount() - 2); 1442 1443 // Fill in the inputs from the profile. 1444 DetailOutputMap outputs; 1445 const DetailInputs& inputs = 1446 controller()->RequestedFieldsForSection(SECTION_BILLING); 1447 AutofillProfile full_profile2(test::GetVerifiedProfile2()); 1448 for (size_t i = 0; i < inputs.size(); ++i) { 1449 const DetailInput& input = inputs[i]; 1450 outputs[&input] = full_profile2.GetInfo(input.type, "en-US"); 1451 } 1452 controller()->GetView()->SetUserInput(SECTION_BILLING, outputs); 1453 1454 controller()->OnAccept(); 1455 const AutofillProfile& added_profile = 1456 controller()->GetTestingManager()->imported_profile(); 1457 1458 const DetailInputs& shipping_inputs = 1459 controller()->RequestedFieldsForSection(SECTION_SHIPPING); 1460 for (size_t i = 0; i < shipping_inputs.size(); ++i) { 1461 const DetailInput& input = shipping_inputs[i]; 1462 EXPECT_EQ(full_profile2.GetInfo(input.type, "en-US"), 1463 added_profile.GetInfo(input.type, "en-US")); 1464 } 1465 1466 // Also, the currently selected email address should get added to the new 1467 // profile. 1468 string16 original_email = full_profile.GetInfo(EMAIL_ADDRESS, "en-US"); 1469 EXPECT_FALSE(original_email.empty()); 1470 EXPECT_EQ(original_email, 1471 added_profile.GetInfo(EMAIL_ADDRESS, "en-US")); 1472} 1473 1474// Makes sure that a newly added email address gets added to an existing profile 1475// (as opposed to creating its own profile). http://crbug.com/240926 1476TEST_F(AutofillDialogControllerTest, AddEmail) { 1477 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(1); 1478 1479 AutofillProfile full_profile(test::GetVerifiedProfile()); 1480 controller()->GetTestingManager()->AddTestingProfile(&full_profile); 1481 1482 ui::MenuModel* model = controller()->MenuModelForSection(SECTION_EMAIL); 1483 ASSERT_TRUE(model); 1484 // Activate the "Add email address" menu item. 1485 model->ActivatedAt(model->GetItemCount() - 2); 1486 1487 // Fill in the inputs from the profile. 1488 DetailOutputMap outputs; 1489 const DetailInputs& inputs = 1490 controller()->RequestedFieldsForSection(SECTION_EMAIL); 1491 const DetailInput& input = inputs[0]; 1492 string16 new_email = ASCIIToUTF16("addemailtest@example.com"); 1493 outputs[&input] = new_email; 1494 controller()->GetView()->SetUserInput(SECTION_EMAIL, outputs); 1495 1496 FillCreditCardInputs(); 1497 controller()->OnAccept(); 1498 std::vector<base::string16> email_values; 1499 full_profile.GetMultiInfo(EMAIL_ADDRESS, "en-US", &email_values); 1500 ASSERT_EQ(2U, email_values.size()); 1501 EXPECT_EQ(new_email, email_values[1]); 1502} 1503 1504TEST_F(AutofillDialogControllerTest, VerifyCvv) { 1505 EXPECT_CALL(*controller()->GetTestingWalletClient(), 1506 GetFullWallet(_)).Times(1); 1507 EXPECT_CALL(*controller()->GetTestingWalletClient(), 1508 AuthenticateInstrument(_, _)).Times(1); 1509 1510 SubmitWithWalletItems(CompleteAndValidWalletItems()); 1511 1512 EXPECT_TRUE(NotificationsOfType(DialogNotification::REQUIRED_ACTION).empty()); 1513 EXPECT_TRUE(controller()->SectionIsActive(SECTION_SHIPPING)); 1514 EXPECT_TRUE(controller()->SectionIsActive(SECTION_CC_BILLING)); 1515 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); 1516 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL)); 1517 1518 SuggestionState suggestion_state = 1519 controller()->SuggestionStateForSection(SECTION_CC_BILLING); 1520 EXPECT_TRUE(suggestion_state.extra_text.empty()); 1521 1522 controller()->OnDidGetFullWallet(CreateFullWallet("verify_cvv")); 1523 1524 EXPECT_FALSE( 1525 NotificationsOfType(DialogNotification::REQUIRED_ACTION).empty()); 1526 EXPECT_FALSE(controller()->SectionIsActive(SECTION_SHIPPING)); 1527 EXPECT_TRUE(controller()->SectionIsActive(SECTION_CC_BILLING)); 1528 1529 suggestion_state = 1530 controller()->SuggestionStateForSection(SECTION_CC_BILLING); 1531 EXPECT_FALSE(suggestion_state.extra_text.empty()); 1532 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC_BILLING)); 1533 1534 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); 1535 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL)); 1536 1537 controller()->OnAccept(); 1538} 1539 1540TEST_F(AutofillDialogControllerTest, ErrorDuringSubmit) { 1541 EXPECT_CALL(*controller()->GetTestingWalletClient(), 1542 GetFullWallet(_)).Times(1); 1543 1544 SubmitWithWalletItems(CompleteAndValidWalletItems()); 1545 1546 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); 1547 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL)); 1548 1549 controller()->OnWalletError(wallet::WalletClient::UNKNOWN_ERROR); 1550 1551 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); 1552 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL)); 1553} 1554 1555// TODO(dbeam): disallow changing accounts instead and remove this test. 1556TEST_F(AutofillDialogControllerTest, ChangeAccountDuringSubmit) { 1557 EXPECT_CALL(*controller()->GetTestingWalletClient(), 1558 GetFullWallet(_)).Times(1); 1559 1560 SubmitWithWalletItems(CompleteAndValidWalletItems()); 1561 1562 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); 1563 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL)); 1564 1565 SwitchToWallet(); 1566 SwitchToAutofill(); 1567 1568 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); 1569 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL)); 1570} 1571 1572TEST_F(AutofillDialogControllerTest, ErrorDuringVerifyCvv) { 1573 EXPECT_CALL(*controller()->GetTestingWalletClient(), 1574 GetFullWallet(_)).Times(1); 1575 1576 SubmitWithWalletItems(CompleteAndValidWalletItems()); 1577 controller()->OnDidGetFullWallet(CreateFullWallet("verify_cvv")); 1578 1579 ASSERT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); 1580 ASSERT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL)); 1581 1582 controller()->OnWalletError(wallet::WalletClient::UNKNOWN_ERROR); 1583 1584 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); 1585 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL)); 1586} 1587 1588// TODO(dbeam): disallow changing accounts instead and remove this test. 1589TEST_F(AutofillDialogControllerTest, ChangeAccountDuringVerifyCvv) { 1590 EXPECT_CALL(*controller()->GetTestingWalletClient(), 1591 GetFullWallet(_)).Times(1); 1592 1593 SubmitWithWalletItems(CompleteAndValidWalletItems()); 1594 controller()->OnDidGetFullWallet(CreateFullWallet("verify_cvv")); 1595 1596 ASSERT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); 1597 ASSERT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL)); 1598 1599 SwitchToWallet(); 1600 SwitchToAutofill(); 1601 1602 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); 1603 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL)); 1604} 1605 1606// Simulates receiving an INVALID_FORM_FIELD required action while processing a 1607// |WalletClientDelegate::OnDid{Save,Update}*()| call. This can happen if Online 1608// Wallet's server validation differs from Chrome's local validation. 1609TEST_F(AutofillDialogControllerTest, WalletServerSideValidation) { 1610 scoped_ptr<wallet::WalletItems> wallet_items = wallet::GetTestWalletItems(); 1611 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument()); 1612 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1613 controller()->OnAccept(); 1614 1615 std::vector<wallet::RequiredAction> required_actions; 1616 required_actions.push_back(wallet::INVALID_FORM_FIELD); 1617 1618 std::vector<wallet::FormFieldError> form_errors; 1619 form_errors.push_back( 1620 wallet::FormFieldError(wallet::FormFieldError::INVALID_POSTAL_CODE, 1621 wallet::FormFieldError::SHIPPING_ADDRESS)); 1622 1623 EXPECT_CALL(*controller()->GetView(), UpdateForErrors()).Times(1); 1624 controller()->OnDidSaveToWallet(std::string(), 1625 std::string(), 1626 required_actions, 1627 form_errors); 1628} 1629 1630// Simulates receiving unrecoverable Wallet server validation errors. 1631TEST_F(AutofillDialogControllerTest, WalletServerSideValidationUnrecoverable) { 1632 scoped_ptr<wallet::WalletItems> wallet_items = wallet::GetTestWalletItems(); 1633 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument()); 1634 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1635 controller()->OnAccept(); 1636 1637 std::vector<wallet::RequiredAction> required_actions; 1638 required_actions.push_back(wallet::INVALID_FORM_FIELD); 1639 1640 std::vector<wallet::FormFieldError> form_errors; 1641 form_errors.push_back( 1642 wallet::FormFieldError(wallet::FormFieldError::UNKNOWN_ERROR, 1643 wallet::FormFieldError::UNKNOWN_LOCATION)); 1644 1645 controller()->OnDidSaveToWallet(std::string(), 1646 std::string(), 1647 required_actions, 1648 form_errors); 1649 1650 EXPECT_EQ(1U, NotificationsOfType( 1651 DialogNotification::REQUIRED_ACTION).size()); 1652} 1653 1654// Test Wallet banners are show in the right situations. These banners explain 1655// where Chrome got the user's data (i.e. "Got details from Wallet") or promote 1656// saving details into Wallet (i.e. "[x] Save details to Wallet"). 1657TEST_F(AutofillDialogControllerTest, WalletBanners) { 1658 CommandLine* command_line = CommandLine::ForCurrentProcess(); 1659 command_line->AppendSwitch(switches::kWalletServiceUseProd); 1660 PrefService* prefs = profile()->GetPrefs(); 1661 1662 // Simulate first run. 1663 prefs->SetBoolean(::prefs::kAutofillDialogHasPaidWithWallet, false); 1664 SetUpControllerWithFormData(DefaultFormData()); 1665 1666 EXPECT_EQ(0U, NotificationsOfType( 1667 DialogNotification::EXPLANATORY_MESSAGE).size()); 1668 EXPECT_EQ(0U, NotificationsOfType( 1669 DialogNotification::WALLET_USAGE_CONFIRMATION).size()); 1670 1671 // Sign in a user with a completed account. 1672 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems()); 1673 1674 // Full account; should show "Details from Wallet" message. 1675 EXPECT_EQ(1U, NotificationsOfType( 1676 DialogNotification::EXPLANATORY_MESSAGE).size()); 1677 EXPECT_EQ(0U, NotificationsOfType( 1678 DialogNotification::WALLET_USAGE_CONFIRMATION).size()); 1679 1680 // Full account; no "[x] Save details in Wallet" option should show. 1681 SwitchToAutofill(); 1682 EXPECT_EQ(0U, NotificationsOfType( 1683 DialogNotification::EXPLANATORY_MESSAGE).size()); 1684 EXPECT_EQ(0U, NotificationsOfType( 1685 DialogNotification::WALLET_USAGE_CONFIRMATION).size()); 1686 1687 SetUpControllerWithFormData(DefaultFormData()); 1688 // |controller()| has already been initialized. Test that should not take 1689 // effect until the next call of |SetUpControllerWithFormData()|. 1690 prefs->SetBoolean(::prefs::kAutofillDialogHasPaidWithWallet, true); 1691 1692 // Sign in a user with a incomplete account. 1693 scoped_ptr<wallet::WalletItems> wallet_items = wallet::GetTestWalletItems(); 1694 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument()); 1695 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1696 1697 // Partial account; no "Details from Wallet" message should show, but a 1698 // "[x] Save details in Wallet" should. 1699 EXPECT_EQ(0U, NotificationsOfType( 1700 DialogNotification::EXPLANATORY_MESSAGE).size()); 1701 EXPECT_EQ(1U, NotificationsOfType( 1702 DialogNotification::WALLET_USAGE_CONFIRMATION).size()); 1703 1704 // Once the usage confirmation banner is shown once, it keeps showing even if 1705 // the user switches to Autofill data. 1706 SwitchToAutofill(); 1707 EXPECT_EQ(0U, NotificationsOfType( 1708 DialogNotification::EXPLANATORY_MESSAGE).size()); 1709 EXPECT_EQ(1U, NotificationsOfType( 1710 DialogNotification::WALLET_USAGE_CONFIRMATION).size()); 1711 1712 // A Wallet error should kill any Wallet promos. 1713 controller()->OnWalletError(wallet::WalletClient::UNKNOWN_ERROR); 1714 1715 EXPECT_EQ(1U, NotificationsOfType( 1716 DialogNotification::WALLET_ERROR).size()); 1717 EXPECT_EQ(0U, NotificationsOfType( 1718 DialogNotification::EXPLANATORY_MESSAGE).size()); 1719 EXPECT_EQ(0U, NotificationsOfType( 1720 DialogNotification::WALLET_USAGE_CONFIRMATION).size()); 1721 1722 SetUpControllerWithFormData(DefaultFormData()); 1723 // |controller()| is error free and thinks the user has already paid w/Wallet. 1724 1725 // User has already paid with wallet. Don't show promos. 1726 EXPECT_EQ(0U, NotificationsOfType( 1727 DialogNotification::EXPLANATORY_MESSAGE).size()); 1728 EXPECT_EQ(0U, NotificationsOfType( 1729 DialogNotification::WALLET_USAGE_CONFIRMATION).size()); 1730 1731 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems()); 1732 1733 EXPECT_EQ(0U, NotificationsOfType( 1734 DialogNotification::EXPLANATORY_MESSAGE).size()); 1735 EXPECT_EQ(0U, NotificationsOfType( 1736 DialogNotification::WALLET_USAGE_CONFIRMATION).size()); 1737 1738 wallet_items = wallet::GetTestWalletItems(); 1739 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument()); 1740 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1741 1742 SwitchToAutofill(); 1743 EXPECT_EQ(0U, NotificationsOfType( 1744 DialogNotification::EXPLANATORY_MESSAGE).size()); 1745 EXPECT_EQ(0U, NotificationsOfType( 1746 DialogNotification::WALLET_USAGE_CONFIRMATION).size()); 1747} 1748 1749TEST_F(AutofillDialogControllerTest, OnAutocheckoutError) { 1750 SwitchToAutofill(); 1751 controller()->set_dialog_type(DIALOG_TYPE_AUTOCHECKOUT); 1752 1753 // We also have to simulate CC inputs to keep the controller happy. 1754 FillCreditCardInputs(); 1755 1756 controller()->OnAccept(); 1757 EXPECT_TRUE(ReadSetVisuallyDeemphasizedIpc()); 1758 controller()->OnAutocheckoutError(); 1759 1760 EXPECT_FALSE(controller()->GetDialogButtons() & ui::DIALOG_BUTTON_CANCEL); 1761 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); 1762 EXPECT_EQ(0U, NotificationsOfType( 1763 DialogNotification::AUTOCHECKOUT_SUCCESS).size()); 1764 EXPECT_EQ(1U, NotificationsOfType( 1765 DialogNotification::AUTOCHECKOUT_ERROR).size()); 1766 1767 controller()->ViewClosed(); 1768 EXPECT_FALSE(ReadSetVisuallyDeemphasizedIpc()); 1769} 1770 1771TEST_F(AutofillDialogControllerTest, OnAutocheckoutSuccess) { 1772 CommandLine* command_line = CommandLine::ForCurrentProcess(); 1773 command_line->AppendSwitch(switches::kWalletServiceUseProd); 1774 controller()->set_dialog_type(DIALOG_TYPE_AUTOCHECKOUT); 1775 1776 // Simulate first run. 1777 profile()->GetPrefs()->SetBoolean(::prefs::kAutofillDialogHasPaidWithWallet, 1778 false); 1779 SetUpControllerWithFormData(DefaultFormData()); 1780 controller()->set_dialog_type(DIALOG_TYPE_AUTOCHECKOUT); 1781 1782 // Sign in a user with a completed account. 1783 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems()); 1784 1785 // Full account; should show "Details from Wallet" message. 1786 EXPECT_EQ(1U, NotificationsOfType( 1787 DialogNotification::EXPLANATORY_MESSAGE).size()); 1788 EXPECT_EQ(0U, NotificationsOfType( 1789 DialogNotification::WALLET_USAGE_CONFIRMATION).size()); 1790 1791 AcceptAndLoadFakeFingerprint(); 1792 EXPECT_TRUE(ReadSetVisuallyDeemphasizedIpc()); 1793 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet()); 1794 EXPECT_TRUE(controller()->GetDialogOverlay().image.IsEmpty()); 1795 1796 EXPECT_EQ(0U, NotificationsOfType( 1797 DialogNotification::EXPLANATORY_MESSAGE).size()); 1798 1799 controller()->OnAutocheckoutSuccess(); 1800 EXPECT_TRUE(controller()->GetDialogOverlay().image.IsEmpty()); 1801 1802 EXPECT_FALSE(controller()->GetDialogButtons() & ui::DIALOG_BUTTON_CANCEL); 1803 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); 1804 EXPECT_EQ(1U, NotificationsOfType( 1805 DialogNotification::AUTOCHECKOUT_SUCCESS).size()); 1806 EXPECT_EQ(0U, NotificationsOfType( 1807 DialogNotification::AUTOCHECKOUT_ERROR).size()); 1808 EXPECT_EQ(0U, NotificationsOfType( 1809 DialogNotification::EXPLANATORY_MESSAGE).size()); 1810 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean( 1811 ::prefs::kAutofillDialogHasPaidWithWallet)); 1812 1813 controller()->ViewClosed(); 1814 EXPECT_FALSE(ReadSetVisuallyDeemphasizedIpc()); 1815} 1816 1817TEST_F(AutofillDialogControllerTest, ViewCancelDoesntSetPref) { 1818 ASSERT_FALSE(profile()->GetPrefs()->HasPrefPath( 1819 ::prefs::kAutofillDialogPayWithoutWallet)); 1820 1821 SwitchToAutofill(); 1822 1823 controller()->OnCancel(); 1824 controller()->ViewClosed(); 1825 1826 EXPECT_FALSE(profile()->GetPrefs()->HasPrefPath( 1827 ::prefs::kAutofillDialogPayWithoutWallet)); 1828} 1829 1830TEST_F(AutofillDialogControllerTest, SubmitWithSigninErrorDoesntSetPref) { 1831 ASSERT_FALSE(profile()->GetPrefs()->HasPrefPath( 1832 ::prefs::kAutofillDialogPayWithoutWallet)); 1833 1834 SimulateSigninError(); 1835 FillCreditCardInputs(); 1836 controller()->OnAccept(); 1837 1838 EXPECT_FALSE(profile()->GetPrefs()->HasPrefPath( 1839 ::prefs::kAutofillDialogPayWithoutWallet)); 1840} 1841 1842// Tests that there's an overlay shown while waiting for full wallet items, 1843// and on first run an additional expository wallet overlay shown after full 1844// wallet items are returned. 1845// TODO(estade): enable on other platforms when overlays are supported there. 1846#if defined(TOOLKIT_VIEWS) 1847TEST_F(AutofillDialogControllerTest, WalletFirstRun) { 1848 // Simulate first run. 1849 PrefService* prefs = profile()->GetPrefs(); 1850 prefs->SetBoolean(::prefs::kAutofillDialogHasPaidWithWallet, false); 1851 SetUpControllerWithFormData(DefaultFormData()); 1852 1853 SwitchToWallet(); 1854 EXPECT_TRUE(controller()->GetDialogOverlay().image.IsEmpty()); 1855 1856 SubmitWithWalletItems(CompleteAndValidWalletItems()); 1857 EXPECT_FALSE(controller()->GetDialogOverlay().image.IsEmpty()); 1858 1859 EXPECT_FALSE(prefs->GetBoolean(::prefs::kAutofillDialogHasPaidWithWallet)); 1860 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet()); 1861 EXPECT_FALSE(prefs->GetBoolean(::prefs::kAutofillDialogHasPaidWithWallet)); 1862 EXPECT_FALSE(controller()->GetDialogOverlay().image.IsEmpty()); 1863 EXPECT_FALSE(form_structure()); 1864 1865 controller()->OverlayButtonPressed(); 1866 EXPECT_TRUE(prefs->GetBoolean(::prefs::kAutofillDialogHasPaidWithWallet)); 1867 EXPECT_TRUE(form_structure()); 1868} 1869#endif 1870 1871// On second run, the second overlay doesn't show. 1872TEST_F(AutofillDialogControllerTest, WalletSecondRun) { 1873 SwitchToWallet(); 1874 EXPECT_TRUE(controller()->GetDialogOverlay().image.IsEmpty()); 1875 1876 SubmitWithWalletItems(CompleteAndValidWalletItems()); 1877 EXPECT_FALSE(controller()->GetDialogOverlay().image.IsEmpty()); 1878 1879 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean( 1880 ::prefs::kAutofillDialogHasPaidWithWallet)); 1881 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet()); 1882 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean( 1883 ::prefs::kAutofillDialogHasPaidWithWallet)); 1884 EXPECT_TRUE(form_structure()); 1885} 1886 1887TEST_F(AutofillDialogControllerTest, ViewSubmitSetsPref) { 1888 ASSERT_FALSE(profile()->GetPrefs()->HasPrefPath( 1889 ::prefs::kAutofillDialogPayWithoutWallet)); 1890 1891 SwitchToAutofill(); 1892 FillCreditCardInputs(); 1893 controller()->OnAccept(); 1894 1895 EXPECT_TRUE(profile()->GetPrefs()->HasPrefPath( 1896 ::prefs::kAutofillDialogPayWithoutWallet)); 1897 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean( 1898 ::prefs::kAutofillDialogPayWithoutWallet)); 1899 1900 // Try again with a signin error (just leaves the pref alone). 1901 SetUpControllerWithFormData(DefaultFormData()); 1902 1903 // Setting up the controller again should not change the pref. 1904 EXPECT_TRUE(profile()->GetPrefs()->HasPrefPath( 1905 ::prefs::kAutofillDialogPayWithoutWallet)); 1906 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean( 1907 ::prefs::kAutofillDialogPayWithoutWallet)); 1908 1909 SimulateSigninError(); 1910 FillCreditCardInputs(); 1911 controller()->OnAccept(); 1912 EXPECT_TRUE(profile()->GetPrefs()->HasPrefPath( 1913 ::prefs::kAutofillDialogPayWithoutWallet)); 1914 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean( 1915 ::prefs::kAutofillDialogPayWithoutWallet)); 1916 1917 // Succesfully choosing wallet does set the pref. 1918 SetUpControllerWithFormData(DefaultFormData()); 1919 1920 SwitchToWallet(); 1921 scoped_ptr<wallet::WalletItems> wallet_items = wallet::GetTestWalletItems(); 1922 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument()); 1923 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1924 controller()->OnAccept(); 1925 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet()); 1926 1927 EXPECT_TRUE(profile()->GetPrefs()->HasPrefPath( 1928 ::prefs::kAutofillDialogPayWithoutWallet)); 1929 EXPECT_FALSE(profile()->GetPrefs()->GetBoolean( 1930 ::prefs::kAutofillDialogPayWithoutWallet)); 1931} 1932 1933TEST_F(AutofillDialogControllerTest, HideWalletEmail) { 1934 SwitchToAutofill(); 1935 1936 // Email section should be showing when using Autofill. 1937 EXPECT_TRUE(controller()->SectionIsActive(SECTION_EMAIL)); 1938 1939 SwitchToWallet(); 1940 1941 // Setup some wallet state, submit, and get a full wallet to end the flow. 1942 scoped_ptr<wallet::WalletItems> wallet_items = CompleteAndValidWalletItems(); 1943 1944 // Filling |form_structure()| depends on the current username and wallet items 1945 // being fetched. Until both of these have occurred, the user should not be 1946 // able to click Submit if using Wallet. The username fetch happened earlier. 1947 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); 1948 controller()->OnDidGetWalletItems(wallet_items.Pass()); 1949 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); 1950 1951 // Email section should be hidden when using Wallet. 1952 EXPECT_FALSE(controller()->SectionIsActive(SECTION_EMAIL)); 1953 1954 controller()->OnAccept(); 1955 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet()); 1956 1957 size_t i = 0; 1958 for (; i < form_structure()->field_count(); ++i) { 1959 if (form_structure()->field(i)->type() == EMAIL_ADDRESS) { 1960 EXPECT_EQ(ASCIIToUTF16(kFakeEmail), form_structure()->field(i)->value); 1961 break; 1962 } 1963 } 1964 ASSERT_LT(i, form_structure()->field_count()); 1965} 1966 1967// Test if autofill types of returned form structure are correct for billing 1968// entries. 1969TEST_F(AutofillDialogControllerTest, AutofillTypes) { 1970 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems()); 1971 controller()->OnAccept(); 1972 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet()); 1973 ASSERT_EQ(20U, form_structure()->field_count()); 1974 EXPECT_EQ(EMAIL_ADDRESS, form_structure()->field(0)->type()); 1975 EXPECT_EQ(CREDIT_CARD_NUMBER, form_structure()->field(2)->type()); 1976 EXPECT_EQ(ADDRESS_BILLING_STATE, form_structure()->field(9)->type()); 1977 EXPECT_EQ(ADDRESS_HOME_STATE, form_structure()->field(16)->type()); 1978} 1979 1980TEST_F(AutofillDialogControllerTest, SaveDetailsInChrome) { 1981 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(2); 1982 1983 AutofillProfile full_profile(test::GetVerifiedProfile()); 1984 controller()->GetTestingManager()->AddTestingProfile(&full_profile); 1985 1986 CreditCard card(test::GetVerifiedCreditCard()); 1987 controller()->GetTestingManager()->AddTestingCreditCard(&card); 1988 EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome()); 1989 1990 controller()->EditClickedForSection(SECTION_EMAIL); 1991 EXPECT_TRUE(controller()->ShouldOfferToSaveInChrome()); 1992 1993 controller()->EditCancelledForSection(SECTION_EMAIL); 1994 EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome()); 1995 1996 controller()->MenuModelForSection(SECTION_EMAIL)->ActivatedAt(1); 1997 EXPECT_TRUE(controller()->ShouldOfferToSaveInChrome()); 1998 1999 profile()->set_incognito(true); 2000 EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome()); 2001} 2002 2003// Tests that user is prompted when using instrument with minimal address. 2004TEST_F(AutofillDialogControllerTest, UpgradeMinimalAddress) { 2005 // A minimal address being selected should trigger error validation in the 2006 // view. Called once for each incomplete suggestion. 2007 EXPECT_CALL(*controller()->GetView(), UpdateForErrors()).Times(1); 2008 2009 scoped_ptr<wallet::WalletItems> wallet_items = wallet::GetTestWalletItems(); 2010 wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentWithIdAndAddress( 2011 "id", wallet::GetTestMinimalAddress())); 2012 scoped_ptr<wallet::Address> address(wallet::GetTestShippingAddress()); 2013 address->set_is_complete_address(false); 2014 wallet_items->AddAddress(address.Pass()); 2015 controller()->OnDidGetWalletItems(wallet_items.Pass()); 2016 2017 // Assert that dialog's SECTION_CC_BILLING section is in edit mode. 2018 ASSERT_TRUE(controller()->IsEditingExistingData(SECTION_CC_BILLING)); 2019 // Shipping section should be in edit mode because of 2020 // is_minimal_shipping_address. 2021 ASSERT_TRUE(controller()->IsEditingExistingData(SECTION_SHIPPING)); 2022} 2023 2024TEST_F(AutofillDialogControllerTest, RiskNeverLoadsWithPendingLegalDocuments) { 2025 EXPECT_CALL(*controller(), LoadRiskFingerprintData()).Times(0); 2026 2027 scoped_ptr<wallet::WalletItems> wallet_items = wallet::GetTestWalletItems(); 2028 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument()); 2029 controller()->OnDidGetWalletItems(wallet_items.Pass()); 2030 controller()->OnAccept(); 2031} 2032 2033TEST_F(AutofillDialogControllerTest, RiskLoadsAfterAcceptingLegalDocuments) { 2034 EXPECT_CALL(*controller(), LoadRiskFingerprintData()).Times(0); 2035 2036 scoped_ptr<wallet::WalletItems> wallet_items = wallet::GetTestWalletItems(); 2037 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument()); 2038 controller()->OnDidGetWalletItems(wallet_items.Pass()); 2039 2040 testing::Mock::VerifyAndClear(controller()); 2041 EXPECT_CALL(*controller(), LoadRiskFingerprintData()).Times(1); 2042 2043 controller()->OnAccept(); 2044 2045 // Simulate a risk load and verify |GetRiskData()| matches the encoded value. 2046 controller()->OnDidAcceptLegalDocuments(); 2047 controller()->OnDidLoadRiskFingerprintData(GetFakeFingerprint().Pass()); 2048 EXPECT_EQ(kFakeFingerprintEncoded, controller()->GetRiskData()); 2049} 2050 2051TEST_F(AutofillDialogControllerTest, NoManageMenuItemForNewWalletUsers) { 2052 // Make sure the menu model item is created for a returning Wallet user. 2053 scoped_ptr<wallet::WalletItems> wallet_items = wallet::GetTestWalletItems(); 2054 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument()); 2055 wallet_items->AddAddress(wallet::GetTestShippingAddress()); 2056 controller()->OnDidGetWalletItems(wallet_items.Pass()); 2057 2058 EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING)); 2059 // "Same as billing", "123 address", "Add address...", and "Manage addresses". 2060 EXPECT_EQ( 2061 4, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount()); 2062 2063 // Make sure the menu model item is not created for new Wallet users. 2064 base::DictionaryValue dict; 2065 scoped_ptr<base::ListValue> required_actions(new base::ListValue); 2066 required_actions->AppendString("setup_wallet"); 2067 dict.Set("required_action", required_actions.release()); 2068 controller()->OnDidGetWalletItems( 2069 wallet::WalletItems::CreateWalletItems(dict).Pass()); 2070 2071 EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC_BILLING)); 2072 // "Same as billing" and "Add address...". 2073 EXPECT_EQ( 2074 2, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount()); 2075} 2076 2077TEST_F(AutofillDialogControllerTest, ShippingSectionCanBeHidden) { 2078 SwitchToAutofill(); 2079 2080 FormFieldData email_field; 2081 email_field.autocomplete_attribute = "email"; 2082 FormFieldData cc_field; 2083 cc_field.autocomplete_attribute = "cc-number"; 2084 FormFieldData billing_field; 2085 billing_field.autocomplete_attribute = "billing region"; 2086 2087 FormData form_data; 2088 form_data.fields.push_back(email_field); 2089 form_data.fields.push_back(cc_field); 2090 form_data.fields.push_back(billing_field); 2091 2092 AutofillProfile full_profile(test::GetVerifiedProfile()); 2093 controller()->GetTestingManager()->AddTestingProfile(&full_profile); 2094 SetUpControllerWithFormData(form_data); 2095 EXPECT_FALSE(controller()->SectionIsActive(SECTION_SHIPPING)); 2096 2097 FillCreditCardInputs(); 2098 controller()->OnAccept(); 2099 EXPECT_TRUE(form_structure()); 2100} 2101 2102TEST_F(AutofillDialogControllerTest, NotProdNotification) { 2103 // To make IsPayingWithWallet() true. 2104 controller()->OnDidGetWalletItems(wallet::GetTestWalletItems()); 2105 2106 CommandLine* command_line = CommandLine::ForCurrentProcess(); 2107 ASSERT_FALSE(command_line->HasSwitch(switches::kWalletServiceUseProd)); 2108 EXPECT_FALSE( 2109 NotificationsOfType(DialogNotification::DEVELOPER_WARNING).empty()); 2110 2111 command_line->AppendSwitch(switches::kWalletServiceUseProd); 2112 EXPECT_TRUE( 2113 NotificationsOfType(DialogNotification::DEVELOPER_WARNING).empty()); 2114} 2115 2116// Ensure Wallet instruments marked expired by the server are shown as invalid. 2117TEST_F(AutofillDialogControllerTest, WalletExpiredCard) { 2118 scoped_ptr<wallet::WalletItems> wallet_items = wallet::GetTestWalletItems(); 2119 wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentExpired()); 2120 controller()->OnDidGetWalletItems(wallet_items.Pass()); 2121 2122 EXPECT_TRUE(controller()->IsEditingExistingData(SECTION_CC_BILLING)); 2123 2124 // Use |SetOutputValue()| to put the right AutofillFieldTypes into the map. 2125 const DetailInputs& inputs = 2126 controller()->RequestedFieldsForSection(SECTION_CC_BILLING); 2127 DetailOutputMap outputs; 2128 SetOutputValue(inputs, &outputs, COMPANY_NAME, "Bluth Company"); 2129 2130 ValidityData validity_data = 2131 controller()->InputsAreValid(SECTION_CC_BILLING, outputs, VALIDATE_EDIT); 2132 EXPECT_EQ(1U, validity_data.count(CREDIT_CARD_EXP_MONTH)); 2133 EXPECT_EQ(1U, validity_data.count(CREDIT_CARD_EXP_4_DIGIT_YEAR)); 2134 2135 // Make the local input year differ from the instrument. 2136 SetOutputValue(inputs, &outputs, CREDIT_CARD_EXP_4_DIGIT_YEAR, "3002"); 2137 2138 validity_data = 2139 controller()->InputsAreValid(SECTION_CC_BILLING, outputs, VALIDATE_EDIT); 2140 EXPECT_EQ(0U, validity_data.count(CREDIT_CARD_EXP_MONTH)); 2141 EXPECT_EQ(0U, validity_data.count(CREDIT_CARD_EXP_4_DIGIT_YEAR)); 2142 2143 // Make the local input month differ from the instrument. 2144 SetOutputValue(inputs, &outputs, CREDIT_CARD_EXP_MONTH, "06"); 2145 2146 validity_data = 2147 controller()->InputsAreValid(SECTION_CC_BILLING, outputs, VALIDATE_EDIT); 2148 EXPECT_EQ(0U, validity_data.count(CREDIT_CARD_EXP_MONTH)); 2149 EXPECT_EQ(0U, validity_data.count(CREDIT_CARD_EXP_4_DIGIT_YEAR)); 2150} 2151 2152TEST_F(AutofillDialogControllerTest, ChooseAnotherInstrumentOrAddress) { 2153 SubmitWithWalletItems(CompleteAndValidWalletItems()); 2154 2155 EXPECT_EQ(0U, NotificationsOfType( 2156 DialogNotification::REQUIRED_ACTION).size()); 2157 EXPECT_CALL(*controller()->GetTestingWalletClient(), 2158 GetWalletItems(_)).Times(1); 2159 controller()->OnDidGetFullWallet( 2160 CreateFullWallet("choose_another_instrument_or_address")); 2161 EXPECT_EQ(1U, NotificationsOfType( 2162 DialogNotification::REQUIRED_ACTION).size()); 2163 controller()->OnDidGetWalletItems(CompleteAndValidWalletItems()); 2164 2165 controller()->OnAccept(); 2166 EXPECT_EQ(0U, NotificationsOfType( 2167 DialogNotification::REQUIRED_ACTION).size()); 2168} 2169 2170// Make sure detailed steps for Autocheckout are added 2171// and updated correctly. 2172TEST_F(AutofillDialogControllerTest, DetailedSteps) { 2173 EXPECT_CALL(*controller()->GetTestingWalletClient(), 2174 GetFullWallet(_)).Times(1); 2175 2176 controller()->set_dialog_type(DIALOG_TYPE_AUTOCHECKOUT); 2177 2178 // Add steps as would normally be done by the AutocheckoutManager. 2179 controller()->AddAutocheckoutStep(AUTOCHECKOUT_STEP_SHIPPING); 2180 controller()->AddAutocheckoutStep(AUTOCHECKOUT_STEP_DELIVERY); 2181 controller()->AddAutocheckoutStep(AUTOCHECKOUT_STEP_BILLING); 2182 2183 scoped_ptr<wallet::WalletItems> wallet_items = wallet::GetTestWalletItems(); 2184 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument()); 2185 wallet_items->AddAddress(wallet::GetTestShippingAddress()); 2186 controller()->OnDidGetWalletItems(wallet_items.Pass()); 2187 // Initiate flow - should add proxy card step since the user is using wallet 2188 // data. 2189 controller()->OnAccept(); 2190 EXPECT_TRUE(ReadSetVisuallyDeemphasizedIpc()); 2191 controller()->OnDidLoadRiskFingerprintData(GetFakeFingerprint().Pass()); 2192 2193 SuggestionState suggestion_state = 2194 controller()->SuggestionStateForSection(SECTION_CC_BILLING); 2195 EXPECT_TRUE(suggestion_state.extra_text.empty()); 2196 2197 // There should be four steps total, with the first being the card generation 2198 // step added by the dialog controller. 2199 EXPECT_EQ(4U, controller()->CurrentAutocheckoutSteps().size()); 2200 EXPECT_EQ(AUTOCHECKOUT_STEP_PROXY_CARD, 2201 controller()->CurrentAutocheckoutSteps()[0].type()); 2202 EXPECT_EQ(AUTOCHECKOUT_STEP_STARTED, 2203 controller()->CurrentAutocheckoutSteps()[0].status()); 2204 2205 // Simulate a wallet error. This should remove the card generation step from 2206 // the flow, as we will have to proceed with local data. 2207 controller()->OnWalletError(wallet::WalletClient::UNKNOWN_ERROR); 2208 2209 AutofillProfile shipping_profile(test::GetVerifiedProfile()); 2210 AutofillProfile billing_profile(test::GetVerifiedProfile2()); 2211 CreditCard credit_card(test::GetVerifiedCreditCard()); 2212 controller()->GetTestingManager()->AddTestingProfile(&shipping_profile); 2213 controller()->GetTestingManager()->AddTestingProfile(&billing_profile); 2214 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card); 2215 ui::MenuModel* billing_model = 2216 controller()->MenuModelForSection(SECTION_BILLING); 2217 billing_model->ActivatedAt(1); 2218 2219 // Re-initiate flow. 2220 controller()->OnAccept(); 2221 EXPECT_TRUE(ReadSetVisuallyDeemphasizedIpc()); 2222 2223 // All steps should be initially unstarted. 2224 EXPECT_EQ(3U, controller()->CurrentAutocheckoutSteps().size()); 2225 EXPECT_EQ(AUTOCHECKOUT_STEP_SHIPPING, 2226 controller()->CurrentAutocheckoutSteps()[0].type()); 2227 EXPECT_EQ(AUTOCHECKOUT_STEP_UNSTARTED, 2228 controller()->CurrentAutocheckoutSteps()[0].status()); 2229 EXPECT_EQ(AUTOCHECKOUT_STEP_DELIVERY, 2230 controller()->CurrentAutocheckoutSteps()[1].type()); 2231 EXPECT_EQ(AUTOCHECKOUT_STEP_UNSTARTED, 2232 controller()->CurrentAutocheckoutSteps()[1].status()); 2233 EXPECT_EQ(AUTOCHECKOUT_STEP_BILLING, 2234 controller()->CurrentAutocheckoutSteps()[2].type()); 2235 EXPECT_EQ(AUTOCHECKOUT_STEP_UNSTARTED, 2236 controller()->CurrentAutocheckoutSteps()[2].status()); 2237 2238 // Update steps in the same manner that we would expect to see from the 2239 // AutocheckoutManager while progressing through a flow. 2240 controller()->UpdateAutocheckoutStep(AUTOCHECKOUT_STEP_SHIPPING, 2241 AUTOCHECKOUT_STEP_STARTED); 2242 controller()->UpdateAutocheckoutStep(AUTOCHECKOUT_STEP_SHIPPING, 2243 AUTOCHECKOUT_STEP_COMPLETED); 2244 controller()->UpdateAutocheckoutStep(AUTOCHECKOUT_STEP_DELIVERY, 2245 AUTOCHECKOUT_STEP_STARTED); 2246 2247 // Verify that the steps were appropriately updated. 2248 EXPECT_EQ(3U, controller()->CurrentAutocheckoutSteps().size()); 2249 EXPECT_EQ(AUTOCHECKOUT_STEP_SHIPPING, 2250 controller()->CurrentAutocheckoutSteps()[0].type()); 2251 EXPECT_EQ(AUTOCHECKOUT_STEP_COMPLETED, 2252 controller()->CurrentAutocheckoutSteps()[0].status()); 2253 EXPECT_EQ(AUTOCHECKOUT_STEP_DELIVERY, 2254 controller()->CurrentAutocheckoutSteps()[1].type()); 2255 EXPECT_EQ(AUTOCHECKOUT_STEP_STARTED, 2256 controller()->CurrentAutocheckoutSteps()[1].status()); 2257 EXPECT_EQ(AUTOCHECKOUT_STEP_BILLING, 2258 controller()->CurrentAutocheckoutSteps()[2].type()); 2259 EXPECT_EQ(AUTOCHECKOUT_STEP_UNSTARTED, 2260 controller()->CurrentAutocheckoutSteps()[2].status()); 2261 2262 controller()->ViewClosed(); 2263 EXPECT_FALSE(ReadSetVisuallyDeemphasizedIpc()); 2264} 2265 2266 2267TEST_F(AutofillDialogControllerTest, NewCardBubbleShown) { 2268 EXPECT_CALL(*test_bubble_controller(), 2269 ShowAsNewCardSavedBubble(ASCIIToUTF16("Visa - 1111"))).Times(1); 2270 EXPECT_CALL(*test_bubble_controller(), 2271 ShowAsGeneratedCardBubble(_, _)).Times(0); 2272 2273 SwitchToAutofill(); 2274 FillCreditCardInputs(); 2275 controller()->OnAccept(); 2276 controller()->ViewClosed(); 2277} 2278 2279TEST_F(AutofillDialogControllerTest, GeneratedCardBubbleShown) { 2280 EXPECT_CALL(*test_bubble_controller(), 2281 ShowAsGeneratedCardBubble(_, _)).Times(1); 2282 EXPECT_CALL(*test_bubble_controller(), ShowAsNewCardSavedBubble(_)).Times(0); 2283 2284 SubmitWithWalletItems(CompleteAndValidWalletItems()); 2285 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet()); 2286 controller()->ViewClosed(); 2287} 2288 2289} // namespace autofill 2290