summaryrefslogtreecommitdiffstats
path: root/devtools/client/responsive/test/browser/browser_tooltip.js
blob: 6721463725ca6b9a0b9faa2006074f444135a9c3 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const TEST_CONTENT = `<h1 title="test title" style="margin: 5px">test h1</h1>`;
const TEST_URL = `data:text/html;charset=utf-8,${TEST_CONTENT}`;

// Test for the tooltip coordinate on the browsing document in RDM.

addRDMTask(TEST_URL, async ({ ui }) => {
  // Make this synthesize mouse events via the parent process because some
  // code may cache the mouse cursor position like PresShell.  That may prevent
  // unexpected DOM events coming from the parent process.
  await pushPref("test.events.async.enabled", true);

  info("Create a promise which waits until the tooltip will be shown");
  const tooltip = ui.browserWindow.gBrowser.ownerDocument.getElementById(
    "remoteBrowserTooltip"
  );
  const onTooltipShown = BrowserTestUtils.waitForEvent(tooltip, "popupshown");
  const onTooltipHidden = BrowserTestUtils.waitForEvent(tooltip, "popuphidden");

  info("Show a tooltip");
  await spawnViewportTask(ui, {}, async () => {
    const target = content.document.querySelector("h1");

    // First, make sure to move mouse cursor outside the target.
    info("Waiting for initial mousemove");
    {
      // FYI: Some `mousemove` events may not be sent to the content process
      // until all preparations done in the main process and the content
      // process. Therefore, we need to synthesize `mousemove`s until we
      // get it in the remote process at least once.  Therefore, we cannot
      // use a promise to wait a `mousemove` here and we need to use the while
      // loop below.
      let mouseMoveFired = false;
      content.document.addEventListener(
        "mousemove",
        event => {
          isnot(
            event.target,
            target,
            "The first mousemove should be fired outside the target"
          );
          mouseMoveFired = true;
        },
        {
          once: true,
        }
      );
      while (!mouseMoveFired) {
        await EventUtils.synthesizeMouse(
          target,
          -2,
          -2,
          { type: "mousemove" },
          content
        );
        await EventUtils.synthesizeMouse(
          target,
          -1,
          -1,
          { type: "mousemove" },
          content
        );
        // Wait for the tooltip for the target is hidden even if it was visible.
        await new Promise(resolve =>
          content.window.requestAnimationFrame(() =>
            content.window.requestAnimationFrame(resolve)
          )
        );
      }
    }

    const eventLogger = event =>
      info(
        `${event.type}: path=[${event.composedPath()}], outerHTML=${
          event.target.outerHTML
        }, relatedTarget=${event.relatedTarget?.outerHTML}`
      );
    target.addEventListener("mouseover", eventLogger);
    target.addEventListener("mouseout", eventLogger);
    content.document.addEventListener("mousemove", eventLogger);
    // Then, move cursor over the target.
    await EventUtils.synthesizeMouse(
      target,
      1,
      1,
      { type: "mousemove", isSynthesized: false },
      content
    );
    await EventUtils.synthesizeMouse(
      target,
      2,
      1,
      { type: "mousemove", isSynthesized: false },
      content
    );
    await EventUtils.synthesizeMouse(
      target,
      3,
      1,
      { type: "mousemove", isSynthesized: false },
      content
    );
  });

  info("Wait for showing the tooltip");
  await onTooltipShown;

  info("Test the X coordinate of the tooltip");
  isnot(tooltip.screenX, 0, "The X coordinate of tooltip should not be 0");

  // Finally, clean up the tooltip if we're running in the verify mode.
  info("Wait for hiding the tooltip");
  await spawnViewportTask(ui, {}, async () => {
    info("Cleaning up the tooltip with moving the cursor");
    const target = content.document.querySelector("h1");
    await EventUtils.synthesizeMouse(
      target,
      -1,
      -1,
      { type: "mousemove", isSynthesized: false },
      content
    );
  });
  await onTooltipHidden;
});