| OLD | NEW |
| (Empty) | |
| 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 "retriever.h" |
| 33 #include "rule.h" |
| 34 #include "ruleset.h" |
| 35 |
| 36 namespace i18n { |
| 37 namespace addressinput { |
| 38 |
| 39 class CountryRulesAggregatorTest : public testing::Test { |
| 40 public: |
| 41 CountryRulesAggregatorTest() |
| 42 : aggregator_(scoped_ptr<Retriever>(new Retriever( |
| 43 FakeDownloader::kFakeDataUrl, |
| 44 scoped_ptr<Downloader>(new FakeDownloader), |
| 45 scoped_ptr<Storage>(new FakeStorage)))), |
| 46 success_(false), |
| 47 country_code_(), |
| 48 ruleset_() {} |
| 49 |
| 50 virtual ~CountryRulesAggregatorTest() {} |
| 51 |
| 52 protected: |
| 53 scoped_ptr<CountryRulesAggregator::Callback> BuildCallback() { |
| 54 return ::i18n::addressinput::BuildScopedPtrCallback( |
| 55 this, &CountryRulesAggregatorTest::OnRulesetReady); |
| 56 } |
| 57 |
| 58 CountryRulesAggregator aggregator_; |
| 59 bool success_; |
| 60 std::string country_code_; |
| 61 scoped_ptr<Ruleset> ruleset_; |
| 62 |
| 63 private: |
| 64 void OnRulesetReady(bool success, |
| 65 const std::string& country_code, |
| 66 scoped_ptr<Ruleset> ruleset) { |
| 67 success_ = success; |
| 68 country_code_ = country_code; |
| 69 ruleset_.reset(ruleset.release()); |
| 70 } |
| 71 |
| 72 DISALLOW_COPY_AND_ASSIGN(CountryRulesAggregatorTest); |
| 73 }; |
| 74 |
| 75 TEST_F(CountryRulesAggregatorTest, ValidRuleset) { |
| 76 aggregator_.AggregateRules("CH", BuildCallback()); |
| 77 EXPECT_TRUE(success_); |
| 78 EXPECT_EQ("CH", country_code_); |
| 79 ASSERT_TRUE(ruleset_ != NULL); |
| 80 |
| 81 const std::vector<std::string>& sub_keys = ruleset_->rule().GetSubKeys(); |
| 82 EXPECT_TRUE(sub_keys.size() > 0); |
| 83 for (std::vector<std::string>::const_iterator sub_key_it = sub_keys.begin(); |
| 84 sub_key_it != sub_keys.end(); ++sub_key_it) { |
| 85 EXPECT_TRUE(ruleset_->GetSubRegionRuleset(*sub_key_it) != NULL); |
| 86 } |
| 87 |
| 88 std::vector<std::string> non_default_languages = |
| 89 ruleset_->rule().GetLanguages(); |
| 90 std::vector<std::string>::iterator default_lang_it = |
| 91 std::find(non_default_languages.begin(), |
| 92 non_default_languages.end(), |
| 93 ruleset_->rule().GetLanguage()); |
| 94 if (default_lang_it != non_default_languages.end()) { |
| 95 non_default_languages.erase(default_lang_it); |
| 96 } |
| 97 |
| 98 EXPECT_TRUE(non_default_languages.size() > 0); |
| 99 for (std::vector<std::string>::const_iterator |
| 100 lang_it = non_default_languages.begin(); |
| 101 lang_it != non_default_languages.end(); |
| 102 ++lang_it) { |
| 103 EXPECT_TRUE(ruleset_->GetLanguageCodeRule(*lang_it).GetLanguage() != |
| 104 ruleset_->rule().GetLanguage()); |
| 105 } |
| 106 } |
| 107 |
| 108 } // namespace addressinput |
| 109 } // namespace i18n |
| OLD | NEW |