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 virtual ~RegionDataBuilderTest() {}
51 PreloadSupplier supplier_;
52 RegionDataBuilder builder_;
53 const scoped_ptr<const PreloadSupplier::Callback> loaded_callback_;
54 std::string best_language_;
57 void OnLoaded(bool success, const std::string& region_code, int num_rules) {
59 ASSERT_FALSE(region_code.empty());
60 ASSERT_LT(0, num_rules);
61 ASSERT_TRUE(supplier_.IsLoaded(region_code));
64 DISALLOW_COPY_AND_ASSIGN(RegionDataBuilderTest);
67 TEST_F(RegionDataBuilderTest, BuildUsRegionTree) {
68 supplier_.LoadRules("US", *loaded_callback_);
69 const RegionData& tree = builder_.Build("US", "en-US", &best_language_);
70 EXPECT_FALSE(tree.sub_regions().empty());
73 TEST_F(RegionDataBuilderTest, BuildCnRegionTree) {
74 supplier_.LoadRules("CN", *loaded_callback_);
75 const RegionData& tree = builder_.Build("CN", "zh-Hans", &best_language_);
76 ASSERT_FALSE(tree.sub_regions().empty());
77 EXPECT_FALSE(tree.sub_regions().front()->sub_regions().empty());
80 TEST_F(RegionDataBuilderTest, BuildChRegionTree) {
81 supplier_.LoadRules("CH", *loaded_callback_);
82 const RegionData& tree = builder_.Build("CH", "de-CH", &best_language_);
83 // Although "CH" has information for its administrative divisions, the
84 // administrative area field is not used, which results in an empty tree of
86 EXPECT_TRUE(tree.sub_regions().empty());
89 TEST_F(RegionDataBuilderTest, BuildZwRegionTree) {
90 supplier_.LoadRules("ZW", *loaded_callback_);
91 const RegionData& tree = builder_.Build("ZW", "en-ZW", &best_language_);
92 EXPECT_TRUE(tree.sub_regions().empty());
95 TEST_F(RegionDataBuilderTest, UsTreeHasStateAbbreviationsAndNames) {
96 supplier_.LoadRules("US", *loaded_callback_);
97 const RegionData& tree = builder_.Build("US", "en-US", &best_language_);
98 EXPECT_EQ("en", best_language_);
99 ASSERT_FALSE(tree.sub_regions().empty());
100 EXPECT_EQ("AL", tree.sub_regions().front()->key());
101 EXPECT_EQ("Alabama", tree.sub_regions().front()->name());
104 TEST_F(RegionDataBuilderTest,
105 KrWithKoLatnLanguageHasKoreanKeysAndLatinScriptNames) {
106 supplier_.LoadRules("KR", *loaded_callback_);
107 const RegionData& tree = builder_.Build("KR", "ko-Latn", &best_language_);
108 EXPECT_EQ("ko-Latn", best_language_);
109 ASSERT_FALSE(tree.sub_regions().empty());
111 "\xEA\xB0\x95\xEC\x9B\x90\xEB\x8F\x84", /* "강원도" */
112 tree.sub_regions().front()->key());
113 EXPECT_EQ("Gangwon", tree.sub_regions().front()->name());
116 TEST_F(RegionDataBuilderTest, KrWithKoKrLanguageHasKoreanKeysAndNames) {
117 supplier_.LoadRules("KR", *loaded_callback_);
118 const RegionData& tree = builder_.Build("KR", "ko-KR", &best_language_);
119 EXPECT_EQ("ko", best_language_);
120 ASSERT_FALSE(tree.sub_regions().empty());
122 "\xEA\xB0\x95\xEC\x9B\x90\xEB\x8F\x84", /* "강원도" */
123 tree.sub_regions().front()->key());
125 "\xEA\xB0\x95\xEC\x9B\x90", /* "강원" */
126 tree.sub_regions().front()->name());