OLD | NEW |
1 // Copyright 2015 Google Inc. All Rights Reserved. | 1 // Copyright 2015 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 | 147 |
148 QUnit.module('storage', { | 148 QUnit.module('storage', { |
149 beforeEach: function () { | 149 beforeEach: function () { |
150 localforage.setItem('test1', 123); | 150 localforage.setItem('test1', 123); |
151 localforage.setItem('test2', '456'); | 151 localforage.setItem('test2', '456'); |
152 sandbox.stub(chrome.caterpillar, 'setError'); | 152 sandbox.stub(chrome.caterpillar, 'setError'); |
153 }, | 153 }, |
154 afterEach: function() { | 154 afterEach: function() { |
155 sandbox.restore(); | 155 sandbox.restore(); |
156 localforage.willError = false; | 156 localforage.willError = false; |
| 157 localforage.clear(); |
| 158 chrome.caterpillar.storage.resetOnChangedListenersForTests(); |
157 } | 159 } |
158 }); | 160 }); |
159 | 161 |
160 // All storage areas are assumed to be the same in this polyfill. | 162 // All storage areas are assumed to be the same in this polyfill. |
161 QUnit.test('all storage areas are the same', assert => { | 163 QUnit.test('all storage areas are the same', assert => { |
162 assert.ok(chrome.storage.sync === chrome.storage.local && | 164 assert.ok(chrome.storage.sync === chrome.storage.local && |
163 chrome.storage.local === chrome.storage.managed) | 165 chrome.storage.local === chrome.storage.managed) |
164 }); | 166 }); |
165 | 167 |
166 QUnit.test('get sends errors to caterpillar when retrieving all items', | 168 QUnit.test('get sends errors to caterpillar when retrieving all items', |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 }); | 245 }); |
244 | 246 |
245 QUnit.test('get can get key value pairs with defaults', function(assert) { | 247 QUnit.test('get can get key value pairs with defaults', function(assert) { |
246 var done = assert.async(); | 248 var done = assert.async(); |
247 chrome.storage.local.get({'notakey': 'default'}, function(items) { | 249 chrome.storage.local.get({'notakey': 'default'}, function(items) { |
248 assert.strictEqual(items['notakey'], 'default'); | 250 assert.strictEqual(items['notakey'], 'default'); |
249 done(); | 251 done(); |
250 }); | 252 }); |
251 }); | 253 }); |
252 | 254 |
253 // TODO(alger): Test onChanged using set and remove. | 255 QUnit.test('getBytesInUse sets an error', function(assert) { |
| 256 var done = assert.async(); |
| 257 chrome.storage.local.getBytesInUse([], function() { |
| 258 assert.equal(chrome.caterpillar.setError.args[0][0], |
| 259 'getBytesInUse not implemented.'); |
| 260 done(); |
| 261 }); |
| 262 }); |
| 263 |
| 264 QUnit.test('getBytesInUse juggles arguments', function(assert) { |
| 265 var done = assert.async(); |
| 266 chrome.storage.local.getBytesInUse(done); |
| 267 assert.expect(0); |
| 268 }); |
| 269 |
| 270 QUnit.test('can set key/value pairs', function(assert) { |
| 271 var done = assert.async(); |
| 272 chrome.storage.local.set({'hello': 'world', 'test': 123}, function() { |
| 273 localforage.getItem('hello') |
| 274 .then(function(value) { |
| 275 assert.strictEqual(value, 'world'); |
| 276 return localforage.getItem('test'); |
| 277 }) |
| 278 .then(function(value) { |
| 279 assert.strictEqual(value, 123); |
| 280 done(); |
| 281 }); |
| 282 }); |
| 283 }); |
| 284 |
| 285 QUnit.test('can remove a single item', function(assert) { |
| 286 var done = assert.async(); |
| 287 chrome.storage.local.remove('test1', function() { |
| 288 localforage.getItem('test1').then(function(value) { |
| 289 assert.strictEqual(value, null); |
| 290 done(); |
| 291 }); |
| 292 }); |
| 293 }); |
| 294 |
| 295 QUnit.test('can remove multiple items', function(assert) { |
| 296 var done = assert.async(); |
| 297 chrome.storage.local.remove(['test1', 'test2'], function() { |
| 298 localforage.getItem('test1') |
| 299 .then(function(value) { |
| 300 assert.strictEqual(value, null); |
| 301 return localforage.getItem('test2'); |
| 302 }) |
| 303 .then(function(value) { |
| 304 assert.strictEqual(value, null); |
| 305 done(); |
| 306 }); |
| 307 }); |
| 308 }); |
| 309 |
| 310 QUnit.test('can clear all items', function(assert) { |
| 311 var done = assert.async(); |
| 312 chrome.storage.local.clear(function() { |
| 313 localforage.getItem('test1') |
| 314 .then(function(value) { |
| 315 assert.strictEqual(value, null); |
| 316 return localforage.getItem('test2'); |
| 317 }) |
| 318 .then(function(value) { |
| 319 assert.strictEqual(value, null); |
| 320 done(); |
| 321 }); |
| 322 }); |
| 323 }); |
| 324 |
| 325 QUnit.test('set runs onChanged handlers', function(assert) { |
| 326 var done = assert.async(); |
| 327 chrome.storage.onChanged.addListener(function(changes) { |
| 328 assert.strictEqual(changes['test1'].oldValue, 123); |
| 329 assert.strictEqual(changes['test1'].newValue, 'hello'); |
| 330 done(); |
| 331 }); |
| 332 chrome.storage.local.set({'test1': 'hello'}); |
| 333 }); |
| 334 |
| 335 QUnit.test('set runs multiple onChanged handlers', function(assert) { |
| 336 assert.expect(0); |
| 337 var done = assert.async(2); |
| 338 chrome.storage.onChanged.addListener(function(changes) { |
| 339 done(); |
| 340 }); |
| 341 chrome.storage.onChanged.addListener(function(changes) { |
| 342 done(); |
| 343 }); |
| 344 chrome.storage.local.set({'test1': 'hello'}); |
| 345 }); |
| 346 |
| 347 QUnit.test('removing 1 key runs onChanged handlers', function(assert) { |
| 348 var done = assert.async(); |
| 349 chrome.storage.onChanged.addListener(function(changes) { |
| 350 assert.strictEqual(changes['test1'].oldValue, 123); |
| 351 assert.strictEqual(changes['test1'].newValue, null); |
| 352 done(); |
| 353 }); |
| 354 chrome.storage.local.remove('test1'); |
| 355 }); |
| 356 |
| 357 QUnit.test('removing 1 key runs multiple onChanged handlers', function(assert) { |
| 358 assert.expect(0); |
| 359 var done = assert.async(2); |
| 360 chrome.storage.onChanged.addListener(function(changes) { |
| 361 done(); |
| 362 }); |
| 363 chrome.storage.onChanged.addListener(function(changes) { |
| 364 done(); |
| 365 }); |
| 366 chrome.storage.local.remove('test1'); |
| 367 }); |
| 368 |
| 369 QUnit.test('removing multiple keys runs onChanged handlers', function(assert) { |
| 370 var done = assert.async(); |
| 371 chrome.storage.onChanged.addListener(function(changes) { |
| 372 assert.strictEqual(changes['test1'].oldValue, 123); |
| 373 assert.strictEqual(changes['test1'].newValue, null); |
| 374 assert.strictEqual(changes['test2'].oldValue, '456'); |
| 375 assert.strictEqual(changes['test2'].newValue, null); |
| 376 done(); |
| 377 }); |
| 378 chrome.storage.local.remove(['test1', 'test2']); |
| 379 }); |
| 380 |
| 381 QUnit.test('removing multiple keys runs multiple onChanged handlers', |
| 382 function(assert) { |
| 383 assert.expect(0); |
| 384 var done = assert.async(2); |
| 385 chrome.storage.onChanged.addListener(function(changes) { |
| 386 done(); |
| 387 }); |
| 388 chrome.storage.onChanged.addListener(function(changes) { |
| 389 done(); |
| 390 }); |
| 391 chrome.storage.local.remove('test1'); |
| 392 } |
| 393 ); |
| 394 |
| 395 QUnit.test('clearing runs onChanged handlers', function(assert) { |
| 396 var done = assert.async(); |
| 397 chrome.storage.onChanged.addListener(function(changes) { |
| 398 assert.strictEqual(changes['test1'].oldValue, 123); |
| 399 assert.strictEqual(changes['test1'].newValue, null); |
| 400 assert.strictEqual(changes['test2'].oldValue, '456'); |
| 401 assert.strictEqual(changes['test2'].newValue, null); |
| 402 done(); |
| 403 }); |
| 404 chrome.storage.local.clear(); |
| 405 }); |
| 406 |
| 407 QUnit.test('clearing runs multiple onChanged handlers', function(assert) { |
| 408 assert.expect(0); |
| 409 var done = assert.async(2); |
| 410 chrome.storage.onChanged.addListener(function(changes) { |
| 411 done(); |
| 412 }); |
| 413 chrome.storage.onChanged.addListener(function(changes) { |
| 414 done(); |
| 415 }); |
| 416 chrome.storage.local.clear(); |
| 417 }); |
OLD | NEW |