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.
6 #include "base/command_line.h"
7 #include "base/logging.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/test/histogram_tester.h"
14 #include "base/time/time.h"
15 #include "chrome/browser/autofill/personal_data_manager_factory.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/autofill/account_chooser_model.h"
18 #include "chrome/browser/ui/autofill/autofill_dialog_controller_impl.h"
19 #include "chrome/browser/ui/autofill/autofill_dialog_i18n_input.h"
20 #include "chrome/browser/ui/autofill/autofill_dialog_view.h"
21 #include "chrome/browser/ui/autofill/autofill_dialog_view_tester.h"
22 #include "chrome/browser/ui/autofill/chrome_autofill_client.h"
23 #include "chrome/browser/ui/autofill/data_model_wrapper.h"
24 #include "chrome/browser/ui/autofill/mock_address_validator.h"
25 #include "chrome/browser/ui/autofill/test_generated_credit_card_bubble_controller.h"
26 #include "chrome/browser/ui/browser.h"
27 #include "chrome/browser/ui/browser_tabstrip.h"
28 #include "chrome/browser/ui/tabs/tab_strip_model.h"
29 #include "chrome/common/chrome_switches.h"
30 #include "chrome/common/pref_names.h"
31 #include "chrome/common/url_constants.h"
32 #include "chrome/test/base/in_process_browser_test.h"
33 #include "chrome/test/base/ui_test_utils.h"
34 #include "components/autofill/content/browser/risk/proto/fingerprint.pb.h"
35 #include "components/autofill/content/browser/wallet/gaia_account.h"
36 #include "components/autofill/content/browser/wallet/mock_wallet_client.h"
37 #include "components/autofill/content/browser/wallet/wallet_service_url.h"
38 #include "components/autofill/content/browser/wallet/wallet_test_util.h"
39 #include "components/autofill/core/browser/autofill_metrics.h"
40 #include "components/autofill/core/browser/autofill_test_utils.h"
41 #include "components/autofill/core/browser/test_personal_data_manager.h"
42 #include "components/autofill/core/browser/validation.h"
43 #include "components/autofill/core/common/autofill_switches.h"
44 #include "components/autofill/core/common/form_data.h"
45 #include "components/autofill/core/common/form_field_data.h"
46 #include "content/public/browser/browser_thread.h"
47 #include "content/public/browser/interstitial_page.h"
48 #include "content/public/browser/navigation_details.h"
49 #include "content/public/browser/navigation_entry.h"
50 #include "content/public/browser/notification_service.h"
51 #include "content/public/browser/notification_types.h"
52 #include "content/public/browser/page_navigator.h"
53 #include "content/public/browser/web_contents.h"
54 #include "content/public/browser/web_contents_delegate.h"
55 #include "content/public/common/content_switches.h"
56 #include "content/public/common/referrer.h"
57 #include "content/public/common/url_constants.h"
58 #include "content/public/test/browser_test_utils.h"
59 #include "content/public/test/test_utils.h"
60 #include "google_apis/gaia/google_service_auth_error.h"
61 #include "net/test/spawned_test_server/spawned_test_server.h"
62 #include "testing/gmock/include/gmock/gmock.h"
63 #include "testing/gtest/include/gtest/gtest.h"
64 #include "third_party/WebKit/public/web/WebInputEvent.h"
65 #include "ui/base/page_transition_types.h"
66 #include "ui/base/window_open_disposition.h"
70 #include "base/win/windows_version.h"
71 #elif defined(OS_MACOSX)
72 #include "base/mac/scoped_nsautorelease_pool.h"
73 #include "chrome/browser/ui/cocoa/run_loop_testing.h"
76 using base::ASCIIToUTF16
;
82 using testing::Return
;
85 void MockCallback(AutofillClient::RequestAutocompleteResult
,
86 const base::string16
& message
,
87 const FormStructure
*) {
90 class TestAutofillDialogController
: public AutofillDialogControllerImpl
{
92 TestAutofillDialogController(
93 content::WebContents
* contents
,
94 const FormData
& form_data
,
95 scoped_refptr
<content::MessageLoopRunner
> runner
)
96 : AutofillDialogControllerImpl(contents
,
99 base::Bind(&MockCallback
)),
101 Profile::FromBrowserContext(contents
->GetBrowserContext())->
102 GetRequestContext(), this, form_data
.origin
),
103 message_loop_runner_(runner
),
104 use_validation_(false),
105 sign_in_user_index_(0U),
106 weak_ptr_factory_(this) {
109 Profile::FromBrowserContext(contents
->GetBrowserContext())->GetPrefs(),
113 virtual ~TestAutofillDialogController() {}
115 GURL
FakeSignInUrl() const {
116 return GURL(chrome::kChromeUIVersionURL
);
119 virtual void ShowSignIn(const GURL
& url
) override
{
120 AutofillDialogControllerImpl::ShowSignIn(FakeSignInUrl());
123 virtual void ViewClosed() override
{
124 message_loop_runner_
->Quit();
125 AutofillDialogControllerImpl::ViewClosed();
128 virtual base::string16
InputValidityMessage(
129 DialogSection section
,
130 ServerFieldType type
,
131 const base::string16
& value
) override
{
132 if (!use_validation_
)
133 return base::string16();
134 return AutofillDialogControllerImpl::InputValidityMessage(
135 section
, type
, value
);
138 virtual ValidityMessages
InputsAreValid(
139 DialogSection section
,
140 const FieldValueMap
& inputs
) override
{
141 if (!use_validation_
)
142 return ValidityMessages();
143 return AutofillDialogControllerImpl::InputsAreValid(section
, inputs
);
146 // Saving to Chrome is tested in AutofillDialogControllerImpl unit tests.
147 // TODO(estade): test that the view defaults to saving to Chrome.
148 virtual bool ShouldOfferToSaveInChrome() const override
{
152 void ForceFinishSubmit() {
156 // Increase visibility for testing.
157 using AutofillDialogControllerImpl::view
;
158 using AutofillDialogControllerImpl::popup_input_type
;
160 MOCK_METHOD0(LoadRiskFingerprintData
, void());
162 virtual std::vector
<DialogNotification
> CurrentNotifications() override
{
163 return notifications_
;
166 void set_notifications(const std::vector
<DialogNotification
>& notifications
) {
167 notifications_
= notifications
;
170 TestPersonalDataManager
* GetTestingManager() {
171 return &test_manager_
;
174 MockAddressValidator
* GetMockValidator() {
175 return &mock_validator_
;
178 using AutofillDialogControllerImpl::IsEditingExistingData
;
179 using AutofillDialogControllerImpl::IsManuallyEditingSection
;
180 using AutofillDialogControllerImpl::IsPayingWithWallet
;
181 using AutofillDialogControllerImpl::IsSubmitPausedOn
;
182 using AutofillDialogControllerImpl::OnDidLoadRiskFingerprintData
;
183 using AutofillDialogControllerImpl::AccountChooserModelForTesting
;
184 using AutofillDialogControllerImpl::
185 ClearLastWalletItemsFetchTimestampForTesting
;
187 void set_use_validation(bool use_validation
) {
188 use_validation_
= use_validation
;
191 base::WeakPtr
<TestAutofillDialogController
> AsWeakPtr() {
192 return weak_ptr_factory_
.GetWeakPtr();
195 wallet::MockWalletClient
* GetTestingWalletClient() {
196 return &mock_wallet_client_
;
199 void set_sign_in_user_index(size_t sign_in_user_index
) {
200 sign_in_user_index_
= sign_in_user_index
;
204 virtual PersonalDataManager
* GetManager() const override
{
205 return &const_cast<TestAutofillDialogController
*>(this)->test_manager_
;
208 virtual AddressValidator
* GetValidator() override
{
209 return &mock_validator_
;
212 virtual wallet::WalletClient
* GetWalletClient() override
{
213 return &mock_wallet_client_
;
216 virtual bool IsSignInContinueUrl(const GURL
& url
, size_t* user_index
) const
218 *user_index
= sign_in_user_index_
;
219 return url
== wallet::GetSignInContinueUrl();
223 TestPersonalDataManager test_manager_
;
224 testing::NiceMock
<MockAddressValidator
> mock_validator_
;
225 testing::NiceMock
<wallet::MockWalletClient
> mock_wallet_client_
;
226 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner_
;
227 bool use_validation_
;
229 // A list of notifications to show in the notification area of the dialog.
230 // This is used to control what |CurrentNotifications()| returns for testing.
231 std::vector
<DialogNotification
> notifications_
;
233 // The user index that is assigned in IsSignInContinueUrl().
234 size_t sign_in_user_index_
;
236 // Allows generation of WeakPtrs, so controller liveness can be tested.
237 base::WeakPtrFactory
<TestAutofillDialogController
> weak_ptr_factory_
;
239 DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogController
);
242 // This is a copy of ui_test_utils::UrlLoadObserver, except it observes
243 // NAV_ENTRY_COMMITTED instead of LOAD_STOP. This is to match the notification
244 // that AutofillDialogControllerImpl observes. Since NAV_ENTRY_COMMITTED comes
245 // before LOAD_STOP, and the controller deletes the web contents after receiving
246 // the former, we will sometimes fail to observe a LOAD_STOP.
247 // TODO(estade): Should the controller observe LOAD_STOP instead?
248 class NavEntryCommittedObserver
: public content::WindowedNotificationObserver
{
250 NavEntryCommittedObserver(const GURL
& url
,
251 const content::NotificationSource
& source
)
252 : WindowedNotificationObserver(content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
256 ~NavEntryCommittedObserver() override
{}
258 // content::NotificationObserver:
259 void Observe(int type
,
260 const content::NotificationSource
& source
,
261 const content::NotificationDetails
& details
) override
{
262 content::LoadCommittedDetails
* load_details
=
263 content::Details
<content::LoadCommittedDetails
>(details
).ptr();
264 if (load_details
->entry
->GetVirtualURL() != url_
)
267 WindowedNotificationObserver::Observe(type
, source
, details
);
273 DISALLOW_COPY_AND_ASSIGN(NavEntryCommittedObserver
);
278 class AutofillDialogControllerTest
: public InProcessBrowserTest
{
280 AutofillDialogControllerTest() : controller_(NULL
) {}
281 ~AutofillDialogControllerTest() override
{}
283 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
284 command_line
->AppendSwitch(::switches::kReduceSecurityForTesting
);
287 void SetUpOnMainThread() override
{
288 autofill::test::DisableSystemServices(browser()->profile()->GetPrefs());
289 InitializeController();
293 bool SectionHasField(DialogSection section
, ServerFieldType type
) {
294 const DetailInputs
& fields
=
295 controller()->RequestedFieldsForSection(section
);
296 for (size_t i
= 0; i
< fields
.size(); ++i
) {
297 if (type
== fields
[i
].type
)
303 // A helper function that cycles the MessageLoop, and on Mac, the Cocoa run
304 // loop. It also drains the NSAutoreleasePool.
305 void CycleRunLoops() {
306 content::RunAllPendingInMessageLoop();
307 #if defined(OS_MACOSX)
308 chrome::testing::NSRunLoopRunAllPending();
309 AutoreleasePool()->Recycle();
313 void InitializeControllerWithoutShowing() {
318 form
.name
= ASCIIToUTF16("TestForm");
319 form
.user_submitted
= true;
322 field
.autocomplete_attribute
= "shipping tel";
323 form
.fields
.push_back(field
);
326 cc
.autocomplete_attribute
= "cc-number";
327 form
.fields
.push_back(cc
);
329 test_generated_bubble_controller_
=
330 new testing::NiceMock
<TestGeneratedCreditCardBubbleController
>(
331 GetActiveWebContents());
332 ASSERT_TRUE(test_generated_bubble_controller_
->IsInstalled());
334 message_loop_runner_
= new content::MessageLoopRunner
;
335 controller_
= new TestAutofillDialogController(
336 GetActiveWebContents(),
338 message_loop_runner_
);
341 void InitializeController() {
342 InitializeControllerWithoutShowing();
344 CycleRunLoops(); // Ensures dialog is fully visible.
347 content::WebContents
* GetActiveWebContents() {
348 return browser()->tab_strip_model()->GetActiveWebContents();
351 content::RenderViewHost
* GetRenderViewHost() {
352 return GetActiveWebContents()->GetRenderViewHost();
355 scoped_ptr
<AutofillDialogViewTester
> GetViewTester() {
356 return AutofillDialogViewTester::For(controller()->view()).Pass();
359 TestAutofillDialogController
* controller() { return controller_
; }
361 void RunMessageLoop() {
362 message_loop_runner_
->Run();
365 // Loads an HTML page in |GetActiveWebContents()| with markup as follows:
366 // <form>|form_inner_html|</form>. After loading, emulates a click event on
367 // the page as requestAutocomplete() must be in response to a user gesture.
368 // Returns the |AutofillDialogControllerImpl| created by this invocation.
369 AutofillDialogControllerImpl
* SetUpHtmlAndInvoke(
370 const std::string
& form_inner_html
) {
371 content::WebContents
* contents
= GetActiveWebContents();
372 ChromeAutofillClient
* client
=
373 ChromeAutofillClient::FromWebContents(contents
);
374 CHECK(!client
->GetDialogControllerForTesting());
376 ui_test_utils::NavigateToURL(
377 browser(), GURL(std::string("data:text/html,") +
381 "<form>" + form_inner_html
+ "</form>"
383 "var invalidEvents = [];"
384 "function recordInvalid(e) {"
385 "if (e.type != 'invalid') throw 'only invalid events allowed';"
386 "invalidEvents.push(e);"
388 "function send(msg) {"
389 "domAutomationController.setAutomationId(0);"
390 "domAutomationController.send(msg);"
392 "document.forms[0].onautocompleteerror = function(e) {"
393 "send('error: ' + e.reason);"
395 "document.forms[0].onautocomplete = function() {"
398 "window.onclick = function() {"
399 "var inputs = document.forms[0].querySelectorAll('input');"
400 "for (var i = 0; i < inputs.length; ++i) {"
401 "inputs[i].oninvalid = recordInvalid;"
403 "document.forms[0].requestAutocomplete();"
406 "function loadIframe() {"
407 " var iframe = document.createElement('iframe');"
408 " iframe.onload = function() {"
409 " send('iframe loaded');"
411 " iframe.src = 'about:blank';"
412 " document.body.appendChild(iframe);"
414 "function getValueForFieldOfType(type) {"
415 " var fields = document.getElementsByTagName('input');"
416 " for (var i = 0; i < fields.length; i++) {"
417 " if (fields[i].autocomplete == type) {"
418 " send(fields[i].value);"
429 AutofillDialogControllerImpl
* controller
=
430 static_cast<AutofillDialogControllerImpl
*>(
431 client
->GetDialogControllerForTesting());
435 // Loads an html page on a provided server, the causes it to launch rAc.
436 // Returns whether rAc succesfully launched.
437 bool RunTestPage(const net::SpawnedTestServer
& server
) {
438 GURL url
= server
.GetURL(
439 "files/request_autocomplete/test_page.html");
440 ui_test_utils::NavigateToURL(browser(), url
);
442 // Pass through the broken SSL interstitial, if any.
443 content::WebContents
* contents
= GetActiveWebContents();
444 content::InterstitialPage
* interstitial_page
=
445 contents
->GetInterstitialPage();
446 if (interstitial_page
) {
447 ui_test_utils::UrlLoadObserver
observer(
449 content::Source
<content::NavigationController
>(
450 &contents
->GetController()));
451 interstitial_page
->Proceed();
457 ChromeAutofillClient
* client
=
458 ChromeAutofillClient::FromWebContents(contents
);
459 AutofillDialogControllerImpl
* controller
=
460 static_cast<AutofillDialogControllerImpl
*>(
461 client
->GetDialogControllerForTesting());
465 void RunTestPageInIframe(const net::SpawnedTestServer
& server
) {
466 InitializeDOMMessageQueue();
467 GURL iframe_url
= server
.GetURL(
468 "files/request_autocomplete/test_page.html");
470 ui_test_utils::NavigateToURL(
471 browser(), GURL(std::string("data:text/html,") +
475 "<iframe style='position: fixed;"
478 "id='racFrame'></iframe>"
480 "function send(msg) {"
481 "domAutomationController.setAutomationId(0);"
482 "domAutomationController.send(msg);"
484 "var racFrame = document.getElementById('racFrame');"
485 "racFrame.onload = function() {"
486 "send('iframe loaded');"
488 "racFrame.src = \"" + iframe_url
.spec() + "\";"
489 "function navigateFrame() {"
490 "racFrame.src = 'about:blank';"
496 ChromeAutofillClient
* client
=
497 ChromeAutofillClient::FromWebContents(GetActiveWebContents());
498 ExpectDomMessage("iframe loaded");
499 EXPECT_FALSE(client
->GetDialogControllerForTesting());
501 EXPECT_TRUE(client
->GetDialogControllerForTesting());
504 // Wait for a message from the DOM automation controller (from JS in the
505 // page). Requires |SetUpHtmlAndInvoke()| be called first.
506 void ExpectDomMessage(const std::string
& expected
) {
508 ASSERT_TRUE(dom_message_queue_
->WaitForMessage(&message
));
509 dom_message_queue_
->ClearQueue();
510 EXPECT_EQ("\"" + expected
+ "\"", message
);
513 void InitiateDialog() {
514 InitializeDOMMessageQueue();
515 // Triggers the onclick handler which invokes requestAutocomplete().
516 content::WebContents
* contents
= GetActiveWebContents();
517 content::SimulateMouseClick(contents
, 0, blink::WebMouseEvent::ButtonLeft
);
518 ExpectDomMessage("clicked");
521 void InitializeDOMMessageQueue() {
522 dom_message_queue_
.reset(new content::DOMMessageQueue
);
525 // Returns the value filled into the first field with autocomplete attribute
526 // equal to |autocomplete_type|, or an empty string if there is no such field.
527 std::string
GetValueForHTMLFieldOfType(const std::string
& autocomplete_type
) {
528 std::string script
= "getValueForFieldOfType('" + autocomplete_type
+ "');";
530 EXPECT_TRUE(content::ExecuteScriptAndExtractString(GetRenderViewHost(),
536 void AddCreditcardToProfile(Profile
* profile
, const CreditCard
& card
) {
537 PersonalDataManagerFactory::GetForProfile(profile
)->AddCreditCard(card
);
541 void AddAutofillProfileToProfile(Profile
* profile
,
542 const AutofillProfile
& autofill_profile
) {
543 PersonalDataManagerFactory::GetForProfile(profile
)->AddProfile(
548 TestGeneratedCreditCardBubbleController
* test_generated_bubble_controller() {
549 return test_generated_bubble_controller_
;
553 void WaitForWebDB() {
554 content::RunAllPendingInMessageLoop(content::BrowserThread::DB
);
557 TestAutofillDialogController
* controller_
; // Weak reference.
558 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner_
;
559 scoped_ptr
<content::DOMMessageQueue
> dom_message_queue_
;
561 // Weak; owned by the active web contents.
562 TestGeneratedCreditCardBubbleController
* test_generated_bubble_controller_
;
564 DISALLOW_COPY_AND_ASSIGN(AutofillDialogControllerTest
);
567 // Submit the form data.
568 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, Submit
) {
569 base::HistogramTester histogram
;
570 GetViewTester()->SubmitForTesting();
573 histogram
.ExpectTotalCount("RequestAutocomplete.UiDuration.Submit", 1);
574 histogram
.ExpectTotalCount("RequestAutocomplete.UiDuration.Cancel", 0);
577 // Cancel out of the dialog.
578 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, Cancel
) {
579 base::HistogramTester histogram
;
580 GetViewTester()->CancelForTesting();
583 histogram
.ExpectTotalCount("RequestAutocomplete.UiDuration.Submit", 0);
584 histogram
.ExpectTotalCount("RequestAutocomplete.UiDuration.Cancel", 1);
585 histogram
.ExpectUniqueSample(
586 "RequestAutocomplete.DismissalState",
587 AutofillMetrics::DIALOG_CANCELED_NO_INVALID_FIELDS
, 1);
590 // Take some other action that dismisses the dialog.
591 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, Hide
) {
592 base::HistogramTester histogram
;
593 controller()->Hide();
597 histogram
.ExpectTotalCount("RequestAutocomplete.UiDuration.Submit", 0);
598 histogram
.ExpectTotalCount("RequestAutocomplete.UiDuration.Cancel", 1);
599 histogram
.ExpectUniqueSample(
600 "RequestAutocomplete.DismissalState",
601 AutofillMetrics::DIALOG_CANCELED_NO_INVALID_FIELDS
, 1);
604 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, CancelWithSuggestions
) {
605 base::HistogramTester histogram
;
607 CreditCard
card(test::GetVerifiedCreditCard());
608 controller()->GetTestingManager()->AddTestingCreditCard(&card
);
609 AutofillProfile
profile(test::GetVerifiedProfile());
610 controller()->GetTestingManager()->AddTestingProfile(&profile
);
612 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_CC
));
613 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_BILLING
));
614 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_SHIPPING
));
616 GetViewTester()->CancelForTesting();
619 histogram
.ExpectTotalCount("RequestAutocomplete.UiDuration.Submit", 0);
620 histogram
.ExpectTotalCount("RequestAutocomplete.UiDuration.Cancel", 1);
621 histogram
.ExpectUniqueSample("RequestAutocomplete.DismissalState",
622 AutofillMetrics::DIALOG_CANCELED_NO_EDITS
, 1);
625 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, AcceptWithSuggestions
) {
626 base::HistogramTester histogram
;
627 CreditCard
card(test::GetVerifiedCreditCard());
628 controller()->GetTestingManager()->AddTestingCreditCard(&card
);
629 AutofillProfile
profile(test::GetVerifiedProfile());
630 controller()->GetTestingManager()->AddTestingProfile(&profile
);
632 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_CC
));
633 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_BILLING
));
634 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_SHIPPING
));
636 GetViewTester()->SubmitForTesting();
639 histogram
.ExpectTotalCount("RequestAutocomplete.UiDuration.Submit", 1);
640 histogram
.ExpectTotalCount("RequestAutocomplete.UiDuration.Cancel", 0);
641 histogram
.ExpectUniqueSample(
642 "RequestAutocomplete.DismissalState",
643 AutofillMetrics::DIALOG_ACCEPTED_EXISTING_AUTOFILL_DATA
, 1);
646 // Ensure that Hide() will only destroy the controller object after the
647 // message loop has run. Otherwise, there may be read-after-free issues
648 // during some tests.
649 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, DeferredDestruction
) {
650 base::WeakPtr
<TestAutofillDialogController
> weak_ptr
=
651 controller()->AsWeakPtr();
652 EXPECT_TRUE(weak_ptr
.get());
654 controller()->Hide();
655 EXPECT_TRUE(weak_ptr
.get());
658 EXPECT_FALSE(weak_ptr
.get());
661 // Ensure that the expected metric is logged when the dialog is closed during
663 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, CloseDuringSignin
) {
664 base::HistogramTester histogram
;
665 controller()->SignInLinkClicked();
667 GetViewTester()->CancelForTesting();
671 histogram
.ExpectTotalCount("RequestAutocomplete.UiDuration.Submit", 0);
672 histogram
.ExpectTotalCount("RequestAutocomplete.UiDuration.Cancel", 1);
673 histogram
.ExpectUniqueSample("RequestAutocomplete.DismissalState",
674 AutofillMetrics::DIALOG_CANCELED_DURING_SIGNIN
,
678 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, FillInputFromAutofill
) {
679 AutofillProfile
full_profile(test::GetFullProfile());
680 const base::string16
formatted_phone(ASCIIToUTF16("+1 (310) 555 1234"));
681 full_profile
.SetRawInfo(PHONE_HOME_WHOLE_NUMBER
, formatted_phone
);
682 controller()->GetTestingManager()->AddTestingProfile(&full_profile
);
684 // Dialog is already asking for a new billing address.
685 EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_BILLING
));
687 // Select "Add new shipping address...".
688 ui::MenuModel
* model
= controller()->MenuModelForSection(SECTION_SHIPPING
);
689 model
->ActivatedAt(model
->GetItemCount() - 2);
690 ASSERT_TRUE(controller()->IsManuallyEditingSection(SECTION_SHIPPING
));
692 // Enter something in a shipping input.
693 const DetailInputs
& inputs
=
694 controller()->RequestedFieldsForSection(SECTION_SHIPPING
);
695 const ServerFieldType triggering_type
= inputs
[0].type
;
696 base::string16 value
= full_profile
.GetRawInfo(triggering_type
);
697 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
698 view
->SetTextContentsOfInput(triggering_type
,
699 value
.substr(0, value
.size() / 2));
700 view
->ActivateInput(triggering_type
);
702 ASSERT_EQ(triggering_type
, controller()->popup_input_type());
703 controller()->DidAcceptSuggestion(base::string16(), 0);
705 // All inputs should be filled.
706 AutofillProfileWrapper
wrapper(&full_profile
);
707 for (size_t i
= 0; i
< inputs
.size(); ++i
) {
708 EXPECT_EQ(wrapper
.GetInfoForDisplay(AutofillType(inputs
[i
].type
)),
709 view
->GetTextContentsOfInput(inputs
[i
].type
));
711 // Double check the correct formatting is used for the phone number.
712 if (inputs
[i
].type
== PHONE_HOME_WHOLE_NUMBER
)
713 EXPECT_EQ(formatted_phone
, view
->GetTextContentsOfInput(inputs
[i
].type
));
716 // Inputs from the other section (billing) should be left alone.
717 const DetailInputs
& other_section_inputs
=
718 controller()->RequestedFieldsForSection(SECTION_BILLING
);
719 for (size_t i
= 0; i
< inputs
.size(); ++i
) {
720 base::string16 input_value
=
721 view
->GetTextContentsOfInput(other_section_inputs
[i
].type
);
722 // If there's a combobox, the string should be non-empty.
723 if (controller()->ComboboxModelForAutofillType(
724 other_section_inputs
[i
].type
)) {
725 EXPECT_NE(base::string16(), input_value
);
727 EXPECT_EQ(base::string16(), input_value
);
731 // Now simulate some user edits and try again.
732 std::vector
<base::string16
> expectations
;
733 for (size_t i
= 0; i
< inputs
.size(); ++i
) {
734 if (controller()->ComboboxModelForAutofillType(inputs
[i
].type
)) {
735 expectations
.push_back(base::string16());
738 base::string16 users_input
= i
% 2 == 0 ? base::string16()
739 : ASCIIToUTF16("dummy");
740 view
->SetTextContentsOfInput(inputs
[i
].type
, users_input
);
741 // Empty inputs should be filled, others should be left alone.
742 base::string16 expectation
=
743 inputs
[i
].type
== triggering_type
|| users_input
.empty() ?
744 wrapper
.GetInfoForDisplay(AutofillType(inputs
[i
].type
)) :
746 expectations
.push_back(expectation
);
749 view
->SetTextContentsOfInput(triggering_type
,
750 value
.substr(0, value
.size() / 2));
751 view
->ActivateInput(triggering_type
);
752 ASSERT_EQ(triggering_type
, controller()->popup_input_type());
753 controller()->DidAcceptSuggestion(base::string16(), 0);
755 for (size_t i
= 0; i
< inputs
.size(); ++i
) {
756 if (controller()->ComboboxModelForAutofillType(inputs
[i
].type
))
758 EXPECT_EQ(expectations
[i
], view
->GetTextContentsOfInput(inputs
[i
].type
));
761 base::HistogramTester histogram
;
762 view
->SubmitForTesting();
763 histogram
.ExpectUniqueSample(
764 "RequestAutocomplete.DismissalState",
765 AutofillMetrics::DIALOG_ACCEPTED_SAVE_TO_AUTOFILL
, 1);
768 // This test makes sure that picking a profile variant in the Autofill
769 // popup works as expected.
770 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
771 FillInputFromAutofillVariant
) {
772 AutofillProfile
full_profile(test::GetFullProfile());
774 // Set up some variant data.
775 std::vector
<base::string16
> names
;
776 names
.push_back(ASCIIToUTF16("John Doe"));
777 names
.push_back(ASCIIToUTF16("Jane Doe"));
778 full_profile
.SetRawMultiInfo(NAME_FULL
, names
);
779 std::vector
<base::string16
> emails
;
780 emails
.push_back(ASCIIToUTF16("user@example.com"));
781 emails
.push_back(ASCIIToUTF16("admin@example.com"));
782 full_profile
.SetRawMultiInfo(EMAIL_ADDRESS
, emails
);
783 controller()->GetTestingManager()->AddTestingProfile(&full_profile
);
785 const DetailInputs
& inputs
=
786 controller()->RequestedFieldsForSection(SECTION_BILLING
);
787 const ServerFieldType triggering_type
= inputs
[0].type
;
788 EXPECT_EQ(NAME_BILLING_FULL
, triggering_type
);
789 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
790 view
->ActivateInput(triggering_type
);
792 ASSERT_EQ(triggering_type
, controller()->popup_input_type());
794 // Choose the variant suggestion.
795 controller()->DidAcceptSuggestion(base::string16(), 1);
797 // All inputs should be filled.
798 AutofillProfileWrapper
wrapper(
799 &full_profile
, AutofillType(NAME_BILLING_FULL
), 1);
800 for (size_t i
= 0; i
< inputs
.size(); ++i
) {
801 EXPECT_EQ(wrapper
.GetInfoForDisplay(AutofillType(inputs
[i
].type
)),
802 view
->GetTextContentsOfInput(inputs
[i
].type
));
805 // Make sure the wrapper applies the variant index to the right group.
806 EXPECT_EQ(names
[1], wrapper
.GetInfo(AutofillType(NAME_BILLING_FULL
)));
807 // Make sure the wrapper doesn't apply the variant index to the wrong group.
808 EXPECT_EQ(emails
[0], wrapper
.GetInfo(AutofillType(EMAIL_ADDRESS
)));
811 // Tests that changing the value of a CC expiration date combobox works as
812 // expected when Autofill is used to fill text inputs.
814 // Flaky on Win7, WinXP, and Win Aura. http://crbug.com/270314.
816 #define MAYBE_FillComboboxFromAutofill DISABLED_FillComboboxFromAutofill
818 #define MAYBE_FillComboboxFromAutofill FillComboboxFromAutofill
820 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
821 MAYBE_FillComboboxFromAutofill
) {
823 test::SetCreditCardInfo(&card1
, "JJ Smith", "4111111111111111", "12", "2018");
824 controller()->GetTestingManager()->AddTestingCreditCard(&card1
);
826 test::SetCreditCardInfo(&card2
, "B Bird", "3111111111111111", "11", "2017");
827 controller()->GetTestingManager()->AddTestingCreditCard(&card2
);
828 AutofillProfile
full_profile(test::GetFullProfile());
829 controller()->GetTestingManager()->AddTestingProfile(&full_profile
);
831 const DetailInputs
& inputs
=
832 controller()->RequestedFieldsForSection(SECTION_CC
);
833 const ServerFieldType triggering_type
= inputs
[0].type
;
834 base::string16 value
= card1
.GetRawInfo(triggering_type
);
835 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
836 view
->SetTextContentsOfInput(triggering_type
,
837 value
.substr(0, value
.size() / 2));
838 view
->ActivateInput(triggering_type
);
840 ASSERT_EQ(triggering_type
, controller()->popup_input_type());
841 controller()->DidAcceptSuggestion(base::string16(), 0);
843 // All inputs should be filled.
844 AutofillCreditCardWrapper
wrapper1(&card1
);
845 for (size_t i
= 0; i
< inputs
.size(); ++i
) {
846 EXPECT_EQ(wrapper1
.GetInfo(AutofillType(inputs
[i
].type
)),
847 view
->GetTextContentsOfInput(inputs
[i
].type
));
850 // Try again with different data. Only expiration date and the triggering
851 // input should be overwritten.
852 value
= card2
.GetRawInfo(triggering_type
);
853 view
->SetTextContentsOfInput(triggering_type
,
854 value
.substr(0, value
.size() / 2));
855 view
->ActivateInput(triggering_type
);
856 ASSERT_EQ(triggering_type
, controller()->popup_input_type());
857 controller()->DidAcceptSuggestion(base::string16(), 0);
859 AutofillCreditCardWrapper
wrapper2(&card2
);
860 for (size_t i
= 0; i
< inputs
.size(); ++i
) {
861 const ServerFieldType type
= inputs
[i
].type
;
862 if (type
== triggering_type
||
863 type
== CREDIT_CARD_EXP_MONTH
||
864 type
== CREDIT_CARD_EXP_4_DIGIT_YEAR
) {
865 EXPECT_EQ(wrapper2
.GetInfo(AutofillType(type
)),
866 view
->GetTextContentsOfInput(type
));
867 } else if (type
== CREDIT_CARD_VERIFICATION_CODE
) {
868 EXPECT_TRUE(view
->GetTextContentsOfInput(type
).empty());
870 EXPECT_EQ(wrapper1
.GetInfo(AutofillType(type
)),
871 view
->GetTextContentsOfInput(type
));
875 // Now fill from a profile. It should not overwrite any CC info.
876 const DetailInputs
& billing_inputs
=
877 controller()->RequestedFieldsForSection(SECTION_BILLING
);
878 const ServerFieldType billing_triggering_type
= billing_inputs
[0].type
;
879 value
= full_profile
.GetRawInfo(triggering_type
);
880 view
->SetTextContentsOfInput(billing_triggering_type
,
881 value
.substr(0, value
.size() / 2));
882 view
->ActivateInput(billing_triggering_type
);
884 ASSERT_EQ(billing_triggering_type
, controller()->popup_input_type());
885 controller()->DidAcceptSuggestion(base::string16(), 0);
887 for (size_t i
= 0; i
< inputs
.size(); ++i
) {
888 const ServerFieldType type
= inputs
[i
].type
;
889 if (type
== triggering_type
||
890 type
== CREDIT_CARD_EXP_MONTH
||
891 type
== CREDIT_CARD_EXP_4_DIGIT_YEAR
) {
892 EXPECT_EQ(wrapper2
.GetInfo(AutofillType(type
)),
893 view
->GetTextContentsOfInput(type
));
894 } else if (type
== CREDIT_CARD_VERIFICATION_CODE
) {
895 EXPECT_TRUE(view
->GetTextContentsOfInput(type
).empty());
897 EXPECT_EQ(wrapper1
.GetInfo(AutofillType(type
)),
898 view
->GetTextContentsOfInput(type
));
903 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, ShouldShowErrorBubble
) {
904 controller()->set_use_validation(true);
905 EXPECT_TRUE(controller()->ShouldShowErrorBubble());
907 CreditCard
card(test::GetCreditCard());
908 ASSERT_FALSE(card
.IsVerified());
909 controller()->GetTestingManager()->AddTestingCreditCard(&card
);
911 EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_CC
));
912 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
913 view
->SetTextContentsOfInput(
915 card
.GetRawInfo(CREDIT_CARD_NUMBER
).substr(0, 1));
917 view
->ActivateInput(CREDIT_CARD_NUMBER
);
918 EXPECT_FALSE(controller()->ShouldShowErrorBubble());
920 controller()->FocusMoved();
921 EXPECT_TRUE(controller()->ShouldShowErrorBubble());
923 base::HistogramTester histogram
;
924 controller()->Hide();
925 histogram
.ExpectUniqueSample(
926 "RequestAutocomplete.DismissalState",
927 AutofillMetrics::DIALOG_CANCELED_WITH_INVALID_FIELDS
, 1);
930 // Ensure that expired cards trigger invalid suggestions.
931 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, ExpiredCard
) {
932 CreditCard
verified_card(test::GetCreditCard());
933 verified_card
.set_origin("Chrome settings");
934 ASSERT_TRUE(verified_card
.IsVerified());
935 controller()->GetTestingManager()->AddTestingCreditCard(&verified_card
);
937 CreditCard
expired_card(test::GetCreditCard());
938 expired_card
.set_origin("Chrome settings");
939 expired_card
.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR
, ASCIIToUTF16("2007"));
940 ASSERT_TRUE(expired_card
.IsVerified());
942 autofill::IsValidCreditCardExpirationDate(
943 expired_card
.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR
),
944 expired_card
.GetRawInfo(CREDIT_CARD_EXP_MONTH
),
946 controller()->GetTestingManager()->AddTestingCreditCard(&expired_card
);
948 ui::MenuModel
* model
= controller()->MenuModelForSection(SECTION_CC
);
949 ASSERT_EQ(4, model
->GetItemCount());
951 ASSERT_TRUE(model
->IsItemCheckedAt(0));
952 EXPECT_FALSE(controller()->IsEditingExistingData(SECTION_CC
));
954 model
->ActivatedAt(1);
955 ASSERT_TRUE(model
->IsItemCheckedAt(1));
956 EXPECT_TRUE(controller()->IsEditingExistingData(SECTION_CC
));
959 // Notifications with long message text should not make the dialog bigger.
960 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, LongNotifications
) {
961 const gfx::Size no_notification_size
= GetViewTester()->GetSize();
962 ASSERT_GT(no_notification_size
.width(), 0);
964 std::vector
<DialogNotification
> notifications
;
965 notifications
.push_back(
966 DialogNotification(DialogNotification::DEVELOPER_WARNING
, ASCIIToUTF16(
967 "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do "
968 "eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim "
969 "ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut "
970 "aliquip ex ea commodo consequat. Duis aute irure dolor in "
971 "reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla "
972 "pariatur. Excepteur sint occaecat cupidatat non proident, sunt in "
973 "culpa qui officia deserunt mollit anim id est laborum.")));
974 controller()->set_notifications(notifications
);
975 controller()->view()->UpdateNotificationArea();
977 EXPECT_EQ(no_notification_size
.width(),
978 GetViewTester()->GetSize().width());
981 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, AutocompleteEvent
) {
982 AutofillDialogControllerImpl
* controller
=
983 SetUpHtmlAndInvoke("<input autocomplete='cc-name'>");
984 ASSERT_TRUE(controller
);
986 AddCreditcardToProfile(controller
->profile(), test::GetVerifiedCreditCard());
987 AddAutofillProfileToProfile(controller
->profile(),
988 test::GetVerifiedProfile());
990 scoped_ptr
<AutofillDialogViewTester
> view
=
991 AutofillDialogViewTester::For(
992 static_cast<TestAutofillDialogController
*>(controller
)->view());
993 view
->SetTextContentsOfSuggestionInput(SECTION_CC
, ASCIIToUTF16("123"));
994 view
->SubmitForTesting();
995 ExpectDomMessage("success");
998 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
999 AutocompleteErrorEventReasonInvalid
) {
1000 AutofillDialogControllerImpl
* controller
=
1001 SetUpHtmlAndInvoke("<input autocomplete='cc-name' pattern='.*zebra.*'>");
1002 ASSERT_TRUE(controller
);
1004 const CreditCard
& credit_card
= test::GetVerifiedCreditCard();
1006 credit_card
.GetRawInfo(CREDIT_CARD_NAME
).find(ASCIIToUTF16("zebra")) ==
1007 base::string16::npos
);
1008 AddCreditcardToProfile(controller
->profile(), credit_card
);
1009 AddAutofillProfileToProfile(controller
->profile(),
1010 test::GetVerifiedProfile());
1012 scoped_ptr
<AutofillDialogViewTester
> view
=
1013 AutofillDialogViewTester::For(
1014 static_cast<TestAutofillDialogController
*>(controller
)->view());
1015 view
->SetTextContentsOfSuggestionInput(SECTION_CC
, ASCIIToUTF16("123"));
1016 view
->SubmitForTesting();
1017 ExpectDomMessage("error: invalid");
1019 int invalid_count
= -1;
1020 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
1021 GetRenderViewHost(), "send(invalidEvents.length);", &invalid_count
));
1022 EXPECT_EQ(1, invalid_count
);
1024 std::string invalid_type
;
1025 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
1026 GetRenderViewHost(),
1027 "send(invalidEvents[0].target.autocomplete);",
1029 EXPECT_EQ("cc-name", invalid_type
);
1032 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1033 AutocompleteErrorEventReasonCancel
) {
1034 AutofillDialogControllerImpl
* controller
=
1035 SetUpHtmlAndInvoke("<input autocomplete='cc-name'>");
1036 ASSERT_TRUE(controller
);
1037 AutofillDialogViewTester::For(
1038 static_cast<TestAutofillDialogController
*>(controller
)->view())->
1040 ExpectDomMessage("error: cancel");
1043 // http://crbug.com/318526
1044 #if defined(OS_MACOSX)
1045 #define MAYBE_ErrorWithFrameNavigation DISABLED_ErrorWithFrameNavigation
1047 #define MAYBE_ErrorWithFrameNavigation ErrorWithFrameNavigation
1049 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1050 MAYBE_ErrorWithFrameNavigation
) {
1051 AutofillDialogControllerImpl
* controller
=
1052 SetUpHtmlAndInvoke("<input autocomplete='cc-name'>");
1053 ASSERT_TRUE(controller
);
1056 ASSERT_TRUE(content::ExecuteScriptAndExtractString(GetRenderViewHost(),
1059 ExpectDomMessage("iframe loaded");
1061 AutofillDialogViewTester::For(
1062 static_cast<TestAutofillDialogController
*>(controller
)->view())->
1064 ExpectDomMessage("error: cancel");
1067 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, NoCvcSegfault
) {
1068 controller()->set_use_validation(true);
1070 CreditCard
credit_card(test::GetVerifiedCreditCard());
1071 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card
);
1072 EXPECT_FALSE(controller()->IsEditingExistingData(SECTION_CC
));
1074 ASSERT_NO_FATAL_FAILURE(GetViewTester()->SubmitForTesting());
1077 // Flaky on Win7, WinXP, and Win Aura. http://crbug.com/270314.
1079 #define MAYBE_PreservedSections DISABLED_PreservedSections
1081 #define MAYBE_PreservedSections PreservedSections
1083 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, MAYBE_PreservedSections
) {
1084 controller()->set_use_validation(true);
1086 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
1087 view
->SetTextContentsOfInput(CREDIT_CARD_NUMBER
,
1088 ASCIIToUTF16("4111111111111111"));
1090 // Create some invalid, manually inputted shipping data.
1091 view
->SetTextContentsOfInput(ADDRESS_HOME_ZIP
, ASCIIToUTF16("shipping zip"));
1093 // Switch to Wallet by simulating a successful server response.
1094 controller()->OnDidFetchWalletCookieValue(std::string());
1095 controller()->OnDidGetWalletItems(
1096 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
));
1097 ASSERT_TRUE(controller()->IsPayingWithWallet());
1099 // The valid data should be preserved.
1100 EXPECT_EQ(ASCIIToUTF16("4111111111111111"),
1101 view
->GetTextContentsOfInput(CREDIT_CARD_NUMBER
));
1103 // The invalid data should be dropped.
1104 EXPECT_TRUE(view
->GetTextContentsOfInput(ADDRESS_HOME_ZIP
).empty());
1106 // Switch back to Autofill.
1107 ui::MenuModel
* account_chooser
= controller()->MenuModelForAccountChooser();
1108 account_chooser
->ActivatedAt(account_chooser
->GetItemCount() - 1);
1109 ASSERT_FALSE(controller()->IsPayingWithWallet());
1111 // The valid data should still be preserved when switched back.
1112 EXPECT_EQ(ASCIIToUTF16("4111111111111111"),
1113 view
->GetTextContentsOfInput(CREDIT_CARD_NUMBER
));
1116 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1117 GeneratedCardLastFourAfterVerifyCvv
) {
1118 controller()->OnDidFetchWalletCookieValue(std::string());
1120 scoped_ptr
<wallet::WalletItems
> wallet_items
=
1121 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
1122 wallet_items
->AddInstrument(wallet::GetTestMaskedInstrument());
1123 wallet_items
->AddAddress(wallet::GetTestShippingAddress());
1125 base::string16 last_four
=
1126 wallet_items
->instruments()[0]->TypeAndLastFourDigits();
1127 controller()->OnDidGetWalletItems(wallet_items
.Pass());
1129 scoped_ptr
<AutofillDialogViewTester
> test_view
= GetViewTester();
1130 EXPECT_FALSE(test_view
->IsShowingOverlay());
1131 EXPECT_CALL(*controller(), LoadRiskFingerprintData());
1132 controller()->OnAccept();
1133 EXPECT_TRUE(test_view
->IsShowingOverlay());
1135 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_
));
1136 scoped_ptr
<risk::Fingerprint
> fingerprint(new risk::Fingerprint());
1137 fingerprint
->mutable_machine_characteristics()->mutable_screen_size()->
1139 controller()->OnDidLoadRiskFingerprintData(fingerprint
.Pass());
1141 controller()->OnDidGetFullWallet(
1142 wallet::GetTestFullWalletWithRequiredActions(
1143 std::vector
<wallet::RequiredAction
>(1, wallet::VERIFY_CVV
)));
1145 ASSERT_TRUE(controller()->IsSubmitPausedOn(wallet::VERIFY_CVV
));
1147 std::string
fake_cvc("123");
1148 test_view
->SetTextContentsOfSuggestionInput(SECTION_CC_BILLING
,
1149 ASCIIToUTF16(fake_cvc
));
1151 EXPECT_FALSE(test_view
->IsShowingOverlay());
1152 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1153 AuthenticateInstrument(_
, fake_cvc
));
1154 controller()->OnAccept();
1155 EXPECT_TRUE(test_view
->IsShowingOverlay());
1157 base::HistogramTester histogram
;
1158 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_
));
1159 controller()->OnDidAuthenticateInstrument(true);
1160 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
1161 controller()->ForceFinishSubmit();
1163 histogram
.ExpectUniqueSample(
1164 "RequestAutocomplete.DismissalState",
1165 AutofillMetrics::DIALOG_ACCEPTED_EXISTING_WALLET_DATA
, 1);
1169 EXPECT_EQ(1, test_generated_bubble_controller()->bubbles_shown());
1170 EXPECT_EQ(last_four
, test_generated_bubble_controller()->backing_card_name());
1173 // Simulates the user signing in to the dialog from the inline web contents.
1174 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, SimulateSuccessfulSignIn
) {
1176 // TODO(msw): Fix potential flakiness on Windows XP; http://crbug.com/333641
1177 if (base::win::GetVersion() <= base::win::VERSION_XP
)
1180 browser()->profile()->GetPrefs()->SetBoolean(
1181 ::prefs::kAutofillDialogPayWithoutWallet
,
1184 InitializeController();
1186 controller()->OnDidFetchWalletCookieValue(std::string());
1187 controller()->OnDidGetWalletItems(
1188 wallet::GetTestWalletItemsWithRequiredAction(wallet::GAIA_AUTH
));
1190 NavEntryCommittedObserver
sign_in_page_observer(
1191 controller()->FakeSignInUrl(),
1192 content::NotificationService::AllSources());
1194 // Simulate a user clicking "Sign In" (which loads dialog's web contents).
1195 controller()->SignInLinkClicked();
1196 EXPECT_TRUE(controller()->ShouldShowSignInWebView());
1198 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
1199 content::WebContents
* sign_in_contents
= view
->GetSignInWebContents();
1200 ASSERT_TRUE(sign_in_contents
);
1202 sign_in_page_observer
.Wait();
1204 NavEntryCommittedObserver
continue_page_observer(
1205 wallet::GetSignInContinueUrl(),
1206 content::NotificationService::AllSources());
1208 EXPECT_EQ(sign_in_contents
->GetURL(), controller()->FakeSignInUrl());
1210 AccountChooserModel
* account_chooser_model
=
1211 controller()->AccountChooserModelForTesting();
1212 EXPECT_FALSE(account_chooser_model
->WalletIsSelected());
1214 content::OpenURLParams
params(wallet::GetSignInContinueUrl(),
1215 content::Referrer(),
1217 ui::PAGE_TRANSITION_LINK
,
1220 sign_in_contents
->GetDelegate()->OpenURLFromTab(sign_in_contents
, params
);
1222 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems(_
, _
));
1223 continue_page_observer
.Wait();
1224 content::RunAllPendingInMessageLoop();
1226 EXPECT_FALSE(controller()->ShouldShowSignInWebView());
1228 scoped_ptr
<wallet::WalletItems
> wallet_items
=
1229 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
1230 wallet_items
->AddInstrument(wallet::GetTestMaskedInstrument());
1231 controller()->OnDidGetWalletItems(wallet_items
.Pass());
1233 // Wallet should now be selected and Chrome shouldn't have crashed (which can
1234 // happen if the WebContents is deleted while proccessing a nav entry commit).
1235 EXPECT_TRUE(account_chooser_model
->WalletIsSelected());
1236 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_CC_BILLING
));
1237 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_SHIPPING
));
1239 base::HistogramTester histogram
;
1240 view
->SubmitForTesting();
1241 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
1242 controller()->ForceFinishSubmit();
1243 histogram
.ExpectUniqueSample(
1244 "RequestAutocomplete.DismissalState",
1245 AutofillMetrics::DIALOG_ACCEPTED_EXISTING_WALLET_DATA
, 1);
1248 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, AddAccount
) {
1250 // TODO(msw): Fix potential flakiness on Windows XP; http://crbug.com/333641
1251 if (base::win::GetVersion() <= base::win::VERSION_XP
)
1255 controller()->OnDidFetchWalletCookieValue(std::string());
1256 std::vector
<std::string
> usernames
;
1257 usernames
.push_back("user_0@example.com");
1258 controller()->OnDidGetWalletItems(
1259 wallet::GetTestWalletItemsWithUsers(usernames
, 0));
1261 // Switch to Autofill.
1262 AccountChooserModel
* account_chooser_model
=
1263 controller()->AccountChooserModelForTesting();
1264 account_chooser_model
->ActivatedAt(
1265 account_chooser_model
->GetItemCount() - 1);
1267 NavEntryCommittedObserver
sign_in_page_observer(
1268 controller()->FakeSignInUrl(),
1269 content::NotificationService::AllSources());
1271 // Simulate a user clicking "add account".
1272 account_chooser_model
->ActivatedAt(
1273 account_chooser_model
->GetItemCount() - 2);
1274 EXPECT_TRUE(controller()->ShouldShowSignInWebView());
1276 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
1277 content::WebContents
* sign_in_contents
= view
->GetSignInWebContents();
1278 ASSERT_TRUE(sign_in_contents
);
1280 sign_in_page_observer
.Wait();
1282 NavEntryCommittedObserver
continue_page_observer(
1283 wallet::GetSignInContinueUrl(),
1284 content::NotificationService::AllSources());
1286 EXPECT_EQ(sign_in_contents
->GetURL(), controller()->FakeSignInUrl());
1288 EXPECT_FALSE(account_chooser_model
->WalletIsSelected());
1290 // User signs into new account, account 3.
1291 controller()->set_sign_in_user_index(3U);
1292 content::OpenURLParams
params(wallet::GetSignInContinueUrl(),
1293 content::Referrer(),
1295 ui::PAGE_TRANSITION_LINK
,
1297 sign_in_contents
->GetDelegate()->OpenURLFromTab(sign_in_contents
, params
);
1299 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems(_
, _
));
1300 continue_page_observer
.Wait();
1301 content::RunAllPendingInMessageLoop();
1303 EXPECT_FALSE(controller()->ShouldShowSignInWebView());
1304 EXPECT_EQ(3U, controller()->GetTestingWalletClient()->user_index());
1306 usernames
.push_back("user_1@example.com");
1307 usernames
.push_back("user_2@example.com");
1308 usernames
.push_back("user_3@example.com");
1309 usernames
.push_back("user_4@example.com");
1310 // Curveball: wallet items comes back with user 4 selected.
1311 controller()->OnDidGetWalletItems(
1312 wallet::GetTestWalletItemsWithUsers(usernames
, 4U));
1314 EXPECT_TRUE(account_chooser_model
->WalletIsSelected());
1315 EXPECT_EQ(4U, account_chooser_model
->GetActiveWalletAccountIndex());
1316 EXPECT_EQ(4U, controller()->GetTestingWalletClient()->user_index());
1319 // Verify that filling a form works correctly, including filling the CVC when
1320 // that is requested separately.
1321 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1322 FillFormIncludesCVC
) {
1323 AutofillDialogControllerImpl
* controller
=
1324 SetUpHtmlAndInvoke("<input autocomplete='cc-csc'>");
1325 ASSERT_TRUE(controller
);
1327 AddCreditcardToProfile(controller
->profile(), test::GetVerifiedCreditCard());
1328 AddAutofillProfileToProfile(controller
->profile(),
1329 test::GetVerifiedProfile());
1331 scoped_ptr
<AutofillDialogViewTester
> view
=
1332 AutofillDialogViewTester::For(
1333 static_cast<TestAutofillDialogController
*>(controller
)->view());
1334 view
->SetTextContentsOfSuggestionInput(SECTION_CC
, ASCIIToUTF16("123"));
1335 view
->SubmitForTesting();
1336 ExpectDomMessage("success");
1337 EXPECT_EQ("123", GetValueForHTMLFieldOfType("cc-csc"));
1340 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, AddNewClearsComboboxes
) {
1341 // Ensure the input under test is a combobox.
1343 controller()->ComboboxModelForAutofillType(CREDIT_CARD_EXP_MONTH
));
1345 // Set up an expired card.
1347 test::SetCreditCardInfo(&card
, "Roy Demeo", "4111111111111111", "8", "2013");
1348 card
.set_origin("Chrome settings");
1349 ASSERT_TRUE(card
.IsVerified());
1351 // Add the card and check that there's a menu for that section.
1352 controller()->GetTestingManager()->AddTestingCreditCard(&card
);
1353 ASSERT_TRUE(controller()->MenuModelForSection(SECTION_CC
));
1355 // Select the invalid, suggested card from the menu.
1356 controller()->MenuModelForSection(SECTION_CC
)->ActivatedAt(0);
1357 EXPECT_TRUE(controller()->IsEditingExistingData(SECTION_CC
));
1359 // Get the contents of the combobox of the credit card's expiration month.
1360 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
1361 base::string16 cc_exp_month_text
=
1362 view
->GetTextContentsOfInput(CREDIT_CARD_EXP_MONTH
);
1364 // Select "New X..." from the suggestion menu to clear the section's inputs.
1365 controller()->MenuModelForSection(SECTION_CC
)->ActivatedAt(1);
1366 EXPECT_FALSE(controller()->IsEditingExistingData(SECTION_CC
));
1368 // Ensure that the credit card expiration month has changed.
1369 EXPECT_NE(cc_exp_month_text
,
1370 view
->GetTextContentsOfInput(CREDIT_CARD_EXP_MONTH
));
1373 // Flaky on Win7 (http://crbug.com/446432)
1375 #define MAYBE_TabOpensToJustRight DISABLED_TabOpensToJustRight
1377 #define MAYBE_TabOpensToJustRight TabOpensToJustRight
1379 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1380 MAYBE_TabOpensToJustRight
) {
1381 ASSERT_TRUE(browser()->is_type_tabbed());
1383 // Tabs should currently be: / rAc() \.
1384 content::WebContents
* dialog_invoker
= controller()->GetWebContents();
1385 EXPECT_EQ(dialog_invoker
, GetActiveWebContents());
1387 TabStripModel
* tab_strip
= browser()->tab_strip_model();
1388 ASSERT_EQ(1, tab_strip
->count());
1389 EXPECT_EQ(0, tab_strip
->GetIndexOfWebContents(dialog_invoker
));
1391 // Open a tab to about:blank in the background at the end of the tab strip.
1392 chrome::AddTabAt(browser(), GURL(), -1, false);
1393 // Tabs should now be: / rAc() \/ blank \.
1394 EXPECT_EQ(2, tab_strip
->count());
1395 EXPECT_EQ(0, tab_strip
->active_index());
1396 EXPECT_EQ(dialog_invoker
, GetActiveWebContents());
1398 content::WebContents
* blank_tab
= tab_strip
->GetWebContentsAt(1);
1400 // Simulate clicking "Manage X...".
1401 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(2);
1402 // Tab should now be: / rAc() \/ manage 1 \/ blank \.
1403 EXPECT_EQ(3, tab_strip
->count());
1404 int dialog_index
= tab_strip
->GetIndexOfWebContents(dialog_invoker
);
1405 EXPECT_EQ(0, dialog_index
);
1406 EXPECT_EQ(1, tab_strip
->active_index());
1407 EXPECT_EQ(2, tab_strip
->GetIndexOfWebContents(blank_tab
));
1409 content::WebContents
* first_manage_tab
= tab_strip
->GetWebContentsAt(1);
1411 // Re-activate the dialog's tab (like a user would have to).
1412 tab_strip
->ActivateTabAt(dialog_index
, true);
1413 EXPECT_EQ(dialog_invoker
, GetActiveWebContents());
1415 // Simulate clicking "Manage X...".
1416 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(2);
1417 // Tabs should now be: / rAc() \/ manage 2 \/ manage 1 \/ blank \.
1418 EXPECT_EQ(4, tab_strip
->count());
1419 EXPECT_EQ(0, tab_strip
->GetIndexOfWebContents(dialog_invoker
));
1420 EXPECT_EQ(1, tab_strip
->active_index());
1421 EXPECT_EQ(2, tab_strip
->GetIndexOfWebContents(first_manage_tab
));
1422 EXPECT_EQ(3, tab_strip
->GetIndexOfWebContents(blank_tab
));
1425 // Flaky on Win7 (http://crbug.com/446432)
1427 #define MAYBE_SignInWebViewOpensLinksInNewTab DISABLED_SignInWebViewOpensLinksInNewTab
1429 #define MAYBE_SignInWebViewOpensLinksInNewTab SignInWebViewOpensLinksInNewTab
1431 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1432 MAYBE_SignInWebViewOpensLinksInNewTab
) {
1433 controller()->OnDidFetchWalletCookieValue(std::string());
1434 controller()->OnDidGetWalletItems(
1435 wallet::GetTestWalletItemsWithRequiredAction(wallet::GAIA_AUTH
));
1437 NavEntryCommittedObserver
sign_in_page_observer(
1438 controller()->FakeSignInUrl(),
1439 content::NotificationService::AllSources());
1441 controller()->SignInLinkClicked();
1443 content::WebContents
* sign_in_contents
=
1444 GetViewTester()->GetSignInWebContents();
1445 ASSERT_TRUE(sign_in_contents
);
1447 sign_in_page_observer
.Wait();
1449 content::OpenURLParams
params(GURL("http://google.com"),
1450 content::Referrer(),
1452 ui::PAGE_TRANSITION_LINK
,
1454 params
.user_gesture
= true;
1456 int num_tabs
= browser()->tab_strip_model()->count();
1457 sign_in_contents
->GetDelegate()->OpenURLFromTab(sign_in_contents
, params
);
1458 EXPECT_EQ(num_tabs
+ 1, browser()->tab_strip_model()->count());
1461 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, RefreshOnManageTabClose
) {
1462 ASSERT_TRUE(browser()->is_type_tabbed());
1464 // GetWalletItems(_, _) is called when controller() is created in SetUp().
1465 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems(_
, _
));
1466 controller()->OnDidFetchWalletCookieValue(std::string());
1467 controller()->OnDidGetWalletItems(
1468 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
));
1470 content::WebContents
* dialog_invoker
= controller()->GetWebContents();
1471 ChromeAutofillClient::FromWebContents(dialog_invoker
)
1472 ->SetDialogControllerForTesting(controller()->AsWeakPtr());
1474 // Open a new tab by selecting "Manage my shipping details..." in Wallet mode.
1475 EXPECT_EQ(1, browser()->tab_strip_model()->count());
1476 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(2);
1477 EXPECT_EQ(2, browser()->tab_strip_model()->count());
1478 ASSERT_NE(dialog_invoker
, GetActiveWebContents());
1480 // Closing the tab opened by "Manage my shipping details..." should refresh
1482 controller()->ClearLastWalletItemsFetchTimestampForTesting();
1483 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems(_
, _
));
1484 GetActiveWebContents()->Close();
1487 // Changes from Wallet to Autofill and verifies that the combined billing/cc
1488 // sections are showing (or not) at the correct times.
1489 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1490 ChangingDataSourceShowsCorrectSections
) {
1491 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
1492 EXPECT_TRUE(view
->IsShowingSection(SECTION_CC
));
1493 EXPECT_TRUE(view
->IsShowingSection(SECTION_BILLING
));
1494 EXPECT_FALSE(view
->IsShowingSection(SECTION_CC_BILLING
));
1496 // Switch the dialog to paying with Wallet.
1497 controller()->OnDidFetchWalletCookieValue(std::string());
1498 controller()->OnDidGetWalletItems(
1499 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
));
1500 ASSERT_TRUE(controller()->IsPayingWithWallet());
1502 EXPECT_FALSE(view
->IsShowingSection(SECTION_CC
));
1503 EXPECT_FALSE(view
->IsShowingSection(SECTION_BILLING
));
1504 EXPECT_TRUE(view
->IsShowingSection(SECTION_CC_BILLING
));
1506 // Now switch back to Autofill to ensure this direction works as well.
1507 ui::MenuModel
* account_chooser
= controller()->MenuModelForAccountChooser();
1508 account_chooser
->ActivatedAt(account_chooser
->GetItemCount() - 1);
1510 EXPECT_TRUE(view
->IsShowingSection(SECTION_CC
));
1511 EXPECT_TRUE(view
->IsShowingSection(SECTION_BILLING
));
1512 EXPECT_FALSE(view
->IsShowingSection(SECTION_CC_BILLING
));
1515 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1516 DoesWorkOnHttpWithFlag
) {
1517 net::SpawnedTestServer
http_server(
1518 net::SpawnedTestServer::TYPE_HTTP
,
1519 net::SpawnedTestServer::kLocalhost
,
1520 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
1521 ASSERT_TRUE(http_server
.Start());
1522 EXPECT_TRUE(RunTestPage(http_server
));
1525 // Like the parent test, but doesn't add the --reduce-security-for-testing flag.
1526 class AutofillDialogControllerSecurityTest
:
1527 public AutofillDialogControllerTest
{
1529 AutofillDialogControllerSecurityTest() {}
1530 ~AutofillDialogControllerSecurityTest() override
{}
1532 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
1533 CHECK(!command_line
->HasSwitch(::switches::kReduceSecurityForTesting
));
1536 typedef net::BaseTestServer::SSLOptions SSLOptions
;
1539 DISALLOW_COPY_AND_ASSIGN(AutofillDialogControllerSecurityTest
);
1542 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerSecurityTest
,
1544 net::SpawnedTestServer
http_server(
1545 net::SpawnedTestServer::TYPE_HTTP
,
1546 net::SpawnedTestServer::kLocalhost
,
1547 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
1548 ASSERT_TRUE(http_server
.Start());
1549 EXPECT_FALSE(RunTestPage(http_server
));
1552 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerSecurityTest
,
1553 DoesWorkOnHttpWithFlags
) {
1554 net::SpawnedTestServer
https_server(
1555 net::SpawnedTestServer::TYPE_HTTPS
,
1556 SSLOptions(SSLOptions::CERT_OK
),
1557 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
1558 ASSERT_TRUE(https_server
.Start());
1559 EXPECT_TRUE(RunTestPage(https_server
));
1562 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerSecurityTest
,
1563 DoesntWorkOnBrokenHttps
) {
1564 net::SpawnedTestServer
https_server(
1565 net::SpawnedTestServer::TYPE_HTTPS
,
1566 SSLOptions(SSLOptions::CERT_EXPIRED
),
1567 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
1568 ASSERT_TRUE(https_server
.Start());
1569 EXPECT_FALSE(RunTestPage(https_server
));
1572 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1573 CountryChangeRebuildsSection
) {
1574 EXPECT_FALSE(SectionHasField(SECTION_BILLING
, ADDRESS_BILLING_SORTING_CODE
));
1575 EXPECT_FALSE(SectionHasField(SECTION_SHIPPING
, ADDRESS_HOME_SORTING_CODE
));
1577 // Select "Add new shipping address...".
1578 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(1);
1580 // Add some valid user input that should be preserved when country changes.
1581 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
1582 view
->SetTextContentsOfInput(NAME_FULL
, ASCIIToUTF16("B. Loblaw"));
1584 // Change both sections' countries.
1585 view
->SetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
, ASCIIToUTF16("France"));
1586 view
->ActivateInput(ADDRESS_BILLING_COUNTRY
);
1587 view
->SetTextContentsOfInput(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("Belarus"));
1588 view
->ActivateInput(ADDRESS_HOME_COUNTRY
);
1590 // Verify the name is still there.
1591 EXPECT_EQ(ASCIIToUTF16("B. Loblaw"), view
->GetTextContentsOfInput(NAME_FULL
));
1593 EXPECT_TRUE(SectionHasField(SECTION_BILLING
, ADDRESS_BILLING_SORTING_CODE
));
1594 EXPECT_TRUE(SectionHasField(SECTION_SHIPPING
, ADDRESS_HOME_SORTING_CODE
));
1597 // Changing the data source to or from Wallet preserves the shipping country,
1598 // but not the billing country because Wallet only supports US billing
1600 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1601 ChangingDataSourcePreservesCountry
) {
1602 InitializeControllerWithoutShowing();
1603 controller()->GetTestingManager()->set_default_country_code("CA");
1604 controller()->Show();
1607 AutofillProfile
verified_profile(test::GetVerifiedProfile());
1608 controller()->GetTestingManager()->AddTestingProfile(&verified_profile
);
1610 CreditCard
verified_card(test::GetVerifiedCreditCard());
1611 controller()->GetTestingManager()->AddTestingCreditCard(&verified_card
);
1612 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_SHIPPING
));
1614 controller()->OnDidFetchWalletCookieValue(std::string());
1615 scoped_ptr
<wallet::WalletItems
> items
=
1616 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
1617 items
->AddAccount(wallet::GetTestGaiaAccount());
1618 items
->AddInstrument(wallet::GetTestMaskedInstrument());
1619 items
->AddAddress(wallet::GetTestShippingAddress());
1620 controller()->OnDidGetWalletItems(items
.Pass());
1622 EXPECT_TRUE(controller()->IsPayingWithWallet());
1624 // Select "Add new shipping address...".
1625 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(2);
1626 EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_SHIPPING
));
1628 // Default shipping country matches PDM's default, but default billing is
1629 // always US in Wallet mode.
1630 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
1631 ASSERT_EQ(ASCIIToUTF16("Canada"),
1632 view
->GetTextContentsOfInput(ADDRESS_HOME_COUNTRY
));
1633 ASSERT_EQ(ASCIIToUTF16("United States"),
1634 view
->GetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
));
1636 // Switch the shipping country.
1637 view
->SetTextContentsOfInput(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("Belarus"));
1638 EXPECT_EQ(ASCIIToUTF16("Belarus"),
1639 view
->GetTextContentsOfInput(ADDRESS_HOME_COUNTRY
));
1640 view
->ActivateInput(ADDRESS_HOME_COUNTRY
);
1642 // Switch to using Autofill instead of Wallet.
1643 ui::MenuModel
* account_chooser
= controller()->MenuModelForAccountChooser();
1644 account_chooser
->ActivatedAt(account_chooser
->GetItemCount() - 1);
1646 EXPECT_FALSE(controller()->IsPayingWithWallet());
1648 // Shipping country should have stayed the same.
1649 EXPECT_EQ(ASCIIToUTF16("Belarus"),
1650 view
->GetTextContentsOfInput(ADDRESS_HOME_COUNTRY
));
1651 ASSERT_TRUE(SectionHasField(SECTION_SHIPPING
, ADDRESS_HOME_SORTING_CODE
));
1653 controller()->MenuModelForSection(SECTION_BILLING
)->ActivatedAt(1);
1654 view
->SetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
,
1655 ASCIIToUTF16("Belarus"));
1656 view
->ActivateInput(ADDRESS_BILLING_COUNTRY
);
1657 EXPECT_EQ(ASCIIToUTF16("Belarus"),
1658 view
->GetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
));
1659 ASSERT_TRUE(SectionHasField(SECTION_BILLING
, ADDRESS_BILLING_SORTING_CODE
));
1661 // Switch back to Wallet. Country should go back to US.
1662 account_chooser
->ActivatedAt(0);
1663 EXPECT_EQ(ASCIIToUTF16("United States"),
1664 view
->GetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
));
1666 SectionHasField(SECTION_CC_BILLING
, ADDRESS_BILLING_SORTING_CODE
));
1668 // Make sure shipping is still on Belarus.
1669 EXPECT_EQ(ASCIIToUTF16("Belarus"),
1670 view
->GetTextContentsOfInput(ADDRESS_HOME_COUNTRY
));
1671 ASSERT_TRUE(SectionHasField(SECTION_SHIPPING
, ADDRESS_HOME_SORTING_CODE
));
1674 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, AddNewResetsCountry
) {
1675 AutofillProfile
verified_profile(test::GetVerifiedProfile());
1676 controller()->GetTestingManager()->AddTestingProfile(&verified_profile
);
1678 // Select "Add new billing/shipping address...".
1679 controller()->MenuModelForSection(SECTION_BILLING
)->ActivatedAt(1);
1680 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(2);
1682 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
1683 ASSERT_EQ(ASCIIToUTF16("United States"),
1684 view
->GetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
));
1685 ASSERT_EQ(ASCIIToUTF16("United States"),
1686 view
->GetTextContentsOfInput(ADDRESS_HOME_COUNTRY
));
1688 // Switch both billing and shipping countries.
1689 view
->SetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
, ASCIIToUTF16("Brazil"));
1690 view
->ActivateInput(ADDRESS_BILLING_COUNTRY
);
1691 view
->SetTextContentsOfInput(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("France"));
1692 view
->ActivateInput(ADDRESS_HOME_COUNTRY
);
1694 // Select "Add new billing/shipping address...".
1695 controller()->MenuModelForSection(SECTION_BILLING
)->ActivatedAt(1);
1696 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(2);
1698 EXPECT_EQ(ASCIIToUTF16("United States"),
1699 view
->GetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
));
1700 EXPECT_EQ(ASCIIToUTF16("United States"),
1701 view
->GetTextContentsOfInput(ADDRESS_HOME_COUNTRY
));
1704 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1705 FillingFormRebuildsInputs
) {
1706 AutofillProfile
full_profile(test::GetFullProfile());
1707 full_profile
.SetRawInfo(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("DE"));
1708 controller()->GetTestingManager()->AddTestingProfile(&full_profile
);
1710 // Select "Add new shipping address...".
1711 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(1);
1713 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
1714 ASSERT_EQ(ASCIIToUTF16("United States"),
1715 view
->GetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
));
1716 ASSERT_EQ(ASCIIToUTF16("United States"),
1717 view
->GetTextContentsOfInput(ADDRESS_HOME_COUNTRY
));
1719 const ServerFieldType input_type
= EMAIL_ADDRESS
;
1720 base::string16 name
= full_profile
.GetRawInfo(input_type
);
1721 view
->SetTextContentsOfInput(input_type
, name
.substr(0, name
.size() / 2));
1722 view
->ActivateInput(input_type
);
1723 ASSERT_EQ(input_type
, controller()->popup_input_type());
1724 controller()->DidAcceptSuggestion(base::string16(), 0);
1726 EXPECT_EQ(ASCIIToUTF16("Germany"),
1727 view
->GetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
));
1728 EXPECT_EQ(ASCIIToUTF16("United States"),
1729 view
->GetTextContentsOfInput(ADDRESS_HOME_COUNTRY
));
1732 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1733 FillingFormPreservesChangedCountry
) {
1734 AutofillProfile
full_profile(test::GetFullProfile());
1735 full_profile
.SetRawInfo(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("DE"));
1736 controller()->GetTestingManager()->AddTestingProfile(&full_profile
);
1738 // Select "Add new shipping address...".
1739 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(1);
1741 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
1742 view
->SetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
, ASCIIToUTF16("France"));
1743 view
->ActivateInput(ADDRESS_BILLING_COUNTRY
);
1744 view
->SetTextContentsOfInput(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("Belarus"));
1745 view
->ActivateInput(ADDRESS_HOME_COUNTRY
);
1747 base::string16 name
= full_profile
.GetRawInfo(NAME_FULL
);
1748 view
->SetTextContentsOfInput(NAME_FULL
, name
.substr(0, name
.size() / 2));
1749 view
->ActivateInput(NAME_FULL
);
1750 ASSERT_EQ(NAME_FULL
, controller()->popup_input_type());
1751 controller()->DidAcceptSuggestion(base::string16(), 0);
1753 EXPECT_EQ(ASCIIToUTF16("France"),
1754 view
->GetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
));
1755 EXPECT_EQ(ASCIIToUTF16("Belarus"),
1756 view
->GetTextContentsOfInput(ADDRESS_HOME_COUNTRY
));
1759 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, RulesLoaded
) {
1760 // Select "Add new shipping address...".
1761 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(1);
1762 controller()->set_use_validation(true);
1764 EXPECT_CALL(*controller()->GetMockValidator(),
1765 ValidateAddress(CountryCodeMatcher("DE"), _
, _
)).Times(2).
1766 WillOnce(Return(AddressValidator::RULES_NOT_READY
));
1768 // Validation should occur on country change and see the rules haven't loaded.
1769 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
1770 view
->SetTextContentsOfInput(ADDRESS_HOME_ZIP
, ASCIIToUTF16("123"));
1771 view
->SetTextContentsOfInput(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("Germany"));
1772 view
->ActivateInput(ADDRESS_HOME_COUNTRY
);
1774 // Different country loaded, validation should not occur.
1775 controller()->OnAddressValidationRulesLoaded("FR", true);
1777 // Relevant country loaded, validation should occur.
1778 controller()->OnAddressValidationRulesLoaded("DE", true);
1780 // Relevant country loaded but revalidation already happened, no further
1781 // validation should occur.
1782 controller()->OnAddressValidationRulesLoaded("DE", false);
1784 // Cancelling the dialog causes additional validation to see if the user
1785 // cancelled with invalid fields, so verify and clear here.
1786 testing::Mock::VerifyAndClearExpectations(controller()->GetMockValidator());
1789 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1790 TransactionAmount
) {
1792 "<input type='number' step='0.01'"
1793 " autocomplete='transaction-amount' value='24'>"
1794 "<input autocomplete='transaction-currency' value='USD'>"
1795 "<input autocomplete='cc-csc'>");
1796 AutofillDialogControllerImpl
* controller
= SetUpHtmlAndInvoke(html
);
1797 ASSERT_TRUE(controller
);
1799 EXPECT_EQ(ASCIIToUTF16("24"), controller
->transaction_amount_
);
1800 EXPECT_EQ(ASCIIToUTF16("USD"), controller
->transaction_currency_
);
1803 // Same as above, plus readonly attribute.
1804 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1805 TransactionAmountReadonly
) {
1807 "<input type='number' step='0.01'"
1808 " autocomplete='transaction-amount' value='24' readonly>"
1809 "<input autocomplete='transaction-currency' value='USD' readonly>"
1810 "<input autocomplete='cc-csc'>");
1811 AutofillDialogControllerImpl
* controller
= SetUpHtmlAndInvoke(html
);
1812 ASSERT_TRUE(controller
);
1814 EXPECT_EQ(ASCIIToUTF16("24"), controller
->transaction_amount_
);
1815 EXPECT_EQ(ASCIIToUTF16("USD"), controller
->transaction_currency_
);
1818 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, HideOnNavigate
) {
1819 base::WeakPtr
<TestAutofillDialogController
> weak_ptr
=
1820 controller()->AsWeakPtr();
1821 EXPECT_TRUE(weak_ptr
.get());
1823 ui_test_utils::NavigateToURL(browser(), GURL("about:blank"));
1824 EXPECT_FALSE(weak_ptr
.get());
1827 // Tests that the rAc dialog hides when the main frame is navigated, even if
1828 // it was invoked from a child frame.
1829 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, HideOnNavigateMainFrame
) {
1830 net::SpawnedTestServer
http_server(
1831 net::SpawnedTestServer::TYPE_HTTP
,
1832 net::SpawnedTestServer::kLocalhost
,
1833 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
1834 ASSERT_TRUE(http_server
.Start());
1835 RunTestPageInIframe(http_server
);
1837 ui_test_utils::NavigateToURL(browser(), GURL("about:blank"));
1838 ChromeAutofillClient
* client
=
1839 ChromeAutofillClient::FromWebContents(GetActiveWebContents());
1840 EXPECT_FALSE(client
->GetDialogControllerForTesting());
1843 // Tests that the rAc dialog hides when the iframe it's in is navigated.
1844 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, HideOnNavigateIframe
) {
1845 net::SpawnedTestServer
http_server(
1846 net::SpawnedTestServer::TYPE_HTTP
,
1847 net::SpawnedTestServer::kLocalhost
,
1848 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
1849 ASSERT_TRUE(http_server
.Start());
1850 RunTestPageInIframe(http_server
);
1853 ASSERT_TRUE(content::ExecuteScriptAndExtractString(GetRenderViewHost(),
1856 ExpectDomMessage("iframe loaded");
1857 ChromeAutofillClient
* client
=
1858 ChromeAutofillClient::FromWebContents(GetActiveWebContents());
1859 EXPECT_FALSE(client
->GetDialogControllerForTesting());
1862 } // namespace autofill