summaryrefslogtreecommitdiffstats
path: root/devtools/client/inspector/test/browser_inspector_highlighter-eyedropper-events.js
blob: 0c05aa219f4106e28b6bc38986b798f3dfee99e5 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

// Test the eyedropper mouse and keyboard handling.

const HIGHLIGHTER_TYPE = "EyeDropper";
const ID = "eye-dropper-";
const TEST_URI = `
<style>
  html{width:100%;height:100%;}
</style>
<body>eye-dropper test</body>`;

const MOVE_EVENTS_DATA = [
  { type: "mouse", x: 200, y: 100, expected: { x: 200, y: 100 } },
  { type: "mouse", x: 100, y: 200, expected: { x: 100, y: 200 } },
  { type: "keyboard", key: "VK_LEFT", expected: { x: 99, y: 200 } },
  {
    type: "keyboard",
    key: "VK_LEFT",
    shift: true,
    expected: { x: 89, y: 200 },
  },
  { type: "keyboard", key: "VK_RIGHT", expected: { x: 90, y: 200 } },
  {
    type: "keyboard",
    key: "VK_RIGHT",
    shift: true,
    expected: { x: 100, y: 200 },
  },
  { type: "keyboard", key: "VK_DOWN", expected: { x: 100, y: 201 } },
  {
    type: "keyboard",
    key: "VK_DOWN",
    shift: true,
    expected: { x: 100, y: 211 },
  },
  { type: "keyboard", key: "VK_UP", expected: { x: 100, y: 210 } },
  { type: "keyboard", key: "VK_UP", shift: true, expected: { x: 100, y: 200 } },
  // Mouse initialization for left and top snapping
  { type: "mouse", x: 7, y: 7, expected: { x: 7, y: 7 } },
  // Left Snapping
  {
    type: "keyboard",
    key: "VK_LEFT",
    shift: true,
    expected: { x: 0, y: 7 },
    desc: "Left Snapping to x=0",
  },
  // Top Snapping
  {
    type: "keyboard",
    key: "VK_UP",
    shift: true,
    expected: { x: 0, y: 0 },
    desc: "Top Snapping to y=0",
  },
  // Mouse initialization for right snapping
  {
    type: "mouse",
    x: (width, height) => width - 5,
    y: 0,
    expected: {
      x: (width, height) => width - 5,
      y: 0,
    },
  },
  // Right snapping
  {
    type: "keyboard",
    key: "VK_RIGHT",
    shift: true,
    expected: {
      x: (width, height) => width,
      y: 0,
    },
    desc: "Right snapping to x=max window width available",
  },
  // Mouse initialization for bottom snapping
  {
    type: "mouse",
    x: 0,
    y: (width, height) => height - 5,
    expected: {
      x: 0,
      y: (width, height) => height - 5,
    },
  },
  // Bottom snapping
  {
    type: "keyboard",
    key: "VK_DOWN",
    shift: true,
    expected: {
      x: 0,
      y: (width, height) => height,
    },
    desc: "Bottom snapping to y=max window height available",
  },
];

add_task(async function () {
  const { inspector, highlighterTestFront } = await openInspectorForURL(
    "data:text/html;charset=utf-8," + encodeURIComponent(TEST_URI)
  );
  const helper = await getHighlighterHelperFor(HIGHLIGHTER_TYPE)({
    inspector,
    highlighterTestFront,
  });

  helper.prefix = ID;

  await helper.show("html");
  await respondsToMoveEvents(helper);
  await respondsToReturnAndEscape(helper);

  helper.finalize();
});

async function respondsToMoveEvents(helper) {
  info(
    "Checking that the eyedropper responds to events from the mouse and keyboard"
  );
  const { mouse } = helper;
  const { width, height } = await SpecialPowers.spawn(
    gBrowser.selectedBrowser,
    [],
    () => {
      const rect = content.document
        .querySelector("html")
        .getBoundingClientRect();
      return { width: rect.width, height: rect.height };
    }
  );

  for (let { type, x, y, key, shift, expected, desc } of MOVE_EVENTS_DATA) {
    x = typeof x === "function" ? x(width, height) : x;
    y = typeof y === "function" ? y(width, height) : y;
    expected.x =
      typeof expected.x === "function" ? expected.x(width, height) : expected.x;
    expected.y =
      typeof expected.y === "function" ? expected.y(width, height) : expected.y;

    if (typeof desc === "undefined") {
      info(`Simulating a ${type} event to move to ${expected.x} ${expected.y}`);
    } else {
      info(`Simulating ${type} event: ${desc}`);
    }

    if (type === "mouse") {
      await mouse.move(x, y);
    } else if (type === "keyboard") {
      const options = shift ? { shiftKey: true } : {};
      await EventUtils.synthesizeAndWaitKey(key, options);
    }
    await checkPosition(expected, helper);
  }
}

async function checkPosition({ x, y }, { getElementAttribute }) {
  const style = await getElementAttribute("root", "style");
  is(
    style,
    `top:${y}px;left:${x}px;`,
    `The eyedropper is at the expected ${x} ${y} position`
  );
}

async function respondsToReturnAndEscape({ isElementHidden, show }) {
  info("Simulating return to select the color and hide the eyedropper");

  await EventUtils.synthesizeAndWaitKey("VK_RETURN", {});
  let hidden = await isElementHidden("root");
  ok(hidden, "The eyedropper has been hidden");

  info("Showing the eyedropper again and simulating escape to hide it");

  await show("html");
  await EventUtils.synthesizeAndWaitKey("VK_ESCAPE", {});
  hidden = await isElementHidden("root");
  ok(hidden, "The eyedropper has been hidden again");
}