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 controller()->OnDidLoadRiskFingerprintData("a");
1138 controller()->OnDidGetFullWallet(
1139 wallet::GetTestFullWalletWithRequiredActions(
1140 std::vector
<wallet::RequiredAction
>(1, wallet::VERIFY_CVV
)));
1142 ASSERT_TRUE(controller()->IsSubmitPausedOn(wallet::VERIFY_CVV
));
1144 std::string
fake_cvc("123");
1145 test_view
->SetTextContentsOfSuggestionInput(SECTION_CC_BILLING
,
1146 ASCIIToUTF16(fake_cvc
));
1148 EXPECT_FALSE(test_view
->IsShowingOverlay());
1149 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1150 AuthenticateInstrument(_
, fake_cvc
));
1151 controller()->OnAccept();
1152 EXPECT_TRUE(test_view
->IsShowingOverlay());
1154 base::HistogramTester histogram
;
1155 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_
));
1156 controller()->OnDidAuthenticateInstrument(true);
1157 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
1158 controller()->ForceFinishSubmit();
1160 histogram
.ExpectUniqueSample(
1161 "RequestAutocomplete.DismissalState",
1162 AutofillMetrics::DIALOG_ACCEPTED_EXISTING_WALLET_DATA
, 1);
1166 EXPECT_EQ(1, test_generated_bubble_controller()->bubbles_shown());
1167 EXPECT_EQ(last_four
, test_generated_bubble_controller()->backing_card_name());
1170 // Simulates the user signing in to the dialog from the inline web contents.
1171 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, SimulateSuccessfulSignIn
) {
1173 // TODO(msw): Fix potential flakiness on Windows XP; http://crbug.com/333641
1174 if (base::win::GetVersion() <= base::win::VERSION_XP
)
1177 browser()->profile()->GetPrefs()->SetBoolean(
1178 ::prefs::kAutofillDialogPayWithoutWallet
,
1181 InitializeController();
1183 controller()->OnDidFetchWalletCookieValue(std::string());
1184 controller()->OnDidGetWalletItems(
1185 wallet::GetTestWalletItemsWithRequiredAction(wallet::GAIA_AUTH
));
1187 NavEntryCommittedObserver
sign_in_page_observer(
1188 controller()->FakeSignInUrl(),
1189 content::NotificationService::AllSources());
1191 // Simulate a user clicking "Sign In" (which loads dialog's web contents).
1192 controller()->SignInLinkClicked();
1193 EXPECT_TRUE(controller()->ShouldShowSignInWebView());
1195 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
1196 content::WebContents
* sign_in_contents
= view
->GetSignInWebContents();
1197 ASSERT_TRUE(sign_in_contents
);
1199 sign_in_page_observer
.Wait();
1201 NavEntryCommittedObserver
continue_page_observer(
1202 wallet::GetSignInContinueUrl(),
1203 content::NotificationService::AllSources());
1205 EXPECT_EQ(sign_in_contents
->GetURL(), controller()->FakeSignInUrl());
1207 AccountChooserModel
* account_chooser_model
=
1208 controller()->AccountChooserModelForTesting();
1209 EXPECT_FALSE(account_chooser_model
->WalletIsSelected());
1211 content::OpenURLParams
params(wallet::GetSignInContinueUrl(),
1212 content::Referrer(),
1214 ui::PAGE_TRANSITION_LINK
,
1217 sign_in_contents
->GetDelegate()->OpenURLFromTab(sign_in_contents
, params
);
1219 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems(_
, _
));
1220 continue_page_observer
.Wait();
1221 content::RunAllPendingInMessageLoop();
1223 EXPECT_FALSE(controller()->ShouldShowSignInWebView());
1225 scoped_ptr
<wallet::WalletItems
> wallet_items
=
1226 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
1227 wallet_items
->AddInstrument(wallet::GetTestMaskedInstrument());
1228 controller()->OnDidGetWalletItems(wallet_items
.Pass());
1230 // Wallet should now be selected and Chrome shouldn't have crashed (which can
1231 // happen if the WebContents is deleted while proccessing a nav entry commit).
1232 EXPECT_TRUE(account_chooser_model
->WalletIsSelected());
1233 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_CC_BILLING
));
1234 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_SHIPPING
));
1236 base::HistogramTester histogram
;
1237 view
->SubmitForTesting();
1238 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
1239 controller()->ForceFinishSubmit();
1240 histogram
.ExpectUniqueSample(
1241 "RequestAutocomplete.DismissalState",
1242 AutofillMetrics::DIALOG_ACCEPTED_EXISTING_WALLET_DATA
, 1);
1245 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, AddAccount
) {
1247 // TODO(msw): Fix potential flakiness on Windows XP; http://crbug.com/333641
1248 if (base::win::GetVersion() <= base::win::VERSION_XP
)
1252 controller()->OnDidFetchWalletCookieValue(std::string());
1253 std::vector
<std::string
> usernames
;
1254 usernames
.push_back("user_0@example.com");
1255 controller()->OnDidGetWalletItems(
1256 wallet::GetTestWalletItemsWithUsers(usernames
, 0));
1258 // Switch to Autofill.
1259 AccountChooserModel
* account_chooser_model
=
1260 controller()->AccountChooserModelForTesting();
1261 account_chooser_model
->ActivatedAt(
1262 account_chooser_model
->GetItemCount() - 1);
1264 NavEntryCommittedObserver
sign_in_page_observer(
1265 controller()->FakeSignInUrl(),
1266 content::NotificationService::AllSources());
1268 // Simulate a user clicking "add account".
1269 account_chooser_model
->ActivatedAt(
1270 account_chooser_model
->GetItemCount() - 2);
1271 EXPECT_TRUE(controller()->ShouldShowSignInWebView());
1273 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
1274 content::WebContents
* sign_in_contents
= view
->GetSignInWebContents();
1275 ASSERT_TRUE(sign_in_contents
);
1277 sign_in_page_observer
.Wait();
1279 NavEntryCommittedObserver
continue_page_observer(
1280 wallet::GetSignInContinueUrl(),
1281 content::NotificationService::AllSources());
1283 EXPECT_EQ(sign_in_contents
->GetURL(), controller()->FakeSignInUrl());
1285 EXPECT_FALSE(account_chooser_model
->WalletIsSelected());
1287 // User signs into new account, account 3.
1288 controller()->set_sign_in_user_index(3U);
1289 content::OpenURLParams
params(wallet::GetSignInContinueUrl(),
1290 content::Referrer(),
1292 ui::PAGE_TRANSITION_LINK
,
1294 sign_in_contents
->GetDelegate()->OpenURLFromTab(sign_in_contents
, params
);
1296 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems(_
, _
));
1297 continue_page_observer
.Wait();
1298 content::RunAllPendingInMessageLoop();
1300 EXPECT_FALSE(controller()->ShouldShowSignInWebView());
1301 EXPECT_EQ(3U, controller()->GetTestingWalletClient()->user_index());
1303 usernames
.push_back("user_1@example.com");
1304 usernames
.push_back("user_2@example.com");
1305 usernames
.push_back("user_3@example.com");
1306 usernames
.push_back("user_4@example.com");
1307 // Curveball: wallet items comes back with user 4 selected.
1308 controller()->OnDidGetWalletItems(
1309 wallet::GetTestWalletItemsWithUsers(usernames
, 4U));
1311 EXPECT_TRUE(account_chooser_model
->WalletIsSelected());
1312 EXPECT_EQ(4U, account_chooser_model
->GetActiveWalletAccountIndex());
1313 EXPECT_EQ(4U, controller()->GetTestingWalletClient()->user_index());
1316 // Verify that filling a form works correctly, including filling the CVC when
1317 // that is requested separately.
1318 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1319 FillFormIncludesCVC
) {
1320 AutofillDialogControllerImpl
* controller
=
1321 SetUpHtmlAndInvoke("<input autocomplete='cc-csc'>");
1322 ASSERT_TRUE(controller
);
1324 AddCreditcardToProfile(controller
->profile(), test::GetVerifiedCreditCard());
1325 AddAutofillProfileToProfile(controller
->profile(),
1326 test::GetVerifiedProfile());
1328 scoped_ptr
<AutofillDialogViewTester
> view
=
1329 AutofillDialogViewTester::For(
1330 static_cast<TestAutofillDialogController
*>(controller
)->view());
1331 view
->SetTextContentsOfSuggestionInput(SECTION_CC
, ASCIIToUTF16("123"));
1332 view
->SubmitForTesting();
1333 ExpectDomMessage("success");
1334 EXPECT_EQ("123", GetValueForHTMLFieldOfType("cc-csc"));
1337 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, AddNewClearsComboboxes
) {
1338 // Ensure the input under test is a combobox.
1340 controller()->ComboboxModelForAutofillType(CREDIT_CARD_EXP_MONTH
));
1342 // Set up an expired card.
1344 test::SetCreditCardInfo(&card
, "Roy Demeo", "4111111111111111", "8", "2013");
1345 card
.set_origin("Chrome settings");
1346 ASSERT_TRUE(card
.IsVerified());
1348 // Add the card and check that there's a menu for that section.
1349 controller()->GetTestingManager()->AddTestingCreditCard(&card
);
1350 ASSERT_TRUE(controller()->MenuModelForSection(SECTION_CC
));
1352 // Select the invalid, suggested card from the menu.
1353 controller()->MenuModelForSection(SECTION_CC
)->ActivatedAt(0);
1354 EXPECT_TRUE(controller()->IsEditingExistingData(SECTION_CC
));
1356 // Get the contents of the combobox of the credit card's expiration month.
1357 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
1358 base::string16 cc_exp_month_text
=
1359 view
->GetTextContentsOfInput(CREDIT_CARD_EXP_MONTH
);
1361 // Select "New X..." from the suggestion menu to clear the section's inputs.
1362 controller()->MenuModelForSection(SECTION_CC
)->ActivatedAt(1);
1363 EXPECT_FALSE(controller()->IsEditingExistingData(SECTION_CC
));
1365 // Ensure that the credit card expiration month has changed.
1366 EXPECT_NE(cc_exp_month_text
,
1367 view
->GetTextContentsOfInput(CREDIT_CARD_EXP_MONTH
));
1370 // Flaky on Win7 (http://crbug.com/446432)
1372 #define MAYBE_TabOpensToJustRight DISABLED_TabOpensToJustRight
1374 #define MAYBE_TabOpensToJustRight TabOpensToJustRight
1376 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1377 MAYBE_TabOpensToJustRight
) {
1378 ASSERT_TRUE(browser()->is_type_tabbed());
1380 // Tabs should currently be: / rAc() \.
1381 content::WebContents
* dialog_invoker
= controller()->GetWebContents();
1382 EXPECT_EQ(dialog_invoker
, GetActiveWebContents());
1384 TabStripModel
* tab_strip
= browser()->tab_strip_model();
1385 ASSERT_EQ(1, tab_strip
->count());
1386 EXPECT_EQ(0, tab_strip
->GetIndexOfWebContents(dialog_invoker
));
1388 // Open a tab to about:blank in the background at the end of the tab strip.
1389 chrome::AddTabAt(browser(), GURL(), -1, false);
1390 // Tabs should now be: / rAc() \/ blank \.
1391 EXPECT_EQ(2, tab_strip
->count());
1392 EXPECT_EQ(0, tab_strip
->active_index());
1393 EXPECT_EQ(dialog_invoker
, GetActiveWebContents());
1395 content::WebContents
* blank_tab
= tab_strip
->GetWebContentsAt(1);
1397 // Simulate clicking "Manage X...".
1398 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(2);
1399 // Tab should now be: / rAc() \/ manage 1 \/ blank \.
1400 EXPECT_EQ(3, tab_strip
->count());
1401 int dialog_index
= tab_strip
->GetIndexOfWebContents(dialog_invoker
);
1402 EXPECT_EQ(0, dialog_index
);
1403 EXPECT_EQ(1, tab_strip
->active_index());
1404 EXPECT_EQ(2, tab_strip
->GetIndexOfWebContents(blank_tab
));
1406 content::WebContents
* first_manage_tab
= tab_strip
->GetWebContentsAt(1);
1408 // Re-activate the dialog's tab (like a user would have to).
1409 tab_strip
->ActivateTabAt(dialog_index
, true);
1410 EXPECT_EQ(dialog_invoker
, GetActiveWebContents());
1412 // Simulate clicking "Manage X...".
1413 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(2);
1414 // Tabs should now be: / rAc() \/ manage 2 \/ manage 1 \/ blank \.
1415 EXPECT_EQ(4, tab_strip
->count());
1416 EXPECT_EQ(0, tab_strip
->GetIndexOfWebContents(dialog_invoker
));
1417 EXPECT_EQ(1, tab_strip
->active_index());
1418 EXPECT_EQ(2, tab_strip
->GetIndexOfWebContents(first_manage_tab
));
1419 EXPECT_EQ(3, tab_strip
->GetIndexOfWebContents(blank_tab
));
1422 // Flaky on Win7 (http://crbug.com/446432)
1424 #define MAYBE_SignInWebViewOpensLinksInNewTab DISABLED_SignInWebViewOpensLinksInNewTab
1426 #define MAYBE_SignInWebViewOpensLinksInNewTab SignInWebViewOpensLinksInNewTab
1428 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1429 MAYBE_SignInWebViewOpensLinksInNewTab
) {
1430 controller()->OnDidFetchWalletCookieValue(std::string());
1431 controller()->OnDidGetWalletItems(
1432 wallet::GetTestWalletItemsWithRequiredAction(wallet::GAIA_AUTH
));
1434 NavEntryCommittedObserver
sign_in_page_observer(
1435 controller()->FakeSignInUrl(),
1436 content::NotificationService::AllSources());
1438 controller()->SignInLinkClicked();
1440 content::WebContents
* sign_in_contents
=
1441 GetViewTester()->GetSignInWebContents();
1442 ASSERT_TRUE(sign_in_contents
);
1444 sign_in_page_observer
.Wait();
1446 content::OpenURLParams
params(GURL("http://google.com"),
1447 content::Referrer(),
1449 ui::PAGE_TRANSITION_LINK
,
1451 params
.user_gesture
= true;
1453 int num_tabs
= browser()->tab_strip_model()->count();
1454 sign_in_contents
->GetDelegate()->OpenURLFromTab(sign_in_contents
, params
);
1455 EXPECT_EQ(num_tabs
+ 1, browser()->tab_strip_model()->count());
1458 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, RefreshOnManageTabClose
) {
1459 ASSERT_TRUE(browser()->is_type_tabbed());
1461 // GetWalletItems(_, _) is called when controller() is created in SetUp().
1462 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems(_
, _
));
1463 controller()->OnDidFetchWalletCookieValue(std::string());
1464 controller()->OnDidGetWalletItems(
1465 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
));
1467 content::WebContents
* dialog_invoker
= controller()->GetWebContents();
1468 ChromeAutofillClient::FromWebContents(dialog_invoker
)
1469 ->SetDialogControllerForTesting(controller()->AsWeakPtr());
1471 // Open a new tab by selecting "Manage my shipping details..." in Wallet mode.
1472 EXPECT_EQ(1, browser()->tab_strip_model()->count());
1473 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(2);
1474 EXPECT_EQ(2, browser()->tab_strip_model()->count());
1475 ASSERT_NE(dialog_invoker
, GetActiveWebContents());
1477 // Closing the tab opened by "Manage my shipping details..." should refresh
1479 controller()->ClearLastWalletItemsFetchTimestampForTesting();
1480 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems(_
, _
));
1481 GetActiveWebContents()->Close();
1484 // Changes from Wallet to Autofill and verifies that the combined billing/cc
1485 // sections are showing (or not) at the correct times.
1486 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1487 ChangingDataSourceShowsCorrectSections
) {
1488 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
1489 EXPECT_TRUE(view
->IsShowingSection(SECTION_CC
));
1490 EXPECT_TRUE(view
->IsShowingSection(SECTION_BILLING
));
1491 EXPECT_FALSE(view
->IsShowingSection(SECTION_CC_BILLING
));
1493 // Switch the dialog to paying with Wallet.
1494 controller()->OnDidFetchWalletCookieValue(std::string());
1495 controller()->OnDidGetWalletItems(
1496 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
));
1497 ASSERT_TRUE(controller()->IsPayingWithWallet());
1499 EXPECT_FALSE(view
->IsShowingSection(SECTION_CC
));
1500 EXPECT_FALSE(view
->IsShowingSection(SECTION_BILLING
));
1501 EXPECT_TRUE(view
->IsShowingSection(SECTION_CC_BILLING
));
1503 // Now switch back to Autofill to ensure this direction works as well.
1504 ui::MenuModel
* account_chooser
= controller()->MenuModelForAccountChooser();
1505 account_chooser
->ActivatedAt(account_chooser
->GetItemCount() - 1);
1507 EXPECT_TRUE(view
->IsShowingSection(SECTION_CC
));
1508 EXPECT_TRUE(view
->IsShowingSection(SECTION_BILLING
));
1509 EXPECT_FALSE(view
->IsShowingSection(SECTION_CC_BILLING
));
1512 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1513 DoesWorkOnHttpWithFlag
) {
1514 net::SpawnedTestServer
http_server(
1515 net::SpawnedTestServer::TYPE_HTTP
,
1516 net::SpawnedTestServer::kLocalhost
,
1517 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
1518 ASSERT_TRUE(http_server
.Start());
1519 EXPECT_TRUE(RunTestPage(http_server
));
1522 // Like the parent test, but doesn't add the --reduce-security-for-testing flag.
1523 class AutofillDialogControllerSecurityTest
:
1524 public AutofillDialogControllerTest
{
1526 AutofillDialogControllerSecurityTest() {}
1527 ~AutofillDialogControllerSecurityTest() override
{}
1529 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
1530 CHECK(!command_line
->HasSwitch(::switches::kReduceSecurityForTesting
));
1533 typedef net::BaseTestServer::SSLOptions SSLOptions
;
1536 DISALLOW_COPY_AND_ASSIGN(AutofillDialogControllerSecurityTest
);
1539 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerSecurityTest
,
1541 net::SpawnedTestServer
http_server(
1542 net::SpawnedTestServer::TYPE_HTTP
,
1543 net::SpawnedTestServer::kLocalhost
,
1544 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
1545 ASSERT_TRUE(http_server
.Start());
1546 EXPECT_FALSE(RunTestPage(http_server
));
1549 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerSecurityTest
,
1550 DoesWorkOnHttpWithFlags
) {
1551 net::SpawnedTestServer
https_server(
1552 net::SpawnedTestServer::TYPE_HTTPS
,
1553 SSLOptions(SSLOptions::CERT_OK
),
1554 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
1555 ASSERT_TRUE(https_server
.Start());
1556 EXPECT_TRUE(RunTestPage(https_server
));
1559 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerSecurityTest
,
1560 DISABLED_DoesntWorkOnBrokenHttps
) {
1561 net::SpawnedTestServer
https_server(
1562 net::SpawnedTestServer::TYPE_HTTPS
,
1563 SSLOptions(SSLOptions::CERT_EXPIRED
),
1564 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
1565 ASSERT_TRUE(https_server
.Start());
1566 EXPECT_FALSE(RunTestPage(https_server
));
1569 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1570 CountryChangeRebuildsSection
) {
1571 EXPECT_FALSE(SectionHasField(SECTION_BILLING
, ADDRESS_BILLING_SORTING_CODE
));
1572 EXPECT_FALSE(SectionHasField(SECTION_SHIPPING
, ADDRESS_HOME_SORTING_CODE
));
1574 // Select "Add new shipping address...".
1575 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(1);
1577 // Add some valid user input that should be preserved when country changes.
1578 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
1579 view
->SetTextContentsOfInput(NAME_FULL
, ASCIIToUTF16("B. Loblaw"));
1581 // Change both sections' countries.
1582 view
->SetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
, ASCIIToUTF16("France"));
1583 view
->ActivateInput(ADDRESS_BILLING_COUNTRY
);
1584 view
->SetTextContentsOfInput(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("Belarus"));
1585 view
->ActivateInput(ADDRESS_HOME_COUNTRY
);
1587 // Verify the name is still there.
1588 EXPECT_EQ(ASCIIToUTF16("B. Loblaw"), view
->GetTextContentsOfInput(NAME_FULL
));
1590 EXPECT_TRUE(SectionHasField(SECTION_BILLING
, ADDRESS_BILLING_SORTING_CODE
));
1591 EXPECT_TRUE(SectionHasField(SECTION_SHIPPING
, ADDRESS_HOME_SORTING_CODE
));
1594 // Changing the data source to or from Wallet preserves the shipping country,
1595 // but not the billing country because Wallet only supports US billing
1597 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1598 ChangingDataSourcePreservesCountry
) {
1599 InitializeControllerWithoutShowing();
1600 controller()->GetTestingManager()->set_default_country_code("CA");
1601 controller()->Show();
1604 AutofillProfile
verified_profile(test::GetVerifiedProfile());
1605 controller()->GetTestingManager()->AddTestingProfile(&verified_profile
);
1607 CreditCard
verified_card(test::GetVerifiedCreditCard());
1608 controller()->GetTestingManager()->AddTestingCreditCard(&verified_card
);
1609 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_SHIPPING
));
1611 controller()->OnDidFetchWalletCookieValue(std::string());
1612 scoped_ptr
<wallet::WalletItems
> items
=
1613 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
1614 items
->AddAccount(wallet::GetTestGaiaAccount());
1615 items
->AddInstrument(wallet::GetTestMaskedInstrument());
1616 items
->AddAddress(wallet::GetTestShippingAddress());
1617 controller()->OnDidGetWalletItems(items
.Pass());
1619 EXPECT_TRUE(controller()->IsPayingWithWallet());
1621 // Select "Add new shipping address...".
1622 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(2);
1623 EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_SHIPPING
));
1625 // Default shipping country matches PDM's default, but default billing is
1626 // always US in Wallet mode.
1627 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
1628 ASSERT_EQ(ASCIIToUTF16("Canada"),
1629 view
->GetTextContentsOfInput(ADDRESS_HOME_COUNTRY
));
1630 ASSERT_EQ(ASCIIToUTF16("United States"),
1631 view
->GetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
));
1633 // Switch the shipping country.
1634 view
->SetTextContentsOfInput(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("Belarus"));
1635 EXPECT_EQ(ASCIIToUTF16("Belarus"),
1636 view
->GetTextContentsOfInput(ADDRESS_HOME_COUNTRY
));
1637 view
->ActivateInput(ADDRESS_HOME_COUNTRY
);
1639 // Switch to using Autofill instead of Wallet.
1640 ui::MenuModel
* account_chooser
= controller()->MenuModelForAccountChooser();
1641 account_chooser
->ActivatedAt(account_chooser
->GetItemCount() - 1);
1643 EXPECT_FALSE(controller()->IsPayingWithWallet());
1645 // Shipping country should have stayed the same.
1646 EXPECT_EQ(ASCIIToUTF16("Belarus"),
1647 view
->GetTextContentsOfInput(ADDRESS_HOME_COUNTRY
));
1648 ASSERT_TRUE(SectionHasField(SECTION_SHIPPING
, ADDRESS_HOME_SORTING_CODE
));
1650 controller()->MenuModelForSection(SECTION_BILLING
)->ActivatedAt(1);
1651 view
->SetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
,
1652 ASCIIToUTF16("Belarus"));
1653 view
->ActivateInput(ADDRESS_BILLING_COUNTRY
);
1654 EXPECT_EQ(ASCIIToUTF16("Belarus"),
1655 view
->GetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
));
1656 ASSERT_TRUE(SectionHasField(SECTION_BILLING
, ADDRESS_BILLING_SORTING_CODE
));
1658 // Switch back to Wallet. Country should go back to US.
1659 account_chooser
->ActivatedAt(0);
1660 EXPECT_EQ(ASCIIToUTF16("United States"),
1661 view
->GetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
));
1663 SectionHasField(SECTION_CC_BILLING
, ADDRESS_BILLING_SORTING_CODE
));
1665 // Make sure shipping is still on Belarus.
1666 EXPECT_EQ(ASCIIToUTF16("Belarus"),
1667 view
->GetTextContentsOfInput(ADDRESS_HOME_COUNTRY
));
1668 ASSERT_TRUE(SectionHasField(SECTION_SHIPPING
, ADDRESS_HOME_SORTING_CODE
));
1671 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, AddNewResetsCountry
) {
1672 AutofillProfile
verified_profile(test::GetVerifiedProfile());
1673 controller()->GetTestingManager()->AddTestingProfile(&verified_profile
);
1675 // Select "Add new billing/shipping address...".
1676 controller()->MenuModelForSection(SECTION_BILLING
)->ActivatedAt(1);
1677 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(2);
1679 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
1680 ASSERT_EQ(ASCIIToUTF16("United States"),
1681 view
->GetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
));
1682 ASSERT_EQ(ASCIIToUTF16("United States"),
1683 view
->GetTextContentsOfInput(ADDRESS_HOME_COUNTRY
));
1685 // Switch both billing and shipping countries.
1686 view
->SetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
, ASCIIToUTF16("Brazil"));
1687 view
->ActivateInput(ADDRESS_BILLING_COUNTRY
);
1688 view
->SetTextContentsOfInput(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("France"));
1689 view
->ActivateInput(ADDRESS_HOME_COUNTRY
);
1691 // Select "Add new billing/shipping address...".
1692 controller()->MenuModelForSection(SECTION_BILLING
)->ActivatedAt(1);
1693 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(2);
1695 EXPECT_EQ(ASCIIToUTF16("United States"),
1696 view
->GetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
));
1697 EXPECT_EQ(ASCIIToUTF16("United States"),
1698 view
->GetTextContentsOfInput(ADDRESS_HOME_COUNTRY
));
1701 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1702 FillingFormRebuildsInputs
) {
1703 AutofillProfile
full_profile(test::GetFullProfile());
1704 full_profile
.SetRawInfo(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("DE"));
1705 controller()->GetTestingManager()->AddTestingProfile(&full_profile
);
1707 // Select "Add new shipping address...".
1708 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(1);
1710 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
1711 ASSERT_EQ(ASCIIToUTF16("United States"),
1712 view
->GetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
));
1713 ASSERT_EQ(ASCIIToUTF16("United States"),
1714 view
->GetTextContentsOfInput(ADDRESS_HOME_COUNTRY
));
1716 const ServerFieldType input_type
= EMAIL_ADDRESS
;
1717 base::string16 name
= full_profile
.GetRawInfo(input_type
);
1718 view
->SetTextContentsOfInput(input_type
, name
.substr(0, name
.size() / 2));
1719 view
->ActivateInput(input_type
);
1720 ASSERT_EQ(input_type
, controller()->popup_input_type());
1721 controller()->DidAcceptSuggestion(base::string16(), 0);
1723 EXPECT_EQ(ASCIIToUTF16("Germany"),
1724 view
->GetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
));
1725 EXPECT_EQ(ASCIIToUTF16("United States"),
1726 view
->GetTextContentsOfInput(ADDRESS_HOME_COUNTRY
));
1729 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1730 FillingFormPreservesChangedCountry
) {
1731 AutofillProfile
full_profile(test::GetFullProfile());
1732 full_profile
.SetRawInfo(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("DE"));
1733 controller()->GetTestingManager()->AddTestingProfile(&full_profile
);
1735 // Select "Add new shipping address...".
1736 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(1);
1738 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
1739 view
->SetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
, ASCIIToUTF16("France"));
1740 view
->ActivateInput(ADDRESS_BILLING_COUNTRY
);
1741 view
->SetTextContentsOfInput(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("Belarus"));
1742 view
->ActivateInput(ADDRESS_HOME_COUNTRY
);
1744 base::string16 name
= full_profile
.GetRawInfo(NAME_FULL
);
1745 view
->SetTextContentsOfInput(NAME_FULL
, name
.substr(0, name
.size() / 2));
1746 view
->ActivateInput(NAME_FULL
);
1747 ASSERT_EQ(NAME_FULL
, controller()->popup_input_type());
1748 controller()->DidAcceptSuggestion(base::string16(), 0);
1750 EXPECT_EQ(ASCIIToUTF16("France"),
1751 view
->GetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
));
1752 EXPECT_EQ(ASCIIToUTF16("Belarus"),
1753 view
->GetTextContentsOfInput(ADDRESS_HOME_COUNTRY
));
1756 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, RulesLoaded
) {
1757 // Select "Add new shipping address...".
1758 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(1);
1759 controller()->set_use_validation(true);
1761 EXPECT_CALL(*controller()->GetMockValidator(),
1762 ValidateAddress(CountryCodeMatcher("DE"), _
, _
)).Times(2).
1763 WillOnce(Return(AddressValidator::RULES_NOT_READY
));
1765 // Validation should occur on country change and see the rules haven't loaded.
1766 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
1767 view
->SetTextContentsOfInput(ADDRESS_HOME_ZIP
, ASCIIToUTF16("123"));
1768 view
->SetTextContentsOfInput(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("Germany"));
1769 view
->ActivateInput(ADDRESS_HOME_COUNTRY
);
1771 // Different country loaded, validation should not occur.
1772 controller()->OnAddressValidationRulesLoaded("FR", true);
1774 // Relevant country loaded, validation should occur.
1775 controller()->OnAddressValidationRulesLoaded("DE", true);
1777 // Relevant country loaded but revalidation already happened, no further
1778 // validation should occur.
1779 controller()->OnAddressValidationRulesLoaded("DE", false);
1781 // Cancelling the dialog causes additional validation to see if the user
1782 // cancelled with invalid fields, so verify and clear here.
1783 testing::Mock::VerifyAndClearExpectations(controller()->GetMockValidator());
1786 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1787 TransactionAmount
) {
1789 "<input type='number' step='0.01'"
1790 " autocomplete='transaction-amount' value='24'>"
1791 "<input autocomplete='transaction-currency' value='USD'>"
1792 "<input autocomplete='cc-csc'>");
1793 AutofillDialogControllerImpl
* controller
= SetUpHtmlAndInvoke(html
);
1794 ASSERT_TRUE(controller
);
1796 EXPECT_EQ(ASCIIToUTF16("24"), controller
->transaction_amount_
);
1797 EXPECT_EQ(ASCIIToUTF16("USD"), controller
->transaction_currency_
);
1800 // Same as above, plus readonly attribute.
1801 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1802 TransactionAmountReadonly
) {
1804 "<input type='number' step='0.01'"
1805 " autocomplete='transaction-amount' value='24' readonly>"
1806 "<input autocomplete='transaction-currency' value='USD' readonly>"
1807 "<input autocomplete='cc-csc'>");
1808 AutofillDialogControllerImpl
* controller
= SetUpHtmlAndInvoke(html
);
1809 ASSERT_TRUE(controller
);
1811 EXPECT_EQ(ASCIIToUTF16("24"), controller
->transaction_amount_
);
1812 EXPECT_EQ(ASCIIToUTF16("USD"), controller
->transaction_currency_
);
1815 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, HideOnNavigate
) {
1816 base::WeakPtr
<TestAutofillDialogController
> weak_ptr
=
1817 controller()->AsWeakPtr();
1818 EXPECT_TRUE(weak_ptr
.get());
1820 ui_test_utils::NavigateToURL(browser(), GURL("about:blank"));
1821 EXPECT_FALSE(weak_ptr
.get());
1824 // Tests that the rAc dialog hides when the main frame is navigated, even if
1825 // it was invoked from a child frame.
1826 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, HideOnNavigateMainFrame
) {
1827 net::SpawnedTestServer
http_server(
1828 net::SpawnedTestServer::TYPE_HTTP
,
1829 net::SpawnedTestServer::kLocalhost
,
1830 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
1831 ASSERT_TRUE(http_server
.Start());
1832 RunTestPageInIframe(http_server
);
1834 ui_test_utils::NavigateToURL(browser(), GURL("about:blank"));
1835 ChromeAutofillClient
* client
=
1836 ChromeAutofillClient::FromWebContents(GetActiveWebContents());
1837 EXPECT_FALSE(client
->GetDialogControllerForTesting());
1840 // Tests that the rAc dialog hides when the iframe it's in is navigated.
1841 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, HideOnNavigateIframe
) {
1842 net::SpawnedTestServer
http_server(
1843 net::SpawnedTestServer::TYPE_HTTP
,
1844 net::SpawnedTestServer::kLocalhost
,
1845 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
1846 ASSERT_TRUE(http_server
.Start());
1847 RunTestPageInIframe(http_server
);
1850 ASSERT_TRUE(content::ExecuteScriptAndExtractString(GetRenderViewHost(),
1853 ExpectDomMessage("iframe loaded");
1854 ChromeAutofillClient
* client
=
1855 ChromeAutofillClient::FromWebContents(GetActiveWebContents());
1856 EXPECT_FALSE(client
->GetDialogControllerForTesting());
1859 } // namespace autofill