OLD | NEW |
(Empty) | |
| 1 // Copyright (C) 2013 Google Inc. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (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 |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include "retriever.h" |
| 16 |
| 17 #include <libaddressinput/callback.h> |
| 18 #include <libaddressinput/downloader.h> |
| 19 #include <libaddressinput/util/scoped_ptr.h> |
| 20 |
| 21 #include <string> |
| 22 |
| 23 #include <gtest/gtest.h> |
| 24 |
| 25 #include "fake_downloader.h" |
| 26 #include "fake_storage.h" |
| 27 #include "region_data_constants.h" |
| 28 |
| 29 namespace { |
| 30 |
| 31 using i18n::addressinput::BuildCallback; |
| 32 using i18n::addressinput::Downloader; |
| 33 using i18n::addressinput::FakeDownloader; |
| 34 using i18n::addressinput::FakeStorage; |
| 35 using i18n::addressinput::RegionDataConstants; |
| 36 using i18n::addressinput::Retriever; |
| 37 using i18n::addressinput::scoped_ptr; |
| 38 |
| 39 const char kKey[] = "data/CA/AB--fr"; |
| 40 |
| 41 // Empty data that the downloader can return. |
| 42 const char kEmptyData[] = "{}"; |
| 43 |
| 44 // Tests for Retriever object. |
| 45 class RetrieverTest : public testing::Test { |
| 46 protected: |
| 47 RetrieverTest() |
| 48 : retriever_(FakeDownloader::kFakeDataUrl, |
| 49 new FakeDownloader, |
| 50 new FakeStorage), |
| 51 success_(false), |
| 52 key_(), |
| 53 data_() {} |
| 54 |
| 55 virtual ~RetrieverTest() {} |
| 56 |
| 57 Retriever::Callback* BuildCallback() { |
| 58 return ::BuildCallback(this, &RetrieverTest::OnDataReady); |
| 59 } |
| 60 |
| 61 Retriever retriever_; |
| 62 bool success_; |
| 63 std::string key_; |
| 64 std::string data_; |
| 65 |
| 66 private: |
| 67 void OnDataReady(bool success, |
| 68 const std::string& key, |
| 69 const std::string& data) { |
| 70 success_ = success; |
| 71 key_ = key; |
| 72 data_ = data; |
| 73 } |
| 74 }; |
| 75 |
| 76 TEST_F(RetrieverTest, RetrieveData) { |
| 77 scoped_ptr<Retriever::Callback> callback(BuildCallback()); |
| 78 retriever_.Retrieve(kKey, *callback); |
| 79 |
| 80 EXPECT_TRUE(success_); |
| 81 EXPECT_EQ(kKey, key_); |
| 82 EXPECT_FALSE(data_.empty()); |
| 83 EXPECT_NE(kEmptyData, data_); |
| 84 } |
| 85 |
| 86 TEST_F(RetrieverTest, ReadDataFromStorage) { |
| 87 scoped_ptr<Retriever::Callback> callback1(BuildCallback()); |
| 88 retriever_.Retrieve(kKey, *callback1); |
| 89 |
| 90 scoped_ptr<Retriever::Callback> callback2(BuildCallback()); |
| 91 retriever_.Retrieve(kKey, *callback2); |
| 92 |
| 93 EXPECT_TRUE(success_); |
| 94 EXPECT_EQ(kKey, key_); |
| 95 EXPECT_FALSE(data_.empty()); |
| 96 EXPECT_NE(kEmptyData, data_); |
| 97 } |
| 98 |
| 99 TEST_F(RetrieverTest, MissingKeyReturnsEmptyData) { |
| 100 static const char kMissingKey[] = "junk"; |
| 101 |
| 102 scoped_ptr<Retriever::Callback> callback(BuildCallback()); |
| 103 retriever_.Retrieve(kMissingKey, *callback); |
| 104 |
| 105 EXPECT_TRUE(success_); |
| 106 EXPECT_EQ(kMissingKey, key_); |
| 107 EXPECT_EQ(kEmptyData, data_); |
| 108 } |
| 109 |
| 110 // The downloader that always fails. |
| 111 class FaultyDownloader : public Downloader { |
| 112 public: |
| 113 FaultyDownloader() {} |
| 114 virtual ~FaultyDownloader() {} |
| 115 |
| 116 // Downloader implementation. |
| 117 virtual void Download(const std::string& url, |
| 118 const Callback& downloaded) const { |
| 119 downloaded(false, url, "garbage"); |
| 120 } |
| 121 }; |
| 122 |
| 123 TEST_F(RetrieverTest, FaultyDownloader) { |
| 124 Retriever bad_retriever(FakeDownloader::kFakeDataUrl, |
| 125 new FaultyDownloader, |
| 126 new FakeStorage); |
| 127 |
| 128 scoped_ptr<Retriever::Callback> callback(BuildCallback()); |
| 129 bad_retriever.Retrieve(kKey, *callback); |
| 130 |
| 131 EXPECT_FALSE(success_); |
| 132 EXPECT_EQ(kKey, key_); |
| 133 EXPECT_TRUE(data_.empty()); |
| 134 } |
| 135 |
| 136 } // namespace |
OLD | NEW |