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