| 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, |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 }; | 56 }; |
| 57 | 57 |
| 58 // Returns testing::AssertionSuccess if |data| is valid downloaded data for | 58 // Returns testing::AssertionSuccess if |data| is valid downloaded data for |
| 59 // |key|. | 59 // |key|. |
| 60 testing::AssertionResult DataIsValid(const std::string& data, | 60 testing::AssertionResult DataIsValid(const std::string& data, |
| 61 const std::string& key) { | 61 const std::string& key) { |
| 62 if (data.empty()) { | 62 if (data.empty()) { |
| 63 return testing::AssertionFailure() << "empty data"; | 63 return testing::AssertionFailure() << "empty data"; |
| 64 } | 64 } |
| 65 | 65 |
| 66 std::string expected_data_begin = "{\"id\":\"" + key + "\""; | 66 static const char kDataBegin[] = "{\"data/"; |
| 67 if (data.compare(0, expected_data_begin.length(), expected_data_begin) != 0) { | 67 static const size_t kDataBeginLength = sizeof kDataBegin - 1; |
| 68 if (data.compare(0, kDataBeginLength, kDataBegin, kDataBeginLength) != 0) { |
| 68 return testing::AssertionFailure() << data << " does not begin with " | 69 return testing::AssertionFailure() << data << " does not begin with " |
| 69 << expected_data_begin; | 70 << kDataBegin; |
| 70 } | 71 } |
| 71 | 72 |
| 72 static const char kDataEnd[] = "\"}"; | 73 static const char kDataEnd[] = "\"}}"; |
| 73 static const size_t kDataEndLength = sizeof kDataEnd - 1; | 74 static const size_t kDataEndLength = sizeof kDataEnd - 1; |
| 74 if (data.compare(data.length() - kDataEndLength, | 75 if (data.compare(data.length() - kDataEndLength, |
| 75 kDataEndLength, | 76 kDataEndLength, |
| 76 kDataEnd, | 77 kDataEnd, |
| 77 kDataEndLength) != 0) { | 78 kDataEndLength) != 0) { |
| 78 return testing::AssertionFailure() << data << " does not end with " | 79 return testing::AssertionFailure() << data << " does not end with " |
| 79 << kDataEnd; | 80 << kDataEnd; |
| 80 } | 81 } |
| 81 | 82 |
| 82 return testing::AssertionSuccess(); | 83 return testing::AssertionSuccess(); |
| 83 } | 84 } |
| 84 | 85 |
| 85 // Verifies that FakeDownloader downloads valid data for a region code. | 86 // Verifies that FakeDownloader downloads valid data for a region code. |
| 86 TEST_P(FakeDownloaderTest, FakeDownloaderHasValidDataForRegion) { | 87 TEST_P(FakeDownloaderTest, FakeDownloaderHasValidDataForRegion) { |
| 87 std::string key = "data/" + GetParam(); | 88 std::string key = "data/" + GetParam(); |
| 88 std::string url = std::string(FakeDownloader::kFakeDataUrl) + key; | 89 std::string url = std::string(FakeDownloader::kFakeDataUrl) + key; |
| 89 downloader_.Download(url, BuildCallback()); | 90 downloader_.Download(url, BuildCallback()); |
| 90 | 91 |
| 91 EXPECT_TRUE(success_); | 92 EXPECT_TRUE(success_); |
| 92 EXPECT_EQ(url, url_); | 93 EXPECT_EQ(url, url_); |
| 93 EXPECT_TRUE(DataIsValid(data_, key)); | 94 EXPECT_TRUE(DataIsValid(data_, key)); |
| 94 }; | 95 }; |
| 95 | 96 |
| 96 // Test all region codes. | 97 // Test all region codes. |
| 97 INSTANTIATE_TEST_CASE_P( | 98 INSTANTIATE_TEST_CASE_P( |
| 98 AllRegions, FakeDownloaderTest, | 99 AllRegions, FakeDownloaderTest, |
| 99 testing::ValuesIn(RegionDataConstants::GetRegionCodes())); | 100 testing::ValuesIn(RegionDataConstants::GetRegionCodes())); |
| 100 | 101 |
| 101 // Verifies that the key "data" also contains valid data. | |
| 102 TEST_F(FakeDownloaderTest, DownloadExistingData) { | |
| 103 static const std::string kKey = "data"; | |
| 104 static const std::string kUrl = | |
| 105 std::string(FakeDownloader::kFakeDataUrl) + kKey; | |
| 106 downloader_.Download(kUrl, BuildCallback()); | |
| 107 | |
| 108 EXPECT_TRUE(success_); | |
| 109 EXPECT_EQ(kUrl, url_); | |
| 110 EXPECT_TRUE(DataIsValid(data_, kKey)); | |
| 111 } | |
| 112 | |
| 113 // Verifies that downloading a missing key will return "{}". | 102 // Verifies that downloading a missing key will return "{}". |
| 114 TEST_F(FakeDownloaderTest, DownloadMissingKeyReturnsEmptyDictionary) { | 103 TEST_F(FakeDownloaderTest, DownloadMissingKeyReturnsEmptyDictionary) { |
| 115 static const std::string kJunkUrl = | 104 static const std::string kJunkUrl = |
| 116 std::string(FakeDownloader::kFakeDataUrl) + "junk"; | 105 std::string(FakeDownloader::kFakeDataUrl) + "junk"; |
| 117 downloader_.Download(kJunkUrl, BuildCallback()); | 106 downloader_.Download(kJunkUrl, BuildCallback()); |
| 118 | 107 |
| 119 EXPECT_TRUE(success_); | 108 EXPECT_TRUE(success_); |
| 120 EXPECT_EQ(kJunkUrl, url_); | 109 EXPECT_EQ(kJunkUrl, url_); |
| 121 EXPECT_EQ("{}", data_); | 110 EXPECT_EQ("{}", data_); |
| 122 } | 111 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 138 | 127 |
| 139 EXPECT_FALSE(success_); | 128 EXPECT_FALSE(success_); |
| 140 EXPECT_EQ(kRealUrl, url_); | 129 EXPECT_EQ(kRealUrl, url_); |
| 141 EXPECT_TRUE(data_.empty()); | 130 EXPECT_TRUE(data_.empty()); |
| 142 } | 131 } |
| 143 | 132 |
| 144 } // namespace | 133 } // namespace |
| 145 | 134 |
| 146 } // namespace addressinput | 135 } // namespace addressinput |
| 147 } // namespace i18n | 136 } // namespace i18n |
| OLD | NEW |