dom/indexedDB/test/unit/test_index_update_delete.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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 let name = this.window ? window.location.pathname : "Splendid Test";
michael@0 11 let request = indexedDB.open(name, 1);
michael@0 12 request.onerror = errorHandler;
michael@0 13 request.onupgradeneeded = grabEventAndContinueHandler;
michael@0 14 request.onsuccess = grabEventAndContinueHandler;
michael@0 15
michael@0 16 let event = yield undefined;
michael@0 17
michael@0 18 let db = event.target.result;
michael@0 19 db.onerror = errorHandler;
michael@0 20
michael@0 21 for each (let autoIncrement in [false, true]) {
michael@0 22 let objectStore =
michael@0 23 db.createObjectStore(autoIncrement, { keyPath: "id",
michael@0 24 autoIncrement: autoIncrement });
michael@0 25
michael@0 26 for (let i = 0; i < 10; i++) {
michael@0 27 objectStore.add({ id: i, index: i });
michael@0 28 }
michael@0 29
michael@0 30 for each (let unique in [false, true]) {
michael@0 31 objectStore.createIndex(unique, "index", { unique: unique });
michael@0 32 }
michael@0 33
michael@0 34 for (let i = 10; i < 20; i++) {
michael@0 35 objectStore.add({ id: i, index: i });
michael@0 36 }
michael@0 37 }
michael@0 38
michael@0 39 event = yield undefined;
michael@0 40 is(event.type, "success", "expect a success event");
michael@0 41
michael@0 42 for each (let autoIncrement in [false, true]) {
michael@0 43 let objectStore = db.transaction(autoIncrement)
michael@0 44 .objectStore(autoIncrement);
michael@0 45
michael@0 46 objectStore.count().onsuccess = grabEventAndContinueHandler;
michael@0 47 let event = yield undefined;
michael@0 48
michael@0 49 is(event.target.result, 20, "Correct number of entries in objectStore");
michael@0 50
michael@0 51 let objectStoreCount = event.target.result;
michael@0 52 let indexCount = event.target.result;
michael@0 53
michael@0 54 for each (let unique in [false, true]) {
michael@0 55 let index = db.transaction(autoIncrement, "readwrite")
michael@0 56 .objectStore(autoIncrement)
michael@0 57 .index(unique);
michael@0 58
michael@0 59 index.count().onsuccess = grabEventAndContinueHandler;
michael@0 60 let event = yield undefined;
michael@0 61
michael@0 62 is(event.target.result, indexCount,
michael@0 63 "Correct number of entries in index");
michael@0 64
michael@0 65 let modifiedEntry = unique ? 5 : 10;
michael@0 66 let keyRange = IDBKeyRange.only(modifiedEntry);
michael@0 67
michael@0 68 let sawEntry = false;
michael@0 69 index.openCursor(keyRange).onsuccess = function(event) {
michael@0 70 let cursor = event.target.result;
michael@0 71 if (cursor) {
michael@0 72 sawEntry = true;
michael@0 73 is(cursor.key, modifiedEntry, "Correct key");
michael@0 74
michael@0 75 cursor.value.index = unique ? 30 : 35;
michael@0 76 cursor.update(cursor.value).onsuccess = function(event) {
michael@0 77 cursor.continue();
michael@0 78 }
michael@0 79 }
michael@0 80 else {
michael@0 81 continueToNextStep();
michael@0 82 }
michael@0 83 }
michael@0 84 yield undefined;
michael@0 85
michael@0 86 is(sawEntry, true, "Saw entry for key value " + modifiedEntry);
michael@0 87
michael@0 88 // Recount index. Shouldn't change.
michael@0 89 index = db.transaction(autoIncrement, "readwrite")
michael@0 90 .objectStore(autoIncrement)
michael@0 91 .index(unique);
michael@0 92
michael@0 93 index.count().onsuccess = grabEventAndContinueHandler;
michael@0 94 event = yield undefined;
michael@0 95
michael@0 96 is(event.target.result, indexCount,
michael@0 97 "Correct number of entries in index");
michael@0 98
michael@0 99 modifiedEntry = unique ? 30 : 35;
michael@0 100 keyRange = IDBKeyRange.only(modifiedEntry);
michael@0 101
michael@0 102 sawEntry = false;
michael@0 103 index.openCursor(keyRange).onsuccess = function(event) {
michael@0 104 let cursor = event.target.result;
michael@0 105 if (cursor) {
michael@0 106 sawEntry = true;
michael@0 107 is(cursor.key, modifiedEntry, "Correct key");
michael@0 108
michael@0 109 delete cursor.value.index;
michael@0 110 cursor.update(cursor.value).onsuccess = function(event) {
michael@0 111 indexCount--;
michael@0 112 cursor.continue();
michael@0 113 }
michael@0 114 }
michael@0 115 else {
michael@0 116 continueToNextStep();
michael@0 117 }
michael@0 118 }
michael@0 119 yield undefined;
michael@0 120
michael@0 121 is(sawEntry, true, "Saw entry for key value " + modifiedEntry);
michael@0 122
michael@0 123 // Recount objectStore. Should be unchanged.
michael@0 124 objectStore = db.transaction(autoIncrement, "readwrite")
michael@0 125 .objectStore(autoIncrement);
michael@0 126
michael@0 127 objectStore.count().onsuccess = grabEventAndContinueHandler;
michael@0 128 event = yield undefined;
michael@0 129
michael@0 130 is(event.target.result, objectStoreCount,
michael@0 131 "Correct number of entries in objectStore");
michael@0 132
michael@0 133 // Recount index. Should be one item less.
michael@0 134 index = objectStore.index(unique);
michael@0 135
michael@0 136 index.count().onsuccess = grabEventAndContinueHandler;
michael@0 137 event = yield undefined;
michael@0 138
michael@0 139 is(event.target.result, indexCount,
michael@0 140 "Correct number of entries in index");
michael@0 141
michael@0 142 modifiedEntry = objectStoreCount - 1;
michael@0 143
michael@0 144 objectStore.delete(modifiedEntry).onsuccess =
michael@0 145 grabEventAndContinueHandler;
michael@0 146 event = yield undefined;
michael@0 147
michael@0 148 objectStoreCount--;
michael@0 149 indexCount--;
michael@0 150
michael@0 151 objectStore.count().onsuccess = grabEventAndContinueHandler;
michael@0 152 event = yield undefined;
michael@0 153
michael@0 154 is(event.target.result, objectStoreCount,
michael@0 155 "Correct number of entries in objectStore");
michael@0 156
michael@0 157 index.count().onsuccess = grabEventAndContinueHandler;
michael@0 158 event = yield undefined;
michael@0 159
michael@0 160 is(event.target.result, indexCount,
michael@0 161 "Correct number of entries in index");
michael@0 162
michael@0 163 index = event = null; // Bug 943409 workaround.
michael@0 164 }
michael@0 165 objectStore = event = null; // Bug 943409 workaround.
michael@0 166 }
michael@0 167
michael@0 168 finishTest();
michael@0 169 event = db = request = null; // Bug 943409 workaround.
michael@0 170 yield undefined;
michael@0 171 }

mercurial