test_remove_objectStore.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 objectStoreName = "Objects";
  10. let request = indexedDB.open(name, 1);
  11. request.onerror = errorHandler;
  12. request.onupgradeneeded = grabEventAndContinueHandler;
  13. request.onsuccess = unexpectedSuccessHandler;
  14. let event = yield undefined;
  15. request.onupgradeneeded = unexpectedSuccessHandler;
  16. request.onsuccess = grabEventAndContinueHandler;
  17. let db = event.target.result;
  18. is(db.objectStoreNames.length, 0, "Correct objectStoreNames list");
  19. let objectStore = db.createObjectStore(objectStoreName,
  20. { keyPath: "foo" });
  21. let addedCount = 0;
  22. for (let i = 0; i < 100; i++) {
  23. request = objectStore.add({foo: i});
  24. request.onerror = errorHandler;
  25. request.onsuccess = function(event) {
  26. if (++addedCount == 100) {
  27. executeSoon(function() { testGenerator.next(); });
  28. }
  29. }
  30. }
  31. yield undefined;
  32. is(db.objectStoreNames.length, 1, "Correct objectStoreNames list");
  33. is(db.objectStoreNames.item(0), objectStoreName, "Correct name");
  34. event.target.transaction.oncomplete = grabEventAndContinueHandler;
  35. event = yield undefined;
  36. // Wait for success.
  37. event = yield undefined;
  38. db.close();
  39. request = indexedDB.open(name, 2);
  40. request.onerror = errorHandler;
  41. request.onupgradeneeded = grabEventAndContinueHandler;
  42. request.onsuccess = unexpectedSuccessHandler;
  43. event = yield undefined;
  44. request.onupgradeneeded = unexpectedSuccessHandler;
  45. request.onsuccess = grabEventAndContinueHandler;
  46. db = event.target.result;
  47. let trans = event.target.transaction;
  48. let oldObjectStore = trans.objectStore(objectStoreName);
  49. isnot(oldObjectStore, null, "Correct object store prior to deleting");
  50. db.deleteObjectStore(objectStoreName);
  51. is(db.objectStoreNames.length, 0, "Correct objectStores list");
  52. try {
  53. trans.objectStore(objectStoreName);
  54. ok(false, "should have thrown");
  55. }
  56. catch(ex) {
  57. ok(ex instanceof DOMException, "Got a DOMException");
  58. is(ex.name, "NotFoundError", "expect a NotFoundError");
  59. is(ex.code, DOMException.NOT_FOUND_ERR, "expect a NOT_FOUND_ERR");
  60. }
  61. objectStore = db.createObjectStore(objectStoreName, { keyPath: "foo" });
  62. is(db.objectStoreNames.length, 1, "Correct objectStoreNames list");
  63. is(db.objectStoreNames.item(0), objectStoreName, "Correct name");
  64. is(trans.objectStore(objectStoreName), objectStore, "Correct new objectStore");
  65. isnot(oldObjectStore, objectStore, "Old objectStore is not new objectStore");
  66. request = objectStore.openCursor();
  67. request.onerror = errorHandler;
  68. request.onsuccess = function(event) {
  69. is(event.target.result, null, "ObjectStore shouldn't have any items");
  70. testGenerator.send(event);
  71. }
  72. event = yield undefined;
  73. db.deleteObjectStore(objectStore.name);
  74. is(db.objectStoreNames.length, 0, "Correct objectStores list");
  75. continueToNextStep();
  76. yield undefined;
  77. trans.oncomplete = grabEventAndContinueHandler;
  78. event = yield undefined;
  79. // Wait for success.
  80. event = yield undefined;
  81. db.close();
  82. request = indexedDB.open(name, 3);
  83. request.onerror = errorHandler;
  84. request.onupgradeneeded = grabEventAndContinueHandler;
  85. event = yield undefined;
  86. db = event.target.result;
  87. trans = event.target.transaction;
  88. objectStore = db.createObjectStore(objectStoreName, { keyPath: "foo" });
  89. request = objectStore.add({foo:"bar"});
  90. request.onerror = errorHandler;
  91. request.onsuccess = grabEventAndContinueHandler;
  92. db.deleteObjectStore(objectStoreName);
  93. event = yield undefined;
  94. trans.oncomplete = grabEventAndContinueHandler;
  95. event = yield undefined;
  96. finishTest();
  97. yield undefined;
  98. }