1 // Copyright (C) 2013 Google Inc.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include "rule_retriever.h"
17 #include <libaddressinput/callback.h>
18 #include <libaddressinput/null_storage.h>
19 #include <libaddressinput/util/basictypes.h>
20 #include <libaddressinput/util/scoped_ptr.h>
24 #include <gtest/gtest.h>
26 #include "retriever.h"
28 #include "testdata_source.h"
32 using i18n::addressinput::BuildCallback;
33 using i18n::addressinput::NullStorage;
34 using i18n::addressinput::Retriever;
35 using i18n::addressinput::Rule;
36 using i18n::addressinput::RuleRetriever;
37 using i18n::addressinput::scoped_ptr;
38 using i18n::addressinput::TestdataSource;
40 // Tests for RuleRetriever object.
41 class RuleRetrieverTest : public testing::Test {
45 new Retriever(new TestdataSource(false), new NullStorage)),
49 rule_ready_(BuildCallback(this, &RuleRetrieverTest::OnRuleReady)) {}
51 virtual ~RuleRetrieverTest() {}
53 RuleRetriever rule_retriever_;
57 const scoped_ptr<const RuleRetriever::Callback> rule_ready_;
60 void OnRuleReady(bool success,
61 const std::string& key,
68 DISALLOW_COPY_AND_ASSIGN(RuleRetrieverTest);
71 TEST_F(RuleRetrieverTest, ExistingRule) {
72 static const char kExistingKey[] = "data/CA";
74 rule_retriever_.RetrieveRule(kExistingKey, *rule_ready_);
76 EXPECT_TRUE(success_);
77 EXPECT_EQ(kExistingKey, key_);
78 EXPECT_FALSE(rule_.GetFormat().empty());
81 TEST_F(RuleRetrieverTest, MissingRule) {
82 static const char kMissingKey[] = "junk";
84 rule_retriever_.RetrieveRule(kMissingKey, *rule_ready_);
86 EXPECT_TRUE(success_); // The server returns "{}" for bad keys.
87 EXPECT_EQ(kMissingKey, key_);
88 EXPECT_TRUE(rule_.GetFormat().empty());