test_cursor_mutation.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 objectStoreData = [
  9. // This one will be removed.
  10. { ss: "237-23-7732", name: "Bob" },
  11. // These will always be included.
  12. { ss: "237-23-7733", name: "Ann" },
  13. { ss: "237-23-7734", name: "Ron" },
  14. { ss: "237-23-7735", name: "Sue" },
  15. { ss: "237-23-7736", name: "Joe" },
  16. // This one will be added.
  17. { ss: "237-23-7737", name: "Pat" }
  18. ];
  19. // Post-add and post-remove data ordered by name.
  20. const objectStoreDataNameSort = [ 1, 4, 5, 2, 3 ];
  21. let request = indexedDB.open(this.window ? window.location.pathname : "Splendid Test", 1);
  22. request.onerror = errorHandler;
  23. request.onupgradeneeded = grabEventAndContinueHandler;
  24. let event = yield undefined;
  25. let db = event.target.result;
  26. event.target.onsuccess = continueToNextStep;
  27. let objectStore = db.createObjectStore("foo", { keyPath: "ss" });
  28. objectStore.createIndex("name", "name", { unique: true });
  29. for (let i = 0; i < objectStoreData.length - 1; i++) {
  30. objectStore.add(objectStoreData[i]);
  31. }
  32. yield undefined;
  33. let count = 0;
  34. let sawAdded = false;
  35. let sawRemoved = false;
  36. db.transaction("foo").objectStore("foo").openCursor().onsuccess =
  37. function(event) {
  38. event.target.transaction.oncomplete = continueToNextStep;
  39. let cursor = event.target.result;
  40. if (cursor) {
  41. if (cursor.value.name == objectStoreData[0].name) {
  42. sawRemoved = true;
  43. }
  44. if (cursor.value.name ==
  45. objectStoreData[objectStoreData.length - 1].name) {
  46. sawAdded = true;
  47. }
  48. cursor.continue();
  49. count++;
  50. }
  51. };
  52. yield undefined;
  53. is(count, objectStoreData.length - 1, "Good initial count");
  54. is(sawAdded, false, "Didn't see item that is about to be added");
  55. is(sawRemoved, true, "Saw item that is about to be removed");
  56. count = 0;
  57. sawAdded = false;
  58. sawRemoved = false;
  59. db.transaction("foo", "readwrite").objectStore("foo")
  60. .index("name").openCursor().onsuccess = function(event) {
  61. event.target.transaction.oncomplete = continueToNextStep;
  62. let cursor = event.target.result;
  63. if (cursor) {
  64. if (cursor.value.name == objectStoreData[0].name) {
  65. sawRemoved = true;
  66. }
  67. if (cursor.value.name ==
  68. objectStoreData[objectStoreData.length - 1].name) {
  69. sawAdded = true;
  70. }
  71. is(cursor.value.name,
  72. objectStoreData[objectStoreDataNameSort[count++]].name,
  73. "Correct name");
  74. if (count == 1) {
  75. let objectStore = event.target.transaction.objectStore("foo");
  76. objectStore.delete(objectStoreData[0].ss)
  77. .onsuccess = function(event) {
  78. objectStore.add(objectStoreData[objectStoreData.length - 1])
  79. .onsuccess =
  80. function(event) {
  81. cursor.continue();
  82. };
  83. };
  84. }
  85. else {
  86. cursor.continue();
  87. }
  88. }
  89. };
  90. yield undefined;
  91. is(count, objectStoreData.length - 1, "Good final count");
  92. is(sawAdded, true, "Saw item that was added");
  93. is(sawRemoved, false, "Didn't see item that was removed");
  94. finishTest();
  95. objectStore = null; // Bug 943409 workaround.
  96. yield undefined;
  97. }