idbtransaction_abort.any.js (3202B)
1 // META: title=IDBTransaction - abort 2 // META: global=window,worker 3 // META: script=resources/support.js 4 5 // Spec: https://w3c.github.io/IndexedDB/#transaction-abort 6 7 'use strict'; 8 9 async_test(t => { 10 let db; 11 let aborted; 12 const record = {indexedProperty: 'bar'}; 13 14 let open_rq = createdb(t); 15 open_rq.onupgradeneeded = function(e) { 16 db = e.target.result; 17 let txn = e.target.transaction; 18 let objStore = db.createObjectStore('store'); 19 objStore.add(record, 1); 20 objStore.add(record, 2); 21 let index = 22 objStore.createIndex('index', 'indexedProperty', {unique: true}); 23 24 assert_true(index instanceof IDBIndex, 'IDBIndex'); 25 26 e.target.transaction.onabort = t.step_func(function(e) { 27 aborted = true; 28 assert_equals(e.type, 'abort', 'event type'); 29 }); 30 31 db.onabort = function(e) { 32 assert_true(aborted, 'transaction.abort event has fired'); 33 t.done(); 34 }; 35 36 e.target.transaction.oncomplete = fail(t, 'got complete, expected abort'); 37 }; 38 }, 'Abort event should fire during transaction'); 39 40 indexeddb_test( 41 (t, db) => { 42 db.createObjectStore('blobs', {keyPath: 'id', autoIncrement: true}); 43 }, 44 (t, db) => { 45 const txn = db.transaction('blobs', 'readwrite'); 46 const objectStore = txn.objectStore('blobs'); 47 const data = new Blob(['test'], {type: 'text/plain'}); 48 49 const putRequest = objectStore.put({id: 0, data: data}); 50 51 putRequest.onsuccess = t.step_func(() => { 52 t.step_timeout(() => { 53 assert_throws_dom('InvalidStateError', () => { 54 txn.abort(); 55 }, 'Abort should throw InvalidStateError on an auto-committing transaction.'); 56 }, 0); 57 }); 58 59 // Ensure the transaction completes. 60 txn.oncomplete = t.step_func(() => { 61 t.done(); 62 }); 63 64 // Abort should fail once the transaction has started committing. 65 txn.onabort = t.step_func((event) => { 66 assert_unreached('Unexpected transaction abort: ' + event.target.error); 67 }); 68 t.add_cleanup(() => { 69 if (db) { 70 db.close(); 71 } 72 }); 73 }, 74 `Abort during auto-committing should throw InvalidStateError.`); 75 76 indexeddb_test( 77 (t, db) => { 78 db.createObjectStore('blobs', {keyPath: 'id', autoIncrement: true}); 79 }, 80 (t, db) => { 81 const txn = db.transaction('blobs', 'readwrite'); 82 const objectStore = txn.objectStore('blobs'); 83 const data = new Blob(['test'], {type: 'text/plain'}); 84 85 // Put the object into the store. 86 const putRequest = objectStore.put({id: 0, data: data}); 87 88 // Handle transaction completion. 89 txn.oncomplete = t.step_func(() => { 90 assert_throws_dom('InvalidStateError', () => { 91 txn.abort(); 92 }, 'Abort should throw InvalidStateError on a completed transaction.'); 93 t.done(); 94 }); 95 96 // Handle transaction error. 97 txn.onerror = t.step_func((event) => { 98 assert_unreached('Unexpected transaction error: ' + event.target.error); 99 }); 100 101 t.add_cleanup(() => { 102 if (db) { 103 db.close(); 104 } 105 }); 106 }, 107 `Abort on completed transaction should throw InvalidStateError.`);