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/time/time.h"
14 #include "chrome/browser/autofill/personal_data_manager_factory.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/autofill/account_chooser_model.h"
17 #include "chrome/browser/ui/autofill/autofill_dialog_controller_impl.h"
18 #include "chrome/browser/ui/autofill/autofill_dialog_i18n_input.h"
19 #include "chrome/browser/ui/autofill/autofill_dialog_view.h"
20 #include "chrome/browser/ui/autofill/autofill_dialog_view_tester.h"
21 #include "chrome/browser/ui/autofill/data_model_wrapper.h"
22 #include "chrome/browser/ui/autofill/mock_address_validator.h"
23 #include "chrome/browser/ui/autofill/tab_autofill_manager_delegate.h"
24 #include "chrome/browser/ui/autofill/test_generated_credit_card_bubble_controller.h"
25 #include "chrome/browser/ui/browser.h"
26 #include "chrome/browser/ui/browser_tabstrip.h"
27 #include "chrome/browser/ui/tabs/tab_strip_model.h"
28 #include "chrome/common/chrome_switches.h"
29 #include "chrome/common/pref_names.h"
30 #include "chrome/common/url_constants.h"
31 #include "chrome/test/base/in_process_browser_test.h"
32 #include "chrome/test/base/ui_test_utils.h"
33 #include "components/autofill/content/browser/risk/proto/fingerprint.pb.h"
34 #include "components/autofill/content/browser/wallet/gaia_account.h"
35 #include "components/autofill/content/browser/wallet/mock_wallet_client.h"
36 #include "components/autofill/content/browser/wallet/wallet_service_url.h"
37 #include "components/autofill/content/browser/wallet/wallet_test_util.h"
38 #include "components/autofill/core/browser/autofill_metrics.h"
39 #include "components/autofill/core/browser/autofill_test_utils.h"
40 #include "components/autofill/core/browser/test_personal_data_manager.h"
41 #include "components/autofill/core/browser/validation.h"
42 #include "components/autofill/core/common/autofill_switches.h"
43 #include "components/autofill/core/common/form_data.h"
44 #include "components/autofill/core/common/form_field_data.h"
45 #include "content/public/browser/browser_thread.h"
46 #include "content/public/browser/interstitial_page.h"
47 #include "content/public/browser/navigation_details.h"
48 #include "content/public/browser/navigation_entry.h"
49 #include "content/public/browser/notification_service.h"
50 #include "content/public/browser/notification_types.h"
51 #include "content/public/browser/page_navigator.h"
52 #include "content/public/browser/web_contents.h"
53 #include "content/public/browser/web_contents_delegate.h"
54 #include "content/public/common/content_switches.h"
55 #include "content/public/common/page_transition_types.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/window_open_disposition.h"
69 #include "base/win/windows_version.h"
70 #elif defined(OS_MACOSX)
71 #include "base/mac/mac_util.h"
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
;
84 using ::i18n::addressinput::AddressValidator
;
86 void MockCallback(const FormStructure
*) {}
88 class MockAutofillMetrics
: public AutofillMetrics
{
91 : dialog_dismissal_action_(static_cast<DialogDismissalAction
>(-1)) {}
92 virtual ~MockAutofillMetrics() {}
94 virtual void LogDialogUiDuration(
95 const base::TimeDelta
& duration
,
96 DialogDismissalAction dismissal_action
) const OVERRIDE
{
97 // Ignore constness for testing.
98 MockAutofillMetrics
* mutable_this
= const_cast<MockAutofillMetrics
*>(this);
99 mutable_this
->dialog_dismissal_action_
= dismissal_action
;
102 AutofillMetrics::DialogDismissalAction
dialog_dismissal_action() const {
103 return dialog_dismissal_action_
;
106 MOCK_CONST_METHOD1(LogDialogDismissalState
,
107 void(DialogDismissalState state
));
110 DialogDismissalAction dialog_dismissal_action_
;
112 DISALLOW_COPY_AND_ASSIGN(MockAutofillMetrics
);
115 class TestAutofillDialogController
: public AutofillDialogControllerImpl
{
117 TestAutofillDialogController(
118 content::WebContents
* contents
,
119 const FormData
& form_data
,
120 const AutofillMetrics
& metric_logger
,
121 scoped_refptr
<content::MessageLoopRunner
> runner
)
122 : AutofillDialogControllerImpl(contents
,
125 base::Bind(&MockCallback
)),
126 metric_logger_(metric_logger
),
128 Profile::FromBrowserContext(contents
->GetBrowserContext())->
129 GetRequestContext(), this, form_data
.origin
),
130 message_loop_runner_(runner
),
131 use_validation_(false),
132 weak_ptr_factory_(this),
133 sign_in_user_index_(0U) {
136 Profile::FromBrowserContext(contents
->GetBrowserContext())->GetPrefs(),
140 virtual ~TestAutofillDialogController() {}
142 virtual GURL
SignInUrl() const OVERRIDE
{
143 return GURL(chrome::kChromeUIVersionURL
);
146 virtual void ViewClosed() OVERRIDE
{
147 message_loop_runner_
->Quit();
148 AutofillDialogControllerImpl::ViewClosed();
151 virtual base::string16
InputValidityMessage(
152 DialogSection section
,
153 ServerFieldType type
,
154 const base::string16
& value
) OVERRIDE
{
155 if (!use_validation_
)
156 return base::string16();
157 return AutofillDialogControllerImpl::InputValidityMessage(
158 section
, type
, value
);
161 virtual ValidityMessages
InputsAreValid(
162 DialogSection section
,
163 const FieldValueMap
& inputs
) OVERRIDE
{
164 if (!use_validation_
)
165 return ValidityMessages();
166 return AutofillDialogControllerImpl::InputsAreValid(section
, inputs
);
169 // Saving to Chrome is tested in AutofillDialogControllerImpl unit tests.
170 // TODO(estade): test that the view defaults to saving to Chrome.
171 virtual bool ShouldOfferToSaveInChrome() const OVERRIDE
{
175 void ForceFinishSubmit() {
179 // Increase visibility for testing.
180 using AutofillDialogControllerImpl::view
;
181 using AutofillDialogControllerImpl::popup_input_type
;
183 MOCK_METHOD0(LoadRiskFingerprintData
, void());
185 virtual std::vector
<DialogNotification
> CurrentNotifications() OVERRIDE
{
186 return notifications_
;
189 void set_notifications(const std::vector
<DialogNotification
>& notifications
) {
190 notifications_
= notifications
;
193 TestPersonalDataManager
* GetTestingManager() {
194 return &test_manager_
;
197 MockAddressValidator
* GetMockValidator() {
198 return &mock_validator_
;
201 using AutofillDialogControllerImpl::IsEditingExistingData
;
202 using AutofillDialogControllerImpl::IsManuallyEditingSection
;
203 using AutofillDialogControllerImpl::IsPayingWithWallet
;
204 using AutofillDialogControllerImpl::IsSubmitPausedOn
;
205 using AutofillDialogControllerImpl::OnDidLoadRiskFingerprintData
;
206 using AutofillDialogControllerImpl::AccountChooserModelForTesting
;
207 using AutofillDialogControllerImpl::
208 ClearLastWalletItemsFetchTimestampForTesting
;
210 void set_use_validation(bool use_validation
) {
211 use_validation_
= use_validation
;
214 base::WeakPtr
<TestAutofillDialogController
> AsWeakPtr() {
215 return weak_ptr_factory_
.GetWeakPtr();
218 wallet::MockWalletClient
* GetTestingWalletClient() {
219 return &mock_wallet_client_
;
222 void set_sign_in_user_index(size_t sign_in_user_index
) {
223 sign_in_user_index_
= sign_in_user_index
;
227 virtual PersonalDataManager
* GetManager() const OVERRIDE
{
228 return &const_cast<TestAutofillDialogController
*>(this)->test_manager_
;
231 virtual AddressValidator
* GetValidator() OVERRIDE
{
232 return &mock_validator_
;
235 virtual wallet::WalletClient
* GetWalletClient() OVERRIDE
{
236 return &mock_wallet_client_
;
239 virtual bool IsSignInContinueUrl(const GURL
& url
, size_t* user_index
) const
241 *user_index
= sign_in_user_index_
;
242 return url
== wallet::GetSignInContinueUrl();
246 // To specify our own metric logger.
247 virtual const AutofillMetrics
& GetMetricLogger() const OVERRIDE
{
248 return metric_logger_
;
251 const AutofillMetrics
& metric_logger_
;
252 TestPersonalDataManager test_manager_
;
253 testing::NiceMock
<MockAddressValidator
> mock_validator_
;
254 testing::NiceMock
<wallet::MockWalletClient
> mock_wallet_client_
;
255 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner_
;
256 bool use_validation_
;
258 // A list of notifications to show in the notification area of the dialog.
259 // This is used to control what |CurrentNotifications()| returns for testing.
260 std::vector
<DialogNotification
> notifications_
;
262 // Allows generation of WeakPtrs, so controller liveness can be tested.
263 base::WeakPtrFactory
<TestAutofillDialogController
> weak_ptr_factory_
;
265 // The user index that is assigned in IsSignInContinueUrl().
266 size_t sign_in_user_index_
;
268 DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogController
);
271 // This is a copy of ui_test_utils::UrlLoadObserver, except it observes
272 // NAV_ENTRY_COMMITTED instead of LOAD_STOP. This is to match the notification
273 // that AutofillDialogControllerImpl observes. Since NAV_ENTRY_COMMITTED comes
274 // before LOAD_STOP, and the controller deletes the web contents after receiving
275 // the former, we will sometimes fail to observe a LOAD_STOP.
276 // TODO(estade): Should the controller observe LOAD_STOP instead?
277 class NavEntryCommittedObserver
: public content::WindowedNotificationObserver
{
279 NavEntryCommittedObserver(const GURL
& url
,
280 const content::NotificationSource
& source
)
281 : WindowedNotificationObserver(content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
285 virtual ~NavEntryCommittedObserver() {}
287 // content::NotificationObserver:
288 virtual void Observe(int type
,
289 const content::NotificationSource
& source
,
290 const content::NotificationDetails
& details
) OVERRIDE
{
291 content::LoadCommittedDetails
* load_details
=
292 content::Details
<content::LoadCommittedDetails
>(details
).ptr();
293 if (load_details
->entry
->GetVirtualURL() != url_
)
296 WindowedNotificationObserver::Observe(type
, source
, details
);
302 DISALLOW_COPY_AND_ASSIGN(NavEntryCommittedObserver
);
307 class AutofillDialogControllerTest
: public InProcessBrowserTest
{
309 AutofillDialogControllerTest() : controller_(NULL
) {}
310 virtual ~AutofillDialogControllerTest() {}
312 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
313 command_line
->AppendSwitch(::switches::kReduceSecurityForTesting
);
316 virtual void SetUpOnMainThread() OVERRIDE
{
317 autofill::test::DisableSystemServices(browser()->profile()->GetPrefs());
318 InitializeController();
322 bool SectionHasField(DialogSection section
, ServerFieldType type
) {
323 const DetailInputs
& fields
=
324 controller()->RequestedFieldsForSection(section
);
325 for (size_t i
= 0; i
< fields
.size(); ++i
) {
326 if (type
== fields
[i
].type
)
332 // A helper function that cycles the MessageLoop, and on Mac, the Cocoa run
333 // loop. It also drains the NSAutoreleasePool.
334 void CycleRunLoops() {
335 content::RunAllPendingInMessageLoop();
336 #if defined(OS_MACOSX)
337 chrome::testing::NSRunLoopRunAllPending();
338 AutoreleasePool()->Recycle();
342 void InitializeControllerWithoutShowing() {
347 form
.name
= ASCIIToUTF16("TestForm");
348 form
.method
= ASCIIToUTF16("POST");
349 form
.user_submitted
= true;
352 field
.autocomplete_attribute
= "shipping tel";
353 form
.fields
.push_back(field
);
355 test_generated_bubble_controller_
=
356 new testing::NiceMock
<TestGeneratedCreditCardBubbleController
>(
357 GetActiveWebContents());
358 ASSERT_TRUE(test_generated_bubble_controller_
->IsInstalled());
360 message_loop_runner_
= new content::MessageLoopRunner
;
361 controller_
= new TestAutofillDialogController(
362 GetActiveWebContents(),
365 message_loop_runner_
);
368 void InitializeController() {
369 InitializeControllerWithoutShowing();
371 CycleRunLoops(); // Ensures dialog is fully visible.
374 content::WebContents
* GetActiveWebContents() {
375 return browser()->tab_strip_model()->GetActiveWebContents();
378 content::RenderViewHost
* GetRenderViewHost() {
379 return GetActiveWebContents()->GetRenderViewHost();
382 scoped_ptr
<AutofillDialogViewTester
> GetViewTester() {
383 return AutofillDialogViewTester::For(controller()->view()).Pass();
386 const MockAutofillMetrics
& metric_logger() { return metric_logger_
; }
387 TestAutofillDialogController
* controller() { return controller_
; }
389 void RunMessageLoop() {
390 message_loop_runner_
->Run();
393 // Loads an HTML page in |GetActiveWebContents()| with markup as follows:
394 // <form>|form_inner_html|</form>. After loading, emulates a click event on
395 // the page as requestAutocomplete() must be in response to a user gesture.
396 // Returns the |AutofillDialogControllerImpl| created by this invocation.
397 AutofillDialogControllerImpl
* SetUpHtmlAndInvoke(
398 const std::string
& form_inner_html
) {
399 content::WebContents
* contents
= GetActiveWebContents();
400 TabAutofillManagerDelegate
* delegate
=
401 TabAutofillManagerDelegate::FromWebContents(contents
);
402 CHECK(!delegate
->GetDialogControllerForTesting());
404 ui_test_utils::NavigateToURL(
405 browser(), GURL(std::string("data:text/html,") +
409 "<form>" + form_inner_html
+ "</form>"
411 "function send(msg) {"
412 "domAutomationController.setAutomationId(0);"
413 "domAutomationController.send(msg);"
415 "document.forms[0].onautocompleteerror = function(e) {"
416 "send('error: ' + e.reason);"
418 "document.forms[0].onautocomplete = function() {"
421 "window.onclick = function() {"
422 "document.forms[0].requestAutocomplete();"
425 "function loadIframe() {"
426 " var iframe = document.createElement('iframe');"
427 " iframe.onload = function() {"
428 " send('iframe loaded');"
430 " iframe.src = 'about:blank';"
431 " document.body.appendChild(iframe);"
433 "function getValueForFieldOfType(type) {"
434 " var fields = document.getElementsByTagName('input');"
435 " for (var i = 0; i < fields.length; i++) {"
436 " if (fields[i].autocomplete == type) {"
437 " send(fields[i].value);"
448 AutofillDialogControllerImpl
* controller
=
449 static_cast<AutofillDialogControllerImpl
*>(
450 delegate
->GetDialogControllerForTesting());
454 // Loads an html page on a provided server, the causes it to launch rAc.
455 // Returns whether rAc succesfully launched.
456 bool RunTestPage(const net::SpawnedTestServer
& server
) {
457 GURL url
= server
.GetURL(
458 "files/request_autocomplete/test_page.html");
459 ui_test_utils::NavigateToURL(browser(), url
);
461 // Pass through the broken SSL interstitial, if any.
462 content::WebContents
* contents
= GetActiveWebContents();
463 content::InterstitialPage
* interstitial_page
=
464 contents
->GetInterstitialPage();
465 if (interstitial_page
) {
466 ui_test_utils::UrlLoadObserver
observer(
468 content::Source
<content::NavigationController
>(
469 &contents
->GetController()));
470 interstitial_page
->Proceed();
476 TabAutofillManagerDelegate
* delegate
=
477 TabAutofillManagerDelegate::FromWebContents(contents
);
478 AutofillDialogControllerImpl
* controller
=
479 static_cast<AutofillDialogControllerImpl
*>(
480 delegate
->GetDialogControllerForTesting());
484 // Wait for a message from the DOM automation controller (from JS in the
485 // page). Requires |SetUpHtmlAndInvoke()| be called first.
486 void ExpectDomMessage(const std::string
& expected
) {
488 ASSERT_TRUE(dom_message_queue_
->WaitForMessage(&message
));
489 dom_message_queue_
->ClearQueue();
490 EXPECT_EQ("\"" + expected
+ "\"", message
);
493 void InitiateDialog() {
494 dom_message_queue_
.reset(new content::DOMMessageQueue
);
496 // Triggers the onclick handler which invokes requestAutocomplete().
497 content::WebContents
* contents
= GetActiveWebContents();
498 content::SimulateMouseClick(contents
, 0, blink::WebMouseEvent::ButtonLeft
);
499 ExpectDomMessage("clicked");
502 // Returns the value filled into the first field with autocomplete attribute
503 // equal to |autocomplete_type|, or an empty string if there is no such field.
504 std::string
GetValueForHTMLFieldOfType(const std::string
& autocomplete_type
) {
505 std::string script
= "getValueForFieldOfType('" + autocomplete_type
+ "');";
507 EXPECT_TRUE(content::ExecuteScriptAndExtractString(GetRenderViewHost(),
513 void AddCreditcardToProfile(Profile
* profile
, const CreditCard
& card
) {
514 PersonalDataManagerFactory::GetForProfile(profile
)->AddCreditCard(card
);
518 void AddAutofillProfileToProfile(Profile
* profile
,
519 const AutofillProfile
& autofill_profile
) {
520 PersonalDataManagerFactory::GetForProfile(profile
)->AddProfile(
525 TestGeneratedCreditCardBubbleController
* test_generated_bubble_controller() {
526 return test_generated_bubble_controller_
;
530 void WaitForWebDB() {
531 content::RunAllPendingInMessageLoop(content::BrowserThread::DB
);
534 testing::NiceMock
<MockAutofillMetrics
> metric_logger_
;
535 TestAutofillDialogController
* controller_
; // Weak reference.
536 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner_
;
537 scoped_ptr
<content::DOMMessageQueue
> dom_message_queue_
;
539 // Weak; owned by the active web contents.
540 TestGeneratedCreditCardBubbleController
* test_generated_bubble_controller_
;
542 DISALLOW_COPY_AND_ASSIGN(AutofillDialogControllerTest
);
545 // Submit the form data.
546 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, Submit
) {
547 GetViewTester()->SubmitForTesting();
550 EXPECT_EQ(AutofillMetrics::DIALOG_ACCEPTED
,
551 metric_logger().dialog_dismissal_action());
554 // Cancel out of the dialog.
555 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, Cancel
) {
556 EXPECT_CALL(metric_logger(),
557 LogDialogDismissalState(
558 AutofillMetrics::DIALOG_CANCELED_NO_INVALID_FIELDS
));
560 GetViewTester()->CancelForTesting();
563 EXPECT_EQ(AutofillMetrics::DIALOG_CANCELED
,
564 metric_logger().dialog_dismissal_action());
567 // Take some other action that dismisses the dialog.
568 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, Hide
) {
569 EXPECT_CALL(metric_logger(),
570 LogDialogDismissalState(
571 AutofillMetrics::DIALOG_CANCELED_NO_INVALID_FIELDS
));
572 controller()->Hide();
576 EXPECT_EQ(AutofillMetrics::DIALOG_CANCELED
,
577 metric_logger().dialog_dismissal_action());
580 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, CancelWithSuggestions
) {
581 EXPECT_CALL(metric_logger(),
582 LogDialogDismissalState(
583 AutofillMetrics::DIALOG_CANCELED_NO_EDITS
));
585 CreditCard
card(test::GetVerifiedCreditCard());
586 controller()->GetTestingManager()->AddTestingCreditCard(&card
);
587 AutofillProfile
profile(test::GetVerifiedProfile());
588 controller()->GetTestingManager()->AddTestingProfile(&profile
);
590 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_CC
));
591 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_BILLING
));
592 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_SHIPPING
));
594 GetViewTester()->CancelForTesting();
597 EXPECT_EQ(AutofillMetrics::DIALOG_CANCELED
,
598 metric_logger().dialog_dismissal_action());
601 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, AcceptWithSuggestions
) {
602 EXPECT_CALL(metric_logger(),
603 LogDialogDismissalState(
604 AutofillMetrics::DIALOG_ACCEPTED_EXISTING_AUTOFILL_DATA
));
606 CreditCard
card(test::GetVerifiedCreditCard());
607 controller()->GetTestingManager()->AddTestingCreditCard(&card
);
608 AutofillProfile
profile(test::GetVerifiedProfile());
609 controller()->GetTestingManager()->AddTestingProfile(&profile
);
611 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_CC
));
612 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_BILLING
));
613 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_SHIPPING
));
615 GetViewTester()->SubmitForTesting();
618 EXPECT_EQ(AutofillMetrics::DIALOG_ACCEPTED
,
619 metric_logger().dialog_dismissal_action());
622 // Ensure that Hide() will only destroy the controller object after the
623 // message loop has run. Otherwise, there may be read-after-free issues
624 // during some tests.
625 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, DeferredDestruction
) {
626 base::WeakPtr
<TestAutofillDialogController
> weak_ptr
=
627 controller()->AsWeakPtr();
628 EXPECT_TRUE(weak_ptr
.get());
630 controller()->Hide();
631 EXPECT_TRUE(weak_ptr
.get());
634 EXPECT_FALSE(weak_ptr
.get());
637 // Ensure that the expected metric is logged when the dialog is closed during
639 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, CloseDuringSignin
) {
640 controller()->SignInLinkClicked();
642 EXPECT_CALL(metric_logger(),
643 LogDialogDismissalState(
644 AutofillMetrics::DIALOG_CANCELED_DURING_SIGNIN
));
645 GetViewTester()->CancelForTesting();
649 EXPECT_EQ(AutofillMetrics::DIALOG_CANCELED
,
650 metric_logger().dialog_dismissal_action());
653 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, FillInputFromAutofill
) {
654 AutofillProfile
full_profile(test::GetFullProfile());
655 controller()->GetTestingManager()->AddTestingProfile(&full_profile
);
657 // Select "Add new shipping address...".
658 ui::MenuModel
* model
= controller()->MenuModelForSection(SECTION_SHIPPING
);
659 model
->ActivatedAt(model
->GetItemCount() - 2);
660 ASSERT_TRUE(controller()->IsManuallyEditingSection(SECTION_SHIPPING
));
662 const DetailInputs
& inputs
=
663 controller()->RequestedFieldsForSection(SECTION_SHIPPING
);
664 const ServerFieldType triggering_type
= inputs
[0].type
;
665 base::string16 value
= full_profile
.GetRawInfo(triggering_type
);
666 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
667 view
->SetTextContentsOfInput(triggering_type
,
668 value
.substr(0, value
.size() / 2));
669 view
->ActivateInput(triggering_type
);
671 ASSERT_EQ(triggering_type
, controller()->popup_input_type());
672 controller()->DidAcceptSuggestion(base::string16(), 0);
674 // All inputs should be filled.
675 AutofillProfileWrapper
wrapper(&full_profile
);
676 for (size_t i
= 0; i
< inputs
.size(); ++i
) {
677 EXPECT_EQ(wrapper
.GetInfo(AutofillType(inputs
[i
].type
)),
678 view
->GetTextContentsOfInput(inputs
[i
].type
));
681 // Now simulate some user edits and try again.
682 std::vector
<base::string16
> expectations
;
683 for (size_t i
= 0; i
< inputs
.size(); ++i
) {
684 if (controller()->ComboboxModelForAutofillType(inputs
[i
].type
)) {
685 expectations
.push_back(base::string16());
688 base::string16 users_input
= i
% 2 == 0 ? base::string16()
689 : ASCIIToUTF16("dummy");
690 view
->SetTextContentsOfInput(inputs
[i
].type
, users_input
);
691 // Empty inputs should be filled, others should be left alone.
692 base::string16 expectation
=
693 inputs
[i
].type
== triggering_type
|| users_input
.empty() ?
694 wrapper
.GetInfo(AutofillType(inputs
[i
].type
)) :
696 expectations
.push_back(expectation
);
699 view
->SetTextContentsOfInput(triggering_type
,
700 value
.substr(0, value
.size() / 2));
701 view
->ActivateInput(triggering_type
);
702 ASSERT_EQ(triggering_type
, controller()->popup_input_type());
703 controller()->DidAcceptSuggestion(base::string16(), 0);
705 for (size_t i
= 0; i
< inputs
.size(); ++i
) {
706 if (controller()->ComboboxModelForAutofillType(inputs
[i
].type
))
708 EXPECT_EQ(expectations
[i
], view
->GetTextContentsOfInput(inputs
[i
].type
));
711 EXPECT_CALL(metric_logger(),
712 LogDialogDismissalState(
713 AutofillMetrics::DIALOG_ACCEPTED_SAVE_TO_AUTOFILL
));
714 view
->SubmitForTesting();
717 // This test makes sure that picking a profile variant in the Autofill
718 // popup works as expected.
719 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
720 FillInputFromAutofillVariant
) {
721 AutofillProfile
full_profile(test::GetFullProfile());
723 // Set up some variant data.
724 std::vector
<base::string16
> names
;
725 names
.push_back(ASCIIToUTF16("John Doe"));
726 names
.push_back(ASCIIToUTF16("Jane Doe"));
727 full_profile
.SetRawMultiInfo(NAME_FULL
, names
);
728 std::vector
<base::string16
> emails
;
729 emails
.push_back(ASCIIToUTF16("user@example.com"));
730 emails
.push_back(ASCIIToUTF16("admin@example.com"));
731 full_profile
.SetRawMultiInfo(EMAIL_ADDRESS
, emails
);
732 controller()->GetTestingManager()->AddTestingProfile(&full_profile
);
734 const DetailInputs
& inputs
=
735 controller()->RequestedFieldsForSection(SECTION_BILLING
);
736 const ServerFieldType triggering_type
= inputs
[0].type
;
737 EXPECT_EQ(NAME_BILLING_FULL
, triggering_type
);
738 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
739 view
->ActivateInput(triggering_type
);
741 ASSERT_EQ(triggering_type
, controller()->popup_input_type());
743 // Choose the variant suggestion.
744 controller()->DidAcceptSuggestion(base::string16(), 1);
746 // All inputs should be filled.
747 AutofillProfileWrapper
wrapper(
748 &full_profile
, AutofillType(NAME_BILLING_FULL
), 1);
749 for (size_t i
= 0; i
< inputs
.size(); ++i
) {
750 EXPECT_EQ(wrapper
.GetInfo(AutofillType(inputs
[i
].type
)),
751 view
->GetTextContentsOfInput(inputs
[i
].type
));
754 // Make sure the wrapper applies the variant index to the right group.
755 EXPECT_EQ(names
[1], wrapper
.GetInfo(AutofillType(NAME_BILLING_FULL
)));
756 // Make sure the wrapper doesn't apply the variant index to the wrong group.
757 EXPECT_EQ(emails
[0], wrapper
.GetInfo(AutofillType(EMAIL_ADDRESS
)));
760 // Tests that changing the value of a CC expiration date combobox works as
761 // expected when Autofill is used to fill text inputs.
763 // Flaky on Win7, WinXP, and Win Aura. http://crbug.com/270314.
765 #define MAYBE_FillComboboxFromAutofill DISABLED_FillComboboxFromAutofill
767 #define MAYBE_FillComboboxFromAutofill FillComboboxFromAutofill
769 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
770 MAYBE_FillComboboxFromAutofill
) {
772 test::SetCreditCardInfo(&card1
, "JJ Smith", "4111111111111111", "12", "2018");
773 controller()->GetTestingManager()->AddTestingCreditCard(&card1
);
775 test::SetCreditCardInfo(&card2
, "B Bird", "3111111111111111", "11", "2017");
776 controller()->GetTestingManager()->AddTestingCreditCard(&card2
);
777 AutofillProfile
full_profile(test::GetFullProfile());
778 controller()->GetTestingManager()->AddTestingProfile(&full_profile
);
780 const DetailInputs
& inputs
=
781 controller()->RequestedFieldsForSection(SECTION_CC
);
782 const ServerFieldType triggering_type
= inputs
[0].type
;
783 base::string16 value
= card1
.GetRawInfo(triggering_type
);
784 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
785 view
->SetTextContentsOfInput(triggering_type
,
786 value
.substr(0, value
.size() / 2));
787 view
->ActivateInput(triggering_type
);
789 ASSERT_EQ(triggering_type
, controller()->popup_input_type());
790 controller()->DidAcceptSuggestion(base::string16(), 0);
792 // All inputs should be filled.
793 AutofillCreditCardWrapper
wrapper1(&card1
);
794 for (size_t i
= 0; i
< inputs
.size(); ++i
) {
795 EXPECT_EQ(wrapper1
.GetInfo(AutofillType(inputs
[i
].type
)),
796 view
->GetTextContentsOfInput(inputs
[i
].type
));
799 // Try again with different data. Only expiration date and the triggering
800 // input should be overwritten.
801 value
= card2
.GetRawInfo(triggering_type
);
802 view
->SetTextContentsOfInput(triggering_type
,
803 value
.substr(0, value
.size() / 2));
804 view
->ActivateInput(triggering_type
);
805 ASSERT_EQ(triggering_type
, controller()->popup_input_type());
806 controller()->DidAcceptSuggestion(base::string16(), 0);
808 AutofillCreditCardWrapper
wrapper2(&card2
);
809 for (size_t i
= 0; i
< inputs
.size(); ++i
) {
810 const ServerFieldType type
= inputs
[i
].type
;
811 if (type
== triggering_type
||
812 type
== CREDIT_CARD_EXP_MONTH
||
813 type
== CREDIT_CARD_EXP_4_DIGIT_YEAR
) {
814 EXPECT_EQ(wrapper2
.GetInfo(AutofillType(type
)),
815 view
->GetTextContentsOfInput(type
));
816 } else if (type
== CREDIT_CARD_VERIFICATION_CODE
) {
817 EXPECT_TRUE(view
->GetTextContentsOfInput(type
).empty());
819 EXPECT_EQ(wrapper1
.GetInfo(AutofillType(type
)),
820 view
->GetTextContentsOfInput(type
));
824 // Now fill from a profile. It should not overwrite any CC info.
825 const DetailInputs
& billing_inputs
=
826 controller()->RequestedFieldsForSection(SECTION_BILLING
);
827 const ServerFieldType billing_triggering_type
= billing_inputs
[0].type
;
828 value
= full_profile
.GetRawInfo(triggering_type
);
829 view
->SetTextContentsOfInput(billing_triggering_type
,
830 value
.substr(0, value
.size() / 2));
831 view
->ActivateInput(billing_triggering_type
);
833 ASSERT_EQ(billing_triggering_type
, controller()->popup_input_type());
834 controller()->DidAcceptSuggestion(base::string16(), 0);
836 for (size_t i
= 0; i
< inputs
.size(); ++i
) {
837 const ServerFieldType type
= inputs
[i
].type
;
838 if (type
== triggering_type
||
839 type
== CREDIT_CARD_EXP_MONTH
||
840 type
== CREDIT_CARD_EXP_4_DIGIT_YEAR
) {
841 EXPECT_EQ(wrapper2
.GetInfo(AutofillType(type
)),
842 view
->GetTextContentsOfInput(type
));
843 } else if (type
== CREDIT_CARD_VERIFICATION_CODE
) {
844 EXPECT_TRUE(view
->GetTextContentsOfInput(type
).empty());
846 EXPECT_EQ(wrapper1
.GetInfo(AutofillType(type
)),
847 view
->GetTextContentsOfInput(type
));
852 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, ShouldShowErrorBubble
) {
853 controller()->set_use_validation(true);
854 EXPECT_TRUE(controller()->ShouldShowErrorBubble());
856 CreditCard
card(test::GetCreditCard());
857 ASSERT_FALSE(card
.IsVerified());
858 controller()->GetTestingManager()->AddTestingCreditCard(&card
);
860 EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_CC
));
861 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
862 view
->SetTextContentsOfInput(
864 card
.GetRawInfo(CREDIT_CARD_NUMBER
).substr(0, 1));
866 view
->ActivateInput(CREDIT_CARD_NUMBER
);
867 EXPECT_FALSE(controller()->ShouldShowErrorBubble());
869 controller()->FocusMoved();
870 EXPECT_TRUE(controller()->ShouldShowErrorBubble());
872 EXPECT_CALL(metric_logger(),
873 LogDialogDismissalState(
874 AutofillMetrics::DIALOG_CANCELED_WITH_INVALID_FIELDS
));
875 controller()->Hide();
878 // Ensure that expired cards trigger invalid suggestions.
879 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, ExpiredCard
) {
880 CreditCard
verified_card(test::GetCreditCard());
881 verified_card
.set_origin("Chrome settings");
882 ASSERT_TRUE(verified_card
.IsVerified());
883 controller()->GetTestingManager()->AddTestingCreditCard(&verified_card
);
885 CreditCard
expired_card(test::GetCreditCard());
886 expired_card
.set_origin("Chrome settings");
887 expired_card
.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR
, ASCIIToUTF16("2007"));
888 ASSERT_TRUE(expired_card
.IsVerified());
890 autofill::IsValidCreditCardExpirationDate(
891 expired_card
.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR
),
892 expired_card
.GetRawInfo(CREDIT_CARD_EXP_MONTH
),
894 controller()->GetTestingManager()->AddTestingCreditCard(&expired_card
);
896 ui::MenuModel
* model
= controller()->MenuModelForSection(SECTION_CC
);
897 ASSERT_EQ(4, model
->GetItemCount());
899 ASSERT_TRUE(model
->IsItemCheckedAt(0));
900 EXPECT_FALSE(controller()->IsEditingExistingData(SECTION_CC
));
902 model
->ActivatedAt(1);
903 ASSERT_TRUE(model
->IsItemCheckedAt(1));
904 EXPECT_TRUE(controller()->IsEditingExistingData(SECTION_CC
));
907 // Notifications with long message text should not make the dialog bigger.
908 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, LongNotifications
) {
909 const gfx::Size no_notification_size
= GetViewTester()->GetSize();
910 ASSERT_GT(no_notification_size
.width(), 0);
912 std::vector
<DialogNotification
> notifications
;
913 notifications
.push_back(
914 DialogNotification(DialogNotification::DEVELOPER_WARNING
, ASCIIToUTF16(
915 "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do "
916 "eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim "
917 "ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut "
918 "aliquip ex ea commodo consequat. Duis aute irure dolor in "
919 "reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla "
920 "pariatur. Excepteur sint occaecat cupidatat non proident, sunt in "
921 "culpa qui officia deserunt mollit anim id est laborum.")));
922 controller()->set_notifications(notifications
);
923 controller()->view()->UpdateNotificationArea();
925 EXPECT_EQ(no_notification_size
.width(),
926 GetViewTester()->GetSize().width());
929 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, AutocompleteEvent
) {
930 AutofillDialogControllerImpl
* controller
=
931 SetUpHtmlAndInvoke("<input autocomplete='cc-name'>");
932 ASSERT_TRUE(controller
);
934 AddCreditcardToProfile(controller
->profile(), test::GetVerifiedCreditCard());
935 AddAutofillProfileToProfile(controller
->profile(),
936 test::GetVerifiedProfile());
938 scoped_ptr
<AutofillDialogViewTester
> view
=
939 AutofillDialogViewTester::For(
940 static_cast<TestAutofillDialogController
*>(controller
)->view());
941 view
->SetTextContentsOfSuggestionInput(SECTION_CC
, ASCIIToUTF16("123"));
942 view
->SubmitForTesting();
943 ExpectDomMessage("success");
946 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
947 AutocompleteErrorEventReasonInvalid
) {
948 AutofillDialogControllerImpl
* controller
=
949 SetUpHtmlAndInvoke("<input autocomplete='cc-name' pattern='.*zebra.*'>");
950 ASSERT_TRUE(controller
);
952 const CreditCard
& credit_card
= test::GetVerifiedCreditCard();
954 credit_card
.GetRawInfo(CREDIT_CARD_NAME
).find(ASCIIToUTF16("zebra")) ==
955 base::string16::npos
);
956 AddCreditcardToProfile(controller
->profile(), credit_card
);
957 AddAutofillProfileToProfile(controller
->profile(),
958 test::GetVerifiedProfile());
960 scoped_ptr
<AutofillDialogViewTester
> view
=
961 AutofillDialogViewTester::For(
962 static_cast<TestAutofillDialogController
*>(controller
)->view());
963 view
->SetTextContentsOfSuggestionInput(SECTION_CC
, ASCIIToUTF16("123"));
964 view
->SubmitForTesting();
965 ExpectDomMessage("error: invalid");
968 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
969 AutocompleteErrorEventReasonCancel
) {
970 AutofillDialogControllerImpl
* controller
=
971 SetUpHtmlAndInvoke("<input autocomplete='cc-name'>");
972 ASSERT_TRUE(controller
);
973 AutofillDialogViewTester::For(
974 static_cast<TestAutofillDialogController
*>(controller
)->view())->
976 ExpectDomMessage("error: cancel");
979 // http://crbug.com/318526
980 #if defined(OS_MACOSX)
981 #define MAYBE_ErrorWithFrameNavigation DISABLED_ErrorWithFrameNavigation
983 #define MAYBE_ErrorWithFrameNavigation ErrorWithFrameNavigation
985 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
986 MAYBE_ErrorWithFrameNavigation
) {
987 AutofillDialogControllerImpl
* controller
=
988 SetUpHtmlAndInvoke("<input autocomplete='cc-name'>");
989 ASSERT_TRUE(controller
);
992 ASSERT_TRUE(content::ExecuteScriptAndExtractString(GetRenderViewHost(),
995 ExpectDomMessage("iframe loaded");
997 AutofillDialogViewTester::For(
998 static_cast<TestAutofillDialogController
*>(controller
)->view())->
1000 ExpectDomMessage("error: cancel");
1003 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, NoCvcSegfault
) {
1004 controller()->set_use_validation(true);
1006 CreditCard
credit_card(test::GetVerifiedCreditCard());
1007 controller()->GetTestingManager()->AddTestingCreditCard(&credit_card
);
1008 EXPECT_FALSE(controller()->IsEditingExistingData(SECTION_CC
));
1010 ASSERT_NO_FATAL_FAILURE(GetViewTester()->SubmitForTesting());
1013 // Flaky on Win7, WinXP, and Win Aura. http://crbug.com/270314.
1015 #define MAYBE_PreservedSections DISABLED_PreservedSections
1017 #define MAYBE_PreservedSections PreservedSections
1019 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, MAYBE_PreservedSections
) {
1020 controller()->set_use_validation(true);
1022 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
1023 view
->SetTextContentsOfInput(CREDIT_CARD_NUMBER
,
1024 ASCIIToUTF16("4111111111111111"));
1026 // Create some invalid, manually inputted shipping data.
1027 view
->SetTextContentsOfInput(ADDRESS_HOME_ZIP
, ASCIIToUTF16("shipping zip"));
1029 // Switch to Wallet by simulating a successful server response.
1030 controller()->OnDidFetchWalletCookieValue(std::string());
1031 controller()->OnDidGetWalletItems(
1032 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
));
1033 ASSERT_TRUE(controller()->IsPayingWithWallet());
1035 // The valid data should be preserved.
1036 EXPECT_EQ(ASCIIToUTF16("4111111111111111"),
1037 view
->GetTextContentsOfInput(CREDIT_CARD_NUMBER
));
1039 // The invalid data should be dropped.
1040 EXPECT_TRUE(view
->GetTextContentsOfInput(ADDRESS_HOME_ZIP
).empty());
1042 // Switch back to Autofill.
1043 ui::MenuModel
* account_chooser
= controller()->MenuModelForAccountChooser();
1044 account_chooser
->ActivatedAt(account_chooser
->GetItemCount() - 1);
1045 ASSERT_FALSE(controller()->IsPayingWithWallet());
1047 // The valid data should still be preserved when switched back.
1048 EXPECT_EQ(ASCIIToUTF16("4111111111111111"),
1049 view
->GetTextContentsOfInput(CREDIT_CARD_NUMBER
));
1052 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1053 GeneratedCardLastFourAfterVerifyCvv
) {
1054 controller()->OnDidFetchWalletCookieValue(std::string());
1056 scoped_ptr
<wallet::WalletItems
> wallet_items
=
1057 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
1058 wallet_items
->AddInstrument(wallet::GetTestMaskedInstrument());
1059 wallet_items
->AddAddress(wallet::GetTestShippingAddress());
1061 base::string16 last_four
=
1062 wallet_items
->instruments()[0]->TypeAndLastFourDigits();
1063 controller()->OnDidGetWalletItems(wallet_items
.Pass());
1065 scoped_ptr
<AutofillDialogViewTester
> test_view
= GetViewTester();
1066 EXPECT_FALSE(test_view
->IsShowingOverlay());
1067 EXPECT_CALL(*controller(), LoadRiskFingerprintData());
1068 controller()->OnAccept();
1069 EXPECT_TRUE(test_view
->IsShowingOverlay());
1071 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_
));
1072 scoped_ptr
<risk::Fingerprint
> fingerprint(new risk::Fingerprint());
1073 fingerprint
->mutable_machine_characteristics()->mutable_screen_size()->
1075 controller()->OnDidLoadRiskFingerprintData(fingerprint
.Pass());
1077 controller()->OnDidGetFullWallet(
1078 wallet::GetTestFullWalletWithRequiredActions(
1079 std::vector
<wallet::RequiredAction
>(1, wallet::VERIFY_CVV
)));
1081 ASSERT_TRUE(controller()->IsSubmitPausedOn(wallet::VERIFY_CVV
));
1083 std::string
fake_cvc("123");
1084 test_view
->SetTextContentsOfSuggestionInput(SECTION_CC_BILLING
,
1085 ASCIIToUTF16(fake_cvc
));
1087 EXPECT_FALSE(test_view
->IsShowingOverlay());
1088 EXPECT_CALL(*controller()->GetTestingWalletClient(),
1089 AuthenticateInstrument(_
, fake_cvc
));
1090 controller()->OnAccept();
1091 EXPECT_TRUE(test_view
->IsShowingOverlay());
1093 EXPECT_CALL(metric_logger(),
1094 LogDialogDismissalState(
1095 AutofillMetrics::DIALOG_ACCEPTED_EXISTING_WALLET_DATA
));
1097 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_
));
1098 controller()->OnDidAuthenticateInstrument(true);
1099 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
1100 controller()->ForceFinishSubmit();
1104 EXPECT_EQ(1, test_generated_bubble_controller()->bubbles_shown());
1105 EXPECT_EQ(last_four
, test_generated_bubble_controller()->backing_card_name());
1108 // Simulates the user signing in to the dialog from the inline web contents.
1109 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, SimulateSuccessfulSignIn
) {
1111 // TODO(msw): Fix potential flakiness on Windows XP; http://crbug.com/333641
1112 if (base::win::GetVersion() <= base::win::VERSION_XP
)
1115 browser()->profile()->GetPrefs()->SetBoolean(
1116 ::prefs::kAutofillDialogPayWithoutWallet
,
1119 InitializeController();
1121 controller()->OnDidFetchWalletCookieValue(std::string());
1122 controller()->OnDidGetWalletItems(
1123 wallet::GetTestWalletItemsWithRequiredAction(wallet::GAIA_AUTH
));
1125 NavEntryCommittedObserver
sign_in_page_observer(
1126 controller()->SignInUrl(),
1127 content::NotificationService::AllSources());
1129 // Simulate a user clicking "Sign In" (which loads dialog's web contents).
1130 controller()->SignInLinkClicked();
1131 EXPECT_TRUE(controller()->ShouldShowSignInWebView());
1133 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
1134 content::WebContents
* sign_in_contents
= view
->GetSignInWebContents();
1135 ASSERT_TRUE(sign_in_contents
);
1137 sign_in_page_observer
.Wait();
1139 NavEntryCommittedObserver
continue_page_observer(
1140 wallet::GetSignInContinueUrl(),
1141 content::NotificationService::AllSources());
1143 EXPECT_EQ(sign_in_contents
->GetURL(), controller()->SignInUrl());
1145 AccountChooserModel
* account_chooser_model
=
1146 controller()->AccountChooserModelForTesting();
1147 EXPECT_FALSE(account_chooser_model
->WalletIsSelected());
1149 content::OpenURLParams
params(wallet::GetSignInContinueUrl(),
1150 content::Referrer(),
1152 content::PAGE_TRANSITION_LINK
,
1155 sign_in_contents
->GetDelegate()->OpenURLFromTab(sign_in_contents
, params
);
1157 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems());
1158 continue_page_observer
.Wait();
1159 content::RunAllPendingInMessageLoop();
1161 EXPECT_FALSE(controller()->ShouldShowSignInWebView());
1163 scoped_ptr
<wallet::WalletItems
> wallet_items
=
1164 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
1165 wallet_items
->AddInstrument(wallet::GetTestMaskedInstrument());
1166 controller()->OnDidGetWalletItems(wallet_items
.Pass());
1168 // Wallet should now be selected and Chrome shouldn't have crashed (which can
1169 // happen if the WebContents is deleted while proccessing a nav entry commit).
1170 EXPECT_TRUE(account_chooser_model
->WalletIsSelected());
1171 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_CC_BILLING
));
1172 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_SHIPPING
));
1174 EXPECT_CALL(metric_logger(),
1175 LogDialogDismissalState(
1176 AutofillMetrics::DIALOG_ACCEPTED_EXISTING_WALLET_DATA
));
1177 view
->SubmitForTesting();
1178 controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
1179 controller()->ForceFinishSubmit();
1182 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, AddAccount
) {
1184 // TODO(msw): Fix potential flakiness on Windows XP; http://crbug.com/333641
1185 if (base::win::GetVersion() <= base::win::VERSION_XP
)
1189 controller()->OnDidFetchWalletCookieValue(std::string());
1190 std::vector
<std::string
> usernames
;
1191 usernames
.push_back("user_0@example.com");
1192 controller()->OnDidGetWalletItems(
1193 wallet::GetTestWalletItemsWithUsers(usernames
, 0));
1195 // Switch to Autofill.
1196 AccountChooserModel
* account_chooser_model
=
1197 controller()->AccountChooserModelForTesting();
1198 account_chooser_model
->ActivatedAt(
1199 account_chooser_model
->GetItemCount() - 1);
1201 NavEntryCommittedObserver
sign_in_page_observer(
1202 controller()->SignInUrl(),
1203 content::NotificationService::AllSources());
1205 // Simulate a user clicking "add account".
1206 account_chooser_model
->ActivatedAt(
1207 account_chooser_model
->GetItemCount() - 2);
1208 EXPECT_TRUE(controller()->ShouldShowSignInWebView());
1210 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
1211 content::WebContents
* sign_in_contents
= view
->GetSignInWebContents();
1212 ASSERT_TRUE(sign_in_contents
);
1214 sign_in_page_observer
.Wait();
1216 NavEntryCommittedObserver
continue_page_observer(
1217 wallet::GetSignInContinueUrl(),
1218 content::NotificationService::AllSources());
1220 EXPECT_EQ(sign_in_contents
->GetURL(), controller()->SignInUrl());
1222 EXPECT_FALSE(account_chooser_model
->WalletIsSelected());
1224 // User signs into new account, account 3.
1225 controller()->set_sign_in_user_index(3U);
1226 content::OpenURLParams
params(wallet::GetSignInContinueUrl(),
1227 content::Referrer(),
1229 content::PAGE_TRANSITION_LINK
,
1231 sign_in_contents
->GetDelegate()->OpenURLFromTab(sign_in_contents
, params
);
1233 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems());
1234 continue_page_observer
.Wait();
1235 content::RunAllPendingInMessageLoop();
1237 EXPECT_FALSE(controller()->ShouldShowSignInWebView());
1238 EXPECT_EQ(3U, controller()->GetTestingWalletClient()->user_index());
1240 usernames
.push_back("user_1@example.com");
1241 usernames
.push_back("user_2@example.com");
1242 usernames
.push_back("user_3@example.com");
1243 usernames
.push_back("user_4@example.com");
1244 // Curveball: wallet items comes back with user 4 selected.
1245 controller()->OnDidGetWalletItems(
1246 wallet::GetTestWalletItemsWithUsers(usernames
, 4U));
1248 EXPECT_TRUE(account_chooser_model
->WalletIsSelected());
1249 EXPECT_EQ(4U, account_chooser_model
->GetActiveWalletAccountIndex());
1250 EXPECT_EQ(4U, controller()->GetTestingWalletClient()->user_index());
1253 // Verify that filling a form works correctly, including filling the CVC when
1254 // that is requested separately.
1255 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1256 FillFormIncludesCVC
) {
1257 AutofillDialogControllerImpl
* controller
=
1258 SetUpHtmlAndInvoke("<input autocomplete='cc-csc'>");
1259 ASSERT_TRUE(controller
);
1261 AddCreditcardToProfile(controller
->profile(), test::GetVerifiedCreditCard());
1262 AddAutofillProfileToProfile(controller
->profile(),
1263 test::GetVerifiedProfile());
1265 scoped_ptr
<AutofillDialogViewTester
> view
=
1266 AutofillDialogViewTester::For(
1267 static_cast<TestAutofillDialogController
*>(controller
)->view());
1268 view
->SetTextContentsOfSuggestionInput(SECTION_CC
, ASCIIToUTF16("123"));
1269 view
->SubmitForTesting();
1270 ExpectDomMessage("success");
1271 EXPECT_EQ("123", GetValueForHTMLFieldOfType("cc-csc"));
1274 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, AddNewClearsComboboxes
) {
1275 // Ensure the input under test is a combobox.
1277 controller()->ComboboxModelForAutofillType(CREDIT_CARD_EXP_MONTH
));
1279 // Set up an expired card.
1281 test::SetCreditCardInfo(&card
, "Roy Demeo", "4111111111111111", "8", "2013");
1282 card
.set_origin("Chrome settings");
1283 ASSERT_TRUE(card
.IsVerified());
1285 // Add the card and check that there's a menu for that section.
1286 controller()->GetTestingManager()->AddTestingCreditCard(&card
);
1287 ASSERT_TRUE(controller()->MenuModelForSection(SECTION_CC
));
1289 // Select the invalid, suggested card from the menu.
1290 controller()->MenuModelForSection(SECTION_CC
)->ActivatedAt(0);
1291 EXPECT_TRUE(controller()->IsEditingExistingData(SECTION_CC
));
1293 // Get the contents of the combobox of the credit card's expiration month.
1294 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
1295 base::string16 cc_exp_month_text
=
1296 view
->GetTextContentsOfInput(CREDIT_CARD_EXP_MONTH
);
1298 // Select "New X..." from the suggestion menu to clear the section's inputs.
1299 controller()->MenuModelForSection(SECTION_CC
)->ActivatedAt(1);
1300 EXPECT_FALSE(controller()->IsEditingExistingData(SECTION_CC
));
1302 // Ensure that the credit card expiration month has changed.
1303 EXPECT_NE(cc_exp_month_text
,
1304 view
->GetTextContentsOfInput(CREDIT_CARD_EXP_MONTH
));
1307 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, TabOpensToJustRight
) {
1308 ASSERT_TRUE(browser()->is_type_tabbed());
1310 // Tabs should currently be: / rAc() \.
1311 content::WebContents
* dialog_invoker
= controller()->GetWebContents();
1312 EXPECT_EQ(dialog_invoker
, GetActiveWebContents());
1314 TabStripModel
* tab_strip
= browser()->tab_strip_model();
1315 ASSERT_EQ(1, tab_strip
->count());
1316 EXPECT_EQ(0, tab_strip
->GetIndexOfWebContents(dialog_invoker
));
1318 // Open a tab to about:blank in the background at the end of the tab strip.
1319 chrome::AddTabAt(browser(), GURL(), -1, false);
1320 // Tabs should now be: / rAc() \/ blank \.
1321 EXPECT_EQ(2, tab_strip
->count());
1322 EXPECT_EQ(0, tab_strip
->active_index());
1323 EXPECT_EQ(dialog_invoker
, GetActiveWebContents());
1325 content::WebContents
* blank_tab
= tab_strip
->GetWebContentsAt(1);
1327 // Simulate clicking "Manage X...".
1328 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(2);
1329 // Tab should now be: / rAc() \/ manage 1 \/ blank \.
1330 EXPECT_EQ(3, tab_strip
->count());
1331 int dialog_index
= tab_strip
->GetIndexOfWebContents(dialog_invoker
);
1332 EXPECT_EQ(0, dialog_index
);
1333 EXPECT_EQ(1, tab_strip
->active_index());
1334 EXPECT_EQ(2, tab_strip
->GetIndexOfWebContents(blank_tab
));
1336 content::WebContents
* first_manage_tab
= tab_strip
->GetWebContentsAt(1);
1338 // Re-activate the dialog's tab (like a user would have to).
1339 tab_strip
->ActivateTabAt(dialog_index
, true);
1340 EXPECT_EQ(dialog_invoker
, GetActiveWebContents());
1342 // Simulate clicking "Manage X...".
1343 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(2);
1344 // Tabs should now be: / rAc() \/ manage 2 \/ manage 1 \/ blank \.
1345 EXPECT_EQ(4, tab_strip
->count());
1346 EXPECT_EQ(0, tab_strip
->GetIndexOfWebContents(dialog_invoker
));
1347 EXPECT_EQ(1, tab_strip
->active_index());
1348 EXPECT_EQ(2, tab_strip
->GetIndexOfWebContents(first_manage_tab
));
1349 EXPECT_EQ(3, tab_strip
->GetIndexOfWebContents(blank_tab
));
1352 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1353 SignInWebViewOpensLinksInNewTab
) {
1354 controller()->OnDidFetchWalletCookieValue(std::string());
1355 controller()->OnDidGetWalletItems(
1356 wallet::GetTestWalletItemsWithRequiredAction(wallet::GAIA_AUTH
));
1358 NavEntryCommittedObserver
sign_in_page_observer(
1359 controller()->SignInUrl(),
1360 content::NotificationService::AllSources());
1362 controller()->SignInLinkClicked();
1364 content::WebContents
* sign_in_contents
=
1365 GetViewTester()->GetSignInWebContents();
1366 ASSERT_TRUE(sign_in_contents
);
1368 sign_in_page_observer
.Wait();
1370 content::OpenURLParams
params(GURL("http://google.com"),
1371 content::Referrer(),
1373 content::PAGE_TRANSITION_LINK
,
1375 params
.user_gesture
= true;
1377 int num_tabs
= browser()->tab_strip_model()->count();
1378 sign_in_contents
->GetDelegate()->OpenURLFromTab(sign_in_contents
, params
);
1379 EXPECT_EQ(num_tabs
+ 1, browser()->tab_strip_model()->count());
1382 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, RefreshOnManageTabClose
) {
1383 ASSERT_TRUE(browser()->is_type_tabbed());
1385 // GetWalletItems() is called when controller() is created in SetUp().
1386 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems());
1387 controller()->OnDidFetchWalletCookieValue(std::string());
1388 controller()->OnDidGetWalletItems(
1389 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
));
1391 content::WebContents
* dialog_invoker
= controller()->GetWebContents();
1392 TabAutofillManagerDelegate::FromWebContents(dialog_invoker
)->
1393 SetDialogControllerForTesting(controller()->AsWeakPtr());
1395 // Open a new tab by selecting "Manage my shipping details..." in Wallet mode.
1396 EXPECT_EQ(1, browser()->tab_strip_model()->count());
1397 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(2);
1398 EXPECT_EQ(2, browser()->tab_strip_model()->count());
1399 ASSERT_NE(dialog_invoker
, GetActiveWebContents());
1401 // Closing the tab opened by "Manage my shipping details..." should refresh
1403 controller()->ClearLastWalletItemsFetchTimestampForTesting();
1404 EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems());
1405 GetActiveWebContents()->Close();
1408 // Changes from Wallet to Autofill and verifies that the combined billing/cc
1409 // sections are showing (or not) at the correct times.
1410 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1411 ChangingDataSourceShowsCorrectSections
) {
1412 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
1413 EXPECT_TRUE(view
->IsShowingSection(SECTION_CC
));
1414 EXPECT_TRUE(view
->IsShowingSection(SECTION_BILLING
));
1415 EXPECT_FALSE(view
->IsShowingSection(SECTION_CC_BILLING
));
1417 // Switch the dialog to paying with Wallet.
1418 controller()->OnDidFetchWalletCookieValue(std::string());
1419 controller()->OnDidGetWalletItems(
1420 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
));
1421 ASSERT_TRUE(controller()->IsPayingWithWallet());
1423 EXPECT_FALSE(view
->IsShowingSection(SECTION_CC
));
1424 EXPECT_FALSE(view
->IsShowingSection(SECTION_BILLING
));
1425 EXPECT_TRUE(view
->IsShowingSection(SECTION_CC_BILLING
));
1427 // Now switch back to Autofill to ensure this direction works as well.
1428 ui::MenuModel
* account_chooser
= controller()->MenuModelForAccountChooser();
1429 account_chooser
->ActivatedAt(account_chooser
->GetItemCount() - 1);
1431 EXPECT_TRUE(view
->IsShowingSection(SECTION_CC
));
1432 EXPECT_TRUE(view
->IsShowingSection(SECTION_BILLING
));
1433 EXPECT_FALSE(view
->IsShowingSection(SECTION_CC_BILLING
));
1436 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1437 DoesWorkOnHttpWithFlag
) {
1438 net::SpawnedTestServer
http_server(
1439 net::SpawnedTestServer::TYPE_HTTP
,
1440 net::SpawnedTestServer::kLocalhost
,
1441 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
1442 ASSERT_TRUE(http_server
.Start());
1443 EXPECT_TRUE(RunTestPage(http_server
));
1446 // Like the parent test, but doesn't add the --reduce-security-for-testing flag.
1447 class AutofillDialogControllerSecurityTest
:
1448 public AutofillDialogControllerTest
{
1450 AutofillDialogControllerSecurityTest() {}
1451 virtual ~AutofillDialogControllerSecurityTest() {}
1453 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
1454 CHECK(!command_line
->HasSwitch(::switches::kReduceSecurityForTesting
));
1457 typedef net::BaseTestServer::SSLOptions SSLOptions
;
1460 DISALLOW_COPY_AND_ASSIGN(AutofillDialogControllerSecurityTest
);
1463 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerSecurityTest
,
1465 net::SpawnedTestServer
http_server(
1466 net::SpawnedTestServer::TYPE_HTTP
,
1467 net::SpawnedTestServer::kLocalhost
,
1468 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
1469 ASSERT_TRUE(http_server
.Start());
1470 EXPECT_FALSE(RunTestPage(http_server
));
1473 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerSecurityTest
,
1474 DoesWorkOnHttpWithFlags
) {
1475 net::SpawnedTestServer
https_server(
1476 net::SpawnedTestServer::TYPE_HTTPS
,
1477 SSLOptions(SSLOptions::CERT_OK
),
1478 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
1479 ASSERT_TRUE(https_server
.Start());
1480 EXPECT_TRUE(RunTestPage(https_server
));
1483 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerSecurityTest
,
1484 DoesntWorkOnBrokenHttps
) {
1485 net::SpawnedTestServer
https_server(
1486 net::SpawnedTestServer::TYPE_HTTPS
,
1487 SSLOptions(SSLOptions::CERT_EXPIRED
),
1488 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
1489 ASSERT_TRUE(https_server
.Start());
1490 EXPECT_FALSE(RunTestPage(https_server
));
1493 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1494 CountryChangeRebuildsSection
) {
1495 EXPECT_FALSE(SectionHasField(SECTION_BILLING
, ADDRESS_BILLING_SORTING_CODE
));
1496 EXPECT_FALSE(SectionHasField(SECTION_SHIPPING
, ADDRESS_HOME_SORTING_CODE
));
1498 // Select "Add new shipping address...".
1499 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(1);
1501 // Add some valid user input that should be preserved when country changes.
1502 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
1503 view
->SetTextContentsOfInput(NAME_FULL
, ASCIIToUTF16("B. Loblaw"));
1505 // Change both sections' countries.
1506 view
->SetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
, ASCIIToUTF16("France"));
1507 view
->ActivateInput(ADDRESS_BILLING_COUNTRY
);
1508 view
->SetTextContentsOfInput(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("Belarus"));
1509 view
->ActivateInput(ADDRESS_HOME_COUNTRY
);
1511 // Verify the name is still there.
1512 EXPECT_EQ(ASCIIToUTF16("B. Loblaw"), view
->GetTextContentsOfInput(NAME_FULL
));
1514 EXPECT_TRUE(SectionHasField(SECTION_BILLING
, ADDRESS_BILLING_SORTING_CODE
));
1515 EXPECT_TRUE(SectionHasField(SECTION_SHIPPING
, ADDRESS_HOME_SORTING_CODE
));
1518 // Changing the data source to or from Wallet preserves the shipping country,
1519 // but not the billing country because Wallet only supports US billing
1521 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1522 ChangingDataSourcePreservesCountry
) {
1523 InitializeControllerWithoutShowing();
1524 controller()->GetTestingManager()->set_default_country_code("CA");
1525 controller()->Show();
1528 AutofillProfile
verified_profile(test::GetVerifiedProfile());
1529 controller()->GetTestingManager()->AddTestingProfile(&verified_profile
);
1531 CreditCard
verified_card(test::GetVerifiedCreditCard());
1532 controller()->GetTestingManager()->AddTestingCreditCard(&verified_card
);
1533 EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_SHIPPING
));
1535 controller()->OnDidFetchWalletCookieValue(std::string());
1536 scoped_ptr
<wallet::WalletItems
> items
=
1537 wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED
);
1538 items
->AddAccount(wallet::GetTestGaiaAccount());
1539 items
->AddInstrument(wallet::GetTestMaskedInstrument());
1540 items
->AddAddress(wallet::GetTestShippingAddress());
1541 controller()->OnDidGetWalletItems(items
.Pass());
1543 EXPECT_TRUE(controller()->IsPayingWithWallet());
1545 // Select "Add new shipping address...".
1546 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(2);
1547 EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_SHIPPING
));
1549 // Default shipping country matches PDM's default, but default billing is
1550 // always US in Wallet mode.
1551 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
1552 ASSERT_EQ(ASCIIToUTF16("Canada"),
1553 view
->GetTextContentsOfInput(ADDRESS_HOME_COUNTRY
));
1554 ASSERT_EQ(ASCIIToUTF16("United States"),
1555 view
->GetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
));
1557 // Switch the shipping country.
1558 view
->SetTextContentsOfInput(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("Belarus"));
1559 EXPECT_EQ(ASCIIToUTF16("Belarus"),
1560 view
->GetTextContentsOfInput(ADDRESS_HOME_COUNTRY
));
1561 view
->ActivateInput(ADDRESS_HOME_COUNTRY
);
1563 // Switch to using Autofill instead of Wallet.
1564 ui::MenuModel
* account_chooser
= controller()->MenuModelForAccountChooser();
1565 account_chooser
->ActivatedAt(account_chooser
->GetItemCount() - 1);
1567 EXPECT_FALSE(controller()->IsPayingWithWallet());
1569 // Shipping country should have stayed the same.
1570 EXPECT_EQ(ASCIIToUTF16("Belarus"),
1571 view
->GetTextContentsOfInput(ADDRESS_HOME_COUNTRY
));
1572 ASSERT_TRUE(SectionHasField(SECTION_SHIPPING
, ADDRESS_HOME_SORTING_CODE
));
1574 controller()->MenuModelForSection(SECTION_BILLING
)->ActivatedAt(1);
1575 view
->SetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
,
1576 ASCIIToUTF16("Belarus"));
1577 view
->ActivateInput(ADDRESS_BILLING_COUNTRY
);
1578 EXPECT_EQ(ASCIIToUTF16("Belarus"),
1579 view
->GetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
));
1580 ASSERT_TRUE(SectionHasField(SECTION_BILLING
, ADDRESS_BILLING_SORTING_CODE
));
1582 // Switch back to Wallet. Country should go back to US.
1583 account_chooser
->ActivatedAt(0);
1584 EXPECT_EQ(ASCIIToUTF16("United States"),
1585 view
->GetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
));
1587 SectionHasField(SECTION_CC_BILLING
, ADDRESS_BILLING_SORTING_CODE
));
1589 // Make sure shipping is still on Belarus.
1590 EXPECT_EQ(ASCIIToUTF16("Belarus"),
1591 view
->GetTextContentsOfInput(ADDRESS_HOME_COUNTRY
));
1592 ASSERT_TRUE(SectionHasField(SECTION_SHIPPING
, ADDRESS_HOME_SORTING_CODE
));
1595 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, AddNewResetsCountry
) {
1596 AutofillProfile
verified_profile(test::GetVerifiedProfile());
1597 controller()->GetTestingManager()->AddTestingProfile(&verified_profile
);
1599 // Select "Add new billing/shipping address...".
1600 controller()->MenuModelForSection(SECTION_BILLING
)->ActivatedAt(1);
1601 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(2);
1603 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
1604 ASSERT_EQ(ASCIIToUTF16("United States"),
1605 view
->GetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
));
1606 ASSERT_EQ(ASCIIToUTF16("United States"),
1607 view
->GetTextContentsOfInput(ADDRESS_HOME_COUNTRY
));
1609 // Switch both billing and shipping countries.
1610 view
->SetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
, ASCIIToUTF16("Brazil"));
1611 view
->ActivateInput(ADDRESS_BILLING_COUNTRY
);
1612 view
->SetTextContentsOfInput(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("France"));
1613 view
->ActivateInput(ADDRESS_HOME_COUNTRY
);
1615 // Select "Add new billing/shipping address...".
1616 controller()->MenuModelForSection(SECTION_BILLING
)->ActivatedAt(1);
1617 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(2);
1619 EXPECT_EQ(ASCIIToUTF16("United States"),
1620 view
->GetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
));
1621 EXPECT_EQ(ASCIIToUTF16("United States"),
1622 view
->GetTextContentsOfInput(ADDRESS_HOME_COUNTRY
));
1625 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1626 FillingFormRebuildsInputs
) {
1627 AutofillProfile
full_profile(test::GetFullProfile());
1628 full_profile
.SetRawInfo(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("DE"));
1629 controller()->GetTestingManager()->AddTestingProfile(&full_profile
);
1631 // Select "Add new shipping address...".
1632 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(1);
1634 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
1635 ASSERT_EQ(ASCIIToUTF16("United States"),
1636 view
->GetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
));
1637 ASSERT_EQ(ASCIIToUTF16("United States"),
1638 view
->GetTextContentsOfInput(ADDRESS_HOME_COUNTRY
));
1640 base::string16 name
= full_profile
.GetRawInfo(NAME_FULL
);
1641 view
->SetTextContentsOfInput(NAME_FULL
, name
.substr(0, name
.size() / 2));
1642 view
->ActivateInput(NAME_FULL
);
1643 ASSERT_EQ(NAME_FULL
, controller()->popup_input_type());
1644 controller()->DidAcceptSuggestion(base::string16(), 0);
1646 EXPECT_EQ(ASCIIToUTF16("Germany"),
1647 view
->GetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
));
1648 EXPECT_EQ(ASCIIToUTF16("Germany"),
1649 view
->GetTextContentsOfInput(ADDRESS_HOME_COUNTRY
));
1652 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
,
1653 FillingFormPreservesChangedCountry
) {
1654 AutofillProfile
full_profile(test::GetFullProfile());
1655 full_profile
.SetRawInfo(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("DE"));
1656 controller()->GetTestingManager()->AddTestingProfile(&full_profile
);
1658 // Select "Add new shipping address...".
1659 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(1);
1661 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
1662 view
->SetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
, ASCIIToUTF16("France"));
1663 view
->ActivateInput(ADDRESS_BILLING_COUNTRY
);
1664 view
->SetTextContentsOfInput(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("Belarus"));
1665 view
->ActivateInput(ADDRESS_HOME_COUNTRY
);
1667 base::string16 name
= full_profile
.GetRawInfo(NAME_FULL
);
1668 view
->SetTextContentsOfInput(NAME_FULL
, name
.substr(0, name
.size() / 2));
1669 view
->ActivateInput(NAME_FULL
);
1670 ASSERT_EQ(NAME_FULL
, controller()->popup_input_type());
1671 controller()->DidAcceptSuggestion(base::string16(), 0);
1673 EXPECT_EQ(ASCIIToUTF16("France"),
1674 view
->GetTextContentsOfInput(ADDRESS_BILLING_COUNTRY
));
1675 EXPECT_EQ(ASCIIToUTF16("Belarus"),
1676 view
->GetTextContentsOfInput(ADDRESS_HOME_COUNTRY
));
1679 IN_PROC_BROWSER_TEST_F(AutofillDialogControllerTest
, RulesLoaded
) {
1680 // Select "Add new shipping address...".
1681 controller()->MenuModelForSection(SECTION_SHIPPING
)->ActivatedAt(1);
1682 controller()->set_use_validation(true);
1684 EXPECT_CALL(*controller()->GetMockValidator(),
1685 ValidateAddress(CountryCodeMatcher("DE"), _
, _
)).Times(2).
1686 WillOnce(Return(AddressValidator::RULES_NOT_READY
));
1688 // Validation should occur on country change and see the rules haven't loaded.
1689 scoped_ptr
<AutofillDialogViewTester
> view
= GetViewTester();
1690 view
->SetTextContentsOfInput(ADDRESS_HOME_ZIP
, ASCIIToUTF16("123"));
1691 view
->SetTextContentsOfInput(ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("Germany"));
1692 view
->ActivateInput(ADDRESS_HOME_COUNTRY
);
1694 // Different country loaded, validation should not occur.
1695 controller()->OnAddressValidationRulesLoaded("FR", true);
1697 // Relevant country loaded, validation should occur.
1698 controller()->OnAddressValidationRulesLoaded("DE", true);
1700 // Relevant country loaded but revalidation already happened, no further
1701 // validation should occur.
1702 controller()->OnAddressValidationRulesLoaded("DE", false);
1704 // Cancelling the dialog causes additional validation to see if the user
1705 // cancelled with invalid fields, so verify and clear here.
1706 testing::Mock::VerifyAndClearExpectations(controller()->GetMockValidator());
1709 } // namespace autofill