Synthesizing mouse events in a mochitest

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:

1. synthesizeMouseAtCenter

The center of the element receiving the event is calculated by getting its the dimensions with getBoundingClientRect.

2. synthesizeMouse

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

3. synthesizeMouseAtPoint

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.

Advertisements

Creating my first Firefox patch

In creating my first Firefox patch I struggled a bit to put everything together.
These are the steps I followed:

1. Configure mercurial

Add this settings to ~/.hgrc

[ui]
username = Diogo Golovanevsky Monteiro <diogo.gmt@gmail.com>
ui.merge = meld
 
[diff]
git = 1
showfunc = 1
unified = 8

[defaults]
diff = -p -U 8
qdiff = -p -U 8
qnew = -U
commit = -v

[extensions]
mq =

2. Clone Mozilla’s repo

hg clone http://hg.mozilla.org/mozilla-central/ src

3. Make changes to the code

4. Create a new patch

hg qnew -f bug-1234

5. Make sure you are in the right patch

hg qtop

6. Enter a commit message to the patch

hg qrefresh -m "Bug 1234 - bug description"

7. Make sure the patch applies cleanly

hg qpop -a
hg pull
hg update
hg qpush

The patch will be saved in /repoRoot/.hg/patches/patchName

For a more detail explanation:
Getting Mozilla Source Code Using Mercurial
How to Submit a Patch
Creating a patch
Using Mercurial
Mercurial Queues
http://blog.bonardo.net/2010/06/22/so-youre-about-to-use-checkin-needed