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/scoped_ptr.h>
20
21#include <string>
22
23#include <gtest/gtest.h>
24
25#include "fake_downloader.h"
26#include "retriever.h"
27#include "rule.h"
28
29namespace {
30
31using i18n::addressinput::BuildCallback;
32using i18n::addressinput::FakeDownloader;
33using i18n::addressinput::NullStorage;
34using i18n::addressinput::Retriever;
35using i18n::addressinput::Rule;
36using i18n::addressinput::RuleRetriever;
37using i18n::addressinput::scoped_ptr;
38
39// Tests for RuleRetriever object.
40class RuleRetrieverTest : public testing::Test {
41 protected:
42  RuleRetrieverTest()
43      : rule_retriever_(new Retriever(FakeDownloader::kFakeDataUrl,
44                                      new FakeDownloader,
45                                      new NullStorage)),
46        success_(false),
47        key_(),
48        rule_() {}
49
50  virtual ~RuleRetrieverTest() {}
51
52  RuleRetriever::Callback* BuildCallback() {
53    return ::BuildCallback(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