| 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 "fake_downloader.h" | 15 #include "fake_downloader.h" |
| 16 | 16 |
| 17 #include <cassert> | 17 #include <cassert> |
| 18 #include <fstream> | 18 #include <fstream> |
| 19 #include <map> | 19 #include <map> |
| 20 #include <string> | 20 #include <string> |
| 21 #include <utility> | 21 #include <utility> |
| 22 | 22 |
| 23 #include "lookup_key_util.h" | |
| 24 | |
| 25 namespace i18n { | 23 namespace i18n { |
| 26 namespace addressinput { | 24 namespace addressinput { |
| 27 | 25 |
| 28 // static | 26 // static |
| 29 const char FakeDownloader::kFakeDataUrl[] = "test:///"; | 27 const char FakeDownloader::kFakeDataUrl[] = "test:///"; |
| 30 | 28 |
| 31 namespace { | 29 namespace { |
| 32 | 30 |
| 33 // The name of the test data file. | 31 // The name of the test data file. |
| 34 const char kDataFileName[] = TEST_DATA_DIR "/countryinfo.txt"; | 32 const char kDataFileName[] = TEST_DATA_DIR "/countryinfo.txt"; |
| 35 | 33 |
| 36 // The number of characters in the fake data URL prefix. | 34 // The number of characters in the fake data URL prefix. |
| 37 const size_t kFakeDataUrlLength = sizeof FakeDownloader::kFakeDataUrl - 1; | 35 const size_t kFakeDataUrlLength = sizeof FakeDownloader::kFakeDataUrl - 1; |
| 38 | 36 |
| 39 const LookupKeyUtil& GetLookupKeyUtil() { | |
| 40 static const LookupKeyUtil kLookupKeyUtil(FakeDownloader::kFakeDataUrl); | |
| 41 return kLookupKeyUtil; | |
| 42 } | |
| 43 | |
| 44 std::map<std::string, std::string> InitData() { | 37 std::map<std::string, std::string> InitData() { |
| 45 std::map<std::string, std::string> data; | 38 std::map<std::string, std::string> data; |
| 46 std::ifstream file(kDataFileName); | 39 std::ifstream file(kDataFileName); |
| 47 assert(file.is_open()); | 40 assert(file.is_open()); |
| 48 | 41 |
| 49 std::string line; | 42 std::string line; |
| 50 while (file.good()) { | 43 while (file.good()) { |
| 51 std::getline(file, line); | 44 std::getline(file, line); |
| 52 std::string::size_type divider = line.find('='); | 45 std::string::size_type divider = line.find('='); |
| 53 if (divider != std::string::npos) { | 46 if (divider != std::string::npos) { |
| 54 data.insert(std::make_pair( | 47 data.insert(std::make_pair( |
| 55 GetLookupKeyUtil().GetUrlForKey(line.substr(0, divider)), | 48 FakeDownloader::kFakeDataUrl + (line.substr(0, divider)), |
| 56 line.substr(divider + 1))); | 49 line.substr(divider + 1))); |
| 57 } | 50 } |
| 58 } | 51 } |
| 59 file.close(); | 52 file.close(); |
| 60 return data; | 53 return data; |
| 61 } | 54 } |
| 62 | 55 |
| 63 const std::map<std::string, std::string>& GetData() { | 56 const std::map<std::string, std::string>& GetData() { |
| 64 static const std::map<std::string, std::string> kData(InitData()); | 57 static const std::map<std::string, std::string> kData(InitData()); |
| 65 return kData; | 58 return kData; |
| 66 } | 59 } |
| 67 | 60 |
| 68 } // namespace | 61 } // namespace |
| 69 | 62 |
| 70 FakeDownloader::FakeDownloader() {} | 63 FakeDownloader::FakeDownloader() {} |
| 71 | 64 |
| 72 FakeDownloader::~FakeDownloader() {} | 65 FakeDownloader::~FakeDownloader() {} |
| 73 | 66 |
| 74 void FakeDownloader::Download(const std::string& url, | 67 void FakeDownloader::Download(const std::string& url, |
| 75 scoped_ptr<Callback> downloaded) { | 68 scoped_ptr<Callback> downloaded) { |
| 76 std::map<std::string, std::string>::const_iterator data_it = | 69 std::map<std::string, std::string>::const_iterator data_it = |
| 77 GetData().find(url); | 70 GetData().find(url); |
| 78 bool success = data_it != GetData().end(); | 71 bool success = data_it != GetData().end(); |
| 79 std::string data = success ? data_it->second : std::string(); | 72 std::string data = success ? data_it->second : std::string(); |
| 80 if (!success && GetLookupKeyUtil().IsValidationDataUrl(url)) { | 73 if (!success && !url.compare(0, kFakeDataUrlLength, kFakeDataUrl)) { |
| 81 // URLs that start with "https://i18napis.appspot.com/ssl-address/" prefix, | 74 // URLs that start with "https://i18napis.appspot.com/ssl-address/" prefix, |
| 82 // but do not have associated data will always return "{}" with status code | 75 // but do not have associated data will always return "{}" with status code |
| 83 // 200. FakeDownloader imitates this behavior for URLs that start with | 76 // 200. FakeDownloader imitates this behavior for URLs that start with |
| 84 // "test:///" prefix. | 77 // "test:///" prefix. |
| 85 success = true; | 78 success = true; |
| 86 data = "{}"; | 79 data = "{}"; |
| 87 } | 80 } |
| 88 (*downloaded)(success, url, data); | 81 (*downloaded)(success, url, data); |
| 89 } | 82 } |
| 90 | 83 |
| 91 } // namespace addressinput | 84 } // namespace addressinput |
| 92 } // namespace i18n | 85 } // namespace i18n |
| OLD | NEW |