summaryrefslogtreecommitdiffstats
path: root/dom/indexedDB/test/unit/test_autoIncrement.js
blob: f2ea0982232ac0fb624212acde6800ba80602825 (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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/**
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

var disableWorkerTest = "Need to implement a gc() function for worker tests";

if (!this.window) {
  this.runTest = function() {
    todo(false, "Test disabled in xpcshell test suite for now");
    finishTest();
  }
}

var testGenerator = testSteps();

function genCheck(key, value, test, options) {
  return function(event) {
    is(JSON.stringify(event.target.result), JSON.stringify(key),
       "correct returned key in " + test);
    if (options && options.store) {
      is(event.target.source, options.store, "correct store in " + test);
    }
    if (options && options.trans) {
      is(event.target.transaction, options.trans, "correct transaction in " + test);
    }

    event.target.source.get(key).onsuccess = function(event) {
      is(JSON.stringify(event.target.result), JSON.stringify(value),
         "correct stored value in " + test);
      continueToNextStepSync();
    }
  }
}

function testSteps()
{
  const dbname = this.window ? window.location.pathname : "Splendid Test";
  const RW = "readwrite";
  let c1 = 1;
  let c2 = 1;

  let openRequest = indexedDB.open(dbname, 1);
  openRequest.onerror = errorHandler;
  openRequest.onupgradeneeded = grabEventAndContinueHandler;
  openRequest.onsuccess = unexpectedSuccessHandler;
  let event = yield undefined;
  let db = event.target.result;
  let trans = event.target.transaction;

  // Create test stores
  let store1 = db.createObjectStore("store1", { autoIncrement: true });
  let store2 = db.createObjectStore("store2", { autoIncrement: true, keyPath: "id" });
  let store3 = db.createObjectStore("store3", { autoIncrement: false });
  is(store1.autoIncrement, true, "store1 .autoIncrement");
  is(store2.autoIncrement, true, "store2 .autoIncrement");
  is(store3.autoIncrement, false, "store3 .autoIncrement");

  store1.createIndex("unique1", "unique", { unique: true });
  store2.createIndex("unique1", "unique", { unique: true });

  // Test simple inserts
  let test = " for test simple insert"
  store1.add({ foo: "value1" }).onsuccess =
    genCheck(c1++, { foo: "value1" }, "first" + test);
  store1.add({ foo: "value2" }).onsuccess =
    genCheck(c1++, { foo: "value2" }, "second" + test);

  yield undefined;
  yield undefined;

  store2.put({ bar: "value1" }).onsuccess =
    genCheck(c2, { bar: "value1", id: c2 }, "first in store2" + test,
             { store: store2 });
  c2++;
  store1.put({ foo: "value3" }).onsuccess =
    genCheck(c1++, { foo: "value3" }, "third" + test,
             { store: store1 });

  yield undefined;
  yield undefined;

  store2.get(IDBKeyRange.lowerBound(c2)).onsuccess = grabEventAndContinueHandler;
  event = yield undefined;
  is(event.target.result, undefined, "no such value" + test);

  // Close version_change transaction
  openRequest.onsuccess = grabEventAndContinueHandler;
  event = yield undefined;

  is(event.target, openRequest, "succeeded to open" + test);
  is(event.type, "success", "succeeded to open" + test);

  // Test inserting explicit keys
  test = " for test explicit keys";
  trans = db.transaction("store1", RW);
  trans.objectStore("store1").add({ explicit: 1 }, 100).onsuccess =
    genCheck(100, { explicit: 1 }, "first" + test);
  c1 = 101;
  trans = db.transaction("store1", RW);
  trans.objectStore("store1").add({ explicit: 2 }).onsuccess =
    genCheck(c1++, { explicit: 2 }, "second" + test);
  yield undefined; yield undefined;

  trans = db.transaction("store1", RW);
  trans.objectStore("store1").add({ explicit: 3 }, 200).onsuccess =
    genCheck(200, { explicit: 3 }, "third" + test);
  c1 = 201;
  trans.objectStore("store1").add({ explicit: 4 }).onsuccess =
    genCheck(c1++, { explicit: 4 }, "fourth" + test);
  yield undefined; yield undefined;

  trans = db.transaction("store1", RW);
  trans.objectStore("store1").add({ explicit: 5 }, 150).onsuccess =
    genCheck(150, { explicit: 5 }, "fifth" + test);
  yield undefined;
  trans.objectStore("store1").add({ explicit: 6 }).onsuccess =
    genCheck(c1++, { explicit: 6 }, "sixth" + test);
  yield undefined;

  trans = db.transaction("store1", RW);
  trans.objectStore("store1").add({ explicit: 7 }, "key").onsuccess =
    genCheck("key", { explicit: 7 }, "seventh" + test);
  yield undefined;
  trans.objectStore("store1").add({ explicit: 8 }).onsuccess =
    genCheck(c1++, { explicit: 8 }, "eighth" + test);
  yield undefined;

  trans = db.transaction("store1", RW);
  trans.objectStore("store1").add({ explicit: 7 }, [100000]).onsuccess =
    genCheck([100000], { explicit: 7 }, "seventh" + test);
  yield undefined;
  trans.objectStore("store1").add({ explicit: 8 }).onsuccess =
    genCheck(c1++, { explicit: 8 }, "eighth" + test);
  yield undefined;

  trans = db.transaction("store1", RW);
  trans.objectStore("store1").add({ explicit: 9 }, -100000).onsuccess =
    genCheck(-100000, { explicit: 9 }, "ninth" + test);
  yield undefined;
  trans.objectStore("store1").add({ explicit: 10 }).onsuccess =
    genCheck(c1++, { explicit: 10 }, "tenth" + test);
  yield undefined;


  trans = db.transaction("store2", RW);
  trans.objectStore("store2").add({ explicit2: 1, id: 300 }).onsuccess =
    genCheck(300, { explicit2: 1, id: 300 }, "first store2" + test);
  c2 = 301;
  trans = db.transaction("store2", RW);
  trans.objectStore("store2").add({ explicit2: 2 }).onsuccess =
    genCheck(c2, { explicit2: 2, id: c2 }, "second store2" + test);
  c2++;
  yield undefined; yield undefined;

  trans = db.transaction("store2", RW);
  trans.objectStore("store2").add({ explicit2: 3, id: 400 }).onsuccess =
    genCheck(400, { explicit2: 3, id: 400 }, "third store2" + test);
  c2 = 401;
  trans.objectStore("store2").add({ explicit2: 4 }).onsuccess =
    genCheck(c2, { explicit2: 4, id: c2 }, "fourth store2" + test);
  c2++;
  yield undefined; yield undefined;

  trans = db.transaction("store2", RW);
  trans.objectStore("store2").add({ explicit: 5, id: 150 }).onsuccess =
    genCheck(150, { explicit: 5, id: 150 }, "fifth store2" + test);
  yield undefined;
  trans.objectStore("store2").add({ explicit: 6 }).onsuccess =
    genCheck(c2, { explicit: 6, id: c2 }, "sixth store2" + test);
  c2++;
  yield undefined;

  trans = db.transaction("store2", RW);
  trans.objectStore("store2").add({ explicit: 7, id: "key" }).onsuccess =
    genCheck("key", { explicit: 7, id: "key" }, "seventh store2" + test);
  yield undefined;
  trans.objectStore("store2").add({ explicit: 8 }).onsuccess =
    genCheck(c2, { explicit: 8, id: c2 }, "eighth store2" + test);
  c2++;
  yield undefined;

  trans = db.transaction("store2", RW);
  trans.objectStore("store2").add({ explicit: 7, id: [100000] }).onsuccess =
    genCheck([100000], { explicit: 7, id: [100000] }, "seventh store2" + test);
  yield undefined;
  trans.objectStore("store2").add({ explicit: 8 }).onsuccess =
    genCheck(c2, { explicit: 8, id: c2 }, "eighth store2" + test);
  c2++;
  yield undefined;

  trans = db.transaction("store2", RW);
  trans.objectStore("store2").add({ explicit: 9, id: -100000 }).onsuccess =
    genCheck(-100000, { explicit: 9, id: -100000 }, "ninth store2" + test);
  yield undefined;
  trans.objectStore("store2").add({ explicit: 10 }).onsuccess =
    genCheck(c2, { explicit: 10, id: c2 }, "tenth store2" + test);
  c2++;
  yield undefined;


  // Test separate transactions doesn't generate overlapping numbers
  test = " for test non-overlapping counts";
  trans = db.transaction("store1", RW);
  trans2 = db.transaction("store1", RW);
  trans2.objectStore("store1").put({ over: 2 }).onsuccess =
    genCheck(c1 + 1, { over: 2 }, "first" + test,
             { trans: trans2 });
  trans.objectStore("store1").put({ over: 1 }).onsuccess =
    genCheck(c1, { over: 1 }, "second" + test,
             { trans: trans });
  c1 += 2;
  yield undefined; yield undefined;

  trans = db.transaction("store2", RW);
  trans2 = db.transaction("store2", RW);
  trans2.objectStore("store2").put({ over: 2 }).onsuccess =
    genCheck(c2 + 1, { over: 2, id: c2 + 1 }, "third" + test,
             { trans: trans2 });
  trans.objectStore("store2").put({ over: 1 }).onsuccess =
    genCheck(c2, { over: 1, id: c2 }, "fourth" + test,
             { trans: trans });
  c2 += 2;
  yield undefined; yield undefined;

  // Test that error inserts doesn't increase generator
  test = " for test error inserts";
  trans = db.transaction(["store1", "store2"], RW);
  trans.objectStore("store1").add({ unique: 1 }, -1);
  trans.objectStore("store2").add({ unique: 1, id: "unique" });

  trans.objectStore("store1").add({ error: 1, unique: 1 }).
    addEventListener("error", new ExpectError("ConstraintError", true));
  trans.objectStore("store1").add({ error: 2 }).onsuccess =
    genCheck(c1++, { error: 2 }, "first" + test);
  yield undefined; yield undefined;

  trans.objectStore("store2").add({ error: 3, unique: 1 }).
    addEventListener("error", new ExpectError("ConstraintError", true));
  trans.objectStore("store2").add({ error: 4 }).onsuccess =
    genCheck(c2, { error: 4, id: c2 }, "second" + test);
  c2++;
  yield undefined; yield undefined;

  trans.objectStore("store1").add({ error: 5, unique: 1 }, 100000).
    addEventListener("error", new ExpectError("ConstraintError", true));
  trans.objectStore("store1").add({ error: 6 }).onsuccess =
    genCheck(c1++, { error: 6 }, "third" + test);
  yield undefined; yield undefined;

  trans.objectStore("store2").add({ error: 7, unique: 1, id: 100000 }).
    addEventListener("error", new ExpectError("ConstraintError", true));
  trans.objectStore("store2").add({ error: 8 }).onsuccess =
    genCheck(c2, { error: 8, id: c2 }, "fourth" + test);
  c2++;
  yield undefined; yield undefined;

  // Test that aborts doesn't increase generator
  test = " for test aborted transaction";
  trans = db.transaction(["store1", "store2"], RW);
  trans.objectStore("store1").add({ abort: 1 }).onsuccess =
    genCheck(c1, { abort: 1 }, "first" + test);
  trans.objectStore("store2").put({ abort: 2 }).onsuccess =
    genCheck(c2, { abort: 2, id: c2 }, "second" + test);
  yield undefined; yield undefined;

  trans.objectStore("store1").add({ abort: 3 }, 500).onsuccess =
    genCheck(500, { abort: 3 }, "third" + test);
  trans.objectStore("store2").put({ abort: 4, id: 600 }).onsuccess =
    genCheck(600, { abort: 4, id: 600 }, "fourth" + test);
  yield undefined; yield undefined;

  trans.objectStore("store1").add({ abort: 5 }).onsuccess =
    genCheck(501, { abort: 5 }, "fifth" + test);
  trans.objectStore("store2").put({ abort: 6 }).onsuccess =
    genCheck(601, { abort: 6, id: 601 }, "sixth" + test);
  yield undefined; yield undefined;

  trans.abort();
  trans.onabort = grabEventAndContinueHandler;
  event = yield
  is(event.type, "abort", "transaction aborted");
  is(event.target, trans, "correct transaction aborted");

  trans = db.transaction(["store1", "store2"], RW);
  trans.objectStore("store1").add({ abort: 1 }).onsuccess =
    genCheck(c1++, { abort: 1 }, "re-first" + test);
  trans.objectStore("store2").put({ abort: 2 }).onsuccess =
    genCheck(c2, { abort: 2, id: c2 }, "re-second" + test);
  c2++;
  yield undefined; yield undefined;

  // Test that delete doesn't decrease generator
  test = " for test delete items"
  trans = db.transaction(["store1", "store2"], RW);
  trans.objectStore("store1").add({ delete: 1 }).onsuccess =
    genCheck(c1++, { delete: 1 }, "first" + test);
  trans.objectStore("store2").put({ delete: 2 }).onsuccess =
    genCheck(c2, { delete: 2, id: c2 }, "second" + test);
  c2++;
  yield undefined; yield undefined;

  trans.objectStore("store1").delete(c1 - 1).onsuccess =
    grabEventAndContinueHandler;
  trans.objectStore("store2").delete(c2 - 1).onsuccess =
    grabEventAndContinueHandler;
  yield undefined; yield undefined;

  trans.objectStore("store1").add({ delete: 3 }).onsuccess =
    genCheck(c1++, { delete: 3 }, "first" + test);
  trans.objectStore("store2").put({ delete: 4 }).onsuccess =
    genCheck(c2, { delete: 4, id: c2 }, "second" + test);
  c2++;
  yield undefined; yield undefined;

  trans.objectStore("store1").delete(c1 - 1).onsuccess =
    grabEventAndContinueHandler;
  trans.objectStore("store2").delete(c2 - 1).onsuccess =
    grabEventAndContinueHandler;
  yield undefined; yield undefined;

  trans = db.transaction(["store1", "store2"], RW);
  trans.objectStore("store1").add({ delete: 5 }).onsuccess =
    genCheck(c1++, { delete: 5 }, "first" + test);
  trans.objectStore("store2").put({ delete: 6 }).onsuccess =
    genCheck(c2, { delete: 6, id: c2 }, "second" + test);
  c2++;
  yield undefined; yield undefined;

  // Test that clears doesn't decrease generator
  test = " for test clear stores";
  trans = db.transaction(["store1", "store2"], RW);
  trans.objectStore("store1").add({ clear: 1 }).onsuccess =
    genCheck(c1++, { clear: 1 }, "first" + test);
  trans.objectStore("store2").put({ clear: 2 }).onsuccess =
    genCheck(c2, { clear: 2, id: c2 }, "second" + test);
  c2++;
  yield undefined; yield undefined;

  trans.objectStore("store1").clear().onsuccess =
    grabEventAndContinueHandler;
  trans.objectStore("store2").clear().onsuccess =
    grabEventAndContinueHandler;
  yield undefined; yield undefined;

  trans.objectStore("store1").add({ clear: 3 }).onsuccess =
    genCheck(c1++, { clear: 3 }, "third" + test);
  trans.objectStore("store2").put({ clear: 4 }).onsuccess =
    genCheck(c2, { clear: 4, id: c2 }, "forth" + test);
  c2++;
  yield undefined; yield undefined;

  trans.objectStore("store1").clear().onsuccess =
    grabEventAndContinueHandler;
  trans.objectStore("store2").clear().onsuccess =
    grabEventAndContinueHandler;
  yield undefined; yield undefined;

  trans = db.transaction(["store1", "store2"], RW);
  trans.objectStore("store1").add({ clear: 5 }).onsuccess =
    genCheck(c1++, { clear: 5 }, "fifth" + test);
  trans.objectStore("store2").put({ clear: 6 }).onsuccess =
    genCheck(c2, { clear: 6, id: c2 }, "sixth" + test);
  c2++;
  yield undefined; yield undefined;


  // Test that close/reopen doesn't decrease generator
  test = " for test clear stores";
  trans = db.transaction(["store1", "store2"], RW);
  trans.objectStore("store1").clear().onsuccess =
    grabEventAndContinueHandler;
  trans.objectStore("store2").clear().onsuccess =
    grabEventAndContinueHandler;
  yield undefined; yield undefined;
  db.close();

  gc();

  openRequest = indexedDB.open(dbname, 2);
  openRequest.onerror = errorHandler;
  openRequest.onupgradeneeded = grabEventAndContinueHandler;
  openRequest.onsuccess = unexpectedSuccessHandler;
  event = yield undefined;
  db = event.target.result;
  trans = event.target.transaction;

  trans.objectStore("store1").add({ reopen: 1 }).onsuccess =
    genCheck(c1++, { reopen: 1 }, "first" + test);
  trans.objectStore("store2").put({ reopen: 2 }).onsuccess =
    genCheck(c2, { reopen: 2, id: c2 }, "second" + test);
  c2++;
  yield undefined; yield undefined;

  openRequest.onsuccess = grabEventAndContinueHandler;
  yield undefined;

  finishTest();
  yield undefined;
}