Writing mochitests for firefox is a tricky task sometimes since you need to recreate use cases of browser usage withouth human interaction.
I was faced with the task of writing some tests to check the mouselock API. One test specifically, needed to check if mouse events were being retartgeted from the child to the parent locked element, for example:
<div id="parent"> <div id="child"> </div> </div>
If parent is locked and child receives a mouseevent, the event should be retargeted to the parent and not be dispatched to the child.
To write the test I had to use the EventUtils library
EventUtils has several different functions to deal with synthesizing mouse and key events, the one I was interested for dispatching mouse events was: synthesizeMouseAtCenter
Since the element is in fullscreen mode, and it doesn’t matter which specific position the mouse event occurs, synthesizeMouseAtCenter was perfect for the task.
Dispatching a mouse event with sythesizeMouseAtCenter involves the work of three functions:
The center of the element receiving the event is calculated by getting its the dimensions with getBoundingClientRect.
Here the center of the element is calculated in a browser perspective.
Again getBoundClientRect is called, but this time the distance between the left and top border from the browser window is added to the center point of the element calculated in synthesizeMouseAtCenter
In the end synthesizeMouseAtPoint is called with the x,y coordinates of where the event should take place in the browser window, the type of event it should be dispatched and the window that will receive the event.
To dispatch the event the nsDOMWindowUtils::SendMouseEvent method gets called.