accessible/tests/mochitest/relations.js

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

michael@0 1 ////////////////////////////////////////////////////////////////////////////////
michael@0 2 // Constants
michael@0 3
michael@0 4 const RELATION_CONTROLLED_BY = nsIAccessibleRelation.RELATION_CONTROLLED_BY;
michael@0 5 const RELATION_CONTROLLER_FOR = nsIAccessibleRelation.RELATION_CONTROLLER_FOR;
michael@0 6 const RELATION_DEFAULT_BUTTON = nsIAccessibleRelation.RELATION_DEFAULT_BUTTON;
michael@0 7 const RELATION_DESCRIBED_BY = nsIAccessibleRelation.RELATION_DESCRIBED_BY;
michael@0 8 const RELATION_DESCRIPTION_FOR = nsIAccessibleRelation.RELATION_DESCRIPTION_FOR;
michael@0 9 const RELATION_EMBEDDED_BY = nsIAccessibleRelation.RELATION_EMBEDDED_BY;
michael@0 10 const RELATION_EMBEDS = nsIAccessibleRelation.RELATION_EMBEDS;
michael@0 11 const RELATION_FLOWS_FROM = nsIAccessibleRelation.RELATION_FLOWS_FROM;
michael@0 12 const RELATION_FLOWS_TO = nsIAccessibleRelation.RELATION_FLOWS_TO;
michael@0 13 const RELATION_LABEL_FOR = nsIAccessibleRelation.RELATION_LABEL_FOR;
michael@0 14 const RELATION_LABELLED_BY = nsIAccessibleRelation.RELATION_LABELLED_BY;
michael@0 15 const RELATION_MEMBER_OF = nsIAccessibleRelation.RELATION_MEMBER_OF;
michael@0 16 const RELATION_NODE_CHILD_OF = nsIAccessibleRelation.RELATION_NODE_CHILD_OF;
michael@0 17 const RELATION_NODE_PARENT_OF = nsIAccessibleRelation.RELATION_NODE_PARENT_OF;
michael@0 18 const RELATION_PARENT_WINDOW_OF = nsIAccessibleRelation.RELATION_PARENT_WINDOW_OF;
michael@0 19 const RELATION_POPUP_FOR = nsIAccessibleRelation.RELATION_POPUP_FOR;
michael@0 20 const RELATION_SUBWINDOW_OF = nsIAccessibleRelation.RELATION_SUBWINDOW_OF;
michael@0 21 const RELATION_CONTAINING_DOCUMENT = nsIAccessibleRelation.RELATION_CONTAINING_DOCUMENT;
michael@0 22 const RELATION_CONTAINING_TAB_PANE = nsIAccessibleRelation.RELATION_CONTAINING_TAB_PANE;
michael@0 23 const RELATION_CONTAINING_APPLICATION = nsIAccessibleRelation.RELATION_CONTAINING_APPLICATION;
michael@0 24
michael@0 25 ////////////////////////////////////////////////////////////////////////////////
michael@0 26 // General
michael@0 27
michael@0 28 /**
michael@0 29 * Test the accessible relation.
michael@0 30 *
michael@0 31 * @param aIdentifier [in] identifier to get an accessible, may be ID
michael@0 32 * attribute or DOM element or accessible object
michael@0 33 * @param aRelType [in] relation type (see constants above)
michael@0 34 * @param aRelatedIdentifiers [in] identifier or array of identifiers of
michael@0 35 * expected related accessibles
michael@0 36 */
michael@0 37 function testRelation(aIdentifier, aRelType, aRelatedIdentifiers)
michael@0 38 {
michael@0 39 var relation = getRelationByType(aIdentifier, aRelType);
michael@0 40
michael@0 41 var relDescr = getRelationErrorMsg(aIdentifier, aRelType);
michael@0 42 var relDescrStart = getRelationErrorMsg(aIdentifier, aRelType, true);
michael@0 43
michael@0 44 if (!relation || !relation.targetsCount) {
michael@0 45 if (!aRelatedIdentifiers) {
michael@0 46 ok(true, "No" + relDescr);
michael@0 47 return;
michael@0 48 }
michael@0 49
michael@0 50 var msg = relDescrStart + "has no expected targets: '" +
michael@0 51 prettyName(aRelatedIdentifiers) + "'";
michael@0 52
michael@0 53 ok(false, msg);
michael@0 54 return;
michael@0 55
michael@0 56 } else if (!aRelatedIdentifiers) {
michael@0 57 ok(false, "There are unexpected targets of " + relDescr);
michael@0 58 return;
michael@0 59 }
michael@0 60
michael@0 61 var relatedIds = (aRelatedIdentifiers instanceof Array) ?
michael@0 62 aRelatedIdentifiers : [aRelatedIdentifiers];
michael@0 63
michael@0 64 var targets = [];
michael@0 65 for (var idx = 0; idx < relatedIds.length; idx++)
michael@0 66 targets.push(getAccessible(relatedIds[idx]));
michael@0 67
michael@0 68 if (targets.length != relatedIds.length)
michael@0 69 return;
michael@0 70
michael@0 71 var actualTargets = relation.getTargets();
michael@0 72
michael@0 73 // Check if all given related accessibles are targets of obtained relation.
michael@0 74 for (var idx = 0; idx < targets.length; idx++) {
michael@0 75 var isFound = false;
michael@0 76 var enumerate = actualTargets.enumerate();
michael@0 77 while (enumerate.hasMoreElements()) {
michael@0 78 var relatedAcc = enumerate.getNext().QueryInterface(nsIAccessible);
michael@0 79 if (targets[idx] == relatedAcc) {
michael@0 80 isFound = true;
michael@0 81 break;
michael@0 82 }
michael@0 83 }
michael@0 84
michael@0 85 ok(isFound, prettyName(relatedIds[idx]) + " is not a target of" + relDescr);
michael@0 86 }
michael@0 87
michael@0 88 // Check if all obtained targets are given related accessibles.
michael@0 89 var enumerate = actualTargets.enumerate();
michael@0 90 while (enumerate.hasMoreElements()) {
michael@0 91 var relatedAcc = enumerate.getNext().QueryInterface(nsIAccessible);
michael@0 92 for (var idx = 0; idx < targets.length && relatedAcc != targets[idx]; idx++);
michael@0 93
michael@0 94 if (idx == targets.length)
michael@0 95 ok(false, "There is unexpected target" + prettyName(relatedAcc) + "of" + relDescr);
michael@0 96 }
michael@0 97 }
michael@0 98
michael@0 99 /**
michael@0 100 * Test that the given accessible relations don't exist.
michael@0 101 *
michael@0 102 * @param aIdentifier [in] identifier to get an accessible, may be ID
michael@0 103 * attribute or DOM element or accessible object
michael@0 104 * @param aRelType [in] relation type (see constants above)
michael@0 105 * @param aUnrelatedIdentifiers [in] identifier or array of identifiers of
michael@0 106 * accessibles that shouldn't exist for this
michael@0 107 * relation.
michael@0 108 */
michael@0 109 function testAbsentRelation(aIdentifier, aRelType, aUnrelatedIdentifiers)
michael@0 110 {
michael@0 111 var relation = getRelationByType(aIdentifier, aRelType);
michael@0 112
michael@0 113 var relDescr = getRelationErrorMsg(aIdentifier, aRelType);
michael@0 114 var relDescrStart = getRelationErrorMsg(aIdentifier, aRelType, true);
michael@0 115
michael@0 116 if (!aUnrelatedIdentifiers) {
michael@0 117 ok(false, "No identifiers given for unrelated accessibles.");
michael@0 118 return;
michael@0 119 }
michael@0 120
michael@0 121 if (!relation || !relation.targetsCount) {
michael@0 122 ok(true, "No relations exist.");
michael@0 123 return;
michael@0 124 }
michael@0 125
michael@0 126 var relatedIds = (aUnrelatedIdentifiers instanceof Array) ?
michael@0 127 aUnrelatedIdentifiers : [aUnrelatedIdentifiers];
michael@0 128
michael@0 129 var targets = [];
michael@0 130 for (var idx = 0; idx < relatedIds.length; idx++)
michael@0 131 targets.push(getAccessible(relatedIds[idx]));
michael@0 132
michael@0 133 if (targets.length != relatedIds.length)
michael@0 134 return;
michael@0 135
michael@0 136 var actualTargets = relation.getTargets();
michael@0 137
michael@0 138 // Any found targets that match given accessibles should be called out.
michael@0 139 for (var idx = 0; idx < targets.length; idx++) {
michael@0 140 var notFound = true;
michael@0 141 var enumerate = actualTargets.enumerate();
michael@0 142 while (enumerate.hasMoreElements()) {
michael@0 143 var relatedAcc = enumerate.getNext().QueryInterface(nsIAccessible);
michael@0 144 if (targets[idx] == relatedAcc) {
michael@0 145 notFound = false;
michael@0 146 break;
michael@0 147 }
michael@0 148 }
michael@0 149
michael@0 150 ok(notFound, prettyName(relatedIds[idx]) + " is a target of " + relDescr);
michael@0 151 }
michael@0 152 }
michael@0 153
michael@0 154 /**
michael@0 155 * Return related accessible for the given relation type.
michael@0 156 *
michael@0 157 * @param aIdentifier [in] identifier to get an accessible, may be ID attribute
michael@0 158 * or DOM element or accessible object
michael@0 159 * @param aRelType [in] relation type (see constants above)
michael@0 160 */
michael@0 161 function getRelationByType(aIdentifier, aRelType)
michael@0 162 {
michael@0 163 var acc = getAccessible(aIdentifier);
michael@0 164 if (!acc)
michael@0 165 return;
michael@0 166
michael@0 167 var relation = null;
michael@0 168 try {
michael@0 169 relation = acc.getRelationByType(aRelType);
michael@0 170 } catch (e) {
michael@0 171 ok(false, "Can't get" + getRelationErrorMsg(aIdentifier, aRelType));
michael@0 172 }
michael@0 173
michael@0 174 return relation;
michael@0 175 }
michael@0 176
michael@0 177 ////////////////////////////////////////////////////////////////////////////////
michael@0 178 // Private implementation details
michael@0 179
michael@0 180 function getRelationErrorMsg(aIdentifier, aRelType, aIsStartSentence)
michael@0 181 {
michael@0 182 var relStr = relationTypeToString(aRelType);
michael@0 183 var msg = aIsStartSentence ? "Relation of '" : " relation of '";
michael@0 184 msg += relStr + "' type for '" + prettyName(aIdentifier) + "'";
michael@0 185 msg += aIsStartSentence ? " " : ".";
michael@0 186
michael@0 187 return msg;
michael@0 188 }

mercurial