1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/datastore/tests/file_keys.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,161 @@ 1.4 +<!DOCTYPE HTML> 1.5 +<html> 1.6 +<head> 1.7 + <meta charset="utf-8"> 1.8 + <title>Test for DataStore - string or unsigned long keys</title> 1.9 +</head> 1.10 +<body> 1.11 +<div id="container"></div> 1.12 + <script type="application/javascript;version=1.7"> 1.13 + 1.14 + var gStore; 1.15 + var gEvent; 1.16 + var gChangeId; 1.17 + 1.18 + function is(a, b, msg) { 1.19 + alert((a === b ? 'OK' : 'KO') + ' ' + msg) 1.20 + } 1.21 + 1.22 + function ok(a, msg) { 1.23 + alert((a ? 'OK' : 'KO')+ ' ' + msg) 1.24 + } 1.25 + 1.26 + function cbError() { 1.27 + alert('KO error'); 1.28 + } 1.29 + 1.30 + function finish() { 1.31 + alert('DONE'); 1.32 + } 1.33 + 1.34 + function testGetDataStores() { 1.35 + navigator.getDataStores('foo').then(function(stores) { 1.36 + gStore = stores[0]; 1.37 + runTest(); 1.38 + }, cbError); 1.39 + } 1.40 + 1.41 + function testAdd_noKey(key) { 1.42 + gEvent = 'added'; 1.43 + gChangeId = key; 1.44 + 1.45 + gStore.add({ a: 42 }).then(function(id) { 1.46 + is(id, key, "Id must be " + key + " received: " + id); 1.47 + }); 1.48 + } 1.49 + 1.50 + function testAdd_withKey(key) { 1.51 + gEvent = 'added'; 1.52 + gChangeId = key; 1.53 + 1.54 + gStore.add({ a: 42 }, key).then(function(id) { 1.55 + is(id, key, "Id must be " + key + " received: " + id); 1.56 + }); 1.57 + } 1.58 + 1.59 + function testPut(key) { 1.60 + gEvent = 'updated'; 1.61 + gChangeId = key; 1.62 + 1.63 + gStore.put({ a: 42 }, key).then(function(id) { 1.64 + is(id, key, "Id must be " + key + " received: " + id); 1.65 + }); 1.66 + } 1.67 + 1.68 + function testGet(key) { 1.69 + gStore.get(key).then(function(value) { 1.70 + ok(value, "Object received!"); 1.71 + is(value.a, 42, "Object received with right value!"); 1.72 + runTest(); 1.73 + }); 1.74 + } 1.75 + 1.76 + function testArrayGet(key) { 1.77 + gStore.get.apply(gStore, key).then(function(values) { 1.78 + is(values.length, key.length, "Object received!"); 1.79 + for (var i = 0; i < values.length; ++i) { 1.80 + is(values[i].a, 42, "Object received with right value!"); 1.81 + } 1.82 + 1.83 + runTest(); 1.84 + }); 1.85 + } 1.86 + 1.87 + function testRemove(key, success) { 1.88 + gEvent = 'removed'; 1.89 + gChangeId = key; 1.90 + 1.91 + gStore.remove(key).then(function(value) { 1.92 + is(value, success, "Status must be " + success + " received: " + value); 1.93 + if (value == false) { 1.94 + runTest(); 1.95 + return; 1.96 + } 1.97 + }); 1.98 + } 1.99 + 1.100 + function eventListener() { 1.101 + gStore.onchange = function(e) { 1.102 + is(e.operation, gEvent, "Operation matches: " + e.operation + " " + gEvent); 1.103 + ok(e.id === gChangeId, "Operation id matches"); 1.104 + runTest(); 1.105 + }; 1.106 + 1.107 + runTest(); 1.108 + } 1.109 + 1.110 + var tests = [ 1.111 + // Test for GetDataStore 1.112 + testGetDataStores, 1.113 + 1.114 + // Event listener 1.115 + eventListener, 1.116 + 1.117 + // add 1.118 + function() { testAdd_noKey(1); }, 1.119 + function() { testAdd_withKey(123); }, 1.120 + function() { testAdd_noKey(124); }, 1.121 + function() { testAdd_withKey('foobar'); }, 1.122 + function() { testAdd_noKey(125); }, 1.123 + function() { testAdd_withKey('125'); }, 1.124 + function() { testAdd_withKey('126'); }, 1.125 + function() { testAdd_noKey(126); }, 1.126 + 1.127 + // put 1.128 + function() { testPut(42); }, 1.129 + function() { testPut('42'); }, 1.130 + 1.131 + // get 1.132 + function() { testGet('42'); }, 1.133 + function() { testGet(42); }, 1.134 + function() { testGet(1); }, 1.135 + function() { testGet(123); }, 1.136 + function() { testGet(124); }, 1.137 + function() { testGet('foobar'); }, 1.138 + function() { testGet(125); }, 1.139 + function() { testGet('125'); }, 1.140 + function() { testGet('126'); }, 1.141 + function() { testGet(126); }, 1.142 + function() { testArrayGet(['42', 42, 1, 123, 124, 'foobar', 125, '125', '126', 126]); }, 1.143 + 1.144 + // remove 1.145 + function() { testRemove(42, true); }, 1.146 + function() { testRemove('42', true); }, 1.147 + function() { testRemove('43', false); }, 1.148 + function() { testRemove(43, false); }, 1.149 + ]; 1.150 + 1.151 + function runTest() { 1.152 + if (!tests.length) { 1.153 + finish(); 1.154 + return; 1.155 + } 1.156 + 1.157 + var test = tests.shift(); 1.158 + test(); 1.159 + } 1.160 + 1.161 + runTest(); 1.162 + </script> 1.163 +</body> 1.164 +</html>