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