1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/accessible/tests/mochitest/jsat/output.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,97 @@ 1.4 +const Cu = Components.utils; 1.5 +const PREF_UTTERANCE_ORDER = "accessibility.accessfu.utterance"; 1.6 + 1.7 +Cu.import('resource://gre/modules/accessibility/Utils.jsm'); 1.8 +Cu.import("resource://gre/modules/accessibility/OutputGenerator.jsm", this); 1.9 + 1.10 +/** 1.11 + * Test context output generation. 1.12 + * 1.13 + * @param expected {Array} expected output. 1.14 + * @param aAccOrElmOrID identifier to get an accessible to test. 1.15 + * @param aOldAccOrElmOrID optional identifier to get an accessible relative to 1.16 + * the |aAccOrElmOrID|. 1.17 + * @param aGenerator the output generator to use when generating accessible 1.18 + * output 1.19 + * 1.20 + * Note: if |aOldAccOrElmOrID| is not provided, the |aAccOrElmOrID| must be 1.21 + * scoped to the "root" element in markup. 1.22 + */ 1.23 +function testContextOutput(expected, aAccOrElmOrID, aOldAccOrElmOrID, aGenerator) { 1.24 + var accessible = getAccessible(aAccOrElmOrID); 1.25 + var oldAccessible = aOldAccOrElmOrID !== null ? 1.26 + getAccessible(aOldAccOrElmOrID || 'root') : null; 1.27 + var context = new PivotContext(accessible, oldAccessible); 1.28 + var output = aGenerator.genForContext(context).output; 1.29 + 1.30 + // Create a version of the output that has null members where we have 1.31 + // null members in the expected output. Those are indexes that are not testable 1.32 + // because of the changing nature of the test (different window names), or strings 1.33 + // that are inaccessible to us, like the title of parent documents. 1.34 + var masked_output = []; 1.35 + for (var i=0; i < output.length; i++) { 1.36 + if (expected[i] === null) { 1.37 + masked_output.push(null); 1.38 + } else { 1.39 + masked_output[i] = output[i]; 1.40 + } 1.41 + } 1.42 + 1.43 + isDeeply(masked_output, expected, 1.44 + "Context output is correct for " + aAccOrElmOrID + 1.45 + " (output: " + output.join(", ") + ") ==" + 1.46 + " (expected: " + expected.join(", ") + ")"); 1.47 +} 1.48 + 1.49 +/** 1.50 + * Test object output generated array that includes names. 1.51 + * Note: test ignores outputs without the name. 1.52 + * 1.53 + * @param aAccOrElmOrID identifier to get an accessible to test. 1.54 + * @param aGenerator the output generator to use when generating accessible 1.55 + * output 1.56 + */ 1.57 +function testObjectOutput(aAccOrElmOrID, aGenerator) { 1.58 + var accessible = getAccessible(aAccOrElmOrID); 1.59 + var context = new PivotContext(accessible); 1.60 + var output = aGenerator.genForObject(accessible, context); 1.61 + var outputOrder; 1.62 + try { 1.63 + outputOrder = SpecialPowers.getIntPref(PREF_UTTERANCE_ORDER); 1.64 + } catch (ex) { 1.65 + // PREF_UTTERANCE_ORDER not set. 1.66 + outputOrder = 0; 1.67 + } 1.68 + var expectedNameIndex = outputOrder === 0 ? output.length - 1 : 0; 1.69 + var nameIndex = output.indexOf(accessible.name); 1.70 + 1.71 + if (nameIndex > -1) { 1.72 + ok(output.indexOf(accessible.name) === expectedNameIndex, 1.73 + "Object output is correct for " + aAccOrElmOrID); 1.74 + } 1.75 +} 1.76 + 1.77 +/** 1.78 + * Test object and context output for an accessible. 1.79 + * 1.80 + * @param expected {Array} expected output. 1.81 + * @param aAccOrElmOrID identifier to get an accessible to test. 1.82 + * @param aOldAccOrElmOrID optional identifier to get an accessible relative to 1.83 + * the |aAccOrElmOrID|. 1.84 + * @param aOutputKind the type of output 1.85 + */ 1.86 +function testOutput(expected, aAccOrElmOrID, aOldAccOrElmOrID, aOutputKind) { 1.87 + var generator; 1.88 + if (aOutputKind === 1) { 1.89 + generator = UtteranceGenerator; 1.90 + } else { 1.91 + generator = BrailleGenerator; 1.92 + } 1.93 + testContextOutput(expected, aAccOrElmOrID, aOldAccOrElmOrID, generator); 1.94 + // Just need to test object output for individual 1.95 + // accOrElmOrID. 1.96 + if (aOldAccOrElmOrID) { 1.97 + return; 1.98 + } 1.99 + testObjectOutput(aAccOrElmOrID, generator); 1.100 +}