| 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 "fake_downloader.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 "region_data_constants.h" | |
| 26 | |
| 27 namespace i18n { | |
| 28 namespace addressinput { | |
| 29 | |
| 30 namespace { | |
| 31 | |
| 32 // Tests for FakeDownloader object. | |
| 33 class FakeDownloaderTest : public testing::Test { | |
| 34 protected: | |
| 35 FakeDownloaderTest() : downloader_(), success_(false), url_(), data_() {} | |
| 36 virtual ~FakeDownloaderTest() {} | |
| 37 | |
| 38 scoped_ptr<Downloader::Callback> BuildCallback() { | |
| 39 return ::i18n::addressinput::BuildScopedPtrCallback( | |
| 40 this, &FakeDownloaderTest::OnDownloaded); | |
| 41 } | |
| 42 | |
| 43 FakeDownloader downloader_; | |
| 44 bool success_; | |
| 45 std::string url_; | |
| 46 scoped_ptr<std::string> data_; | |
| 47 | |
| 48 private: | |
| 49 void OnDownloaded(bool success, | |
| 50 const std::string& url, | |
| 51 scoped_ptr<std::string> data) { | |
| 52 success_ = success; | |
| 53 url_ = url; | |
| 54 data_ = data.Pass(); | |
| 55 } | |
| 56 }; | |
| 57 | |
| 58 // Returns testing::AssertionSuccess if |data| is valid downloaded data for | |
| 59 // |key|. | |
| 60 testing::AssertionResult DataIsValid(const std::string& data, | |
| 61 const std::string& key) { | |
| 62 if (data.empty()) { | |
| 63 return testing::AssertionFailure() << "empty data"; | |
| 64 } | |
| 65 | |
| 66 static const char kDataBegin[] = "{\"data/"; | |
| 67 static const size_t kDataBeginLength = sizeof kDataBegin - 1; | |
| 68 if (data.compare(0, kDataBeginLength, kDataBegin, kDataBeginLength) != 0) { | |
| 69 return testing::AssertionFailure() << data << " does not begin with " | |
| 70 << kDataBegin; | |
| 71 } | |
| 72 | |
| 73 static const char kDataEnd[] = "\"}}"; | |
| 74 static const size_t kDataEndLength = sizeof kDataEnd - 1; | |
| 75 if (data.compare(data.length() - kDataEndLength, | |
| 76 kDataEndLength, | |
| 77 kDataEnd, | |
| 78 kDataEndLength) != 0) { | |
| 79 return testing::AssertionFailure() << data << " does not end with " | |
| 80 << kDataEnd; | |
| 81 } | |
| 82 | |
| 83 return testing::AssertionSuccess(); | |
| 84 } | |
| 85 | |
| 86 // Verifies that FakeDownloader downloads valid data for a region code. | |
| 87 TEST_F(FakeDownloaderTest, FakeDownloaderHasValidDataForRegion) { | |
| 88 const std::vector<std::string>& region_codes = | |
| 89 RegionDataConstants::GetRegionCodes(); | |
| 90 for (size_t i = 0; i < region_codes.size(); ++i) { | |
| 91 std::string key = "data/" + region_codes[i]; | |
| 92 std::string url = std::string(FakeDownloader::kFakeDataUrl) + key; | |
| 93 SCOPED_TRACE("For url: " + url); | |
| 94 | |
| 95 downloader_.Download(url, BuildCallback()); | |
| 96 | |
| 97 EXPECT_TRUE(success_); | |
| 98 EXPECT_EQ(url, url_); | |
| 99 EXPECT_TRUE(DataIsValid(*data_, key)); | |
| 100 } | |
| 101 }; | |
| 102 | |
| 103 // Verifies that downloading a missing key will return "{}". | |
| 104 TEST_F(FakeDownloaderTest, DownloadMissingKeyReturnsEmptyDictionary) { | |
| 105 static const std::string kJunkUrl = | |
| 106 std::string(FakeDownloader::kFakeDataUrl) + "junk"; | |
| 107 downloader_.Download(kJunkUrl, BuildCallback()); | |
| 108 | |
| 109 EXPECT_TRUE(success_); | |
| 110 EXPECT_EQ(kJunkUrl, url_); | |
| 111 EXPECT_EQ("{}", *data_); | |
| 112 } | |
| 113 | |
| 114 // Verifies that downloading an empty key will return "{}". | |
| 115 TEST_F(FakeDownloaderTest, DownloadEmptyKeyReturnsEmptyDictionary) { | |
| 116 static const std::string kPrefixOnlyUrl = FakeDownloader::kFakeDataUrl; | |
| 117 downloader_.Download(kPrefixOnlyUrl, BuildCallback()); | |
| 118 | |
| 119 EXPECT_TRUE(success_); | |
| 120 EXPECT_EQ(kPrefixOnlyUrl, url_); | |
| 121 EXPECT_EQ("{}", *data_); | |
| 122 } | |
| 123 | |
| 124 // Verifies that downloading a real URL fails. | |
| 125 TEST_F(FakeDownloaderTest, DownloadRealUrlFals) { | |
| 126 static const std::string kRealUrl = "http://www.google.com/"; | |
| 127 downloader_.Download(kRealUrl, BuildCallback()); | |
| 128 | |
| 129 EXPECT_FALSE(success_); | |
| 130 EXPECT_EQ(kRealUrl, url_); | |
| 131 EXPECT_TRUE(data_->empty()); | |
| 132 } | |
| 133 | |
| 134 } // namespace | |
| 135 | |
| 136 } // namespace addressinput | |
| 137 } // namespace i18n | |
| OLD | NEW |