test_key_requirements.js (6624B)
1 /** 2 * Any copyright is dedicated to the Public Domain. 3 * http://creativecommons.org/publicdomain/zero/1.0/ 4 */ 5 6 /* exported testGenerator */ 7 var testGenerator = testSteps(); 8 9 function* testSteps() { 10 const name = this.window ? window.location.pathname : "Splendid Test"; 11 12 let request = indexedDB.open(name, 1); 13 request.onerror = errorHandler; 14 request.onupgradeneeded = grabEventAndContinueHandler; 15 request.onsuccess = grabEventAndContinueHandler; 16 let event = yield undefined; 17 18 let db = event.target.result; 19 db.addEventListener("error", function (event) { 20 event.preventDefault(); 21 }); 22 23 let objectStore = db.createObjectStore("foo", { autoIncrement: true }); 24 25 request = objectStore.add({}); 26 request.onerror = errorHandler; 27 request.onsuccess = grabEventAndContinueHandler; 28 event = yield undefined; 29 30 let key1 = event.target.result; 31 32 request = objectStore.put({}, key1); 33 request.onerror = errorHandler; 34 request.onsuccess = grabEventAndContinueHandler; 35 event = yield undefined; 36 37 is(event.target.result, key1, "put gave the same key back"); 38 39 let key2 = 10; 40 41 request = objectStore.put({}, key2); 42 request.onerror = errorHandler; 43 request.onsuccess = grabEventAndContinueHandler; 44 event = yield undefined; 45 46 is(event.target.result, key2, "put gave the same key back"); 47 48 key2 = 100; 49 50 request = objectStore.add({}, key2); 51 request.onerror = errorHandler; 52 request.onsuccess = grabEventAndContinueHandler; 53 event = yield undefined; 54 55 is(event.target.result, key2, "put gave the same key back"); 56 57 try { 58 objectStore.put({}); 59 ok(true, "put with no key should not throw with autoIncrement!"); 60 } catch (e) { 61 ok(false, "put with no key threw with autoIncrement"); 62 } 63 64 try { 65 objectStore.put({}); 66 ok(true, "put with no key should not throw with autoIncrement!"); 67 } catch (e) { 68 ok(false, "put with no key threw with autoIncrement"); 69 } 70 71 try { 72 objectStore.delete(); 73 ok(false, "remove with no key should throw!"); 74 } catch (e) { 75 ok(true, "remove with no key threw"); 76 } 77 78 objectStore = db.createObjectStore("bar"); 79 80 try { 81 objectStore.add({}); 82 ok(false, "add with no key should throw!"); 83 } catch (e) { 84 ok(true, "add with no key threw"); 85 } 86 87 try { 88 objectStore.put({}); 89 ok(false, "put with no key should throw!"); 90 } catch (e) { 91 ok(true, "put with no key threw"); 92 } 93 94 try { 95 objectStore.put({}); 96 ok(false, "put with no key should throw!"); 97 } catch (e) { 98 ok(true, "put with no key threw"); 99 } 100 101 try { 102 objectStore.delete(); 103 ok(false, "remove with no key should throw!"); 104 } catch (e) { 105 ok(true, "remove with no key threw"); 106 } 107 108 objectStore = db.createObjectStore("baz", { keyPath: "id" }); 109 110 try { 111 objectStore.add({}); 112 ok(false, "add with no key should throw!"); 113 } catch (e) { 114 ok(true, "add with no key threw"); 115 } 116 117 try { 118 objectStore.add({ id: 5 }, 5); 119 ok(false, "add with inline key and passed key should throw!"); 120 } catch (e) { 121 ok(true, "add with inline key and passed key threw"); 122 } 123 124 try { 125 objectStore.put({}); 126 ok(false, "put with no key should throw!"); 127 } catch (e) { 128 ok(true, "put with no key threw"); 129 } 130 131 try { 132 objectStore.put({}); 133 ok(false, "put with no key should throw!"); 134 } catch (e) { 135 ok(true, "put with no key threw"); 136 } 137 138 try { 139 objectStore.delete(); 140 ok(false, "remove with no key should throw!"); 141 } catch (e) { 142 ok(true, "remove with no key threw"); 143 } 144 145 key1 = 10; 146 147 request = objectStore.add({ id: key1 }); 148 request.onerror = errorHandler; 149 request.onsuccess = grabEventAndContinueHandler; 150 event = yield undefined; 151 152 is(event.target.result, key1, "add gave back the same key"); 153 154 request = objectStore.put({ id: 10 }); 155 request.onerror = errorHandler; 156 request.onsuccess = grabEventAndContinueHandler; 157 event = yield undefined; 158 159 is(event.target.result, key1, "put gave back the same key"); 160 161 request = objectStore.put({ id: 10 }); 162 request.onerror = errorHandler; 163 request.onsuccess = grabEventAndContinueHandler; 164 event = yield undefined; 165 166 is(event.target.result, key1, "put gave back the same key"); 167 168 request = objectStore.add({ id: 10 }); 169 request.addEventListener("error", new ExpectError("ConstraintError", true)); 170 request.onsuccess = unexpectedSuccessHandler; 171 event = yield undefined; 172 173 try { 174 objectStore.add({}, null); 175 ok(false, "add with null key should throw!"); 176 } catch (e) { 177 ok(true, "add with null key threw"); 178 } 179 180 try { 181 objectStore.put({}, null); 182 ok(false, "put with null key should throw!"); 183 } catch (e) { 184 ok(true, "put with null key threw"); 185 } 186 187 try { 188 objectStore.put({}, null); 189 ok(false, "put with null key should throw!"); 190 } catch (e) { 191 ok(true, "put with null key threw"); 192 } 193 194 try { 195 objectStore.delete({}, null); 196 ok(false, "remove with null key should throw!"); 197 } catch (e) { 198 ok(true, "remove with null key threw"); 199 } 200 201 objectStore = db.createObjectStore("bazing", { 202 keyPath: "id", 203 autoIncrement: true, 204 }); 205 206 request = objectStore.add({}); 207 request.onerror = errorHandler; 208 request.onsuccess = grabEventAndContinueHandler; 209 event = yield undefined; 210 211 key1 = event.target.result; 212 213 request = objectStore.put({ id: key1 }); 214 request.onerror = errorHandler; 215 request.onsuccess = grabEventAndContinueHandler; 216 event = yield undefined; 217 218 is(event.target.result, key1, "put gave the same key back"); 219 220 key2 = 10; 221 222 request = objectStore.put({ id: key2 }); 223 request.onerror = errorHandler; 224 request.onsuccess = grabEventAndContinueHandler; 225 event = yield undefined; 226 227 is(event.target.result, key2, "put gave the same key back"); 228 229 try { 230 objectStore.put({}); 231 ok(true, "put with no key should not throw with autoIncrement!"); 232 } catch (e) { 233 ok(false, "put with no key threw with autoIncrement"); 234 } 235 236 try { 237 objectStore.put({}); 238 ok(true, "put with no key should not throw with autoIncrement!"); 239 } catch (e) { 240 ok(false, "put with no key threw with autoIncrement"); 241 } 242 243 try { 244 objectStore.delete(); 245 ok(false, "remove with no key should throw!"); 246 } catch (e) { 247 ok(true, "remove with no key threw"); 248 } 249 250 try { 251 objectStore.add({ id: 5 }, 5); 252 ok(false, "add with inline key and passed key should throw!"); 253 } catch (e) { 254 ok(true, "add with inline key and passed key threw"); 255 } 256 257 request = objectStore.delete(key2); 258 request.onerror = errorHandler; 259 request.onsuccess = grabEventAndContinueHandler; 260 event = yield undefined; 261 262 // Wait for success 263 yield undefined; 264 265 finishTest(); 266 }