|
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 // Blob constructor is not implemented outside of windows yet (Bug 827723). |
|
11 if (!this.window) { |
|
12 finishTest(); |
|
13 yield undefined; |
|
14 } |
|
15 |
|
16 const name = this.window ? window.location.pathname : "Splendid Test"; |
|
17 |
|
18 const objectStoreName = "Things"; |
|
19 |
|
20 const blob1 = new Blob(["foo", "bar"], { type: "text/plain" }); |
|
21 const blob2 = new Blob(["foobazybar"], { type: "text/plain" }); |
|
22 const blob3 = new Blob(["2"], { type: "bogus/" }); |
|
23 const str = "The Book of Mozilla"; |
|
24 str.type = blob1; |
|
25 const arr = [1, 2, 3, 4, 5]; |
|
26 |
|
27 const objectStoreData = [ |
|
28 { key: "1", value: blob1}, |
|
29 { key: "2", value: blob2}, |
|
30 { key: "3", value: blob3}, |
|
31 { key: "4", value: str}, |
|
32 { key: "5", value: arr}, |
|
33 ]; |
|
34 |
|
35 const indexData = [ |
|
36 { name: "type", keyPath: "type", options: { } }, |
|
37 { name: "length", keyPath: "length", options: { unique: true } } |
|
38 ]; |
|
39 |
|
40 const objectStoreDataTypeSort = [ |
|
41 { key: "3", value: blob3}, |
|
42 { key: "1", value: blob1}, |
|
43 { key: "2", value: blob2}, |
|
44 ]; |
|
45 |
|
46 const objectStoreDataLengthSort = [ |
|
47 { key: "5", value: arr}, |
|
48 //{ key: "4", value: str}, |
|
49 ]; |
|
50 |
|
51 let request = indexedDB.open(name, 1); |
|
52 request.onerror = errorHandler; |
|
53 request.onupgradeneeded = grabEventAndContinueHandler; |
|
54 request.onsuccess = grabEventAndContinueHandler; |
|
55 let event = yield undefined; |
|
56 let db = event.target.result; |
|
57 |
|
58 let objectStore = db.createObjectStore(objectStoreName, { keyPath: null }); |
|
59 |
|
60 // First, add all our data to the object store. |
|
61 let addedData = 0; |
|
62 for (let i in objectStoreData) { |
|
63 request = objectStore.add(objectStoreData[i].value, |
|
64 objectStoreData[i].key); |
|
65 request.onerror = errorHandler; |
|
66 request.onsuccess = function(event) { |
|
67 if (++addedData == objectStoreData.length) { |
|
68 testGenerator.send(event); |
|
69 } |
|
70 } |
|
71 } |
|
72 event = yield undefined; |
|
73 // Now create the indexes. |
|
74 for (let i in indexData) { |
|
75 objectStore.createIndex(indexData[i].name, indexData[i].keyPath, |
|
76 indexData[i].options); |
|
77 } |
|
78 is(objectStore.indexNames.length, indexData.length, "Good index count"); |
|
79 yield undefined; |
|
80 objectStore = db.transaction(objectStoreName) |
|
81 .objectStore(objectStoreName); |
|
82 |
|
83 // Check global properties to make sure they are correct. |
|
84 is(objectStore.indexNames.length, indexData.length, "Good index count"); |
|
85 for (let i in indexData) { |
|
86 let found = false; |
|
87 for (let j = 0; j < objectStore.indexNames.length; j++) { |
|
88 if (objectStore.indexNames.item(j) == indexData[i].name) { |
|
89 found = true; |
|
90 break; |
|
91 } |
|
92 } |
|
93 is(found, true, "objectStore has our index"); |
|
94 let index = objectStore.index(indexData[i].name); |
|
95 is(index.name, indexData[i].name, "Correct name"); |
|
96 is(index.storeName, objectStore.name, "Correct store name"); |
|
97 is(index.keyPath, indexData[i].keyPath, "Correct keyPath"); |
|
98 is(index.unique, indexData[i].options.unique ? true : false, |
|
99 "Correct unique value"); |
|
100 } |
|
101 |
|
102 ok(true, "Test group 1"); |
|
103 |
|
104 let keyIndex = 0; |
|
105 |
|
106 request = objectStore.index("type").openKeyCursor(); |
|
107 request.onerror = errorHandler; |
|
108 request.onsuccess = function (event) { |
|
109 let cursor = event.target.result; |
|
110 if (cursor) { |
|
111 is(cursor.key, objectStoreDataTypeSort[keyIndex].value.type, |
|
112 "Correct key"); |
|
113 is(cursor.primaryKey, objectStoreDataTypeSort[keyIndex].key, |
|
114 "Correct primary key"); |
|
115 ok(!("value" in cursor), "No value"); |
|
116 |
|
117 cursor.continue(); |
|
118 |
|
119 is(cursor.key, objectStoreDataTypeSort[keyIndex].value.type, |
|
120 "Correct key"); |
|
121 is(cursor.primaryKey, objectStoreDataTypeSort[keyIndex].key, |
|
122 "Correct value"); |
|
123 ok(!("value" in cursor), "No value"); |
|
124 |
|
125 keyIndex++; |
|
126 } |
|
127 else { |
|
128 testGenerator.next(); |
|
129 } |
|
130 } |
|
131 yield undefined; |
|
132 |
|
133 is(keyIndex, objectStoreDataTypeSort.length, "Saw all the expected keys"); |
|
134 |
|
135 ok(true, "Test group 2"); |
|
136 |
|
137 keyIndex = 0; |
|
138 |
|
139 request = objectStore.index("length").openKeyCursor(null, "next"); |
|
140 request.onerror = errorHandler; |
|
141 request.onsuccess = function (event) { |
|
142 let cursor = event.target.result; |
|
143 if (cursor) { |
|
144 is(cursor.key, objectStoreDataLengthSort[keyIndex].value.length, |
|
145 "Correct key"); |
|
146 is(cursor.primaryKey, objectStoreDataLengthSort[keyIndex].key, |
|
147 "Correct value"); |
|
148 |
|
149 cursor.continue(); |
|
150 |
|
151 is(cursor.key, objectStoreDataLengthSort[keyIndex].value.length, |
|
152 "Correct key"); |
|
153 is(cursor.primaryKey, objectStoreDataLengthSort[keyIndex].key, |
|
154 "Correct value"); |
|
155 |
|
156 keyIndex++; |
|
157 } |
|
158 else { |
|
159 testGenerator.next(); |
|
160 } |
|
161 } |
|
162 yield undefined; |
|
163 |
|
164 is(keyIndex, objectStoreDataLengthSort.length, "Saw all the expected keys"); |
|
165 |
|
166 finishTest(); |
|
167 yield undefined; |
|
168 } |