test_advance.js 3.9 KB

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