summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/IndexedDB/fire-error-event-exception.html
blob: f380e4081f103dbb9f510b8f73f0f299bd6c27bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<!DOCTYPE html>
<meta charset=utf-8>
<title>Fire error event - Exception thrown</title>
<link rel="help" href="https://w3c.github.io/IndexedDB/#fire-error-event">
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=resources/support.js></script>
<script>
setup({allow_uncaught_exception:true});

function fire_error_event_test(func, description) {
  indexeddb_test(
    (t, db) => {
      db.createObjectStore('s');
    },
    (t, db) => {
      const tx = db.transaction('s', 'readwrite', {durability: 'relaxed'});
      tx.oncomplete = t.unreached_func('transaction should abort');
      const store = tx.objectStore('s');
      store.put(0, 0);
      const request = store.add(0, 0);
      request.onsuccess = t.unreached_func('request should fail');
      func(t, db, tx, request);
      tx.addEventListener('abort', t.step_func_done(() => {
        assert_equals(tx.error.name, 'AbortError');
      }));
    },
    description);
}

// Listeners on the request.

fire_error_event_test((t, db, tx, request) => {
  request.onerror = () => {
    throw Error();
  };
}, 'Exception in error event handler on request');

fire_error_event_test((t, db, tx, request) => {
  request.onerror = e => {
    e.preventDefault();
    throw Error();
  };
}, 'Exception in error event handler on request, with preventDefault');

fire_error_event_test((t, db, tx, request) => {
  request.addEventListener('error', () => {
    throw Error();
  });
}, 'Exception in error event listener on request');

fire_error_event_test((t, db, tx, request) => {
  request.addEventListener('error', {
    get handleEvent() {
      throw new Error();
    },
  });
}, 'Exception in error event listener ("handleEvent" lookup) on request');

fire_error_event_test((t, db, tx, request) => {
  request.addEventListener('error', {});
}, 'Exception in error event listener (non-callable "handleEvent") on request');

fire_error_event_test((t, db, tx, request) => {
  request.addEventListener('error', () => {
    // no-op
  });
  request.addEventListener('error', () => {
    throw Error();
  });
}, 'Exception in second error event listener on request');

fire_error_event_test((t, db, tx, request) => {
  let second_listener_called = false;
  request.addEventListener('error', () => {
    throw Error();
  });
  request.addEventListener('error', t.step_func(() => {
    second_listener_called = true;
    assert_true(is_transaction_active(tx, 's'),
                'Transaction should be active until dispatch completes');
  }));
  tx.addEventListener('abort', t.step_func(() => {
    assert_true(second_listener_called);
  }));
}, 'Exception in first error event listener on request, ' +
   'transaction active in second');

// Listeners on the transaction.

fire_error_event_test((t, db, tx, request) => {
  tx.onerror = () => {
    throw Error();
  };
}, 'Exception in error event handler on transaction');

fire_error_event_test((t, db, tx, request) => {
  tx.onerror = e => {
    e.preventDefault();
    throw Error();
  };
}, 'Exception in error event handler on transaction, with preventDefault');

fire_error_event_test((t, db, tx, request) => {
  tx.addEventListener('error', () => {
    throw Error();
  });
}, 'Exception in error event listener on transaction');

fire_error_event_test((t, db, tx, request) => {
  tx.addEventListener('error', () => {
    // no-op
  });
  tx.addEventListener('error', () => {
    throw Error();
  });
}, 'Exception in second error event listener on transaction');

fire_error_event_test((t, db, tx, request) => {
  let second_listener_called = false;
  tx.addEventListener('error', () => {
    throw Error();
  });
  tx.addEventListener('error', t.step_func(() => {
    second_listener_called = true;
    assert_true(is_transaction_active(tx, 's'),
                'Transaction should be active until dispatch completes');
  }));
  tx.addEventListener('abort', t.step_func(() => {
    assert_true(second_listener_called);
  }));
}, 'Exception in first error event listener on transaction, ' +
   'transaction active in second');

// Listeners on the connection.

fire_error_event_test((t, db, tx, request) => {
  db.onerror = () => {
    throw Error();
  };
}, 'Exception in error event handler on connection');

fire_error_event_test((t, db, tx, request) => {
  db.onerror = e => {
    e.preventDefault()
    throw Error();
  };
}, 'Exception in error event handler on connection, with preventDefault');

fire_error_event_test((t, db, tx, request) => {
  db.addEventListener('error', () => {
    throw Error();
  });
}, 'Exception in error event listener on connection');

fire_error_event_test((t, db, tx, request) => {
  db.addEventListener('error', () => {
    // no-op
  });
  db.addEventListener('error', () => {
    throw Error();
  });
}, 'Exception in second error event listener on connection');

fire_error_event_test((t, db, tx, request) => {
  let second_listener_called = false;
  db.addEventListener('error', () => {
    throw Error();
  });
  db.addEventListener('error', t.step_func(() => {
    second_listener_called = true;
    assert_true(is_transaction_active(tx, 's'),
                'Transaction should be active until dispatch completes');
  }));
  tx.addEventListener('abort', t.step_func(() => {
    assert_true(second_listener_called);
  }));
}, 'Exception in first error event listener on connection, ' +
   'transaction active in second');

</script>