Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 | |
|
please use gerrit instead
2017/02/21 17:15:16
nit: no newline.
sebsg
2017/02/21 18:30:59
Done.
| |
| 20 namespace { | |
| 21 | |
| 22 using ::i18n::addressinput::NullStorage; | |
| 23 using ::i18n::addressinput::Source; | |
| 24 using ::i18n::addressinput::Storage; | |
| 25 using ::i18n::addressinput::TestdataSource; | |
|
please use gerrit instead
2017/02/21 17:15:14
Somone's been reading up on C++, I see. Good job!
sebsg
2017/02/21 18:30:59
Thanks :)
| |
| 26 | |
| 27 // The requester of normalization for this test. | |
| 28 class NormalizationDelegate : public AddressNormalizer::Delegate { | |
| 29 public: | |
| 30 NormalizationDelegate() | |
| 31 : normalized_called_(false), not_normalized_called_(false){}; | |
| 32 | |
|
please use gerrit instead
2017/02/21 17:15:16
Need to override the destructor.
sebsg
2017/02/21 18:31:00
Done.
| |
| 33 void OnAddressNormalized( | |
| 34 const autofill::AutofillProfile& normalized_profile) override { | |
| 35 normalized_called_ = true; | |
| 36 } | |
| 37 | |
| 38 void OnCouldNotNormalize(const autofill::AutofillProfile& profile) override { | |
| 39 not_normalized_called_ = true; | |
| 40 } | |
| 41 | |
| 42 bool NormalizedCalled() { return normalized_called_; } | |
|
please use gerrit instead
2017/02/21 17:15:16
nit: hacker_case for simple accessor.
sebsg
2017/02/21 18:31:00
Done.
| |
| 43 | |
| 44 bool NotNormalizedCalled() { return not_normalized_called_; } | |
|
please use gerrit instead
2017/02/21 17:15:15
ditto.
sebsg
2017/02/21 18:30:59
Done.
| |
| 45 | |
| 46 private: | |
| 47 bool normalized_called_; | |
| 48 bool not_normalized_called_; | |
| 49 }; | |
| 50 | |
| 51 // Used to load region rules for this test. | |
| 52 class ChromiumTestdataSource : public TestdataSource { | |
|
please use gerrit instead
2017/02/21 17:15:18
Why not use TestdataSource as-is? One caveat is th
Mathieu
2017/02/21 17:40:43
We could try, but I'm pretty sure this won't work
sebsg
2017/02/21 18:30:59
Acknowledged.
sebsg
2017/02/21 18:31:00
Acknowledged.
| |
| 53 public: | |
| 54 ChromiumTestdataSource() : TestdataSource(true) {} | |
| 55 | |
| 56 // For this test, only load the rules for the "US". | |
| 57 void Get(const std::string& key, const Callback& data_ready) const override { | |
| 58 data_ready( | |
| 59 true, key, | |
| 60 new std::string("{\"data/US\": " | |
| 61 "{\"id\":\"data/US\",\"key\":\"US\",\"name\":\"UNITED " | |
| 62 "STATES\",\"lang\":\"en\",\"languages\":\"en\"}}")); | |
| 63 } | |
| 64 }; | |
| 65 | |
| 66 // A test subclass of the AddressNormalizer. Used to simulate rules not being | |
| 67 // loaded. | |
| 68 class TestAddressNormalizer : public AddressNormalizer { | |
| 69 public: | |
| 70 TestAddressNormalizer(std::unique_ptr<i18n::addressinput::Source> source, | |
| 71 std::unique_ptr<i18n::addressinput::Storage> storage) | |
| 72 : AddressNormalizer(std::move(source), std::move(storage)), | |
| 73 should_load_rules_(true) {} | |
|
please use gerrit instead
2017/02/21 17:15:13
Need to override the destructor.
sebsg
2017/02/21 18:31:00
Done.
| |
| 74 | |
| 75 void ShouldLoadRules(bool should_load_rules) { | |
| 76 should_load_rules_ = should_load_rules; | |
| 77 } | |
| 78 | |
| 79 void LoadRulesForRegion(const std::string& region_code) override { | |
| 80 if (should_load_rules_) { | |
| 81 AddressNormalizer::LoadRulesForRegion(region_code); | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 private: | |
| 86 bool should_load_rules_; | |
| 87 }; | |
| 88 | |
| 89 } // namespace | |
| 90 | |
| 91 class AddressNormalizerTest : public testing::Test { | |
| 92 protected: | |
| 93 AddressNormalizerTest() | |
| 94 : normalizer_(new TestAddressNormalizer( | |
| 95 std::unique_ptr<Source>(new ChromiumTestdataSource), | |
| 96 std::unique_ptr<Storage>(new NullStorage))) {} | |
| 97 | |
| 98 ~AddressNormalizerTest() override {} | |
| 99 | |
| 100 const std::unique_ptr<TestAddressNormalizer> normalizer_; | |
| 101 | |
| 102 base::test::ScopedTaskScheduler scoped_task_scheduler_; | |
| 103 | |
| 104 DISALLOW_COPY_AND_ASSIGN(AddressNormalizerTest); | |
|
please use gerrit instead
2017/02/21 17:15:19
This works only in a "private:" section.
sebsg
2017/02/21 18:30:59
Done.
| |
| 105 }; | |
| 106 | |
| 107 // Tests that rules are not loaded by default. | |
| 108 TEST_F(AddressNormalizerTest, AreRulesLoadedForRegion_NotLoaded) { | |
| 109 EXPECT_FALSE(normalizer_->AreRulesLoadedForRegion("US")); | |
| 110 } | |
| 111 | |
| 112 // Tests that the rules are loaded correctly. | |
| 113 TEST_F(AddressNormalizerTest, AreRulesLoadedForRegion_Loaded) { | |
| 114 normalizer_->LoadRulesForRegion("US"); | |
| 115 EXPECT_TRUE(normalizer_->AreRulesLoadedForRegion("US")); | |
| 116 } | |
| 117 | |
| 118 // Tests that if the rules are loaded before the normalization is started, the | |
| 119 // normalized profile will be returned to the delegate synchronously. | |
| 120 TEST_F(AddressNormalizerTest, StartNormalization_RulesLoaded) { | |
| 121 NormalizationDelegate delegate; | |
| 122 AutofillProfile profile; | |
| 123 | |
| 124 // Load the rules. | |
| 125 normalizer_->LoadRulesForRegion("US"); | |
| 126 EXPECT_TRUE(normalizer_->AreRulesLoadedForRegion("US")); | |
| 127 | |
| 128 // Start the normalization. | |
| 129 normalizer_->StartAddressNormalization(profile, "US", 0, &delegate); | |
| 130 | |
| 131 // Since the rules are already loaded, the address should be normalized | |
| 132 // synchronously. | |
| 133 EXPECT_TRUE(delegate.NormalizedCalled()); | |
| 134 EXPECT_FALSE(delegate.NotNormalizedCalled()); | |
| 135 } | |
| 136 | |
| 137 // Tests that if the rules are not loaded before the normalization and cannot be | |
| 138 // loaded after, the address will not be normalized and the delegate will be | |
| 139 // notified. | |
| 140 TEST_F(AddressNormalizerTest, StartNormalization_RulesNotLoaded_WillNotLoad) { | |
| 141 NormalizationDelegate delegate; | |
| 142 AutofillProfile profile; | |
| 143 | |
| 144 // Make sure the rules will not be loaded in the StartAddressNormalization | |
| 145 // call. | |
| 146 normalizer_->ShouldLoadRules(false); | |
| 147 | |
| 148 // Start the normalization. | |
| 149 normalizer_->StartAddressNormalization(profile, "US", 0, &delegate); | |
| 150 | |
| 151 // Let the timeout execute. | |
| 152 base::RunLoop().RunUntilIdle(); | |
| 153 | |
| 154 // Since the rules are never loaded and the timeout is 0, the delegate should | |
| 155 // get notified that the address could not be normalized. | |
| 156 EXPECT_FALSE(delegate.NormalizedCalled()); | |
| 157 EXPECT_TRUE(delegate.NotNormalizedCalled()); | |
| 158 } | |
| 159 | |
| 160 // Tests that if the rules are not loaded before the call to | |
| 161 // StartAddressNormalization, they will be loaded in the call. | |
| 162 TEST_F(AddressNormalizerTest, StartNormalization_RulesNotLoaded_WillLoad) { | |
| 163 NormalizationDelegate delegate; | |
| 164 AutofillProfile profile; | |
| 165 | |
| 166 // Start the normalization. | |
| 167 normalizer_->StartAddressNormalization(profile, "US", 0, &delegate); | |
| 168 | |
| 169 // Even if the rules are not loaded before the call to | |
| 170 // StartAddressNormalization, they should get loaded in the call. Since our | |
| 171 // test source is synchronous, the normalization will happen synchronously | |
| 172 // too. | |
| 173 EXPECT_TRUE(normalizer_->AreRulesLoadedForRegion("US")); | |
| 174 EXPECT_TRUE(delegate.NormalizedCalled()); | |
| 175 EXPECT_FALSE(delegate.NotNormalizedCalled()); | |
| 176 } | |
| 177 | |
| 178 } // namespace payments | |
| OLD | NEW |