Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / libaddressinput / src / cpp / test / 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 "retriever.h"
16
17 #include <libaddressinput/callback.h>
18 #include <libaddressinput/null_storage.h>
19 #include <libaddressinput/storage.h>
20 #include <libaddressinput/util/basictypes.h>
21 #include <libaddressinput/util/scoped_ptr.h>
22
23 #include <cstddef>
24 #include <string>
25
26 #include <gtest/gtest.h>
27
28 #include "mock_source.h"
29 #include "testdata_source.h"
30
31 #define CHECKSUM "dd63dafcbd4d5b28badfcaf86fb6fcdb"
32 #define DATA "{'foo': 'bar'}"
33 #define OLD_TIMESTAMP "0"
34
35 namespace {
36
37 using i18n::addressinput::BuildCallback;
38 using i18n::addressinput::MockSource;
39 using i18n::addressinput::NullStorage;
40 using i18n::addressinput::Retriever;
41 using i18n::addressinput::scoped_ptr;
42 using i18n::addressinput::Storage;
43 using i18n::addressinput::TestdataSource;
44
45 const char kKey[] = "data/CA/AB--fr";
46
47 // Empty data that the source can return.
48 const char kEmptyData[] = "{}";
49
50 // The value of the data that the stale storage returns.
51 const char kStaleData[] = DATA;
52
53 // The actual data that the stale storage returns.
54 const char kStaleWrappedData[] = "timestamp=" OLD_TIMESTAMP "\n"
55                                  "checksum=" CHECKSUM "\n"
56                                  DATA;
57
58 // Tests for Retriever object.
59 class RetrieverTest : public testing::Test {
60  protected:
61   RetrieverTest()
62       : retriever_(new TestdataSource(false), new NullStorage),
63         success_(false),
64         key_(),
65         data_(),
66         data_ready_(BuildCallback(this, &RetrieverTest::OnDataReady)) {}
67
68   virtual ~RetrieverTest() {}
69
70   Retriever retriever_;
71   bool success_;
72   std::string key_;
73   std::string data_;
74   const scoped_ptr<const Retriever::Callback> data_ready_;
75
76  private:
77   void OnDataReady(bool success,
78                    const std::string& key,
79                    const std::string& data) {
80     success_ = success;
81     key_ = key;
82     data_ = data;
83   }
84
85   DISALLOW_COPY_AND_ASSIGN(RetrieverTest);
86 };
87
88 TEST_F(RetrieverTest, RetrieveData) {
89   retriever_.Retrieve(kKey, *data_ready_);
90
91   EXPECT_TRUE(success_);
92   EXPECT_EQ(kKey, key_);
93   EXPECT_FALSE(data_.empty());
94   EXPECT_NE(kEmptyData, data_);
95 }
96
97 TEST_F(RetrieverTest, ReadDataFromStorage) {
98   retriever_.Retrieve(kKey, *data_ready_);
99   retriever_.Retrieve(kKey, *data_ready_);
100
101   EXPECT_TRUE(success_);
102   EXPECT_EQ(kKey, key_);
103   EXPECT_FALSE(data_.empty());
104   EXPECT_NE(kEmptyData, data_);
105 }
106
107 TEST_F(RetrieverTest, MissingKeyReturnsEmptyData) {
108   static const char kMissingKey[] = "junk";
109
110   retriever_.Retrieve(kMissingKey, *data_ready_);
111
112   EXPECT_TRUE(success_);
113   EXPECT_EQ(kMissingKey, key_);
114   EXPECT_EQ(kEmptyData, data_);
115 }
116
117 TEST_F(RetrieverTest, FaultySource) {
118   // An empty MockSource will fail for any request.
119   Retriever bad_retriever(new MockSource, new NullStorage);
120
121   bad_retriever.Retrieve(kKey, *data_ready_);
122
123   EXPECT_FALSE(success_);
124   EXPECT_EQ(kKey, key_);
125   EXPECT_TRUE(data_.empty());
126 }
127
128 // The storage that always returns stale data.
129 class StaleStorage : public Storage {
130  public:
131   StaleStorage() : data_updated_(false) {}
132   virtual ~StaleStorage() {}
133
134   // Storage implementation.
135   virtual void Get(const std::string& key, const Callback& data_ready) const {
136     data_ready(true, key, new std::string(kStaleWrappedData));
137   }
138
139   virtual void Put(const std::string& key, std::string* value) {
140     ASSERT_TRUE(value != NULL);
141     data_updated_ = true;
142     delete value;
143   }
144
145   bool data_updated_;
146
147  private:
148   DISALLOW_COPY_AND_ASSIGN(StaleStorage);
149 };
150
151 TEST_F(RetrieverTest, UseStaleDataWhenSourceFails) {
152   // Owned by |resilient_retriver|.
153   StaleStorage* stale_storage = new StaleStorage;
154   // An empty MockSource will fail for any request.
155   Retriever resilient_retriever(new MockSource, stale_storage);
156
157   resilient_retriever.Retrieve(kKey, *data_ready_);
158
159   EXPECT_TRUE(success_);
160   EXPECT_EQ(kKey, key_);
161   EXPECT_EQ(kStaleData, data_);
162   EXPECT_FALSE(stale_storage->data_updated_);
163 }
164
165 TEST_F(RetrieverTest, DoNotUseStaleDataWhenSourceSucceeds) {
166   // Owned by |resilient_retriver|.
167   StaleStorage* stale_storage = new StaleStorage;
168   Retriever resilient_retriever(new TestdataSource(false), stale_storage);
169
170   resilient_retriever.Retrieve(kKey, *data_ready_);
171
172   EXPECT_TRUE(success_);
173   EXPECT_EQ(kKey, key_);
174   EXPECT_FALSE(data_.empty());
175   EXPECT_NE(kEmptyData, data_);
176   EXPECT_NE(kStaleData, data_);
177   EXPECT_TRUE(stale_storage->data_updated_);
178 }
179
180 }  // namespace