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

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

mercurial