Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(235)

Side by Side Diff: chrome/browser/extensions/extension_settings_storage_unittest.cc

Issue 7189029: Implement an initial extension settings API. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Change to RefCountedThreadSafe Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // TODO(kalman): More tests:
6 // - Keys/values containing dots.
7 // - More than one active settings object.
8 // - Failure cases.
9 // - Test with existing data (i.e. shut down leveldb storage, re-start).
10
11 #include "chrome/browser/extensions/extension_settings_storage_unittest.h"
12
13 #include "base/bind.h"
14 #include "base/json/json_writer.h"
15 #include "base/file_util.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/message_loop.h"
18 #include "base/values.h"
19 #include "chrome/browser/extensions/extension_settings.h"
20 #include "content/browser/browser_thread.h"
21
22 // Define macro to get the __LINE__ expansion.
23 #define NEW_CALLBACK(expected) \
24 (new AssertEqualsCallback((expected), __LINE__))
25
26 namespace {
27
28 // Callback from storage methods which performs the test assertions.
29 class AssertEqualsCallback : public ExtensionSettingsStorage::Callback {
30 public:
31 AssertEqualsCallback(DictionaryValue* expected, int line)
32 : expected_(expected), line_(line), called_(false) {
33 }
34
35 ~AssertEqualsCallback() {
36 // Need to DCHECK since ASSERT_* can't be used from destructors.
37 DCHECK(called_);
38 }
39
40 virtual void OnSuccess(DictionaryValue* actual) OVERRIDE {
41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
42 ASSERT_FALSE(called_) << "Callback has already been called";
43 called_ = true;
44 ASSERT_TRUE(expected_->Equals(actual)) << "Values are different:\n" <<
45 "Line: " << line_ << "\n" <<
46 "Expected: " << GetJson(expected_) <<
47 "Got: " << GetJson(actual);
48 delete actual;
49 }
50
51 virtual void OnFailure(const std::string& message) OVERRIDE {
52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
53 ASSERT_FALSE(called_) << "Callback has already been called";
54 called_ = true;
55 // No tests allow failure (yet).
56 ASSERT_TRUE(false) << "Callback failed on line " << line_;
57 }
58
59 private:
60 std::string GetJson(Value* value) {
61 std::string json;
62 base::JSONWriter::Write(value, true, &json);
63 return json;
64 }
65
66 DictionaryValue* expected_;
67 int line_;
68 bool called_;
69 };
70
71 } // namespace
72
73 ExtensionSettingsStorageTest::ExtensionSettingsStorageTest()
74 : key1_("foo"), key2_("bar"), key3_("baz") {
75 val1_.reset(Value::CreateStringValue(key1_ + "Value"));
76 val2_.reset(Value::CreateStringValue(key2_ + "Value"));
77 val3_.reset(Value::CreateStringValue(key3_ + "Value"));
78
79 emptyList_.reset(new ListValue());
80
81 list1_.reset(new ListValue());
82 list1_->Append(Value::CreateStringValue(key1_));
83
84 list2_.reset(new ListValue());
85 list2_->Append(Value::CreateStringValue(key2_));
86
87 list12_.reset(new ListValue());
88 list12_->Append(Value::CreateStringValue(key1_));
89 list12_->Append(Value::CreateStringValue(key2_));
90
91 list13_.reset(new ListValue());
92 list13_->Append(Value::CreateStringValue(key1_));
93 list13_->Append(Value::CreateStringValue(key3_));
94
95 list123_.reset(new ListValue());
96 list123_->Append(Value::CreateStringValue(key1_));
97 list123_->Append(Value::CreateStringValue(key2_));
98 list123_->Append(Value::CreateStringValue(key3_));
99
100 emptyDict_.reset(new DictionaryValue());
101
102 dict1_.reset(new DictionaryValue());
103 dict1_->Set(key1_, val1_->DeepCopy());
104
105 dict12_.reset(new DictionaryValue());
106 dict12_->Set(key1_, val1_->DeepCopy());
107 dict12_->Set(key2_, val2_->DeepCopy());
108
109 dict123_.reset(new DictionaryValue());
110 dict123_->Set(key1_, val1_->DeepCopy());
111 dict123_->Set(key2_, val2_->DeepCopy());
112 dict123_->Set(key3_, val3_->DeepCopy());
113 }
114
115 void ExtensionSettingsStorageTest::SetUp() {
116 ui_message_loop_ = new MessageLoopForUI();
117 // Use the same message loop for the UI and FILE threads, giving a test
118 // pattern where storage API calls get posted to the same message loop (the
119 // current one), then all run with MessageLoop::current()->RunAllPending().
120 ui_thread_ = new BrowserThread(BrowserThread::UI, MessageLoop::current());
121 file_thread_ = new BrowserThread(BrowserThread::FILE, MessageLoop::current());
122
123 FilePath temp_dir;
124 file_util::CreateNewTempDirectory(FilePath::StringType(), &temp_dir);
125 settings_ = new ExtensionSettings(temp_dir);
126
127 storage_ = NULL;
128 (GetParam())(
129 settings_,
130 "fakeExtension",
131 base::Bind(
132 &ExtensionSettingsStorageTest::SetStorage,
133 base::Unretained(this)));
134 MessageLoop::current()->RunAllPending();
135 DCHECK(storage_ != NULL);
136 }
137
138 void ExtensionSettingsStorageTest::TearDown() {
139 settings_ = NULL;
140 delete file_thread_;
141 delete ui_thread_;
142 delete ui_message_loop_;
143 }
144
145 void ExtensionSettingsStorageTest::SetStorage(
146 ExtensionSettingsStorage* storage) {
147 storage_ = storage;
148 }
149
150 TEST_P(ExtensionSettingsStorageTest, GetWhenEmpty) {
151 storage_->Get(key1_, NEW_CALLBACK(emptyDict_.get()));
152 storage_->Get(key2_, NEW_CALLBACK(emptyDict_.get()));
153 storage_->Get(key3_, NEW_CALLBACK(emptyDict_.get()));
154 storage_->Get(*emptyList_, NEW_CALLBACK(emptyDict_.get()));
155 storage_->Get(*list1_, NEW_CALLBACK(emptyDict_.get()));
156 storage_->Get(*list123_, NEW_CALLBACK(emptyDict_.get()));
157 storage_->Get(NEW_CALLBACK(emptyDict_.get()));
158 MessageLoop::current()->RunAllPending();
159 }
160
161 TEST_P(ExtensionSettingsStorageTest, GetWithSingleValue) {
162 storage_->Set(key1_, *val1_, NEW_CALLBACK(dict1_.get()));
163 MessageLoop::current()->RunAllPending();
164
165 storage_->Get(key1_, NEW_CALLBACK(dict1_.get()));
166 storage_->Get(key2_, NEW_CALLBACK(emptyDict_.get()));
167 storage_->Get(key3_, NEW_CALLBACK(emptyDict_.get()));
168 storage_->Get(*emptyList_, NEW_CALLBACK(emptyDict_.get()));
169 storage_->Get(*list1_, NEW_CALLBACK(dict1_.get()));
170 storage_->Get(*list2_, NEW_CALLBACK(emptyDict_.get()));
171 storage_->Get(*list123_, NEW_CALLBACK(dict1_.get()));
172 storage_->Get(NEW_CALLBACK(dict1_.get()));
173 MessageLoop::current()->RunAllPending();
174 }
175
176 TEST_P(ExtensionSettingsStorageTest, GetWithMultipleValues) {
177 storage_->Set(*dict12_, NEW_CALLBACK(dict12_.get()));
178 MessageLoop::current()->RunAllPending();
179
180 storage_->Get(key1_, NEW_CALLBACK(dict1_.get()));
181 storage_->Get(key3_, NEW_CALLBACK(emptyDict_.get()));
182 storage_->Get(*emptyList_, NEW_CALLBACK(emptyDict_.get()));
183 storage_->Get(*list1_, NEW_CALLBACK(dict1_.get()));
184 storage_->Get(*list13_, NEW_CALLBACK(dict1_.get()));
185 storage_->Get(*list12_, NEW_CALLBACK(dict12_.get()));
186 storage_->Get(*list123_, NEW_CALLBACK(dict12_.get()));
187 storage_->Get(NEW_CALLBACK(dict12_.get()));
188 MessageLoop::current()->RunAllPending();
189 }
190
191 TEST_P(ExtensionSettingsStorageTest, RemoveWhenEmpty) {
192 storage_->Remove(key1_, NEW_CALLBACK(emptyDict_.get()));
193 MessageLoop::current()->RunAllPending();
194
195 storage_->Get(key1_, NEW_CALLBACK(emptyDict_.get()));
196 storage_->Get(*list1_, NEW_CALLBACK(emptyDict_.get()));
197 storage_->Get(NEW_CALLBACK(emptyDict_.get()));
198 MessageLoop::current()->RunAllPending();
199 }
200
201 TEST_P(ExtensionSettingsStorageTest, RemoveWithSingleValue) {
202 storage_->Set(key1_, *val1_, NEW_CALLBACK(dict1_.get()));
203 MessageLoop::current()->RunAllPending();
204 storage_->Remove(key1_, NEW_CALLBACK(emptyDict_.get()));
205 MessageLoop::current()->RunAllPending();
206
207 storage_->Get(key1_, NEW_CALLBACK(emptyDict_.get()));
208 storage_->Get(key2_, NEW_CALLBACK(emptyDict_.get()));
209 storage_->Get(*list1_, NEW_CALLBACK(emptyDict_.get()));
210 storage_->Get(*list12_, NEW_CALLBACK(emptyDict_.get()));
211 storage_->Get(NEW_CALLBACK(emptyDict_.get()));
212 MessageLoop::current()->RunAllPending();
213 }
214
215 TEST_P(ExtensionSettingsStorageTest, RemoveWithMultipleValues) {
216 storage_->Set(*dict123_, NEW_CALLBACK(dict123_.get()));
217 MessageLoop::current()->RunAllPending();
218 storage_->Remove(key3_, NEW_CALLBACK(emptyDict_.get()));
219 MessageLoop::current()->RunAllPending();
220
221 storage_->Get(key1_, NEW_CALLBACK(dict1_.get()));
222 storage_->Get(key3_, NEW_CALLBACK(emptyDict_.get()));
223 storage_->Get(*emptyList_, NEW_CALLBACK(emptyDict_.get()));
224 storage_->Get(*list1_, NEW_CALLBACK(dict1_.get()));
225 storage_->Get(*list13_, NEW_CALLBACK(dict1_.get()));
226 storage_->Get(*list12_, NEW_CALLBACK(dict12_.get()));
227 storage_->Get(*list123_, NEW_CALLBACK(dict12_.get()));
228 storage_->Get(NEW_CALLBACK(dict12_.get()));
229
230 storage_->Remove(*list2_, NEW_CALLBACK(emptyDict_.get()));
231 MessageLoop::current()->RunAllPending();
232
233 storage_->Get(key1_, NEW_CALLBACK(dict1_.get()));
234 storage_->Get(key2_, NEW_CALLBACK(emptyDict_.get()));
235 storage_->Get(key3_, NEW_CALLBACK(emptyDict_.get()));
236 storage_->Get(*emptyList_, NEW_CALLBACK(emptyDict_.get()));
237 storage_->Get(*list1_, NEW_CALLBACK(dict1_.get()));
238 storage_->Get(*list2_, NEW_CALLBACK(emptyDict_.get()));
239 storage_->Get(*list123_, NEW_CALLBACK(dict1_.get()));
240 storage_->Get(NEW_CALLBACK(dict1_.get()));
241 MessageLoop::current()->RunAllPending();
242 }
243
244 TEST_P(ExtensionSettingsStorageTest, SetWhenOverwriting) {
245 DictionaryValue key1Val2;
246 key1Val2.Set(key1_, val2_->DeepCopy());
247 storage_->Set(key1_, *val2_, NEW_CALLBACK(&key1Val2));
248 MessageLoop::current()->RunAllPending();
249
250 storage_->Set(*dict12_, NEW_CALLBACK(dict12_.get()));
251 MessageLoop::current()->RunAllPending();
252
253 storage_->Get(key1_, NEW_CALLBACK(dict1_.get()));
254 storage_->Get(key3_, NEW_CALLBACK(emptyDict_.get()));
255 storage_->Get(*emptyList_, NEW_CALLBACK(emptyDict_.get()));
256 storage_->Get(*list1_, NEW_CALLBACK(dict1_.get()));
257 storage_->Get(*list13_, NEW_CALLBACK(dict1_.get()));
258 storage_->Get(*list12_, NEW_CALLBACK(dict12_.get()));
259 storage_->Get(*list123_, NEW_CALLBACK(dict12_.get()));
260 storage_->Get(NEW_CALLBACK(dict12_.get()));
261 MessageLoop::current()->RunAllPending();
262 }
263
264 TEST_P(ExtensionSettingsStorageTest, ClearWhenEmpty) {
265 storage_->Clear(NEW_CALLBACK(emptyDict_.get()));
266 MessageLoop::current()->RunAllPending();
267
268 storage_->Get(key1_, NEW_CALLBACK(emptyDict_.get()));
269 storage_->Get(*list1_, NEW_CALLBACK(emptyDict_.get()));
270 storage_->Get(NEW_CALLBACK(emptyDict_.get()));
271 MessageLoop::current()->RunAllPending();
272 }
273
274 TEST_P(ExtensionSettingsStorageTest, ClearWhenNotEmpty) {
275 storage_->Set(*dict12_, NEW_CALLBACK(dict12_.get()));
276 MessageLoop::current()->RunAllPending();
277
278 storage_->Clear(NEW_CALLBACK(emptyDict_.get()));
279 MessageLoop::current()->RunAllPending();
280
281 storage_->Get(key1_, NEW_CALLBACK(emptyDict_.get()));
282 storage_->Get(*list1_, NEW_CALLBACK(emptyDict_.get()));
283 storage_->Get(NEW_CALLBACK(emptyDict_.get()));
284 MessageLoop::current()->RunAllPending();
285 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698