| <!DOCTYPE HTML> |
| <html> |
| <head> |
| <meta charset="utf-8"> |
| <title>HTML5 storage</title> |
| <link rel="stylesheet" href="/filez/_main/third_party/js/qunit/qunit.css"> |
| <script src="/filez/_main/third_party/js/qunit/qunit.js"></script> |
| <script src="/filez/_main/third_party/js/qunit/qunit_test_runner.js"></script> |
| <script src="../test_bootstrap.js"></script> |
| <script type="text/javascript"> |
| goog.require('bot.dom'); |
| goog.require('bot.storage'); |
| goog.require('bot.html5'); |
| goog.require('goog.array'); |
| goog.require('goog.userAgent.product'); |
| </script> |
| |
| <script type="text/javascript"> |
| // WebDriver does not enable storage for Firefox (b/5787180). |
| var LOCAL_STORAGE_NOT_WORKING = |
| !bot.html5.isSupported(bot.html5.API.LOCAL_STORAGE) || |
| goog.userAgent.product.FIREFOX; |
| var SESSION_STORAGE_NOT_WORKING = |
| !bot.html5.isSupported(bot.html5.API.SESSION_STORAGE) || |
| goog.userAgent.product.FIREFOX; |
| |
| QUnit.test('getLocalStorage', function(assert) { |
| if (LOCAL_STORAGE_NOT_WORKING) { |
| assert.ok(true, 'Skipping: local storage not working'); |
| return; |
| } |
| |
| var storage = bot.storage.getLocalStorage(); |
| assert.strictEqual(storage.getStorageMap(), localStorage); |
| }); |
| |
| QUnit.test('getLocalStorageWithWindow', function(assert) { |
| if (LOCAL_STORAGE_NOT_WORKING) { |
| assert.ok(true, 'Skipping: local storage not working'); |
| return; |
| } |
| |
| var storage = bot.storage.getLocalStorage(bot.getWindow()); |
| assert.strictEqual(storage.getStorageMap(), localStorage); |
| }); |
| |
| QUnit.test('getSessionStorage', function(assert) { |
| if (SESSION_STORAGE_NOT_WORKING) { |
| assert.ok(true, 'Skipping: session storage not working'); |
| return; |
| } |
| |
| var storage = bot.storage.getSessionStorage(); |
| assert.strictEqual(storage.getStorageMap(), sessionStorage); |
| }); |
| |
| QUnit.test('getSessionStorageWithWindow', function(assert) { |
| if (SESSION_STORAGE_NOT_WORKING) { |
| assert.ok(true, 'Skipping: session storage not working'); |
| return; |
| } |
| |
| var storage = bot.storage.getSessionStorage(bot.getWindow()); |
| assert.strictEqual(storage.getStorageMap(), storage.getStorageMap()); |
| }); |
| |
| QUnit.test('clear', function(assert) { |
| if (LOCAL_STORAGE_NOT_WORKING) { |
| assert.ok(true, 'Skipping: local storage not working'); |
| return; |
| } |
| |
| var storage = bot.storage.getLocalStorage(); |
| storage.clear(); |
| assert.strictEqual(storage.size(), 0); |
| |
| storage.setItem('first', 'one'); |
| storage.setItem('second', 'two'); |
| storage.setItem('third', 'two'); |
| storage.clear(); |
| assert.strictEqual(storage.size(), 0); |
| assert.strictEqual(storage.getItem('first'), null); |
| assert.strictEqual(storage.getItem('second'), null); |
| assert.strictEqual(storage.getItem('third'), null); |
| }); |
| |
| QUnit.test('setAndGetSimpleItem', function(assert) { |
| if (SESSION_STORAGE_NOT_WORKING) { |
| assert.ok(true, 'Skipping: session storage not working'); |
| return; |
| } |
| |
| var storage = bot.storage.getSessionStorage(); |
| |
| // set-get |
| storage.setItem('first', 'one'); |
| assert.strictEqual(storage.getItem('first'), 'one'); |
| storage.setItem('second', 2); |
| assert.strictEqual(storage.getItem('second'), '2'); |
| assert.strictEqual(storage.getItem('third'), null); |
| |
| // resetItem |
| storage.setItem('first', '1'); |
| assert.strictEqual(storage.getItem('first'), '1'); |
| |
| storage.clear(); |
| }); |
| |
| QUnit.test('setArbitraryValue', function(assert) { |
| if (SESSION_STORAGE_NOT_WORKING) { |
| assert.ok(true, 'Skipping: session storage not working'); |
| return; |
| } |
| |
| var storage = bot.storage.getSessionStorage(); |
| |
| // set different types of value |
| storage.setItem('foo', ''); |
| assert.strictEqual(storage.getItem('foo'), ''); |
| var d = new Date(2011, 02, 29, 12, 33, 44, 44); |
| storage.setItem('date', d); |
| assert.strictEqual(storage.getItem('date'), d.toString()); |
| |
| // set null value. |
| storage.setItem('foo1', null); |
| assert.strictEqual(storage.getItem('foo1'), 'null'); |
| storage.setItem('foo2', 'null'); |
| assert.strictEqual(storage.getItem('foo1'), storage.getItem('foo2')); |
| |
| storage.clear(); |
| }); |
| |
| QUnit.test('removeItem', function(assert) { |
| if (SESSION_STORAGE_NOT_WORKING) { |
| assert.ok(true, 'Skipping: session storage not working'); |
| return; |
| } |
| |
| var storage = bot.storage.getSessionStorage(); |
| storage.clear(); |
| |
| storage.setItem('first', 'one'); |
| storage.setItem('second', 'two'); |
| |
| assert.strictEqual(storage.removeItem('first'), 'one'); |
| assert.strictEqual(storage.removeItem('second'), 'two'); |
| assert.strictEqual(storage.removeItem('first'), null); |
| assert.strictEqual(storage.removeItem('foo'), null); |
| }); |
| |
| QUnit.test('size', function(assert) { |
| if (SESSION_STORAGE_NOT_WORKING) { |
| assert.ok(true, 'Skipping: session storage not working'); |
| return; |
| } |
| |
| var storage = bot.storage.getSessionStorage(); |
| storage.clear(); |
| |
| storage.setItem('first', 'one'); |
| storage.setItem('second', 'two'); |
| storage.setItem('third', 'three'); |
| assert.strictEqual(storage.size(), 3); |
| }); |
| |
| QUnit.test('keySet', function(assert) { |
| if (SESSION_STORAGE_NOT_WORKING) { |
| assert.ok(true, 'Skipping: session storage not working'); |
| return; |
| } |
| |
| var storage = bot.storage.getSessionStorage(); |
| storage.clear(); |
| |
| storage.setItem('third', 'three'); |
| storage.setItem('first', 'one'); |
| storage.setItem('second', 'two'); |
| assert.strictEqual(storage.size(), 3); |
| |
| // keySet |
| var keys = storage.keySet(); |
| // Use goog.array.indexOf because IE9 doesn't have Array.indexOf |
| assert.notStrictEqual(goog.array.indexOf(keys, 'first'), -1); |
| assert.notStrictEqual(goog.array.indexOf(keys, 'second'), -1); |
| assert.strictEqual(goog.array.indexOf(keys, 'five'), -1); |
| var sortedKeys = keys.sort(); |
| assert.deepEqual(sortedKeys, ['first', 'second', 'third']); |
| }); |
| |
| QUnit.test('identicalLocalStorage', function(assert) { |
| if (LOCAL_STORAGE_NOT_WORKING) { |
| assert.ok(true, 'Skipping: local storage not working'); |
| return; |
| } |
| var localStorage1 = bot.storage.getLocalStorage(); |
| var localStorage2 = bot.storage.getLocalStorage(); |
| |
| localStorage1.clear(); |
| assert.strictEqual(localStorage1.size(), 0); |
| assert.strictEqual(localStorage2.size(), 0); |
| localStorage1.setItem('foo', 'bar'); |
| assert.strictEqual(localStorage1.getItem('foo'), 'bar'); |
| assert.strictEqual(localStorage2.getItem('foo'), 'bar'); |
| |
| localStorage2.removeItem('foo'); |
| assert.strictEqual(localStorage1.getItem('foo'), null); |
| assert.strictEqual(localStorage2.getItem('foo'), null); |
| }); |
| |
| QUnit.test('persistentLocalStorage', function(assert) { |
| if (LOCAL_STORAGE_NOT_WORKING) { |
| assert.ok(true, 'Skipping: local storage not working'); |
| return; |
| } |
| |
| var localStorage = bot.storage.getLocalStorage(); |
| |
| if (localStorage.getItem('foosession') === null) { |
| localStorage.setItem('foosession', 'barsession'); |
| } |
| assert.strictEqual(localStorage.getItem('foosession'), 'barsession'); |
| localStorage.removeItem('foosession'); |
| assert.strictEqual(localStorage.getItem('foosession'), null); |
| }); |
| </script> |
| |
| </head> |
| <body> |
| <div id="qunit"></div> |
| <div id="qunit-fixture"></div> |
| </body> |
| </html> |