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 <libaddressinput/address_ui.h>
17 #include <libaddressinput/address_field.h>
18 #include <libaddressinput/address_ui_component.h>
24 #include <gtest/gtest.h>
29 namespace addressinput {
33 // Returns testing::AssertionSuccess if the |components| are valid.
34 testing::AssertionResult ComponentsAreValid(
35 const std::vector<AddressUiComponent>& components) {
36 if (components.empty()) {
37 return testing::AssertionFailure() << "no components";
40 std::set<AddressField> fields;
41 for (std::vector<AddressUiComponent>::const_iterator
42 component_it = components.begin();
43 component_it != components.end();
45 static const AddressField kMinAddressField = COUNTRY;
46 static const AddressField kMaxAddressField = RECIPIENT;
47 if (component_it->field < kMinAddressField ||
48 component_it->field > kMaxAddressField) {
49 return testing::AssertionFailure() << "unexpected input field "
50 << component_it->field;
53 if (fields.find(component_it->field) != fields.end()) {
54 return testing::AssertionFailure() << "duplicate input field "
55 << component_it->field;
57 fields.insert(component_it->field);
59 if (component_it->name_id == INVALID_MESSAGE_ID) {
60 return testing::AssertionFailure() << "invalid field name_id for field "
61 << component_it->field;
65 return testing::AssertionSuccess();
68 // 1) Verifies that a region code consists of two characters, for example "TW".
69 // 2) Verifies that BuildComponents() returns valid UI components for a region
71 // 3) Verifies that BuildComponents() returns a non-empty vector for a region
73 TEST(AddressUiTest, RegionsAndComponentsAreValid) {
74 const std::vector<std::string>& region_codes = GetRegionCodes();
75 for (size_t i = 0; i < region_codes.size(); ++i) {
76 SCOPED_TRACE("Region code: " + region_codes[i]);
77 EXPECT_EQ(2U, region_codes[i].size());
78 EXPECT_TRUE(ComponentsAreValid(
79 BuildComponents(region_codes[i], std::string(), NULL)));
83 // Verifies that BuildComponents() returns an empty
84 // vector for an invalid region code.
85 TEST(AddressUiTest, InvalidRegionCodeReturnsEmptyVector) {
87 BuildComponents("INVALID-REGION-CODE", std::string(), NULL).empty());
90 struct SeparatorData {
91 SeparatorData(const std::string& language_code,
92 const std::string& compact_line_separator)
93 : language_code(language_code),
94 compact_line_separator(compact_line_separator) {}
98 std::string language_code;
99 std::string compact_line_separator;
102 // Tests for compact line separator.
103 class CompactLineSeparatorTest
104 : public testing::TestWithParam<SeparatorData> {};
106 TEST_P(CompactLineSeparatorTest, BasicTest) {
107 EXPECT_EQ(GetParam().compact_line_separator,
108 GetCompactAddressLinesSeparator(GetParam().language_code));
111 INSTANTIATE_TEST_CASE_P(
112 CompactLineSeparators, CompactLineSeparatorTest,
114 SeparatorData("ja", ""),
115 SeparatorData("zh", ""),
116 SeparatorData("zh-hans", ""),
117 SeparatorData("ar", "، "),
118 SeparatorData("ko", " "),
119 SeparatorData("th", " "),
120 SeparatorData("en", ", ")));
122 TEST(AddressUiTest, ComponentLanguageCodeTest) {
123 static const struct LanguageCodeData {
124 const char* region_code;
125 const char* ui_language_code;
126 const char* components_language_code;
127 } kLangugeCodes[] = {
130 {"AM", "en", "hy-latn"},
131 {"CN", "zh-hans", "zh-hans"},
132 {"CN", "zh-hant", "zh-hant"},
134 {"CN", "zh-latn", "zh-latn"},
135 {"CN", "zh-Latn", "zh-latn"},
136 {"CN", "zh-latn-US", "zh-latn"},
137 {"CN", "zh-Latn-US", "zh-latn"},
138 {"CN", "en", "zh-latn"},
139 {"CN", "ja", "zh-latn"},
140 {"CN", "ko", "zh-latn"},
142 {"HK", "zh-hans", "zh-hans"},
143 {"HK", "zh-hant", "zh-hant"},
144 {"HK", "zh-latn", "zh-latn"},
146 {"HK", "fr", "zh-latn"},
147 {"HK", "ja", "zh-latn"},
148 {"HK", "ko", "zh-latn"},
151 {"MO", "en", "zh-latn"},
157 static const size_t kArraySize =
158 sizeof kLangugeCodes / sizeof (LanguageCodeData);
159 for (size_t i = 0; i < kArraySize; ++i) {
160 SCOPED_TRACE(std::string("region code = ") +
161 kLangugeCodes[i].region_code + ", ui language code = " +
162 kLangugeCodes[i].ui_language_code + ", components language code = " +
163 kLangugeCodes[i].components_language_code);
164 std::string components_language_code;
165 EXPECT_FALSE(BuildComponents(kLangugeCodes[i].region_code,
166 kLangugeCodes[i].ui_language_code,
167 &components_language_code).empty());
169 kLangugeCodes[i].components_language_code, components_language_code);
175 } // namespace addressinput