summaryrefslogtreecommitdiffstats
path: root/accessible/tests/mochitest/layout.js
blob: 1467d5fe7fa88032d262a88f2e5a9779aff9d075 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/* import-globals-from common.js */

/**
 * Tests if the given child and grand child accessibles at the given point are
 * expected.
 *
 * @param aID            [in] accessible identifier
 * @param aX             [in] x coordinate of the point relative accessible
 * @param aY             [in] y coordinate of the point relative accessible
 * @param aChildID       [in] expected child accessible
 * @param aGrandChildID  [in] expected child accessible
 */
function testChildAtPoint(aID, aX, aY, aChildID, aGrandChildID) {
  var child = getChildAtPoint(aID, aX, aY, false);
  var expectedChild = getAccessible(aChildID);

  var msg =
    "Wrong direct child accessible at the point (" +
    aX +
    ", " +
    aY +
    ") of " +
    prettyName(aID);
  isObject(child, expectedChild, msg);

  var grandChild = getChildAtPoint(aID, aX, aY, true);
  var expectedGrandChild = getAccessible(aGrandChildID);

  msg =
    "Wrong deepest child accessible at the point (" +
    aX +
    ", " +
    aY +
    ") of " +
    prettyName(aID);
  isObject(grandChild, expectedGrandChild, msg);
}

/**
 * Test if getChildAtPoint returns the given child and grand child accessibles
 * at coordinates of child accessible (direct and deep hit test).
 */
function hitTest(aContainerID, aChildID, aGrandChildID) {
  var container = getAccessible(aContainerID);
  var child = getAccessible(aChildID);
  var grandChild = getAccessible(aGrandChildID);

  var [x, y] = getBoundsForDOMElm(child);

  var actualChild = container.getChildAtPoint(x + 1, y + 1);
  isObject(
    actualChild,
    child,
    "Wrong direct child of " + prettyName(aContainerID)
  );

  var actualGrandChild = container.getDeepestChildAtPoint(x + 1, y + 1);
  isObject(
    actualGrandChild,
    grandChild,
    "Wrong deepest child of " + prettyName(aContainerID)
  );
}

/**
 * Test if getOffsetAtPoint returns the given text offset at given coordinates.
 */
function testOffsetAtPoint(aHyperTextID, aX, aY, aCoordType, aExpectedOffset) {
  var hyperText = getAccessible(aHyperTextID, [nsIAccessibleText]);
  var offset = hyperText.getOffsetAtPoint(aX, aY, aCoordType);
  is(
    offset,
    aExpectedOffset,
    "Wrong offset at given point (" +
      aX +
      ", " +
      aY +
      ") for " +
      prettyName(aHyperTextID)
  );
}

/**
 * Zoom the given document.
 */
function zoomDocument(aDocument, aZoom) {
  SpecialPowers.setFullZoom(aDocument.defaultView, aZoom);
}

/**
 * Set the relative resolution of this document. This is what apz does.
 * On non-mobile platforms you won't see a visible change.
 */
function setResolution(aDocument, aZoom) {
  var windowUtils = aDocument.defaultView.windowUtils;

  windowUtils.setResolutionAndScaleTo(aZoom);
}

/**
 * Return child accessible at the given point.
 *
 * @param aIdentifier        [in] accessible identifier
 * @param aX                 [in] x coordinate of the point relative accessible
 * @param aY                 [in] y coordinate of the point relative accessible
 * @param aFindDeepestChild  [in] points whether deepest or nearest child should
 *                           be returned
 * @return                   the child accessible at the given point
 */
function getChildAtPoint(aIdentifier, aX, aY, aFindDeepestChild) {
  var acc = getAccessible(aIdentifier);
  if (!acc) {
    return null;
  }

  var [screenX, screenY] = getBoundsForDOMElm(acc.DOMNode);

  var x = screenX + aX;
  var y = screenY + aY;

  try {
    if (aFindDeepestChild) {
      return acc.getDeepestChildAtPoint(x, y);
    }
    return acc.getChildAtPoint(x, y);
  } catch (e) {}

  return null;
}

/**
 * Test the accessible position.
 */
function testPos(aID, aPoint) {
  var [expectedX, expectedY] =
    aPoint != undefined ? aPoint : getBoundsForDOMElm(aID);

  var [x, y] = getBounds(aID);
  is(x, expectedX, "Wrong x coordinate of " + prettyName(aID));
  is(y, expectedY, "Wrong y coordinate of " + prettyName(aID));
}

/**
 * Test the accessible boundaries.
 */
function testBounds(aID, aRect) {
  var [expectedX, expectedY, expectedWidth, expectedHeight] =
    aRect != undefined ? aRect : getBoundsForDOMElm(aID);

  var [x, y, width, height] = getBounds(aID);
  is(x, expectedX, "Wrong x coordinate of " + prettyName(aID));
  is(y, expectedY, "Wrong y coordinate of " + prettyName(aID));
  is(width, expectedWidth, "Wrong width of " + prettyName(aID));
  is(height, expectedHeight, "Wrong height of " + prettyName(aID));
}

/**
 * Test text position at the given offset.
 */
function testTextPos(aID, aOffset, aPoint, aCoordOrigin) {
  var [expectedX, expectedY] = aPoint;

  var xObj = {},
    yObj = {};
  var hyperText = getAccessible(aID, [nsIAccessibleText]);
  hyperText.getCharacterExtents(aOffset, xObj, yObj, {}, {}, aCoordOrigin);
  is(
    xObj.value,
    expectedX,
    "Wrong x coordinate at offset " + aOffset + " for " + prettyName(aID)
  );
  ok(
    yObj.value - expectedY <= 2 && expectedY - yObj.value <= 2,
    "Wrong y coordinate at offset " +
      aOffset +
      " for " +
      prettyName(aID) +
      " - got " +
      yObj.value +
      ", expected " +
      expectedY +
      "The difference doesn't exceed 1."
  );
}

