dom/indexedDB/test/unit/test_indexes_funny_things.js

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

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

mercurial