dom/indexedDB/test/unit/test_advance.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 dataCount = 30;
michael@0 11
michael@0 12 let request = indexedDB.open(this.window ? window.location.pathname : "Splendid Test", 1);
michael@0 13 request.onerror = errorHandler;
michael@0 14 request.onupgradeneeded = grabEventAndContinueHandler;
michael@0 15 let event = yield undefined;
michael@0 16
michael@0 17 let db = event.target.result;
michael@0 18 db.onerror = errorHandler;
michael@0 19
michael@0 20 event.target.onsuccess = continueToNextStep;
michael@0 21
michael@0 22 let objectStore = db.createObjectStore("", { keyPath: "key" });
michael@0 23 objectStore.createIndex("", "index");
michael@0 24
michael@0 25 for (let i = 0; i < dataCount; i++) {
michael@0 26 objectStore.add({ key: i, index: i });
michael@0 27 }
michael@0 28 yield undefined;
michael@0 29
michael@0 30 function getObjectStore() {
michael@0 31 return db.transaction("").objectStore("");
michael@0 32 }
michael@0 33
michael@0 34 function getIndex() {
michael@0 35 return db.transaction("").objectStore("").index("");
michael@0 36 }
michael@0 37
michael@0 38 let count = 0;
michael@0 39
michael@0 40 getObjectStore().openCursor().onsuccess = function(event) {
michael@0 41 let cursor = event.target.result;
michael@0 42 if (cursor) {
michael@0 43 count++;
michael@0 44 cursor.continue();
michael@0 45 }
michael@0 46 else {
michael@0 47 continueToNextStep();
michael@0 48 }
michael@0 49 };
michael@0 50 yield undefined;
michael@0 51
michael@0 52 is(count, dataCount, "Saw all data");
michael@0 53
michael@0 54 count = 0;
michael@0 55
michael@0 56 getObjectStore().openCursor().onsuccess = function(event) {
michael@0 57 let cursor = event.target.result;
michael@0 58 if (cursor) {
michael@0 59 is(cursor.primaryKey, count, "Got correct object");
michael@0 60 if (count) {
michael@0 61 count++;
michael@0 62 cursor.continue();
michael@0 63 }
michael@0 64 else {
michael@0 65 count = 10;
michael@0 66 cursor.advance(10);
michael@0 67 }
michael@0 68 }
michael@0 69 else {
michael@0 70 continueToNextStep();
michael@0 71 }
michael@0 72 };
michael@0 73 yield undefined;
michael@0 74
michael@0 75 is(count, dataCount, "Saw all data");
michael@0 76
michael@0 77 count = 0;
michael@0 78
michael@0 79 getIndex().openCursor().onsuccess = function(event) {
michael@0 80 let cursor = event.target.result;
michael@0 81 if (cursor) {
michael@0 82 is(cursor.primaryKey, count, "Got correct object");
michael@0 83 if (count) {
michael@0 84 count++;
michael@0 85 cursor.continue();
michael@0 86 }
michael@0 87 else {
michael@0 88 count = 10;
michael@0 89 cursor.advance(10);
michael@0 90 }
michael@0 91 }
michael@0 92 else {
michael@0 93 continueToNextStep();
michael@0 94 }
michael@0 95 };
michael@0 96 yield undefined;
michael@0 97
michael@0 98 is(count, dataCount, "Saw all data");
michael@0 99
michael@0 100 count = 0;
michael@0 101
michael@0 102 getIndex().openKeyCursor().onsuccess = function(event) {
michael@0 103 let cursor = event.target.result;
michael@0 104 if (cursor) {
michael@0 105 is(cursor.primaryKey, count, "Got correct object");
michael@0 106 if (count) {
michael@0 107 count++;
michael@0 108 cursor.continue();
michael@0 109 }
michael@0 110 else {
michael@0 111 count = 10;
michael@0 112 cursor.advance(10);
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(count, dataCount, "Saw all data");
michael@0 122
michael@0 123 count = 0;
michael@0 124
michael@0 125 getObjectStore().openCursor().onsuccess = function(event) {
michael@0 126 let cursor = event.target.result;
michael@0 127 if (cursor) {
michael@0 128 is(cursor.primaryKey, count, "Got correct object");
michael@0 129 if (count == 0) {
michael@0 130 cursor.advance(dataCount + 1);
michael@0 131 }
michael@0 132 else {
michael@0 133 ok(false, "Should never get here!");
michael@0 134 cursor.continue();
michael@0 135 }
michael@0 136 }
michael@0 137 else {
michael@0 138 continueToNextStep();
michael@0 139 }
michael@0 140 };
michael@0 141 yield undefined;
michael@0 142
michael@0 143 is(count, 0, "Saw all data");
michael@0 144
michael@0 145 count = dataCount - 1;
michael@0 146
michael@0 147 getObjectStore().openCursor(null, "prev").onsuccess = function(event) {
michael@0 148 let cursor = event.target.result;
michael@0 149 if (cursor) {
michael@0 150 is(cursor.primaryKey, count, "Got correct object");
michael@0 151 count--;
michael@0 152 if (count == dataCount - 2) {
michael@0 153 cursor.advance(10);
michael@0 154 count -= 9;
michael@0 155 }
michael@0 156 else {
michael@0 157 cursor.continue();
michael@0 158 }
michael@0 159 }
michael@0 160 else {
michael@0 161 continueToNextStep();
michael@0 162 }
michael@0 163 };
michael@0 164 yield undefined;
michael@0 165
michael@0 166 is(count, -1, "Saw all data");
michael@0 167
michael@0 168 count = dataCount - 1;
michael@0 169
michael@0 170 getObjectStore().openCursor(null, "prev").onsuccess = function(event) {
michael@0 171 let cursor = event.target.result;
michael@0 172 if (cursor) {
michael@0 173 is(cursor.primaryKey, count, "Got correct object");
michael@0 174 if (count == dataCount - 1) {
michael@0 175 cursor.advance(dataCount + 1);
michael@0 176 }
michael@0 177 else {
michael@0 178 ok(false, "Should never get here!");
michael@0 179 cursor.continue();
michael@0 180 }
michael@0 181 }
michael@0 182 else {
michael@0 183 continueToNextStep();
michael@0 184 }
michael@0 185 };
michael@0 186 yield undefined;
michael@0 187
michael@0 188 is(count, dataCount - 1, "Saw all data");
michael@0 189
michael@0 190 finishTest();
michael@0 191 yield undefined;
michael@0 192 }

mercurial