1 // Copyright (C) 2013 Google Inc.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include "testdata_source.h"
17 #include <libaddressinput/callback.h>
18 #include <libaddressinput/source.h>
19 #include <libaddressinput/util/basictypes.h>
20 #include <libaddressinput/util/scoped_ptr.h>
25 #include <gtest/gtest.h>
27 #include "region_data_constants.h"
31 using i18n::addressinput::BuildCallback;
32 using i18n::addressinput::RegionDataConstants;
33 using i18n::addressinput::scoped_ptr;
34 using i18n::addressinput::Source;
35 using i18n::addressinput::TestdataSource;
37 // Tests for TestdataSource object.
38 class TestdataSourceTest : public testing::TestWithParam<std::string> {
42 aggregate_source_(true),
46 data_ready_(BuildCallback(this, &TestdataSourceTest::OnDataReady)) {}
48 TestdataSource source_;
49 TestdataSource aggregate_source_;
53 const scoped_ptr<const Source::Callback> data_ready_;
56 void OnDataReady(bool success, const std::string& key, std::string* data) {
57 ASSERT_FALSE(success && data == NULL);
66 DISALLOW_COPY_AND_ASSIGN(TestdataSourceTest);
69 // Returns testing::AssertionSuccess if |data| is valid callback data for
71 testing::AssertionResult DataIsValid(const std::string& data,
72 const std::string& key) {
74 return testing::AssertionFailure() << "empty data";
77 std::string expected_data_begin = "{\"id\":\"" + key + "\"";
78 if (data.compare(0, expected_data_begin.length(), expected_data_begin) != 0) {
79 return testing::AssertionFailure() << data << " does not begin with "
80 << expected_data_begin;
83 // Verify that the data ends on "}.
84 static const char kDataEnd[] = "\"}";
85 static const size_t kDataEndLength = sizeof kDataEnd - 1;
86 if (data.compare(data.length() - kDataEndLength,
89 kDataEndLength) != 0) {
90 return testing::AssertionFailure() << data << " does not end with "
94 return testing::AssertionSuccess();
97 // Verifies that TestdataSource gets valid data for a region code.
98 TEST_P(TestdataSourceTest, TestdataSourceHasValidDataForRegion) {
99 std::string key = "data/" + GetParam();
100 source_.Get(key, *data_ready_);
102 EXPECT_TRUE(success_);
103 EXPECT_EQ(key, key_);
104 EXPECT_TRUE(DataIsValid(data_, key));
107 // Returns testing::AssertionSuccess if |data| is valid aggregated callback
109 testing::AssertionResult AggregateDataIsValid(const std::string& data,
110 const std::string& key) {
112 return testing::AssertionFailure() << "empty data";
115 std::string expected_data_begin = "{\"" + key;
116 if (data.compare(0, expected_data_begin.length(), expected_data_begin) != 0) {
117 return testing::AssertionFailure() << data << " does not begin with "
118 << expected_data_begin;
121 // Verify that the data ends on "}}.
122 static const char kDataEnd[] = "\"}}";
123 static const size_t kDataEndLength = sizeof kDataEnd - 1;
124 if (data.compare(data.length() - kDataEndLength,
127 kDataEndLength) != 0) {
128 return testing::AssertionFailure() << data << " does not end with "
132 return testing::AssertionSuccess();
135 // Verifies that TestdataSource gets valid aggregated data for a region code.
136 TEST_P(TestdataSourceTest, TestdataSourceHasValidAggregatedDataForRegion) {
137 std::string key = "data/" + GetParam();
138 aggregate_source_.Get(key, *data_ready_);
140 EXPECT_TRUE(success_);
141 EXPECT_EQ(key, key_);
142 EXPECT_TRUE(AggregateDataIsValid(data_, key));
145 // Test all region codes.
146 INSTANTIATE_TEST_CASE_P(
147 AllRegions, TestdataSourceTest,
148 testing::ValuesIn(RegionDataConstants::GetRegionCodes()));
150 // Verifies that the key "data" also contains valid data.
151 TEST_F(TestdataSourceTest, GetExistingData) {
152 static const std::string kKey = "data";
153 source_.Get(kKey, *data_ready_);
155 EXPECT_TRUE(success_);
156 EXPECT_EQ(kKey, key_);
157 EXPECT_TRUE(DataIsValid(data_, kKey));
160 // Verifies that requesting a missing key will return "{}".
161 TEST_F(TestdataSourceTest, GetMissingKeyReturnsEmptyDictionary) {
162 static const std::string kJunkKey = "junk";
163 source_.Get(kJunkKey, *data_ready_);
165 EXPECT_TRUE(success_);
166 EXPECT_EQ(kJunkKey, key_);
167 EXPECT_EQ("{}", data_);
170 // Verifies that aggregate requesting of a missing key will also return "{}".
171 TEST_F(TestdataSourceTest, AggregateGetMissingKeyReturnsEmptyDictionary) {
172 static const std::string kJunkKey = "junk";
173 aggregate_source_.Get(kJunkKey, *data_ready_);
175 EXPECT_TRUE(success_);
176 EXPECT_EQ(kJunkKey, key_);
177 EXPECT_EQ("{}", data_);
180 // Verifies that requesting an empty key will return "{}".
181 TEST_F(TestdataSourceTest, GetEmptyKeyReturnsEmptyDictionary) {
182 static const std::string kEmptyKey;
183 source_.Get(kEmptyKey, *data_ready_);
185 EXPECT_TRUE(success_);
186 EXPECT_EQ(kEmptyKey, key_);
187 EXPECT_EQ("{}", data_);