OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/guid.h" | 5 #include "base/guid.h" |
6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
7 #include "base/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "chrome/browser/ui/autofill/autofill_dialog_controller_impl.h" | 9 #include "chrome/browser/ui/autofill/autofill_dialog_controller_impl.h" |
10 #include "chrome/browser/ui/autofill/autofill_dialog_view.h" | 10 #include "chrome/browser/ui/autofill/autofill_dialog_view.h" |
11 #include "chrome/common/pref_names.h" | 11 #include "chrome/common/pref_names.h" |
12 #include "chrome/test/base/testing_profile.h" | 12 #include "chrome/test/base/testing_profile.h" |
13 #include "components/autofill/browser/autofill_common_test.h" | 13 #include "components/autofill/browser/autofill_common_test.h" |
14 #include "components/autofill/browser/autofill_metrics.h" | 14 #include "components/autofill/browser/autofill_metrics.h" |
| 15 #include "components/autofill/browser/wallet/full_wallet.h" |
15 #include "components/autofill/browser/wallet/instrument.h" | 16 #include "components/autofill/browser/wallet/instrument.h" |
16 #include "components/autofill/browser/wallet/wallet_address.h" | 17 #include "components/autofill/browser/wallet/wallet_address.h" |
17 #include "components/autofill/browser/wallet/wallet_client.h" | 18 #include "components/autofill/browser/wallet/wallet_client.h" |
18 #include "components/autofill/browser/wallet/wallet_test_util.h" | 19 #include "components/autofill/browser/wallet/wallet_test_util.h" |
19 #include "components/autofill/common/form_data.h" | 20 #include "components/autofill/common/form_data.h" |
20 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
21 #include "content/public/test/test_browser_thread.h" | 22 #include "content/public/test/test_browser_thread.h" |
22 #include "content/public/test/web_contents_tester.h" | 23 #include "content/public/test/web_contents_tester.h" |
23 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 TestWalletClient(net::URLRequestContextGetter* context, | 87 TestWalletClient(net::URLRequestContextGetter* context, |
87 wallet::WalletClientDelegate* delegate) | 88 wallet::WalletClientDelegate* delegate) |
88 : wallet::WalletClient(context, delegate) {} | 89 : wallet::WalletClient(context, delegate) {} |
89 virtual ~TestWalletClient() {} | 90 virtual ~TestWalletClient() {} |
90 | 91 |
91 MOCK_METHOD3(AcceptLegalDocuments, | 92 MOCK_METHOD3(AcceptLegalDocuments, |
92 void(const std::vector<wallet::WalletItems::LegalDocument*>& documents, | 93 void(const std::vector<wallet::WalletItems::LegalDocument*>& documents, |
93 const std::string& google_transaction_id, | 94 const std::string& google_transaction_id, |
94 const GURL& source_url)); | 95 const GURL& source_url)); |
95 | 96 |
| 97 MOCK_METHOD3(AuthenticateInstrument, |
| 98 void(const std::string& instrument_id, |
| 99 const std::string& card_verification_number, |
| 100 const std::string& obfuscated_gaia_id)); |
| 101 |
96 MOCK_METHOD1(GetFullWallet, | 102 MOCK_METHOD1(GetFullWallet, |
97 void(const wallet::WalletClient::FullWalletRequest& request)); | 103 void(const wallet::WalletClient::FullWalletRequest& request)); |
98 | 104 |
99 MOCK_METHOD2(SaveAddress, | 105 MOCK_METHOD2(SaveAddress, |
100 void(const wallet::Address& address, const GURL& source_url)); | 106 void(const wallet::Address& address, const GURL& source_url)); |
101 | 107 |
102 MOCK_METHOD3(SaveInstrument, | 108 MOCK_METHOD3(SaveInstrument, |
103 void(const wallet::Instrument& instrument, | 109 void(const wallet::Instrument& instrument, |
104 const std::string& obfuscated_gaia_id, | 110 const std::string& obfuscated_gaia_id, |
105 const GURL& source_url)); | 111 const GURL& source_url)); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 callback); | 223 callback); |
218 controller_->Init(profile()); | 224 controller_->Init(profile()); |
219 controller_->Show(); | 225 controller_->Show(); |
220 } | 226 } |
221 | 227 |
222 virtual void TearDown() OVERRIDE { | 228 virtual void TearDown() OVERRIDE { |
223 controller_->ViewClosed(); | 229 controller_->ViewClosed(); |
224 } | 230 } |
225 | 231 |
226 protected: | 232 protected: |
| 233 size_t CountNotificationsOfType(DialogNotification::Type type) { |
| 234 size_t count = 0; |
| 235 const std::vector<DialogNotification>& notifications = |
| 236 controller()->CurrentNotifications(); |
| 237 for (size_t i = 0; i < notifications.size(); ++i) { |
| 238 if (notifications[i].type() == type) |
| 239 ++count; |
| 240 } |
| 241 return count; |
| 242 } |
| 243 |
227 TestAutofillDialogController* controller() { return controller_; } | 244 TestAutofillDialogController* controller() { return controller_; } |
228 | 245 |
229 TestingProfile* profile() { return &profile_; } | 246 TestingProfile* profile() { return &profile_; } |
230 | 247 |
231 private: | 248 private: |
232 // A bunch of threads are necessary for classes like TestWebContents and | 249 // A bunch of threads are necessary for classes like TestWebContents and |
233 // URLRequestContextGetter not to fall over. | 250 // URLRequestContextGetter not to fall over. |
234 MessageLoopForUI loop_; | 251 MessageLoopForUI loop_; |
235 content::TestBrowserThread ui_thread_; | 252 content::TestBrowserThread ui_thread_; |
236 content::TestBrowserThread file_thread_; | 253 content::TestBrowserThread file_thread_; |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 // Respect variants for the email address field only. | 333 // Respect variants for the email address field only. |
317 controller()->GetTestingManager()->AddTestingProfile(&full_profile); | 334 controller()->GetTestingManager()->AddTestingProfile(&full_profile); |
318 ui::MenuModel* shipping_model = | 335 ui::MenuModel* shipping_model = |
319 controller()->MenuModelForSection(SECTION_SHIPPING); | 336 controller()->MenuModelForSection(SECTION_SHIPPING); |
320 EXPECT_EQ(2, shipping_model->GetItemCount()); | 337 EXPECT_EQ(2, shipping_model->GetItemCount()); |
321 ui::MenuModel* email_model = | 338 ui::MenuModel* email_model = |
322 controller()->MenuModelForSection(SECTION_EMAIL); | 339 controller()->MenuModelForSection(SECTION_EMAIL); |
323 EXPECT_EQ(3, email_model->GetItemCount()); | 340 EXPECT_EQ(3, email_model->GetItemCount()); |
324 | 341 |
325 email_model->ActivatedAt(0); | 342 email_model->ActivatedAt(0); |
326 EXPECT_EQ(kEmail1, controller()->SuggestionTextForSection(SECTION_EMAIL)); | 343 EXPECT_EQ(kEmail1, |
| 344 controller()->SuggestionStateForSection(SECTION_EMAIL).text); |
327 email_model->ActivatedAt(1); | 345 email_model->ActivatedAt(1); |
328 EXPECT_EQ(kEmail2, controller()->SuggestionTextForSection(SECTION_EMAIL)); | 346 EXPECT_EQ(kEmail2, |
| 347 controller()->SuggestionStateForSection(SECTION_EMAIL).text); |
329 | 348 |
330 controller()->EditClickedForSection(SECTION_EMAIL); | 349 controller()->EditClickedForSection(SECTION_EMAIL); |
331 const DetailInputs& inputs = | 350 const DetailInputs& inputs = |
332 controller()->RequestedFieldsForSection(SECTION_EMAIL); | 351 controller()->RequestedFieldsForSection(SECTION_EMAIL); |
333 EXPECT_EQ(kEmail2, inputs[0].autofilled_value); | 352 EXPECT_EQ(kEmail2, inputs[0].autofilled_value); |
334 } | 353 } |
335 | 354 |
336 TEST_F(AutofillDialogControllerTest, AcceptLegalDocuments) { | 355 TEST_F(AutofillDialogControllerTest, AcceptLegalDocuments) { |
337 controller()->set_is_paying_with_wallet(true); | 356 controller()->set_is_paying_with_wallet(true); |
338 | 357 |
339 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(1); | 358 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(1); |
340 EXPECT_CALL(*controller()->GetTestingWalletClient(), | 359 EXPECT_CALL(*controller()->GetTestingWalletClient(), |
341 AcceptLegalDocuments(_, _, _)).Times(1); | 360 AcceptLegalDocuments(_, _, _)).Times(1); |
342 EXPECT_CALL(*controller()->GetTestingWalletClient(), | 361 EXPECT_CALL(*controller()->GetTestingWalletClient(), |
343 GetFullWallet(_)).Times(1); | 362 GetFullWallet(_)).Times(1); |
344 | 363 |
345 scoped_ptr<wallet::WalletItems> wallet_items = wallet::GetTestWalletItems(); | 364 scoped_ptr<wallet::WalletItems> wallet_items = wallet::GetTestWalletItems(); |
346 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument()); | 365 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument()); |
347 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument()); | 366 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument()); |
348 wallet_items->AddAddress(wallet::GetTestShippingAddress()); | 367 wallet_items->AddAddress(wallet::GetTestShippingAddress()); |
349 controller()->OnDidGetWalletItems(wallet_items.Pass()); | 368 controller()->OnDidGetWalletItems(wallet_items.Pass()); |
350 controller()->OnSubmit(); | 369 controller()->OnAccept(); |
351 } | 370 } |
352 | 371 |
353 TEST_F(AutofillDialogControllerTest, SaveAddress) { | 372 TEST_F(AutofillDialogControllerTest, SaveAddress) { |
354 controller()->set_is_paying_with_wallet(true); | 373 controller()->set_is_paying_with_wallet(true); |
355 | 374 |
356 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(1); | 375 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(1); |
357 EXPECT_CALL(*controller()->GetTestingWalletClient(), | 376 EXPECT_CALL(*controller()->GetTestingWalletClient(), |
358 SaveAddress(_, _)).Times(1); | 377 SaveAddress(_, _)).Times(1); |
359 | 378 |
360 scoped_ptr<wallet::WalletItems> wallet_items = wallet::GetTestWalletItems(); | 379 scoped_ptr<wallet::WalletItems> wallet_items = wallet::GetTestWalletItems(); |
361 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument()); | 380 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument()); |
362 controller()->OnDidGetWalletItems(wallet_items.Pass()); | 381 controller()->OnDidGetWalletItems(wallet_items.Pass()); |
363 controller()->OnSubmit(); | 382 controller()->OnAccept(); |
364 } | 383 } |
365 | 384 |
366 TEST_F(AutofillDialogControllerTest, SaveInstrument) { | 385 TEST_F(AutofillDialogControllerTest, SaveInstrument) { |
367 controller()->set_is_paying_with_wallet(true); | 386 controller()->set_is_paying_with_wallet(true); |
368 | 387 |
369 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(1); | 388 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(1); |
370 EXPECT_CALL(*controller()->GetTestingWalletClient(), | 389 EXPECT_CALL(*controller()->GetTestingWalletClient(), |
371 SaveInstrument(_, _, _)).Times(1); | 390 SaveInstrument(_, _, _)).Times(1); |
372 | 391 |
373 scoped_ptr<wallet::WalletItems> wallet_items = wallet::GetTestWalletItems(); | 392 scoped_ptr<wallet::WalletItems> wallet_items = wallet::GetTestWalletItems(); |
374 wallet_items->AddAddress(wallet::GetTestShippingAddress()); | 393 wallet_items->AddAddress(wallet::GetTestShippingAddress()); |
375 controller()->OnDidGetWalletItems(wallet_items.Pass()); | 394 controller()->OnDidGetWalletItems(wallet_items.Pass()); |
376 controller()->OnSubmit(); | 395 controller()->OnAccept(); |
377 } | 396 } |
378 | 397 |
379 TEST_F(AutofillDialogControllerTest, SaveInstrumentAndAddress) { | 398 TEST_F(AutofillDialogControllerTest, SaveInstrumentAndAddress) { |
380 controller()->set_is_paying_with_wallet(true); | 399 controller()->set_is_paying_with_wallet(true); |
381 | 400 |
382 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(1); | 401 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(1); |
383 EXPECT_CALL(*controller()->GetTestingWalletClient(), | 402 EXPECT_CALL(*controller()->GetTestingWalletClient(), |
384 SaveInstrumentAndAddress(_, _, _, _)).Times(1); | 403 SaveInstrumentAndAddress(_, _, _, _)).Times(1); |
385 | 404 |
386 controller()->OnDidGetWalletItems(wallet::GetTestWalletItems()); | 405 controller()->OnDidGetWalletItems(wallet::GetTestWalletItems()); |
387 controller()->OnSubmit(); | 406 controller()->OnAccept(); |
388 } | 407 } |
389 | 408 |
390 TEST_F(AutofillDialogControllerTest, Cancel) { | 409 TEST_F(AutofillDialogControllerTest, CancelNoSave) { |
391 controller()->set_is_paying_with_wallet(true); | 410 controller()->set_is_paying_with_wallet(true); |
| 411 EXPECT_CALL(*controller()->GetTestingWalletClient(), |
| 412 SaveInstrumentAndAddress(_, _, _, _)).Times(0); |
392 | 413 |
393 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(1); | 414 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(1); |
394 | 415 |
395 controller()->OnDidGetWalletItems(wallet::GetTestWalletItems()); | 416 controller()->OnDidGetWalletItems(wallet::GetTestWalletItems()); |
396 controller()->OnCancel(); | 417 controller()->OnCancel(); |
397 } | 418 } |
398 | 419 |
399 TEST_F(AutofillDialogControllerTest, EditClickedCancelled) { | 420 TEST_F(AutofillDialogControllerTest, EditClickedCancelled) { |
400 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(1); | 421 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(1); |
401 | 422 |
402 AutofillProfile full_profile(autofill_test::GetFullProfile()); | 423 AutofillProfile full_profile(autofill_test::GetFullProfile()); |
403 const string16 kEmail = ASCIIToUTF16("first@johndoe.com"); | 424 const string16 kEmail = ASCIIToUTF16("first@johndoe.com"); |
404 full_profile.SetRawInfo(EMAIL_ADDRESS, kEmail); | 425 full_profile.SetRawInfo(EMAIL_ADDRESS, kEmail); |
405 controller()->GetTestingManager()->AddTestingProfile(&full_profile); | 426 controller()->GetTestingManager()->AddTestingProfile(&full_profile); |
406 | 427 |
407 ui::MenuModel* email_model = | 428 ui::MenuModel* email_model = |
408 controller()->MenuModelForSection(SECTION_EMAIL); | 429 controller()->MenuModelForSection(SECTION_EMAIL); |
409 EXPECT_EQ(2, email_model->GetItemCount()); | 430 EXPECT_EQ(2, email_model->GetItemCount()); |
410 | 431 |
411 // When unedited, the autofilled_value should be empty. | 432 // When unedited, the autofilled_value should be empty. |
412 email_model->ActivatedAt(0); | 433 email_model->ActivatedAt(0); |
413 const DetailInputs& inputs0 = | 434 const DetailInputs& inputs0 = |
414 controller()->RequestedFieldsForSection(SECTION_EMAIL); | 435 controller()->RequestedFieldsForSection(SECTION_EMAIL); |
415 EXPECT_EQ(string16(), inputs0[0].autofilled_value); | 436 EXPECT_EQ(string16(), inputs0[0].autofilled_value); |
416 EXPECT_EQ(kEmail, controller()->SuggestionTextForSection(SECTION_EMAIL)); | 437 EXPECT_EQ(kEmail, |
| 438 controller()->SuggestionStateForSection(SECTION_EMAIL).text); |
417 | 439 |
418 // When edited, the autofilled_value should contain the value. | 440 // When edited, the autofilled_value should contain the value. |
419 controller()->EditClickedForSection(SECTION_EMAIL); | 441 controller()->EditClickedForSection(SECTION_EMAIL); |
420 const DetailInputs& inputs1 = | 442 const DetailInputs& inputs1 = |
421 controller()->RequestedFieldsForSection(SECTION_EMAIL); | 443 controller()->RequestedFieldsForSection(SECTION_EMAIL); |
422 EXPECT_EQ(kEmail, inputs1[0].autofilled_value); | 444 EXPECT_EQ(kEmail, inputs1[0].autofilled_value); |
423 EXPECT_EQ(string16(), controller()->SuggestionTextForSection(SECTION_EMAIL)); | 445 EXPECT_EQ(string16(), |
| 446 controller()->SuggestionStateForSection(SECTION_EMAIL).text); |
424 | 447 |
425 // When edit is cancelled, the autofilled_value should be empty. | 448 // When edit is cancelled, the autofilled_value should be empty. |
426 controller()->EditCancelledForSection(SECTION_EMAIL); | 449 controller()->EditCancelledForSection(SECTION_EMAIL); |
427 const DetailInputs& inputs2 = | 450 const DetailInputs& inputs2 = |
428 controller()->RequestedFieldsForSection(SECTION_EMAIL); | 451 controller()->RequestedFieldsForSection(SECTION_EMAIL); |
429 EXPECT_EQ(kEmail, controller()->SuggestionTextForSection(SECTION_EMAIL)); | 452 EXPECT_EQ(kEmail, |
| 453 controller()->SuggestionStateForSection(SECTION_EMAIL).text); |
430 EXPECT_EQ(string16(), inputs2[0].autofilled_value); | 454 EXPECT_EQ(string16(), inputs2[0].autofilled_value); |
431 } | 455 } |
432 | 456 |
| 457 TEST_F(AutofillDialogControllerTest, VerifyCvv) { |
| 458 controller()->set_is_paying_with_wallet(true); |
| 459 |
| 460 EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(2); |
| 461 EXPECT_CALL(*controller()->GetTestingWalletClient(), |
| 462 GetFullWallet(_)).Times(1); |
| 463 EXPECT_CALL(*controller()->GetTestingWalletClient(), |
| 464 AuthenticateInstrument(_, _, _)).Times(1); |
| 465 |
| 466 scoped_ptr<wallet::WalletItems> wallet_items = wallet::GetTestWalletItems(); |
| 467 wallet_items->AddInstrument(wallet::GetTestMaskedInstrument()); |
| 468 wallet_items->AddAddress(wallet::GetTestShippingAddress()); |
| 469 controller()->OnDidGetWalletItems(wallet_items.Pass()); |
| 470 controller()->OnAccept(); |
| 471 |
| 472 EXPECT_EQ(0U, CountNotificationsOfType(DialogNotification::REQUIRED_ACTION)); |
| 473 EXPECT_TRUE(controller()->SectionIsActive(SECTION_EMAIL)); |
| 474 EXPECT_TRUE(controller()->SectionIsActive(SECTION_SHIPPING)); |
| 475 EXPECT_TRUE(controller()->SectionIsActive(SECTION_CC_BILLING)); |
| 476 EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); |
| 477 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL)); |
| 478 |
| 479 SuggestionState suggestion_state = |
| 480 controller()->SuggestionStateForSection(SECTION_CC_BILLING); |
| 481 EXPECT_TRUE(suggestion_state.extra_text.empty()); |
| 482 |
| 483 base::DictionaryValue dict; |
| 484 scoped_ptr<base::ListValue> list(new base::ListValue()); |
| 485 list->AppendString("verify_cvv"); |
| 486 dict.Set("required_action", list.release()); |
| 487 controller()->OnDidGetFullWallet(wallet::FullWallet::CreateFullWallet(dict)); |
| 488 |
| 489 EXPECT_EQ(1U, CountNotificationsOfType(DialogNotification::REQUIRED_ACTION)); |
| 490 EXPECT_FALSE(controller()->SectionIsActive(SECTION_EMAIL)); |
| 491 EXPECT_FALSE(controller()->SectionIsActive(SECTION_SHIPPING)); |
| 492 EXPECT_TRUE(controller()->SectionIsActive(SECTION_CC_BILLING)); |
| 493 |
| 494 suggestion_state = |
| 495 controller()->SuggestionStateForSection(SECTION_CC_BILLING); |
| 496 EXPECT_FALSE(suggestion_state.extra_text.empty()); |
| 497 EXPECT_EQ( |
| 498 0, controller()->MenuModelForSection(SECTION_CC_BILLING)->GetItemCount()); |
| 499 |
| 500 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); |
| 501 EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL)); |
| 502 |
| 503 controller()->OnAccept(); |
| 504 } |
| 505 |
433 } // namespace autofill | 506 } // namespace autofill |
OLD | NEW |