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); |
79 | |
80 if (!country_code.compare("US")) { | |
sebsg
2017/03/21 15:29:52
This should be in it's own test case instead of in
Parastoo
2017/03/21 20:05:52
Done.
| |
81 std::vector<std::string> sub_keys = | |
82 validator_->GetRegionSubKeys("US"); | |
83 ASSERT_FALSE(sub_keys.empty()); | |
84 ASSERT_EQ(sub_keys[0], "AL"); | |
85 } | |
78 } | 86 } |
79 | 87 |
80 DISALLOW_COPY_AND_ASSIGN(AddressValidatorTest); | 88 DISALLOW_COPY_AND_ASSIGN(AddressValidatorTest); |
81 }; | 89 }; |
82 | 90 |
83 // Use this test fixture if you're going to use a region with a large set of | 91 // Use this test fixture if you're going to use a region with a large set of |
84 // validation rules. All rules should be loaded in SetUpTestCase(). | 92 // validation rules. All rules should be loaded in SetUpTestCase(). |
85 class LargeAddressValidatorTest : public testing::Test { | 93 class LargeAddressValidatorTest : public testing::Test { |
86 protected: | 94 protected: |
87 LargeAddressValidatorTest() {} | 95 LargeAddressValidatorTest() {} |
(...skipping 663 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
751 TestAddressValidator(std::unique_ptr<::i18n::addressinput::Source> source, | 759 TestAddressValidator(std::unique_ptr<::i18n::addressinput::Source> source, |
752 std::unique_ptr<::i18n::addressinput::Storage> storage, | 760 std::unique_ptr<::i18n::addressinput::Storage> storage, |
753 LoadRulesListener* load_rules_listener) | 761 LoadRulesListener* load_rules_listener) |
754 : AddressValidator(std::move(source), | 762 : AddressValidator(std::move(source), |
755 std::move(storage), | 763 std::move(storage), |
756 load_rules_listener) {} | 764 load_rules_listener) {} |
757 | 765 |
758 virtual ~TestAddressValidator() {} | 766 virtual ~TestAddressValidator() {} |
759 | 767 |
760 protected: | 768 protected: |
761 virtual base::TimeDelta GetBaseRetryPeriod() const override { | 769 base::TimeDelta GetBaseRetryPeriod() const override { |
762 return base::TimeDelta::FromSeconds(0); | 770 return base::TimeDelta::FromSeconds(0); |
763 } | 771 } |
764 | 772 |
765 private: | 773 private: |
766 DISALLOW_COPY_AND_ASSIGN(TestAddressValidator); | 774 DISALLOW_COPY_AND_ASSIGN(TestAddressValidator); |
767 }; | 775 }; |
768 | 776 |
769 // A source that always fails |failures_number| times before downloading | 777 // A source that always fails |failures_number| times before downloading |
770 // data. | 778 // data. |
771 class FailingSource : public Source { | 779 class FailingSource : public Source { |
772 public: | 780 public: |
773 explicit FailingSource() | 781 FailingSource() |
774 : failures_number_(0), attempts_number_(0), actual_source_(true) {} | 782 : failures_number_(0), attempts_number_(0), actual_source_(true) {} |
775 virtual ~FailingSource() {} | 783 virtual ~FailingSource() {} |
776 | 784 |
777 // Sets the number of times to fail before downloading data. | 785 // Sets the number of times to fail before downloading data. |
778 void set_failures_number(int failures_number) { | 786 void set_failures_number(int failures_number) { |
779 failures_number_ = failures_number; | 787 failures_number_ = failures_number; |
780 } | 788 } |
781 | 789 |
782 // Source implementation. | 790 // Source implementation. |
783 // Always fails for the first |failures_number| times. | 791 // Always fails for the first |failures_number| times. |
784 virtual void Get(const std::string& url, | 792 void Get(const std::string& url, const Callback& callback) const override { |
785 const Callback& callback) const override { | |
786 ++attempts_number_; | 793 ++attempts_number_; |
787 // |callback| takes ownership of the |new std::string|. | 794 // |callback| takes ownership of the |new std::string|. |
788 if (failures_number_-- > 0) | 795 if (failures_number_-- > 0) |
789 callback(false, url, new std::string); | 796 callback(false, url, new std::string); |
790 else | 797 else |
791 actual_source_.Get(url, callback); | 798 actual_source_.Get(url, callback); |
792 } | 799 } |
793 | 800 |
794 // Returns the number of download attempts. | 801 // Returns the number of download attempts. |
795 int attempts_number() const { return attempts_number_; } | 802 int attempts_number() const { return attempts_number_; } |
(...skipping 20 matching lines...) Expand all Loading... | |
816 load_rules_success_(false) {} | 823 load_rules_success_(false) {} |
817 | 824 |
818 virtual ~FailingAddressValidatorTest() {} | 825 virtual ~FailingAddressValidatorTest() {} |
819 | 826 |
820 FailingSource* source_; // Owned by |validator_|. | 827 FailingSource* source_; // Owned by |validator_|. |
821 std::unique_ptr<AddressValidator> validator_; | 828 std::unique_ptr<AddressValidator> validator_; |
822 bool load_rules_success_; | 829 bool load_rules_success_; |
823 | 830 |
824 private: | 831 private: |
825 // LoadRulesListener implementation. | 832 // LoadRulesListener implementation. |
826 virtual void OnAddressValidationRulesLoaded(const std::string&, | 833 void OnAddressRulesLoaded(const std::string&, bool success) override { |
827 bool success) override { | |
828 load_rules_success_ = success; | 834 load_rules_success_ = success; |
829 } | 835 } |
830 | 836 |
831 base::MessageLoop ui_; | 837 base::MessageLoop ui_; |
832 | 838 |
833 DISALLOW_COPY_AND_ASSIGN(FailingAddressValidatorTest); | 839 DISALLOW_COPY_AND_ASSIGN(FailingAddressValidatorTest); |
834 }; | 840 }; |
835 | 841 |
836 // The validator will attempt to load rules at most 8 times. | 842 // The validator will attempt to load rules at most 8 times. |
837 TEST_F(FailingAddressValidatorTest, RetryLoadingRulesHasLimit) { | 843 TEST_F(FailingAddressValidatorTest, RetryLoadingRulesHasLimit) { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
891 source_->set_failures_number(99); | 897 source_->set_failures_number(99); |
892 validator_->LoadRules("CH"); | 898 validator_->LoadRules("CH"); |
893 validator_->LoadRules("GB"); | 899 validator_->LoadRules("GB"); |
894 base::RunLoop().RunUntilIdle(); | 900 base::RunLoop().RunUntilIdle(); |
895 | 901 |
896 EXPECT_FALSE(load_rules_success_); | 902 EXPECT_FALSE(load_rules_success_); |
897 EXPECT_EQ(16, source_->attempts_number()); | 903 EXPECT_EQ(16, source_->attempts_number()); |
898 } | 904 } |
899 | 905 |
900 } // namespace autofill | 906 } // namespace autofill |
OLD | NEW |