1 // Copyright (C) 2013 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 "country_rules_aggregator.h" 16 17 #include <libaddressinput/callback.h> 18 #include <libaddressinput/downloader.h> 19 #include <libaddressinput/storage.h> 20 #include <libaddressinput/util/basictypes.h> 21 #include <libaddressinput/util/scoped_ptr.h> 22 23 #include <algorithm> 24 #include <cstddef> 25 #include <string> 26 #include <vector> 27 28 #include <gtest/gtest.h> 29 30 #include "fake_downloader.h" 31 #include "fake_storage.h" 32 #include "region_data_constants.h" 33 #include "retriever.h" 34 #include "rule.h" 35 #include "ruleset.h" 36 37 namespace i18n { 38 namespace addressinput { 39 40 class CountryRulesAggregatorTest : public testing::Test { 41 public: 42 CountryRulesAggregatorTest() 43 : aggregator_(scoped_ptr<Retriever>(new Retriever( 44 FakeDownloader::kFakeDataUrl, 45 scoped_ptr<Downloader>(new FakeDownloader), 46 scoped_ptr<Storage>(new FakeStorage)))), 47 success_(false), 48 country_code_(), 49 ruleset_() {} 50 51 virtual ~CountryRulesAggregatorTest() {} 52 53 protected: 54 scoped_ptr<CountryRulesAggregator::Callback> BuildCallback() { 55 return ::i18n::addressinput::BuildScopedPtrCallback( 56 this, &CountryRulesAggregatorTest::OnRulesetReady); 57 } 58 59 CountryRulesAggregator aggregator_; 60 bool success_; 61 std::string country_code_; 62 scoped_ptr<Ruleset> ruleset_; 63 64 private: 65 void OnRulesetReady(bool success, 66 const std::string& country_code, 67 scoped_ptr<Ruleset> ruleset) { 68 success_ = success; 69 country_code_ = country_code; 70 ruleset_.reset(ruleset.release()); 71 } 72 73 DISALLOW_COPY_AND_ASSIGN(CountryRulesAggregatorTest); 74 }; 75 76 TEST_F(CountryRulesAggregatorTest, ValidRulesets) { 77 const std::vector<std::string>& region_codes = 78 RegionDataConstants::GetRegionCodes(); 79 80 for (size_t i = 0; i < region_codes.size(); ++i) { 81 SCOPED_TRACE("For region: " + region_codes[i]); 82 83 aggregator_.AggregateRules(region_codes[i], BuildCallback()); 84 EXPECT_TRUE(success_); 85 EXPECT_EQ(region_codes[i], country_code_); 86 ASSERT_TRUE(ruleset_ != NULL); 87 88 const std::vector<std::string>& sub_keys = ruleset_->rule().GetSubKeys(); 89 for (std::vector<std::string>::const_iterator sub_key_it = sub_keys.begin(); 90 sub_key_it != sub_keys.end(); ++sub_key_it) { 91 EXPECT_TRUE(ruleset_->GetSubRegionRuleset(*sub_key_it) != NULL); 92 } 93 94 std::vector<std::string> non_default_languages = 95 ruleset_->rule().GetLanguages(); 96 std::vector<std::string>::iterator default_lang_it = 97 std::find(non_default_languages.begin(), 98 non_default_languages.end(), 99 ruleset_->rule().GetLanguage()); 100 if (default_lang_it != non_default_languages.end()) { 101 non_default_languages.erase(default_lang_it); 102 } 103 104 for (std::vector<std::string>::const_iterator 105 lang_it = non_default_languages.begin(); 106 lang_it != non_default_languages.end(); 107 ++lang_it) { 108 EXPECT_TRUE(ruleset_->GetLanguageCodeRule(*lang_it).GetLanguage() != 109 ruleset_->rule().GetLanguage()); 110 } 111 } 112 } 113 114 } // namespace addressinput 115 } // namespace i18n 116