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