Chromium Code Reviews| 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/subkey_requester.h" | 5 #include "components/payments/core/subkey_requester.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/test/scoped_task_scheduler.h" | 12 #include "base/test/scoped_task_scheduler.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 ::i18n::addressinput::NullStorage; | 22 using ::i18n::addressinput::NullStorage; |
| 23 using ::i18n::addressinput::Source; | 23 using ::i18n::addressinput::Source; |
| 24 using ::i18n::addressinput::Storage; | 24 using ::i18n::addressinput::Storage; |
| 25 using ::i18n::addressinput::TestdataSource; | 25 using ::i18n::addressinput::TestdataSource; |
| 26 | 26 |
| 27 const char kLocale[] = "OZ"; | 27 const char kLocale[] = "OZ"; |
| 28 const char kLanguage[] = "en"; | |
| 28 const int kInvalidSize = -1; | 29 const int kInvalidSize = -1; |
| 29 const int kCorrectSize = 2; // for subkeys = Do, Re | 30 const int kCorrectSize = 2; // for subkeys = Do, Re |
| 30 const int kEmptySize = 0; | 31 const int kEmptySize = 0; |
| 31 | 32 |
| 32 class SubKeyReceiver : public base::RefCountedThreadSafe<SubKeyReceiver> { | 33 class SubKeyReceiver : public base::RefCountedThreadSafe<SubKeyReceiver> { |
| 33 public: | 34 public: |
| 34 SubKeyReceiver() : subkeys_size_(kInvalidSize) {} | 35 SubKeyReceiver() : subkeys_size_(kInvalidSize) {} |
| 35 | 36 |
| 36 void OnSubKeysReceived(const std::vector<std::string>& subkeys) { | 37 void OnSubKeysReceived(const std::vector<std::string>& subkeys_codes, |
| 37 subkeys_size_ = subkeys.size(); | 38 const std::vector<std::string>& subkeys_names) { |
| 39 subkeys_size_ = subkeys_codes.size(); | |
| 38 } | 40 } |
| 39 | 41 |
| 40 int subkeys_size() const { return subkeys_size_; } | 42 int subkeys_size() const { return subkeys_size_; } |
| 41 | 43 |
| 42 private: | 44 private: |
| 43 friend class base::RefCountedThreadSafe<SubKeyReceiver>; | 45 friend class base::RefCountedThreadSafe<SubKeyReceiver>; |
| 44 ~SubKeyReceiver() {} | 46 ~SubKeyReceiver() {} |
| 45 | 47 |
| 46 int subkeys_size_; | 48 int subkeys_size_; |
| 47 | 49 |
| 48 DISALLOW_COPY_AND_ASSIGN(SubKeyReceiver); | 50 DISALLOW_COPY_AND_ASSIGN(SubKeyReceiver); |
| 49 }; | 51 }; |
| 50 | 52 |
| 51 // Used to load region rules for this test. | 53 // Used to load region rules for this test. |
| 52 class ChromiumTestdataSource : public TestdataSource { | 54 class ChromiumTestdataSource : public TestdataSource { |
| 53 public: | 55 public: |
| 54 ChromiumTestdataSource() : TestdataSource(true) {} | 56 ChromiumTestdataSource() : TestdataSource(true) {} |
| 55 | 57 |
| 56 ~ChromiumTestdataSource() override {} | 58 ~ChromiumTestdataSource() override {} |
| 57 | 59 |
| 58 // For this test, only load the rules for the kLocale. | 60 // For this test, only load the rules for the kLocale. |
| 59 void Get(const std::string& key, const Callback& data_ready) const override { | 61 void Get(const std::string& key, const Callback& data_ready) const override { |
| 60 data_ready( | 62 data_ready( |
| 61 true, key, | 63 true, key, |
| 62 new std::string( | 64 new std::string( |
| 63 "{\"data/OZ\": " | 65 "{\"data/OZ\": " |
| 64 "{\"id\":\"data/OZ\",\"key\":\"OZ\",\"name\":\"Oz \", " | 66 "{\"id\":\"data/OZ\",\"key\":\"OZ\",\"name\":\"Oz \", " |
| 65 "\"lang\":\"en\",\"languages\":\"en\",\"sub_keys\":\"DO~Re\"}}")); | 67 "\"lang\":\"en\",\"sub_keys\":\"DO~RE\", \"sub_names\":\"Do~Re\"}," |
| 68 "\"data/OZ/DO\": " | |
| 69 "{\"id\":\"data/OZ/DO\",\"key\":\"DO\",\"name\":\"Do \", " | |
| 70 "\"lang\":\"en\"}," | |
| 71 "\"data/OZ/RE\": " | |
| 72 "{\"id\":\"data/OZ/RE\",\"key\":\"RE\",\"name\":\"Re \", " | |
| 73 "\"lang\":\"en\"}}")); | |
|
sebsg
2017/07/07 18:05:04
Could you add a second language? That way we can c
Parastoo
2017/07/10 15:40:19
It's not the subkey_requester, but the ChromeAddre
sebsg
2017/07/10 15:44:48
SG!
| |
| 66 } | 74 } |
| 67 | 75 |
| 68 private: | 76 private: |
| 69 DISALLOW_COPY_AND_ASSIGN(ChromiumTestdataSource); | 77 DISALLOW_COPY_AND_ASSIGN(ChromiumTestdataSource); |
| 70 }; | 78 }; |
| 71 | 79 |
| 72 // A test subclass of the SubKeyRequesterImpl. Used to simulate rules not | 80 // A test subclass of the SubKeyRequesterImpl. Used to simulate rules not |
| 73 // being loaded. | 81 // being loaded. |
| 74 class TestSubKeyRequester : public SubKeyRequester { | 82 class TestSubKeyRequester : public SubKeyRequester { |
| 75 public: | 83 public: |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 131 scoped_refptr<SubKeyReceiver> subkey_receiver_ = new SubKeyReceiver(); | 139 scoped_refptr<SubKeyReceiver> subkey_receiver_ = new SubKeyReceiver(); |
| 132 | 140 |
| 133 SubKeyReceiverCallback cb = | 141 SubKeyReceiverCallback cb = |
| 134 base::BindOnce(&SubKeyReceiver::OnSubKeysReceived, subkey_receiver_); | 142 base::BindOnce(&SubKeyReceiver::OnSubKeysReceived, subkey_receiver_); |
| 135 | 143 |
| 136 // Load the rules. | 144 // Load the rules. |
| 137 requester_->LoadRulesForRegion(kLocale); | 145 requester_->LoadRulesForRegion(kLocale); |
| 138 EXPECT_TRUE(requester_->AreRulesLoadedForRegion(kLocale)); | 146 EXPECT_TRUE(requester_->AreRulesLoadedForRegion(kLocale)); |
| 139 | 147 |
| 140 // Start the request. | 148 // Start the request. |
| 141 requester_->StartRegionSubKeysRequest(kLocale, 0, std::move(cb)); | 149 requester_->StartRegionSubKeysRequest(kLocale, kLanguage, 0, std::move(cb)); |
| 142 | 150 |
| 143 // Since the rules are already loaded, the subkeys should be received | 151 // Since the rules are already loaded, the subkeys should be received |
| 144 // synchronously. | 152 // synchronously. |
| 145 EXPECT_EQ(subkey_receiver_->subkeys_size(), kCorrectSize); | 153 EXPECT_EQ(subkey_receiver_->subkeys_size(), kCorrectSize); |
| 146 } | 154 } |
| 147 | 155 |
| 148 // Tests that if the rules are not loaded before the request and cannot be | 156 // 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 | 157 // loaded after, the subkeys will not be received and the delegate will be |
| 150 // notified. | 158 // notified. |
| 151 TEST_F(SubKeyRequesterTest, StartRequest_RulesNotLoaded_WillNotLoad) { | 159 TEST_F(SubKeyRequesterTest, StartRequest_RulesNotLoaded_WillNotLoad) { |
| 152 scoped_refptr<SubKeyReceiver> subkey_receiver_ = new SubKeyReceiver(); | 160 scoped_refptr<SubKeyReceiver> subkey_receiver_ = new SubKeyReceiver(); |
| 153 | 161 |
| 154 SubKeyReceiverCallback cb = | 162 SubKeyReceiverCallback cb = |
| 155 base::BindOnce(&SubKeyReceiver::OnSubKeysReceived, subkey_receiver_); | 163 base::BindOnce(&SubKeyReceiver::OnSubKeysReceived, subkey_receiver_); |
| 156 | 164 |
| 157 // Make sure the rules will not be loaded in the StartRegionSubKeysRequest | 165 // Make sure the rules will not be loaded in the StartRegionSubKeysRequest |
| 158 // call. | 166 // call. |
| 159 requester_->ShouldLoadRules(false); | 167 requester_->ShouldLoadRules(false); |
| 160 | 168 |
| 161 // Start the normalization. | 169 // Start the normalization. |
| 162 requester_->StartRegionSubKeysRequest(kLocale, 0, std::move(cb)); | 170 requester_->StartRegionSubKeysRequest(kLocale, kLanguage, 0, std::move(cb)); |
| 163 | 171 |
| 164 // Let the timeout execute. | 172 // Let the timeout execute. |
| 165 base::RunLoop().RunUntilIdle(); | 173 base::RunLoop().RunUntilIdle(); |
| 166 | 174 |
| 167 // 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 |
| 168 // get notified that the subkeys could not be received. | 176 // get notified that the subkeys could not be received. |
| 169 EXPECT_EQ(subkey_receiver_->subkeys_size(), kEmptySize); | 177 EXPECT_EQ(subkey_receiver_->subkeys_size(), kEmptySize); |
| 170 } | 178 } |
| 171 | 179 |
| 172 // Tests that if the rules are not loaded before the call to | 180 // Tests that if the rules are not loaded before the call to |
| 173 // StartRegionSubKeysRequest, they will be loaded in the call. | 181 // StartRegionSubKeysRequest, they will be loaded in the call. |
| 174 TEST_F(SubKeyRequesterTest, StartRequest_RulesNotLoaded_WillLoad) { | 182 TEST_F(SubKeyRequesterTest, StartRequest_RulesNotLoaded_WillLoad) { |
| 175 scoped_refptr<SubKeyReceiver> subkey_receiver_ = new SubKeyReceiver(); | 183 scoped_refptr<SubKeyReceiver> subkey_receiver_ = new SubKeyReceiver(); |
| 176 | 184 |
| 177 SubKeyReceiverCallback cb = | 185 SubKeyReceiverCallback cb = |
| 178 base::BindOnce(&SubKeyReceiver::OnSubKeysReceived, subkey_receiver_); | 186 base::BindOnce(&SubKeyReceiver::OnSubKeysReceived, subkey_receiver_); |
| 179 | 187 |
| 180 // Make sure the rules will not be loaded in the StartRegionSubKeysRequest | 188 // Make sure the rules will not be loaded in the StartRegionSubKeysRequest |
| 181 // call. | 189 // call. |
| 182 requester_->ShouldLoadRules(true); | 190 requester_->ShouldLoadRules(true); |
| 183 // Start the request. | 191 // Start the request. |
| 184 requester_->StartRegionSubKeysRequest(kLocale, 0, std::move(cb)); | 192 requester_->StartRegionSubKeysRequest(kLocale, kLanguage, 0, std::move(cb)); |
| 185 | 193 |
| 186 // Even if the rules are not loaded before the call to | 194 // Even if the rules are not loaded before the call to |
| 187 // StartRegionSubKeysRequest, they should get loaded in the call. Since our | 195 // StartRegionSubKeysRequest, they should get loaded in the call. Since our |
| 188 // test source is synchronous, the request will happen synchronously | 196 // test source is synchronous, the request will happen synchronously |
| 189 // too. | 197 // too. |
| 190 EXPECT_TRUE(requester_->AreRulesLoadedForRegion(kLocale)); | 198 EXPECT_TRUE(requester_->AreRulesLoadedForRegion(kLocale)); |
| 191 EXPECT_EQ(subkey_receiver_->subkeys_size(), kCorrectSize); | 199 EXPECT_EQ(subkey_receiver_->subkeys_size(), kCorrectSize); |
| 192 } | 200 } |
| 193 | 201 |
| 194 } // namespace payments | 202 } // namespace payments |
| OLD | NEW |