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

mercurial