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/core/subkey_requester.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 "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 const char kLocale[] = "OZ"; |
| 27 const int kInvalidSize = -1; |
| 28 const int kCorrectSize = 2; // for subkeys = Do, Re |
| 29 const int kEmptySize = 0; |
| 30 |
| 31 // The requester of normalization for this test. |
| 32 class RequestDelegate : public SubKeyRequester::Delegate { |
| 33 public: |
| 34 RequestDelegate() : subkeys_size_(kInvalidSize) {} |
| 35 |
| 36 ~RequestDelegate() override {} |
| 37 |
| 38 void OnSubKeysReceived(std::vector<std::string> subkeys) override { |
| 39 subkeys_size_ = subkeys.size(); |
| 40 } |
| 41 |
| 42 int subkeys_size() const { return subkeys_size_; } |
| 43 |
| 44 private: |
| 45 int subkeys_size_; |
| 46 |
| 47 DISALLOW_COPY_AND_ASSIGN(RequestDelegate); |
| 48 }; |
| 49 |
| 50 // Used to load region rules for this test. |
| 51 class ChromiumTestdataSource : public TestdataSource { |
| 52 public: |
| 53 ChromiumTestdataSource() : TestdataSource(true) {} |
| 54 |
| 55 ~ChromiumTestdataSource() override {} |
| 56 |
| 57 // For this test, only load the rules for the kLocale. |
| 58 void Get(const std::string& key, const Callback& data_ready) const override { |
| 59 data_ready( |
| 60 true, key, |
| 61 new std::string( |
| 62 "{\"data/OZ\": " |
| 63 "{\"id\":\"data/OZ\",\"key\":\"OZ\",\"name\":\"Oz \", " |
| 64 "\"lang\":\"en\",\"languages\":\"en\",\"sub_keys\":\"DO~Re\"}}")); |
| 65 } |
| 66 |
| 67 private: |
| 68 DISALLOW_COPY_AND_ASSIGN(ChromiumTestdataSource); |
| 69 }; |
| 70 |
| 71 // A test subclass of the SubKeyRequesterImpl. Used to simulate rules not |
| 72 // being loaded. |
| 73 class TestSubKeyRequester : public SubKeyRequester { |
| 74 public: |
| 75 TestSubKeyRequester(std::unique_ptr<::i18n::addressinput::Source> source, |
| 76 std::unique_ptr<::i18n::addressinput::Storage> storage) |
| 77 : SubKeyRequester(std::move(source), std::move(storage)), |
| 78 should_load_rules_(true) {} |
| 79 |
| 80 ~TestSubKeyRequester() override {} |
| 81 |
| 82 void ShouldLoadRules(bool should_load_rules) { |
| 83 should_load_rules_ = should_load_rules; |
| 84 } |
| 85 |
| 86 void LoadRulesForRegion(const std::string& region_code) override { |
| 87 if (should_load_rules_) { |
| 88 SubKeyRequester::LoadRulesForRegion(region_code); |
| 89 } |
| 90 } |
| 91 |
| 92 private: |
| 93 bool should_load_rules_; |
| 94 base::test::ScopedTaskScheduler scoped_task_scheduler_; |
| 95 |
| 96 DISALLOW_COPY_AND_ASSIGN(TestSubKeyRequester); |
| 97 }; |
| 98 |
| 99 } // namespace |
| 100 |
| 101 class SubKeyRequesterTest : public testing::Test { |
| 102 protected: |
| 103 SubKeyRequesterTest() |
| 104 : requester_(new TestSubKeyRequester( |
| 105 std::unique_ptr<Source>(new ChromiumTestdataSource), |
| 106 std::unique_ptr<Storage>(new NullStorage))) {} |
| 107 |
| 108 ~SubKeyRequesterTest() override {} |
| 109 |
| 110 const std::unique_ptr<TestSubKeyRequester> requester_; |
| 111 |
| 112 private: |
| 113 DISALLOW_COPY_AND_ASSIGN(SubKeyRequesterTest); |
| 114 }; |
| 115 |
| 116 // Tests that rules are not loaded by default. |
| 117 TEST_F(SubKeyRequesterTest, AreRulesLoadedForRegion_NotLoaded) { |
| 118 EXPECT_FALSE(requester_->AreRulesLoadedForRegion(kLocale)); |
| 119 } |
| 120 |
| 121 // Tests that the rules are loaded correctly. |
| 122 TEST_F(SubKeyRequesterTest, AreRulesLoadedForRegion_Loaded) { |
| 123 requester_->LoadRulesForRegion(kLocale); |
| 124 EXPECT_TRUE(requester_->AreRulesLoadedForRegion(kLocale)); |
| 125 } |
| 126 |
| 127 // Tests that if the rules are loaded before the subkey request is started, the |
| 128 // received subkeys will be returned to the delegate synchronously. |
| 129 TEST_F(SubKeyRequesterTest, StartRequest_RulesLoaded) { |
| 130 RequestDelegate delegate; |
| 131 |
| 132 // Load the rules. |
| 133 requester_->LoadRulesForRegion(kLocale); |
| 134 EXPECT_TRUE(requester_->AreRulesLoadedForRegion(kLocale)); |
| 135 |
| 136 // Start the request. |
| 137 requester_->StartRegionSubKeysRequest(kLocale, 0, &delegate); |
| 138 |
| 139 // Since the rules are already loaded, the subkeys should be received |
| 140 // synchronously. |
| 141 EXPECT_EQ(delegate.subkeys_size(), kCorrectSize); |
| 142 } |
| 143 |
| 144 // Tests that if the rules are not loaded before the request and cannot be |
| 145 // loaded after, the subkeys will not be received and the delegate will be |
| 146 // notified. |
| 147 TEST_F(SubKeyRequesterTest, StartRequest_RulesNotLoaded_WillNotLoad) { |
| 148 RequestDelegate delegate; |
| 149 |
| 150 // Make sure the rules will not be loaded in the StartRegionSubKeysRequest |
| 151 // call. |
| 152 requester_->ShouldLoadRules(false); |
| 153 |
| 154 // Start the normalization. |
| 155 requester_->StartRegionSubKeysRequest(kLocale, 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 subkeys could not be received. |
| 162 EXPECT_EQ(delegate.subkeys_size(), kEmptySize); |
| 163 } |
| 164 |
| 165 // Tests that if the rules are not loaded before the call to |
| 166 // StartRegionSubKeysRequest, they will be loaded in the call. |
| 167 TEST_F(SubKeyRequesterTest, StartRequest_RulesNotLoaded_WillLoad) { |
| 168 RequestDelegate delegate; |
| 169 |
| 170 // Make sure the rules will not be loaded in the StartRegionSubKeysRequest |
| 171 // call. |
| 172 requester_->ShouldLoadRules(true); |
| 173 // Start the request. |
| 174 requester_->StartRegionSubKeysRequest(kLocale, 0, &delegate); |
| 175 |
| 176 // Even if the rules are not loaded before the call to |
| 177 // StartRegionSubKeysRequest, they should get loaded in the call. Since our |
| 178 // test source is synchronous, the request will happen synchronously |
| 179 // too. |
| 180 EXPECT_TRUE(requester_->AreRulesLoadedForRegion(kLocale)); |
| 181 EXPECT_EQ(delegate.subkeys_size(), kCorrectSize); |
| 182 } |
| 183 |
| 184 } // namespace payments |
OLD | NEW |