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

Side by Side Diff: third_party/libaddressinput/chromium/chrome_address_validator_unittest.cc

Issue 392083002: Retry downloading rules for libaddressinput. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase. Created 6 years, 5 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 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 <cstddef> 7 #include <cstddef>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/basictypes.h" 11 #include "base/message_loop/message_loop.h"
12 #include "base/macros.h" 12 #include "base/run_loop.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da ta.h" 14 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da ta.h"
16 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_fi eld.h"
17 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_pr oblem.h" 15 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_pr oblem.h"
18 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui .h" 16 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui .h"
19 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_va lidator.h"
20 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/callback.h "
21 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/downloader .h" 17 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/downloader .h"
22 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/null_stora ge.h" 18 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/null_stora ge.h"
23 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h" 19 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h"
24 #include "third_party/libaddressinput/src/cpp/test/fake_downloader.h" 20 #include "third_party/libaddressinput/src/cpp/test/fake_downloader.h"
25 21
26 namespace { 22 namespace autofill {
27 23
28 using ::autofill::AddressValidator;
29 using ::autofill::LoadRulesListener;
30 using ::i18n::addressinput::AddressData; 24 using ::i18n::addressinput::AddressData;
31 using ::i18n::addressinput::AddressField; 25 using ::i18n::addressinput::AddressField;
32 using ::i18n::addressinput::AddressProblem; 26 using ::i18n::addressinput::AddressProblem;
33 using ::i18n::addressinput::BuildCallback; 27 using ::i18n::addressinput::BuildCallback;
34 using ::i18n::addressinput::Downloader; 28 using ::i18n::addressinput::Downloader;
35 using ::i18n::addressinput::FakeDownloader; 29 using ::i18n::addressinput::FakeDownloader;
36 using ::i18n::addressinput::FieldProblemMap; 30 using ::i18n::addressinput::FieldProblemMap;
37 using ::i18n::addressinput::GetRegionCodes; 31 using ::i18n::addressinput::GetRegionCodes;
38 using ::i18n::addressinput::NullStorage; 32 using ::i18n::addressinput::NullStorage;
39 using ::i18n::addressinput::Storage; 33 using ::i18n::addressinput::Storage;
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 address_data.region_code = country_code; 71 address_data.region_code = country_code;
78 FieldProblemMap dummy; 72 FieldProblemMap dummy;
79 AddressValidator::Status status = 73 AddressValidator::Status status =
80 validator_->ValidateAddress(address_data, NULL, &dummy); 74 validator_->ValidateAddress(address_data, NULL, &dummy);
81 ASSERT_EQ(success, status == AddressValidator::SUCCESS); 75 ASSERT_EQ(success, status == AddressValidator::SUCCESS);
82 } 76 }
83 77
84 DISALLOW_COPY_AND_ASSIGN(AddressValidatorTest); 78 DISALLOW_COPY_AND_ASSIGN(AddressValidatorTest);
85 }; 79 };
86 80
87 // Use this text fixture if you're going to use a region with a large set of 81 // Use this test fixture if you're going to use a region with a large set of
88 // validation rules. All rules should be loaded in SetUpTestCase(). 82 // validation rules. All rules should be loaded in SetUpTestCase().
89 class LargeAddressValidatorTest : public testing::Test { 83 class LargeAddressValidatorTest : public testing::Test {
90 protected: 84 protected:
91 LargeAddressValidatorTest() {} 85 LargeAddressValidatorTest() {}
92 virtual ~LargeAddressValidatorTest() {} 86 virtual ~LargeAddressValidatorTest() {}
93 87
94 static void SetUpTestCase() { 88 static void SetUpTestCase() {
95 validator_ = 89 validator_ =
96 new AddressValidator(FakeDownloader::kFakeAggregateDataUrl, 90 new AddressValidator(FakeDownloader::kFakeAggregateDataUrl,
97 scoped_ptr<Downloader>(new FakeDownloader), 91 scoped_ptr<Downloader>(new FakeDownloader),
(...skipping 632 matching lines...) Expand 10 before | Expand all | Expand 10 after
730 724
731 FieldProblemMap filter; 725 FieldProblemMap filter;
732 filter.insert(std::make_pair(COUNTRY, UNKNOWN_VALUE)); 726 filter.insert(std::make_pair(COUNTRY, UNKNOWN_VALUE));
733 727
734 FieldProblemMap problems; 728 FieldProblemMap problems;
735 EXPECT_EQ(AddressValidator::RULES_UNAVAILABLE, 729 EXPECT_EQ(AddressValidator::RULES_UNAVAILABLE,
736 validator_->ValidateAddress(address, &filter, &problems)); 730 validator_->ValidateAddress(address, &filter, &problems));
737 EXPECT_TRUE(problems.empty()); 731 EXPECT_TRUE(problems.empty());
738 } 732 }
739 733
740 } // namespace 734 // Use this test fixture for configuring the number of failed attempts to load
735 // rules.
736 class FailingAddressValidatorTest : public testing::Test, LoadRulesListener {
737 protected:
738 // A downloader that always fails |failures_number| times before downloading
739 // data.
740 class FailingDownloader : public Downloader {
741 public:
742 explicit FailingDownloader() : failures_number_(0), attempts_number_(0) {}
743 virtual ~FailingDownloader() {}
744
745 // Sets the number of times to fail before downloading data.
746 void set_failures_number(int failures_number) {
747 failures_number_ = failures_number;
748 }
749
750 // Downloader implementation.
751 // Always fails for the first |failures_number| times.
752 virtual void Download(const std::string& url,
753 const Callback& callback) const OVERRIDE {
754 ++attempts_number_;
755 // |callback| takes ownership of the |new std::string|.
756 if (failures_number_-- > 0)
757 callback(false, url, new std::string);
758 else
759 actual_downloader_.Download(url, callback);
760 }
761
762 // Returns the number of download attempts.
763 int attempts_number() const { return attempts_number_; }
764
765 private:
766 // The number of times to fail before downloading data.
767 mutable int failures_number_;
768
769 // The number of times Download was called.
770 mutable int attempts_number_;
771
772 // The downloader to use for successful downloads.
773 FakeDownloader actual_downloader_;
774
775 DISALLOW_COPY_AND_ASSIGN(FailingDownloader);
776 };
777
778 FailingAddressValidatorTest()
779 : downloader_(new FailingDownloader),
780 validator_(new AddressValidator(FakeDownloader::kFakeAggregateDataUrl,
781 scoped_ptr<Downloader>(downloader_),
782 scoped_ptr<Storage>(new NullStorage),
783 this)),
784 load_rules_success_(false) {
785 validator_->retrier_->set_retry_period(base::TimeDelta::FromSeconds(0));
786 }
787
788 virtual ~FailingAddressValidatorTest() {}
789
790 FailingDownloader* downloader_; // Owned by |validator_|.
791 scoped_ptr<AddressValidator> validator_;
792 bool load_rules_success_;
793 base::MessageLoop ui_;
794
795 private:
796 // LoadRulesListener implementation.
797 virtual void OnAddressValidationRulesLoaded(const std::string&,
798 bool success) OVERRIDE {
799 load_rules_success_ = success;
800 }
801
802 DISALLOW_COPY_AND_ASSIGN(FailingAddressValidatorTest);
803 };
804
805 // The validator will attempt to load rules at most 8 times.
806 TEST_F(FailingAddressValidatorTest, RetryLoadingRulesHasLimit) {
807 downloader_->set_failures_number(99);
808 validator_->LoadRules("CH");
809 base::RunLoop().RunUntilIdle();
810
811 EXPECT_FALSE(load_rules_success_);
812 EXPECT_EQ(8, downloader_->attempts_number());
813 }
814
815 // The validator will load rules successfully if the downloader returns data
816 // before the maximum number of retries.
817 TEST_F(FailingAddressValidatorTest, RuleRetryingWillSucceed) {
818 downloader_->set_failures_number(4);
819 validator_->LoadRules("CH");
820 base::RunLoop().RunUntilIdle();
821
822 EXPECT_TRUE(load_rules_success_);
823 EXPECT_EQ(5, downloader_->attempts_number());
824 }
825
826 // The delayed task to retry loading rules should stop (instead of crashing) if
827 // the validator is destroyed before it fires.
828 TEST_F(FailingAddressValidatorTest, DestroyedValidatorStopsRetries) {
829 downloader_->set_failures_number(4);
830 validator_->LoadRules("CH");
831
832 // Destroy the validator.
833 validator_.reset();
834
835 // Fire the delayed task to retry loading rules.
836 EXPECT_NO_FATAL_FAILURE(base::RunLoop().RunUntilIdle());
837 }
838
839 // Each call to LoadRules should reset the number of retry attempts. If the
840 // first call to LoadRules exceeded the maximum number of retries, the second
841 // call to LoadRules should start counting the retries from zero.
842 TEST_F(FailingAddressValidatorTest, LoadingRulesSecondTimeSucceeds) {
843 downloader_->set_failures_number(11);
844 validator_->LoadRules("CH");
845 base::RunLoop().RunUntilIdle();
846
847 EXPECT_FALSE(load_rules_success_);
848 EXPECT_EQ(8, downloader_->attempts_number());
849
850 validator_->LoadRules("CH");
851 base::RunLoop().RunUntilIdle();
852
853 EXPECT_TRUE(load_rules_success_);
854 EXPECT_EQ(12, downloader_->attempts_number());
855 }
856
857 // Calling LoadRules("CH") and LoadRules("GB") simultaneously should attempt to
858 // load both rules up to the maximum number of attempts for each region.
859 TEST_F(FailingAddressValidatorTest, RegionsShouldRetryIndividually) {
860 downloader_->set_failures_number(99);
861 validator_->LoadRules("CH");
862 validator_->LoadRules("GB");
863 base::RunLoop().RunUntilIdle();
864
865 EXPECT_FALSE(load_rules_success_);
866 EXPECT_EQ(16, downloader_->attempts_number());
867 }
868
869 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698