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

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

Issue 110533002: [rac] Temporarily copy libaddressinput to Chrome. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Avoid redefinition of basictypes.h Created 7 years 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
(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 <libaddressinput/callback.h>
18 #include <libaddressinput/downloader.h>
19 #include <libaddressinput/util/scoped_ptr.h>
20
21 #include <string>
22
23 #include <gtest/gtest.h>
24
25 #include "region_data_constants.h"
26
27 namespace {
28
29 using i18n::addressinput::BuildCallback;
30 using i18n::addressinput::Downloader;
31 using i18n::addressinput::FakeDownloader;
32 using i18n::addressinput::RegionDataConstants;
33 using i18n::addressinput::scoped_ptr;
34
35 // Tests for FakeDownloader object.
36 class FakeDownloaderTest : public testing::TestWithParam<std::string> {
37 protected:
38 FakeDownloaderTest() : downloader_(), success_(false), url_(), data_() {}
39 virtual ~FakeDownloaderTest() {}
40
41 Downloader::Callback* BuildCallback() {
42 return ::BuildCallback(this, &FakeDownloaderTest::OnDownloaded);
43 }
44
45 FakeDownloader downloader_;
46 bool success_;
47 std::string url_;
48 std::string data_;
49
50 private:
51 void OnDownloaded(bool success,
52 const std::string& url,
53 const std::string& data) {
54 success_ = success;
55 url_ = url;
56 data_ = data;
57 }
58 };
59
60 // Returns testing::AssertionSuccess if |data| is valid downloaded data for
61 // |key|.
62 testing::AssertionResult DataIsValid(const std::string& data,
63 const std::string& key) {
64 if (data.empty()) {
65 return testing::AssertionFailure() << "empty data";
66 }
67
68 std::string expected_data_begin = "{\"id\":\"" + key + "\"";
69 if (data.compare(0, expected_data_begin.length(), expected_data_begin) != 0) {
70 return testing::AssertionFailure() << data << " does not begin with "
71 << expected_data_begin;
72 }
73
74 static const char kDataEnd[] = "\"}";
75 static const size_t kDataEndLength = sizeof kDataEnd - 1;
76 if (data.compare(data.length() - kDataEndLength,
77 kDataEndLength,
78 kDataEnd,
79 kDataEndLength) != 0) {
80 return testing::AssertionFailure() << data << " does not end with "
81 << kDataEnd;
82 }
83
84 return testing::AssertionSuccess();
85 }
86
87 // Verifies that FakeDownloader downloads valid data for a region code.
88 TEST_P(FakeDownloaderTest, FakeDownloaderHasValidDataForRegion) {
89 std::string key = "data/" + GetParam();
90 std::string url = std::string(FakeDownloader::kFakeDataUrl) + key;
91 scoped_ptr<Downloader::Callback> callback(BuildCallback());
92 downloader_.Download(url, *callback);
93
94 EXPECT_TRUE(success_);
95 EXPECT_EQ(url, url_);
96 EXPECT_TRUE(DataIsValid(data_, key));
97 };
98
99 // Test all region codes.
100 INSTANTIATE_TEST_CASE_P(
101 AllRegions, FakeDownloaderTest,
102 testing::ValuesIn(RegionDataConstants::GetRegionCodes()));
103
104 // Verifies that the key "data" also contains valid data.
105 TEST_F(FakeDownloaderTest, DownloadExistingData) {
106 static const std::string kKey = "data";
107 static const std::string kUrl =
108 std::string(FakeDownloader::kFakeDataUrl) + kKey;
109 scoped_ptr<Downloader::Callback> callback(BuildCallback());
110 downloader_.Download(kUrl, *callback);
111
112 EXPECT_TRUE(success_);
113 EXPECT_EQ(kUrl, url_);
114 EXPECT_TRUE(DataIsValid(data_, kKey));
115 }
116
117 // Verifies that downloading a missing key will return "{}".
118 TEST_F(FakeDownloaderTest, DownloadMissingKeyReturnsEmptyDictionary) {
119 static const std::string kJunkUrl =
120 std::string(FakeDownloader::kFakeDataUrl) + "junk";
121 scoped_ptr<Downloader::Callback> callback(BuildCallback());
122 downloader_.Download(kJunkUrl, *callback);
123
124 EXPECT_TRUE(success_);
125 EXPECT_EQ(kJunkUrl, url_);
126 EXPECT_EQ("{}", data_);
127 }
128
129 // Verifies that downloading an empty key will return "{}".
130 TEST_F(FakeDownloaderTest, DownloadEmptyKeyReturnsEmptyDictionary) {
131 static const std::string kPrefixOnlyUrl = FakeDownloader::kFakeDataUrl;
132 scoped_ptr<Downloader::Callback> callback(BuildCallback());
133 downloader_.Download(kPrefixOnlyUrl, *callback);
134
135 EXPECT_TRUE(success_);
136 EXPECT_EQ(kPrefixOnlyUrl, url_);
137 EXPECT_EQ("{}", data_);
138 }
139
140 // Verifies that downloading a real URL fails.
141 TEST_F(FakeDownloaderTest, DownloadRealUrlFals) {
142 static const std::string kRealUrl = "http://www.google.com/";
143 scoped_ptr<Downloader::Callback> callback(BuildCallback());
144 downloader_.Download(kRealUrl, *callback);
145
146 EXPECT_FALSE(success_);
147 EXPECT_EQ(kRealUrl, url_);
148 EXPECT_TRUE(data_.empty());
149 }
150
151 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698