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