-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds tests for checking interstitials
- Loading branch information
Showing
5 changed files
with
229 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* @license | ||
* Copyright 2016 Google Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const Audit = require('./audit'); | ||
|
||
class Interstitial extends Audit { | ||
/** | ||
* @return {!AuditMeta} | ||
*/ | ||
static get meta() { | ||
return { | ||
category: 'UX', | ||
name: 'interstitial', | ||
description: 'The page does not place interstitials over the page content', | ||
requiredArtifacts: ['Interstitial'] | ||
}; | ||
} | ||
|
||
/** | ||
* @param {!Artifacts} artifacts | ||
* @return {!AuditResult} | ||
*/ | ||
static audit(artifacts) { | ||
if (typeof artifacts.Interstitial === 'undefined' || | ||
!Array.isArray(artifacts.Interstitial)) { | ||
return Interstitial.generateAuditResult({ | ||
rawValue: false, | ||
debugString: 'Unable to retrieve element information.' | ||
}); | ||
} | ||
|
||
return Interstitial.generateAuditResult({ | ||
rawValue: (artifacts.Interstitial.length === 0) | ||
}); | ||
} | ||
} | ||
|
||
module.exports = Interstitial; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* @license | ||
* Copyright 2016 Google Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
'use strict'; | ||
|
||
const Gather = require('./gather'); | ||
|
||
/* global window, document, __returnResults */ | ||
|
||
/* istanbul ignore next */ | ||
function getInterstitial() { | ||
const viewportSize = window.outerWidth * window.outerHeight; | ||
|
||
// Walk the tree of elements | ||
const candidates = [...document.querySelectorAll('*')] | ||
.filter(e => { | ||
// Check for position fixed. | ||
const isFixed = window.getComputedStyle(e).position === 'fixed'; | ||
|
||
// Check for nav elements. | ||
const regExpNav = /menu|nav|sidebar|drawer/i; | ||
const isNav = regExpNav.test(e.className) || regExpNav.test(e.id); | ||
|
||
// Get the size of the element. | ||
const eBCR = e.getBoundingClientRect(); | ||
const size = eBCR.width * eBCR.height; | ||
|
||
// Only allow through fixed, non-nav elements whose size makes them cover | ||
// over 50% of the available viewport. | ||
return isFixed && !isNav && (size / viewportSize > 0.5); | ||
}); | ||
|
||
// __returnResults is injected by evaluateAsync for passing back the result. | ||
__returnResults(candidates); | ||
} | ||
|
||
class Interstitial extends Gather { | ||
|
||
afterPass(options) { | ||
const driver = options.driver; | ||
|
||
return driver.evaluateAsync(`(${getInterstitial.toString()}())`) | ||
|
||
.then(returnedValue => { | ||
this.artifact = returnedValue; | ||
}, _ => { | ||
this.artifact = -1; | ||
return; | ||
}); | ||
} | ||
} | ||
|
||
module.exports = Interstitial; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* Copyright 2016 Google Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
const Audit = require('../../audits/interstitial.js'); | ||
const assert = require('assert'); | ||
|
||
/* global describe, it*/ | ||
|
||
describe('Mobile-friendly: interstitial audit', () => { | ||
it('fails when no input present', () => { | ||
return assert.equal(Audit.audit({}).rawValue, false); | ||
}); | ||
|
||
it('fails when input is of incorrect type', () => { | ||
return assert.equal(Audit.audit({ | ||
Interstitial: 3 | ||
}).rawValue, false); | ||
}); | ||
|
||
it('fails when element array is non-empty', () => { | ||
return assert.equal(Audit.audit({ | ||
Interstitial: [ | ||
'x' | ||
] | ||
}).rawValue, false); | ||
}); | ||
|
||
it('passes when widths match', () => { | ||
return assert.equal(Audit.audit({ | ||
Interstitial: [] | ||
}).rawValue, true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* Copyright 2016 Google Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
'use strict'; | ||
|
||
/* eslint-env mocha */ | ||
|
||
const InterstitialGatherer = require('../../../driver/gatherers/interstitial'); | ||
const assert = require('assert'); | ||
let interstitialGatherer; | ||
|
||
describe('Interstitial gatherer', () => { | ||
// Reset the Gatherer before each test. | ||
beforeEach(() => { | ||
interstitialGatherer = new InterstitialGatherer(); | ||
}); | ||
|
||
it('returns an artifact', () => { | ||
return interstitialGatherer.afterPass({ | ||
driver: { | ||
evaluateAsync() { | ||
return Promise.resolve(['test']); | ||
} | ||
} | ||
}).then(_ => { | ||
assert.ok(Array.isArray(interstitialGatherer.artifact)); | ||
assert.equal(interstitialGatherer.artifact.length, 1); | ||
}); | ||
}); | ||
|
||
it('handles driver failure', () => { | ||
return interstitialGatherer.afterPass({ | ||
driver: { | ||
evaluateAsync() { | ||
return Promise.reject('such a fail'); | ||
} | ||
} | ||
}).then(_ => { | ||
assert(false); | ||
}).catch(_ => { | ||
assert.equal(interstitialGatherer.artifact, -1); | ||
}); | ||
}); | ||
}); |