event_propagation_iframe.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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;version=1.7">
  9. let testGenerator = testSteps();
  10. function ok(val, message) {
  11. val = val ? "true" : "false";
  12. window.parent.postMessage("SimpleTest.ok(" + val + ", '" + message +
  13. "');", "*");
  14. }
  15. function grabEventAndContinueHandler(event) {
  16. testGenerator.send(event);
  17. }
  18. function errorHandler(event) {
  19. ok(false, "indexedDB error, code " + event.target.error.name);
  20. finishTest();
  21. }
  22. function finishTest() {
  23. // Let window.onerror have a chance to fire
  24. setTimeout(function() {
  25. setTimeout(function() {
  26. testGenerator.close();
  27. ok(windowErrorCount == 1, "Good window.onerror count");
  28. window.parent.postMessage("SimpleTest.finish();", "*");
  29. }, 0);
  30. }, 0);
  31. }
  32. const eventChain = [
  33. "IDBRequest",
  34. "IDBTransaction",
  35. "IDBDatabase"
  36. ];
  37. let captureCount = 0;
  38. let bubbleCount = 0;
  39. let atTargetCount = 0;
  40. let windowErrorCount = 0;
  41. window.onerror = function(event) {
  42. ok(!windowErrorCount++, "Correct number of window.onerror events");
  43. setTimeout(function() { testGenerator.next(); }, 0);
  44. };
  45. function errorEventCounter(event) {
  46. ok(event.type == "error", "Got an error event");
  47. ok(event.target instanceof window[eventChain[0]],
  48. "Correct event.target");
  49. let constructor;
  50. if (event.eventPhase == event.AT_TARGET) {
  51. atTargetCount++;
  52. constructor = eventChain[0];
  53. }
  54. else if (event.eventPhase == event.CAPTURING_PHASE) {
  55. constructor = eventChain[eventChain.length - 1 - captureCount++];
  56. }
  57. else if (event.eventPhase == event.BUBBLING_PHASE) {
  58. constructor = eventChain[++bubbleCount];
  59. if (windowErrorCount && bubbleCount == eventChain.length - 1) {
  60. event.preventDefault();
  61. }
  62. }
  63. ok(event.currentTarget instanceof window[constructor],
  64. "Correct event.currentTarget");
  65. if (bubbleCount == eventChain.length - 1) {
  66. ok(bubbleCount == captureCount,
  67. "Got same number of calls for both phases");
  68. ok(atTargetCount == 1, "Got one atTarget event");
  69. captureCount = bubbleCount = atTargetCount = 0;
  70. if (windowErrorCount) {
  71. finishTest();
  72. }
  73. }
  74. }
  75. function testSteps() {
  76. window.parent.SpecialPowers.addPermission("indexedDB", true, document);
  77. let request = indexedDB.open(window.location.pathname, 1);
  78. request.onerror = errorHandler;
  79. request.onupgradeneeded = grabEventAndContinueHandler;
  80. let event = yield undefined;
  81. let db = event.target.result;
  82. db.onerror = errorEventCounter;
  83. db.addEventListener("error", errorEventCounter, true);
  84. event.target.onsuccess = grabEventAndContinueHandler;
  85. db.createObjectStore("foo", { autoIncrement: true });
  86. yield undefined;
  87. let transaction = db.transaction("foo", "readwrite");
  88. transaction.addEventListener("error", errorEventCounter, false);
  89. transaction.addEventListener("error", errorEventCounter, true);
  90. let objectStore = transaction.objectStore("foo");
  91. request = objectStore.add({}, 1);
  92. request.onsuccess = grabEventAndContinueHandler;
  93. request.onerror = errorHandler;
  94. event = yield undefined;
  95. request = objectStore.add({}, 1);
  96. request.onsuccess = function(event) {
  97. ok(false, "Did not expect second add to succeed.");
  98. };
  99. request.onerror = errorEventCounter;
  100. yield undefined;
  101. transaction = db.transaction("foo", "readwrite");
  102. transaction.addEventListener("error", errorEventCounter, false);
  103. transaction.addEventListener("error", errorEventCounter, true);
  104. objectStore = transaction.objectStore("foo");
  105. request = objectStore.add({}, 1);
  106. request.onsuccess = grabEventAndContinueHandler;
  107. request.onerror = errorHandler;
  108. event = yield undefined;
  109. request = objectStore.add({}, 1);
  110. request.onsuccess = function(event) {
  111. ok(false, "Did not expect second add to succeed.");
  112. };
  113. request.onerror = errorEventCounter;
  114. yield undefined;
  115. }
  116. </script>
  117. </head>
  118. <body onload="testGenerator.next();"></body>
  119. </html>