| |
1 /** |
| |
2 * Any copyright is dedicated to the Public Domain. |
| |
3 * http://creativecommons.org/publicdomain/zero/1.0/ |
| |
4 */ |
| |
5 |
| |
6 var testGenerator = testSteps(); |
| |
7 |
| |
8 function testSteps() |
| |
9 { |
| |
10 const objectStoreData = [ |
| |
11 { name: "", options: { keyPath: "id", autoIncrement: true } }, |
| |
12 { name: null, options: { keyPath: "ss" } }, |
| |
13 { name: undefined, options: { } }, |
| |
14 { name: "4", options: { autoIncrement: true } }, |
| |
15 ]; |
| |
16 |
| |
17 const indexData = [ |
| |
18 { name: "", keyPath: "name", options: { unique: true } }, |
| |
19 { name: null, keyPath: "height", options: { } } |
| |
20 ]; |
| |
21 |
| |
22 const data = [ |
| |
23 { ss: "237-23-7732", name: "Ann", height: 60 }, |
| |
24 { ss: "237-23-7733", name: "Bob", height: 65 } |
| |
25 ]; |
| |
26 |
| |
27 let request = indexedDB.open(this.window ? window.location.pathname : "Splendid Test", 1); |
| |
28 request.onerror = errorHandler; |
| |
29 request.onupgradeneeded = grabEventAndContinueHandler; |
| |
30 let event = yield undefined; |
| |
31 |
| |
32 let db = event.target.result; |
| |
33 db.onerror = errorHandler; |
| |
34 |
| |
35 event.target.onsuccess = continueToNextStep; |
| |
36 |
| |
37 for (let objectStoreIndex in objectStoreData) { |
| |
38 const objectStoreInfo = objectStoreData[objectStoreIndex]; |
| |
39 let objectStore = db.createObjectStore(objectStoreInfo.name, |
| |
40 objectStoreInfo.options); |
| |
41 for (let indexIndex in indexData) { |
| |
42 const indexInfo = indexData[indexIndex]; |
| |
43 let index = objectStore.createIndex(indexInfo.name, |
| |
44 indexInfo.keyPath, |
| |
45 indexInfo.options); |
| |
46 } |
| |
47 } |
| |
48 yield undefined; |
| |
49 |
| |
50 ok(true, "Initial setup"); |
| |
51 |
| |
52 for (let objectStoreIndex in objectStoreData) { |
| |
53 const info = objectStoreData[objectStoreIndex]; |
| |
54 |
| |
55 for (let indexIndex in indexData) { |
| |
56 const objectStoreName = objectStoreData[objectStoreIndex].name; |
| |
57 const indexName = indexData[indexIndex].name; |
| |
58 |
| |
59 let objectStore = |
| |
60 db.transaction(objectStoreName, "readwrite") |
| |
61 .objectStore(objectStoreName); |
| |
62 ok(true, "Got objectStore " + objectStoreName); |
| |
63 |
| |
64 for (let dataIndex in data) { |
| |
65 const obj = data[dataIndex]; |
| |
66 let key; |
| |
67 if (!info.options.keyPath && !info.options.autoIncrement) { |
| |
68 key = obj.ss; |
| |
69 } |
| |
70 objectStore.add(obj, key); |
| |
71 } |
| |
72 |
| |
73 let index = objectStore.index(indexName); |
| |
74 ok(true, "Got index " + indexName); |
| |
75 |
| |
76 let keyIndex = 0; |
| |
77 |
| |
78 index.openCursor().onsuccess = function(event) { |
| |
79 let cursor = event.target.result; |
| |
80 if (!cursor) { |
| |
81 continueToNextStep(); |
| |
82 return; |
| |
83 } |
| |
84 |
| |
85 is(cursor.key, data[keyIndex][indexData[indexIndex].keyPath], |
| |
86 "Good key"); |
| |
87 is(cursor.value.ss, data[keyIndex].ss, "Correct ss"); |
| |
88 is(cursor.value.name, data[keyIndex].name, "Correct name"); |
| |
89 is(cursor.value.height, data[keyIndex].height, "Correct height"); |
| |
90 |
| |
91 if (!keyIndex) { |
| |
92 let obj = cursor.value; |
| |
93 obj.updated = true; |
| |
94 |
| |
95 cursor.update(obj).onsuccess = function(event) { |
| |
96 ok(true, "Object updated"); |
| |
97 cursor.continue(); |
| |
98 keyIndex++ |
| |
99 } |
| |
100 return; |
| |
101 } |
| |
102 |
| |
103 cursor.delete().onsuccess = function(event) { |
| |
104 ok(true, "Object deleted"); |
| |
105 cursor.continue(); |
| |
106 keyIndex++ |
| |
107 } |
| |
108 }; |
| |
109 yield undefined; |
| |
110 |
| |
111 is(keyIndex, 2, "Saw all the items"); |
| |
112 |
| |
113 keyIndex = 0; |
| |
114 |
| |
115 db.transaction(objectStoreName).objectStore(objectStoreName) |
| |
116 .openCursor() |
| |
117 .onsuccess = function(event) { |
| |
118 let cursor = event.target.result; |
| |
119 if (!cursor) { |
| |
120 continueToNextStep(); |
| |
121 return; |
| |
122 } |
| |
123 |
| |
124 is(cursor.value.ss, data[keyIndex].ss, "Correct ss"); |
| |
125 is(cursor.value.name, data[keyIndex].name, "Correct name"); |
| |
126 is(cursor.value.height, data[keyIndex].height, "Correct height"); |
| |
127 is(cursor.value.updated, true, "Correct updated flag"); |
| |
128 |
| |
129 cursor.continue(); |
| |
130 keyIndex++; |
| |
131 }; |
| |
132 yield undefined; |
| |
133 |
| |
134 is(keyIndex, 1, "Saw all the items"); |
| |
135 |
| |
136 db.transaction(objectStoreName, "readwrite") |
| |
137 .objectStore(objectStoreName).clear() |
| |
138 .onsuccess = continueToNextStep; |
| |
139 yield undefined; |
| |
140 |
| |
141 objectStore = index = null; // Bug 943409 workaround. |
| |
142 } |
| |
143 } |
| |
144 |
| |
145 finishTest(); |
| |
146 yield undefined; |
| |
147 } |