OLD | NEW |
1 // Copyright (C) 2013 Google Inc. | 1 // Copyright (C) 2013 Google Inc. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 #include <libaddressinput/address_ui.h> | 15 #include <libaddressinput/address_ui.h> |
16 | 16 |
17 #include <libaddressinput/address_field.h> | 17 #include <libaddressinput/address_field.h> |
18 #include <libaddressinput/address_ui_component.h> | 18 #include <libaddressinput/address_ui_component.h> |
19 #include <libaddressinput/localization.h> | |
20 | 19 |
21 #include <string> | 20 #include <string> |
22 #include <vector> | 21 #include <vector> |
23 | 22 |
24 #include <gtest/gtest.h> | 23 #include <gtest/gtest.h> |
25 | 24 |
| 25 #include "grit.h" |
| 26 |
| 27 namespace i18n { |
| 28 namespace addressinput { |
| 29 |
26 namespace { | 30 namespace { |
27 | 31 |
28 using i18n::addressinput::AddressField; | |
29 using i18n::addressinput::AddressUiComponent; | |
30 using i18n::addressinput::BuildComponents; | |
31 using i18n::addressinput::COUNTRY; | |
32 using i18n::addressinput::GetRegionCodes; | |
33 using i18n::addressinput::Localization; | |
34 using i18n::addressinput::RECIPIENT; | |
35 | |
36 // Returns testing::AssertionSuccess if the |components| are valid. Uses | 32 // Returns testing::AssertionSuccess if the |components| are valid. Uses |
37 // |region_code| in test failure messages. | 33 // |region_code| in test failure messages. |
38 testing::AssertionResult ComponentsAreValid( | 34 testing::AssertionResult ComponentsAreValid( |
39 const std::vector<AddressUiComponent>& components) { | 35 const std::vector<AddressUiComponent>& components) { |
40 if (components.empty()) { | 36 if (components.empty()) { |
41 return testing::AssertionFailure() << "no components"; | 37 return testing::AssertionFailure() << "no components"; |
42 } | 38 } |
43 | 39 |
44 for (std::vector<AddressUiComponent>::const_iterator | 40 for (std::vector<AddressUiComponent>::const_iterator |
45 component_it = components.begin(); | 41 component_it = components.begin(); |
46 component_it != components.end(); ++component_it) { | 42 component_it != components.end(); ++component_it) { |
47 static const AddressField kMinAddressField = COUNTRY; | 43 static const AddressField kMinAddressField = COUNTRY; |
48 static const AddressField kMaxAddressField = RECIPIENT; | 44 static const AddressField kMaxAddressField = RECIPIENT; |
49 if (component_it->field < kMinAddressField || | 45 if (component_it->field < kMinAddressField || |
50 component_it->field > kMaxAddressField) { | 46 component_it->field > kMaxAddressField) { |
51 return testing::AssertionFailure() << "unexpected field " | 47 return testing::AssertionFailure() << "unexpected field " |
52 << component_it->field; | 48 << component_it->field; |
53 } | 49 } |
54 | 50 |
55 if (component_it->name.empty()) { | 51 if (component_it->name_id == INVALID_MESSAGE_ID) { |
56 return testing::AssertionFailure() << "empty field name for field " | 52 return testing::AssertionFailure() << "invalid field name_id for field " |
57 << component_it->field; | 53 << component_it->field; |
58 } | 54 } |
59 } | 55 } |
60 | 56 |
61 return testing::AssertionSuccess(); | 57 return testing::AssertionSuccess(); |
62 } | 58 } |
63 | 59 |
64 // Tests for address UI functions. | 60 // Tests for address UI functions. |
65 class AddressUiTest : public testing::TestWithParam<std::string> { | 61 class AddressUiTest : public testing::TestWithParam<std::string> {}; |
66 protected: | |
67 Localization localization_; | |
68 }; | |
69 | 62 |
70 // Verifies that a region code consists of two characters, for example "TW". | 63 // Verifies that a region code consists of two characters, for example "TW". |
71 TEST_P(AddressUiTest, RegionCodeHasTwoCharacters) { | 64 TEST_P(AddressUiTest, RegionCodeHasTwoCharacters) { |
72 EXPECT_EQ(2, GetParam().size()); | 65 EXPECT_EQ(2, GetParam().size()); |
73 } | 66 } |
74 | 67 |
75 // Verifies that BuildComponents() returns valid UI components for a region | 68 // Verifies that BuildComponents() returns valid UI components for a region |
76 // code. | 69 // code. |
77 TEST_P(AddressUiTest, ComponentsAreValid) { | 70 TEST_P(AddressUiTest, ComponentsAreValid) { |
78 EXPECT_TRUE(ComponentsAreValid(BuildComponents(GetParam(), localization_))); | 71 EXPECT_TRUE(ComponentsAreValid(BuildComponents(GetParam()))); |
79 } | 72 } |
80 | 73 |
81 // Test all regions codes. | 74 // Test all regions codes. |
82 INSTANTIATE_TEST_CASE_P( | 75 INSTANTIATE_TEST_CASE_P( |
83 AllRegions, AddressUiTest, | 76 AllRegions, AddressUiTest, |
84 testing::ValuesIn(GetRegionCodes())); | 77 testing::ValuesIn(GetRegionCodes())); |
85 | 78 |
86 // Verifies that BuildComponents() returns an empty vector for an invalid region | 79 // Verifies that BuildComponents() returns an empty vector for an invalid region |
87 // code. | 80 // code. |
88 TEST_F(AddressUiTest, InvalidRegionCodeReturnsEmptyVector) { | 81 TEST_F(AddressUiTest, InvalidRegionCodeReturnsEmptyVector) { |
89 EXPECT_TRUE(BuildComponents("INVALID-REGION-CODE", localization_).empty()); | 82 EXPECT_TRUE(BuildComponents("INVALID-REGION-CODE").empty()); |
90 } | 83 } |
91 | 84 |
92 } // namespace | 85 } // namespace |
| 86 |
| 87 } // namespace addressinput |
| 88 } // namespace i18n |
OLD | NEW |