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