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