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