Home | History | Annotate | Download | only in test
      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 "fake_downloader.h"
     25 
     26 #include <gtest/gtest.h>
     27 
     28 namespace {
     29 
     30 using i18n::addressinput::AddressData;
     31 using i18n::addressinput::AddressNormalizer;
     32 using i18n::addressinput::BuildCallback;
     33 using i18n::addressinput::FakeDownloader;
     34 using i18n::addressinput::NullStorage;
     35 using i18n::addressinput::PreloadSupplier;
     36 using i18n::addressinput::scoped_ptr;
     37 
     38 class AddressNormalizerTest : public testing::Test {
     39  protected:
     40   AddressNormalizerTest()
     41       : supplier_(FakeDownloader::kFakeAggregateDataUrl,
     42                   new FakeDownloader,
     43                   new NullStorage),
     44         loaded_(BuildCallback(this, &AddressNormalizerTest::OnLoaded)),
     45         normalizer_(&supplier_) {}
     46 
     47   virtual ~AddressNormalizerTest() {}
     48 
     49   PreloadSupplier supplier_;
     50   const scoped_ptr<const PreloadSupplier::Callback> loaded_;
     51   const AddressNormalizer normalizer_;
     52 
     53  private:
     54   void OnLoaded(bool success, const std::string& region_code, int num_rules) {
     55     ASSERT_TRUE(success);
     56     ASSERT_FALSE(region_code.empty());
     57     ASSERT_LT(0, num_rules);
     58   }
     59 
     60   DISALLOW_COPY_AND_ASSIGN(AddressNormalizerTest);
     61 };
     62 
     63 TEST_F(AddressNormalizerTest, CaliforniaShortNameCa) {
     64   supplier_.LoadRules("US", *loaded_);
     65   AddressData address;
     66   address.language_code = "en-US";
     67   address.region_code = "US";
     68   address.administrative_area = "California";
     69   address.locality = "Mountain View";
     70   normalizer_.Normalize(&address);
     71   EXPECT_EQ("CA", address.administrative_area);
     72 }
     73 
     74 TEST_F(AddressNormalizerTest, GangwonLatinNameStaysUnchanged) {
     75   supplier_.LoadRules("KR", *loaded_);
     76   AddressData address;
     77   address.language_code = "ko-Latn";
     78   address.region_code = "KR";
     79   address.administrative_area = "Gangwon";
     80   normalizer_.Normalize(&address);
     81   EXPECT_EQ("Gangwon", address.administrative_area);
     82 }
     83 
     84 TEST_F(AddressNormalizerTest, GangwonKoreanName) {
     85   supplier_.LoadRules("KR", *loaded_);
     86   AddressData address;
     87   address.language_code = "ko-KR";
     88   address.region_code = "KR";
     89   address.administrative_area = "";
     90   normalizer_.Normalize(&address);
     91   EXPECT_EQ("", address.administrative_area);
     92 }
     93 
     94 
     95 TEST_F(AddressNormalizerTest, DontSwitchLatinScriptForUnknownLanguage) {
     96   supplier_.LoadRules("KR", *loaded_);
     97   AddressData address;
     98   address.region_code = "KR";
     99   address.administrative_area = "Gangwon";
    100   normalizer_.Normalize(&address);
    101   EXPECT_EQ("Gangwon", address.administrative_area);
    102 }
    103 
    104 TEST_F(AddressNormalizerTest, DontSwitchLocalScriptForUnknownLanguage) {
    105   supplier_.LoadRules("KR", *loaded_);
    106   AddressData address;
    107   address.region_code = "KR";
    108   address.administrative_area = "";
    109   normalizer_.Normalize(&address);
    110   EXPECT_EQ("", address.administrative_area);
    111 }
    112 
    113 }  // namespace
    114