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

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: Use base::Closure for storage callback, style fixes, mac/windows fixes Created 9 years, 6 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 newCallback(expected) (new AssertEqualsCallback((expected), __LINE__))
24
25 namespace {
26
27 // Callback from GetStorage() that places the new storage in a given location.
28 class GetStorageCallback {
29 public:
30 explicit GetStorageCallback(ExtensionSettingsStorage** storagePtr)
31 : storagePtr_(storagePtr) {
32 *storagePtr = NULL;
33 }
34
35 void Run(ExtensionSettingsStorage* storage) {
36 DCHECK(*storagePtr_ == NULL);
37 *storagePtr_ = storage;
38 delete this;
39 }
40
41 private:
42 ExtensionSettingsStorage** storagePtr_;
43 };
44
45 // Callback from storage methods which performs the test assertions.
46 class AssertEqualsCallback : public ExtensionSettingsStorage::Callback {
47 public:
48 AssertEqualsCallback(DictionaryValue* expected, int line)
49 : expected_(expected), line_(line), called_(false) {
50 }
51
52 ~AssertEqualsCallback() {
53 // Need to DCHECK since ASSERT_* can't be used from destructors.
54 DCHECK(called_);
55 }
56
57 virtual void OnSuccess(DictionaryValue* actual) OVERRIDE {
58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
59 ASSERT_FALSE(called_) << "Callback has already been called";
60 called_ = true;
61 ASSERT_TRUE(expected_->Equals(actual)) << "Values are different:\n" <<
62 "Line: " << line_ << "\n" <<
63 "Expected: " << GetJson(expected_) <<
64 "Got: " << GetJson(actual);
65 delete actual;
66 }
67
68 virtual void OnFailure(const std::string& message) OVERRIDE {
69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
70 ASSERT_FALSE(called_) << "Callback has already been called";
71 called_ = true;
72 // No tests allow failure (yet).
73 ASSERT_TRUE(false) << "Callback failed on line " << line_;
74 }
75
76 private:
77 std::string GetJson(Value* value) {
78 std::string json;
79 base::JSONWriter::Write(value, true, &json);
80 return json;
81 }
82
83 DictionaryValue* expected_;
84 int line_;
85 bool called_;
86 };
87
88 std::string key1() {
89 return "foo";
90 }
91
92 std::string key2() {
93 return "bar";
94 }
95
96 std::string key3() {
97 return "baz";
98 }
99
100 Value* val1() {
101 Value* val = NULL;
102 if (val == NULL) {
103 val = Value::CreateStringValue(key1() + "Value");
104 }
105 return val;
106 }
107
108 Value* val2() {
109 static Value* val = NULL;
110 if (val == NULL) {
111 val = Value::CreateStringValue(key2() + "Value");
112 }
113 return val;
114 }
115
116 Value* val3() {
117 static Value* val = NULL;
118 if (val == NULL) {
119 val = Value::CreateStringValue(key3() + "Value");
120 }
121 return val;
122 }
123
124 ListValue* emptyList() {
125 static ListValue* val = NULL;
126 if (val == NULL) {
127 val = new ListValue();
128 }
129 return val;
130 }
131
132 ListValue* list1() {
133 static ListValue* val = NULL;
134 if (val == NULL) {
135 val = new ListValue();
136 val->Append(Value::CreateStringValue(key1()));
137 }
138 return val;
139 }
140
141 ListValue* list2() {
142 static ListValue* val = NULL;
143 if (val == NULL) {
144 val = new ListValue();
145 val->Append(Value::CreateStringValue(key2()));
146 }
147 return val;
148 }
149
150 ListValue* list12() {
151 static ListValue* val = NULL;
152 if (val == NULL) {
153 val = new ListValue();
154 val->Append(Value::CreateStringValue(key1()));
155 val->Append(Value::CreateStringValue(key2()));
156 }
157 return val;
158 }
159
160 ListValue* list13() {
161 static ListValue* val = NULL;
162 if (val == NULL) {
163 val = new ListValue();
164 val->Append(Value::CreateStringValue(key1()));
165 val->Append(Value::CreateStringValue(key3()));
166 }
167 return val;
168 }
169
170 ListValue* list123() {
171 static ListValue* val = NULL;
172 if (val == NULL) {
173 val = new ListValue();
174 val->Append(Value::CreateStringValue(key1()));
175 val->Append(Value::CreateStringValue(key2()));
176 val->Append(Value::CreateStringValue(key3()));
177 }
178 return val;
179 }
180
181 DictionaryValue* emptyDict() {
182 static DictionaryValue* val = NULL;
183 if (val == NULL) {
184 val = new DictionaryValue();
185 }
186 return val;
187 }
188
189 DictionaryValue* dict1() {
190 static DictionaryValue* val = NULL;
191 if (val == NULL) {
192 val = new DictionaryValue();
193 val->Set(key1(), val1()->DeepCopy());
194 }
195 return val;
196 }
197
198 DictionaryValue* dict12() {
199 static DictionaryValue* val = NULL;
200 if (val == NULL) {
201 val = new DictionaryValue();
202 val->Set(key1(), val1()->DeepCopy());
203 val->Set(key2(), val2()->DeepCopy());
204 }
205 return val;
206 }
207
208 DictionaryValue* dict123() {
209 static DictionaryValue* val = NULL;
210 if (val == NULL) {
211 val = new DictionaryValue();
212 val->Set(key1(), val1()->DeepCopy());
213 val->Set(key2(), val2()->DeepCopy());
214 val->Set(key3(), val3()->DeepCopy());
215 }
216 return val;
217 }
218
219 } // namespace
220
221 void ExtensionSettingsStorageTest::SetUp() {
222 ui_message_loop_ = new MessageLoopForUI();
223 // Use the same message loop for the UI and FILE threads, giving a test
224 // pattern where storage API calls get posted to the same message loop (the
225 // current one), then all run with MessageLoop::current()->RunAllPending().
226 ui_thread_ = new BrowserThread(BrowserThread::UI, MessageLoop::current());
227 file_thread_ = new BrowserThread(BrowserThread::FILE, MessageLoop::current());
228
229 FilePath temp_dir;
230 file_util::CreateNewTempDirectory(FilePath::StringType(), &temp_dir);
231 settings_ = new ExtensionSettings(temp_dir);
232
233 storage_ = NULL;
234 (GetParam())(
235 settings_,
236 "fakeExtension",
237 base::Bind(
238 &GetStorageCallback::Run,
239 base::Unretained(new GetStorageCallback(&storage_))));
240 MessageLoop::current()->RunAllPending();
241 DCHECK(storage_ != NULL);
242 }
243
244 void ExtensionSettingsStorageTest::TearDown() {
245 delete settings_;
246 delete file_thread_;
247 delete ui_thread_;
248 delete ui_message_loop_;
249 }
250
251 TEST_P(ExtensionSettingsStorageTest, GetWhenEmpty) {
252 storage_->Get(key1(), newCallback(emptyDict()));
253 storage_->Get(key2(), newCallback(emptyDict()));
254 storage_->Get(key3(), newCallback(emptyDict()));
255 storage_->Get(*emptyList(), newCallback(emptyDict()));
256 storage_->Get(*list1(), newCallback(emptyDict()));
257 storage_->Get(*list123(), newCallback(emptyDict()));
258 storage_->Get(newCallback(emptyDict()));
259 MessageLoop::current()->RunAllPending();
260 }
261
262 TEST_P(ExtensionSettingsStorageTest, GetWithSingleValue) {
263 storage_->Set(key1(), *val1(), newCallback(dict1()));
264 MessageLoop::current()->RunAllPending();
265
266 storage_->Get(key1(), newCallback(dict1()));
267 storage_->Get(key2(), newCallback(emptyDict()));
268 storage_->Get(key3(), newCallback(emptyDict()));
269 storage_->Get(*emptyList(), newCallback(emptyDict()));
270 storage_->Get(*list1(), newCallback(dict1()));
271 storage_->Get(*list2(), newCallback(emptyDict()));
272 storage_->Get(*list123(), newCallback(dict1()));
273 storage_->Get(newCallback(dict1()));
274 MessageLoop::current()->RunAllPending();
275 }
276
277 TEST_P(ExtensionSettingsStorageTest, GetWithMultipleValues) {
278 storage_->Set(*dict12(), newCallback(dict12()));
279 MessageLoop::current()->RunAllPending();
280
281 storage_->Get(key1(), newCallback(dict1()));
282 storage_->Get(key3(), newCallback(emptyDict()));
283 storage_->Get(*emptyList(), newCallback(emptyDict()));
284 storage_->Get(*list1(), newCallback(dict1()));
285 storage_->Get(*list13(), newCallback(dict1()));
286 storage_->Get(*list12(), newCallback(dict12()));
287 storage_->Get(*list123(), newCallback(dict12()));
288 storage_->Get(newCallback(dict12()));
289 MessageLoop::current()->RunAllPending();
290 }
291
292 TEST_P(ExtensionSettingsStorageTest, RemoveWhenEmpty) {
293 storage_->Remove(key1(), newCallback(emptyDict()));
294 MessageLoop::current()->RunAllPending();
295
296 storage_->Get(key1(), newCallback(emptyDict()));
297 storage_->Get(*list1(), newCallback(emptyDict()));
298 storage_->Get(newCallback(emptyDict()));
299 MessageLoop::current()->RunAllPending();
300 }
301
302 TEST_P(ExtensionSettingsStorageTest, RemoveWithSingleValue) {
303 storage_->Set(key1(), *val1(), newCallback(dict1()));
304 MessageLoop::current()->RunAllPending();
305 storage_->Remove(key1(), newCallback(emptyDict()));
306 MessageLoop::current()->RunAllPending();
307
308 storage_->Get(key1(), newCallback(emptyDict()));
309 storage_->Get(key2(), newCallback(emptyDict()));
310 storage_->Get(*list1(), newCallback(emptyDict()));
311 storage_->Get(*list12(), newCallback(emptyDict()));
312 storage_->Get(newCallback(emptyDict()));
313 MessageLoop::current()->RunAllPending();
314 }
315
316 TEST_P(ExtensionSettingsStorageTest, RemoveWithMultipleValues) {
317 storage_->Set(*dict123(), newCallback(dict123()));
318 MessageLoop::current()->RunAllPending();
319 storage_->Remove(key3(), newCallback(emptyDict()));
320 MessageLoop::current()->RunAllPending();
321
322 storage_->Get(key1(), newCallback(dict1()));
323 storage_->Get(key3(), newCallback(emptyDict()));
324 storage_->Get(*emptyList(), newCallback(emptyDict()));
325 storage_->Get(*list1(), newCallback(dict1()));
326 storage_->Get(*list13(), newCallback(dict1()));
327 storage_->Get(*list12(), newCallback(dict12()));
328 storage_->Get(*list123(), newCallback(dict12()));
329 storage_->Get(newCallback(dict12()));
330
331 storage_->Remove(*list2(), newCallback(emptyDict()));
332 MessageLoop::current()->RunAllPending();
333
334 storage_->Get(key1(), newCallback(dict1()));
335 storage_->Get(key2(), newCallback(emptyDict()));
336 storage_->Get(key3(), newCallback(emptyDict()));
337 storage_->Get(*emptyList(), newCallback(emptyDict()));
338 storage_->Get(*list1(), newCallback(dict1()));
339 storage_->Get(*list2(), newCallback(emptyDict()));
340 storage_->Get(*list123(), newCallback(dict1()));
341 storage_->Get(newCallback(dict1()));
342 MessageLoop::current()->RunAllPending();
343 }
344
345 TEST_P(ExtensionSettingsStorageTest, SetWhenOverwriting) {
346 DictionaryValue key1Val2;
347 key1Val2.Set(key1(), val2()->DeepCopy());
348 storage_->Set(key1(), *val2(), newCallback(&key1Val2));
349 MessageLoop::current()->RunAllPending();
350
351 storage_->Set(*dict12(), newCallback(dict12()));
352 MessageLoop::current()->RunAllPending();
353
354 storage_->Get(key1(), newCallback(dict1()));
355 storage_->Get(key3(), newCallback(emptyDict()));
356 storage_->Get(*emptyList(), newCallback(emptyDict()));
357 storage_->Get(*list1(), newCallback(dict1()));
358 storage_->Get(*list13(), newCallback(dict1()));
359 storage_->Get(*list12(), newCallback(dict12()));
360 storage_->Get(*list123(), newCallback(dict12()));
361 storage_->Get(newCallback(dict12()));
362 MessageLoop::current()->RunAllPending();
363 }
364
365 TEST_P(ExtensionSettingsStorageTest, ClearWhenEmpty) {
366 storage_->Clear(newCallback(emptyDict()));
367 MessageLoop::current()->RunAllPending();
368
369 storage_->Get(key1(), newCallback(emptyDict()));
370 storage_->Get(*list1(), newCallback(emptyDict()));
371 storage_->Get(newCallback(emptyDict()));
372 MessageLoop::current()->RunAllPending();
373 }
374
375 TEST_P(ExtensionSettingsStorageTest, ClearWhenNotEmpty) {
376 storage_->Set(*dict12(), newCallback(dict12()));
377 MessageLoop::current()->RunAllPending();
378
379 storage_->Clear(newCallback(emptyDict()));
380 MessageLoop::current()->RunAllPending();
381
382 storage_->Get(key1(), newCallback(emptyDict()));
383 storage_->Get(*list1(), newCallback(emptyDict()));
384 storage_->Get(newCallback(emptyDict()));
385 MessageLoop::current()->RunAllPending();
386 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698