Skip to content

Commit

Permalink
update EventListeners gatherer to use Errors for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
brendankenny committed Feb 6, 2017
1 parent c610e0d commit a8f470a
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 48 deletions.
4 changes: 0 additions & 4 deletions lighthouse-core/audits/dobetterweb/no-mutation-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ class NoMutationEventsAudit extends Audit {
* @return {!AuditResult}
*/
static audit(artifacts) {
if (artifacts.EventListeners.rawValue === -1) {
return NoMutationEventsAudit.generateAuditResult(artifacts.EventListeners);
}

let debugString;
const listeners = artifacts.EventListeners;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ class PassiveEventsAudit extends Audit {
* @return {!AuditResult}
*/
static audit(artifacts) {
if (artifacts.EventListeners.rawValue === -1) {
return PassiveEventsAudit.generateAuditResult(artifacts.EventListeners);
}

let debugString;
const listeners = artifacts.EventListeners;

Expand Down
27 changes: 13 additions & 14 deletions lighthouse-core/gather/gatherers/dobetterweb/all-event-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class EventListeners extends Gatherer {
/**
* @param {number|string} nodeIdOrObject The node id of the element or the
* string of and object ('document', 'window').
* @return {!Promise<!Array.<EventListener>>}
* @return {!Promise<!Array<{listeners: !Array, tagName: string}>>}
* @private
*/
_listEventListeners(nodeIdOrObject) {
Expand Down Expand Up @@ -74,7 +74,7 @@ class EventListeners extends Gatherer {
* listenForScriptParsedEvents should be called before this method to ensure
* the page's parsed scripts are collected at page load.
* @param {string} nodeId The node to look for attached event listeners.
* @return {!Promise<!Array.<Object>>} List of event listeners attached to
* @return {!Promise<!Array<!Object>>} List of event listeners attached to
* the node.
*/
getEventListeners(nodeId) {
Expand Down Expand Up @@ -107,8 +107,8 @@ class EventListeners extends Gatherer {

/**
* Aggregates the event listeners used on each element into a single list.
* @param {Array.<Element>} nodes List of elements to fetch event listeners for.
* @return {!Promise<!Array.<Object>>} Resolves to a list of all the event
* @param {!Array<!Element>} nodes List of elements to fetch event listeners for.
* @return {!Promise<!Array<!Object>>} Resolves to a list of all the event
* listeners found across the elements.
*/
collectListeners(nodes) {
Expand All @@ -127,18 +127,17 @@ class EventListeners extends Gatherer {
return this.listenForScriptParsedEvents();
}

/**
* @param {!Object} options
* @return {!Promise<!Array<!Object>>}
*/
afterPass(options) {
return this.unlistenForScriptParsedEvents()
.then(_ => options.driver.querySelectorAll('body, body /deep/ *')) // drill into shadow trees
.then(nodes => {
nodes.push('document', 'window');
return this.collectListeners(nodes);
}).catch(_ => {
return {
rawValue: -1,
debugString: 'Unable to collect passive events listener usage.'
};
});
.then(_ => options.driver.querySelectorAll('body, body /deep/ *')) // drill into shadow trees
.then(nodes => {
nodes.push('document', 'window');
return this.collectListeners(nodes);
});
}
}

Expand Down
13 changes: 0 additions & 13 deletions lighthouse-core/test/audits/dobetterweb/no-mutation-events-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,6 @@ const URL = 'https://example.com';
/* eslint-env mocha */

describe('Page does not use mutation events', () => {
it('fails when gatherer returns error', () => {
const debugString = 'event-y debug string';
const auditResult = NoMutationEventsAudit.audit({
EventListeners: {
rawValue: -1,
debugString
},
URL: {finalUrl: URL}
});
assert.equal(auditResult.rawValue, -1);
assert.strictEqual(auditResult.debugString, debugString);
});

it('passes when mutation events are not used', () => {
const auditResult = NoMutationEventsAudit.audit({
EventListeners: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,6 @@ const URL = 'https://example.com';
/* eslint-env mocha */

describe('Page uses passive events listeners where applicable', () => {
it('fails when gatherer returns error', () => {
const debugString = 'Unable to gather passive event listeners usage.';
const auditResult = PassiveEventsAudit.audit({
EventListeners: {
rawValue: -1,
debugString: debugString
},
URL: {finalUrl: URL}
});
assert.equal(auditResult.rawValue, -1);
assert.equal(auditResult.debugString, debugString);
});

it('fails when scroll blocking listeners should be passive', () => {
const auditResult = PassiveEventsAudit.audit({
EventListeners: fixtureData,
Expand Down

0 comments on commit a8f470a

Please sign in to comment.