| 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/address_normalizer.h" | 5 #include "components/payments/core/address_normalizer.h" |
| 6 | 6 |
| 7 #include "base/macros.h" | |
| 8 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 10 #include "base/test/scoped_task_scheduler.h" | 9 #include "base/test/scoped_task_scheduler.h" |
| 11 #include "components/autofill/core/browser/autofill_profile.h" | 10 #include "components/autofill/core/browser/autofill_profile.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/null_stora
ge.h" | 12 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/null_stora
ge.h" |
| 14 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h" | 13 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h" |
| 15 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h" | 14 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h" |
| 16 #include "third_party/libaddressinput/src/cpp/test/testdata_source.h" | 15 #include "third_party/libaddressinput/src/cpp/test/testdata_source.h" |
| 17 | 16 |
| 18 namespace payments { | 17 namespace payments { |
| 19 namespace { | 18 namespace { |
| 20 | 19 |
| 20 using ::autofill::AutofillProfile; |
| 21 using ::i18n::addressinput::NullStorage; | 21 using ::i18n::addressinput::NullStorage; |
| 22 using ::i18n::addressinput::Source; | 22 using ::i18n::addressinput::Source; |
| 23 using ::i18n::addressinput::Storage; | 23 using ::i18n::addressinput::Storage; |
| 24 using ::i18n::addressinput::TestdataSource; | 24 using ::i18n::addressinput::TestdataSource; |
| 25 | 25 |
| 26 // The requester of normalization for this test. | 26 // The requester of normalization for this test. |
| 27 class NormalizationDelegate : public AddressNormalizer::Delegate { | 27 class NormalizationDelegate : public AddressNormalizer::Delegate { |
| 28 public: | 28 public: |
| 29 NormalizationDelegate() | 29 NormalizationDelegate() |
| 30 : normalized_called_(false), not_normalized_called_(false) {} | 30 : normalized_called_(false), not_normalized_called_(false) {} |
| 31 | 31 |
| 32 ~NormalizationDelegate() override {} | 32 ~NormalizationDelegate() override {} |
| 33 | 33 |
| 34 void OnAddressNormalized( | 34 void OnAddressNormalized( |
| 35 const autofill::AutofillProfile& normalized_profile) override { | 35 const autofill::AutofillProfile& normalized_profile) override { |
| 36 normalized_called_ = true; | 36 normalized_called_ = true; |
| 37 } | 37 } |
| 38 | 38 |
| 39 void OnCouldNotNormalize(const autofill::AutofillProfile& profile) override { | 39 void OnCouldNotNormalize(const autofill::AutofillProfile& profile) override { |
| 40 not_normalized_called_ = true; | 40 not_normalized_called_ = true; |
| 41 } | 41 } |
| 42 | 42 |
| 43 bool normalized_called() { return normalized_called_; } | 43 bool normalized_called() { return normalized_called_; } |
| 44 | 44 |
| 45 bool not_normalized_called() { return not_normalized_called_; } | 45 bool not_normalized_called() { return not_normalized_called_; } |
| 46 | 46 |
| 47 private: | 47 private: |
| 48 bool normalized_called_; | 48 bool normalized_called_; |
| 49 bool not_normalized_called_; | 49 bool not_normalized_called_; |
| 50 |
| 51 DISALLOW_COPY_AND_ASSIGN(NormalizationDelegate); |
| 50 }; | 52 }; |
| 51 | 53 |
| 52 // Used to load region rules for this test. | 54 // Used to load region rules for this test. |
| 53 class ChromiumTestdataSource : public TestdataSource { | 55 class ChromiumTestdataSource : public TestdataSource { |
| 54 public: | 56 public: |
| 55 ChromiumTestdataSource() : TestdataSource(true) {} | 57 ChromiumTestdataSource() : TestdataSource(true) {} |
| 56 | 58 |
| 57 ~ChromiumTestdataSource() override {} | 59 ~ChromiumTestdataSource() override {} |
| 58 | 60 |
| 59 // For this test, only load the rules for the "US". | 61 // For this test, only load the rules for the "US". |
| 60 void Get(const std::string& key, const Callback& data_ready) const override { | 62 void Get(const std::string& key, const Callback& data_ready) const override { |
| 61 data_ready( | 63 data_ready( |
| 62 true, key, | 64 true, key, |
| 63 new std::string("{\"data/US\": " | 65 new std::string("{\"data/US\": " |
| 64 "{\"id\":\"data/US\",\"key\":\"US\",\"name\":\"UNITED " | 66 "{\"id\":\"data/US\",\"key\":\"US\",\"name\":\"UNITED " |
| 65 "STATES\",\"lang\":\"en\",\"languages\":\"en\"}}")); | 67 "STATES\",\"lang\":\"en\",\"languages\":\"en\"}}")); |
| 66 } | 68 } |
| 69 |
| 70 private: |
| 71 DISALLOW_COPY_AND_ASSIGN(ChromiumTestdataSource); |
| 67 }; | 72 }; |
| 68 | 73 |
| 69 // A test subclass of the AddressNormalizer. Used to simulate rules not being | 74 // A test subclass of the AddressNormalizer. Used to simulate rules not being |
| 70 // loaded. | 75 // loaded. |
| 71 class TestAddressNormalizer : public AddressNormalizer { | 76 class TestAddressNormalizer : public AddressNormalizer { |
| 72 public: | 77 public: |
| 73 TestAddressNormalizer(std::unique_ptr<i18n::addressinput::Source> source, | 78 TestAddressNormalizer(std::unique_ptr<i18n::addressinput::Source> source, |
| 74 std::unique_ptr<i18n::addressinput::Storage> storage) | 79 std::unique_ptr<i18n::addressinput::Storage> storage) |
| 75 : AddressNormalizer(std::move(source), std::move(storage)), | 80 : AddressNormalizer(std::move(source), std::move(storage)), |
| 76 should_load_rules_(true) {} | 81 should_load_rules_(true) {} |
| 77 | 82 |
| 78 ~TestAddressNormalizer() override {} | 83 ~TestAddressNormalizer() override {} |
| 79 | 84 |
| 80 void ShouldLoadRules(bool should_load_rules) { | 85 void ShouldLoadRules(bool should_load_rules) { |
| 81 should_load_rules_ = should_load_rules; | 86 should_load_rules_ = should_load_rules; |
| 82 } | 87 } |
| 83 | 88 |
| 84 void LoadRulesForRegion(const std::string& region_code) override { | 89 void LoadRulesForRegion(const std::string& region_code) override { |
| 85 if (should_load_rules_) { | 90 if (should_load_rules_) { |
| 86 AddressNormalizer::LoadRulesForRegion(region_code); | 91 AddressNormalizer::LoadRulesForRegion(region_code); |
| 87 } | 92 } |
| 88 } | 93 } |
| 89 | 94 |
| 90 private: | 95 private: |
| 91 bool should_load_rules_; | 96 bool should_load_rules_; |
| 97 |
| 98 DISALLOW_COPY_AND_ASSIGN(TestAddressNormalizer); |
| 92 }; | 99 }; |
| 93 | 100 |
| 94 } // namespace | 101 } // namespace |
| 95 | 102 |
| 96 class AddressNormalizerTest : public testing::Test { | 103 class AddressNormalizerTest : public testing::Test { |
| 97 protected: | 104 protected: |
| 98 AddressNormalizerTest() | 105 AddressNormalizerTest() |
| 99 : normalizer_(new TestAddressNormalizer( | 106 : normalizer_(new TestAddressNormalizer( |
| 100 std::unique_ptr<Source>(new ChromiumTestdataSource), | 107 std::unique_ptr<Source>(new ChromiumTestdataSource), |
| 101 std::unique_ptr<Storage>(new NullStorage))) {} | 108 std::unique_ptr<Storage>(new NullStorage))) {} |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 // Even if the rules are not loaded before the call to | 182 // Even if the rules are not loaded before the call to |
| 176 // StartAddressNormalization, they should get loaded in the call. Since our | 183 // StartAddressNormalization, they should get loaded in the call. Since our |
| 177 // test source is synchronous, the normalization will happen synchronously | 184 // test source is synchronous, the normalization will happen synchronously |
| 178 // too. | 185 // too. |
| 179 EXPECT_TRUE(normalizer_->AreRulesLoadedForRegion("US")); | 186 EXPECT_TRUE(normalizer_->AreRulesLoadedForRegion("US")); |
| 180 EXPECT_TRUE(delegate.normalized_called()); | 187 EXPECT_TRUE(delegate.normalized_called()); |
| 181 EXPECT_FALSE(delegate.not_normalized_called()); | 188 EXPECT_FALSE(delegate.not_normalized_called()); |
| 182 } | 189 } |
| 183 | 190 |
| 184 } // namespace payments | 191 } // namespace payments |
| OLD | NEW |