| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "third_party/libaddressinput/chromium/chrome_address_validator.h" | 5 #include "third_party/libaddressinput/chromium/chrome_address_validator.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <set> | |
| 9 #include <string> | 8 #include <string> |
| 10 #include <utility> | 9 #include <utility> |
| 11 #include <vector> | 10 #include <vector> |
| 12 | 11 |
| 13 #include "base/macros.h" | 12 #include "base/macros.h" |
| 14 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 15 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da
ta.h" | 17 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da
ta.h" |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 this)) { | 60 this)) { |
| 62 validator_->LoadRules("US"); | 61 validator_->LoadRules("US"); |
| 63 } | 62 } |
| 64 | 63 |
| 65 virtual ~AddressValidatorTest() {} | 64 virtual ~AddressValidatorTest() {} |
| 66 | 65 |
| 67 const std::unique_ptr<AddressValidator> validator_; | 66 const std::unique_ptr<AddressValidator> validator_; |
| 68 | 67 |
| 69 private: | 68 private: |
| 70 // LoadRulesListener implementation. | 69 // LoadRulesListener implementation. |
| 71 void OnAddressRulesLoaded(const std::string& country_code, | 70 virtual void OnAddressValidationRulesLoaded(const std::string& country_code, |
| 72 bool success) override { | 71 bool success) override { |
| 73 AddressData address_data; | 72 AddressData address_data; |
| 74 address_data.region_code = country_code; | 73 address_data.region_code = country_code; |
| 75 FieldProblemMap dummy; | 74 FieldProblemMap dummy; |
| 76 AddressValidator::Status status = | 75 AddressValidator::Status status = |
| 77 validator_->ValidateAddress(address_data, NULL, &dummy); | 76 validator_->ValidateAddress(address_data, NULL, &dummy); |
| 78 ASSERT_EQ(success, status == AddressValidator::SUCCESS); | 77 ASSERT_EQ(success, status == AddressValidator::SUCCESS); |
| 79 } | 78 } |
| 80 | 79 |
| 81 DISALLOW_COPY_AND_ASSIGN(AddressValidatorTest); | 80 DISALLOW_COPY_AND_ASSIGN(AddressValidatorTest); |
| 82 }; | 81 }; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 104 | 103 |
| 105 // Owned shared instance of validator with large sets validation rules. | 104 // Owned shared instance of validator with large sets validation rules. |
| 106 static AddressValidator* validator_; | 105 static AddressValidator* validator_; |
| 107 | 106 |
| 108 private: | 107 private: |
| 109 DISALLOW_COPY_AND_ASSIGN(LargeAddressValidatorTest); | 108 DISALLOW_COPY_AND_ASSIGN(LargeAddressValidatorTest); |
| 110 }; | 109 }; |
| 111 | 110 |
| 112 AddressValidator* LargeAddressValidatorTest::validator_ = NULL; | 111 AddressValidator* LargeAddressValidatorTest::validator_ = NULL; |
| 113 | 112 |
| 114 TEST_F(AddressValidatorTest, SubKeysLoaded) { | |
| 115 const std::string country_code = "US"; | |
| 116 const std::string first_state = "AL"; | |
| 117 | |
| 118 validator_->LoadRules(country_code); | |
| 119 std::vector<std::string> sub_keys = | |
| 120 validator_->GetRegionSubKeys(country_code); | |
| 121 ASSERT_FALSE(sub_keys.empty()); | |
| 122 ASSERT_EQ(sub_keys[0], first_state); | |
| 123 } | |
| 124 | |
| 125 TEST_F(AddressValidatorTest, SubKeysNotLoaded) { | |
| 126 const std::string country_code = "ZZ"; | |
| 127 | |
| 128 validator_->LoadRules(country_code); | |
| 129 std::vector<std::string> sub_keys = | |
| 130 validator_->GetRegionSubKeys(country_code); | |
| 131 ASSERT_TRUE(sub_keys.empty()); | |
| 132 } | |
| 133 | |
| 134 TEST_F(AddressValidatorTest, RegionHasRules) { | 113 TEST_F(AddressValidatorTest, RegionHasRules) { |
| 135 const std::vector<std::string>& region_codes = GetRegionCodes(); | 114 const std::vector<std::string>& region_codes = GetRegionCodes(); |
| 136 AddressData address; | 115 AddressData address; |
| 137 for (size_t i = 0; i < region_codes.size(); ++i) { | 116 for (size_t i = 0; i < region_codes.size(); ++i) { |
| 138 SCOPED_TRACE("For region: " + region_codes[i]); | 117 SCOPED_TRACE("For region: " + region_codes[i]); |
| 139 validator_->LoadRules(region_codes[i]); | 118 validator_->LoadRules(region_codes[i]); |
| 140 address.region_code = region_codes[i]; | 119 address.region_code = region_codes[i]; |
| 141 FieldProblemMap dummy; | 120 FieldProblemMap dummy; |
| 142 EXPECT_EQ(AddressValidator::SUCCESS, | 121 EXPECT_EQ(AddressValidator::SUCCESS, |
| 143 validator_->ValidateAddress(address, NULL, &dummy)); | 122 validator_->ValidateAddress(address, NULL, &dummy)); |
| (...skipping 628 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 772 TestAddressValidator(std::unique_ptr<::i18n::addressinput::Source> source, | 751 TestAddressValidator(std::unique_ptr<::i18n::addressinput::Source> source, |
| 773 std::unique_ptr<::i18n::addressinput::Storage> storage, | 752 std::unique_ptr<::i18n::addressinput::Storage> storage, |
| 774 LoadRulesListener* load_rules_listener) | 753 LoadRulesListener* load_rules_listener) |
| 775 : AddressValidator(std::move(source), | 754 : AddressValidator(std::move(source), |
| 776 std::move(storage), | 755 std::move(storage), |
| 777 load_rules_listener) {} | 756 load_rules_listener) {} |
| 778 | 757 |
| 779 virtual ~TestAddressValidator() {} | 758 virtual ~TestAddressValidator() {} |
| 780 | 759 |
| 781 protected: | 760 protected: |
| 782 base::TimeDelta GetBaseRetryPeriod() const override { | 761 virtual base::TimeDelta GetBaseRetryPeriod() const override { |
| 783 return base::TimeDelta::FromSeconds(0); | 762 return base::TimeDelta::FromSeconds(0); |
| 784 } | 763 } |
| 785 | 764 |
| 786 private: | 765 private: |
| 787 DISALLOW_COPY_AND_ASSIGN(TestAddressValidator); | 766 DISALLOW_COPY_AND_ASSIGN(TestAddressValidator); |
| 788 }; | 767 }; |
| 789 | 768 |
| 790 // A source that always fails |failures_number| times before downloading | 769 // A source that always fails |failures_number| times before downloading |
| 791 // data. | 770 // data. |
| 792 class FailingSource : public Source { | 771 class FailingSource : public Source { |
| 793 public: | 772 public: |
| 794 FailingSource() | 773 explicit FailingSource() |
| 795 : failures_number_(0), attempts_number_(0), actual_source_(true) {} | 774 : failures_number_(0), attempts_number_(0), actual_source_(true) {} |
| 796 virtual ~FailingSource() {} | 775 virtual ~FailingSource() {} |
| 797 | 776 |
| 798 // Sets the number of times to fail before downloading data. | 777 // Sets the number of times to fail before downloading data. |
| 799 void set_failures_number(int failures_number) { | 778 void set_failures_number(int failures_number) { |
| 800 failures_number_ = failures_number; | 779 failures_number_ = failures_number; |
| 801 } | 780 } |
| 802 | 781 |
| 803 // Source implementation. | 782 // Source implementation. |
| 804 // Always fails for the first |failures_number| times. | 783 // Always fails for the first |failures_number| times. |
| 805 void Get(const std::string& url, const Callback& callback) const override { | 784 virtual void Get(const std::string& url, |
| 785 const Callback& callback) const override { |
| 806 ++attempts_number_; | 786 ++attempts_number_; |
| 807 // |callback| takes ownership of the |new std::string|. | 787 // |callback| takes ownership of the |new std::string|. |
| 808 if (failures_number_-- > 0) | 788 if (failures_number_-- > 0) |
| 809 callback(false, url, new std::string); | 789 callback(false, url, new std::string); |
| 810 else | 790 else |
| 811 actual_source_.Get(url, callback); | 791 actual_source_.Get(url, callback); |
| 812 } | 792 } |
| 813 | 793 |
| 814 // Returns the number of download attempts. | 794 // Returns the number of download attempts. |
| 815 int attempts_number() const { return attempts_number_; } | 795 int attempts_number() const { return attempts_number_; } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 836 load_rules_success_(false) {} | 816 load_rules_success_(false) {} |
| 837 | 817 |
| 838 virtual ~FailingAddressValidatorTest() {} | 818 virtual ~FailingAddressValidatorTest() {} |
| 839 | 819 |
| 840 FailingSource* source_; // Owned by |validator_|. | 820 FailingSource* source_; // Owned by |validator_|. |
| 841 std::unique_ptr<AddressValidator> validator_; | 821 std::unique_ptr<AddressValidator> validator_; |
| 842 bool load_rules_success_; | 822 bool load_rules_success_; |
| 843 | 823 |
| 844 private: | 824 private: |
| 845 // LoadRulesListener implementation. | 825 // LoadRulesListener implementation. |
| 846 void OnAddressRulesLoaded(const std::string&, bool success) override { | 826 virtual void OnAddressValidationRulesLoaded(const std::string&, |
| 827 bool success) override { |
| 847 load_rules_success_ = success; | 828 load_rules_success_ = success; |
| 848 } | 829 } |
| 849 | 830 |
| 850 base::MessageLoop ui_; | 831 base::MessageLoop ui_; |
| 851 | 832 |
| 852 DISALLOW_COPY_AND_ASSIGN(FailingAddressValidatorTest); | 833 DISALLOW_COPY_AND_ASSIGN(FailingAddressValidatorTest); |
| 853 }; | 834 }; |
| 854 | 835 |
| 855 // The validator will attempt to load rules at most 8 times. | 836 // The validator will attempt to load rules at most 8 times. |
| 856 TEST_F(FailingAddressValidatorTest, RetryLoadingRulesHasLimit) { | 837 TEST_F(FailingAddressValidatorTest, RetryLoadingRulesHasLimit) { |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 910 source_->set_failures_number(99); | 891 source_->set_failures_number(99); |
| 911 validator_->LoadRules("CH"); | 892 validator_->LoadRules("CH"); |
| 912 validator_->LoadRules("GB"); | 893 validator_->LoadRules("GB"); |
| 913 base::RunLoop().RunUntilIdle(); | 894 base::RunLoop().RunUntilIdle(); |
| 914 | 895 |
| 915 EXPECT_FALSE(load_rules_success_); | 896 EXPECT_FALSE(load_rules_success_); |
| 916 EXPECT_EQ(16, source_->attempts_number()); | 897 EXPECT_EQ(16, source_->attempts_number()); |
| 917 } | 898 } |
| 918 | 899 |
| 919 } // namespace autofill | 900 } // namespace autofill |
| OLD | NEW |