summaryrefslogtreecommitdiffstats
path: root/accessible/tests/mochitest/attributes.js
blob: 2621e2407686e35b39a14b2acd2a065b8f91e91b (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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/* import-globals-from common.js */

// //////////////////////////////////////////////////////////////////////////////
// Object attributes.

/**
 * Test object attributes.
 *
 * @param aAccOrElmOrID         [in] the accessible identifier
 * @param aAttrs                [in] the map of expected object attributes
 *                              (name/value pairs)
 * @param aSkipUnexpectedAttrs  [in] points this function doesn't fail if
 *                              unexpected attribute is encountered
 * @param aTodo                 [in] true if this is a 'todo'
 */
function testAttrs(aAccOrElmOrID, aAttrs, aSkipUnexpectedAttrs, aTodo) {
  testAttrsInternal(aAccOrElmOrID, aAttrs, aSkipUnexpectedAttrs, null, aTodo);
}

/**
 * Test object attributes that must not be present.
 *
 * @param aAccOrElmOrID         [in] the accessible identifier
 * @param aAbsentAttrs          [in] map of attributes that should not be
 *                              present (name/value pairs)
 * @param aTodo                 [in] true if this is a 'todo'
 */
function testAbsentAttrs(aAccOrElmOrID, aAbsentAttrs, aTodo) {
  testAttrsInternal(aAccOrElmOrID, {}, true, aAbsentAttrs, aTodo);
}

/**
 * Test object attributes that aren't right, but should be (todo)
 *
 * @param aAccOrElmOrID         [in] the accessible identifier
 * @param aKey                  [in] attribute name
 * @param aExpectedValue        [in] expected attribute value
 */
function todoAttr(aAccOrElmOrID, aKey, aExpectedValue) {
  testAttrs(
    aAccOrElmOrID,
    Object.fromEntries([[aKey, aExpectedValue]]),
    true,
    true
  );
}

/**
 * Test CSS based object attributes.
 */
function testCSSAttrs(aID) {
  var node = document.getElementById(aID);
  var computedStyle = document.defaultView.getComputedStyle(node);

  var attrs = {
    display: computedStyle.display,
    "text-align": computedStyle.textAlign,
    "text-indent": computedStyle.textIndent,
    "margin-left": computedStyle.marginLeft,
    "margin-right": computedStyle.marginRight,
    "margin-top": computedStyle.marginTop,
    "margin-bottom": computedStyle.marginBottom,
  };
  testAttrs(aID, attrs, true);
}

/**
 * Test the accessible that it doesn't have CSS-based object attributes.
 */
function testAbsentCSSAttrs(aID) {
  var attrs = {
    display: "",
    "text-align": "",
    "text-indent": "",
    "margin-left": "",
    "margin-right": "",
    "margin-top": "",
    "margin-bottom": "",
  };
  testAbsentAttrs(aID, attrs);
}

/**
 * Test group object attributes (posinset, setsize and level) and
 * nsIAccessible::groupPosition() method.
 *
 * @param aAccOrElmOrID  [in] the ID, DOM node or accessible
 * @param aPosInSet      [in] the value of 'posinset' attribute
 * @param aSetSize       [in] the value of 'setsize' attribute
 * @param aLevel         [in, optional] the value of 'level' attribute
 */
function testGroupAttrs(aAccOrElmOrID, aPosInSet, aSetSize, aLevel, aTodo) {
  var acc = getAccessible(aAccOrElmOrID);
  var levelObj = {},
    posInSetObj = {},
    setSizeObj = {};
  acc.groupPosition(levelObj, setSizeObj, posInSetObj);

  let groupPos = {},
    expectedGroupPos = {};

  if (aPosInSet && aSetSize) {
    groupPos.setsize = String(setSizeObj.value);
    groupPos.posinset = String(posInSetObj.value);

    expectedGroupPos.setsize = String(aSetSize);
    expectedGroupPos.posinset = String(aPosInSet);
  }

  if (aLevel) {
    groupPos.level = String(levelObj.value);

    expectedGroupPos.level = String(aLevel);
  }

  compareSimpleObjects(
    groupPos,
    expectedGroupPos,
    false,
    "wrong groupPos",
    aTodo
  );

  testAttrs(aAccOrElmOrID, expectedGroupPos, true, aTodo);
}

function testGroupParentAttrs(
  aAccOrElmOrID,
  aChildItemCount,
  aIsHierarchical,
  aTodo
) {
  testAttrs(
    aAccOrElmOrID,
    { "child-item-count": String(aChildItemCount) },
    true,
    aTodo
  );

  if (aIsHierarchical) {
    testAttrs(aAccOrElmOrID, { tree: "true" }, true, aTodo);
  } else {
    testAbsentAttrs(aAccOrElmOrID, { tree: "true" });
  }
}

// //////////////////////////////////////////////////////////////////////////////
// Text attributes.

/**
 * Test text attributes.
 *
 * @param aID                   [in] the ID of DOM element having text
 *                              accessible
 * @param aOffset               [in] the offset inside text accessible to fetch
 *                              text attributes
 * @param aAttrs                [in] the map of expected text attributes
 *                              (name/value pairs) exposed at the offset
 * @param aDefAttrs             [in] the map of expected text attributes
 *                              (name/value pairs) exposed on hyper text
 *                              accessible
 * @param aStartOffset          [in] expected start offset where text attributes
 *                              are applied
 * @param aEndOffset            [in] expected end offset where text attribute
 *                              are applied
 * @param aSkipUnexpectedAttrs  [in] points the function doesn't fail if
 *                              unexpected attribute is encountered
 */
function testTextAttrs(
  aID,
  aOffset,
  aAttrs,
  aDefAttrs,
  aStartOffset,
  aEndOffset,
  aSkipUnexpectedAttrs
) {
  var accessible = getAccessible(aID, [nsIAccessibleText]);
  if (!accessible) {
    return;
  }

  var startOffset = { value: -1 };
  var endOffset = { value: -1 };

  // do not include attributes exposed on hyper text accessible
  var attrs = getTextAttributes(
    aID,
    accessible,
    false,
    aOffset,
    startOffset,
    endOffset
  );

  if (!attrs) {
    return;
  }

  var errorMsg = " for " + aID + " at offset " + aOffset;

  is(startOffset.value, aStartOffset, "Wrong start offset" + errorMsg);
  is(endOffset.value, aEndOffset, "Wrong end offset" + errorMsg);

  compareAttrs(errorMsg, attrs, aAttrs, aSkipUnexpectedAttrs);

  // include attributes exposed on hyper text accessible
  var expectedAttrs = {};
  for (let name in aAttrs) {
    expectedAttrs[name] = aAttrs[name];
  }

  for (let name in aDefAttrs) {
    if (!(name in expectedAttrs)) {
      expectedAttrs[name] = aDefAttrs[name];
    }
  }

  attrs = getTextAttributes(
    aID,
    accessible,
    true,
    aOffset,
    startOffset,
    endOffset
  );

  if (!attrs) {
    return;
  }

  compareAttrs(errorMsg, attrs, expectedAttrs, aSkipUnexpectedAttrs);
}

/**
 * Test default text attributes.
 *
 * @param aID                   [in] the ID of DOM element having text
 *                              accessible
 * @param aDefAttrs             [in] the map of expected text attributes
 *                              (name/value pairs)
 * @param aSkipUnexpectedAttrs  [in] points the function doesn't fail if
 *                              unexpected attribute is encountered
 */
function testDefaultTextAttrs(aID, aDefAttrs, aSkipUnexpectedAttrs) {
  var accessible = getAccessible(aID, [nsIAccessibleText]);
  if (!accessible) {
    return;
  }

  var defAttrs = null;
  try {
    defAttrs = accessible.defaultTextAttributes;
  } catch (e) {}

  if (!defAttrs) {
    ok(false, "Can't get default text attributes for " + aID);
    return;
  }

  var errorMsg = ". Getting default text attributes for " + aID;
  compareAttrs(errorMsg, defAttrs, aDefAttrs, aSkipUnexpectedAttrs);
}

/**
 * Test text attributes for wrong offset.
 */
function testTextAttrsWrongOffset(aID, aOffset) {
  var res = false;
  try {
    var s = {},
      e = {};
    // Bug 1602031
    // eslint-disable-next-line no-undef
    var acc = getAccessible(ID, [nsIAccessibleText]);
    acc.getTextAttributes(false, 157, s, e);
  } catch (ex) {
    res = true;
  }

  ok(
    res,
    "text attributes are calculated successfully at wrong offset " +
      aOffset +
      " for " +
      prettyName(aID)
  );
}

const kNormalFontWeight = function equalsToNormal(aWeight) {
  return aWeight <= 400;
};

const kBoldFontWeight = function equalsToBold(aWeight) {
  return aWeight > 400;
};

let isNNT = SpecialPowers.getBoolPref("widget.non-native-theme.enabled");
// The pt font size of the input element can vary by Linux distro.
const kInputFontSize =
  WIN || (MAC && isNNT)
    ? "10pt"
    : MAC
    ? "8pt"
    : function() {
        return true;
      };

const kAbsentFontFamily = function(aFontFamily) {
  return aFontFamily != "sans-serif";
};
const kInputFontFamily = function(aFontFamily) {
  return aFontFamily != "sans-serif";
};

const kMonospaceFontFamily = function(aFontFamily) {
  return aFontFamily != "monospace";
};
const kSansSerifFontFamily = function(aFontFamily) {
  return aFontFamily != "sans-serif";
};
const kSerifFontFamily = function(aFontFamily) {
  return aFontFamily != "serif";
};

const kCursiveFontFamily = LINUX ? "DejaVu Serif" : "Comic Sans MS";

/**
 * Return used font from the given computed style.
 */
function fontFamily(aComputedStyle) {
  var name = aComputedStyle.fontFamily;
  switch (name) {
    case "monospace":
      return kMonospaceFontFamily;
    case "sans-serif":
      return kSansSerifFontFamily;
    case "serif":
      return kSerifFontFamily;
    default:
      return name;
  }
}

/**
 * Returns a computed system color for this document.
 */
function getSystemColor(aColor) {
  let { r, g, b, a } = InspectorUtils.colorToRGBA(aColor, document);
  return a == 1 ? `rgb(${r}, ${g}, ${b})` : `rgba(${r}, ${g}, ${b}, ${a})`;
}

/**
 * Build an object of default text attributes expected for the given accessible.
 *
 * @param aID          [in] identifier of accessible
 * @param aFontSize    [in] font size
 * @param aFontWeight  [in, optional] kBoldFontWeight or kNormalFontWeight,
 *                      default value is kNormalFontWeight
 */
function buildDefaultTextAttrs(aID, aFontSize, aFontWeight, aFontFamily) {
  var elm = getNode(aID);
  var computedStyle = document.defaultView.getComputedStyle(elm);
  var bgColor =
    computedStyle.backgroundColor == "rgba(0, 0, 0, 0)"
      ? getSystemColor("Canvas")
      : computedStyle.backgroundColor;

  var defAttrs = {
    "font-style": computedStyle.fontStyle,
    "font-size": aFontSize,
    "background-color": bgColor,
    "font-weight": aFontWeight ? aFontWeight : kNormalFontWeight,
    color: computedStyle.color,
    "font-family": aFontFamily ? aFontFamily : fontFamily(computedStyle),
    "text-position": computedStyle.verticalAlign,
  };

  return defAttrs;
}

// //////////////////////////////////////////////////////////////////////////////
// Private.

function getTextAttributes(
  aID,
  aAccessible,
  aIncludeDefAttrs,
  aOffset,
  aStartOffset,
  aEndOffset
) {
  // This function expects the passed in accessible to already be queried for
  // nsIAccessibleText.
  var attrs = null;
  try {
    attrs = aAccessible.getTextAttributes(
      aIncludeDefAttrs,
      aOffset,
      aStartOffset,
      aEndOffset
    );
  } catch (e) {}

  if (attrs) {
    return attrs;
  }

  ok(false, "Can't get text attributes for " + aID);
  return null;
}

function testAttrsInternal(
  aAccOrElmOrID,
  aAttrs,
  aSkipUnexpectedAttrs,
  aAbsentAttrs,
  aTodo
) {
  var accessible = getAccessible(aAccOrElmOrID);
  if (!accessible) {
    return;
  }

  var attrs = null;
  try {
    attrs = accessible.attributes;
  } catch (e) {}

  if (!attrs) {
    ok(false, "Can't get object attributes for " + prettyName(aAccOrElmOrID));
    return;
  }

  var errorMsg = " for " + prettyName(aAccOrElmOrID);
  compareAttrs(
    errorMsg,
    attrs,
    aAttrs,
    aSkipUnexpectedAttrs,
    aAbsentAttrs,
    aTodo
  );
}

function compareAttrs(
  aErrorMsg,
  aAttrs,
  aExpectedAttrs,
  aSkipUnexpectedAttrs,
  aAbsentAttrs,
  aTodo
) {
  // Check if all obtained attributes are expected and have expected value.
  let attrObject = {};
  for (let prop of aAttrs.enumerate()) {
    attrObject[prop.key] = prop.value;
  }

  // Create expected attributes set by using the return values from
  // embedded functions to determine the entry's value.
  let expectedObj = Object.fromEntries(
    Object.entries(aExpectedAttrs).map(([k, v]) => {
      if (v instanceof Function) {
        // If value is a function that returns true given the received
        // attribute value, assign the attribute value to the entry.
        // If it is false, stringify the function for good error reporting.
        let value = v(attrObject[k]) ? attrObject[k] : v.toString();
        return [k, value];
      }

      return [k, v];
    })
  );

  compareSimpleObjects(
    attrObject,
    expectedObj,
    aSkipUnexpectedAttrs,
    aErrorMsg,
    aTodo
  );

  // Check if all unexpected attributes are absent.
  if (aAbsentAttrs) {
    let presentAttrs = Object.keys(attrObject).filter(
      k => aAbsentAttrs[k] !== undefined
    );
    if (presentAttrs.length) {
      (aTodo ? todo : ok)(
        false,
        `There were unexpected attributes: ${presentAttrs}`
      );
    }
  }
}

function compareSimpleObjects(
  aObj,
  aExpectedObj,
  aSkipUnexpectedAttrs,
  aMessage,
  aTodo
) {
  let keys = aSkipUnexpectedAttrs
    ? Object.keys(aExpectedObj).sort()
    : Object.keys(aObj).sort();
  let o1 = JSON.stringify(aObj, keys);
  let o2 = JSON.stringify(aExpectedObj, keys);

  if (aTodo) {
    todo_is(o1, o2, `${aMessage} - Got ${o1}, expected ${o2}`);
  } else {
    is(o1, o2, aMessage);
  }
}