| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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 "components/payments/core/address_normalizer.h" | 5 #include "components/payments/core/address_normalizer.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "base/test/scoped_task_scheduler.h" | 11 #include "base/test/scoped_task_scheduler.h" |
| 12 #include "components/autofill/core/browser/autofill_profile.h" | 12 #include "components/autofill/core/browser/autofill_profile.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/null_stora
ge.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" | 15 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h" |
| 16 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h" | 16 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h" |
| 17 #include "third_party/libaddressinput/src/cpp/test/testdata_source.h" | 17 #include "third_party/libaddressinput/src/cpp/test/testdata_source.h" |
| 18 | 18 |
| 19 namespace payments { | 19 namespace payments { |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 using ::autofill::AutofillProfile; | 22 using ::autofill::AutofillProfile; |
| 23 using ::i18n::addressinput::NullStorage; | 23 using ::i18n::addressinput::NullStorage; |
| 24 using ::i18n::addressinput::Source; | 24 using ::i18n::addressinput::Source; |
| 25 using ::i18n::addressinput::Storage; | 25 using ::i18n::addressinput::Storage; |
| 26 using ::i18n::addressinput::TestdataSource; | 26 using ::i18n::addressinput::TestdataSource; |
| 27 | 27 |
| 28 const char kLocale[] = "US"; |
| 29 |
| 28 // The requester of normalization for this test. | 30 // The requester of normalization for this test. |
| 29 class NormalizationDelegate : public AddressNormalizer::Delegate { | 31 class NormalizationDelegate : public AddressNormalizer::Delegate { |
| 30 public: | 32 public: |
| 31 NormalizationDelegate() | 33 NormalizationDelegate() |
| 32 : normalized_called_(false), not_normalized_called_(false) {} | 34 : normalized_called_(false), not_normalized_called_(false) {} |
| 33 | 35 |
| 34 ~NormalizationDelegate() override {} | 36 ~NormalizationDelegate() override {} |
| 35 | 37 |
| 36 void OnAddressNormalized( | 38 void OnAddressNormalized(const autofill::AutofillProfile& profile) override { |
| 37 const autofill::AutofillProfile& normalized_profile) override { | |
| 38 normalized_called_ = true; | 39 normalized_called_ = true; |
| 40 profile_ = profile; |
| 39 } | 41 } |
| 40 | 42 |
| 41 void OnCouldNotNormalize(const autofill::AutofillProfile& profile) override { | 43 void OnCouldNotNormalize(const autofill::AutofillProfile& profile) override { |
| 42 not_normalized_called_ = true; | 44 not_normalized_called_ = true; |
| 45 profile_ = profile; |
| 43 } | 46 } |
| 44 | 47 |
| 45 bool normalized_called() { return normalized_called_; } | 48 bool normalized_called() const { return normalized_called_; } |
| 46 | 49 |
| 47 bool not_normalized_called() { return not_normalized_called_; } | 50 bool not_normalized_called() const { return not_normalized_called_; } |
| 51 |
| 52 const AutofillProfile& profile() const { return profile_; } |
| 48 | 53 |
| 49 private: | 54 private: |
| 50 bool normalized_called_; | 55 bool normalized_called_; |
| 51 bool not_normalized_called_; | 56 bool not_normalized_called_; |
| 57 AutofillProfile profile_; |
| 52 | 58 |
| 53 DISALLOW_COPY_AND_ASSIGN(NormalizationDelegate); | 59 DISALLOW_COPY_AND_ASSIGN(NormalizationDelegate); |
| 54 }; | 60 }; |
| 55 | 61 |
| 56 // Used to load region rules for this test. | 62 // Used to load region rules for this test. |
| 57 class ChromiumTestdataSource : public TestdataSource { | 63 class ChromiumTestdataSource : public TestdataSource { |
| 58 public: | 64 public: |
| 59 ChromiumTestdataSource() : TestdataSource(true) {} | 65 ChromiumTestdataSource() : TestdataSource(true) {} |
| 60 | 66 |
| 61 ~ChromiumTestdataSource() override {} | 67 ~ChromiumTestdataSource() override {} |
| 62 | 68 |
| 63 // For this test, only load the rules for the "US". | 69 // For this test, only load the rules for the kLocale. |
| 64 void Get(const std::string& key, const Callback& data_ready) const override { | 70 void Get(const std::string& key, const Callback& data_ready) const override { |
| 65 data_ready( | 71 data_ready( |
| 66 true, key, | 72 true, key, |
| 67 new std::string("{\"data/US\": " | 73 new std::string("{\"data/US\": " |
| 68 "{\"id\":\"data/US\",\"key\":\"US\",\"name\":\"UNITED " | 74 "{\"id\":\"data/US\",\"key\":\"US\",\"name\":\"UNITED " |
| 69 "STATES\",\"lang\":\"en\",\"languages\":\"en\"}}")); | 75 "STATES\",\"lang\":\"en\",\"languages\":\"en\"}}")); |
| 70 } | 76 } |
| 71 | 77 |
| 72 private: | 78 private: |
| 73 DISALLOW_COPY_AND_ASSIGN(ChromiumTestdataSource); | 79 DISALLOW_COPY_AND_ASSIGN(ChromiumTestdataSource); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 const std::unique_ptr<TestAddressNormalizer> normalizer_; | 120 const std::unique_ptr<TestAddressNormalizer> normalizer_; |
| 115 | 121 |
| 116 base::test::ScopedTaskScheduler scoped_task_scheduler_; | 122 base::test::ScopedTaskScheduler scoped_task_scheduler_; |
| 117 | 123 |
| 118 private: | 124 private: |
| 119 DISALLOW_COPY_AND_ASSIGN(AddressNormalizerTest); | 125 DISALLOW_COPY_AND_ASSIGN(AddressNormalizerTest); |
| 120 }; | 126 }; |
| 121 | 127 |
| 122 // Tests that rules are not loaded by default. | 128 // Tests that rules are not loaded by default. |
| 123 TEST_F(AddressNormalizerTest, AreRulesLoadedForRegion_NotLoaded) { | 129 TEST_F(AddressNormalizerTest, AreRulesLoadedForRegion_NotLoaded) { |
| 124 EXPECT_FALSE(normalizer_->AreRulesLoadedForRegion("US")); | 130 EXPECT_FALSE(normalizer_->AreRulesLoadedForRegion(kLocale)); |
| 125 } | 131 } |
| 126 | 132 |
| 127 // Tests that the rules are loaded correctly. | 133 // Tests that the rules are loaded correctly. |
| 128 TEST_F(AddressNormalizerTest, AreRulesLoadedForRegion_Loaded) { | 134 TEST_F(AddressNormalizerTest, AreRulesLoadedForRegion_Loaded) { |
| 129 normalizer_->LoadRulesForRegion("US"); | 135 normalizer_->LoadRulesForRegion(kLocale); |
| 130 EXPECT_TRUE(normalizer_->AreRulesLoadedForRegion("US")); | 136 EXPECT_TRUE(normalizer_->AreRulesLoadedForRegion(kLocale)); |
| 131 } | 137 } |
| 132 | 138 |
| 133 // Tests that if the rules are loaded before the normalization is started, the | 139 // Tests that if the rules are loaded before the normalization is started, the |
| 134 // normalized profile will be returned to the delegate synchronously. | 140 // normalized profile will be returned to the delegate synchronously. |
| 135 TEST_F(AddressNormalizerTest, StartNormalization_RulesLoaded) { | 141 TEST_F(AddressNormalizerTest, StartNormalization_RulesLoaded) { |
| 136 NormalizationDelegate delegate; | 142 NormalizationDelegate delegate; |
| 137 AutofillProfile profile; | 143 AutofillProfile profile; |
| 138 | 144 |
| 139 // Load the rules. | 145 // Load the rules. |
| 140 normalizer_->LoadRulesForRegion("US"); | 146 normalizer_->LoadRulesForRegion(kLocale); |
| 141 EXPECT_TRUE(normalizer_->AreRulesLoadedForRegion("US")); | 147 EXPECT_TRUE(normalizer_->AreRulesLoadedForRegion(kLocale)); |
| 142 | 148 |
| 143 // Start the normalization. | 149 // Start the normalization. |
| 144 normalizer_->StartAddressNormalization(profile, "US", 0, &delegate); | 150 normalizer_->StartAddressNormalization(profile, kLocale, 0, &delegate); |
| 145 | 151 |
| 146 // Since the rules are already loaded, the address should be normalized | 152 // Since the rules are already loaded, the address should be normalized |
| 147 // synchronously. | 153 // synchronously. |
| 148 EXPECT_TRUE(delegate.normalized_called()); | 154 EXPECT_TRUE(delegate.normalized_called()); |
| 149 EXPECT_FALSE(delegate.not_normalized_called()); | 155 EXPECT_FALSE(delegate.not_normalized_called()); |
| 150 } | 156 } |
| 151 | 157 |
| 152 // Tests that if the rules are not loaded before the normalization and cannot be | 158 // Tests that if the rules are not loaded before the normalization and cannot be |
| 153 // loaded after, the address will not be normalized and the delegate will be | 159 // loaded after, the address will not be normalized and the delegate will be |
| 154 // notified. | 160 // notified. |
| 155 TEST_F(AddressNormalizerTest, StartNormalization_RulesNotLoaded_WillNotLoad) { | 161 TEST_F(AddressNormalizerTest, StartNormalization_RulesNotLoaded_WillNotLoad) { |
| 156 NormalizationDelegate delegate; | 162 NormalizationDelegate delegate; |
| 157 AutofillProfile profile; | 163 AutofillProfile profile; |
| 158 | 164 |
| 159 // Make sure the rules will not be loaded in the StartAddressNormalization | 165 // Make sure the rules will not be loaded in the StartAddressNormalization |
| 160 // call. | 166 // call. |
| 161 normalizer_->ShouldLoadRules(false); | 167 normalizer_->ShouldLoadRules(false); |
| 162 | 168 |
| 163 // Start the normalization. | 169 // Start the normalization. |
| 164 normalizer_->StartAddressNormalization(profile, "US", 0, &delegate); | 170 normalizer_->StartAddressNormalization(profile, kLocale, 0, &delegate); |
| 165 | 171 |
| 166 // Let the timeout execute. | 172 // Let the timeout execute. |
| 167 base::RunLoop().RunUntilIdle(); | 173 base::RunLoop().RunUntilIdle(); |
| 168 | 174 |
| 169 // Since the rules are never loaded and the timeout is 0, the delegate should | 175 // Since the rules are never loaded and the timeout is 0, the delegate should |
| 170 // get notified that the address could not be normalized. | 176 // get notified that the address could not be normalized. |
| 171 EXPECT_FALSE(delegate.normalized_called()); | 177 EXPECT_FALSE(delegate.normalized_called()); |
| 172 EXPECT_TRUE(delegate.not_normalized_called()); | 178 EXPECT_TRUE(delegate.not_normalized_called()); |
| 173 } | 179 } |
| 174 | 180 |
| 175 // Tests that if the rules are not loaded before the call to | 181 // Tests that if the rules are not loaded before the call to |
| 176 // StartAddressNormalization, they will be loaded in the call. | 182 // StartAddressNormalization, they will be loaded in the call. |
| 177 TEST_F(AddressNormalizerTest, StartNormalization_RulesNotLoaded_WillLoad) { | 183 TEST_F(AddressNormalizerTest, StartNormalization_RulesNotLoaded_WillLoad) { |
| 178 NormalizationDelegate delegate; | 184 NormalizationDelegate delegate; |
| 179 AutofillProfile profile; | 185 AutofillProfile profile; |
| 180 | 186 |
| 181 // Start the normalization. | 187 // Start the normalization. |
| 182 normalizer_->StartAddressNormalization(profile, "US", 0, &delegate); | 188 normalizer_->StartAddressNormalization(profile, kLocale, 0, &delegate); |
| 183 | 189 |
| 184 // Even if the rules are not loaded before the call to | 190 // Even if the rules are not loaded before the call to |
| 185 // StartAddressNormalization, they should get loaded in the call. Since our | 191 // StartAddressNormalization, they should get loaded in the call. Since our |
| 186 // test source is synchronous, the normalization will happen synchronously | 192 // test source is synchronous, the normalization will happen synchronously |
| 187 // too. | 193 // too. |
| 188 EXPECT_TRUE(normalizer_->AreRulesLoadedForRegion("US")); | 194 EXPECT_TRUE(normalizer_->AreRulesLoadedForRegion(kLocale)); |
| 189 EXPECT_TRUE(delegate.normalized_called()); | 195 EXPECT_TRUE(delegate.normalized_called()); |
| 190 EXPECT_FALSE(delegate.not_normalized_called()); | 196 EXPECT_FALSE(delegate.not_normalized_called()); |
| 191 } | 197 } |
| 192 | 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 |
| 193 } // namespace payments | 247 } // namespace payments |
| OLD | NEW |