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 - string or unsigned long keys</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 | var gEvent; |
michael@0 | 13 | var gChangeId; |
michael@0 | 14 | |
michael@0 | 15 | function is(a, b, msg) { |
michael@0 | 16 | alert((a === b ? 'OK' : 'KO') + ' ' + msg) |
michael@0 | 17 | } |
michael@0 | 18 | |
michael@0 | 19 | function ok(a, msg) { |
michael@0 | 20 | alert((a ? 'OK' : 'KO')+ ' ' + msg) |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | function cbError() { |
michael@0 | 24 | alert('KO error'); |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | function finish() { |
michael@0 | 28 | alert('DONE'); |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | function testGetDataStores() { |
michael@0 | 32 | navigator.getDataStores('foo').then(function(stores) { |
michael@0 | 33 | gStore = stores[0]; |
michael@0 | 34 | runTest(); |
michael@0 | 35 | }, cbError); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | function testAdd_noKey(key) { |
michael@0 | 39 | gEvent = 'added'; |
michael@0 | 40 | gChangeId = key; |
michael@0 | 41 | |
michael@0 | 42 | gStore.add({ a: 42 }).then(function(id) { |
michael@0 | 43 | is(id, key, "Id must be " + key + " received: " + id); |
michael@0 | 44 | }); |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | function testAdd_withKey(key) { |
michael@0 | 48 | gEvent = 'added'; |
michael@0 | 49 | gChangeId = key; |
michael@0 | 50 | |
michael@0 | 51 | gStore.add({ a: 42 }, key).then(function(id) { |
michael@0 | 52 | is(id, key, "Id must be " + key + " received: " + id); |
michael@0 | 53 | }); |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | function testPut(key) { |
michael@0 | 57 | gEvent = 'updated'; |
michael@0 | 58 | gChangeId = key; |
michael@0 | 59 | |
michael@0 | 60 | gStore.put({ a: 42 }, key).then(function(id) { |
michael@0 | 61 | is(id, key, "Id must be " + key + " received: " + id); |
michael@0 | 62 | }); |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | function testGet(key) { |
michael@0 | 66 | gStore.get(key).then(function(value) { |
michael@0 | 67 | ok(value, "Object received!"); |
michael@0 | 68 | is(value.a, 42, "Object received with right value!"); |
michael@0 | 69 | runTest(); |
michael@0 | 70 | }); |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | function testArrayGet(key) { |
michael@0 | 74 | gStore.get.apply(gStore, key).then(function(values) { |
michael@0 | 75 | is(values.length, key.length, "Object received!"); |
michael@0 | 76 | for (var i = 0; i < values.length; ++i) { |
michael@0 | 77 | is(values[i].a, 42, "Object received with right value!"); |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | runTest(); |
michael@0 | 81 | }); |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | function testRemove(key, success) { |
michael@0 | 85 | gEvent = 'removed'; |
michael@0 | 86 | gChangeId = key; |
michael@0 | 87 | |
michael@0 | 88 | gStore.remove(key).then(function(value) { |
michael@0 | 89 | is(value, success, "Status must be " + success + " received: " + value); |
michael@0 | 90 | if (value == false) { |
michael@0 | 91 | runTest(); |
michael@0 | 92 | return; |
michael@0 | 93 | } |
michael@0 | 94 | }); |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | function eventListener() { |
michael@0 | 98 | gStore.onchange = function(e) { |
michael@0 | 99 | is(e.operation, gEvent, "Operation matches: " + e.operation + " " + gEvent); |
michael@0 | 100 | ok(e.id === gChangeId, "Operation id matches"); |
michael@0 | 101 | runTest(); |
michael@0 | 102 | }; |
michael@0 | 103 | |
michael@0 | 104 | runTest(); |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | var tests = [ |
michael@0 | 108 | // Test for GetDataStore |
michael@0 | 109 | testGetDataStores, |
michael@0 | 110 | |
michael@0 | 111 | // Event listener |
michael@0 | 112 | eventListener, |
michael@0 | 113 | |
michael@0 | 114 | // add |
michael@0 | 115 | function() { testAdd_noKey(1); }, |
michael@0 | 116 | function() { testAdd_withKey(123); }, |
michael@0 | 117 | function() { testAdd_noKey(124); }, |
michael@0 | 118 | function() { testAdd_withKey('foobar'); }, |
michael@0 | 119 | function() { testAdd_noKey(125); }, |
michael@0 | 120 | function() { testAdd_withKey('125'); }, |
michael@0 | 121 | function() { testAdd_withKey('126'); }, |
michael@0 | 122 | function() { testAdd_noKey(126); }, |
michael@0 | 123 | |
michael@0 | 124 | // put |
michael@0 | 125 | function() { testPut(42); }, |
michael@0 | 126 | function() { testPut('42'); }, |
michael@0 | 127 | |
michael@0 | 128 | // get |
michael@0 | 129 | function() { testGet('42'); }, |
michael@0 | 130 | function() { testGet(42); }, |
michael@0 | 131 | function() { testGet(1); }, |
michael@0 | 132 | function() { testGet(123); }, |
michael@0 | 133 | function() { testGet(124); }, |
michael@0 | 134 | function() { testGet('foobar'); }, |
michael@0 | 135 | function() { testGet(125); }, |
michael@0 | 136 | function() { testGet('125'); }, |
michael@0 | 137 | function() { testGet('126'); }, |
michael@0 | 138 | function() { testGet(126); }, |
michael@0 | 139 | function() { testArrayGet(['42', 42, 1, 123, 124, 'foobar', 125, '125', '126', 126]); }, |
michael@0 | 140 | |
michael@0 | 141 | // remove |
michael@0 | 142 | function() { testRemove(42, true); }, |
michael@0 | 143 | function() { testRemove('42', true); }, |
michael@0 | 144 | function() { testRemove('43', false); }, |
michael@0 | 145 | function() { testRemove(43, false); }, |
michael@0 | 146 | ]; |
michael@0 | 147 | |
michael@0 | 148 | function runTest() { |
michael@0 | 149 | if (!tests.length) { |
michael@0 | 150 | finish(); |
michael@0 | 151 | return; |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | var test = tests.shift(); |
michael@0 | 155 | test(); |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | runTest(); |
michael@0 | 159 | </script> |
michael@0 | 160 | </body> |
michael@0 | 161 | </html> |