summaryrefslogtreecommitdiffstats
path: root/accessible/tests/mochitest/name/markup.js
blob: d3ecd8982b082437aa1dd51ba6ef9eae2425fc67 (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
////////////////////////////////////////////////////////////////////////////////
// Name tests described by "markuprules.xml" file.

var gNameRulesFileURL = "markuprules.xml";

var gRuleDoc = null;

// Debuggin stuff.
var gDumpToConsole = false;

/**
 * Start name tests. Run through markup elements and test names for test
 * element (see namerules.xml for details).
 */
function testNames()
{
  //enableLogging("tree,stack"); // debugging

  var request = new XMLHttpRequest();
  request.open("get", gNameRulesFileURL, false);
  request.send();

  gRuleDoc = request.responseXML;

  var markupElms = evaluateXPath(gRuleDoc, "//rules/rulesample/markup");
  gTestIterator.iterateMarkups(markupElms);
}

////////////////////////////////////////////////////////////////////////////////
// Private section.

/**
 * Helper class to interate through name tests.
 */
var gTestIterator =
{
  iterateMarkups: function gTestIterator_iterateMarkups(aMarkupElms)
  {
    this.markupElms = aMarkupElms;

    this.iterateNext();
  },

  iterateRules: function gTestIterator_iterateRules(aElm, aContainer,
                                                    aRuleSetElm, aRuleElms,
                                                    aTestID)
  {
    this.ruleSetElm = aRuleSetElm;
    this.ruleElms = aRuleElms;
    this.elm = aElm;
    this.container = aContainer;
    this.testID = aTestID;

    this.iterateNext();
  },

  iterateNext: function gTestIterator_iterateNext()
  {
    if (this.markupIdx == -1) {
      this.markupIdx++;
      testNamesForMarkup(this.markupElms[this.markupIdx]);
      return;
    }

    this.ruleIdx++;
    if (this.ruleIdx == this.ruleElms.length) {
      // When test is finished then name is empty and no explict-name.
      var defaultName = this.ruleSetElm.hasAttribute("defaultName") ?
        this.ruleSetElm.getAttribute("defaultName") : null;
      testName(this.elm, defaultName,
               "Default name test (" + gTestIterator.testID + "). ");
      testAbsentAttrs(this.elm, {"explicit-name" : "true"});

      this.markupIdx++;
      if (this.markupIdx == this.markupElms.length) {
        //disableLogging("tree"); // debugging
        SimpleTest.finish();
        return;
      }

      this.ruleIdx = -1;

      if (gDumpToConsole) {
        dump("\nPend next markup processing. Wait for reorder event on " +
             prettyName(document) + "'\n");
      }
      waitForEvent(EVENT_REORDER, document, testNamesForMarkup,
                   null, this.markupElms[this.markupIdx]);

      document.body.removeChild(this.container);
      return;
    }

    testNameForRule(this.elm, this.ruleElms[this.ruleIdx]);
  },

  markupElms: null,
  markupIdx: -1,
  rulesetElm: null,
  ruleElms: null,
  ruleIdx: -1,
  elm: null,
  container: null,
  testID: ""
};

/**
 * Process every 'markup' element and test names for it. Used by testNames
 * function.
 */
function testNamesForMarkup(aMarkupElm)
{
  if (gDumpToConsole)
    dump("\nProcessing markup '" + aMarkupElm.getAttribute("id") + "'\n");

  var div = document.createElement("div");
  div.setAttribute("id", "test");

  var child = aMarkupElm.firstChild;
  while (child) {
    var newChild = document.importNode(child, true);
    div.appendChild(newChild);
    child = child.nextSibling;
  }

  if (gDumpToConsole) {
    dump("\nProcessing markup. Wait for reorder event on " +
         prettyName(document) + "'\n");
  }
  waitForEvent(EVENT_REORDER, document, testNamesForMarkupRules,
                null, aMarkupElm, div);

  document.body.appendChild(div);
}

function testNamesForMarkupRules(aMarkupElm, aContainer)
{
  var testID = aMarkupElm.getAttribute("id");
  if (gDumpToConsole)
    dump("\nProcessing markup rules '" + testID + "'\n");

  var serializer = new XMLSerializer();

  var expr = "//html/body/div[@id='test']/" + aMarkupElm.getAttribute("ref");
  var elm = evaluateXPath(document, expr, htmlDocResolver)[0];

  var ruleId = aMarkupElm.getAttribute("ruleset");
  var ruleElm = gRuleDoc.querySelector("[id='" + ruleId + "']");
  var ruleElms = getRuleElmsByRulesetId(ruleId);

  var processMarkupRules =
    gTestIterator.iterateRules.bind(gTestIterator, elm, aContainer,
                                    ruleElm, ruleElms, testID);

  // Images may be recreated after we append them into subtree. We need to wait
  // in this case. If we are on profiling enabled build then stack tracing
  // works and thus let's log instead. Note, that works if you enabled logging
  // (refer to testNames() function).
  if (isAccessible(elm) || isLogged("stack"))
    processMarkupRules();
  else
    waitForEvent(EVENT_SHOW, elm, processMarkupRules);
}

/**
 * Test name for current rule and current 'markup' element. Used by
 * testNamesForMarkup function.
 */
function testNameForRule(aElm, aRuleElm)
{
  if (aRuleElm.hasAttribute("attr")) {
    if (gDumpToConsole) {
      dump("\nProcessing rule { attr: " + aRuleElm.getAttribute("attr") +" }\n");
    }

    testNameForAttrRule(aElm, aRuleElm);

  } else if (aRuleElm.hasAttribute("elm")) {
    if (gDumpToConsole) {
      dump("\nProcessing rule { elm: " + aRuleElm.getAttribute("elm") +
           ", elmattr: " + aRuleElm.getAttribute("elmattr") +" }\n");
    }

    testNameForElmRule(aElm, aRuleElm);

  } else if (aRuleElm.getAttribute("fromsubtree") == "true") {
    if (gDumpToConsole) {
      dump("\nProcessing rule { fromsubtree: " +
           aRuleElm.getAttribute("fromsubtree") +" }\n");
    }

    testNameForSubtreeRule(aElm, aRuleElm);
  }
}

function testNameForAttrRule(aElm, aRule)
{
  var name = "";

  var attr = aRule.getAttribute("attr");
  var attrValue = aElm.getAttribute(attr);

  var type = aRule.getAttribute("type");
  if (type == "string") {
    name = attrValue;

  } else if (type == "ref" && attrValue) {
    var ids = attrValue.split(/\s+/);
    for (var idx = 0; idx < ids.length; idx++) {
      var labelElm = getNode(ids[idx]);
      if (name != "")
        name += " ";

      name += labelElm.getAttribute("textequiv");
    }
  }

  var msg = "Attribute '" + attr + "' test (" + gTestIterator.testID + "). ";
  testName(aElm, name, msg);

  if (aRule.getAttribute("explict-name") != "false")
    testAttrs(aElm, {"explicit-name" : "true"}, true);
  else
    testAbsentAttrs(aElm, {"explicit-name" : "true"});

  // If @recreated attribute is used then this attribute change recreates an
  // accessible. Wait for reorder event in this case or otherwise proceed next
  // test immediately.
  if (aRule.hasAttribute("recreated")) {
    waitForEvent(EVENT_REORDER, aElm.parentNode,
                 gTestIterator.iterateNext, gTestIterator);
    aElm.removeAttribute(attr);

  } else if (aRule.hasAttribute("textchanged")) {
    waitForEvent(EVENT_TEXT_INSERTED, aElm,
                 gTestIterator.iterateNext, gTestIterator);
    aElm.removeAttribute(attr);

  } else if (aRule.hasAttribute("contentchanged")) {
    waitForEvent(EVENT_REORDER, aElm,
                 gTestIterator.iterateNext, gTestIterator);
    aElm.removeAttribute(attr);

  } else {
    aElm.removeAttribute(attr);
    gTestIterator.iterateNext();
  }
}

function testNameForElmRule(aElm, aRule)
{
  var labelElm;

  var tagname = aRule.getAttribute("elm");
  var attrname = aRule.getAttribute("elmattr");
  if (attrname) {
    var filter = {
      acceptNode: function filter_acceptNode(aNode)
      {
        if (aNode.localName == this.mLocalName &&
            aNode.getAttribute(this.mAttrName) == this.mAttrValue)
          return NodeFilter.FILTER_ACCEPT;

        return NodeFilter.FILTER_SKIP;
      },

      mLocalName: tagname,
      mAttrName: attrname,
      mAttrValue: aElm.getAttribute("id")
    };

    var treeWalker = document.createTreeWalker(document.body,
                                               NodeFilter.SHOW_ELEMENT,
                                               filter);
    labelElm = treeWalker.nextNode();

  } else {
    // if attrname is empty then look for the element in subtree.
    labelElm = aElm.getElementsByTagName(tagname)[0];
    if (!labelElm)
      labelElm = aElm.getElementsByTagName("html:" + tagname)[0];
  }

  if (!labelElm) {
    ok(false, msg + " Failed to find '" + tagname + "' element.");
    gTestIterator.iterateNext();
    return;
  }

  var msg = "Element '" + tagname + "' test (" + gTestIterator.testID + ").";
  testName(aElm, labelElm.getAttribute("textequiv"), msg);
  testAttrs(aElm, {"explicit-name" : "true"}, true);

  var parentNode = labelElm.parentNode;

  if (gDumpToConsole) {
    dump("\nProcessed elm rule. Wait for reorder event on " +
         prettyName(parentNode) + "\n");
  }
  waitForEvent(EVENT_REORDER, parentNode,
               gTestIterator.iterateNext, gTestIterator);

  parentNode.removeChild(labelElm);
}

function testNameForSubtreeRule(aElm, aRule)
{
  var msg = "From subtree test (" + gTestIterator.testID + ").";
  testName(aElm, aElm.getAttribute("textequiv"), msg);
  testAbsentAttrs(aElm, {"explicit-name" : "true"});

  if (gDumpToConsole) {
    dump("\nProcessed from subtree rule. Wait for reorder event on " +
         prettyName(aElm) + "\n");
  }
  waitForEvent(EVENT_REORDER, aElm, gTestIterator.iterateNext, gTestIterator);

  while (aElm.firstChild)
    aElm.removeChild(aElm.firstChild);
}

/**
 * Return array of 'rule' elements. Used in conjunction with
 * getRuleElmsFromRulesetElm() function.
 */
function getRuleElmsByRulesetId(aRulesetId)
{
  var expr = "//rules/ruledfn/ruleset[@id='" + aRulesetId + "']";
  var rulesetElm = evaluateXPath(gRuleDoc, expr);
  return getRuleElmsFromRulesetElm(rulesetElm[0]);
}

function getRuleElmsFromRulesetElm(aRulesetElm)
{
  var rulesetId = aRulesetElm.getAttribute("ref");
  if (rulesetId)
    return getRuleElmsByRulesetId(rulesetId);

  var ruleElms = [];

  var child = aRulesetElm.firstChild;
  while (child) {
    if (child.localName == "ruleset")
      ruleElms = ruleElms.concat(getRuleElmsFromRulesetElm(child));
    if (child.localName == "rule")
      ruleElms.push(child);

    child = child.nextSibling;
  }

  return ruleElms;
}

/**
 * Helper method to evaluate xpath expression.
 */
function evaluateXPath(aNode, aExpr, aResolver)
{
  var xpe = new XPathEvaluator();

  var resolver = aResolver;
  if (!resolver) {
    var node = aNode.ownerDocument == null ?
      aNode.documentElement : aNode.ownerDocument.documentElement;
    resolver = xpe.createNSResolver(node);
  }

  var result = xpe.evaluate(aExpr, aNode, resolver, 0, null);
  var found = [];
  var res;
  while (res = result.iterateNext())
    found.push(res);

  return found;
}

function htmlDocResolver(aPrefix) {
  var ns = {
    'html' : 'http://www.w3.org/1999/xhtml'
  };
  return ns[aPrefix] || null;
}