Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | <!DOCTYPE HTML> |
michael@0 | 2 | <html> |
michael@0 | 3 | <head> |
michael@0 | 4 | <meta charset="utf-8"> |
michael@0 | 5 | <title>Test for DataStore - basic operation on a readonly db</title> |
michael@0 | 6 | </head> |
michael@0 | 7 | <body> |
michael@0 | 8 | <div id="container"></div> |
michael@0 | 9 | <script type="application/javascript;version=1.7"> |
michael@0 | 10 | |
michael@0 | 11 | var gStore; |
michael@0 | 12 | |
michael@0 | 13 | function is(a, b, msg) { |
michael@0 | 14 | alert((a === b ? 'OK' : 'KO') + ' ' + msg) |
michael@0 | 15 | } |
michael@0 | 16 | |
michael@0 | 17 | function ok(a, msg) { |
michael@0 | 18 | alert((a ? 'OK' : 'KO')+ ' ' + msg) |
michael@0 | 19 | } |
michael@0 | 20 | |
michael@0 | 21 | function cbError() { |
michael@0 | 22 | alert('KO error'); |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | function finish() { |
michael@0 | 26 | alert('DONE'); |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | function testGetDataStores() { |
michael@0 | 30 | navigator.getDataStores('foo').then(function(stores) { |
michael@0 | 31 | is(stores.length, 1, "getDataStores('foo') returns 1 element"); |
michael@0 | 32 | is(stores[0].name, 'foo', 'The dataStore.name is foo'); |
michael@0 | 33 | is(stores[0].readOnly, false, 'The dataStore foo is not in readonly'); |
michael@0 | 34 | |
michael@0 | 35 | var store = stores[0]; |
michael@0 | 36 | ok("get" in store, "store.get exists"); |
michael@0 | 37 | ok("put" in store, "store.put exists"); |
michael@0 | 38 | ok("add" in store, "store.add exists"); |
michael@0 | 39 | ok("remove" in store, "store.remove exists"); |
michael@0 | 40 | ok("clear" in store, "store.clear exists"); |
michael@0 | 41 | ok("revisionId" in store, "store.revisionId exists"); |
michael@0 | 42 | ok("getLength" in store, "store.getLength exists"); |
michael@0 | 43 | ok("sync" in store, "store.sync exists"); |
michael@0 | 44 | |
michael@0 | 45 | gStore = stores[0]; |
michael@0 | 46 | |
michael@0 | 47 | runTest(); |
michael@0 | 48 | }, cbError); |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | function testStoreGet(id, value) { |
michael@0 | 52 | gStore.get(id).then(function(what) { |
michael@0 | 53 | ok(true, "store.get() retrieves data"); |
michael@0 | 54 | is(what, value, "store.get(" + id + ") returns " + value); |
michael@0 | 55 | }, function() { |
michael@0 | 56 | ok(false, "store.get(" + id + ") retrieves data"); |
michael@0 | 57 | }).then(runTest, cbError); |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | function testStoreAdd(value) { |
michael@0 | 61 | return gStore.add(value).then(function(what) { |
michael@0 | 62 | ok(true, "store.add() is called"); |
michael@0 | 63 | ok(what > 0, "store.add() returns something"); |
michael@0 | 64 | return what; |
michael@0 | 65 | }, cbError); |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | function testStorePut(value, id) { |
michael@0 | 69 | return gStore.put(value, id).then(function() { |
michael@0 | 70 | ok(true, "store.put() is called"); |
michael@0 | 71 | }, cbError); |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | function testStoreGetLength(number) { |
michael@0 | 75 | return gStore.getLength().then(function(n) { |
michael@0 | 76 | is(number, n, "store.getLength() returns the right number"); |
michael@0 | 77 | }, cbError); |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | function testStoreRemove(id) { |
michael@0 | 81 | return gStore.remove(id).then(function() { |
michael@0 | 82 | ok(true, "store.remove() is called"); |
michael@0 | 83 | }, cbError); |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | function testStoreClear() { |
michael@0 | 87 | return gStore.clear().then(function() { |
michael@0 | 88 | ok(true, "store.clear() is called"); |
michael@0 | 89 | }, cbError); |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | var tests = [ |
michael@0 | 93 | // Test for GetDataStore |
michael@0 | 94 | testGetDataStores, |
michael@0 | 95 | |
michael@0 | 96 | // Unknown ID |
michael@0 | 97 | function() { testStoreGet(42, undefined); }, |
michael@0 | 98 | function() { testStoreGet(42, undefined); }, // twice |
michael@0 | 99 | |
michael@0 | 100 | // Add + Get - number |
michael@0 | 101 | function() { testStoreAdd(42).then(function(id) { |
michael@0 | 102 | gId = id; runTest(); }, cbError); }, |
michael@0 | 103 | function() { testStoreGet(gId, 42); }, |
michael@0 | 104 | |
michael@0 | 105 | // Add + Get - boolean |
michael@0 | 106 | function() { testStoreAdd(true).then(function(id) { |
michael@0 | 107 | gId = id; runTest(); }, cbError); }, |
michael@0 | 108 | function() { testStoreGet(gId, true); }, |
michael@0 | 109 | |
michael@0 | 110 | // Add + Get - string |
michael@0 | 111 | function() { testStoreAdd("hello world").then(function(id) { |
michael@0 | 112 | gId = id; runTest(); }, cbError); }, |
michael@0 | 113 | function() { testStoreGet(gId, "hello world"); }, |
michael@0 | 114 | |
michael@0 | 115 | // Put + Get - string |
michael@0 | 116 | function() { testStorePut("hello world 2", gId).then(function() { |
michael@0 | 117 | runTest(); }, cbError); }, |
michael@0 | 118 | function() { testStoreGet(gId, "hello world 2"); }, |
michael@0 | 119 | |
michael@0 | 120 | // getLength |
michael@0 | 121 | function() { testStoreGetLength(3).then(function() { runTest(); }, cbError); }, |
michael@0 | 122 | |
michael@0 | 123 | // Remove |
michael@0 | 124 | function() { testStoreRemove(gId).then(function(what) { |
michael@0 | 125 | runTest(); }, cbError); }, |
michael@0 | 126 | function() { testStoreGet(gId, undefined); }, |
michael@0 | 127 | |
michael@0 | 128 | // Remove - wrong ID |
michael@0 | 129 | function() { testStoreRemove(gId).then(function(what) { |
michael@0 | 130 | runTest(); }, cbError); }, |
michael@0 | 131 | |
michael@0 | 132 | // Clear |
michael@0 | 133 | function() { testStoreClear().then(function(what) { |
michael@0 | 134 | runTest(); }, cbError); }, |
michael@0 | 135 | ]; |
michael@0 | 136 | |
michael@0 | 137 | function runTest() { |
michael@0 | 138 | if (!tests.length) { |
michael@0 | 139 | finish(); |
michael@0 | 140 | return; |
michael@0 | 141 | } |
michael@0 | 142 | |
michael@0 | 143 | var test = tests.shift(); |
michael@0 | 144 | test(); |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | runTest(); |
michael@0 | 148 | </script> |
michael@0 | 149 | </body> |
michael@0 | 150 | </html> |