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 "rule_retriever.h" 16 17#include <libaddressinput/callback.h> 18#include <libaddressinput/util/scoped_ptr.h> 19 20#include <string> 21 22#include <gtest/gtest.h> 23 24#include "fake_downloader.h" 25#include "fake_storage.h" 26#include "retriever.h" 27#include "rule.h" 28 29namespace { 30 31using i18n::addressinput::FakeDownloader; 32using i18n::addressinput::FakeStorage; 33using i18n::addressinput::Retriever; 34using i18n::addressinput::Rule; 35using i18n::addressinput::RuleRetriever; 36using i18n::addressinput::scoped_ptr; 37 38// Tests for RuleRetriever object. 39class RuleRetrieverTest : public testing::Test { 40 protected: 41 RuleRetrieverTest() 42 : rule_retriever_(new Retriever(FakeDownloader::kFakeDataUrl, 43 new FakeDownloader, 44 new FakeStorage)), 45 success_(false), 46 key_(), 47 rule_() {} 48 49 virtual ~RuleRetrieverTest() {} 50 51 RuleRetriever::Callback* BuildCallback() { 52 return i18n::addressinput::BuildCallback( 53 this, &RuleRetrieverTest::OnRuleReady); 54 } 55 56 RuleRetriever rule_retriever_; 57 bool success_; 58 std::string key_; 59 Rule rule_; 60 61 private: 62 void OnRuleReady(bool success, 63 const std::string& key, 64 const Rule& rule) { 65 success_ = success; 66 key_ = key; 67 rule_.CopyFrom(rule); 68 } 69}; 70 71TEST_F(RuleRetrieverTest, ExistingRule) { 72 static const char kExistingKey[] = "data/CA"; 73 74 scoped_ptr<RuleRetriever::Callback> callback(BuildCallback()); 75 rule_retriever_.RetrieveRule(kExistingKey, *callback); 76 77 EXPECT_TRUE(success_); 78 EXPECT_EQ(kExistingKey, key_); 79 EXPECT_FALSE(rule_.GetFormat().empty()); 80} 81 82TEST_F(RuleRetrieverTest, MissingRule) { 83 static const char kMissingKey[] = "junk"; 84 85 scoped_ptr<RuleRetriever::Callback> callback(BuildCallback()); 86 rule_retriever_.RetrieveRule(kMissingKey, *callback); 87 88 EXPECT_TRUE(success_); // The server returns "{}" for bad keys. 89 EXPECT_EQ(kMissingKey, key_); 90 EXPECT_TRUE(rule_.GetFormat().empty()); 91} 92 93} // namespace 94