Bug 1478776 - Part 2: Add utility class for counting events. r=botond,masayuki
authorJan Henning <jh+bugzilla@buttercookie.de>
Thu, 20 Dec 2018 21:34:50 +0000
changeset 451693 93a3c7fc4acea01c618a459f5502b638aa718a84
parent 451692 72a39ded10f87d08b67b84c45030934510d926fd
child 451694 e6b186bffed79a39f5a3694679ec853a4943ca8c
push id35251
push userccoroiu@mozilla.com
push dateFri, 21 Dec 2018 21:54:30 +0000
treeherdermozilla-central@74101900e7d4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbotond, masayuki
bugs1478776
milestone66.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1478776 - Part 2: Add utility class for counting events. r=botond,masayuki Differential Revision: https://phabricator.services.mozilla.com/D14039
testing/mochitest/tests/SimpleTest/EventUtils.js
--- a/testing/mochitest/tests/SimpleTest/EventUtils.js
+++ b/testing/mochitest/tests/SimpleTest/EventUtils.js
@@ -2496,8 +2496,47 @@ var PluginUtils =
         callback(tags[i]);
         return true;
       }
     }
     todo(false, "Need a test plugin on this platform");
     return false;
   }
 };
+
+class EventCounter {
+  constructor(aTarget, aType, aOptions = {}) {
+    this.target = aTarget;
+    this.type = aType;
+    this.options = aOptions;
+
+    this.eventCount = 0;
+    // Bug 1512817:
+    // SpecialPowers is picky and needs to be passed an explicit reference to
+    // the function to be called. To avoid having to bind "this", we therefore
+    // define the method this way, via a property.
+    this.handleEvent = (aEvent) => {
+        this.eventCount++;
+    };
+
+    if (aOptions.mozSystemGroup) {
+      SpecialPowers.addSystemEventListener(aTarget, aType,
+                                           this.handleEvent,
+                                           aOptions.capture);
+    } else {
+      aTarget.addEventListener(aType, this, aOptions);
+    }
+  }
+
+  unregister() {
+    if (this.options.mozSystemGroup) {
+      SpecialPowers.removeSystemEventListener(this.target, this.type,
+                                              this.handleEvent,
+                                              this.options.capture);
+    } else {
+      this.target.removeEventListener(this.type, this, this.options);
+    }
+  }
+
+  get count() {
+    return this.eventCount;
+  }
+}