-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
233 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,68 @@ | ||
/** | ||
* @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 ContentWidth extends Audit { | ||
/** | ||
* @return {!AuditMeta} | ||
*/ | ||
static get meta() { | ||
return { | ||
category: 'Mobile Friendly', | ||
name: 'content-width', | ||
description: 'Content is sized correctly for the viewport', | ||
requiredArtifacts: ['ContentWidth'] | ||
}; | ||
} | ||
|
||
/** | ||
* @param {!Artifacts} artifacts | ||
* @return {!AuditResult} | ||
*/ | ||
static audit(artifacts) { | ||
if (typeof artifacts.ContentWidth === 'undefined' || | ||
typeof artifacts.ContentWidth.scrollWidth === 'undefined' || | ||
typeof artifacts.ContentWidth.viewportWidth === 'undefined') { | ||
return ContentWidth.generateAuditResult({ | ||
rawValue: false, | ||
debugString: 'Unable to find scroll and viewport widths.' | ||
}); | ||
} | ||
|
||
const widthsMatch = | ||
artifacts.ContentWidth.scrollWidth === artifacts.ContentWidth.viewportWidth; | ||
|
||
return ContentWidth.generateAuditResult({ | ||
rawValue: widthsMatch, | ||
debugString: this.createDebugString(widthsMatch, artifacts.ContentWidth) | ||
}); | ||
} | ||
|
||
static createDebugString(match, artifact) { | ||
if (match) { | ||
return ''; | ||
} | ||
|
||
return 'The content scroll size is ' + artifact.scrollWidth + 'px, ' + | ||
'whereas the viewport size is ' + artifact.viewportWidth + 'px.'; | ||
} | ||
} | ||
|
||
module.exports = ContentWidth; |
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
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,52 @@ | ||
/** | ||
* @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, __returnResults */ | ||
|
||
/* istanbul ignore next */ | ||
function getContentWidth() { | ||
// window.innerWidth to get the scrollable size of the window (irrespective of zoom) | ||
// window.outerWidth to get the size of the visible area | ||
__returnResults({ | ||
scrollWidth: window.innerWidth, | ||
viewportWidth: window.outerWidth | ||
}); | ||
} | ||
|
||
class ContentWidth extends Gather { | ||
|
||
afterPass(options) { | ||
const driver = options.driver; | ||
|
||
return driver.evaluateAsync(`(${getContentWidth.toString()}())`) | ||
|
||
.then(returnedValue => { | ||
this.artifact = returnedValue; | ||
}, _ => { | ||
this.artifact = { | ||
scrollWidth: -1, | ||
viewportWidth: -1 | ||
}; | ||
return; | ||
}); | ||
} | ||
} | ||
|
||
module.exports = ContentWidth; |
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,43 @@ | ||
/** | ||
* 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/content-width.js'); | ||
const assert = require('assert'); | ||
|
||
/* global describe, it*/ | ||
|
||
describe('Mobile-friendly: content-width audit', () => { | ||
it('fails when no input present', () => { | ||
return assert.equal(Audit.audit({}).rawValue, false); | ||
}); | ||
|
||
it('fails when scroll width differs from viewport width', () => { | ||
return assert.equal(Audit.audit({ | ||
ContentWidth: { | ||
scrollWidth: 100, | ||
viewportWidth: 300 | ||
} | ||
}).rawValue, false); | ||
}); | ||
|
||
it('passes when widths match', () => { | ||
return assert.equal(Audit.audit({ | ||
ContentWidth: { | ||
scrollWidth: 300, | ||
viewportWidth: 300 | ||
} | ||
}).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,59 @@ | ||
/** | ||
* 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 ContentWidthGatherer = require('../../../driver/gatherers/content-width'); | ||
const assert = require('assert'); | ||
let contentWidthGatherer; | ||
|
||
describe('Viewport gatherer', () => { | ||
// Reset the Gatherer before each test. | ||
beforeEach(() => { | ||
contentWidthGatherer = new ContentWidthGatherer(); | ||
}); | ||
|
||
it('returns an artifact', () => { | ||
return contentWidthGatherer.afterPass({ | ||
driver: { | ||
evaluateAsync() { | ||
return Promise.resolve({ | ||
scrollWidth: 400, | ||
viewportWidth: 400 | ||
}); | ||
} | ||
} | ||
}).then(_ => { | ||
assert.ok(typeof contentWidthGatherer.artifact === 'object'); | ||
assert.ok(contentWidthGatherer.artifact.viewportWidth === 400); | ||
}); | ||
}); | ||
|
||
it('handles driver failure', () => { | ||
return contentWidthGatherer.afterPass({ | ||
driver: { | ||
evaluateAsync() { | ||
return Promise.reject('such a fail'); | ||
} | ||
} | ||
}).then(_ => { | ||
assert(false); | ||
}).catch(_ => { | ||
assert.equal(contentWidthGatherer.artifact.scrollWidth, -1); | ||
}); | ||
}); | ||
}); |