michael@0: /** michael@0: * Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ michael@0: */ michael@0: michael@0: var testGenerator = testSteps(); michael@0: michael@0: function testSteps() michael@0: { michael@0: const objectStoreData = [ michael@0: // This one will be removed. michael@0: { ss: "237-23-7732", name: "Bob" }, michael@0: michael@0: // These will always be included. michael@0: { ss: "237-23-7733", name: "Ann" }, michael@0: { ss: "237-23-7734", name: "Ron" }, michael@0: { ss: "237-23-7735", name: "Sue" }, michael@0: { ss: "237-23-7736", name: "Joe" }, michael@0: michael@0: // This one will be added. michael@0: { ss: "237-23-7737", name: "Pat" } michael@0: ]; michael@0: michael@0: // Post-add and post-remove data ordered by name. michael@0: const objectStoreDataNameSort = [ 1, 4, 5, 2, 3 ]; michael@0: michael@0: let request = indexedDB.open(this.window ? window.location.pathname : "Splendid Test", 1); michael@0: request.onerror = errorHandler; michael@0: request.onupgradeneeded = grabEventAndContinueHandler; michael@0: let event = yield undefined; michael@0: michael@0: let db = event.target.result; michael@0: event.target.onsuccess = continueToNextStep; michael@0: michael@0: let objectStore = db.createObjectStore("foo", { keyPath: "ss" }); michael@0: objectStore.createIndex("name", "name", { unique: true }); michael@0: michael@0: for (let i = 0; i < objectStoreData.length - 1; i++) { michael@0: objectStore.add(objectStoreData[i]); michael@0: } michael@0: yield undefined; michael@0: michael@0: let count = 0; michael@0: michael@0: let sawAdded = false; michael@0: let sawRemoved = false; michael@0: michael@0: db.transaction("foo").objectStore("foo").openCursor().onsuccess = michael@0: function(event) { michael@0: event.target.transaction.oncomplete = continueToNextStep; michael@0: let cursor = event.target.result; michael@0: if (cursor) { michael@0: if (cursor.value.name == objectStoreData[0].name) { michael@0: sawRemoved = true; michael@0: } michael@0: if (cursor.value.name == michael@0: objectStoreData[objectStoreData.length - 1].name) { michael@0: sawAdded = true; michael@0: } michael@0: cursor.continue(); michael@0: count++; michael@0: } michael@0: }; michael@0: yield undefined; michael@0: michael@0: is(count, objectStoreData.length - 1, "Good initial count"); michael@0: is(sawAdded, false, "Didn't see item that is about to be added"); michael@0: is(sawRemoved, true, "Saw item that is about to be removed"); michael@0: michael@0: count = 0; michael@0: sawAdded = false; michael@0: sawRemoved = false; michael@0: michael@0: db.transaction("foo", "readwrite").objectStore("foo") michael@0: .index("name").openCursor().onsuccess = function(event) { michael@0: event.target.transaction.oncomplete = continueToNextStep; michael@0: let cursor = event.target.result; michael@0: if (cursor) { michael@0: if (cursor.value.name == objectStoreData[0].name) { michael@0: sawRemoved = true; michael@0: } michael@0: if (cursor.value.name == michael@0: objectStoreData[objectStoreData.length - 1].name) { michael@0: sawAdded = true; michael@0: } michael@0: michael@0: is(cursor.value.name, michael@0: objectStoreData[objectStoreDataNameSort[count++]].name, michael@0: "Correct name"); michael@0: michael@0: if (count == 1) { michael@0: let objectStore = event.target.transaction.objectStore("foo"); michael@0: objectStore.delete(objectStoreData[0].ss) michael@0: .onsuccess = function(event) { michael@0: objectStore.add(objectStoreData[objectStoreData.length - 1]) michael@0: .onsuccess = michael@0: function(event) { michael@0: cursor.continue(); michael@0: }; michael@0: }; michael@0: } michael@0: else { michael@0: cursor.continue(); michael@0: } michael@0: } michael@0: }; michael@0: yield undefined; michael@0: michael@0: is(count, objectStoreData.length - 1, "Good final count"); michael@0: is(sawAdded, true, "Saw item that was added"); michael@0: is(sawRemoved, false, "Didn't see item that was removed"); michael@0: michael@0: finishTest(); michael@0: michael@0: objectStore = null; // Bug 943409 workaround. michael@0: michael@0: yield undefined; michael@0: }