Home | History | Annotate | Download | only in test
      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 <libaddressinput/address_ui.h>
     16 
     17 #include <libaddressinput/address_field.h>
     18 #include <libaddressinput/address_ui_component.h>
     19 
     20 #include <set>
     21 #include <string>
     22 #include <vector>
     23 
     24 #include <gtest/gtest.h>
     25 
     26 #include "grit.h"
     27 
     28 namespace i18n {
     29 namespace addressinput {
     30 
     31 namespace {
     32 
     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";
     38   }
     39 
     40   std::set<AddressField> fields;
     41   for (std::vector<AddressUiComponent>::const_iterator
     42            component_it = components.begin();
     43        component_it != components.end();
     44        ++component_it) {
     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;
     51     }
     52 
     53     if (fields.find(component_it->field) != fields.end()) {
     54       return testing::AssertionFailure() << "duplicate input field "
     55                                          << component_it->field;
     56     }
     57     fields.insert(component_it->field);
     58 
     59     if (component_it->name_id == INVALID_MESSAGE_ID) {
     60       return testing::AssertionFailure() << "invalid field name_id for field "
     61                                          << component_it->field;
     62     }
     63   }
     64 
     65   return testing::AssertionSuccess();
     66 }
     67 
     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
     70 //    code.
     71 // 3) Verifies that BuildComponents() returns a non-empty vector for a region
     72 //    code.
     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)));
     80   }
     81 }
     82 
     83 // Verifies that BuildComponents() returns an empty
     84 // vector for an invalid region code.
     85 TEST(AddressUiTest, InvalidRegionCodeReturnsEmptyVector) {
     86   EXPECT_TRUE(
     87       BuildComponents("INVALID-REGION-CODE", std::string(), NULL).empty());
     88 }
     89 
     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) {}
     95 
     96   ~SeparatorData() {}
     97 
     98   std::string language_code;
     99   std::string compact_line_separator;
    100 };
    101 
    102 // Tests for compact line separator.
    103 class CompactLineSeparatorTest
    104   : public testing::TestWithParam<SeparatorData> {};
    105 
    106 TEST_P(CompactLineSeparatorTest, BasicTest) {
    107   EXPECT_EQ(GetParam().compact_line_separator,
    108             GetCompactAddressLinesSeparator(GetParam().language_code));
    109 }
    110 
    111 INSTANTIATE_TEST_CASE_P(
    112     CompactLineSeparators, CompactLineSeparatorTest,
    113     testing::Values(
    114         SeparatorData("ja", ""),
    115         SeparatorData("zh", ""),
    116         SeparatorData("zh-hans", ""),
    117         SeparatorData("ar", " "),
    118         SeparatorData("ko", " "),
    119         SeparatorData("th", " "),
    120         SeparatorData("en", ", ")));
    121 
    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[] = {
    128     {"AM", "", "hy"},
    129     {"AM", "hy", "hy"},
    130     {"AM", "en", "hy-latn"},
    131     {"CN", "zh-hans", "zh-hans"},
    132     {"CN", "zh-hant", "zh-hant"},
    133     {"CN", "zh", "zh"},
    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"},
    141     {"HK", "zh", "zh"},
    142     {"HK", "zh-hans", "zh-hans"},
    143     {"HK", "zh-hant", "zh-hant"},
    144     {"HK", "zh-latn", "zh-latn"},
    145     {"HK", "en", "en"},
    146     {"HK", "fr", "zh-latn"},
    147     {"HK", "ja", "zh-latn"},
    148     {"HK", "ko", "zh-latn"},
    149     {"MO", "zh", "zh"},
    150     {"MO", "pt", "pt"},
    151     {"MO", "en", "zh-latn"},
    152     {"AQ", "en", "en"},
    153     {"AQ", "fr", "fr"},
    154     {"AQ", "es", "es"},
    155     {"AQ", "zh", "zh"}
    156   };
    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());
    168     EXPECT_EQ(
    169         kLangugeCodes[i].components_language_code, components_language_code);
    170   }
    171 }
    172 
    173 }  // namespace
    174 
    175 }  // namespace addressinput
    176 }  // namespace i18n
    177