1// Copyright (C) 2014 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/region_data_builder.h>
16
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>
23
24#include <string>
25
26#include <gtest/gtest.h>
27
28#include "testdata_source.h"
29
30namespace {
31
32using i18n::addressinput::BuildCallback;
33using i18n::addressinput::NullStorage;
34using i18n::addressinput::PreloadSupplier;
35using i18n::addressinput::RegionData;
36using i18n::addressinput::RegionDataBuilder;
37using i18n::addressinput::scoped_ptr;
38using i18n::addressinput::TestdataSource;
39
40class RegionDataBuilderTest : public testing::Test {
41 protected:
42  RegionDataBuilderTest()
43      : supplier_(new TestdataSource(true),
44                  new NullStorage),
45        builder_(&supplier_),
46        loaded_callback_(BuildCallback(this, &RegionDataBuilderTest::OnLoaded)),
47        best_language_() {}
48
49  PreloadSupplier supplier_;
50  RegionDataBuilder builder_;
51  const scoped_ptr<const PreloadSupplier::Callback> loaded_callback_;
52  std::string best_language_;
53
54 private:
55  void OnLoaded(bool success, const std::string& region_code, int num_rules) {
56    ASSERT_TRUE(success);
57    ASSERT_FALSE(region_code.empty());
58    ASSERT_LT(0, num_rules);
59    ASSERT_TRUE(supplier_.IsLoaded(region_code));
60  }
61
62  DISALLOW_COPY_AND_ASSIGN(RegionDataBuilderTest);
63};
64
65TEST_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());
69}
70
71TEST_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());
76}
77
78TEST_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
83  // sub-regions.
84  EXPECT_TRUE(tree.sub_regions().empty());
85}
86
87TEST_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());
91}
92
93TEST_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());
100}
101
102TEST_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());
108  EXPECT_EQ(
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());
112}
113
114TEST_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());
119  EXPECT_EQ(
120      "\xEA\xB0\x95\xEC\x9B\x90\xEB\x8F\x84",  /* "강원도" */
121      tree.sub_regions().front()->key());
122  EXPECT_EQ(
123      "\xEA\xB0\x95\xEC\x9B\x90",  /* "강원" */
124      tree.sub_regions().front()->name());
125}
126
127}  // namespace
128