exceptions_in_events_iframe.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <!--
  2. Any copyright is dedicated to the Public Domain.
  3. http://creativecommons.org/publicdomain/zero/1.0/
  4. -->
  5. <html>
  6. <head>
  7. <title>Indexed Database Property Test</title>
  8. <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  9. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  10. <script type="text/javascript;version=1.7">
  11. let testGenerator = testSteps();
  12. function ok(val, message) {
  13. val = val ? "true" : "false";
  14. window.parent.postMessage("SimpleTest.ok(" + val + ", '" + message +
  15. "');", "*");
  16. }
  17. function is(a, b, message) {
  18. ok(a == b, message);
  19. }
  20. function grabEventAndContinueHandler(event) {
  21. testGenerator.send(event);
  22. }
  23. function errorHandler(event) {
  24. ok(false, "indexedDB error, code " + event.target.error.name);
  25. finishTest();
  26. }
  27. function unexpectedSuccessHandler(event) {
  28. ok(false, "got success when it was not expected!");
  29. finishTest();
  30. }
  31. function finishTest() {
  32. // Let window.onerror have a chance to fire
  33. setTimeout(function() {
  34. setTimeout(function() {
  35. testGenerator.close();
  36. window.parent.postMessage("SimpleTest.finish();", "*");
  37. }, 0);
  38. }, 0);
  39. }
  40. window.onerror = function() {
  41. return false;
  42. };
  43. function testSteps() {
  44. window.parent.SpecialPowers.addPermission("indexedDB", true, document);
  45. // Test 1: Throwing an exception in an upgradeneeded handler should
  46. // abort the versionchange transaction and fire an error at the request.
  47. let request = indexedDB.open(window.location.pathname, 1);
  48. request.onerror = errorHandler;
  49. request.onsuccess = unexpectedSuccessHandler;
  50. request.onupgradeneeded = function () {
  51. let transaction = request.transaction;
  52. transaction.oncomplete = unexpectedSuccessHandler;
  53. transaction.onabort = grabEventAndContinueHandler
  54. throw "STOP";
  55. };
  56. let event = yield undefined;
  57. is(event.type, "abort",
  58. "Throwing during an upgradeneeded event should abort the transaction.");
  59. is(event.target.error.name, "AbortError", "Got AbortError object");
  60. request.onerror = grabEventAndContinueHandler;
  61. event = yield undefined;
  62. is(event.type, "error",
  63. "Throwing during an upgradeneeded event should fire an error.");
  64. // Test 2: Throwing during a request's success handler should abort the
  65. // transaction.
  66. request = indexedDB.open(window.location.pathname, 1);
  67. request.onerror = errorHandler;
  68. request.onblocked = errorHandler;
  69. request.onupgradeneeded = grabEventAndContinueHandler;
  70. request.onsuccess = unexpectedSuccessHandler;
  71. let openrequest = request;
  72. event = yield undefined;
  73. request.onupgradeneeded = unexpectedSuccessHandler;
  74. let db = event.target.result;
  75. db.onerror = function(event) {
  76. event.preventDefault();
  77. };
  78. event.target.transaction.oncomplete = unexpectedSuccessHandler;
  79. event.target.transaction.onabort = grabEventAndContinueHandler;
  80. is(db.version, 1, "Correct version");
  81. is(db.objectStoreNames.length, 0, "Correct objectStoreNames length");
  82. let objectStore = db.createObjectStore("foo");
  83. is(db.objectStoreNames.length, 1, "Correct objectStoreNames length");
  84. ok(db.objectStoreNames.contains("foo"), "Has correct objectStore");
  85. request = objectStore.add({}, 1);
  86. request.onsuccess = function(event) {
  87. throw "foo";
  88. };
  89. event = yield undefined;
  90. is(event.type, "abort", "Got transaction abort event");
  91. is(event.target.error.name, "AbortError", "Got AbortError object");
  92. openrequest.onerror = grabEventAndContinueHandler;
  93. event = yield undefined;
  94. is(event.type, "error", "Got IDBOpenDBRequest error event");
  95. is(event.target, openrequest, "Right event target");
  96. is(event.target.error.name, "AbortError", "Right error name");
  97. // Test 3: Throwing during a request's error handler should abort the
  98. // transaction, even if preventDefault is called on the error event.
  99. request = indexedDB.open(window.location.pathname, 1);
  100. request.onerror = errorHandler;
  101. request.onblocked = errorHandler;
  102. request.onupgradeneeded = grabEventAndContinueHandler;
  103. request.onsuccess = unexpectedSuccessHandler;
  104. openrequest = request;
  105. event = yield undefined;
  106. request.onupgradeneeded = unexpectedSuccessHandler;
  107. db = event.target.result;
  108. db.onerror = function(event) {
  109. event.preventDefault();
  110. };
  111. event.target.transaction.oncomplete = unexpectedSuccessHandler;
  112. event.target.transaction.onabort = grabEventAndContinueHandler;
  113. is(db.version, 1, "Correct version");
  114. is(db.objectStoreNames.length, 0, "Correct objectStoreNames length");
  115. objectStore = db.createObjectStore("foo");
  116. is(db.objectStoreNames.length, 1, "Correct objectStoreNames length");
  117. ok(db.objectStoreNames.contains("foo"), "Has correct objectStore");
  118. request = objectStore.add({}, 1);
  119. request.onerror = errorHandler;
  120. request = objectStore.add({}, 1);
  121. request.onsuccess = unexpectedSuccessHandler;
  122. request.onerror = function (event) {
  123. event.preventDefault();
  124. throw "STOP";
  125. };
  126. event = yield undefined;
  127. is(event.type, "abort", "Got transaction abort event");
  128. is(event.target.error.name, "AbortError", "Got AbortError object");
  129. openrequest.onerror = grabEventAndContinueHandler;
  130. event = yield undefined;
  131. is(event.type, "error", "Got IDBOpenDBRequest error event");
  132. is(event.target, openrequest, "Right event target");
  133. is(event.target.error.name, "AbortError", "Right error name");
  134. finishTest();
  135. yield undefined;
  136. }
  137. </script>
  138. </head>
  139. <body onload="testGenerator.next();"></body>
  140. </html>