1 // Copyright (C) 2014 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/region_data_builder.h>
17 #include <libaddressinput/callback.h>
18 #include <libaddressinput/null_storage.h>
19 #include <libaddressinput/preload_supplier.h>
20 #include <libaddressinput/region_data.h>
21 #include <libaddressinput/util/basictypes.h>
22 #include <libaddressinput/util/scoped_ptr.h>
26 #include <gtest/gtest.h>
28 #include "testdata_source.h"
32 using i18n::addressinput::BuildCallback;
33 using i18n::addressinput::NullStorage;
34 using i18n::addressinput::PreloadSupplier;
35 using i18n::addressinput::RegionData;
36 using i18n::addressinput::RegionDataBuilder;
37 using i18n::addressinput::scoped_ptr;
38 using i18n::addressinput::TestdataSource;
40 class RegionDataBuilderTest : public testing::Test {
42 RegionDataBuilderTest()
43 : supplier_(new TestdataSource(true),
46 loaded_callback_(BuildCallback(this, &RegionDataBuilderTest::OnLoaded)),
49 PreloadSupplier supplier_;
50 RegionDataBuilder builder_;
51 const scoped_ptr<const PreloadSupplier::Callback> loaded_callback_;
52 std::string best_language_;
55 void OnLoaded(bool success, const std::string& region_code, int num_rules) {
57 ASSERT_FALSE(region_code.empty());
58 ASSERT_LT(0, num_rules);
59 ASSERT_TRUE(supplier_.IsLoaded(region_code));
62 DISALLOW_COPY_AND_ASSIGN(RegionDataBuilderTest);
65 TEST_F(RegionDataBuilderTest, BuildUsRegionTree) {
66 supplier_.LoadRules("US", *loaded_callback_);
67 const RegionData& tree = builder_.Build("US", "en-US", &best_language_);
68 EXPECT_FALSE(tree.sub_regions().empty());
71 TEST_F(RegionDataBuilderTest, BuildCnRegionTree) {
72 supplier_.LoadRules("CN", *loaded_callback_);
73 const RegionData& tree = builder_.Build("CN", "zh-Hans", &best_language_);
74 ASSERT_FALSE(tree.sub_regions().empty());
75 EXPECT_FALSE(tree.sub_regions().front()->sub_regions().empty());
78 TEST_F(RegionDataBuilderTest, BuildChRegionTree) {
79 supplier_.LoadRules("CH", *loaded_callback_);
80 const RegionData& tree = builder_.Build("CH", "de-CH", &best_language_);
81 // Although "CH" has information for its administrative divisions, the
82 // administrative area field is not used, which results in an empty tree of
84 EXPECT_TRUE(tree.sub_regions().empty());
87 TEST_F(RegionDataBuilderTest, BuildZwRegionTree) {
88 supplier_.LoadRules("ZW", *loaded_callback_);
89 const RegionData& tree = builder_.Build("ZW", "en-ZW", &best_language_);
90 EXPECT_TRUE(tree.sub_regions().empty());
93 TEST_F(RegionDataBuilderTest, UsTreeHasStateAbbreviationsAndNames) {
94 supplier_.LoadRules("US", *loaded_callback_);
95 const RegionData& tree = builder_.Build("US", "en-US", &best_language_);
96 EXPECT_EQ("en", best_language_);
97 ASSERT_FALSE(tree.sub_regions().empty());
98 EXPECT_EQ("AL", tree.sub_regions().front()->key());
99 EXPECT_EQ("Alabama", tree.sub_regions().front()->name());
102 TEST_F(RegionDataBuilderTest,
103 KrWithKoLatnLanguageHasKoreanKeysAndLatinScriptNames) {
104 supplier_.LoadRules("KR", *loaded_callback_);
105 const RegionData& tree = builder_.Build("KR", "ko-Latn", &best_language_);
106 EXPECT_EQ("ko-Latn", best_language_);
107 ASSERT_FALSE(tree.sub_regions().empty());
109 "\xEA\xB0\x95\xEC\x9B\x90\xEB\x8F\x84", /* "강원도" */
110 tree.sub_regions().front()->key());
111 EXPECT_EQ("Gangwon", tree.sub_regions().front()->name());
114 TEST_F(RegionDataBuilderTest, KrWithKoKrLanguageHasKoreanKeysAndNames) {
115 supplier_.LoadRules("KR", *loaded_callback_);
116 const RegionData& tree = builder_.Build("KR", "ko-KR", &best_language_);
117 EXPECT_EQ("ko", best_language_);
118 ASSERT_FALSE(tree.sub_regions().empty());
120 "\xEA\xB0\x95\xEC\x9B\x90\xEB\x8F\x84", /* "강원도" */
121 tree.sub_regions().front()->key());
123 "\xEA\xB0\x95\xEC\x9B\x90", /* "강원" */
124 tree.sub_regions().front()->name());