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/address_normalizer.h>
16
17#include <libaddressinput/address_data.h>
18#include <libaddressinput/callback.h>
19#include <libaddressinput/null_storage.h>
20#include <libaddressinput/preload_supplier.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::AddressData;
33using i18n::addressinput::AddressNormalizer;
34using i18n::addressinput::BuildCallback;
35using i18n::addressinput::NullStorage;
36using i18n::addressinput::PreloadSupplier;
37using i18n::addressinput::scoped_ptr;
38using i18n::addressinput::TestdataSource;
39
40class AddressNormalizerTest : public testing::Test {
41 protected:
42  AddressNormalizerTest()
43      : supplier_(new TestdataSource(true), new NullStorage),
44        loaded_(BuildCallback(this, &AddressNormalizerTest::OnLoaded)),
45        normalizer_(&supplier_) {}
46
47  PreloadSupplier supplier_;
48  const scoped_ptr<const PreloadSupplier::Callback> loaded_;
49  const AddressNormalizer normalizer_;
50
51 private:
52  void OnLoaded(bool success, const std::string& region_code, int num_rules) {
53    ASSERT_TRUE(success);
54    ASSERT_FALSE(region_code.empty());
55    ASSERT_LT(0, num_rules);
56  }
57
58  DISALLOW_COPY_AND_ASSIGN(AddressNormalizerTest);
59};
60
61TEST_F(AddressNormalizerTest, CaliforniaShortNameCa) {
62  supplier_.LoadRules("US", *loaded_);
63  AddressData address;
64  address.language_code = "en-US";
65  address.region_code = "US";
66  address.administrative_area = "California";
67  address.locality = "Mountain View";
68  normalizer_.Normalize(&address);
69  EXPECT_EQ("CA", address.administrative_area);
70}
71
72TEST_F(AddressNormalizerTest, GangwonLatinNameStaysUnchanged) {
73  supplier_.LoadRules("KR", *loaded_);
74  AddressData address;
75  address.language_code = "ko-Latn";
76  address.region_code = "KR";
77  address.administrative_area = "Gangwon";
78  normalizer_.Normalize(&address);
79  EXPECT_EQ("Gangwon", address.administrative_area);
80}
81
82TEST_F(AddressNormalizerTest, GangwonKoreanName) {
83  supplier_.LoadRules("KR", *loaded_);
84  AddressData address;
85  address.language_code = "ko-KR";
86  address.region_code = "KR";
87  address.administrative_area = "\xEA\xB0\x95\xEC\x9B\x90";  /* "강원" */
88  normalizer_.Normalize(&address);
89  EXPECT_EQ(
90      "\xEA\xB0\x95\xEC\x9B\x90\xEB\x8F\x84",  /* "강원도" */
91      address.administrative_area);
92}
93
94TEST_F(AddressNormalizerTest, DontSwitchLatinScriptForUnknownLanguage) {
95  supplier_.LoadRules("KR", *loaded_);
96  AddressData address;
97  address.region_code = "KR";
98  address.administrative_area = "Gangwon";
99  normalizer_.Normalize(&address);
100  EXPECT_EQ("Gangwon", address.administrative_area);
101}
102
103TEST_F(AddressNormalizerTest, DontSwitchLocalScriptForUnknownLanguage) {
104  supplier_.LoadRules("KR", *loaded_);
105  AddressData address;
106  address.region_code = "KR";
107  address.administrative_area = "\xEA\xB0\x95\xEC\x9B\x90";  /* "강원" */
108  normalizer_.Normalize(&address);
109  EXPECT_EQ(
110      "\xEA\xB0\x95\xEC\x9B\x90\xEB\x8F\x84",  /* "강원도" */
111      address.administrative_area);
112}
113
114}  // namespace
115