test_table_rollback.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 dbName = ("window" in this) ? window.location.pathname : "test";
  9. const objName1 = "foo";
  10. const objName2 = "bar";
  11. const data1 = "1234567890";
  12. const data2 = "0987654321";
  13. const dataCount = 500;
  14. let request = indexedDB.open(dbName, 1);
  15. request.onerror = errorHandler;
  16. request.onupgradeneeded = grabEventAndContinueHandler;
  17. let event = yield undefined;
  18. is(event.type, "upgradeneeded", "Got upgradeneeded");
  19. request.onupgradeneeded = errorHandler;
  20. request.onsuccess = grabEventAndContinueHandler;
  21. let db = request.result;
  22. let objectStore1 = db.createObjectStore(objName1, { autoIncrement: true });
  23. let objectStore2 = db.createObjectStore(objName2, { autoIncrement: true });
  24. info("Created object stores, adding data");
  25. for (let i = 0; i < dataCount; i++) {
  26. objectStore1.add(data1);
  27. objectStore2.add(data2);
  28. }
  29. info("Done adding data");
  30. event = yield undefined;
  31. is(event.type, "success", "Got success");
  32. let readResult = null;
  33. let readError = null;
  34. let writeAborted = false;
  35. info("Creating readwrite transaction");
  36. objectStore1 = db.transaction(objName1, "readwrite").objectStore(objName1);
  37. objectStore1.openCursor().onsuccess = grabEventAndContinueHandler;
  38. event = yield undefined;
  39. let cursor = event.target.result;
  40. is(cursor.value, data1, "Got correct data for readwrite transaction");
  41. info("Modifying object store on readwrite transaction");
  42. cursor.update(data2);
  43. cursor.continue();
  44. event = yield undefined;
  45. info("Done modifying object store on readwrite transaction, creating " +
  46. "readonly transaction");
  47. objectStore2 = db.transaction(objName2, "readonly").objectStore(objName2);
  48. request = objectStore2.getAll();
  49. request.onsuccess = function(event) {
  50. readResult = event.target.result;
  51. is(readResult.length,
  52. dataCount,
  53. "Got correct number of results on readonly transaction");
  54. for (let i = 0; i < readResult.length; i++) {
  55. is(readResult[i], data2, "Got correct data for readonly transaction");
  56. }
  57. if (writeAborted) {
  58. continueToNextStep();
  59. }
  60. };
  61. request.onerror = function(event) {
  62. readResult = null;
  63. readError = event.target.error;
  64. ok(false, "Got read error: " + readError.name);
  65. event.preventDefault();
  66. if (writeAborted) {
  67. continueToNextStep();
  68. }
  69. }
  70. cursor = event.target.result;
  71. is(cursor.value, data1, "Got correct data for readwrite transaction");
  72. info("Aborting readwrite transaction");
  73. cursor.source.transaction.abort();
  74. writeAborted = true;
  75. if (!readError && !readResult) {
  76. info("Waiting for readonly transaction to complete");
  77. yield undefined;
  78. }
  79. ok(readResult, "Got result from readonly transaction");
  80. is(readError, null, "No read error");
  81. is(writeAborted, true, "Aborted readwrite transaction");
  82. finishTest();
  83. yield undefined;
  84. }