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

Side by Side Diff: components/payments/address_normalizer_unittest.cc

Issue 2708933003: [Payments] Add timeout to the address_normalizer. (Closed)
Patch Set: Nits Created 3 years, 10 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
« no previous file with comments | « components/payments/address_normalizer.cc ('k') | third_party/libaddressinput/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/payments/address_normalizer.h"
6
7 #include "base/macros.h"
8 #include "base/run_loop.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/test/scoped_task_scheduler.h"
11 #include "components/autofill/core/browser/autofill_profile.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/null_stora ge.h"
14 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h"
15 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h"
16 #include "third_party/libaddressinput/src/cpp/test/testdata_source.h"
17
18 namespace payments {
19 namespace {
20
21 using ::i18n::addressinput::NullStorage;
22 using ::i18n::addressinput::Source;
23 using ::i18n::addressinput::Storage;
24 using ::i18n::addressinput::TestdataSource;
25
26 // The requester of normalization for this test.
27 class NormalizationDelegate : public AddressNormalizer::Delegate {
28 public:
29 NormalizationDelegate()
30 : normalized_called_(false), not_normalized_called_(false) {}
31
32 ~NormalizationDelegate() override {}
33
34 void OnAddressNormalized(
35 const autofill::AutofillProfile& normalized_profile) override {
36 normalized_called_ = true;
37 }
38
39 void OnCouldNotNormalize(const autofill::AutofillProfile& profile) override {
40 not_normalized_called_ = true;
41 }
42
43 bool normalized_called() { return normalized_called_; }
44
45 bool not_normalized_called() { return not_normalized_called_; }
46
47 private:
48 bool normalized_called_;
49 bool not_normalized_called_;
50 };
51
52 // Used to load region rules for this test.
53 class ChromiumTestdataSource : public TestdataSource {
54 public:
55 ChromiumTestdataSource() : TestdataSource(true) {}
56
57 ~ChromiumTestdataSource() override {}
58
59 // For this test, only load the rules for the "US".
60 void Get(const std::string& key, const Callback& data_ready) const override {
61 data_ready(
62 true, key,
63 new std::string("{\"data/US\": "
64 "{\"id\":\"data/US\",\"key\":\"US\",\"name\":\"UNITED "
65 "STATES\",\"lang\":\"en\",\"languages\":\"en\"}}"));
66 }
67 };
68
69 // A test subclass of the AddressNormalizer. Used to simulate rules not being
70 // loaded.
71 class TestAddressNormalizer : public AddressNormalizer {
72 public:
73 TestAddressNormalizer(std::unique_ptr<i18n::addressinput::Source> source,
74 std::unique_ptr<i18n::addressinput::Storage> storage)
75 : AddressNormalizer(std::move(source), std::move(storage)),
76 should_load_rules_(true) {}
77
78 ~TestAddressNormalizer() override {}
79
80 void ShouldLoadRules(bool should_load_rules) {
81 should_load_rules_ = should_load_rules;
82 }
83
84 void LoadRulesForRegion(const std::string& region_code) override {
85 if (should_load_rules_) {
86 AddressNormalizer::LoadRulesForRegion(region_code);
87 }
88 }
89
90 private:
91 bool should_load_rules_;
92 };
93
94 } // namespace
95
96 class AddressNormalizerTest : public testing::Test {
97 protected:
98 AddressNormalizerTest()
99 : normalizer_(new TestAddressNormalizer(
100 std::unique_ptr<Source>(new ChromiumTestdataSource),
101 std::unique_ptr<Storage>(new NullStorage))) {}
102
103 ~AddressNormalizerTest() override {}
104
105 const std::unique_ptr<TestAddressNormalizer> normalizer_;
106
107 base::test::ScopedTaskScheduler scoped_task_scheduler_;
108
109 private:
110 DISALLOW_COPY_AND_ASSIGN(AddressNormalizerTest);
111 };
112
113 // Tests that rules are not loaded by default.
114 TEST_F(AddressNormalizerTest, AreRulesLoadedForRegion_NotLoaded) {
115 EXPECT_FALSE(normalizer_->AreRulesLoadedForRegion("US"));
116 }
117
118 // Tests that the rules are loaded correctly.
119 TEST_F(AddressNormalizerTest, AreRulesLoadedForRegion_Loaded) {
120 normalizer_->LoadRulesForRegion("US");
121 EXPECT_TRUE(normalizer_->AreRulesLoadedForRegion("US"));
122 }
123
124 // Tests that if the rules are loaded before the normalization is started, the
125 // normalized profile will be returned to the delegate synchronously.
126 TEST_F(AddressNormalizerTest, StartNormalization_RulesLoaded) {
127 NormalizationDelegate delegate;
128 AutofillProfile profile;
129
130 // Load the rules.
131 normalizer_->LoadRulesForRegion("US");
132 EXPECT_TRUE(normalizer_->AreRulesLoadedForRegion("US"));
133
134 // Start the normalization.
135 normalizer_->StartAddressNormalization(profile, "US", 0, &delegate);
136
137 // Since the rules are already loaded, the address should be normalized
138 // synchronously.
139 EXPECT_TRUE(delegate.normalized_called());
140 EXPECT_FALSE(delegate.not_normalized_called());
141 }
142
143 // Tests that if the rules are not loaded before the normalization and cannot be
144 // loaded after, the address will not be normalized and the delegate will be
145 // notified.
146 TEST_F(AddressNormalizerTest, StartNormalization_RulesNotLoaded_WillNotLoad) {
147 NormalizationDelegate delegate;
148 AutofillProfile profile;
149
150 // Make sure the rules will not be loaded in the StartAddressNormalization
151 // call.
152 normalizer_->ShouldLoadRules(false);
153
154 // Start the normalization.
155 normalizer_->StartAddressNormalization(profile, "US", 0, &delegate);
156
157 // Let the timeout execute.
158 base::RunLoop().RunUntilIdle();
159
160 // Since the rules are never loaded and the timeout is 0, the delegate should
161 // get notified that the address could not be normalized.
162 EXPECT_FALSE(delegate.normalized_called());
163 EXPECT_TRUE(delegate.not_normalized_called());
164 }
165
166 // Tests that if the rules are not loaded before the call to
167 // StartAddressNormalization, they will be loaded in the call.
168 TEST_F(AddressNormalizerTest, StartNormalization_RulesNotLoaded_WillLoad) {
169 NormalizationDelegate delegate;
170 AutofillProfile profile;
171
172 // Start the normalization.
173 normalizer_->StartAddressNormalization(profile, "US", 0, &delegate);
174
175 // Even if the rules are not loaded before the call to
176 // StartAddressNormalization, they should get loaded in the call. Since our
177 // test source is synchronous, the normalization will happen synchronously
178 // too.
179 EXPECT_TRUE(normalizer_->AreRulesLoadedForRegion("US"));
180 EXPECT_TRUE(delegate.normalized_called());
181 EXPECT_FALSE(delegate.not_normalized_called());
182 }
183
184 } // namespace payments
OLDNEW
« no previous file with comments | « components/payments/address_normalizer.cc ('k') | third_party/libaddressinput/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698