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 "fake_storage.h"
16
17#include <libaddressinput/callback.h>
18#include <libaddressinput/storage.h>
19#include <libaddressinput/util/basictypes.h>
20#include <libaddressinput/util/scoped_ptr.h>
21
22#include <cstddef>
23#include <string>
24
25#include <gtest/gtest.h>
26
27namespace {
28
29using i18n::addressinput::BuildCallback;
30using i18n::addressinput::FakeStorage;
31using i18n::addressinput::scoped_ptr;
32using i18n::addressinput::Storage;
33
34// Tests for FakeStorage object.
35class FakeStorageTest : public testing::Test {
36 protected:
37  FakeStorageTest()
38      : storage_(),
39        success_(false),
40        key_(),
41        data_(),
42        data_ready_(BuildCallback(this, &FakeStorageTest::OnDataReady)) {}
43
44  FakeStorage storage_;
45  bool success_;
46  std::string key_;
47  std::string data_;
48  const scoped_ptr<const Storage::Callback> data_ready_;
49
50 private:
51  void OnDataReady(bool success, const std::string& key, std::string* data) {
52    ASSERT_FALSE(success && data == NULL);
53    success_ = success;
54    key_ = key;
55    if (data != NULL) {
56      data_ = *data;
57      delete data;
58    }
59  }
60
61  DISALLOW_COPY_AND_ASSIGN(FakeStorageTest);
62};
63
64TEST_F(FakeStorageTest, GetWithoutPutReturnsEmptyData) {
65  storage_.Get("key", *data_ready_);
66
67  EXPECT_FALSE(success_);
68  EXPECT_EQ("key", key_);
69  EXPECT_TRUE(data_.empty());
70}
71
72TEST_F(FakeStorageTest, GetReturnsWhatWasPut) {
73  storage_.Put("key", new std::string("value"));
74  storage_.Get("key", *data_ready_);
75
76  EXPECT_TRUE(success_);
77  EXPECT_EQ("key", key_);
78  EXPECT_EQ("value", data_);
79}
80
81TEST_F(FakeStorageTest, SecondPutOverwritesData) {
82  storage_.Put("key", new std::string("bad-value"));
83  storage_.Put("key", new std::string("good-value"));
84  storage_.Get("key", *data_ready_);
85
86  EXPECT_TRUE(success_);
87  EXPECT_EQ("key", key_);
88  EXPECT_EQ("good-value", data_);
89}
90
91}  // namespace
92