test_bug486990.xul 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?xml version="1.0"?>
  2. <?xml-stylesheet type="text/css" href="chrome://global/skin"?>
  3. <?xml-stylesheet type="text/css" href="/tests/SimpleTest/test.css"?>
  4. <!--
  5. https://bugzilla.mozilla.org/show_bug.cgi?id=486990
  6. -->
  7. <window title="Mozilla Bug 486990"
  8. xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
  9. onload="setTimeout(runTests, 0);">
  10. <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"/>
  11. <script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"/>
  12. <!-- test results are displayed in the html:body -->
  13. <body xmlns="http://www.w3.org/1999/xhtml">
  14. <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=486990"
  15. target="_blank">Mozilla Bug 486990</a>
  16. </body>
  17. <div xmlns="http://www.w3.org/1999/xhtml">
  18. <select size="5" id="select">
  19. <option>1</option>
  20. <option>2</option>
  21. <option>3</option>
  22. <option>4</option>
  23. <option>5</option>
  24. <option>6</option>
  25. <option>7</option>
  26. <option>8</option>
  27. <option>9</option>
  28. <option>10</option>
  29. </select>
  30. </div>
  31. <menupopup id="cm" onpopupshowing="popupShowing(event);">
  32. <menuitem label="Mozilla" value="http://mozilla.org"/>
  33. <menuitem label="Slashdot" value="http://slashdot.org"/>
  34. <menuitem label="Sourceforge" value="http://sf.net"/>
  35. <menuitem label="Freshmeat" value="http://freshmeat.net"/>
  36. </menupopup>
  37. <button label="test button" contextmenu="cm" id="testbutton"/>
  38. <!-- test code goes here -->
  39. <script type="application/javascript">
  40. <![CDATA[
  41. /** Test for Bug 486990 **/
  42. SimpleTest.waitForExplicitFinish();
  43. var prevented = false;
  44. var eventCount = 0;
  45. function fooListener(evt) {
  46. evt.preventDefault();
  47. prevented = evt.defaultPrevented;
  48. ++eventCount;
  49. };
  50. var clickCount = 0;
  51. var mouseDownCount = 0;
  52. var mouseUpCount = 0;
  53. function clickListener(evt) {
  54. ++clickCount;
  55. }
  56. function mouseDownListener(evt) {
  57. ++mouseDownCount;
  58. }
  59. function mouseUpListener(evt) {
  60. ++mouseUpCount;
  61. }
  62. var popupshowingcount = 0;
  63. function popupShowing(evt) {
  64. ++popupshowingcount;
  65. evt.preventDefault();
  66. }
  67. function contextMenuStopper(evt) {
  68. evt.stopPropagation();
  69. }
  70. function contextMenuPreventer(evt) {
  71. evt.preventDefault();
  72. }
  73. var tb;
  74. function runTests() {
  75. document.addEventListener("foo", fooListener, true);
  76. var e1 = document.createEvent("Event");
  77. e1.initEvent("foo", true, true);
  78. document.dispatchEvent(e1);
  79. is(eventCount, 1, "Wrong event count");
  80. ok(prevented, "Default handling should have been prevented.");
  81. prevented = false;
  82. var e2 = document.createEvent("Event");
  83. e2.initEvent("foo", false, false);
  84. document.dispatchEvent(e1);
  85. is(eventCount, 2, "Wrong event count");
  86. ok(prevented, "Default handling should have been prevented.");
  87. tb = document.getElementById("testbutton");
  88. dispatchTrustedContextMenuEvent(tb);
  89. is(popupshowingcount, 1, "Should have got 'popupShowing' event!");
  90. tb.addEventListener("contextmenu", contextMenuStopper, true);
  91. dispatchTrustedContextMenuEvent(tb);
  92. is(popupshowingcount, 2, "Should have got 'popupShowing' event!");
  93. tb.addEventListener("contextmenu", contextMenuPreventer, true);
  94. dispatchTrustedContextMenuEvent(tb);
  95. is(popupshowingcount, 2, "Should not have got 'popupShowing' event!");
  96. SpecialPowers.pushPrefEnv({"set": [["dom.event.contextmenu.enabled", false]]}, test2);
  97. }
  98. function test2() {
  99. dispatchTrustedContextMenuEvent(tb);
  100. is(popupshowingcount, 3, "Should have got 'popupShowing' event!");
  101. SpecialPowers.pushPrefEnv({"set": [["dom.event.contextmenu.enabled", true]]}, test3);
  102. }
  103. function test3() {
  104. dispatchTrustedContextMenuEvent(tb);
  105. is(popupshowingcount, 3, "Should not have got 'popupshowing' event!");
  106. var s = document.getElementById("select");
  107. s.addEventListener("click", clickListener, true);
  108. s.addEventListener("mousedown", mouseDownListener, true);
  109. s.addEventListener("mouseup", mouseUpListener, true);
  110. synthesizeMouse(s, 1, 10, {}, window);
  111. is(clickCount, 1, "Should have got click event!");
  112. is(mouseDownCount, 1, "Should have got mousedown event!");
  113. is(mouseUpCount, 1, "Should have got mouseup event!");
  114. // Dispatch to scrollbar.
  115. synthesizeMouse(s, s.getBoundingClientRect().right - 3, 10, {}, window);
  116. is(clickCount, 1, "Should not have got click event!");
  117. is(mouseDownCount, 2, "Should have got mousedown event!");
  118. is(mouseUpCount, 2, "Should have got mouseup event!");
  119. SimpleTest.finish();
  120. }
  121. function dispatchTrustedContextMenuEvent(target) {
  122. return sendMouseEvent({type:"contextmenu"}, target, window);
  123. }
  124. ]]>
  125. </script>
  126. </window>