accessible/tests/mochitest/relations.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/accessible/tests/mochitest/relations.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,188 @@
     1.4 +////////////////////////////////////////////////////////////////////////////////
     1.5 +// Constants
     1.6 +
     1.7 +const RELATION_CONTROLLED_BY = nsIAccessibleRelation.RELATION_CONTROLLED_BY;
     1.8 +const RELATION_CONTROLLER_FOR = nsIAccessibleRelation.RELATION_CONTROLLER_FOR;
     1.9 +const RELATION_DEFAULT_BUTTON = nsIAccessibleRelation.RELATION_DEFAULT_BUTTON;
    1.10 +const RELATION_DESCRIBED_BY = nsIAccessibleRelation.RELATION_DESCRIBED_BY;
    1.11 +const RELATION_DESCRIPTION_FOR = nsIAccessibleRelation.RELATION_DESCRIPTION_FOR;
    1.12 +const RELATION_EMBEDDED_BY = nsIAccessibleRelation.RELATION_EMBEDDED_BY;
    1.13 +const RELATION_EMBEDS = nsIAccessibleRelation.RELATION_EMBEDS;
    1.14 +const RELATION_FLOWS_FROM = nsIAccessibleRelation.RELATION_FLOWS_FROM;
    1.15 +const RELATION_FLOWS_TO = nsIAccessibleRelation.RELATION_FLOWS_TO;
    1.16 +const RELATION_LABEL_FOR = nsIAccessibleRelation.RELATION_LABEL_FOR;
    1.17 +const RELATION_LABELLED_BY = nsIAccessibleRelation.RELATION_LABELLED_BY;
    1.18 +const RELATION_MEMBER_OF = nsIAccessibleRelation.RELATION_MEMBER_OF;
    1.19 +const RELATION_NODE_CHILD_OF = nsIAccessibleRelation.RELATION_NODE_CHILD_OF;
    1.20 +const RELATION_NODE_PARENT_OF = nsIAccessibleRelation.RELATION_NODE_PARENT_OF;
    1.21 +const RELATION_PARENT_WINDOW_OF = nsIAccessibleRelation.RELATION_PARENT_WINDOW_OF;
    1.22 +const RELATION_POPUP_FOR = nsIAccessibleRelation.RELATION_POPUP_FOR;
    1.23 +const RELATION_SUBWINDOW_OF = nsIAccessibleRelation.RELATION_SUBWINDOW_OF;
    1.24 +const RELATION_CONTAINING_DOCUMENT = nsIAccessibleRelation.RELATION_CONTAINING_DOCUMENT;
    1.25 +const RELATION_CONTAINING_TAB_PANE = nsIAccessibleRelation.RELATION_CONTAINING_TAB_PANE;
    1.26 +const RELATION_CONTAINING_APPLICATION = nsIAccessibleRelation.RELATION_CONTAINING_APPLICATION;
    1.27 +
    1.28 +////////////////////////////////////////////////////////////////////////////////
    1.29 +// General
    1.30 +
    1.31 +/**
    1.32 + * Test the accessible relation.
    1.33 + *
    1.34 + * @param aIdentifier          [in] identifier to get an accessible, may be ID
    1.35 + *                             attribute or DOM element or accessible object
    1.36 + * @param aRelType             [in] relation type (see constants above)
    1.37 + * @param aRelatedIdentifiers  [in] identifier or array of identifiers of
    1.38 + *                             expected related accessibles
    1.39 + */
    1.40 +function testRelation(aIdentifier, aRelType, aRelatedIdentifiers)
    1.41 +{
    1.42 +  var relation = getRelationByType(aIdentifier, aRelType);
    1.43 +
    1.44 +  var relDescr = getRelationErrorMsg(aIdentifier, aRelType);
    1.45 +  var relDescrStart = getRelationErrorMsg(aIdentifier, aRelType, true);
    1.46 +
    1.47 +  if (!relation || !relation.targetsCount) {
    1.48 +    if (!aRelatedIdentifiers) {
    1.49 +      ok(true, "No" + relDescr);
    1.50 +      return;
    1.51 +    }
    1.52 +
    1.53 +    var msg = relDescrStart + "has no expected targets: '" +
    1.54 +      prettyName(aRelatedIdentifiers) + "'";
    1.55 +
    1.56 +    ok(false, msg);
    1.57 +    return;
    1.58 +
    1.59 +  } else if (!aRelatedIdentifiers) {
    1.60 +    ok(false, "There are unexpected targets of " + relDescr);
    1.61 +    return;
    1.62 +  }
    1.63 +
    1.64 +  var relatedIds = (aRelatedIdentifiers instanceof Array) ?
    1.65 +  aRelatedIdentifiers : [aRelatedIdentifiers];
    1.66 +
    1.67 +  var targets = [];
    1.68 +   for (var idx = 0; idx < relatedIds.length; idx++)
    1.69 +     targets.push(getAccessible(relatedIds[idx]));
    1.70 +
    1.71 +  if (targets.length != relatedIds.length)
    1.72 +    return;
    1.73 +
    1.74 +  var actualTargets = relation.getTargets();
    1.75 +
    1.76 +  // Check if all given related accessibles are targets of obtained relation.
    1.77 +  for (var idx = 0; idx < targets.length; idx++) {
    1.78 +    var isFound = false;
    1.79 +    var enumerate = actualTargets.enumerate();
    1.80 +    while (enumerate.hasMoreElements()) {
    1.81 +      var relatedAcc = enumerate.getNext().QueryInterface(nsIAccessible);
    1.82 +      if (targets[idx] == relatedAcc) {
    1.83 +        isFound = true;
    1.84 +        break;
    1.85 +      }
    1.86 +    }
    1.87 +
    1.88 +    ok(isFound, prettyName(relatedIds[idx]) + " is not a target of" + relDescr);
    1.89 +  }
    1.90 +
    1.91 +  // Check if all obtained targets are given related accessibles.
    1.92 +  var enumerate = actualTargets.enumerate();
    1.93 +  while (enumerate.hasMoreElements()) {
    1.94 +    var relatedAcc = enumerate.getNext().QueryInterface(nsIAccessible);
    1.95 +    for (var idx = 0; idx < targets.length && relatedAcc != targets[idx]; idx++);
    1.96 +
    1.97 +    if (idx == targets.length)
    1.98 +      ok(false, "There is unexpected target" + prettyName(relatedAcc) + "of" + relDescr);
    1.99 +  }
   1.100 +}
   1.101 +
   1.102 +/**
   1.103 + * Test that the given accessible relations don't exist.
   1.104 + *
   1.105 + * @param aIdentifier           [in] identifier to get an accessible, may be ID
   1.106 + *                              attribute or DOM element or accessible object
   1.107 + * @param aRelType              [in] relation type (see constants above)
   1.108 + * @param aUnrelatedIdentifiers [in] identifier or array of identifiers of
   1.109 + *                              accessibles that shouldn't exist for this
   1.110 + *                              relation.
   1.111 + */
   1.112 +function testAbsentRelation(aIdentifier, aRelType, aUnrelatedIdentifiers)
   1.113 +{
   1.114 +  var relation = getRelationByType(aIdentifier, aRelType);
   1.115 +
   1.116 +  var relDescr = getRelationErrorMsg(aIdentifier, aRelType);
   1.117 +  var relDescrStart = getRelationErrorMsg(aIdentifier, aRelType, true);
   1.118 +
   1.119 +  if (!aUnrelatedIdentifiers) {
   1.120 +    ok(false, "No identifiers given for unrelated accessibles.");
   1.121 +    return;
   1.122 +  }
   1.123 +
   1.124 +  if (!relation || !relation.targetsCount) {
   1.125 +    ok(true, "No relations exist.");
   1.126 +    return;
   1.127 +  }
   1.128 +
   1.129 +  var relatedIds = (aUnrelatedIdentifiers instanceof Array) ?
   1.130 +    aUnrelatedIdentifiers : [aUnrelatedIdentifiers];
   1.131 +
   1.132 +  var targets = [];
   1.133 +  for (var idx = 0; idx < relatedIds.length; idx++)
   1.134 +    targets.push(getAccessible(relatedIds[idx]));
   1.135 +
   1.136 +  if (targets.length != relatedIds.length)
   1.137 +    return;
   1.138 +
   1.139 +  var actualTargets = relation.getTargets();
   1.140 +
   1.141 +  // Any found targets that match given accessibles should be called out.
   1.142 +  for (var idx = 0; idx < targets.length; idx++) {
   1.143 +    var notFound = true;
   1.144 +    var enumerate = actualTargets.enumerate();
   1.145 +    while (enumerate.hasMoreElements()) {
   1.146 +      var relatedAcc = enumerate.getNext().QueryInterface(nsIAccessible);
   1.147 +      if (targets[idx] == relatedAcc) {
   1.148 +        notFound = false;
   1.149 +        break;
   1.150 +      }
   1.151 +    }
   1.152 +
   1.153 +    ok(notFound, prettyName(relatedIds[idx]) + " is a target of " + relDescr);
   1.154 +  }
   1.155 +}
   1.156 +
   1.157 +/**
   1.158 + * Return related accessible for the given relation type.
   1.159 + *
   1.160 + * @param aIdentifier  [in] identifier to get an accessible, may be ID attribute
   1.161 + *                     or DOM element or accessible object
   1.162 + * @param aRelType     [in] relation type (see constants above)
   1.163 + */
   1.164 +function getRelationByType(aIdentifier, aRelType)
   1.165 +{
   1.166 +  var acc = getAccessible(aIdentifier);
   1.167 +  if (!acc)
   1.168 +    return;
   1.169 +
   1.170 +  var relation = null;
   1.171 +  try {
   1.172 +    relation = acc.getRelationByType(aRelType);
   1.173 +  } catch (e) {
   1.174 +    ok(false, "Can't get" + getRelationErrorMsg(aIdentifier, aRelType));
   1.175 +  }
   1.176 +
   1.177 +  return relation;
   1.178 +}
   1.179 +
   1.180 +////////////////////////////////////////////////////////////////////////////////
   1.181 +// Private implementation details
   1.182 +
   1.183 +function getRelationErrorMsg(aIdentifier, aRelType, aIsStartSentence)
   1.184 +{
   1.185 +  var relStr = relationTypeToString(aRelType);
   1.186 +  var msg = aIsStartSentence ? "Relation of '" : " relation of '";
   1.187 +  msg += relStr + "' type for '" + prettyName(aIdentifier) + "'";
   1.188 +  msg += aIsStartSentence ? " " : ".";
   1.189 +
   1.190 +  return msg;
   1.191 +}

mercurial