46f249c9849208288787cd5446a71725e992c233
[gecko.git] / event-dispatch-active-flag.html
blob46f249c9849208288787cd5446a71725e992c233
1 <!DOCTYPE html>
2 <meta charset=utf-8>
3 <title>Transaction active flag is set during event dispatch</title>
4 <link rel="help" href="https://w3c.github.io/IndexedDB/#fire-success-event">
5 <link rel="help" href="https://w3c.github.io/IndexedDB/#fire-error-event">
6 <script src=/resources/testharness.js></script>
7 <script src=/resources/testharnessreport.js></script>
8 <script src=resources/support.js></script>
9 <script>
11 indexeddb_test(
12 (t, db, tx) => {
13 db.createObjectStore('store');
15 (t, db) => {
16 const tx = db.transaction('store', 'readonly', {durability: 'relaxed'});
17 const release_tx = keep_alive(tx, 'store');
19 assert_true(is_transaction_active(tx, 'store'),
20 'Transaction should be active after creation');
22 const request = tx.objectStore('store').get(0);
23 request.onerror = t.unreached_func('request should succeed');
24 request.onsuccess = () => {
25 assert_true(is_transaction_active(tx, 'store'),
26 'Transaction should be active during success handler');
28 let saw_handler_promise = false;
29 Promise.resolve().then(t.step_func(() => {
30 saw_handler_promise = true;
31 assert_true(is_transaction_active(tx, 'store'),
32 'Transaction should be active in handler\'s microtasks');
33 }));
35 setTimeout(t.step_func(() => {
36 assert_true(saw_handler_promise);
37 assert_false(is_transaction_active(tx, 'store'),
38 'Transaction should be inactive in next task');
39 release_tx();
40 t.done();
41 }), 0);
44 'Transactions are active during success handlers');
46 indexeddb_test(
47 (t, db, tx) => {
48 db.createObjectStore('store');
50 (t, db) => {
51 const tx = db.transaction('store', 'readonly', {durability: 'relaxed'});
52 const release_tx = keep_alive(tx, 'store');
53 assert_true(is_transaction_active(tx, 'store'),
54 'Transaction should be active after creation');
56 const request = tx.objectStore('store').get(0);
57 request.onerror = t.unreached_func('request should succeed');
58 request.addEventListener('success', () => {
59 assert_true(is_transaction_active(tx, 'store'),
60 'Transaction should be active during success listener');
62 let saw_listener_promise = false;
63 Promise.resolve().then(t.step_func(() => {
64 saw_listener_promise = true;
65 assert_true(is_transaction_active(tx, 'store'),
66 'Transaction should be active in listener\'s microtasks');
67 }));
69 setTimeout(t.step_func(() => {
70 assert_true(saw_listener_promise);
71 assert_false(is_transaction_active(tx, 'store'),
72 'Transaction should be inactive in next task');
73 release_tx();
74 t.done();
75 }), 0);
76 });
78 'Transactions are active during success listeners');
80 indexeddb_test(
81 (t, db, tx) => {
82 db.createObjectStore('store');
84 (t, db) => {
85 const tx = db.transaction('store', 'readwrite', {durability: 'relaxed'});
86 const release_tx = keep_alive(tx, 'store');
87 assert_true(is_transaction_active(tx, 'store'),
88 'Transaction should be active after creation');
90 tx.objectStore('store').put(0, 0);
91 const request = tx.objectStore('store').add(0, 0);
92 request.onsuccess = t.unreached_func('request should fail');
93 request.onerror = e => {
94 e.preventDefault();
96 assert_true(is_transaction_active(tx, 'store'),
97 'Transaction should be active during error handler');
99 let saw_handler_promise = false;
100 Promise.resolve().then(t.step_func(() => {
101 saw_handler_promise = true;
102 assert_true(is_transaction_active(tx, 'store'),
103 'Transaction should be active in handler\'s microtasks');
104 }));
106 setTimeout(t.step_func(() => {
107 assert_true(saw_handler_promise);
108 assert_false(is_transaction_active(tx, 'store'),
109 'Transaction should be inactive in next task');
110 release_tx();
111 t.done();
112 }), 0);
115 'Transactions are active during error handlers');
117 indexeddb_test(
118 (t, db, tx) => {
119 db.createObjectStore('store');
121 (t, db) => {
122 const tx = db.transaction('store', 'readwrite', {durability: 'relaxed'});
123 const release_tx = keep_alive(tx, 'store');
124 assert_true(is_transaction_active(tx, 'store'),
125 'Transaction should be active after creation');
127 tx.objectStore('store').put(0, 0);
128 const request = tx.objectStore('store').add(0, 0);
129 request.onsuccess = t.unreached_func('request should fail');
130 request.addEventListener('error', e => {
131 e.preventDefault();
133 assert_true(is_transaction_active(tx, 'store'),
134 'Transaction should be active during error listener');
136 let saw_listener_promise = false;
137 Promise.resolve().then(t.step_func(() => {
138 saw_listener_promise = true;
139 assert_true(is_transaction_active(tx, 'store'),
140 'Transaction should be active in listener\'s microtasks');
141 }));
143 setTimeout(t.step_func(() => {
144 assert_true(saw_listener_promise);
145 assert_false(is_transaction_active(tx, 'store'),
146 'Transaction should be inactive in next task');
147 release_tx();
148 t.done();
149 }), 0);
152 'Transactions are active during error listeners');
154 </script>