a60189f1054c8a485bd3d518deb8ef5663421294
[platform/framework/web/crosswalk.git] / src / third_party / libaddressinput / src / cpp / test / region_data_builder_test.cc
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
30 namespace {
31
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;
39
40 class 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   virtual ~RegionDataBuilderTest() {}
50
51   PreloadSupplier supplier_;
52   RegionDataBuilder builder_;
53   const scoped_ptr<const PreloadSupplier::Callback> loaded_callback_;
54   std::string best_language_;
55
56  private:
57   void OnLoaded(bool success, const std::string& region_code, int num_rules) {
58     ASSERT_TRUE(success);
59     ASSERT_FALSE(region_code.empty());
60     ASSERT_LT(0, num_rules);
61     ASSERT_TRUE(supplier_.IsLoaded(region_code));
62   }
63
64   DISALLOW_COPY_AND_ASSIGN(RegionDataBuilderTest);
65 };
66
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());
71 }
72
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());
78 }
79
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
85   // sub-regions.
86   EXPECT_TRUE(tree.sub_regions().empty());
87 }
88
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());
93 }
94
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());
102 }
103
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());
110   EXPECT_EQ(
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());
114 }
115
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());
121   EXPECT_EQ(
122       "\xEA\xB0\x95\xEC\x9B\x90\xEB\x8F\x84",  /* "강원도" */
123       tree.sub_regions().front()->key());
124   EXPECT_EQ(
125       "\xEA\xB0\x95\xEC\x9B\x90",  /* "강원" */
126       tree.sub_regions().front()->name());
127 }
128
129 }  // namespace