/**
 * Test text bounds that is enclosed betwene the given offsets.
 */
function testTextBounds(aID, aStartOffset, aEndOffset, aRect, aCoordOrigin) {
  var [expectedX, expectedY, expectedWidth, expectedHeight] = aRect;

  var xObj = {},
    yObj = {},
    widthObj = {},
    heightObj = {};
  var hyperText = getAccessible(aID, [nsIAccessibleText]);
  hyperText.getRangeExtents(
    aStartOffset,
    aEndOffset,
    xObj,
    yObj,
    widthObj,
    heightObj,
    aCoordOrigin
  );

  // x
  isWithin(
    expectedX,
    xObj.value,
    1,
    "Wrong x coordinate of text between offsets (" +
      aStartOffset +
      ", " +
      aEndOffset +
      ") for " +
      prettyName(aID)
  );

  // y
  isWithin(
    expectedY,
    yObj.value,
    1,
    `y coord of text between offsets (${aStartOffset}, ${aEndOffset}) ` +
      `for ${prettyName(aID)}`
  );

  // Width
  var msg =
    "Wrong width of text between offsets (" +
    aStartOffset +
    ", " +
    aEndOffset +
    ") for " +
    prettyName(aID) +
    " - Got " +
    widthObj.value +
    " Expected " +
    expectedWidth;
  if (!WIN) {
    isWithin(expectedWidth, widthObj.value, 1, msg);
  } else {
    // fails on some windows machines
    todo(false, msg);
  }

  // Height
  isWithin(
    expectedHeight,
    heightObj.value,
    1,
    `height of text between offsets (${aStartOffset}, ${aEndOffset}) ` +
      `for ${prettyName(aID)}`
  );
}

/**
 * Return the accessible coordinates relative to the screen in device pixels.
 */
function getPos(aID) {
  var accessible = getAccessible(aID);
  var x = {},
    y = {};
  accessible.getBounds(x, y, {}, {});
  return [x.value, y.value];
}

/**
 * Return the accessible coordinates and size relative to the screen in device
 * pixels. This methods also retrieves coordinates in CSS pixels and ensures that they
 * match Dev pixels with a given device pixel ratio.
 */
function getBounds(aID, aDPR = window.devicePixelRatio) {
  const accessible = getAccessible(aID);
  let x = {},
    y = {},
    width = {},
    height = {};
  let xInCSS = {},
    yInCSS = {},
    widthInCSS = {},
    heightInCSS = {};
  accessible.getBounds(x, y, width, height);
  accessible.getBoundsInCSSPixels(xInCSS, yInCSS, widthInCSS, heightInCSS);

  info(`DPR is: ${aDPR}`);
  isWithin(
    xInCSS.value,
    x.value / aDPR,
    1,
    "X in CSS pixels is calculated correctly"
  );
  isWithin(
    yInCSS.value,
    y.value / aDPR,
    1,
    "Y in CSS pixels is calculated correctly"
  );
  isWithin(
    widthInCSS.value,
    width.value / aDPR,
    1,
    "Width in CSS pixels is calculated correctly"
  );
  isWithin(
    heightInCSS.value,
    height.value / aDPR,
    1,
    "Height in CSS pixels is calculated correctly"
  );

  return [x.value, y.value, width.value, height.value];
}

function getRangeExtents(aID, aStartOffset, aEndOffset, aCoordOrigin) {
  var hyperText = getAccessible(aID, [nsIAccessibleText]);
  var x = {},
    y = {},
    width = {},
    height = {};
  hyperText.getRangeExtents(
    aStartOffset,
    aEndOffset,
    x,
    y,
    width,
    height,
    aCoordOrigin
  );
  return [x.value, y.value, width.value, height.value];
}

/**
 * Return DOM node coordinates relative the screen and its size in device
 * pixels.
 */
function getBoundsForDOMElm(aID) {
  var x = 0,
    y = 0,
    width = 0,
    height = 0;

  var elm = getNode(aID);
  if (elm.localName == "area") {
    var mapName = elm.parentNode.getAttribute("name");
    var selector = "[usemap='#" + mapName + "']";
    var img = elm.ownerDocument.querySelector(selector);

    var areaCoords = elm.coords.split(",");
    var areaX = parseInt(areaCoords[0]);
    var areaY = parseInt(areaCoords[1]);
    var areaWidth = parseInt(areaCoords[2]) - areaX;
    var areaHeight = parseInt(areaCoords[3]) - areaY;

    let rect = img.getBoundingClientRect();
    x = rect.left + areaX;
    y = rect.top + areaY;
    width = areaWidth;
    height = areaHeight;
  } else {
    let rect = elm.getBoundingClientRect();
    x = rect.left;
    y = rect.top;
    width = rect.width;
    height = rect.height;
  }

  var elmWindow = elm.ownerGlobal;
  return CSSToDevicePixels(
    elmWindow,
    x + elmWindow.mozInnerScreenX,
    y + elmWindow.mozInnerScreenY,
    width,
    height
  );
}

function CSSToDevicePixels(aWindow, aX, aY, aWidth, aHeight) {
  var ratio = aWindow.devicePixelRatio;

  // CSS pixels and ratio can be not integer. Device pixels are always integer.
  // Do our best and hope it works.
  return [
    Math.round(aX * ratio),
    Math.round(aY * ratio),
    Math.round(aWidth * ratio),
    Math.round(aHeight * ratio),
  ];
}