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 "validating_storage.h" 16 17#include <libaddressinput/callback.h> 18#include <libaddressinput/storage.h> 19#include <libaddressinput/util/scoped_ptr.h> 20 21#include <string> 22 23#include <gtest/gtest.h> 24 25#include "fake_storage.h" 26 27namespace { 28 29using i18n::addressinput::FakeStorage; 30using i18n::addressinput::scoped_ptr; 31using i18n::addressinput::Storage; 32using i18n::addressinput::ValidatingStorage; 33 34// Tests for ValidatingStorage object. 35class ValidatingStorageTest : public testing::Test { 36 protected: 37 ValidatingStorageTest() 38 : wrapped_storage_(new FakeStorage), 39 storage_(wrapped_storage_), 40 success_(false), 41 key_(), 42 data_() {} 43 44 virtual ~ValidatingStorageTest() {} 45 46 Storage::Callback* BuildCallback() { 47 return i18n::addressinput::BuildCallback( 48 this, &ValidatingStorageTest::OnDataReady); 49 } 50 51 FakeStorage* const wrapped_storage_; // Owned by |storage_|. 52 ValidatingStorage storage_; 53 bool success_; 54 std::string key_; 55 std::string data_; 56 57 private: 58 void OnDataReady(bool success, 59 const std::string& key, 60 const std::string& data) { 61 success_ = success; 62 key_ = key; 63 data_ = data; 64 } 65}; 66 67TEST_F(ValidatingStorageTest, Basic) { 68 storage_.Put("key", "value"); 69 70 scoped_ptr<Storage::Callback> callback(BuildCallback()); 71 storage_.Get("key", *callback); 72 73 EXPECT_TRUE(success_); 74 EXPECT_EQ("key", key_); 75 EXPECT_EQ("value", data_); 76} 77 78TEST_F(ValidatingStorageTest, EmptyData) { 79 storage_.Put("key", std::string()); 80 81 scoped_ptr<Storage::Callback> callback(BuildCallback()); 82 storage_.Get("key", *callback); 83 84 EXPECT_TRUE(success_); 85 EXPECT_EQ("key", key_); 86 EXPECT_TRUE(data_.empty()); 87} 88 89TEST_F(ValidatingStorageTest, MissingKey) { 90 scoped_ptr<Storage::Callback> callback(BuildCallback()); 91 storage_.Get("key", *callback); 92 93 EXPECT_FALSE(success_); 94 EXPECT_EQ("key", key_); 95 EXPECT_TRUE(data_.empty()); 96} 97 98TEST_F(ValidatingStorageTest, GarbageData) { 99 storage_.Put("key", "value"); 100 wrapped_storage_->Put("key", "garbage"); 101 102 scoped_ptr<Storage::Callback> callback(BuildCallback()); 103 storage_.Get("key", *callback); 104 105 EXPECT_FALSE(success_); 106 EXPECT_EQ("key", key_); 107 EXPECT_TRUE(data_.empty()); 108} 109 110} // namespace 111