Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(272)

Side by Side Diff: third_party/libaddressinput/chromium/cpp/test/retriever_test.cc

Issue 145553009: rAc: use libaddressinput to validate international addresses. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 const std::string& checksum, 48 const std::string& checksum,
49 const std::string& timestamp) { 49 const std::string& timestamp) {
50 return data + "\n" + "checksum=" + checksum + "\n" + "timestamp=" + timestamp; 50 return data + "\n" + "checksum=" + checksum + "\n" + "timestamp=" + timestamp;
51 } 51 }
52 52
53 } // namespace 53 } // namespace
54 54
55 // Tests for Retriever object. 55 // Tests for Retriever object.
56 class RetrieverTest : public testing::Test { 56 class RetrieverTest : public testing::Test {
57 protected: 57 protected:
58 RetrieverTest() 58 RetrieverTest() : success_(false) {
59 : storage_(NULL),
60 retriever_(),
61 success_(false),
62 key_(),
63 data_() {
64 ResetRetriever(FakeDownloader::kFakeDataUrl); 59 ResetRetriever(FakeDownloader::kFakeDataUrl);
65 } 60 }
66 61
67 virtual ~RetrieverTest() {} 62 virtual ~RetrieverTest() {}
68 63
69 scoped_ptr<Retriever::Callback> BuildCallback() { 64 scoped_ptr<Retriever::Callback> BuildCallback() {
70 return ::i18n::addressinput::BuildCallback( 65 return ::i18n::addressinput::BuildCallback(
71 this, &RetrieverTest::OnDataReady); 66 this, &RetrieverTest::OnDataReady);
72 } 67 }
73 68
74 void ResetRetriever(const std::string& url) { 69 void ResetRetriever(const std::string& url) {
75 storage_ = new FakeStorage; 70 retriever_.reset(new Retriever(
76 retriever_.reset( 71 url, scoped_ptr<Downloader>(new FakeDownloader), &storage_));
77 new Retriever(url,
78 scoped_ptr<Downloader>(new FakeDownloader),
79 scoped_ptr<Storage>(storage_)));
80 } 72 }
81 73
82 std::string GetUrlForKey(const std::string& key) { 74 std::string GetUrlForKey(const std::string& key) {
83 return retriever_->GetUrlForKey(key); 75 return retriever_->GetUrlForKey(key);
84 } 76 }
85 77
86 std::string GetKeyForUrl(const std::string& url) { 78 std::string GetKeyForUrl(const std::string& url) {
87 return retriever_->GetKeyForUrl(url); 79 return retriever_->GetKeyForUrl(url);
88 } 80 }
89 81
90 Storage* storage_; // Owned by |retriever_|. 82 FakeStorage storage_;
91 scoped_ptr<Retriever> retriever_; 83 scoped_ptr<Retriever> retriever_;
92 bool success_; 84 bool success_;
93 std::string key_; 85 std::string key_;
94 std::string data_; 86 std::string data_;
95 87
96 private: 88 private:
97 void OnDataReady(bool success, 89 void OnDataReady(bool success,
98 const std::string& key, 90 const std::string& key,
99 const std::string& data) { 91 const std::string& data) {
100 success_ = success; 92 success_ = success;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 // Downloader implementation. 133 // Downloader implementation.
142 virtual void Download(const std::string& url, 134 virtual void Download(const std::string& url,
143 scoped_ptr<Callback> downloaded) { 135 scoped_ptr<Callback> downloaded) {
144 (*downloaded)(false, url, make_scoped_ptr(new std::string("garbage"))); 136 (*downloaded)(false, url, make_scoped_ptr(new std::string("garbage")));
145 } 137 }
146 }; 138 };
147 139
148 TEST_F(RetrieverTest, FaultyDownloader) { 140 TEST_F(RetrieverTest, FaultyDownloader) {
149 Retriever bad_retriever(FakeDownloader::kFakeDataUrl, 141 Retriever bad_retriever(FakeDownloader::kFakeDataUrl,
150 scoped_ptr<Downloader>(new FaultyDownloader), 142 scoped_ptr<Downloader>(new FaultyDownloader),
151 scoped_ptr<Storage>(new FakeStorage)); 143 &storage_);
152 bad_retriever.Retrieve(kKey, BuildCallback()); 144 bad_retriever.Retrieve(kKey, BuildCallback());
153 145
154 EXPECT_FALSE(success_); 146 EXPECT_FALSE(success_);
155 EXPECT_EQ(kKey, key_); 147 EXPECT_EQ(kKey, key_);
156 EXPECT_TRUE(data_.empty()); 148 EXPECT_TRUE(data_.empty());
157 } 149 }
158 150
159 TEST_F(RetrieverTest, FaultyDownloaderFallback) { 151 TEST_F(RetrieverTest, FaultyDownloaderFallback) {
160 Retriever bad_retriever(FakeDownloader::kFakeDataUrl, 152 Retriever bad_retriever(FakeDownloader::kFakeDataUrl,
161 scoped_ptr<Downloader>(new FaultyDownloader), 153 scoped_ptr<Downloader>(new FaultyDownloader),
162 scoped_ptr<Storage>(new FakeStorage)); 154 &storage_);
163 const char kFallbackDataKey[] = "data/US"; 155 const char kFallbackDataKey[] = "data/US";
164 bad_retriever.Retrieve(kFallbackDataKey, BuildCallback()); 156 bad_retriever.Retrieve(kFallbackDataKey, BuildCallback());
165 157
166 EXPECT_TRUE(success_); 158 EXPECT_TRUE(success_);
167 EXPECT_EQ(kFallbackDataKey, key_); 159 EXPECT_EQ(kFallbackDataKey, key_);
168 EXPECT_FALSE(data_.empty()); 160 EXPECT_FALSE(data_.empty());
169 EXPECT_NE(kEmptyData, data_); 161 EXPECT_NE(kEmptyData, data_);
170 } 162 }
171 163
172 TEST_F(RetrieverTest, NoChecksumAndTimestampWillRedownload) { 164 TEST_F(RetrieverTest, NoChecksumAndTimestampWillRedownload) {
173 storage_->Put(kKey, kEmptyData); 165 storage_.Put(kKey, kEmptyData);
174 retriever_->Retrieve(kKey, BuildCallback()); 166 retriever_->Retrieve(kKey, BuildCallback());
175 EXPECT_TRUE(success_); 167 EXPECT_TRUE(success_);
176 EXPECT_EQ(kKey, key_); 168 EXPECT_EQ(kKey, key_);
177 EXPECT_FALSE(data_.empty()); 169 EXPECT_FALSE(data_.empty());
178 EXPECT_NE(kEmptyData, data_); 170 EXPECT_NE(kEmptyData, data_);
179 } 171 }
180 172
181 TEST_F(RetrieverTest, ChecksumAndTimestampWillNotRedownload) { 173 TEST_F(RetrieverTest, ChecksumAndTimestampWillNotRedownload) {
182 storage_->Put(kKey, 174 storage_.Put(kKey,
183 Wrap(kEmptyData, kEmptyDataChecksum, TimeToString(time(NULL)))); 175 Wrap(kEmptyData, kEmptyDataChecksum, TimeToString(time(NULL))));
184 retriever_->Retrieve(kKey, BuildCallback()); 176 retriever_->Retrieve(kKey, BuildCallback());
185 EXPECT_TRUE(success_); 177 EXPECT_TRUE(success_);
186 EXPECT_EQ(kKey, key_); 178 EXPECT_EQ(kKey, key_);
187 EXPECT_EQ(kEmptyData, data_); 179 EXPECT_EQ(kEmptyData, data_);
188 } 180 }
189 181
190 TEST_F(RetrieverTest, OldTimestampWillRedownload) { 182 TEST_F(RetrieverTest, OldTimestampWillRedownload) {
191 storage_->Put(kKey, Wrap(kEmptyData, kEmptyDataChecksum, "0")); 183 storage_.Put(kKey, Wrap(kEmptyData, kEmptyDataChecksum, "0"));
192 retriever_->Retrieve(kKey, BuildCallback()); 184 retriever_->Retrieve(kKey, BuildCallback());
193 EXPECT_TRUE(success_); 185 EXPECT_TRUE(success_);
194 EXPECT_EQ(kKey, key_); 186 EXPECT_EQ(kKey, key_);
195 EXPECT_FALSE(data_.empty()); 187 EXPECT_FALSE(data_.empty());
196 EXPECT_NE(kEmptyData, data_); 188 EXPECT_NE(kEmptyData, data_);
197 } 189 }
198 190
199 TEST_F(RetrieverTest, OldTimestampOkIfDownloadFails) { 191 TEST_F(RetrieverTest, OldTimestampOkIfDownloadFails) {
200 storage_ = new FakeStorage;
201 Retriever bad_retriever(FakeDownloader::kFakeDataUrl, 192 Retriever bad_retriever(FakeDownloader::kFakeDataUrl,
202 scoped_ptr<Downloader>(new FaultyDownloader), 193 scoped_ptr<Downloader>(new FaultyDownloader),
203 scoped_ptr<Storage>(storage_)); 194 &storage_);
204 storage_->Put(kKey, Wrap(kEmptyData, kEmptyDataChecksum, "0")); 195 storage_.Put(kKey, Wrap(kEmptyData, kEmptyDataChecksum, "0"));
205 bad_retriever.Retrieve(kKey, BuildCallback()); 196 bad_retriever.Retrieve(kKey, BuildCallback());
206 EXPECT_TRUE(success_); 197 EXPECT_TRUE(success_);
207 EXPECT_EQ(kKey, key_); 198 EXPECT_EQ(kKey, key_);
208 EXPECT_EQ(kEmptyData, data_); 199 EXPECT_EQ(kEmptyData, data_);
209 } 200 }
210 201
211 TEST_F(RetrieverTest, WrongChecksumWillRedownload) { 202 TEST_F(RetrieverTest, WrongChecksumWillRedownload) {
212 static const char kNonEmptyData[] = "{\"non-empty\": \"data\"}"; 203 static const char kNonEmptyData[] = "{\"non-empty\": \"data\"}";
213 storage_->Put( 204 storage_.Put(
214 kKey, 205 kKey,
215 Wrap(kNonEmptyData, kEmptyDataChecksum, TimeToString(time(NULL)))); 206 Wrap(kNonEmptyData, kEmptyDataChecksum, TimeToString(time(NULL))));
216 retriever_->Retrieve(kKey, BuildCallback()); 207 retriever_->Retrieve(kKey, BuildCallback());
217 EXPECT_TRUE(success_); 208 EXPECT_TRUE(success_);
218 EXPECT_EQ(kKey, key_); 209 EXPECT_EQ(kKey, key_);
219 EXPECT_FALSE(data_.empty()); 210 EXPECT_FALSE(data_.empty());
220 EXPECT_NE(kNonEmptyData, data_); 211 EXPECT_NE(kNonEmptyData, data_);
221 } 212 }
222 213
223 // The downloader that doesn't get back to you. 214 // The downloader that doesn't get back to you.
224 class HangingDownloader : public Downloader { 215 class HangingDownloader : public Downloader {
225 public: 216 public:
226 HangingDownloader() {} 217 HangingDownloader() {}
227 virtual ~HangingDownloader() {} 218 virtual ~HangingDownloader() {}
228 219
229 // Downloader implementation. 220 // Downloader implementation.
230 virtual void Download(const std::string& url, 221 virtual void Download(const std::string& url,
231 scoped_ptr<Callback> downloaded) {} 222 scoped_ptr<Callback> downloaded) {}
232 }; 223 };
233 224
234 TEST_F(RetrieverTest, RequestsDontStack) { 225 TEST_F(RetrieverTest, RequestsDontStack) {
235 Retriever slow_retriever(FakeDownloader::kFakeDataUrl, 226 Retriever slow_retriever(FakeDownloader::kFakeDataUrl,
236 scoped_ptr<Downloader>(new HangingDownloader), 227 scoped_ptr<Downloader>(new HangingDownloader),
237 scoped_ptr<Storage>(new FakeStorage)); 228 &storage_);
238 229
239 slow_retriever.Retrieve(kKey, BuildCallback()); 230 slow_retriever.Retrieve(kKey, BuildCallback());
240 EXPECT_FALSE(success_); 231 EXPECT_FALSE(success_);
241 EXPECT_TRUE(key_.empty()); 232 EXPECT_TRUE(key_.empty());
242 233
243 EXPECT_NO_FATAL_FAILURE(slow_retriever.Retrieve(kKey, BuildCallback())); 234 EXPECT_NO_FATAL_FAILURE(slow_retriever.Retrieve(kKey, BuildCallback()));
244 } 235 }
245 236
246 TEST_F(RetrieverTest, GetUrlForKey) { 237 TEST_F(RetrieverTest, GetUrlForKey) {
247 ResetRetriever("test:///"); 238 ResetRetriever("test:///");
(...skipping 16 matching lines...) Expand all
264 255
265 TEST_F(RetrieverTest, NullCallbackNoCrash) { 256 TEST_F(RetrieverTest, NullCallbackNoCrash) {
266 ASSERT_NO_FATAL_FAILURE( 257 ASSERT_NO_FATAL_FAILURE(
267 retriever_->Retrieve(kKey, scoped_ptr<Retriever::Callback>())); 258 retriever_->Retrieve(kKey, scoped_ptr<Retriever::Callback>()));
268 ASSERT_NO_FATAL_FAILURE( 259 ASSERT_NO_FATAL_FAILURE(
269 retriever_->Retrieve(kKey, scoped_ptr<Retriever::Callback>())); 260 retriever_->Retrieve(kKey, scoped_ptr<Retriever::Callback>()));
270 } 261 }
271 262
272 } // namespace addressinput 263 } // namespace addressinput
273 } // namespace i18n 264 } // namespace i18n
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698