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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
<!--
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
-->
<html>
<head>
<title>Indexed Database Property Test</title>
<script type="text/javascript;version=1.7">
let testGenerator = testSteps();
function ok(val, message) {
val = val ? "true" : "false";
window.parent.postMessage("SimpleTest.ok(" + val + ", '" + message +
"');", "*");
}
function is(a, b, message) {
ok(a == b, message);
}
function grabEventAndContinueHandler(event) {
testGenerator.send(event);
}
function errorHandler(event) {
ok(false, "indexedDB error, code " + event.target.errorCcode);
finishTest();
}
function unexpectedSuccessHandler(event) {
ok(false, "got success when it was not expected!");
finishTest();
}
function finishTest() {
// Let window.onerror have a chance to fire
setTimeout(function() {
setTimeout(function() {
testGenerator.close();
window.parent.postMessage("SimpleTest.finish();", "*");
}, 0);
}, 0);
}
window.onerror = function(message, filename, lineno) {
is(message, "ConstraintError", "Expect a constraint error");
};
function testSteps() {
window.parent.SpecialPowers.addPermission("indexedDB", true, document);
let request = indexedDB.open(window.location.pathname, 1);
request.onsuccess = unexpectedSuccessHandler;
request.onerror = grabEventAndContinueHandler;
request.onupgradeneeded = grabEventAndContinueHandler;
let event = yield undefined;
let db = event.target.result;
db.onversionchange = function(event) {
event.target.close();
};
is(db.version, 1, "Correct version");
is(db.objectStoreNames.length, 0, "Correct objectStoreNames length");
let trans = event.target.transaction;
trans.oncomplete = unexpectedSuccessHandler;
trans.onabort = grabEventAndContinueHandler;
let objectStore = db.createObjectStore("foo");
is(db.objectStoreNames.length, 1, "Correct objectStoreNames length");
ok(db.objectStoreNames.contains("foo"), "Has correct objectStore");
let originalRequest = request;
request = objectStore.add({}, 1);
request.onsuccess = grabEventAndContinueHandler;
request.onerror = errorHandler;
event = yield undefined;
request = objectStore.add({}, 1);
request.onsuccess = unexpectedSuccessHandler;
request.onerror = function(event) {
// Don't do anything! ConstraintError is expected in window.onerror.
}
event = yield undefined;
is(event.type, "abort", "Got a transaction abort event");
is(db.version, 0, "Correct version");
is(db.objectStoreNames.length, 0, "Correct objectStoreNames length");
is(trans.error.name, "ConstraintError", "Right error");
ok(trans.error === request.error, "Object identity holds");
is(originalRequest.transaction, trans, "request.transaction should still be set");
event = yield undefined;
is(event.type, "error", "Got request error event");
is(event.target, originalRequest, "error event has right target");
is(event.target.error.name, "AbortError", "Right error");
is(originalRequest.transaction, null, "request.transaction should now be null");
// Skip the verification of ConstraintError in window.onerror.
event.preventDefault();
request = indexedDB.open(window.location.pathname, 1);
request.onerror = errorHandler;
request.onupgradeneeded = grabEventAndContinueHandler;
event = yield undefined;
db = event.target.result;
db.onversionchange = function(event) {
event.target.close();
};
event.target.transaction.oncomplete = grabEventAndContinueHandler;
event.target.transaction.onabort = unexpectedSuccessHandler;
is(db.version, "1", "Correct version");
is(db.objectStoreNames.length, 0, "Correct objectStoreNames length");
objectStore = db.createObjectStore("foo");
is(db.objectStoreNames.length, 1, "Correct objectStoreNames length");
ok(db.objectStoreNames.contains("foo"), "Has correct objectStore");
objectStore.createIndex("baz", "key.path");
objectStore.createIndex("dontDeleteMe", "");
is(objectStore.indexNames.length, 2, "Correct indexNames length");
ok(objectStore.indexNames.contains("baz"), "Has correct index");
ok(objectStore.indexNames.contains("dontDeleteMe"), "Has correct index");
let objectStoreForDeletion = db.createObjectStore("bar");
is(db.objectStoreNames.length, 2, "Correct objectStoreNames length");
ok(db.objectStoreNames.contains("bar"), "Has correct objectStore");
objectStoreForDeletion.createIndex("foo", "key.path");
is(objectStoreForDeletion.indexNames.length, 1, "Correct indexNames length");
ok(objectStoreForDeletion.indexNames.contains("foo"), "Has correct index");
request = objectStore.add({}, 1);
request.onsuccess = grabEventAndContinueHandler;
request.onerror = errorHandler;
event = yield undefined;
request = objectStore.add({}, 1);
request.onsuccess = unexpectedSuccessHandler;
request.onerror = function(event) {
// Expected, but prevent the abort.
event.preventDefault();
}
event = yield undefined;
is(event.type, "complete", "Got a transaction complete event");
is(db.version, "1", "Correct version");
is(db.objectStoreNames.length, 2, "Correct objectStoreNames length");
ok(db.objectStoreNames.contains("foo"), "Has correct objectStore");
ok(db.objectStoreNames.contains("bar"), "Has correct objectStore");
request = indexedDB.open(window.location.pathname, 2);
request.onerror = errorHandler;
request.onupgradeneeded = grabEventAndContinueHandler;
event = yield undefined;
db = event.target.result;
db.onversionchange = function(event) {
event.target.close();
};
trans = event.target.transaction;
trans.oncomplete = unexpectedSuccessHandler;
is(db.version, "2", "Correct version");
is(db.objectStoreNames.length, 2, "Correct objectStoreNames length");
ok(db.objectStoreNames.contains("foo"), "Has correct objectStore");
ok(db.objectStoreNames.contains("bar"), "Has correct objectStore");
let createdObjectStore = db.createObjectStore("newlyCreated");
objectStore = trans.objectStore("foo");
let deletedObjectStore = trans.objectStore("bar");
deletedObjectStore.deleteIndex("foo");
db.deleteObjectStore("bar");
createdObjectStore.createIndex("newIndex", "key.path");
objectStore.createIndex("newIndex", "key.path");
objectStore.deleteIndex("baz");
is(db.objectStoreNames.length, 2, "Correct objectStoreNames length");
ok(db.objectStoreNames.contains("newlyCreated"), "Has correct objectStore");
ok(db.objectStoreNames.contains("foo"), "Has correct objectStore");
is(createdObjectStore.indexNames.length, 1, "Correct indexNames length");
ok(createdObjectStore.indexNames.contains("newIndex"), "Has correct index");
is(objectStore.indexNames.length, 2, "Correct indexNames length");
ok(objectStore.indexNames.contains("dontDeleteMe"), "Has correct index");
ok(objectStore.indexNames.contains("newIndex"), "Has correct index");
// ConstraintError is expected in window.onerror.
objectStore.add({}, 1);
trans.onabort = grabEventAndContinueHandler;
event = yield undefined;
// Test that the world has been restored.
is(db.version, "1", "Correct version");
is(db.objectStoreNames.length, 2, "Correct objectStoreNames length");
ok(db.objectStoreNames.contains("foo"), "Has correct objectStore");
ok(db.objectStoreNames.contains("bar"), "Has correct objectStore");
is(objectStore.indexNames.length, 2, "Correct indexNames length");
ok(objectStore.indexNames.contains("dontDeleteMe"), "Has correct index");
ok(objectStore.indexNames.contains("baz"), "Has correct index");
is(createdObjectStore.indexNames.length, 0, "Correct indexNames length");
is(deletedObjectStore.indexNames.length, 1, "Correct indexNames length");
ok(deletedObjectStore.indexNames.contains("foo"), "Has correct index");
request.onerror = grabEventAndContinueHandler;
event = yield undefined;
is(event.type, "error", "Got request error event");
is(event.target.error.name, "AbortError", "Right error");
// Skip the verification of ConstraintError in window.onerror.
event.preventDefault();
finishTest();
yield undefined;
}
</script>
</head>
<body onload="testGenerator.next();"></body>
</html>
|