Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / libaddressinput / src / cpp / test / rule_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 "rule.h"
16
17 #include <libaddressinput/address_field.h>
18 #include <libaddressinput/localization.h>
19 #include <libaddressinput/util/basictypes.h>
20
21 #include <cstddef>
22 #include <string>
23 #include <utility>
24 #include <vector>
25
26 #include <gtest/gtest.h>
27
28 #include "format_element.h"
29 #include "grit.h"
30 #include "messages.h"
31 #include "region_data_constants.h"
32 #include "util/json.h"
33
34 namespace {
35
36 using i18n::addressinput::AddressField;
37 using i18n::addressinput::ADMIN_AREA;
38 using i18n::addressinput::FormatElement;
39 using i18n::addressinput::INVALID_MESSAGE_ID;
40 using i18n::addressinput::Json;
41 using i18n::addressinput::LOCALITY;
42 using i18n::addressinput::Localization;
43 using i18n::addressinput::RegionDataConstants;
44 using i18n::addressinput::Rule;
45 using i18n::addressinput::STREET_ADDRESS;
46
47 TEST(RuleTest, CopyOverwritesRule) {
48   Rule rule;
49   ASSERT_TRUE(rule.ParseSerializedRule(
50       "{"
51       "\"fmt\":\"%S%Z\","
52       "\"lfmt\":\"%Z%S\","
53       "\"id\":\"data/XA\","
54       "\"name\":\"Le Test\","
55       "\"lname\":\"Testistan\","
56       "\"require\":\"AC\","
57       "\"sub_keys\":\"aa~bb~cc\","
58       "\"languages\":\"en~fr\","
59       "\"zip\":\"\\\\d{3}\","
60       "\"state_name_type\":\"area\","
61       "\"locality_name_type\":\"post_town\","
62       "\"sublocality_name_type\":\"neighborhood\","
63       "\"zip_name_type\":\"postal\","
64       "\"zipex\":\"1234\","
65       "\"posturl\":\"http://www.testpost.com\""
66       "}"));
67
68   Rule copy;
69   EXPECT_NE(rule.GetFormat(), copy.GetFormat());
70   EXPECT_NE(rule.GetLatinFormat(), copy.GetLatinFormat());
71   EXPECT_NE(rule.GetId(), copy.GetId());
72   EXPECT_NE(rule.GetRequired(), copy.GetRequired());
73   EXPECT_NE(rule.GetSubKeys(), copy.GetSubKeys());
74   EXPECT_NE(rule.GetLanguages(), copy.GetLanguages());
75   EXPECT_NE(rule.GetAdminAreaNameMessageId(),
76             copy.GetAdminAreaNameMessageId());
77   EXPECT_NE(rule.GetPostalCodeNameMessageId(),
78             copy.GetPostalCodeNameMessageId());
79   EXPECT_NE(rule.GetLocalityNameMessageId(),
80             copy.GetLocalityNameMessageId());
81   EXPECT_NE(rule.GetSublocalityNameMessageId(),
82             copy.GetSublocalityNameMessageId());
83   EXPECT_NE(rule.GetName(), copy.GetName());
84   EXPECT_NE(rule.GetLatinName(), copy.GetLatinName());
85   EXPECT_NE(rule.GetPostalCodeExample(), copy.GetPostalCodeExample());
86   EXPECT_NE(rule.GetPostServiceUrl(), copy.GetPostServiceUrl());
87
88   EXPECT_TRUE(rule.GetPostalCodeMatcher() != NULL);
89   EXPECT_TRUE(copy.GetPostalCodeMatcher() == NULL);
90
91   copy.CopyFrom(rule);
92   EXPECT_EQ(rule.GetFormat(), copy.GetFormat());
93   EXPECT_EQ(rule.GetLatinFormat(), copy.GetLatinFormat());
94   EXPECT_EQ(rule.GetId(), copy.GetId());
95   EXPECT_EQ(rule.GetRequired(), copy.GetRequired());
96   EXPECT_EQ(rule.GetSubKeys(), copy.GetSubKeys());
97   EXPECT_EQ(rule.GetLanguages(), copy.GetLanguages());
98   EXPECT_EQ(rule.GetAdminAreaNameMessageId(),
99             copy.GetAdminAreaNameMessageId());
100   EXPECT_EQ(rule.GetPostalCodeNameMessageId(),
101             copy.GetPostalCodeNameMessageId());
102   EXPECT_EQ(rule.GetSublocalityNameMessageId(),
103             copy.GetSublocalityNameMessageId());
104   EXPECT_EQ(rule.GetLocalityNameMessageId(),
105             copy.GetLocalityNameMessageId());
106   EXPECT_EQ(rule.GetName(), copy.GetName());
107   EXPECT_EQ(rule.GetLatinName(), copy.GetLatinName());
108   EXPECT_EQ(rule.GetPostalCodeExample(), copy.GetPostalCodeExample());
109   EXPECT_EQ(rule.GetPostServiceUrl(), copy.GetPostServiceUrl());
110
111   EXPECT_TRUE(copy.GetPostalCodeMatcher() != NULL);
112 }
113
114 TEST(RuleTest, ParseOverwritesRule) {
115   Rule rule;
116   ASSERT_TRUE(rule.ParseSerializedRule("{"
117                                        "\"fmt\":\"%S%Z\","
118                                        "\"state_name_type\":\"area\","
119                                        "\"zip\":\"1234\","
120                                        "\"zip_name_type\":\"postal\","
121                                        "\"zipex\":\"1234\","
122                                        "\"posturl\":\"http://www.testpost.com\""
123                                        "}"));
124   EXPECT_FALSE(rule.GetFormat().empty());
125   EXPECT_EQ(IDS_LIBADDRESSINPUT_AREA,
126             rule.GetAdminAreaNameMessageId());
127   EXPECT_EQ(IDS_LIBADDRESSINPUT_POSTAL_CODE_LABEL,
128             rule.GetPostalCodeNameMessageId());
129   EXPECT_EQ("1234", rule.GetSolePostalCode());
130   EXPECT_EQ("1234", rule.GetPostalCodeExample());
131   EXPECT_EQ("http://www.testpost.com", rule.GetPostServiceUrl());
132
133   ASSERT_TRUE(rule.ParseSerializedRule("{"
134                                        "\"fmt\":\"\","
135                                        "\"state_name_type\":\"do_si\","
136                                        "\"zip_name_type\":\"zip\","
137                                        "\"zipex\":\"5678\","
138                                        "\"posturl\":\"http://www.fakepost.com\""
139                                        "}"));
140   EXPECT_TRUE(rule.GetFormat().empty());
141   EXPECT_EQ(IDS_LIBADDRESSINPUT_DO_SI,
142             rule.GetAdminAreaNameMessageId());
143   EXPECT_EQ(IDS_LIBADDRESSINPUT_ZIP_CODE_LABEL,
144             rule.GetPostalCodeNameMessageId());
145   EXPECT_TRUE(rule.GetSolePostalCode().empty());
146   EXPECT_EQ("5678", rule.GetPostalCodeExample());
147   EXPECT_EQ("http://www.fakepost.com", rule.GetPostServiceUrl());
148 }
149
150 TEST(RuleTest, ParsesFormatCorrectly) {
151   std::vector<FormatElement> expected;
152   expected.push_back(FormatElement(ADMIN_AREA));
153   expected.push_back(FormatElement(LOCALITY));
154   Rule rule;
155   ASSERT_TRUE(rule.ParseSerializedRule("{\"fmt\":\"%S%C\"}"));
156   EXPECT_EQ(expected, rule.GetFormat());
157 }
158
159 TEST(RuleTest, ParsesNameCorrectly) {
160   Rule rule;
161   ASSERT_TRUE(rule.ParseSerializedRule("{\"name\":\"Le Test\"}"));
162   EXPECT_EQ("Le Test", rule.GetName());
163 }
164
165 TEST(RuleTest, ParsesLatinNameCorrectly) {
166   Rule rule;
167   ASSERT_TRUE(rule.ParseSerializedRule("{\"lname\":\"Testistan\"}"));
168   EXPECT_EQ("Testistan", rule.GetLatinName());
169 }
170
171 TEST(RuleTest, ParsesLatinFormatCorrectly) {
172   std::vector<FormatElement> expected;
173   expected.push_back(FormatElement(LOCALITY));
174   expected.push_back(FormatElement(ADMIN_AREA));
175   Rule rule;
176   ASSERT_TRUE(rule.ParseSerializedRule("{\"lfmt\":\"%C%S\"}"));
177   EXPECT_EQ(expected, rule.GetLatinFormat());
178 }
179
180 TEST(RuleTest, ParsesRequiredCorrectly) {
181   std::vector<AddressField> expected;
182   expected.push_back(STREET_ADDRESS);
183   expected.push_back(LOCALITY);
184   Rule rule;
185   ASSERT_TRUE(rule.ParseSerializedRule("{\"require\":\"AC\"}"));
186   EXPECT_EQ(expected, rule.GetRequired());
187 }
188
189 TEST(RuleTest, ParsesSubKeysCorrectly) {
190   std::vector<std::string> expected;
191   expected.push_back("aa");
192   expected.push_back("bb");
193   expected.push_back("cc");
194   Rule rule;
195   ASSERT_TRUE(rule.ParseSerializedRule("{\"sub_keys\":\"aa~bb~cc\"}"));
196   EXPECT_EQ(expected, rule.GetSubKeys());
197 }
198
199 TEST(RuleTest, ParsesLanguagesCorrectly) {
200   std::vector<std::string> expected;
201   expected.push_back("de");
202   expected.push_back("fr");
203   expected.push_back("it");
204   Rule rule;
205   ASSERT_TRUE(rule.ParseSerializedRule("{\"languages\":\"de~fr~it\"}"));
206   EXPECT_EQ(expected, rule.GetLanguages());
207 }
208
209 TEST(RuleTest, ParsesPostalCodeExampleCorrectly) {
210   Rule rule;
211   ASSERT_TRUE(rule.ParseSerializedRule("{\"zipex\":\"1234,12345-6789\"}"));
212   EXPECT_EQ("1234,12345-6789", rule.GetPostalCodeExample());
213 }
214
215 TEST(RuleTest, ParsesPostServiceUrlCorrectly) {
216   Rule rule;
217   ASSERT_TRUE(
218       rule.ParseSerializedRule("{\"posturl\":\"http://www.testpost.com\"}"));
219   EXPECT_EQ("http://www.testpost.com", rule.GetPostServiceUrl());
220 }
221
222 TEST(RuleTest, PostalCodeMatcher) {
223   Rule rule;
224   ASSERT_TRUE(rule.ParseSerializedRule("{\"zip\":\"\\\\d{3}\"}"));
225   EXPECT_TRUE(rule.GetPostalCodeMatcher() != NULL);
226 }
227
228 TEST(RuleTest, PostalCodeMatcherInvalidRegExp) {
229   Rule rule;
230   ASSERT_TRUE(rule.ParseSerializedRule("{\"zip\":\"(\"}"));
231   EXPECT_TRUE(rule.GetPostalCodeMatcher() == NULL);
232 }
233
234 TEST(RuleTest, ParsesJsonRuleCorrectly) {
235   Json json;
236   ASSERT_TRUE(json.ParseObject("{\"zip\":\"\\\\d{3}\"}"));
237   Rule rule;
238   rule.ParseJsonRule(json);
239   EXPECT_TRUE(rule.GetPostalCodeMatcher() != NULL);
240 }
241
242 TEST(RuleTest, EmptyStringIsNotValid) {
243   Rule rule;
244   EXPECT_FALSE(rule.ParseSerializedRule(std::string()));
245 }
246
247 TEST(RuleTest, EmptyDictionaryIsValid) {
248   Rule rule;
249   EXPECT_TRUE(rule.ParseSerializedRule("{}"));
250 }
251
252 // Tests for parsing the postal code name.
253 class PostalCodeNameParseTest
254     : public testing::TestWithParam<std::pair<std::string, int> > {
255  protected:
256   PostalCodeNameParseTest() {}
257   Rule rule_;
258
259  private:
260   DISALLOW_COPY_AND_ASSIGN(PostalCodeNameParseTest);
261 };
262
263 // Verifies that a postal code name is parsed correctly.
264 TEST_P(PostalCodeNameParseTest, ParsedCorrectly) {
265   ASSERT_TRUE(rule_.ParseSerializedRule(GetParam().first));
266   EXPECT_EQ(GetParam().second, rule_.GetPostalCodeNameMessageId());
267 }
268
269 // Test parsing all postal code names.
270 INSTANTIATE_TEST_CASE_P(
271     AllPostalCodeNames, PostalCodeNameParseTest,
272     testing::Values(
273         std::make_pair("{\"zip_name_type\":\"postal\"}",
274                        IDS_LIBADDRESSINPUT_POSTAL_CODE_LABEL),
275         std::make_pair("{\"zip_name_type\":\"zip\"}",
276                        IDS_LIBADDRESSINPUT_ZIP_CODE_LABEL)));
277
278 // Tests for parsing the locality name.
279 class LocalityNameParseTest
280     : public testing::TestWithParam<std::pair<std::string, int> > {
281  protected:
282   LocalityNameParseTest() {}
283   Rule rule_;
284
285  private:
286   DISALLOW_COPY_AND_ASSIGN(LocalityNameParseTest);
287 };
288
289 // Verifies that a locality name is parsed correctly.
290 TEST_P(LocalityNameParseTest, ParsedCorrectly) {
291   ASSERT_TRUE(rule_.ParseSerializedRule(GetParam().first));
292   EXPECT_EQ(GetParam().second, rule_.GetLocalityNameMessageId());
293 }
294
295 // Test parsing all locality names.
296 INSTANTIATE_TEST_CASE_P(
297     AllLocalityNames, LocalityNameParseTest,
298     testing::Values(
299         std::make_pair("{\"locality_name_type\":\"post_town\"}",
300                        IDS_LIBADDRESSINPUT_POST_TOWN),
301         std::make_pair("{\"locality_name_type\":\"city\"}",
302                        IDS_LIBADDRESSINPUT_LOCALITY_LABEL),
303         std::make_pair("{\"locality_name_type\":\"district\"}",
304                        IDS_LIBADDRESSINPUT_DISTRICT)));
305
306 // Tests for parsing the locality name.
307 class SublocalityNameParseTest
308     : public testing::TestWithParam<std::pair<std::string, int> > {
309  protected:
310   SublocalityNameParseTest() {}
311   Rule rule_;
312
313  private:
314   DISALLOW_COPY_AND_ASSIGN(SublocalityNameParseTest);
315 };
316
317 // Verifies that a sublocality name is parsed correctly.
318 TEST_P(SublocalityNameParseTest, ParsedCorrectly) {
319   ASSERT_TRUE(rule_.ParseSerializedRule(GetParam().first));
320   EXPECT_EQ(GetParam().second, rule_.GetSublocalityNameMessageId());
321 }
322
323 // Test parsing all sublocality names.
324 INSTANTIATE_TEST_CASE_P(
325     AllSublocalityNames, SublocalityNameParseTest,
326     testing::Values(
327         std::make_pair("{\"sublocality_name_type\":\"village_township\"}",
328                        IDS_LIBADDRESSINPUT_VILLAGE_TOWNSHIP),
329         std::make_pair("{\"sublocality_name_type\":\"neighborhood\"}",
330                        IDS_LIBADDRESSINPUT_NEIGHBORHOOD),
331         std::make_pair("{\"sublocality_name_type\":\"suburb\"}",
332                        IDS_LIBADDRESSINPUT_SUBURB),
333         std::make_pair("{\"sublocality_name_type\":\"district\"}",
334                        IDS_LIBADDRESSINPUT_DISTRICT)));
335
336 // Tests for parsing the administrative area name.
337 class AdminAreaNameParseTest
338     : public testing::TestWithParam<std::pair<std::string, int> > {
339  protected:
340   AdminAreaNameParseTest() {}
341   Rule rule_;
342
343  private:
344   DISALLOW_COPY_AND_ASSIGN(AdminAreaNameParseTest);
345 };
346
347 // Verifies that an administrative area name is parsed correctly.
348 TEST_P(AdminAreaNameParseTest, ParsedCorrectly) {
349   ASSERT_TRUE(rule_.ParseSerializedRule(GetParam().first));
350   EXPECT_EQ(GetParam().second, rule_.GetAdminAreaNameMessageId());
351 }
352
353 // Test parsing all administrative area names.
354 INSTANTIATE_TEST_CASE_P(
355     AllAdminAreaNames, AdminAreaNameParseTest,
356     testing::Values(
357         std::make_pair("{\"state_name_type\":\"area\"}",
358                        IDS_LIBADDRESSINPUT_AREA),
359         std::make_pair("{\"state_name_type\":\"county\"}",
360                        IDS_LIBADDRESSINPUT_COUNTY),
361         std::make_pair("{\"state_name_type\":\"department\"}",
362                        IDS_LIBADDRESSINPUT_DEPARTMENT),
363         std::make_pair("{\"state_name_type\":\"district\"}",
364                        IDS_LIBADDRESSINPUT_DISTRICT),
365         std::make_pair("{\"state_name_type\":\"do_si\"}",
366                        IDS_LIBADDRESSINPUT_DO_SI),
367         std::make_pair("{\"state_name_type\":\"emirate\"}",
368                        IDS_LIBADDRESSINPUT_EMIRATE),
369         std::make_pair("{\"state_name_type\":\"island\"}",
370                        IDS_LIBADDRESSINPUT_ISLAND),
371         std::make_pair("{\"state_name_type\":\"parish\"}",
372                        IDS_LIBADDRESSINPUT_PARISH),
373         std::make_pair("{\"state_name_type\":\"prefecture\"}",
374                        IDS_LIBADDRESSINPUT_PREFECTURE),
375         std::make_pair("{\"state_name_type\":\"province\"}",
376                        IDS_LIBADDRESSINPUT_PROVINCE),
377         std::make_pair("{\"state_name_type\":\"state\"}",
378                        IDS_LIBADDRESSINPUT_STATE)));
379
380 // Tests for rule parsing.
381 class RuleParseTest : public testing::TestWithParam<std::string> {
382  protected:
383   RuleParseTest() {}
384
385   const std::string& GetRegionData() const {
386     // GetParam() is either a region code or the region data itself.
387     // RegionDataContants::GetRegionData() returns an empty string for anything
388     // that's not a region code.
389     const std::string& data = RegionDataConstants::GetRegionData(GetParam());
390     return !data.empty() ? data : GetParam();
391   }
392
393   Rule rule_;
394   Localization localization_;
395
396  private:
397   DISALLOW_COPY_AND_ASSIGN(RuleParseTest);
398 };
399
400 // Verifies that a region data can be parsed successfully.
401 TEST_P(RuleParseTest, RegionDataParsedSuccessfully) {
402   EXPECT_TRUE(rule_.ParseSerializedRule(GetRegionData()));
403 }
404
405 // Verifies that the admin area name type corresponds to a UI string.
406 TEST_P(RuleParseTest, AdminAreaNameTypeHasUiString) {
407   const std::string& region_data = GetRegionData();
408   rule_.ParseSerializedRule(region_data);
409   if (region_data.find("state_name_type") != std::string::npos) {
410     EXPECT_NE(INVALID_MESSAGE_ID, rule_.GetAdminAreaNameMessageId());
411     EXPECT_FALSE(
412         localization_.GetString(rule_.GetAdminAreaNameMessageId()).empty());
413   }
414 }
415
416 // Verifies that the postal code name type corresponds to a UI string.
417 TEST_P(RuleParseTest, PostalCodeNameTypeHasUiString) {
418   const std::string& region_data = GetRegionData();
419   rule_.ParseSerializedRule(region_data);
420   if (region_data.find("zip_name_type") != std::string::npos) {
421     EXPECT_NE(INVALID_MESSAGE_ID, rule_.GetPostalCodeNameMessageId());
422     EXPECT_FALSE(
423         localization_.GetString(rule_.GetPostalCodeNameMessageId()).empty());
424   }
425 }
426
427 // Verifies that the locality name type corresponds to a UI string.
428 TEST_P(RuleParseTest, LocalityNameTypeHasUiString) {
429   const std::string& region_data = GetRegionData();
430   rule_.ParseSerializedRule(region_data);
431   // The leading quote here ensures we don't match against sublocality_name_type
432   // in the data.
433   if (region_data.find("\"locality_name_type") != std::string::npos) {
434     EXPECT_NE(INVALID_MESSAGE_ID, rule_.GetLocalityNameMessageId());
435     EXPECT_FALSE(
436         localization_.GetString(rule_.GetLocalityNameMessageId()).empty());
437   }
438 }
439
440 // Verifies that the sublocality name type corresponds to a UI string.
441 TEST_P(RuleParseTest, SublocalityNameTypeHasUiString) {
442   const std::string& region_data = GetRegionData();
443   rule_.ParseSerializedRule(region_data);
444   if (region_data.find("sublocality_name_type") != std::string::npos) {
445     EXPECT_NE(INVALID_MESSAGE_ID, rule_.GetSublocalityNameMessageId());
446     EXPECT_FALSE(
447         localization_.GetString(rule_.GetSublocalityNameMessageId()).empty());
448   }
449 }
450
451 // Verifies that the sole postal code is correctly recognised and copied.
452 TEST_P(RuleParseTest, SolePostalCode) {
453   Rule rule;
454   ASSERT_TRUE(rule.ParseSerializedRule("{\"zip\":\"1234\"}"));
455   EXPECT_TRUE(rule.GetPostalCodeMatcher() != NULL);
456   EXPECT_TRUE(rule.GetSolePostalCode() == "1234");
457
458   Rule copy;
459   EXPECT_TRUE(copy.GetPostalCodeMatcher() == NULL);
460   EXPECT_TRUE(copy.GetSolePostalCode().empty());
461
462   copy.CopyFrom(rule);
463   EXPECT_TRUE(copy.GetPostalCodeMatcher() != NULL);
464   EXPECT_EQ(rule.GetSolePostalCode(), copy.GetSolePostalCode());
465 }
466
467 // Test parsing all region data.
468 INSTANTIATE_TEST_CASE_P(
469     AllRulesTest, RuleParseTest,
470     testing::ValuesIn(RegionDataConstants::GetRegionCodes()));
471
472 // Test parsing the default rule.
473 INSTANTIATE_TEST_CASE_P(
474     DefaultRuleTest, RuleParseTest,
475     testing::Values(RegionDataConstants::GetDefaultRegionData()));
476
477 }  // namespace