test_index_update_delete.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. let name = this.window ? window.location.pathname : "Splendid Test";
  9. let request = indexedDB.open(name, 1);
  10. request.onerror = errorHandler;
  11. request.onupgradeneeded = grabEventAndContinueHandler;
  12. request.onsuccess = grabEventAndContinueHandler;
  13. let event = yield undefined;
  14. let db = event.target.result;
  15. db.onerror = errorHandler;
  16. for (let autoIncrement of [false, true]) {
  17. let objectStore =
  18. db.createObjectStore(autoIncrement, { keyPath: "id",
  19. autoIncrement: autoIncrement });
  20. for (let i = 0; i < 10; i++) {
  21. objectStore.add({ id: i, index: i });
  22. }
  23. for (let unique of [false, true]) {
  24. objectStore.createIndex(unique, "index", { unique: unique });
  25. }
  26. for (let i = 10; i < 20; i++) {
  27. objectStore.add({ id: i, index: i });
  28. }
  29. }
  30. event = yield undefined;
  31. is(event.type, "success", "expect a success event");
  32. for (let autoIncrement of [false, true]) {
  33. let objectStore = db.transaction(autoIncrement)
  34. .objectStore(autoIncrement);
  35. objectStore.count().onsuccess = grabEventAndContinueHandler;
  36. let event = yield undefined;
  37. is(event.target.result, 20, "Correct number of entries in objectStore");
  38. let objectStoreCount = event.target.result;
  39. let indexCount = event.target.result;
  40. for (let unique of [false, true]) {
  41. let index = db.transaction(autoIncrement, "readwrite")
  42. .objectStore(autoIncrement)
  43. .index(unique);
  44. index.count().onsuccess = grabEventAndContinueHandler;
  45. let event = yield undefined;
  46. is(event.target.result, indexCount,
  47. "Correct number of entries in index");
  48. let modifiedEntry = unique ? 5 : 10;
  49. let keyRange = IDBKeyRange.only(modifiedEntry);
  50. let sawEntry = false;
  51. index.openCursor(keyRange).onsuccess = function(event) {
  52. let cursor = event.target.result;
  53. if (cursor) {
  54. sawEntry = true;
  55. is(cursor.key, modifiedEntry, "Correct key");
  56. cursor.value.index = unique ? 30 : 35;
  57. cursor.update(cursor.value).onsuccess = function(event) {
  58. cursor.continue();
  59. }
  60. }
  61. else {
  62. continueToNextStep();
  63. }
  64. }
  65. yield undefined;
  66. is(sawEntry, true, "Saw entry for key value " + modifiedEntry);
  67. // Recount index. Shouldn't change.
  68. index = db.transaction(autoIncrement, "readwrite")
  69. .objectStore(autoIncrement)
  70. .index(unique);
  71. index.count().onsuccess = grabEventAndContinueHandler;
  72. event = yield undefined;
  73. is(event.target.result, indexCount,
  74. "Correct number of entries in index");
  75. modifiedEntry = unique ? 30 : 35;
  76. keyRange = IDBKeyRange.only(modifiedEntry);
  77. sawEntry = false;
  78. index.openCursor(keyRange).onsuccess = function(event) {
  79. let cursor = event.target.result;
  80. if (cursor) {
  81. sawEntry = true;
  82. is(cursor.key, modifiedEntry, "Correct key");
  83. delete cursor.value.index;
  84. cursor.update(cursor.value).onsuccess = function(event) {
  85. indexCount--;
  86. cursor.continue();
  87. }
  88. }
  89. else {
  90. continueToNextStep();
  91. }
  92. }
  93. yield undefined;
  94. is(sawEntry, true, "Saw entry for key value " + modifiedEntry);
  95. // Recount objectStore. Should be unchanged.
  96. objectStore = db.transaction(autoIncrement, "readwrite")
  97. .objectStore(autoIncrement);
  98. objectStore.count().onsuccess = grabEventAndContinueHandler;
  99. event = yield undefined;
  100. is(event.target.result, objectStoreCount,
  101. "Correct number of entries in objectStore");
  102. // Recount index. Should be one item less.
  103. index = objectStore.index(unique);
  104. index.count().onsuccess = grabEventAndContinueHandler;
  105. event = yield undefined;
  106. is(event.target.result, indexCount,
  107. "Correct number of entries in index");
  108. modifiedEntry = objectStoreCount - 1;
  109. objectStore.delete(modifiedEntry).onsuccess =
  110. grabEventAndContinueHandler;
  111. event = yield undefined;
  112. objectStoreCount--;
  113. indexCount--;
  114. objectStore.count().onsuccess = grabEventAndContinueHandler;
  115. event = yield undefined;
  116. is(event.target.result, objectStoreCount,
  117. "Correct number of entries in objectStore");
  118. index.count().onsuccess = grabEventAndContinueHandler;
  119. event = yield undefined;
  120. is(event.target.result, indexCount,
  121. "Correct number of entries in index");
  122. index = event = null; // Bug 943409 workaround.
  123. }
  124. objectStore = event = null; // Bug 943409 workaround.
  125. }
  126. finishTest();
  127. event = db = request = null; // Bug 943409 workaround.
  128. yield undefined;
  129. }