test_cursors.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /**
  2. * Any copyright is dedicated to the Public Domain.
  3. * http://creativecommons.org/publicdomain/zero/1.0/
  4. */
  5. var testGenerator = testSteps();
  6. function testSteps()
  7. {
  8. const name = this.window ? window.location.pathname : "Splendid Test";
  9. const keys = [1, -1, 0, 10, 2000, "q", "z", "two", "b", "a"];
  10. const sortedKeys = [-1, 0, 1, 10, 2000, "a", "b", "q", "two", "z"];
  11. is(keys.length, sortedKeys.length, "Good key setup");
  12. let request = indexedDB.open(name, 1);
  13. request.onerror = errorHandler;
  14. request.onupgradeneeded = grabEventAndContinueHandler;
  15. request.onsuccess = grabEventAndContinueHandler;
  16. let event = yield undefined;
  17. let db = event.target.result;
  18. let objectStore = db.createObjectStore("autoIncrement",
  19. { autoIncrement: true });
  20. request = objectStore.openCursor();
  21. request.onerror = errorHandler;
  22. request.onsuccess = function (event) {
  23. ok(!event.target.result, "No results");
  24. testGenerator.next();
  25. }
  26. yield undefined;
  27. objectStore = db.createObjectStore("autoIncrementKeyPath",
  28. { keyPath: "foo",
  29. autoIncrement: true });
  30. request = objectStore.openCursor();
  31. request.onerror = errorHandler;
  32. request.onsuccess = function (event) {
  33. ok(!event.target.result, "No results");
  34. testGenerator.next();
  35. }
  36. yield undefined;
  37. objectStore = db.createObjectStore("keyPath", { keyPath: "foo" });
  38. request = objectStore.openCursor();
  39. request.onerror = errorHandler;
  40. request.onsuccess = function (event) {
  41. ok(!event.target.result, "No results");
  42. testGenerator.next();
  43. }
  44. yield undefined;
  45. objectStore = db.createObjectStore("foo");
  46. request = objectStore.openCursor();
  47. request.onerror = errorHandler;
  48. request.onsuccess = function (event) {
  49. ok(!event.target.result, "No results");
  50. testGenerator.next();
  51. }
  52. yield undefined;
  53. let keyIndex = 0;
  54. for (let i in keys) {
  55. request = objectStore.add("foo", keys[i]);
  56. request.onerror = errorHandler;
  57. request.onsuccess = function(event) {
  58. if (++keyIndex == keys.length) {
  59. testGenerator.next();
  60. }
  61. };
  62. }
  63. yield undefined;
  64. keyIndex = 0;
  65. request = objectStore.openCursor();
  66. request.onerror = errorHandler;
  67. request.onsuccess = function (event) {
  68. let cursor = event.target.result;
  69. if (cursor) {
  70. is(cursor.key, sortedKeys[keyIndex], "Correct key");
  71. is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key");
  72. is(cursor.value, "foo", "Correct value");
  73. cursor.continue();
  74. try {
  75. cursor.continue();
  76. ok(false, "continue twice should throw");
  77. }
  78. catch (e) {
  79. ok(e instanceof DOMException, "got a database exception");
  80. is(e.name, "InvalidStateError", "correct error");
  81. is(e.code, DOMException.INVALID_STATE_ERR, "correct code");
  82. }
  83. is(cursor.key, sortedKeys[keyIndex], "Correct key");
  84. is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key");
  85. is(cursor.value, "foo", "Correct value");
  86. keyIndex++;
  87. }
  88. else {
  89. testGenerator.next();
  90. }
  91. }
  92. yield undefined;
  93. is(keyIndex, keys.length, "Saw all added items");
  94. keyIndex = 4;
  95. let range = IDBKeyRange.bound(2000, "q");
  96. request = objectStore.openCursor(range);
  97. request.onerror = errorHandler;
  98. request.onsuccess = function (event) {
  99. let cursor = event.target.result;
  100. if (cursor) {
  101. is(cursor.key, sortedKeys[keyIndex], "Correct key");
  102. is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key");
  103. is(cursor.value, "foo", "Correct value");
  104. cursor.continue();
  105. is(cursor.key, sortedKeys[keyIndex], "Correct key");
  106. is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key");
  107. is(cursor.value, "foo", "Correct value");
  108. keyIndex++;
  109. }
  110. else {
  111. testGenerator.next();
  112. }
  113. }
  114. yield undefined;
  115. is(keyIndex, 8, "Saw all the expected keys");
  116. keyIndex = 0;
  117. request = objectStore.openCursor();
  118. request.onerror = errorHandler;
  119. request.onsuccess = function (event) {
  120. let cursor = event.target.result;
  121. if (cursor) {
  122. is(cursor.key, sortedKeys[keyIndex], "Correct key");
  123. is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key");
  124. is(cursor.value, "foo", "Correct value");
  125. if (keyIndex) {
  126. cursor.continue();
  127. }
  128. else {
  129. cursor.continue("b");
  130. }
  131. is(cursor.key, sortedKeys[keyIndex], "Correct key");
  132. is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key");
  133. is(cursor.value, "foo", "Correct value");
  134. keyIndex += keyIndex ? 1: 6;
  135. }
  136. else {
  137. testGenerator.next();
  138. }
  139. }
  140. yield undefined;
  141. is(keyIndex, keys.length, "Saw all the expected keys");
  142. keyIndex = 0;
  143. request = objectStore.openCursor();
  144. request.onerror = errorHandler;
  145. request.onsuccess = function (event) {
  146. let cursor = event.target.result;
  147. if (cursor) {
  148. is(cursor.key, sortedKeys[keyIndex], "Correct key");
  149. is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key");
  150. is(cursor.value, "foo", "Correct value");
  151. if (keyIndex) {
  152. cursor.continue();
  153. }
  154. else {
  155. cursor.continue(10);
  156. }
  157. is(cursor.key, sortedKeys[keyIndex], "Correct key");
  158. is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key");
  159. is(cursor.value, "foo", "Correct value");
  160. keyIndex += keyIndex ? 1: 3;
  161. }
  162. else {
  163. testGenerator.next();
  164. }
  165. }
  166. yield undefined;
  167. is(keyIndex, keys.length, "Saw all the expected keys");
  168. keyIndex = 0;
  169. request = objectStore.openCursor();
  170. request.onerror = errorHandler;
  171. request.onsuccess = function (event) {
  172. let cursor = event.target.result;
  173. if (cursor) {
  174. is(cursor.key, sortedKeys[keyIndex], "Correct key");
  175. is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key");
  176. is(cursor.value, "foo", "Correct value");
  177. if (keyIndex) {
  178. cursor.continue();
  179. }
  180. else {
  181. cursor.continue("c");
  182. }
  183. is(cursor.key, sortedKeys[keyIndex], "Correct key");
  184. is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key");
  185. is(cursor.value, "foo", "Correct value");
  186. keyIndex += keyIndex ? 1 : 7;
  187. }
  188. else {
  189. ok(cursor === null, "The request result should be null.");
  190. testGenerator.next();
  191. }
  192. }
  193. yield undefined;
  194. is(keyIndex, keys.length, "Saw all the expected keys");
  195. keyIndex = 0;
  196. request = objectStore.openCursor();
  197. request.onerror = errorHandler;
  198. let storedCursor = null;
  199. request.onsuccess = function (event) {
  200. let cursor = event.target.result;
  201. if (cursor) {
  202. storedCursor = cursor;
  203. is(cursor.key, sortedKeys[keyIndex], "Correct key");
  204. is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key");
  205. is(cursor.value, "foo", "Correct value");
  206. if (keyIndex == 4) {
  207. request = cursor.update("bar");
  208. request.onerror = errorHandler;
  209. request.onsuccess = function(event) {
  210. keyIndex++;
  211. cursor.continue();
  212. };
  213. }
  214. else {
  215. keyIndex++;
  216. cursor.continue();
  217. }
  218. }
  219. else {
  220. ok(cursor === null, "The request result should be null.");
  221. ok(storedCursor.value === undefined, "The cursor's value should be undefined.");
  222. testGenerator.next();
  223. }
  224. }
  225. yield undefined;
  226. is(keyIndex, keys.length, "Saw all the expected keys");
  227. request = objectStore.get(sortedKeys[4]);
  228. request.onerror = errorHandler;
  229. request.onsuccess = grabEventAndContinueHandler;
  230. event = yield undefined;
  231. is(event.target.result, "bar", "Update succeeded");
  232. request = objectStore.put("foo", sortedKeys[4]);
  233. request.onerror = errorHandler;
  234. request.onsuccess = grabEventAndContinueHandler;
  235. event = yield undefined;
  236. keyIndex = 0;
  237. let gotRemoveEvent = false;
  238. let retval = false;
  239. request = objectStore.openCursor(null, "next");
  240. request.onerror = errorHandler;
  241. storedCursor = null;
  242. request.onsuccess = function (event) {
  243. let cursor = event.target.result;
  244. if (cursor) {
  245. storedCursor = cursor;
  246. is(cursor.key, sortedKeys[keyIndex], "Correct key");
  247. is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key");
  248. is(cursor.value, "foo", "Correct value");
  249. if (keyIndex == 4) {
  250. request = cursor.delete();
  251. request.onerror = errorHandler;
  252. request.onsuccess = function(event) {
  253. ok(event.target.result === undefined, "Should be undefined");
  254. is(keyIndex, 5, "Got result of remove before next continue");
  255. gotRemoveEvent = true;
  256. };
  257. }
  258. keyIndex++;
  259. cursor.continue();
  260. }
  261. else {
  262. ok(cursor === null, "The request result should be null.");
  263. ok(storedCursor.value === undefined, "The cursor's value should be undefined.");
  264. testGenerator.next();
  265. }
  266. }
  267. yield undefined;
  268. is(keyIndex, keys.length, "Saw all the expected keys");
  269. is(gotRemoveEvent, true, "Saw the remove event");
  270. request = objectStore.get(sortedKeys[4]);
  271. request.onerror = errorHandler;
  272. request.onsuccess = grabEventAndContinueHandler;
  273. event = yield undefined;
  274. is(event.target.result, undefined, "Entry was deleted");
  275. request = objectStore.add("foo", sortedKeys[4]);
  276. request.onerror = errorHandler;
  277. request.onsuccess = grabEventAndContinueHandler;
  278. event = yield undefined;
  279. keyIndex = sortedKeys.length - 1;
  280. request = objectStore.openCursor(null, "prev");
  281. request.onerror = errorHandler;
  282. storedCursor = null;
  283. request.onsuccess = function (event) {
  284. let cursor = event.target.result;
  285. if (cursor) {
  286. storedCursor = cursor;
  287. is(cursor.key, sortedKeys[keyIndex], "Correct key");
  288. is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key");
  289. is(cursor.value, "foo", "Correct value");
  290. cursor.continue();
  291. is(cursor.key, sortedKeys[keyIndex], "Correct key");
  292. is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key");
  293. is(cursor.value, "foo", "Correct value");
  294. keyIndex--;
  295. }
  296. else {
  297. ok(cursor === null, "The request result should be null.");
  298. ok(storedCursor.value === undefined, "The cursor's value should be undefined.");
  299. testGenerator.next();
  300. }
  301. }
  302. yield undefined;
  303. is(keyIndex, -1, "Saw all added items");
  304. // Wait for success
  305. yield undefined;
  306. db.close();
  307. finishTest();
  308. yield undefined;
  309. }