2 * Any copyright is dedicated to the Public Domain.
3 * http://creativecommons.org/publicdomain/zero/1.0/
6 var testGenerator = testSteps();
8 function* testSteps() {
9 // Blob constructor is not implemented outside of windows yet (Bug 827723).
15 const name = this.window ? window.location.pathname : "Splendid Test";
17 const objectStoreName = "Things";
19 const blob1 = new Blob(["foo", "bar"], { type: "text/plain" });
20 const blob2 = new Blob(["foobazybar"], { type: "text/plain" });
21 const blob3 = new Blob(["2"], { type: "bogus/" });
22 const str = "The Book of Mozilla";
24 const arr = [1, 2, 3, 4, 5];
26 const objectStoreData = [
27 { key: "1", value: blob1 },
28 { key: "2", value: blob2 },
29 { key: "3", value: blob3 },
30 { key: "4", value: str },
31 { key: "5", value: arr },
35 { name: "type", keyPath: "type", options: {} },
36 { name: "length", keyPath: "length", options: { unique: true } },
39 const objectStoreDataTypeSort = [
40 { key: "3", value: blob3 },
41 { key: "1", value: blob1 },
42 { key: "2", value: blob2 },
45 const objectStoreDataLengthSort = [
46 { key: "5", value: arr },
47 { key: "4", value: str },
50 let request = indexedDB.open(name, 1);
51 request.onerror = errorHandler;
52 request.onupgradeneeded = grabEventAndContinueHandler;
53 request.onsuccess = grabEventAndContinueHandler;
54 let event = yield undefined;
55 let db = event.target.result;
57 let objectStore = db.createObjectStore(objectStoreName, { keyPath: null });
59 // First, add all our data to the object store.
61 for (let i in objectStoreData) {
62 request = objectStore.add(objectStoreData[i].value, objectStoreData[i].key);
63 request.onerror = errorHandler;
64 request.onsuccess = function(event) {
65 if (++addedData == objectStoreData.length) {
66 testGenerator.next(event);
70 event = yield undefined;
71 // Now create the indexes.
72 for (let i in indexData) {
73 objectStore.createIndex(
79 is(objectStore.indexNames.length, indexData.length, "Good index count");
81 objectStore = db.transaction(objectStoreName).objectStore(objectStoreName);
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) {
87 for (let j = 0; j < objectStore.indexNames.length; j++) {
88 if (objectStore.indexNames.item(j) == indexData[i].name) {
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.objectStore.name, objectStore.name, "Correct store name");
97 is(index.keyPath, indexData[i].keyPath, "Correct keyPath");
98 is(index.unique, !!indexData[i].options.unique, "Correct unique value");
101 ok(true, "Test group 1");
105 request = objectStore.index("type").openKeyCursor();
106 request.onerror = errorHandler;
107 request.onsuccess = function(event) {
108 let cursor = event.target.result;
112 objectStoreDataTypeSort[keyIndex].value.type,
117 objectStoreDataTypeSort[keyIndex].key,
118 "Correct primary key"
120 ok(!("value" in cursor), "No value");
126 objectStoreDataTypeSort[keyIndex].value.type,
131 objectStoreDataTypeSort[keyIndex].key,
134 ok(!("value" in cursor), "No value");
138 testGenerator.next();
143 is(keyIndex, objectStoreDataTypeSort.length, "Saw all the expected keys");
145 ok(true, "Test group 2");
149 request = objectStore.index("length").openKeyCursor(null, "next");
150 request.onerror = errorHandler;
151 request.onsuccess = function(event) {
152 let cursor = event.target.result;
156 objectStoreDataLengthSort[keyIndex].value.length,
161 objectStoreDataLengthSort[keyIndex].key,
169 objectStoreDataLengthSort[keyIndex].value.length,
174 objectStoreDataLengthSort[keyIndex].key,
180 testGenerator.next();
185 is(keyIndex, objectStoreDataLengthSort.length, "Saw all the expected keys");