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

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: . 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 25 matching lines...) Expand all
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"
51 #include "testing/gmock/include/gmock/gmock.h" 53 #include "testing/gmock/include/gmock/gmock.h"
52 #include "testing/gtest/include/gtest/gtest.h" 54 #include "testing/gtest/include/gtest/gtest.h"
55 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/addre ss_data.h"
56 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/addre ss_validator.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 MockAddressValidator() {
249 ON_CALL(*this, ValidateAddress(_, _, _)).WillByDefault(Return(SUCCESS));
250 }
251
252 virtual ~MockAddressValidator() {}
253
254 MOCK_METHOD1(LoadRules, void(const std::string& country_code));
255
256 MOCK_CONST_METHOD3(ValidateAddress,
257 ::i18n::addressinput::AddressValidator::Status(
258 const AddressData& address,
259 const AddressProblemFilter& filter,
260 AddressProblems* problems));
261 };
262
236 class TestAutofillDialogController 263 class TestAutofillDialogController
237 : public AutofillDialogControllerImpl, 264 : public AutofillDialogControllerImpl,
238 public base::SupportsWeakPtr<TestAutofillDialogController> { 265 public base::SupportsWeakPtr<TestAutofillDialogController> {
239 public: 266 public:
240 TestAutofillDialogController( 267 TestAutofillDialogController(
241 content::WebContents* contents, 268 content::WebContents* contents,
242 const FormData& form_structure, 269 const FormData& form_structure,
243 const GURL& source_url, 270 const GURL& source_url,
244 const AutofillMetrics& metric_logger, 271 const AutofillMetrics& metric_logger,
245 const base::Callback<void(const FormStructure*)>& callback, 272 const base::Callback<void(const FormStructure*)>& callback,
(...skipping 25 matching lines...) Expand all
271 } 298 }
272 299
273 TestAutofillDialogView* GetView() { 300 TestAutofillDialogView* GetView() {
274 return static_cast<TestAutofillDialogView*>(view()); 301 return static_cast<TestAutofillDialogView*>(view());
275 } 302 }
276 303
277 TestPersonalDataManager* GetTestingManager() { 304 TestPersonalDataManager* GetTestingManager() {
278 return &test_manager_; 305 return &test_manager_;
279 } 306 }
280 307
308 MockAddressValidator* GetMockValidator() {
309 return &mock_validator_;
310 }
311
281 wallet::MockWalletClient* GetTestingWalletClient() { 312 wallet::MockWalletClient* GetTestingWalletClient() {
282 return &mock_wallet_client_; 313 return &mock_wallet_client_;
283 } 314 }
284 315
285 const GURL& open_tab_url() { return open_tab_url_; } 316 const GURL& open_tab_url() { return open_tab_url_; }
286 317
287 void SimulateSigninError() { 318 void SimulateSigninError() {
288 OnWalletSigninError(); 319 OnWalletSigninError();
289 } 320 }
290 321
(...skipping 26 matching lines...) Expand all
317 using AutofillDialogControllerImpl::IsSubmitPausedOn; 348 using AutofillDialogControllerImpl::IsSubmitPausedOn;
318 using AutofillDialogControllerImpl::NOT_CHECKED; 349 using AutofillDialogControllerImpl::NOT_CHECKED;
319 using AutofillDialogControllerImpl::SignedInState; 350 using AutofillDialogControllerImpl::SignedInState;
320 351
321 protected: 352 protected:
322 virtual PersonalDataManager* GetManager() const OVERRIDE { 353 virtual PersonalDataManager* GetManager() const OVERRIDE {
323 return const_cast<TestAutofillDialogController*>(this)-> 354 return const_cast<TestAutofillDialogController*>(this)->
324 GetTestingManager(); 355 GetTestingManager();
325 } 356 }
326 357
358 virtual AddressValidator* GetValidator() OVERRIDE {
359 return &mock_validator_;
360 }
361
327 virtual wallet::WalletClient* GetWalletClient() OVERRIDE { 362 virtual wallet::WalletClient* GetWalletClient() OVERRIDE {
328 return &mock_wallet_client_; 363 return &mock_wallet_client_;
329 } 364 }
330 365
331 virtual void OpenTabWithUrl(const GURL& url) OVERRIDE { 366 virtual void OpenTabWithUrl(const GURL& url) OVERRIDE {
332 open_tab_url_ = url; 367 open_tab_url_ = url;
333 } 368 }
334 369
335 virtual void ShowNewCreditCardBubble( 370 virtual void ShowNewCreditCardBubble(
336 scoped_ptr<CreditCard> new_card, 371 scoped_ptr<CreditCard> new_card,
(...skipping 11 matching lines...) Expand all
348 383
349 private: 384 private:
350 // To specify our own metric logger. 385 // To specify our own metric logger.
351 virtual const AutofillMetrics& GetMetricLogger() const OVERRIDE { 386 virtual const AutofillMetrics& GetMetricLogger() const OVERRIDE {
352 return metric_logger_; 387 return metric_logger_;
353 } 388 }
354 389
355 const AutofillMetrics& metric_logger_; 390 const AutofillMetrics& metric_logger_;
356 TestPersonalDataManager test_manager_; 391 TestPersonalDataManager test_manager_;
357 testing::NiceMock<wallet::MockWalletClient> mock_wallet_client_; 392 testing::NiceMock<wallet::MockWalletClient> mock_wallet_client_;
393
394 // A mock validator object to prevent network requests and track when
395 // validation rules are loaded or validation attempts occur.
396 testing::NiceMock<MockAddressValidator> mock_validator_;
397
358 GURL open_tab_url_; 398 GURL open_tab_url_;
359 MockNewCreditCardBubbleController* mock_new_card_bubble_controller_; 399 MockNewCreditCardBubbleController* mock_new_card_bubble_controller_;
360 400
361 // The number of times that the submit button was delayed. 401 // The number of times that the submit button was delayed.
362 int submit_button_delay_count_; 402 int submit_button_delay_count_;
363 403
364 DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogController); 404 DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogController);
365 }; 405 };
366 406
367 class AutofillDialogControllerTest : public ChromeRenderViewHostTestHarness { 407 class AutofillDialogControllerTest : public ChromeRenderViewHostTestHarness {
(...skipping 2558 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 MATCHER_P(CountryCode, country_code, "Checks country code of an AddressData") {
3027 // |arg| is an AddressData object.
3028 return arg.country_code == country_code;
3029 }
3030
3031 TEST_F(AutofillDialogControllerI18nTest, CorrectCountryFromInputs) {
3032 EXPECT_CALL(*controller()->GetMockValidator(),
3033 ValidateAddress(CountryCode("CN"), _, _));
3034
3035 FieldValueMap billing_inputs;
3036 billing_inputs[ADDRESS_BILLING_COUNTRY] = ASCIIToUTF16("China");
3037 controller()->InputsAreValid(SECTION_BILLING, billing_inputs);
3038
3039 EXPECT_CALL(*controller()->GetMockValidator(),
3040 ValidateAddress(CountryCode("FR"), _, _));
3041
3042 FieldValueMap shipping_inputs;
3043 shipping_inputs[ADDRESS_HOME_COUNTRY] = ASCIIToUTF16("France");
3044 controller()->InputsAreValid(SECTION_SHIPPING, shipping_inputs);
3045 }
3046
3047 TEST_F(AutofillDialogControllerI18nTest, LoadValidationRules) {
3048 ResetControllerWithFormData(DefaultFormData());
3049 EXPECT_CALL(*controller()->GetMockValidator(), LoadRules("US"));
3050 controller()->Show();
3051
3052 EXPECT_CALL(*controller()->GetMockValidator(), LoadRules("FR"));
3053 controller()->UserEditedOrActivatedInput(SECTION_CC_BILLING,
3054 ADDRESS_BILLING_COUNTRY,
3055 gfx::NativeView(),
3056 gfx::Rect(),
3057 ASCIIToUTF16("France"),
3058 true);
3059 }
3060
2981 } // namespace autofill 3061 } // namespace autofill
OLDNEW
« no previous file with comments | « chrome/browser/ui/autofill/autofill_dialog_controller_impl.cc ('k') | chrome/browser/ui/autofill/autofill_dialog_i18n_input.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698