-
Notifications
You must be signed in to change notification settings - Fork 9.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
report: add PSI.prepareLabData() (#5804)
- Loading branch information
Showing
6 changed files
with
206 additions
and
44 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
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,74 @@ | ||
/** | ||
* @license | ||
* Copyright 2018 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'; | ||
|
||
/* globals self DOM PerformanceCategoryRenderer Util DetailsRenderer */ | ||
|
||
class PSI { | ||
/** | ||
* Returns all the elements that PSI needs to render the report | ||
* We expose this helper method to minimize the 'public' API surface of the renderer | ||
* and allow us to refactor without two-sided patches. | ||
* | ||
* const {scoreGaugeEl, perfCategoryEl, finalScreenshotDataUri} = PSI.prepareLabData( | ||
* LHResultJsonString, | ||
* document | ||
* ); | ||
* | ||
* @param {string} LHResultJsonString The stringified version of {LH.Result} | ||
* @param {Document} document The host page's window.document | ||
* @return {{scoreGaugeEl: Element, perfCategoryEl: Element, finalScreenshotDataUri: string|null}} | ||
*/ | ||
static prepareLabData(LHResultJsonString, document) { | ||
const lhResult = /** @type {LH.Result} */ JSON.parse(LHResultJsonString); | ||
const dom = new DOM(document); | ||
|
||
const reportLHR = Util.prepareReportResult(lhResult); | ||
const perfCategory = reportLHR.reportCategories.find(cat => cat.id === 'performance'); | ||
if (!perfCategory) throw new Error(`No performance category. Can't make lab data section`); | ||
if (!reportLHR.categoryGroups) throw new Error(`No category groups found.`); | ||
|
||
const perfRenderer = new PerformanceCategoryRenderer(dom, new DetailsRenderer(dom)); | ||
// PSI environment string will ensure the categoryHeader and permalink elements are excluded | ||
const perfCategoryEl = perfRenderer.render(perfCategory, reportLHR.categoryGroups, 'PSI'); | ||
|
||
const scoreGaugeEl = dom.find('.lh-score__gauge', perfCategoryEl); | ||
const scoreGaugeWrapperEl = dom.find('.lh-gauge__wrapper', scoreGaugeEl); | ||
scoreGaugeWrapperEl.classList.add('lh-gauge__wrapper--huge'); | ||
// Remove navigation link on gauge | ||
scoreGaugeWrapperEl.removeAttribute('href'); | ||
|
||
const finalScreenshotDataUri = PSI.getFinalScreenshot(perfCategory); | ||
return {scoreGaugeEl, perfCategoryEl, finalScreenshotDataUri}; | ||
} | ||
|
||
/** | ||
* @param {LH.ReportResult.Category} perfCategory | ||
* @return {null|string} | ||
*/ | ||
static getFinalScreenshot(perfCategory) { | ||
const auditRef = perfCategory.auditRefs.find(audit => audit.id === 'final-screenshot'); | ||
if (!auditRef || !auditRef.result || auditRef.result.scoreDisplayMode === 'error') return null; | ||
return auditRef.result.details.data; | ||
} | ||
} | ||
|
||
if (typeof module !== 'undefined' && module.exports) { | ||
module.exports = PSI; | ||
} else { | ||
self.PSI = PSI; | ||
} |
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,119 @@ | ||
/** | ||
* @license Copyright 2017 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 assert = require('assert'); | ||
const fs = require('fs'); | ||
|
||
const jsdom = require('jsdom'); | ||
|
||
const URL = require('../../../../lib/url-shim'); | ||
const PSI = require('../../../../report/html/renderer/psi.js'); | ||
const Util = require('../../../../report/html/renderer/util.js'); | ||
const DOM = require('../../../../report/html/renderer/dom.js'); | ||
const CategoryRenderer = require('../../../../report/html/renderer/category-renderer'); | ||
const DetailsRenderer = require('../../../../report/html/renderer/details-renderer'); | ||
const CriticalRequestChainRenderer = | ||
require('../../../../report/html/renderer/crc-details-renderer'); | ||
|
||
const sampleResultsStr = fs.readFileSync(__dirname + '/../../../results/sample_v2.json', 'utf-8'); | ||
const sampleResults = JSON.parse(sampleResultsStr) | ||
; | ||
const TEMPLATE_FILE = fs.readFileSync( | ||
__dirname + '/../../../../report/html/templates.html', | ||
'utf8' | ||
); | ||
|
||
/* eslint-env jest */ | ||
|
||
describe('DOM', () => { | ||
let document; | ||
beforeAll(() => { | ||
global.URL = URL; // COMPAT: Needed for Node < 10 | ||
global.Util = Util; | ||
global.DOM = DOM; | ||
global.CategoryRenderer = CategoryRenderer; | ||
global.DetailsRenderer = DetailsRenderer; | ||
|
||
// Delayed so that CategoryRenderer is in global scope | ||
const PerformanceCategoryRenderer = | ||
require('../../../../report/html/renderer/performance-category-renderer'); | ||
global.PerformanceCategoryRenderer = PerformanceCategoryRenderer; | ||
global.CriticalRequestChainRenderer = CriticalRequestChainRenderer; | ||
|
||
document = jsdom.jsdom(TEMPLATE_FILE); | ||
}); | ||
|
||
afterAll(() => { | ||
global.URL = undefined; | ||
global.Util = undefined; | ||
global.DOM = undefined; | ||
global.CategoryRenderer = undefined; | ||
global.DetailsRenderer = undefined; | ||
global.PerformanceCategoryRenderer = undefined; | ||
global.CriticalRequestChainRenderer = undefined; | ||
}); | ||
|
||
describe('psi prepareLabData helpers', () => { | ||
describe('prepareLabData', () => { | ||
it('reports expected data', () => { | ||
const result = PSI.prepareLabData(sampleResultsStr, document); | ||
assert.ok(result.scoreGaugeEl instanceof document.defaultView.Element); | ||
assert.equal(result.scoreGaugeEl.querySelector('.lh-gauge__wrapper').href, ''); | ||
assert.ok(result.scoreGaugeEl.outerHTML.includes('<style>'), 'score gauge comes with CSS'); | ||
assert.ok(result.scoreGaugeEl.outerHTML.includes('<svg'), 'score gauge comes with SVG'); | ||
|
||
assert.ok(result.perfCategoryEl instanceof document.defaultView.Element); | ||
assert.ok(result.perfCategoryEl.outerHTML.length > 50000, 'perfCategory HTML is populated'); | ||
assert.ok(!result.perfCategoryEl.outerHTML.includes('lh-permalink'), | ||
'PSI\'s perfCategory HTML doesn\'t include a lh-permalink element'); | ||
|
||
assert.equal(typeof result.finalScreenshotDataUri, 'string'); | ||
assert.ok(result.finalScreenshotDataUri.startsWith('data:image/jpeg;base64,')); | ||
}); | ||
|
||
it('throws if there is no perf category', () => { | ||
const lhrWithoutPerf = JSON.parse(sampleResultsStr); | ||
delete lhrWithoutPerf.categories.performance; | ||
const lhrWithoutPerfStr = JSON.stringify(lhrWithoutPerf); | ||
|
||
assert.throws(() => { | ||
PSI.prepareLabData(lhrWithoutPerfStr, document); | ||
}, /no performance category/i); | ||
}); | ||
|
||
it('throws if there is no category groups', () => { | ||
const lhrWithoutGroups = JSON.parse(sampleResultsStr); | ||
delete lhrWithoutGroups.categoryGroups; | ||
const lhrWithoutGroupsStr = JSON.stringify(lhrWithoutGroups); | ||
|
||
assert.throws(() => { | ||
PSI.prepareLabData(lhrWithoutGroupsStr, document); | ||
}, /no category groups/i); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('getFinalScreenshot', () => { | ||
it('gets a datauri as a string', () => { | ||
const cloneResults = Util.prepareReportResult(sampleResults); | ||
const perfCategory = cloneResults.reportCategories.find(cat => cat.id === 'performance'); | ||
const datauri = PSI.getFinalScreenshot(perfCategory); | ||
assert.equal(typeof datauri, 'string'); | ||
assert.ok(datauri.startsWith('data:image/jpeg;base64,')); | ||
}); | ||
|
||
it('returns null if there is no final-screenshot audit', () => { | ||
const clonedResults = JSON.parse(JSON.stringify(sampleResults)); | ||
delete clonedResults.audits['final-screenshot']; | ||
const lhrNoFinalSS = Util.prepareReportResult(clonedResults); | ||
const perfCategory = lhrNoFinalSS.reportCategories.find(cat => cat.id === 'performance'); | ||
|
||
const datauri = PSI.getFinalScreenshot(perfCategory); | ||
assert.equal(datauri, null); | ||
}); | ||
}); | ||
}); |
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