| OLD | NEW |
| 1 // Copyright (C) 2013 Google Inc. | 1 // Copyright (C) 2013 Google Inc. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. | 13 // limitations under the License. |
| 14 | 14 |
| 15 #include "rule_retriever.h" | 15 #include "rule_retriever.h" |
| 16 | 16 |
| 17 #include <libaddressinput/callback.h> | 17 #include <libaddressinput/callback.h> |
| 18 #include <libaddressinput/util/scoped_ptr.h> | 18 #include <libaddressinput/util/scoped_ptr.h> |
| 19 | 19 |
| 20 #include <string> | 20 #include <string> |
| 21 | 21 |
| 22 #include <gtest/gtest.h> | 22 #include <gtest/gtest.h> |
| 23 | 23 |
| 24 #include "fake_downloader.h" | 24 #include "fake_downloader.h" |
| 25 #include "fake_storage.h" | 25 #include "fake_storage.h" |
| 26 #include "lookup_key.h" |
| 26 #include "retriever.h" | 27 #include "retriever.h" |
| 27 #include "rule.h" | 28 #include "rule.h" |
| 28 | 29 |
| 29 namespace { | 30 namespace { |
| 30 | 31 |
| 31 using i18n::addressinput::FakeDownloader; | 32 using i18n::addressinput::FakeDownloader; |
| 32 using i18n::addressinput::FakeStorage; | 33 using i18n::addressinput::FakeStorage; |
| 34 using i18n::addressinput::LookupKey; |
| 33 using i18n::addressinput::Retriever; | 35 using i18n::addressinput::Retriever; |
| 34 using i18n::addressinput::Rule; | 36 using i18n::addressinput::Rule; |
| 35 using i18n::addressinput::RuleRetriever; | 37 using i18n::addressinput::RuleRetriever; |
| 36 using i18n::addressinput::scoped_ptr; | 38 using i18n::addressinput::scoped_ptr; |
| 37 | 39 |
| 38 // Tests for RuleRetriever object. | 40 // Tests for RuleRetriever object. |
| 39 class RuleRetrieverTest : public testing::Test { | 41 class RuleRetrieverTest : public testing::Test { |
| 40 protected: | 42 protected: |
| 41 RuleRetrieverTest() | 43 RuleRetrieverTest() |
| 42 : rule_retriever_(new Retriever(FakeDownloader::kFakeDataUrl, | 44 : rule_retriever_(new Retriever(FakeDownloader::kFakeDataUrl, |
| 43 new FakeDownloader, | 45 new FakeDownloader, |
| 44 new FakeStorage)), | 46 new FakeStorage)), |
| 45 success_(false), | 47 success_(false), |
| 46 key_(), | |
| 47 rule_() {} | 48 rule_() {} |
| 48 | 49 |
| 49 virtual ~RuleRetrieverTest() {} | 50 virtual ~RuleRetrieverTest() {} |
| 50 | 51 |
| 51 RuleRetriever::Callback* BuildCallback() { | 52 RuleRetriever::Callback* BuildCallback() { |
| 52 return i18n::addressinput::BuildCallback( | 53 return i18n::addressinput::BuildCallback( |
| 53 this, &RuleRetrieverTest::OnRuleReady); | 54 this, &RuleRetrieverTest::OnRuleReady); |
| 54 } | 55 } |
| 55 | 56 |
| 56 RuleRetriever rule_retriever_; | 57 RuleRetriever rule_retriever_; |
| 57 bool success_; | 58 bool success_; |
| 58 std::string key_; | |
| 59 Rule rule_; | 59 Rule rule_; |
| 60 | 60 |
| 61 private: | 61 private: |
| 62 void OnRuleReady(bool success, | 62 void OnRuleReady(bool success, |
| 63 const std::string& key, | 63 const LookupKey& key, |
| 64 const Rule& rule) { | 64 const Rule& rule) { |
| 65 success_ = success; | 65 success_ = success; |
| 66 key_ = key; | |
| 67 rule_.CopyFrom(rule); | 66 rule_.CopyFrom(rule); |
| 68 } | 67 } |
| 69 }; | 68 }; |
| 70 | 69 |
| 71 TEST_F(RuleRetrieverTest, ExistingRule) { | 70 TEST_F(RuleRetrieverTest, Basic) { |
| 72 static const char kExistingKey[] = "data/CA"; | 71 LookupKey key("CA"); |
| 73 | 72 |
| 74 scoped_ptr<RuleRetriever::Callback> callback(BuildCallback()); | 73 scoped_ptr<RuleRetriever::Callback> callback(BuildCallback()); |
| 75 rule_retriever_.RetrieveRule(kExistingKey, *callback); | 74 rule_retriever_.RetrieveRule(key, *callback); |
| 76 | 75 |
| 77 EXPECT_TRUE(success_); | 76 EXPECT_TRUE(success_); |
| 78 EXPECT_EQ(kExistingKey, key_); | |
| 79 EXPECT_FALSE(rule_.GetFormat().empty()); | 77 EXPECT_FALSE(rule_.GetFormat().empty()); |
| 80 } | 78 } |
| 81 | 79 |
| 82 TEST_F(RuleRetrieverTest, MissingRule) { | |
| 83 static const char kMissingKey[] = "junk"; | |
| 84 | |
| 85 scoped_ptr<RuleRetriever::Callback> callback(BuildCallback()); | |
| 86 rule_retriever_.RetrieveRule(kMissingKey, *callback); | |
| 87 | |
| 88 EXPECT_TRUE(success_); // The server returns "{}" for bad keys. | |
| 89 EXPECT_EQ(kMissingKey, key_); | |
| 90 EXPECT_TRUE(rule_.GetFormat().empty()); | |
| 91 } | |
| 92 | |
| 93 } // namespace | 80 } // namespace |
| OLD | NEW |