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

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

Issue 2829903004: Reland: Normalize shipping address for merchant on Desktop. (Closed)
Patch Set: Make android code use the new impl Created 3 years, 8 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
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/core/address_normalizer.h"
6
7 #include <utility>
8
9 #include "base/run_loop.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/test/scoped_task_scheduler.h"
12 #include "components/autofill/core/browser/autofill_profile.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/null_stora ge.h"
15 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h"
16 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h"
17 #include "third_party/libaddressinput/src/cpp/test/testdata_source.h"
18
19 namespace payments {
20 namespace {
21
22 using ::autofill::AutofillProfile;
23 using ::i18n::addressinput::NullStorage;
24 using ::i18n::addressinput::Source;
25 using ::i18n::addressinput::Storage;
26 using ::i18n::addressinput::TestdataSource;
27
28 const char kLocale[] = "US";
29
30 // The requester of normalization for this test.
31 class NormalizationDelegate : public AddressNormalizer::Delegate {
32 public:
33 NormalizationDelegate()
34 : normalized_called_(false), not_normalized_called_(false) {}
35
36 ~NormalizationDelegate() override {}
37
38 void OnAddressNormalized(const autofill::AutofillProfile& profile) override {
39 normalized_called_ = true;
40 profile_ = profile;
41 }
42
43 void OnCouldNotNormalize(const autofill::AutofillProfile& profile) override {
44 not_normalized_called_ = true;
45 profile_ = profile;
46 }
47
48 bool normalized_called() const { return normalized_called_; }
49
50 bool not_normalized_called() const { return not_normalized_called_; }
51
52 const AutofillProfile& profile() const { return profile_; }
53
54 private:
55 bool normalized_called_;
56 bool not_normalized_called_;
57 AutofillProfile profile_;
58
59 DISALLOW_COPY_AND_ASSIGN(NormalizationDelegate);
60 };
61
62 // Used to load region rules for this test.
63 class ChromiumTestdataSource : public TestdataSource {
64 public:
65 ChromiumTestdataSource() : TestdataSource(true) {}
66
67 ~ChromiumTestdataSource() override {}
68
69 // For this test, only load the rules for the kLocale.
70 void Get(const std::string& key, const Callback& data_ready) const override {
71 data_ready(
72 true, key,
73 new std::string("{\"data/US\": "
74 "{\"id\":\"data/US\",\"key\":\"US\",\"name\":\"UNITED "
75 "STATES\",\"lang\":\"en\",\"languages\":\"en\"}}"));
76 }
77
78 private:
79 DISALLOW_COPY_AND_ASSIGN(ChromiumTestdataSource);
80 };
81
82 // A test subclass of the AddressNormalizer. Used to simulate rules not being
83 // loaded.
84 class TestAddressNormalizer : public AddressNormalizer {
85 public:
86 TestAddressNormalizer(std::unique_ptr<::i18n::addressinput::Source> source,
87 std::unique_ptr<::i18n::addressinput::Storage> storage)
88 : AddressNormalizer(std::move(source), std::move(storage)),
89 should_load_rules_(true) {}
90
91 ~TestAddressNormalizer() override {}
92
93 void ShouldLoadRules(bool should_load_rules) {
94 should_load_rules_ = should_load_rules;
95 }
96
97 void LoadRulesForRegion(const std::string& region_code) override {
98 if (should_load_rules_) {
99 AddressNormalizer::LoadRulesForRegion(region_code);
100 }
101 }
102
103 private:
104 bool should_load_rules_;
105
106 DISALLOW_COPY_AND_ASSIGN(TestAddressNormalizer);
107 };
108
109 } // namespace
110
111 class AddressNormalizerTest : public testing::Test {
112 protected:
113 AddressNormalizerTest()
114 : normalizer_(new TestAddressNormalizer(
115 std::unique_ptr<Source>(new ChromiumTestdataSource),
116 std::unique_ptr<Storage>(new NullStorage))) {}
117
118 ~AddressNormalizerTest() override {}
119
120 const std::unique_ptr<TestAddressNormalizer> normalizer_;
121
122 base::test::ScopedTaskScheduler scoped_task_scheduler_;
123
124 private:
125 DISALLOW_COPY_AND_ASSIGN(AddressNormalizerTest);
126 };
127
128 // Tests that rules are not loaded by default.
129 TEST_F(AddressNormalizerTest, AreRulesLoadedForRegion_NotLoaded) {
130 EXPECT_FALSE(normalizer_->AreRulesLoadedForRegion(kLocale));
131 }
132
133 // Tests that the rules are loaded correctly.
134 TEST_F(AddressNormalizerTest, AreRulesLoadedForRegion_Loaded) {
135 normalizer_->LoadRulesForRegion(kLocale);
136 EXPECT_TRUE(normalizer_->AreRulesLoadedForRegion(kLocale));
137 }
138
139 // Tests that if the rules are loaded before the normalization is started, the
140 // normalized profile will be returned to the delegate synchronously.
141 TEST_F(AddressNormalizerTest, StartNormalization_RulesLoaded) {
142 NormalizationDelegate delegate;
143 AutofillProfile profile;
144
145 // Load the rules.
146 normalizer_->LoadRulesForRegion(kLocale);
147 EXPECT_TRUE(normalizer_->AreRulesLoadedForRegion(kLocale));
148
149 // Start the normalization.
150 normalizer_->StartAddressNormalization(profile, kLocale, 0, &delegate);
151
152 // Since the rules are already loaded, the address should be normalized
153 // synchronously.
154 EXPECT_TRUE(delegate.normalized_called());
155 EXPECT_FALSE(delegate.not_normalized_called());
156 }
157
158 // Tests that if the rules are not loaded before the normalization and cannot be
159 // loaded after, the address will not be normalized and the delegate will be
160 // notified.
161 TEST_F(AddressNormalizerTest, StartNormalization_RulesNotLoaded_WillNotLoad) {
162 NormalizationDelegate delegate;
163 AutofillProfile profile;
164
165 // Make sure the rules will not be loaded in the StartAddressNormalization
166 // call.
167 normalizer_->ShouldLoadRules(false);
168
169 // Start the normalization.
170 normalizer_->StartAddressNormalization(profile, kLocale, 0, &delegate);
171
172 // Let the timeout execute.
173 base::RunLoop().RunUntilIdle();
174
175 // Since the rules are never loaded and the timeout is 0, the delegate should
176 // get notified that the address could not be normalized.
177 EXPECT_FALSE(delegate.normalized_called());
178 EXPECT_TRUE(delegate.not_normalized_called());
179 }
180
181 // Tests that if the rules are not loaded before the call to
182 // StartAddressNormalization, they will be loaded in the call.
183 TEST_F(AddressNormalizerTest, StartNormalization_RulesNotLoaded_WillLoad) {
184 NormalizationDelegate delegate;
185 AutofillProfile profile;
186
187 // Start the normalization.
188 normalizer_->StartAddressNormalization(profile, kLocale, 0, &delegate);
189
190 // Even if the rules are not loaded before the call to
191 // StartAddressNormalization, they should get loaded in the call. Since our
192 // test source is synchronous, the normalization will happen synchronously
193 // too.
194 EXPECT_TRUE(normalizer_->AreRulesLoadedForRegion(kLocale));
195 EXPECT_TRUE(delegate.normalized_called());
196 EXPECT_FALSE(delegate.not_normalized_called());
197 }
198
199 // Tests that the phone number is formatted when the address is normalized.
200 TEST_F(AddressNormalizerTest, FormatPhone_AddressNormalized) {
201 NormalizationDelegate delegate;
202 AutofillProfile profile;
203 profile.SetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER,
204 base::UTF8ToUTF16("(515) 123-1234"));
205
206 // Load the rules.
207 normalizer_->LoadRulesForRegion(kLocale);
208 EXPECT_TRUE(normalizer_->AreRulesLoadedForRegion(kLocale));
209
210 // Start the normalization.
211 normalizer_->StartAddressNormalization(profile, kLocale, 0, &delegate);
212
213 // Make sure the address was normalized.
214 EXPECT_TRUE(delegate.normalized_called());
215
216 // Expect that the phone number was formatted.
217 EXPECT_EQ("+15151231234", base::UTF16ToUTF8(delegate.profile().GetRawInfo(
218 autofill::PHONE_HOME_WHOLE_NUMBER)));
219 }
220
221 // Tests that the phone number is formatted even when the address is not
222 // normalized.
223 TEST_F(AddressNormalizerTest, FormatPhone_AddressNotNormalized) {
224 NormalizationDelegate delegate;
225 AutofillProfile profile;
226 profile.SetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER,
227 base::UTF8ToUTF16("515-123-1234"));
228
229 // Make sure the rules will not be loaded in the StartAddressNormalization
230 // call.
231 normalizer_->ShouldLoadRules(false);
232
233 // Start the normalization.
234 normalizer_->StartAddressNormalization(profile, kLocale, 0, &delegate);
235
236 // Let the timeout execute.
237 base::RunLoop().RunUntilIdle();
238
239 // Make sure the address was not normalized.
240 EXPECT_TRUE(delegate.not_normalized_called());
241
242 // Expect that the phone number was formatted.
243 EXPECT_EQ("+15151231234", base::UTF16ToUTF8(delegate.profile().GetRawInfo(
244 autofill::PHONE_HOME_WHOLE_NUMBER)));
245 }
246
247 } // namespace payments
OLDNEW
« no previous file with comments | « components/payments/core/address_normalizer_impl_unittest.cc ('k') | components/payments/core/payment_request_delegate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698