summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/uievents/resources/eventrecorder.js
blob: 1e306c0d0a042feb2b85ae3aa4244bf7e39a9e44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// interface EventRecorder {
//    static void start();
//    static void stop();
//    static void clearRecords();
//    static sequence<EventRecord> getRecords();
//    static void configure(EventRecorderOptions options);
// };
// * getRecords
//   * returns an array of EventRecord objects; the array represents the sequence of events captured at anytime after the last clear()
//             call, between when the recorder was started and stopped (including multiple start/stop pairs)
// * configure
//   * sets options that should apply to the recorder. If the recorder has any existing records, than this API throws an exception.
// * start
//   * starts/un-pauses the recorder
// * stop
//   * stops/pauses the recorder
// * clear
//   * purges all recorded records

// ----------------------

// dictionary EventRecorderOptions {
//    sequence<SupportedEventTypes> mergeEventTypes;
//    ObjectNamedMap objectMap;
// };
// * mergeEventTypes
//   * a list of event types that should be consolidated into one record when all of the following conditions are true:
//     1) The events are of the same type and follow each other chronologically
//     2) The events' currentTarget is the same
//   * The default is an empty list (no event types are merged).
// * objectMap
//   * Sets up a series

// dictionary ObjectNamedMap {
//    //<keys will be 'targetTestID' names, with values of the objects which they label>
// };
//   * targetTestID = the string identifier that the associated target object should be known as (for purposes of unique identification. This
//                    need not be the same as the Node's id attribute if it has one. If no 'targetTestID' string mapping is provided via this
//                    map, but is encountered later when recording specific events, a generic targetTestID of 'UNKNOWN_OBJECT' is used.

// ----------------------

// dictionary EventRecord {
//    unsigned long chronologicalOrder;
//    unsigned long sequentialOccurrences;
//    sequence<EventRecord>? nestedEvents;
//    DOMString interfaceType;
//    EventRecordDetails event;
// };
// * chronologicalOrder
//   * Since some events may be dispatched re-entrantly (e.g., while existing events are being dispatched), and others may be merged
//     given the 'mergeEventTypes' option in the EventRecorder, this value is the actual chronological order that the event fired
// * sequentialOccurrences
//   * If this event was fired multiple times in a row (see the 'mergeEventTypes' option), this value is the count of occurrences.
//     A value of 1 means this was the only occurrence of this event (that no events were merged with it). A value greater than 1
//     indicates that the event occurred that many times in a row.
// * nestedEvents
//   * The holds all the events that were sequentially dispatched synchronously while the current event was still being dispatched
//     (e.g., between the time that this event listener was triggered and when it returned).
//   * Has the value null if no nested events were recorded during the invocation of this listener.
// * interfaceType
//   * The string indicating which Event object (or derived Event object type) the recorded event object instance is based on.
// * event
//   * Access to the recorded event properties for the event instance (not the actual event instance itself). A snapshot of the
//     enumerable properties of the event object instance at the moment the listener was first triggered.

// ----------------------

// dictionary EventRecordDetails {
//    //<recorded property names with their values for all enumerable properties of the event object instance>
// };
// * EventRecordDetails
//   * For records with 'sequentialOccurrences' > 1, only the first occurence is recorded (subsequent event details are dropped).
//   * Object reference values (e.g., event.target, event.currentTarget, etc.) are replaced with their mapped 'targetTestID' string.
//     If no 'targetTestID' string mapping is available for a particular object, the value 'UNKNOWN_OBJECT' is returned.

// ----------------------

// partial interface Node {
//    void addRecordedEventListener(SupportedEventTypes type, EventListener? handler, optional boolean capturePhase = false);
//    void removeRecordedEventListener(SupportedEventTypes type, EventListener? handler, optional boolean capturePhase = false);
// };
//
// enum SupportedEventTypes = {
//    "mousemove",
//    etc...
// };
// * addRecordedEventListener
//   * handler =      pass null if you want only a default recording of the event (and don't need any other special handling). Otherwise,
//                    the handler will be invoked normally as part of the event's dispatch.
//   * <other params> are the same as those defined on addEventListener/removeEventListenter APIs (see DOM4)
//   * Use this API *instead of* addEventListener to record your events for testing purposes.

