Thu, 22 Jan 2015 13:21:57 +0100
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 | const objectStoreData = [ |
michael@0 | 11 | // This one will be removed. |
michael@0 | 12 | { ss: "237-23-7732", name: "Bob" }, |
michael@0 | 13 | |
michael@0 | 14 | // These will always be included. |
michael@0 | 15 | { ss: "237-23-7733", name: "Ann" }, |
michael@0 | 16 | { ss: "237-23-7734", name: "Ron" }, |
michael@0 | 17 | { ss: "237-23-7735", name: "Sue" }, |
michael@0 | 18 | { ss: "237-23-7736", name: "Joe" }, |
michael@0 | 19 | |
michael@0 | 20 | // This one will be added. |
michael@0 | 21 | { ss: "237-23-7737", name: "Pat" } |
michael@0 | 22 | ]; |
michael@0 | 23 | |
michael@0 | 24 | // Post-add and post-remove data ordered by name. |
michael@0 | 25 | const objectStoreDataNameSort = [ 1, 4, 5, 2, 3 ]; |
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 | event.target.onsuccess = continueToNextStep; |
michael@0 | 34 | |
michael@0 | 35 | let objectStore = db.createObjectStore("foo", { keyPath: "ss" }); |
michael@0 | 36 | objectStore.createIndex("name", "name", { unique: true }); |
michael@0 | 37 | |
michael@0 | 38 | for (let i = 0; i < objectStoreData.length - 1; i++) { |
michael@0 | 39 | objectStore.add(objectStoreData[i]); |
michael@0 | 40 | } |
michael@0 | 41 | yield undefined; |
michael@0 | 42 | |
michael@0 | 43 | let count = 0; |
michael@0 | 44 | |
michael@0 | 45 | let sawAdded = false; |
michael@0 | 46 | let sawRemoved = false; |
michael@0 | 47 | |
michael@0 | 48 | db.transaction("foo").objectStore("foo").openCursor().onsuccess = |
michael@0 | 49 | function(event) { |
michael@0 | 50 | event.target.transaction.oncomplete = continueToNextStep; |
michael@0 | 51 | let cursor = event.target.result; |
michael@0 | 52 | if (cursor) { |
michael@0 | 53 | if (cursor.value.name == objectStoreData[0].name) { |
michael@0 | 54 | sawRemoved = true; |
michael@0 | 55 | } |
michael@0 | 56 | if (cursor.value.name == |
michael@0 | 57 | objectStoreData[objectStoreData.length - 1].name) { |
michael@0 | 58 | sawAdded = true; |
michael@0 | 59 | } |
michael@0 | 60 | cursor.continue(); |
michael@0 | 61 | count++; |
michael@0 | 62 | } |
michael@0 | 63 | }; |
michael@0 | 64 | yield undefined; |
michael@0 | 65 | |
michael@0 | 66 | is(count, objectStoreData.length - 1, "Good initial count"); |
michael@0 | 67 | is(sawAdded, false, "Didn't see item that is about to be added"); |
michael@0 | 68 | is(sawRemoved, true, "Saw item that is about to be removed"); |
michael@0 | 69 | |
michael@0 | 70 | count = 0; |
michael@0 | 71 | sawAdded = false; |
michael@0 | 72 | sawRemoved = false; |
michael@0 | 73 | |
michael@0 | 74 | db.transaction("foo", "readwrite").objectStore("foo") |
michael@0 | 75 | .index("name").openCursor().onsuccess = function(event) { |
michael@0 | 76 | event.target.transaction.oncomplete = continueToNextStep; |
michael@0 | 77 | let cursor = event.target.result; |
michael@0 | 78 | if (cursor) { |
michael@0 | 79 | if (cursor.value.name == objectStoreData[0].name) { |
michael@0 | 80 | sawRemoved = true; |
michael@0 | 81 | } |
michael@0 | 82 | if (cursor.value.name == |
michael@0 | 83 | objectStoreData[objectStoreData.length - 1].name) { |
michael@0 | 84 | sawAdded = true; |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | is(cursor.value.name, |
michael@0 | 88 | objectStoreData[objectStoreDataNameSort[count++]].name, |
michael@0 | 89 | "Correct name"); |
michael@0 | 90 | |
michael@0 | 91 | if (count == 1) { |
michael@0 | 92 | let objectStore = event.target.transaction.objectStore("foo"); |
michael@0 | 93 | objectStore.delete(objectStoreData[0].ss) |
michael@0 | 94 | .onsuccess = function(event) { |
michael@0 | 95 | objectStore.add(objectStoreData[objectStoreData.length - 1]) |
michael@0 | 96 | .onsuccess = |
michael@0 | 97 | function(event) { |
michael@0 | 98 | cursor.continue(); |
michael@0 | 99 | }; |
michael@0 | 100 | }; |
michael@0 | 101 | } |
michael@0 | 102 | else { |
michael@0 | 103 | cursor.continue(); |
michael@0 | 104 | } |
michael@0 | 105 | } |
michael@0 | 106 | }; |
michael@0 | 107 | yield undefined; |
michael@0 | 108 | |
michael@0 | 109 | is(count, objectStoreData.length - 1, "Good final count"); |
michael@0 | 110 | is(sawAdded, true, "Saw item that was added"); |
michael@0 | 111 | is(sawRemoved, false, "Didn't see item that was removed"); |
michael@0 | 112 | |
michael@0 | 113 | finishTest(); |
michael@0 | 114 | |
michael@0 | 115 | objectStore = null; // Bug 943409 workaround. |
michael@0 | 116 | |
michael@0 | 117 | yield undefined; |
michael@0 | 118 | } |