Bug 1568151 - Replace `target.getInspector()` by `target.getFront("inspector")`....
[gecko.git] / accessible / tests / mochitest / actions.js
blob01da33bca349496cdf0b4bed53d3ea55b50180ea
1 // //////////////////////////////////////////////////////////////////////////////
2 // Event constants
4 const MOUSEDOWN_EVENT = 1;
5 const MOUSEUP_EVENT = 2;
6 const CLICK_EVENT = 4;
7 const COMMAND_EVENT = 8;
8 const FOCUS_EVENT = 16;
10 const CLICK_EVENTS = MOUSEDOWN_EVENT | MOUSEUP_EVENT | CLICK_EVENT;
11 const XUL_EVENTS = CLICK_EVENTS | COMMAND_EVENT;
13 // //////////////////////////////////////////////////////////////////////////////
14 // Public functions
16 /**
17  * Test default accessible actions.
18  *
19  * Action tester interface is:
20  *
21  *  var actionObj = {
22  *    // identifier of accessible to perform an action on
23  *    get ID() {},
24  *
25  *    // index of the action
26  *    get actionIndex() {},
27  *
28  *    // name of the action
29  *    get actionName() {},
30  *
31  *    // DOM events (see constants defined above)
32  *    get events() {},
33  *
34  *    // [optional] identifier of target DOM events listeners are registered on,
35  *    // used with 'events', if missing then 'ID' is used instead.
36  *    get targetID() {},
37  *
38  *    // [optional] perform checks when 'click' event is handled if 'events'
39  *    // is used.
40  *    checkOnClickEvent: function() {},
41  *
42  *    // [optional] an array of invoker's checker objects (see eventQueue
43  *    // constructor events.js)
44  *    get eventSeq() {}
45  *  };
46  *
47  *
48  * @param  aArray [in] an array of action cheker objects
49  */
50 function testActions(aArray) {
51   gActionsQueue = new eventQueue();
53   for (var idx = 0; idx < aArray.length; idx++) {
54     var actionObj = aArray[idx];
55     var accOrElmOrID = actionObj.ID;
56     var actionIndex = actionObj.actionIndex;
57     var actionName = actionObj.actionName;
58     var events = actionObj.events;
59     var accOrElmOrIDOfTarget = actionObj.targetID
60       ? actionObj.targetID
61       : accOrElmOrID;
63     var eventSeq = [];
64     if (events) {
65       var elm = getNode(accOrElmOrIDOfTarget);
66       if (events & MOUSEDOWN_EVENT) {
67         eventSeq.push(new checkerOfActionInvoker("mousedown", elm, actionObj));
68       }
70       if (events & MOUSEUP_EVENT) {
71         eventSeq.push(new checkerOfActionInvoker("mouseup", elm, actionObj));
72       }
74       if (events & CLICK_EVENT) {
75         eventSeq.push(new checkerOfActionInvoker("click", elm, actionObj));
76       }
78       if (events & COMMAND_EVENT) {
79         eventSeq.push(new checkerOfActionInvoker("command", elm, actionObj));
80       }
82       if (events & FOCUS_EVENT) {
83         eventSeq.push(new focusChecker(elm));
84       }
85     }
87     if (actionObj.eventSeq) {
88       eventSeq = eventSeq.concat(actionObj.eventSeq);
89     }
91     var invoker = new actionInvoker(
92       accOrElmOrID,
93       actionIndex,
94       actionName,
95       eventSeq
96     );
97     gActionsQueue.push(invoker);
98   }
100   gActionsQueue.invoke();
104  * Test action names and descriptions.
105  */
106 function testActionNames(aID, aActions) {
107   var actions = typeof aActions == "string" ? [aActions] : aActions || [];
109   var acc = getAccessible(aID);
110   is(acc.actionCount, actions.length, "Wong number of actions.");
111   for (var i = 0; i < actions.length; i++) {
112     is(
113       acc.getActionName(i),
114       actions[i],
115       "Wrong action name at " + i + " index."
116     );
117     is(
118       acc.getActionDescription(0),
119       gActionDescrMap[actions[i]],
120       "Wrong action description at " + i + "index."
121     );
122   }
125 // //////////////////////////////////////////////////////////////////////////////
126 // Private
128 var gActionsQueue = null;
130 function actionInvoker(aAccOrElmOrId, aActionIndex, aActionName, aEventSeq) {
131   this.invoke = function actionInvoker_invoke() {
132     var acc = getAccessible(aAccOrElmOrId);
133     if (!acc) {
134       return INVOKER_ACTION_FAILED;
135     }
137     var isThereActions = acc.actionCount > 0;
138     ok(
139       isThereActions,
140       "No actions on the accessible for " + prettyName(aAccOrElmOrId)
141     );
143     if (!isThereActions) {
144       return INVOKER_ACTION_FAILED;
145     }
147     is(
148       acc.getActionName(aActionIndex),
149       aActionName,
150       "Wrong action name of the accessible for " + prettyName(aAccOrElmOrId)
151     );
153     try {
154       acc.doAction(aActionIndex);
155     } catch (e) {
156       ok(false, "doAction(" + aActionIndex + ") failed with: " + e.name);
157       return INVOKER_ACTION_FAILED;
158     }
159     return null;
160   };
162   this.eventSeq = aEventSeq;
164   this.getID = function actionInvoker_getID() {
165     return (
166       "invoke an action " +
167       aActionName +
168       " at index " +
169       aActionIndex +
170       " on " +
171       prettyName(aAccOrElmOrId)
172     );
173   };
176 function checkerOfActionInvoker(aType, aTarget, aActionObj) {
177   this.type = aType;
179   this.target = aTarget;
181   if (aActionObj && "eventTarget" in aActionObj) {
182     this.eventTarget = aActionObj.eventTarget;
183   }
185   this.phase = false;
187   this.getID = function getID() {
188     return aType + " event handling";
189   };
191   this.check = function check(aEvent) {
192     if (aType == "click" && aActionObj && "checkOnClickEvent" in aActionObj) {
193       aActionObj.checkOnClickEvent(aEvent);
194     }
195   };
198 var gActionDescrMap = {
199   jump: "Jump",
200   press: "Press",
201   check: "Check",
202   uncheck: "Uncheck",
203   select: "Select",
204   open: "Open",
205   close: "Close",
206   switch: "Switch",
207   click: "Click",
208   collapse: "Collapse",
209   expand: "Expand",
210   activate: "Activate",
211   cycle: "Cycle",