Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / libaddressinput / src / cpp / test / rule_retriever_test.cc
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
30 namespace {
31
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;
39
40 // Tests for RuleRetriever object.
41 class 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   virtual ~RuleRetrieverTest() {}
52
53   RuleRetriever rule_retriever_;
54   bool success_;
55   std::string key_;
56   Rule rule_;
57   const scoped_ptr<const RuleRetriever::Callback> rule_ready_;
58
59  private:
60   void OnRuleReady(bool success,
61                    const std::string& key,
62                    const Rule& rule) {
63     success_ = success;
64     key_ = key;
65     rule_.CopyFrom(rule);
66   }
67
68   DISALLOW_COPY_AND_ASSIGN(RuleRetrieverTest);
69 };
70
71 TEST_F(RuleRetrieverTest, ExistingRule) {
72   static const char kExistingKey[] = "data/CA";
73
74   rule_retriever_.RetrieveRule(kExistingKey, *rule_ready_);
75
76   EXPECT_TRUE(success_);
77   EXPECT_EQ(kExistingKey, key_);
78   EXPECT_FALSE(rule_.GetFormat().empty());
79 }
80
81 TEST_F(RuleRetrieverTest, MissingRule) {
82   static const char kMissingKey[] = "junk";
83
84   rule_retriever_.RetrieveRule(kMissingKey, *rule_ready_);
85
86   EXPECT_TRUE(success_);  // The server returns "{}" for bad keys.
87   EXPECT_EQ(kMissingKey, key_);
88   EXPECT_TRUE(rule_.GetFormat().empty());
89 }
90
91 }  // namespace