summaryrefslogtreecommitdiffstats
path: root/dom/events/test/browser_mouseout_notification_panel.js
blob: 4bd7b93afc53ea00acc8f8f1658591de3f10780f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"use strict";

async function showNotification(aBrowser, aId) {
  info(`Show notification ${aId}`);
  let promise = BrowserTestUtils.waitForEvent(
    PopupNotifications.panel,
    "popupshown"
  );
  let notification = PopupNotifications.show(
    aBrowser /* browser */,
    "test-notification-" + aId /* id */,
    aId + ": Will you allow <> to perform this action?" /* message */,
    null /* anchorID */,
    {
      label: "Main Action",
      accessKey: "M",
      callback: () => {},
    } /* mainAction */,
    [
      {
        label: "Secondary Action",
        accessKey: "S",
        callback: () => {},
      },
    ] /* secondaryActions */
  );
  await promise;

  let rect = PopupNotifications.panel.getBoundingClientRect();
  return { notification, rect };
}

function waitForMouseEvent(aType, aElement) {
  return new Promise(resolve => {
    aElement.addEventListener(
      aType,
      e => {
        resolve({
          screenX: e.screenX,
          screenY: e.screenY,
          clientX: e.clientX,
          clientY: e.clientY,
        });
      },
      { once: true }
    );
  });
}

function waitForRemoteMouseEvent(aType, aBrowser) {
  return SpecialPowers.spawn(aBrowser, [aType], async aType => {
    return new Promise(
      resolve => {
        content.document.addEventListener(aType, e => {
          resolve({
            screenX: e.screenX,
            screenY: e.screenY,
            clientX: e.clientX,
            clientY: e.clientY,
          });
        });
      },
      { once: true }
    );
  });
}

function synthesizeMouseAtCenter(aRect) {
  EventUtils.synthesizeMouseAtPoint(
    aRect.left + aRect.width / 2,
    aRect.top + aRect.height / 2,
    {
      type: "mousemove",
    }
  );
}

let notificationRect;

add_setup(async function init() {
  ok(PopupNotifications, "PopupNotifications object exists");
  ok(PopupNotifications.panel, "PopupNotifications panel exists");

  await SpecialPowers.pushPrefEnv({
    set: [["test.events.async.enabled", true]],
  });

  info(`Show notification to get its size and position`);
  let { notification, rect } = await showNotification(
    gBrowser.selectedBrowser,
    "Test#Init"
  );
  PopupNotifications.remove(notification);
  notificationRect = rect;
});

add_task(async function test_mouseout_chrome() {
  await BrowserTestUtils.withNewTab("about:blank", async browser => {
    info(`Generate mousemove event on browser`);
    let mousemovePromise = waitForMouseEvent("mousemove", browser);
    synthesizeMouseAtCenter(notificationRect);
    let mousemoveCoordinate = await mousemovePromise;
    info(`mousemove event: ${JSON.stringify(mousemoveCoordinate)}`);

    info(`Showing notification should generate mouseout event on browser`);
    let mouseoutPromise = waitForMouseEvent("mouseout", browser);
    let { notification } = await showNotification(browser, "Test#Chrome");
    let mouseoutCoordinate = await mouseoutPromise;
    info(`mouseout event: ${JSON.stringify(mouseoutCoordinate)}`);

    SimpleTest.isDeeply(
      mouseoutCoordinate,
      mousemoveCoordinate,
      "Test event coordinate"
    );
    info(`Remove notification`);
    PopupNotifications.remove(notification);
  });
});

add_task(async function test_mouseout_content() {
  await BrowserTestUtils.withNewTab("about:blank", async browser => {
    info(`Generate mousemove event on content`);
    let mousemovePromise = waitForRemoteMouseEvent("mousemove", browser);
    SimpleTest.executeSoon(() => {
      synthesizeMouseAtCenter(notificationRect);
    });
    let mousemoveCoordinate = await mousemovePromise;
    info(`mousemove event on content: ${JSON.stringify(mousemoveCoordinate)}`);

    info(`Showing notification should generate mouseout event on content`);
    let mouseoutPromise = waitForRemoteMouseEvent("mouseout", browser);
    SimpleTest.executeSoon(async () => {
      showNotification(browser, "Test#Content");
    });
    let mouseoutCoordinate = await mouseoutPromise;
    info(`remote mouseout event: ${JSON.stringify(mouseoutCoordinate)}`);

    SimpleTest.isDeeply(
      mouseoutCoordinate,
      mousemoveCoordinate,
      "Test event coordinate"
    );
  });
});