(function EventRecorderScope(global) {
   "use strict";

   if (global.EventRecorder)
      return; // Already initialized.

   // WeakMap polyfill
   if (!global.WeakMap) {
      throw new Error("EventRecorder depends on WeakMap! Please polyfill for completeness to run in this user agent!");
   }

   // Globally applicable variables
   var allRecords = [];
   var recording = false;
   var rawOrder = 1;
   var mergeTypesTruthMap = {}; // format of { eventType: true, ... }
   var eventsInScope = []; // Tracks synchronous event dispatches
   var handlerMap = new WeakMap(); // Keeps original handlers (so that they can be used to un-register for events.

   // Find all Event Object Constructors on the global and add them to the map along with their name (sans 'Event')
   var eventConstructorsNameMap = new WeakMap(); // format of key: hostObject, value: alias to use.
   var regex = /[A-Z][A-Za-z0-9]+Event$/;
   Object.getOwnPropertyNames(global).forEach(function (propName) {
        if (regex.test(propName))
         eventConstructorsNameMap.set(global[propName], propName);
   });
   var knownObjectsMap = eventConstructorsNameMap;

   Object.defineProperty(global, "EventRecorder", {
      writable: true,
      configurable: true,
      value: Object.create(null, {
         start: {
            enumerable: true, configurable: true, writable: true, value: function start() { recording = true; }
         },
         stop: {
            enumerable: true, configurable: true, writable: true, value: function stop() { recording = false; }
         },
         clearRecords: {
            enumerable: true, configurable: true, writable: true, value: function clearRecords() {
               rawOrder = 1;
               allRecords = [];
            }
         },
         getRecords: {
            enumerable: true, configurable: true, writable: true, value: function getRecords() { return allRecords; }
         },
         checkRecords: {
            enumerable: true, configurable: true, writable: true, value: function checkRecords(expected) {
               if (expected.length < allRecords.length) {
                  return false;
               }
               var j = 0;
               for (var i = 0; i < expected.length; ++i) {
                  if (j >= allRecords.length) {
                     if (expected[i].optional) {
                        continue;
                     }
                     return false;
                  }
                  if (expected[i].type == allRecords[j].event.type && expected[i].target == allRecords[j].event.currentTarget) {
                     ++j;
                     continue;
                  }
                  if (expected[i].optional) {
                     continue;
                  }
                  return false;
               }
               return true;
            }
         },
         configure: {
            enumerable: true, configurable: true, writable: true, value: function configure(options) {
               if (allRecords.length > 0)
                  throw new Error("Wrong time to call me: EventRecorder.configure must only be called when no recorded events are present. Try 'clearRecords' first.");

               // Un-configure existing options by calling again with no options set...
               mergeTypesTruthMap = {};
               knownObjectsMap = eventConstructorsNameMap;

               if (!(options instanceof Object))
                  return;
               // Sanitize the passed object (tease-out getter functions)
               var sanitizedOptions = {};
               for (var x in options) {
                  sanitizedOptions[x] = options[x];
               }
               if (sanitizedOptions.mergeEventTypes && Array.isArray(sanitizedOptions.mergeEventTypes)) {
                  sanitizedOptions.mergeEventTypes.forEach(function (eventType) {
                     if (typeof eventType == "string")
                        mergeTypesTruthMap[eventType] = true;
                  });
               }
               if (sanitizedOptions.objectMap && (sanitizedOptions.objectMap instanceof Object)) {
                  for (var y in sanitizedOptions.objectMap) {
                     knownObjectsMap.set(sanitizedOptions.objectMap[y], y);
                  }
               }
            }
         },
         addEventListenersForNodes: {
            enumerable: true, configurable: true, writable: true, value: function addEventListenersForNodes(events, nodes, handler) {
               for (var i = 0; i < nodes.length; ++i) {
                  for (var j = 0; j < events.length; ++j) {
                     nodes[i].addRecordedEventListener(events[j], handler);
                  }
               }
            }
         }
      })
   });

   function EventRecord(rawEvent) {
      this.chronologicalOrder = rawOrder++;
      this.sequentialOccurrences = 1;
      this.nestedEvents = null; // potentially a []
      this.interfaceType = knownObjectsMap.get(rawEvent.constructor);
      if (!this.interfaceType) // In case (somehow) this event's constructor is not named something with an 'Event' suffix...
         this.interfaceType = rawEvent.constructor.toString();
      this.event = new CloneObjectLike(rawEvent);
   }

   // Only enumerable props including prototype-chain (non-recursive), w/no functions.
   function CloneObjectLike(object) {
      for (var prop in object) {
         var val = object[prop];
         if (Array.isArray(val))
            this[prop] = CloneArray(val);
         else if (typeof val == "function")
            continue;
         else if ((typeof val == "object") && (val != null)) {
            this[prop] = knownObjectsMap.get(val);
            if (this[prop] === undefined)
               this[prop] = "UNKNOWN_OBJECT (" + val.toString() + ")";
         }
         else
            this[prop] = val;
      }
   }

   function CloneArray(array) {
      var dup = [];
      for (var i = 0, len = array.length; i < len; i++) {
         var val = array[i]
         if (typeof val == "undefined")
            throw new Error("Ugg. Sparce arrays are not supported. Sorry!");
         else if (Array.isArray(val))
            dup[i] = "UNKNOWN_ARRAY";
         else if (typeof val == "function")
            dup[i] = "UNKNOWN_FUNCTION";
         else if ((typeof val == "object") && (val != null)) {
            dup[i] = knownObjectsMap.get(val);
            if (dup[i] === undefined)
               dup[i] = "UNKNOWN_OBJECT (" + val.toString() + ")";
         }
         else
            dup[i] = val;
      }
      return dup;
   }

   function generateRecordedEventHandlerWithCallback(callback) {
      return function(e) {
         if (recording) {
            // Setup the scope for any synchronous events
            eventsInScope.push(recordEvent(e));
            callback.call(this, e);
            eventsInScope.pop();
         }
      }
   }

   function recordedEventHandler(e) {
      if (recording)
         recordEvent(e);
   }

   function recordEvent(e) {
      var record = new EventRecord(e);
      var recordList = allRecords;
      // Adjust which sequential list to use depending on scope
      if (eventsInScope.length > 0) {
         recordList = eventsInScope[eventsInScope.length - 1].nestedEvents;
         if (recordList == null) // This top-of-stack event record hasn't had any nested events yet.
            recordList = eventsInScope[eventsInScope.length - 1].nestedEvents = [];
      }
      if (mergeTypesTruthMap[e.type] && (recordList.length > 0)) {
         var tail = recordList[recordList.length-1];
         // Same type and currentTarget?
         if ((tail.event.type == record.event.type) && (tail.event.currentTarget == record.event.currentTarget)) {
            tail.sequentialOccurrences++;
            return;
         }
      }
      recordList.push(record);
      return record;
   }

   Object.defineProperties(Node.prototype, {
      addRecordedEventListener: {
         enumerable: true, writable: true, configurable: true,
         value: function addRecordedEventListener(type, handler, capture) {
            if (handler == null)
               this.addEventListener(type, recordedEventHandler, capture);
            else {
               var subvertedHandler = generateRecordedEventHandlerWithCallback(handler);
               handlerMap.set(handler, subvertedHandler);
               this.addEventListener(type, subvertedHandler, capture);
            }
         }
      },
      removeRecordedEventListener: {
         enumerable: true, writable: true, configurable: true,
         value: function addRecordedEventListener(type, handler, capture) {
            var alternateHandlerUsed = handlerMap.get(handler);
            this.removeEventListenter(type, alternateHandlerUsed ? alternateHandlerUsed : recordedEventHandler, capture);
         }
      }
   });

})(window);