| 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 16 matching lines...) Expand all Loading... |
| 27 const char FakeDownloader::kFakeDataUrl[] = "test:///"; | 27 const char FakeDownloader::kFakeDataUrl[] = "test:///"; |
| 28 | 28 |
| 29 namespace { | 29 namespace { |
| 30 | 30 |
| 31 // The name of the test data file. | 31 // The name of the test data file. |
| 32 const char kDataFileName[] = TEST_DATA_DIR "/countryinfo.txt"; | 32 const char kDataFileName[] = TEST_DATA_DIR "/countryinfo.txt"; |
| 33 | 33 |
| 34 // The number of characters in the fake data URL prefix. | 34 // The number of characters in the fake data URL prefix. |
| 35 const size_t kFakeDataUrlLength = sizeof FakeDownloader::kFakeDataUrl - 1; | 35 const size_t kFakeDataUrlLength = sizeof FakeDownloader::kFakeDataUrl - 1; |
| 36 | 36 |
| 37 // Each key begins with this string. |
| 38 const char kKeyPrefix[] = "data/"; |
| 39 |
| 40 // The number of characters in the key prefix. |
| 41 const size_t kKeyPrefixLength = sizeof kKeyPrefix - 1; |
| 42 |
| 43 // The number of characters in a country-level key. |
| 44 const size_t kCountryKeyLength = kKeyPrefixLength + 2; |
| 45 |
| 37 std::map<std::string, std::string> InitData() { | 46 std::map<std::string, std::string> InitData() { |
| 38 std::map<std::string, std::string> data; | 47 std::map<std::string, std::string> data; |
| 39 std::ifstream file(kDataFileName); | 48 std::ifstream file(kDataFileName); |
| 40 assert(file.is_open()); | 49 assert(file.is_open()); |
| 41 | 50 |
| 42 std::string line; | 51 std::string line; |
| 43 while (file.good()) { | 52 while (file.good()) { |
| 44 std::getline(file, line); | 53 std::getline(file, line); |
| 54 if (line.compare(0, kKeyPrefixLength, kKeyPrefix, kKeyPrefixLength) != 0) { |
| 55 continue; |
| 56 } |
| 57 |
| 45 std::string::size_type divider = line.find('='); | 58 std::string::size_type divider = line.find('='); |
| 46 if (divider != std::string::npos) { | 59 if (divider == std::string::npos) { |
| 47 data.insert(std::make_pair( | 60 continue; |
| 48 FakeDownloader::kFakeDataUrl + (line.substr(0, divider)), | 61 } |
| 49 line.substr(divider + 1))); | 62 |
| 63 std::string key = line.substr(0, divider); |
| 64 std::string value = line.substr(divider + 1); |
| 65 std::string url = |
| 66 FakeDownloader::kFakeDataUrl + key.substr(0, kCountryKeyLength); |
| 67 std::map<std::string, std::string>::iterator data_it = data.find(url); |
| 68 if (data_it != data.end()) { |
| 69 data_it->second += ", \"" + key + "\": " + value; |
| 70 } else { |
| 71 data.insert(std::make_pair(url, "{\"" + key + "\": " + value)); |
| 50 } | 72 } |
| 51 } | 73 } |
| 52 file.close(); | 74 file.close(); |
| 75 |
| 76 for (std::map<std::string, std::string>::iterator data_it = data.begin(); |
| 77 data_it != data.end(); ++data_it) { |
| 78 data_it->second += "}"; |
| 79 } |
| 80 |
| 53 return data; | 81 return data; |
| 54 } | 82 } |
| 55 | 83 |
| 56 const std::map<std::string, std::string>& GetData() { | 84 const std::map<std::string, std::string>& GetData() { |
| 57 static const std::map<std::string, std::string> kData(InitData()); | 85 static const std::map<std::string, std::string> kData(InitData()); |
| 58 return kData; | 86 return kData; |
| 59 } | 87 } |
| 60 | 88 |
| 61 } // namespace | 89 } // namespace |
| 62 | 90 |
| 63 FakeDownloader::FakeDownloader() {} | 91 FakeDownloader::FakeDownloader() {} |
| 64 | 92 |
| 65 FakeDownloader::~FakeDownloader() {} | 93 FakeDownloader::~FakeDownloader() {} |
| 66 | 94 |
| 67 void FakeDownloader::Download(const std::string& url, | 95 void FakeDownloader::Download(const std::string& url, |
| 68 scoped_ptr<Callback> downloaded) { | 96 scoped_ptr<Callback> downloaded) { |
| 69 std::map<std::string, std::string>::const_iterator data_it = | 97 std::map<std::string, std::string>::const_iterator data_it = |
| 70 GetData().find(url); | 98 GetData().find(url); |
| 71 bool success = data_it != GetData().end(); | 99 bool success = data_it != GetData().end(); |
| 72 std::string data = success ? data_it->second : std::string(); | 100 std::string data = success ? data_it->second : std::string(); |
| 73 if (!success && !url.compare(0, kFakeDataUrlLength, kFakeDataUrl)) { | 101 if (!success && |
| 74 // URLs that start with "https://i18napis.appspot.com/ssl-address/" prefix, | 102 url.compare( |
| 75 // but do not have associated data will always return "{}" with status code | 103 0, kFakeDataUrlLength, kFakeDataUrl, kFakeDataUrlLength) == 0) { |
| 76 // 200. FakeDownloader imitates this behavior for URLs that start with | 104 // URLs that start with |
| 77 // "test:///" prefix. | 105 // "https://i18napis.appspot.com/ssl-aggregate-address/" prefix, but do not |
| 106 // have associated data will always return "{}" with status code 200. |
| 107 // FakeDownloader imitates this behavior for URLs that start with "test:///" |
| 108 // prefix. |
| 78 success = true; | 109 success = true; |
| 79 data = "{}"; | 110 data = "{}"; |
| 80 } | 111 } |
| 81 (*downloaded)(success, url, data); | 112 (*downloaded)(success, url, data); |
| 82 } | 113 } |
| 83 | 114 |
| 84 } // namespace addressinput | 115 } // namespace addressinput |
| 85 } // namespace i18n | 116 } // namespace i18n |
| OLD | NEW |