dom/indexedDB/test/unit/test_advance.js

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

mercurial