| 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 |
| 26 namespace { | 27 namespace { |
| 27 | 28 |
| 28 using i18n::addressinput::AddressField; | 29 using i18n::addressinput::AddressField; |
| 29 using i18n::addressinput::AddressUiComponent; | 30 using i18n::addressinput::AddressUiComponent; |
| 30 using i18n::addressinput::BuildComponents; | 31 using i18n::addressinput::BuildComponents; |
| 31 using i18n::addressinput::COUNTRY; | 32 using i18n::addressinput::COUNTRY; |
| 32 using i18n::addressinput::GetRegionCodes; | 33 using i18n::addressinput::GetRegionCodes; |
| 33 using i18n::addressinput::Localization; | |
| 34 using i18n::addressinput::RECIPIENT; | 34 using i18n::addressinput::RECIPIENT; |
| 35 | 35 |
| 36 // Returns testing::AssertionSuccess if the |components| are valid. Uses | 36 // Returns testing::AssertionSuccess if the |components| are valid. Uses |
| 37 // |region_code| in test failure messages. | 37 // |region_code| in test failure messages. |
| 38 testing::AssertionResult ComponentsAreValid( | 38 testing::AssertionResult ComponentsAreValid( |
| 39 const std::vector<AddressUiComponent>& components) { | 39 const std::vector<AddressUiComponent>& components) { |
| 40 if (components.empty()) { | 40 if (components.empty()) { |
| 41 return testing::AssertionFailure() << "no components"; | 41 return testing::AssertionFailure() << "no components"; |
| 42 } | 42 } |
| 43 | 43 |
| 44 for (std::vector<AddressUiComponent>::const_iterator | 44 for (std::vector<AddressUiComponent>::const_iterator |
| 45 component_it = components.begin(); | 45 component_it = components.begin(); |
| 46 component_it != components.end(); ++component_it) { | 46 component_it != components.end(); ++component_it) { |
| 47 static const AddressField kMinAddressField = COUNTRY; | 47 static const AddressField kMinAddressField = COUNTRY; |
| 48 static const AddressField kMaxAddressField = RECIPIENT; | 48 static const AddressField kMaxAddressField = RECIPIENT; |
| 49 if (component_it->field < kMinAddressField || | 49 if (component_it->field < kMinAddressField || |
| 50 component_it->field > kMaxAddressField) { | 50 component_it->field > kMaxAddressField) { |
| 51 return testing::AssertionFailure() << "unexpected field " | 51 return testing::AssertionFailure() << "unexpected field " |
| 52 << component_it->field; | 52 << component_it->field; |
| 53 } | 53 } |
| 54 | 54 |
| 55 if (component_it->name.empty()) { | 55 if (component_it->name_id == INVALID_MESSAGE_ID) { |
| 56 return testing::AssertionFailure() << "empty field name for field " | 56 return testing::AssertionFailure() << "invalid field name_id for field " |
| 57 << component_it->field; | 57 << component_it->field; |
| 58 } | 58 } |
| 59 } | 59 } |
| 60 | 60 |
| 61 return testing::AssertionSuccess(); | 61 return testing::AssertionSuccess(); |
| 62 } | 62 } |
| 63 | 63 |
| 64 // Tests for address UI functions. | 64 // Tests for address UI functions. |
| 65 class AddressUiTest : public testing::TestWithParam<std::string> { | 65 class AddressUiTest : public testing::TestWithParam<std::string> {}; |
| 66 protected: | |
| 67 Localization localization_; | |
| 68 }; | |
| 69 | 66 |
| 70 // Verifies that a region code consists of two characters, for example "TW". | 67 // Verifies that a region code consists of two characters, for example "TW". |
| 71 TEST_P(AddressUiTest, RegionCodeHasTwoCharacters) { | 68 TEST_P(AddressUiTest, RegionCodeHasTwoCharacters) { |
| 72 EXPECT_EQ(2, GetParam().size()); | 69 EXPECT_EQ(2, GetParam().size()); |
| 73 } | 70 } |
| 74 | 71 |
| 75 // Verifies that BuildComponents() returns valid UI components for a region | 72 // Verifies that BuildComponents() returns valid UI components for a region |
| 76 // code. | 73 // code. |
| 77 TEST_P(AddressUiTest, ComponentsAreValid) { | 74 TEST_P(AddressUiTest, ComponentsAreValid) { |
| 78 EXPECT_TRUE(ComponentsAreValid(BuildComponents(GetParam(), localization_))); | 75 EXPECT_TRUE(ComponentsAreValid(BuildComponents(GetParam()))); |
| 79 } | 76 } |
| 80 | 77 |
| 81 // Test all regions codes. | 78 // Test all regions codes. |
| 82 INSTANTIATE_TEST_CASE_P( | 79 INSTANTIATE_TEST_CASE_P( |
| 83 AllRegions, AddressUiTest, | 80 AllRegions, AddressUiTest, |
| 84 testing::ValuesIn(GetRegionCodes())); | 81 testing::ValuesIn(GetRegionCodes())); |
| 85 | 82 |
| 86 // Verifies that BuildComponents() returns an empty vector for an invalid region | 83 // Verifies that BuildComponents() returns an empty vector for an invalid region |
| 87 // code. | 84 // code. |
| 88 TEST_F(AddressUiTest, InvalidRegionCodeReturnsEmptyVector) { | 85 TEST_F(AddressUiTest, InvalidRegionCodeReturnsEmptyVector) { |
| 89 EXPECT_TRUE(BuildComponents("INVALID-REGION-CODE", localization_).empty()); | 86 EXPECT_TRUE(BuildComponents("INVALID-REGION-CODE").empty()); |
| 90 } | 87 } |
| 91 | 88 |
| 92 } // namespace | 89 } // namespace |
| OLD | NEW |