Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(285)

Side by Side Diff: chrome/browser/ui/autofill/autofill_dialog_controller_unittest.cc

Issue 145553009: rAc: use libaddressinput to validate international addresses. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: win compile? Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <map> 5 #include <map>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback.h"
8 #include "base/command_line.h" 10 #include "base/command_line.h"
9 #include "base/guid.h" 11 #include "base/guid.h"
10 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
12 #include "base/prefs/pref_service.h" 14 #include "base/prefs/pref_service.h"
13 #include "base/run_loop.h" 15 #include "base/run_loop.h"
14 #include "base/strings/string_number_conversions.h" 16 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_piece.h" 17 #include "base/strings/string_piece.h"
16 #include "base/strings/utf_string_conversions.h" 18 #include "base/strings/utf_string_conversions.h"
17 #include "base/tuple.h" 19 #include "base/tuple.h"
(...skipping 23 matching lines...) Expand all
41 #include "components/autofill/core/browser/autofill_test_utils.h" 43 #include "components/autofill/core/browser/autofill_test_utils.h"
42 #include "components/autofill/core/browser/test_personal_data_manager.h" 44 #include "components/autofill/core/browser/test_personal_data_manager.h"
43 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" 45 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
44 #include "components/autofill/core/common/autofill_switches.h" 46 #include "components/autofill/core/common/autofill_switches.h"
45 #include "components/autofill/core/common/form_data.h" 47 #include "components/autofill/core/common/form_data.h"
46 #include "components/user_prefs/user_prefs.h" 48 #include "components/user_prefs/user_prefs.h"
47 #include "content/public/browser/web_contents.h" 49 #include "content/public/browser/web_contents.h"
48 #include "content/public/test/mock_render_process_host.h" 50 #include "content/public/test/mock_render_process_host.h"
49 #include "google_apis/gaia/google_service_auth_error.h" 51 #include "google_apis/gaia/google_service_auth_error.h"
50 #include "grit/webkit_resources.h" 52 #include "grit/webkit_resources.h"
53 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/addre ss_data.h"
54 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/addre ss_validator.h"
51 #include "testing/gmock/include/gmock/gmock.h" 55 #include "testing/gmock/include/gmock/gmock.h"
52 #include "testing/gtest/include/gtest/gtest.h" 56 #include "testing/gtest/include/gtest/gtest.h"
53 #include "ui/base/resource/resource_bundle.h" 57 #include "ui/base/resource/resource_bundle.h"
54 58
55 #if defined(OS_WIN) 59 #if defined(OS_WIN)
56 #include "ui/base/win/scoped_ole_initializer.h" 60 #include "ui/base/win/scoped_ole_initializer.h"
57 #endif 61 #endif
58 62
59 using base::ASCIIToUTF16; 63 using base::ASCIIToUTF16;
60 using base::UTF8ToUTF16; 64 using base::UTF8ToUTF16;
61 65
62 namespace autofill { 66 namespace autofill {
63 67
64 namespace { 68 namespace {
65 69
70 using ::i18n::addressinput::AddressData;
71 using ::i18n::addressinput::AddressProblemFilter;
72 using ::i18n::addressinput::AddressProblems;
73 using ::i18n::addressinput::AddressValidator;
74 using testing::AtLeast;
75 using testing::Return;
66 using testing::_; 76 using testing::_;
67 77
68 const char kFakeEmail[] = "user@chromium.org"; 78 const char kFakeEmail[] = "user@chromium.org";
69 const char kFakeFingerprintEncoded[] = "CgVaAwiACA=="; 79 const char kFakeFingerprintEncoded[] = "CgVaAwiACA==";
70 const char kEditedBillingAddress[] = "123 edited billing address"; 80 const char kEditedBillingAddress[] = "123 edited billing address";
71 const char* kFieldsFromPage[] = 81 const char* kFieldsFromPage[] =
72 { "email", 82 { "email",
73 "cc-name", 83 "cc-name",
74 "cc-number", 84 "cc-number",
75 "cc-exp-month", 85 "cc-exp-month",
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 private: 236 private:
227 std::map<DialogSection, FieldValueMap> outputs_; 237 std::map<DialogSection, FieldValueMap> outputs_;
228 std::map<DialogSection, size_t> section_updates_; 238 std::map<DialogSection, size_t> section_updates_;
229 239
230 int updates_started_; 240 int updates_started_;
231 bool save_details_locally_checked_; 241 bool save_details_locally_checked_;
232 242
233 DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogView); 243 DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogView);
234 }; 244 };
235 245
246 class MockAddressValidator : public AddressValidator {
247 public:
248 MOCK_METHOD1(LoadRules, void(const std::string& country_code));
249 MOCK_CONST_METHOD3(ValidateAddress,
250 ::i18n::addressinput::AddressValidator::Status(
251 const AddressData& address,
252 const AddressProblemFilter& filter,
253 AddressProblems* problems));
254 using ::i18n::addressinput::AddressValidator::RULES_NOT_READY;
255 };
256
236 class TestAutofillDialogController 257 class TestAutofillDialogController
237 : public AutofillDialogControllerImpl, 258 : public AutofillDialogControllerImpl,
238 public base::SupportsWeakPtr<TestAutofillDialogController> { 259 public base::SupportsWeakPtr<TestAutofillDialogController> {
239 public: 260 public:
240 TestAutofillDialogController( 261 TestAutofillDialogController(
241 content::WebContents* contents, 262 content::WebContents* contents,
242 const FormData& form_structure, 263 const FormData& form_structure,
243 const GURL& source_url, 264 const GURL& source_url,
244 const AutofillMetrics& metric_logger, 265 const AutofillMetrics& metric_logger,
245 const base::Callback<void(const FormStructure*)>& callback, 266 const base::Callback<void(const FormStructure*)>& callback,
267 MockAddressValidator* mock_validator,
246 MockNewCreditCardBubbleController* mock_new_card_bubble_controller) 268 MockNewCreditCardBubbleController* mock_new_card_bubble_controller)
247 : AutofillDialogControllerImpl(contents, 269 : AutofillDialogControllerImpl(contents,
248 form_structure, 270 form_structure,
249 source_url, 271 source_url,
250 callback), 272 callback),
251 metric_logger_(metric_logger), 273 metric_logger_(metric_logger),
252 mock_wallet_client_( 274 mock_wallet_client_(
253 Profile::FromBrowserContext(contents->GetBrowserContext())-> 275 Profile::FromBrowserContext(contents->GetBrowserContext())->
254 GetRequestContext(), this, source_url), 276 GetRequestContext(), this, source_url),
277 mock_validator_(mock_validator),
255 mock_new_card_bubble_controller_(mock_new_card_bubble_controller), 278 mock_new_card_bubble_controller_(mock_new_card_bubble_controller),
256 submit_button_delay_count_(0) {} 279 submit_button_delay_count_(0) {}
257 280
258 virtual ~TestAutofillDialogController() {} 281 virtual ~TestAutofillDialogController() {}
259 282
260 virtual AutofillDialogView* CreateView() OVERRIDE { 283 virtual AutofillDialogView* CreateView() OVERRIDE {
261 return new testing::NiceMock<TestAutofillDialogView>(); 284 return new testing::NiceMock<TestAutofillDialogView>();
262 } 285 }
263 286
264 void Init(content::BrowserContext* browser_context) { 287 void Init(content::BrowserContext* browser_context) {
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
321 protected: 344 protected:
322 virtual PersonalDataManager* GetManager() const OVERRIDE { 345 virtual PersonalDataManager* GetManager() const OVERRIDE {
323 return const_cast<TestAutofillDialogController*>(this)-> 346 return const_cast<TestAutofillDialogController*>(this)->
324 GetTestingManager(); 347 GetTestingManager();
325 } 348 }
326 349
327 virtual wallet::WalletClient* GetWalletClient() OVERRIDE { 350 virtual wallet::WalletClient* GetWalletClient() OVERRIDE {
328 return &mock_wallet_client_; 351 return &mock_wallet_client_;
329 } 352 }
330 353
354 virtual AddressValidator* GetValidator() {
355 return mock_validator_;
356 }
357
331 virtual void OpenTabWithUrl(const GURL& url) OVERRIDE { 358 virtual void OpenTabWithUrl(const GURL& url) OVERRIDE {
332 open_tab_url_ = url; 359 open_tab_url_ = url;
333 } 360 }
334 361
335 virtual void ShowNewCreditCardBubble( 362 virtual void ShowNewCreditCardBubble(
336 scoped_ptr<CreditCard> new_card, 363 scoped_ptr<CreditCard> new_card,
337 scoped_ptr<AutofillProfile> billing_profile) OVERRIDE { 364 scoped_ptr<AutofillProfile> billing_profile) OVERRIDE {
338 mock_new_card_bubble_controller_->Show(new_card.Pass(), 365 mock_new_card_bubble_controller_->Show(new_card.Pass(),
339 billing_profile.Pass()); 366 billing_profile.Pass());
340 } 367 }
341 368
342 // AutofillDialogControllerImpl calls this method before showing the dialog 369 // AutofillDialogControllerImpl calls this method before showing the dialog
343 // window. 370 // window.
344 virtual void SubmitButtonDelayBegin() OVERRIDE { 371 virtual void SubmitButtonDelayBegin() OVERRIDE {
345 // Do not delay enabling the submit button in testing. 372 // Do not delay enabling the submit button in testing.
346 submit_button_delay_count_++; 373 submit_button_delay_count_++;
347 } 374 }
348 375
349 private: 376 private:
350 // To specify our own metric logger. 377 // To specify our own metric logger.
351 virtual const AutofillMetrics& GetMetricLogger() const OVERRIDE { 378 virtual const AutofillMetrics& GetMetricLogger() const OVERRIDE {
352 return metric_logger_; 379 return metric_logger_;
353 } 380 }
354 381
355 const AutofillMetrics& metric_logger_; 382 const AutofillMetrics& metric_logger_;
356 TestPersonalDataManager test_manager_; 383 TestPersonalDataManager test_manager_;
357 testing::NiceMock<wallet::MockWalletClient> mock_wallet_client_; 384 testing::NiceMock<wallet::MockWalletClient> mock_wallet_client_;
385 MockAddressValidator* mock_validator_;
358 GURL open_tab_url_; 386 GURL open_tab_url_;
359 MockNewCreditCardBubbleController* mock_new_card_bubble_controller_; 387 MockNewCreditCardBubbleController* mock_new_card_bubble_controller_;
360 388
361 // The number of times that the submit button was delayed. 389 // The number of times that the submit button was delayed.
362 int submit_button_delay_count_; 390 int submit_button_delay_count_;
363 391
364 DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogController); 392 DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogController);
365 }; 393 };
366 394
367 class AutofillDialogControllerTest : public ChromeRenderViewHostTestHarness { 395 class AutofillDialogControllerTest : public ChromeRenderViewHostTestHarness {
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
419 447
420 base::Callback<void(const FormStructure*)> callback = 448 base::Callback<void(const FormStructure*)> callback =
421 base::Bind(&AutofillDialogControllerTest::FinishedCallback, 449 base::Bind(&AutofillDialogControllerTest::FinishedCallback,
422 base::Unretained(this)); 450 base::Unretained(this));
423 controller_ = (new testing::NiceMock<TestAutofillDialogController>( 451 controller_ = (new testing::NiceMock<TestAutofillDialogController>(
424 web_contents(), 452 web_contents(),
425 form_data, 453 form_data,
426 GURL(), 454 GURL(),
427 metric_logger_, 455 metric_logger_,
428 callback, 456 callback,
457 mock_validator_.get(),
429 mock_new_card_bubble_controller_.get()))->AsWeakPtr(); 458 mock_new_card_bubble_controller_.get()))->AsWeakPtr();
430 controller_->Init(profile()); 459 controller_->Init(profile());
431 } 460 }
432 461
433 // Creates a new controller for |form_data| and sets up some initial wallet 462 // Creates a new controller for |form_data| and sets up some initial wallet
434 // data for it. 463 // data for it.
435 void SetUpControllerWithFormData(const FormData& form_data) { 464 void SetUpControllerWithFormData(const FormData& form_data) {
436 ResetControllerWithFormData(form_data); 465 ResetControllerWithFormData(form_data);
437 controller()->Show(); 466 controller()->Show();
438 if (!profile()->GetPrefs()->GetBoolean( 467 if (!profile()->GetPrefs()->GetBoolean(
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
598 EXPECT_FALSE(billing_name.empty()); 627 EXPECT_FALSE(billing_name.empty());
599 EXPECT_FALSE(shipping_name.empty()); 628 EXPECT_FALSE(shipping_name.empty());
600 EXPECT_EQ(cc_name, billing_name); 629 EXPECT_EQ(cc_name, billing_name);
601 EXPECT_EQ(cc_name, shipping_name); 630 EXPECT_EQ(cc_name, shipping_name);
602 } 631 }
603 632
604 TestAutofillDialogController* controller() { return controller_.get(); } 633 TestAutofillDialogController* controller() { return controller_.get(); }
605 634
606 const FormStructure* form_structure() { return form_structure_; } 635 const FormStructure* form_structure() { return form_structure_; }
607 636
637 MockAddressValidator* GetMockValidator() const {
638 return mock_validator_.get();
639 }
640 void SetMockValidator(scoped_ptr<MockAddressValidator> validator) {
641 mock_validator_ = validator.Pass();
642 }
643
608 TestGeneratedCreditCardBubbleController* test_generated_bubble_controller() { 644 TestGeneratedCreditCardBubbleController* test_generated_bubble_controller() {
609 return test_generated_bubble_controller_; 645 return test_generated_bubble_controller_;
610 } 646 }
611 647
612 const MockNewCreditCardBubbleController* mock_new_card_bubble_controller() { 648 const MockNewCreditCardBubbleController* mock_new_card_bubble_controller() {
613 return mock_new_card_bubble_controller_.get(); 649 return mock_new_card_bubble_controller_.get();
614 } 650 }
615 651
616 private: 652 private:
617 void FinishedCallback(const FormStructure* form_structure) { 653 void FinishedCallback(const FormStructure* form_structure) {
618 form_structure_ = form_structure; 654 form_structure_ = form_structure;
619 } 655 }
620 656
621 #if defined(OS_WIN) 657 #if defined(OS_WIN)
622 // http://crbug.com/227221 658 // http://crbug.com/227221
623 ui::ScopedOleInitializer ole_initializer_; 659 ui::ScopedOleInitializer ole_initializer_;
624 #endif 660 #endif
625 661
626 // The controller owns itself. 662 // The controller owns itself.
627 base::WeakPtr<TestAutofillDialogController> controller_; 663 base::WeakPtr<TestAutofillDialogController> controller_;
628 664
629 // Must outlive the controller. 665 // Must outlive the controller.
630 AutofillMetrics metric_logger_; 666 AutofillMetrics metric_logger_;
631 667
632 // Returned when the dialog closes successfully. 668 // Returned when the dialog closes successfully.
633 const FormStructure* form_structure_; 669 const FormStructure* form_structure_;
634 670
671 // A mock validator object to prevent network requests and track when
672 // validation rules are loaded or validation attempts occur.
673 scoped_ptr<MockAddressValidator> mock_validator_;
674
635 // Used to monitor if the Autofill credit card bubble is shown. Owned by 675 // Used to monitor if the Autofill credit card bubble is shown. Owned by
636 // |web_contents()|. 676 // |web_contents()|.
637 TestGeneratedCreditCardBubbleController* test_generated_bubble_controller_; 677 TestGeneratedCreditCardBubbleController* test_generated_bubble_controller_;
638 678
639 // Used to record when new card bubbles would show. Created in |Reset()|. 679 // Used to record when new card bubbles would show. Created in |Reset()|.
640 scoped_ptr<MockNewCreditCardBubbleController> 680 scoped_ptr<MockNewCreditCardBubbleController>
641 mock_new_card_bubble_controller_; 681 mock_new_card_bubble_controller_;
642 682
643 scoped_ptr<ScopedTestingLocalState> scoped_local_state_; 683 scoped_ptr<ScopedTestingLocalState> scoped_local_state_;
644 }; 684 };
(...skipping 2281 matching lines...) Expand 10 before | Expand all | Expand 10 after
2926 }; 2966 };
2927 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 2967 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
2928 for (size_t i = 0; i < arraysize(kSupportedCardIdrs); ++i) { 2968 for (size_t i = 0; i < arraysize(kSupportedCardIdrs); ++i) {
2929 SCOPED_TRACE(base::IntToString(i)); 2969 SCOPED_TRACE(base::IntToString(i));
2930 gfx::Image supported_card_icon = rb.GetImageNamed(kSupportedCardIdrs[i]); 2970 gfx::Image supported_card_icon = rb.GetImageNamed(kSupportedCardIdrs[i]);
2931 EXPECT_GE(placeholder_icon.Width(), supported_card_icon.Width()); 2971 EXPECT_GE(placeholder_icon.Width(), supported_card_icon.Width());
2932 EXPECT_GE(placeholder_icon.Height(), supported_card_icon.Height()); 2972 EXPECT_GE(placeholder_icon.Height(), supported_card_icon.Height());
2933 } 2973 }
2934 } 2974 }
2935 2975
2936 TEST_F(AutofillDialogControllerTest, CountryChangeUpdatesSection) { 2976 class AutofillDialogControllerI18nTest : public AutofillDialogControllerTest {
2937 CommandLine* command_line = CommandLine::ForCurrentProcess(); 2977 public:
2938 command_line->AppendSwitch(::switches::kEnableAutofillAddressI18n); 2978 // AutofillDialogControllerTest implementation.
2979 virtual void SetUp() OVERRIDE {
2980 CommandLine* command_line = CommandLine::ForCurrentProcess();
2981 command_line->AppendSwitch(::switches::kEnableAutofillAddressI18n);
2982 AutofillDialogControllerTest::SetUp();
2983 }
2984 };
2939 2985
2940 Reset(); 2986 TEST_F(AutofillDialogControllerI18nTest, CountryChangeUpdatesSection) {
2941
2942 TestAutofillDialogView* view = controller()->GetView(); 2987 TestAutofillDialogView* view = controller()->GetView();
2943 view->ClearSectionUpdates(); 2988 view->ClearSectionUpdates();
2944 2989
2945 controller()->UserEditedOrActivatedInput(SECTION_SHIPPING, 2990 controller()->UserEditedOrActivatedInput(SECTION_SHIPPING,
2946 ADDRESS_HOME_COUNTRY, 2991 ADDRESS_HOME_COUNTRY,
2947 gfx::NativeView(), 2992 gfx::NativeView(),
2948 gfx::Rect(), 2993 gfx::Rect(),
2949 ASCIIToUTF16("China"), 2994 ASCIIToUTF16("China"),
2950 true); 2995 true);
2951 std::map<DialogSection, size_t> updates = view->section_updates(); 2996 std::map<DialogSection, size_t> updates = view->section_updates();
(...skipping 19 matching lines...) Expand all
2971 ADDRESS_BILLING_COUNTRY, 3016 ADDRESS_BILLING_COUNTRY,
2972 gfx::NativeView(), 3017 gfx::NativeView(),
2973 gfx::Rect(), 3018 gfx::Rect(),
2974 ASCIIToUTF16("Italy"), 3019 ASCIIToUTF16("Italy"),
2975 true); 3020 true);
2976 updates = view->section_updates(); 3021 updates = view->section_updates();
2977 EXPECT_EQ(1U, updates[SECTION_BILLING]); 3022 EXPECT_EQ(1U, updates[SECTION_BILLING]);
2978 EXPECT_EQ(1U, updates.size()); 3023 EXPECT_EQ(1U, updates.size());
2979 } 3024 }
2980 3025
3026 TEST_F(AutofillDialogControllerI18nTest, CorrectCountryFromInputs) {
3027 FieldValueMap inputs;
3028 inputs[ADDRESS_BILLING_COUNTRY] = base::ASCIIToUTF16("China");
3029 inputs[ADDRESS_HOME_COUNTRY] = base::ASCIIToUTF16("France");
3030
3031 base::Callback<base::string16(DialogSection, const AutofillType&)> get_info =
3032 base::Bind(&AutofillDialogControllerImpl::GetInfoFromInputs,
Evan Stade 2014/01/31 04:09:52 I feel like you should be able to test this functi
Dan Beam 2014/02/01 00:43:22 Done.
3033 base::ConstRef(inputs));
3034
3035 // Ensure that callbacks bound to the billing section only return
3036 // billing-specific info from |inputs| (e.g. the billing country).
3037 EXPECT_EQ(base::ASCIIToUTF16("China"),
3038 get_info.Run(SECTION_BILLING, AutofillType(ADDRESS_HOME_COUNTRY)));
3039 EXPECT_EQ(base::ASCIIToUTF16("CN"),
3040 get_info.Run(SECTION_BILLING, AutofillType(HTML_TYPE_COUNTRY_CODE,
3041 HTML_MODE_SHIPPING)));
3042
3043 // And conversely that shipping sections only return the shipping country.
3044 EXPECT_EQ(
3045 base::ASCIIToUTF16("France"),
3046 get_info.Run(SECTION_SHIPPING, AutofillType(ADDRESS_BILLING_COUNTRY)));
3047 EXPECT_EQ(base::ASCIIToUTF16("FR"),
3048 get_info.Run(SECTION_SHIPPING, AutofillType(HTML_TYPE_COUNTRY_CODE,
3049 HTML_MODE_BILLING)));
3050 }
3051
3052 TEST_F(AutofillDialogControllerI18nTest, LoadValidationRules) {
3053 SetMockValidator(make_scoped_ptr(new MockAddressValidator));
3054 EXPECT_CALL(*GetMockValidator(), ValidateAddress(_, _, _)).
3055 WillRepeatedly(Return(MockAddressValidator::RULES_NOT_READY));
3056
3057 EXPECT_CALL(*GetMockValidator(), LoadRules("US")).Times(AtLeast(1));
3058 Reset();
3059
3060 EXPECT_CALL(*GetMockValidator(), LoadRules("FR"));
3061 controller()->UserEditedOrActivatedInput(SECTION_CC_BILLING,
3062 ADDRESS_BILLING_COUNTRY,
3063 gfx::NativeView(),
3064 gfx::Rect(),
3065 ASCIIToUTF16("France"),
3066 true);
3067 }
3068
2981 } // namespace autofill 3069 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698