test_rename_objectStore.js 5.6 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. const name = this.window ? window.location.pathname : "Splendid Test";
  9. const storeName_ToBeDeleted = "test store to be deleted";
  10. const storeName_v0 = "test store v0";
  11. const storeName_v1 = "test store v1";
  12. const storeName_v2 = "test store v2";
  13. const storeName_v3 = storeName_ToBeDeleted;
  14. const storeName_v4 = "test store v4";
  15. info("Rename in v1.");
  16. let request = indexedDB.open(name, 1);
  17. request.onerror = errorHandler;
  18. request.onupgradeneeded = grabEventAndContinueHandler;
  19. request.onsuccess = unexpectedSuccessHandler;
  20. let event = yield undefined;
  21. let db = event.target.result;
  22. let txn = event.target.transaction;
  23. is(db.objectStoreNames.length, 0, "Correct objectStoreNames list");
  24. // create objectstore to be deleted later in v3.
  25. db.createObjectStore(storeName_ToBeDeleted, { keyPath: "foo" });
  26. is(db.objectStoreNames.length, 1, "Correct objectStoreNames list");
  27. ok(db.objectStoreNames.contains(storeName_ToBeDeleted), "Correct name");
  28. // create target objectstore to be renamed.
  29. let objectStore = db.createObjectStore(storeName_v0, { keyPath: "bar" });
  30. is(db.objectStoreNames.length, 2, "Correct objectStoreNames list");
  31. ok(db.objectStoreNames.contains(objectStore.name), "Correct name");
  32. objectStore.name = storeName_v1;
  33. is(objectStore.name, storeName_v1, "Renamed successfully");
  34. txn.oncomplete = continueToNextStepSync;
  35. yield undefined;
  36. request.onsuccess = continueToNextStep;
  37. yield undefined;
  38. db.close();
  39. info("Verify renaming done in v1 and run renaming in v2.");
  40. request = indexedDB.open(name, 2);
  41. request.onerror = errorHandler;
  42. request.onupgradeneeded = grabEventAndContinueHandler;
  43. request.onsuccess = unexpectedSuccessHandler;
  44. event = yield undefined;
  45. db = event.target.result;
  46. txn = event.target.transaction;
  47. is(db.objectStoreNames.length, 2, "Correct objectStoreNames list");
  48. ok(db.objectStoreNames.contains(storeName_v1), "Correct name");
  49. ok(db.objectStoreNames.contains(storeName_ToBeDeleted), "Correct name");
  50. objectStore = txn.objectStore(storeName_v1);
  51. objectStore.name = storeName_v2;
  52. is(objectStore.name, storeName_v2, "Renamed successfully");
  53. txn.oncomplete = continueToNextStepSync;
  54. yield undefined;
  55. request.onsuccess = continueToNextStep;
  56. yield undefined;
  57. db.close();
  58. info("Verify renaming done in v2.");
  59. request = indexedDB.open(name, 2);
  60. request.onerror = errorHandler;
  61. request.onupgradeneeded = errorHandler;
  62. request.onsuccess = grabEventAndContinueHandler;
  63. event = yield undefined;
  64. db = event.target.result;
  65. is(db.objectStoreNames.length, 2, "Correct objectStoreNames list");
  66. ok(db.objectStoreNames.contains(storeName_v2), "Correct name");
  67. ok(db.objectStoreNames.contains(storeName_ToBeDeleted), "Correct name");
  68. db.close();
  69. info("Rename in v3.");
  70. request = indexedDB.open(name, 3);
  71. request.onerror = errorHandler;
  72. request.onupgradeneeded = grabEventAndContinueHandler;
  73. request.onsuccess = unexpectedSuccessHandler;
  74. event = yield undefined;
  75. db = event.target.result;
  76. txn = event.target.transaction;
  77. is(db.objectStoreNames.length, 2, "Correct objectStoreNames list");
  78. ok(db.objectStoreNames.contains(storeName_v2), "Correct name");
  79. ok(db.objectStoreNames.contains(storeName_ToBeDeleted), "Correct name");
  80. db.deleteObjectStore(storeName_ToBeDeleted);
  81. is(db.objectStoreNames.length, 1, "Correct objectStoreNames list");
  82. ok(db.objectStoreNames.contains(storeName_v2) &&
  83. !db.objectStoreNames.contains(storeName_ToBeDeleted), "Deleted correctly");
  84. objectStore = txn.objectStore(storeName_v2);
  85. objectStore.name = storeName_v3;
  86. is(objectStore.name, storeName_v3, "Renamed successfully");
  87. txn.oncomplete = continueToNextStepSync;
  88. yield undefined;
  89. request.onsuccess = continueToNextStep;
  90. yield undefined;
  91. db.close();
  92. info("Verify renaming done in v3.");
  93. request = indexedDB.open(name, 3);
  94. request.onerror = errorHandler;
  95. request.onupgradeneeded = errorHandler;
  96. request.onsuccess = grabEventAndContinueHandler;
  97. event = yield undefined;
  98. db = event.target.result;
  99. is(db.objectStoreNames.length, 1, "Correct objectStoreNames list");
  100. ok(db.objectStoreNames.contains(storeName_v3), "Correct name");
  101. db.close();
  102. info("Abort the version change transaction while renaming objectstore.");
  103. request = indexedDB.open(name, 4);
  104. request.onerror = errorHandler;
  105. request.onupgradeneeded = grabEventAndContinueHandler;
  106. request.onsuccess = unexpectedSuccessHandler;
  107. event = yield undefined;
  108. db = event.target.result;
  109. txn = event.target.transaction;
  110. objectStore = txn.objectStore(storeName_v3);
  111. objectStore.name = storeName_v4;
  112. is(objectStore.name, storeName_v4, "Renamed successfully");
  113. let putRequest = objectStore.put({ bar: "barValue" });
  114. putRequest.onsuccess = continueToNextStepSync;
  115. yield undefined;
  116. // Aborting the transaction.
  117. request.onerror = expectedErrorHandler("AbortError");
  118. txn.abort();
  119. yield undefined;
  120. // Verify if the name of the objectStore handle is reverted.
  121. is(objectStore.name, storeName_v3, "The name is reverted after aborted.");
  122. info("Verify if the objectstore name is unchanged.");
  123. request = indexedDB.open(name, 3);
  124. request.onerror = errorHandler;
  125. request.onupgradeneeded = errorHandler;
  126. request.onsuccess = grabEventAndContinueHandler;
  127. event = yield undefined;
  128. db = event.target.result;
  129. is(db.objectStoreNames.length, 1, "Correct objectStoreNames list");
  130. ok(db.objectStoreNames.contains(storeName_v3), "Correct name");
  131. db.close();
  132. finishTest();
  133. yield undefined;
  134. }