Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / libaddressinput / src / cpp / test / testdata_source_test.cc
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 "testdata_source.h"
16
17 #include <libaddressinput/callback.h>
18 #include <libaddressinput/source.h>
19 #include <libaddressinput/util/basictypes.h>
20 #include <libaddressinput/util/scoped_ptr.h>
21
22 #include <cstddef>
23 #include <string>
24
25 #include <gtest/gtest.h>
26
27 #include "region_data_constants.h"
28
29 namespace {
30
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;
36
37 // Tests for TestdataSource object.
38 class TestdataSourceTest : public testing::TestWithParam<std::string> {
39  protected:
40   TestdataSourceTest()
41       : source_(false),
42         aggregate_source_(true),
43         success_(false),
44         key_(),
45         data_(),
46         data_ready_(BuildCallback(this, &TestdataSourceTest::OnDataReady)) {}
47
48   TestdataSource source_;
49   TestdataSource aggregate_source_;
50   bool success_;
51   std::string key_;
52   std::string data_;
53   const scoped_ptr<const Source::Callback> data_ready_;
54
55  private:
56   void OnDataReady(bool success, const std::string& key, std::string* data) {
57     ASSERT_FALSE(success && data == NULL);
58     success_ = success;
59     key_ = key;
60     if (data != NULL) {
61       data_ = *data;
62       delete data;
63     }
64   }
65
66   DISALLOW_COPY_AND_ASSIGN(TestdataSourceTest);
67 };
68
69 // Returns testing::AssertionSuccess if |data| is valid callback data for
70 // |key|.
71 testing::AssertionResult DataIsValid(const std::string& data,
72                                      const std::string& key) {
73   if (data.empty()) {
74     return testing::AssertionFailure() << "empty data";
75   }
76
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;
81   }
82
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,
87                    kDataEndLength,
88                    kDataEnd,
89                    kDataEndLength) != 0) {
90     return testing::AssertionFailure() << data << " does not end with "
91                                        << kDataEnd;
92   }
93
94   return testing::AssertionSuccess();
95 }
96
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_);
101
102   EXPECT_TRUE(success_);
103   EXPECT_EQ(key, key_);
104   EXPECT_TRUE(DataIsValid(data_, key));
105 };
106
107 // Returns testing::AssertionSuccess if |data| is valid aggregated callback
108 // data for |key|.
109 testing::AssertionResult AggregateDataIsValid(const std::string& data,
110                                               const std::string& key) {
111   if (data.empty()) {
112     return testing::AssertionFailure() << "empty data";
113   }
114
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;
119   }
120
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,
125                    kDataEndLength,
126                    kDataEnd,
127                    kDataEndLength) != 0) {
128     return testing::AssertionFailure() << data << " does not end with "
129                                        << kDataEnd;
130   }
131
132   return testing::AssertionSuccess();
133 }
134
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_);
139
140   EXPECT_TRUE(success_);
141   EXPECT_EQ(key, key_);
142   EXPECT_TRUE(AggregateDataIsValid(data_, key));
143 };
144
145 // Test all region codes.
146 INSTANTIATE_TEST_CASE_P(
147     AllRegions, TestdataSourceTest,
148     testing::ValuesIn(RegionDataConstants::GetRegionCodes()));
149
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_);
154
155   EXPECT_TRUE(success_);
156   EXPECT_EQ(kKey, key_);
157   EXPECT_TRUE(DataIsValid(data_, kKey));
158 }
159
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_);
164
165   EXPECT_TRUE(success_);
166   EXPECT_EQ(kJunkKey, key_);
167   EXPECT_EQ("{}", data_);
168 }
169
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_);
174
175   EXPECT_TRUE(success_);
176   EXPECT_EQ(kJunkKey, key_);
177   EXPECT_EQ("{}", data_);
178 }
179
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_);
184
185   EXPECT_TRUE(success_);
186   EXPECT_EQ(kEmptyKey, key_);
187   EXPECT_EQ("{}", data_);
188 }
189
190 }  // namespace