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