Bug 1816170 - Disable perftest-on-autoland cron. r=aglavic
[gecko.git] / dom / indexedDB / test / unit / test_schema21upgrade.js
blobc38e2fb08ffc6bb7c695e5640b365f2dd3d373bc
1 /**
2  * Any copyright is dedicated to the Public Domain.
3  * http://creativecommons.org/publicdomain/zero/1.0/
4  */
6 /* exported testGenerator */
7 var testGenerator = testSteps();
9 function* testSteps() {
10   const testName = "schema21upgrade";
11   const testKeys = [
12     -1 / 0,
13     -1.7e308,
14     -10000,
15     -2,
16     -1.5,
17     -1,
18     -1.00001e-200,
19     -1e-200,
20     0,
21     1e-200,
22     1.00001e-200,
23     1,
24     2,
25     10000,
26     1.7e308,
27     1 / 0,
28     new Date("1750-01-02"),
29     new Date("1800-12-31T12:34:56.001Z"),
30     new Date(-1000),
31     new Date(-10),
32     new Date(-1),
33     new Date(0),
34     new Date(1),
35     new Date(2),
36     new Date(1000),
37     new Date("1971-01-01"),
38     new Date("1971-01-01T01:01:01Z"),
39     new Date("1971-01-01T01:01:01.001Z"),
40     new Date("1971-01-01T01:01:01.01Z"),
41     new Date("1971-01-01T01:01:01.1Z"),
42     new Date("1980-02-02"),
43     new Date("3333-03-19T03:33:33.333Z"),
44     "",
45     "\x00",
46     "\x00\x00",
47     "\x00\x01",
48     "\x01",
49     "\x02",
50     "\x03",
51     "\x04",
52     "\x07",
53     "\x08",
54     "\x0F",
55     "\x10",
56     "\x1F",
57     "\x20",
58     "01234",
59     "\x3F",
60     "\x40",
61     "A",
62     "A\x00",
63     "A1",
64     "ZZZZ",
65     "a",
66     "a\x00",
67     "aa",
68     "azz",
69     "}",
70     "\x7E",
71     "\x7F",
72     "\x80",
73     "\xFF",
74     "\u0100",
75     "\u01FF",
76     "\u0200",
77     "\u03FF",
78     "\u0400",
79     "\u07FF",
80     "\u0800",
81     "\u0FFF",
82     "\u1000",
83     "\u1FFF",
84     "\u2000",
85     "\u3FFF",
86     "\u4000",
87     "\u7FFF",
88     "\u8000",
89     "\uD800",
90     "\uD800a",
91     "\uD800\uDC01",
92     "\uDBFF",
93     "\uDC00",
94     "\uDFFF\uD800",
95     "\uFFFE",
96     "\uFFFF",
97     "\uFFFF\x00",
98     "\uFFFFZZZ",
99     [],
100     [-1 / 0],
101     [-1],
102     [0],
103     [1],
104     [1, "a"],
105     [1, []],
106     [1, [""]],
107     [2, 3],
108     [2, 3.0000000000001],
109     [12, [[]]],
110     [12, [[[]]]],
111     [12, [[[""]]]],
112     [12, [[["foo"]]]],
113     [12, [[[[[3]]]]]],
114     [12, [[[[[[3]]]]]]],
115     [12, [[[[[[3], [[[[[4.2]]]]]]]]]]],
116     [new Date(-1)],
117     [new Date(1)],
118     [""],
119     ["", [[]]],
120     ["", [[[]]]],
121     ["abc"],
122     ["abc", "def"],
123     ["abc\x00"],
124     ["abc\x00", "\x00\x01"],
125     ["abc\x00", "\x00def"],
126     ["abc\x00\x00def"],
127     ["x", [[]]],
128     ["x", [[[]]]],
129     [[]],
130     [[], "foo"],
131     [[], []],
132     [[[]]],
133     [[[]], []],
134     [[[]], [[]]],
135     [[[]], [[1]]],
136     [[[]], [[[]]]],
137     [[[1]]],
138     [[[[]], []]],
139   ];
140   const testString =
141     "abcdefghijklmnopqrstuvwxyz0123456789`~!@#$%^&*()-_+=,<.>/?\\|";
143   clearAllDatabases(continueToNextStepSync);
144   yield undefined;
146   info("Installing profile");
148   installPackagedProfile(testName + "_profile");
150   info("Opening database with no version");
152   let request = indexedDB.open(testName);
153   request.onerror = errorHandler;
154   request.onupgradeneeded = unexpectedSuccessHandler;
155   request.onsuccess = grabEventAndContinueHandler;
156   let event = yield undefined;
158   let db = event.target.result;
160   is(db.version, 1, "Correct db version");
162   let transaction = db.transaction(testName);
163   transaction.oncomplete = grabEventAndContinueHandler;
165   let objectStore = transaction.objectStore(testName);
166   let index = objectStore.index("uniqueIndex");
168   info("Starting 'uniqueIndex' cursor");
170   let keyIndex = 0;
171   index.openCursor().onsuccess = event => {
172     let cursor = event.target.result;
173     if (cursor) {
174       info(
175         "Comparing " +
176           JSON.stringify(cursor.primaryKey) +
177           " to " +
178           JSON.stringify(testKeys[cursor.key]) +
179           " [" +
180           cursor.key +
181           "]"
182       );
183       is(
184         indexedDB.cmp(cursor.primaryKey, testKeys[cursor.key]),
185         0,
186         "Keys compare equally via 'indexedDB.cmp'"
187       );
188       is(
189         compareKeys(cursor.primaryKey, testKeys[cursor.key]),
190         true,
191         "Keys compare equally via 'compareKeys'"
192       );
194       let indexProperty = cursor.value.index;
195       is(Array.isArray(indexProperty), true, "index property is Array");
196       is(indexProperty[0], cursor.key, "index property first item correct");
197       is(
198         indexProperty[1],
199         cursor.key + 1,
200         "index property second item correct"
201       );
203       is(cursor.key, keyIndex, "Cursor key property is correct");
205       is(cursor.value.testString, testString, "Test string compared equally");
207       keyIndex++;
208       cursor.continue();
209     }
210   };
211   yield undefined;
213   is(keyIndex, testKeys.length, "Saw all keys");
215   transaction = db.transaction(testName, "readwrite");
216   transaction.oncomplete = grabEventAndContinueHandler;
218   objectStore = transaction.objectStore(testName);
219   index = objectStore.index("index");
221   info("Getting all 'index' keys");
223   index.getAllKeys().onsuccess = grabEventAndContinueHandler;
224   event = yield undefined;
226   is(event.target.result.length, testKeys.length * 2, "Got all keys");
228   info("Starting objectStore cursor");
230   objectStore.openCursor().onsuccess = event => {
231     let cursor = event.target.result;
232     if (cursor) {
233       let value = cursor.value;
234       is(value.testString, testString, "Test string compared equally");
236       delete value.index;
237       cursor.update(value);
239       cursor.continue();
240     } else {
241       continueToNextStepSync();
242     }
243   };
244   yield undefined;
246   info("Getting all 'index' keys");
248   index.getAllKeys().onsuccess = grabEventAndContinueHandler;
249   event = yield undefined;
251   is(event.target.result.length, 0, "Removed all keys");
252   yield undefined;
254   db.close();
256   info("Opening database with new version");
258   request = indexedDB.open(testName, 2);
259   request.onerror = errorHandler;
260   request.onupgradeneeded = grabEventAndContinueHandler;
261   request.onsuccess = grabEventAndContinueHandler;
262   event = yield undefined;
264   info("Deleting indexes");
266   objectStore = event.target.transaction.objectStore(testName);
267   objectStore.deleteIndex("index");
268   objectStore.deleteIndex("uniqueIndex");
270   event = yield undefined;
272   db = event.target.result;
274   transaction = db.transaction(testName, "readwrite");
275   transaction.oncomplete = grabEventAndContinueHandler;
277   info("Starting objectStore cursor");
279   objectStore = transaction.objectStore(testName);
280   objectStore.openCursor().onsuccess = event => {
281     let cursor = event.target.result;
282     if (cursor) {
283       let value = cursor.value;
284       is(value.testString, testString, "Test string compared equally");
286       value.index = value.keyPath;
287       cursor.update(value);
289       cursor.continue();
290     }
291   };
292   event = yield undefined;
294   db.close();
296   info("Opening database with new version");
298   request = indexedDB.open(testName, 3);
299   request.onerror = errorHandler;
300   request.onupgradeneeded = grabEventAndContinueHandler;
301   request.onsuccess = grabEventAndContinueHandler;
302   event = yield undefined;
304   info("Creating indexes");
306   objectStore = event.target.transaction.objectStore(testName);
307   objectStore.createIndex("index", "index");
309   event = yield undefined;
311   db = event.target.result;
313   transaction = db.transaction(testName);
314   transaction.oncomplete = grabEventAndContinueHandler;
316   objectStore = transaction.objectStore(testName);
317   index = objectStore.index("index");
319   info("Starting 'index' cursor");
321   keyIndex = 0;
322   index.openCursor().onsuccess = event => {
323     let cursor = event.target.result;
324     if (cursor) {
325       is(
326         indexedDB.cmp(cursor.primaryKey, testKeys[keyIndex]),
327         0,
328         "Keys compare equally via 'indexedDB.cmp'"
329       );
330       is(
331         compareKeys(cursor.primaryKey, testKeys[keyIndex]),
332         true,
333         "Keys compare equally via 'compareKeys'"
334       );
335       is(
336         indexedDB.cmp(cursor.key, testKeys[keyIndex]),
337         0,
338         "Keys compare equally via 'indexedDB.cmp'"
339       );
340       is(
341         compareKeys(cursor.key, testKeys[keyIndex]),
342         true,
343         "Keys compare equally via 'compareKeys'"
344       );
346       let indexProperty = cursor.value.index;
347       is(
348         indexedDB.cmp(indexProperty, testKeys[keyIndex]),
349         0,
350         "Keys compare equally via 'indexedDB.cmp'"
351       );
352       is(
353         compareKeys(indexProperty, testKeys[keyIndex]),
354         true,
355         "Keys compare equally via 'compareKeys'"
356       );
358       is(cursor.value.testString, testString, "Test string compared equally");
360       keyIndex++;
361       cursor.continue();
362     }
363   };
364   yield undefined;
366   is(keyIndex, testKeys.length, "Added all keys again");
368   finishTest();
369   yield undefined;