mochitest_support_external.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // This file supports translating W3C tests
  2. // to tests on auto MochiTest system with minimum changes.
  3. // Author: Maksim Lebedev <alessarik@gmail.com>
  4. // Function allows to prepare our tests after load document
  5. addEventListener("load", function(event) {
  6. console.log("OnLoad external document");
  7. prepareTest();
  8. }, false);
  9. // Function allows to initialize prerequisites before testing
  10. function prepareTest() {
  11. SimpleTest.waitForExplicitFinish();
  12. SimpleTest.requestCompleteLog();
  13. turnOnPointerEvents(startTest);
  14. }
  15. function setImplicitPointerCapture(capture, callback) {
  16. console.log("SET dom.w3c_pointer_events.implicit_capture as " + capture);
  17. SpecialPowers.pushPrefEnv({
  18. "set": [
  19. ["dom.w3c_pointer_events.implicit_capture", capture]
  20. ]
  21. }, callback);
  22. }
  23. function turnOnPointerEvents(callback) {
  24. console.log("SET dom.w3c_pointer_events.enabled as TRUE");
  25. console.log("SET layout.css.touch_action.enabled as TRUE");
  26. SpecialPowers.pushPrefEnv({
  27. "set": [
  28. ["dom.w3c_pointer_events.enabled", true],
  29. ["layout.css.touch_action.enabled", true]
  30. ]
  31. }, callback);
  32. }
  33. // Helper function to send MouseEvent with different parameters
  34. function sendMouseEvent(int_win, elemId, mouseEventType, params) {
  35. var elem = int_win.document.getElementById(elemId);
  36. if(!!elem) {
  37. var rect = elem.getBoundingClientRect();
  38. var eventObj = {type: mouseEventType};
  39. if(params && "button" in params)
  40. eventObj.button = params.button;
  41. if(params && "inputSource" in params)
  42. eventObj.inputSource = params.inputSource;
  43. if(params && "buttons" in params)
  44. eventObj.buttons = params.buttons;
  45. // Default to the center of the target element but we can still send to a
  46. // position outside of the target element.
  47. var offsetX = params && "offsetX" in params ? params.offsetX : rect.width / 2;
  48. var offsetY = params && "offsetY" in params ? params.offsetY : rect.height / 2;
  49. console.log(elemId, eventObj);
  50. synthesizeMouse(elem, offsetX, offsetY, eventObj, int_win);
  51. } else {
  52. is(!!elem, true, "Document should have element with id: " + elemId);
  53. }
  54. }
  55. // Helper function to send TouchEvent with different parameters
  56. function sendTouchEvent(int_win, elemId, touchEventType, params) {
  57. var elem = int_win.document.getElementById(elemId);
  58. if(!!elem) {
  59. var rect = elem.getBoundingClientRect();
  60. var eventObj = {type: touchEventType};
  61. // Default to the center of the target element but we can still send to a
  62. // position outside of the target element.
  63. var offsetX = params && "offsetX" in params ? params.offsetX : rect.width / 2;
  64. var offsetY = params && "offsetY" in params ? params.offsetY : rect.height / 2;
  65. console.log(elemId, eventObj);
  66. synthesizeTouch(elem, offsetX, offsetY, eventObj, int_win);
  67. } else {
  68. is(!!elem, true, "Document should have element with id: " + elemId);
  69. }
  70. }
  71. // Helper function to run Point Event test in a new tab.
  72. function runTestInNewWindow(aFile) {
  73. var w = window.open('', "_blank");
  74. w.is = function(a, b, msg) { return is(a, b, aFile + " | " + msg); };
  75. w.ok = function(cond, name, diag) { return ok(cond, aFile + " | " + name, diag); };
  76. w.location = location.href.substring(0, location.href.lastIndexOf('/') + 1) + aFile;
  77. w.testContext = {
  78. result_callback: (aTestObj) => {
  79. if(aTestObj["status"] != aTestObj["PASS"]) {
  80. console.log(aTestObj["status"] + " = " + aTestObj["PASS"] + ". " + aTestObj["name"]);
  81. }
  82. is(aTestObj["status"], aTestObj["PASS"], aTestObj["name"]);
  83. },
  84. completion_callback: () => {
  85. if (!!w.testContext.executionPromise) {
  86. // We need to wait tests done and execute finished then we can close the window
  87. w.testContext.executionPromise.then(() => {
  88. w.close();
  89. SimpleTest.finish();
  90. });
  91. } else {
  92. // execute may synchronous trigger tests done. In that case executionPromise
  93. // is not yet assigned
  94. w.close();
  95. SimpleTest.finish();
  96. }
  97. },
  98. execute: (aWindow) => {
  99. turnOnPointerEvents(() => {
  100. w.testContext.executionPromise = new Promise((aResolve, aReject) => {
  101. executeTest(aWindow);
  102. aResolve();
  103. });
  104. });
  105. }
  106. };
  107. return w;
  108. }