-
Notifications
You must be signed in to change notification settings - Fork 9.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
report: add PSI.prepareLabData() #5804
Changes from 4 commits
6556ef5
030d5f7
46dc3aa
e9d9fbf
bd34c84
e8b7c01
930c0b9
c136de3
cea947e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* @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 keep 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( | ||
* LHresponseJsonString, | ||
* document | ||
* ); | ||
* | ||
* @param {string} LHresponseJsonString | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. name updated to
I considered that. The fact that it's a string is due to LR. (And me, obv). And at some point we'll fix LR and this won't be a string anymore and will be received on the PSI side as a proper Since that seems like an implementation detail on the LH/LR side, it seemed reasonable that we eat the maintenance cost. Both LR and third_party/lh-renderer will have to roll in coordination, but that's about it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
SG, just wondering |
||
* @param {Document} document | ||
* @return {{scoreGaugeEl: Element, perfCategoryEl: Element, finalScreenshotDataUri: string|null}} | ||
*/ | ||
static prepareLabData(LHresponseJsonString, document) { | ||
const lhResult = /** @type {LH.Result} */ JSON.parse(LHresponseJsonString); | ||
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)); | ||
const perfCategoryEl = perfRenderer.render(perfCategory, reportLHR.categoryGroups); | ||
|
||
const scoreGaugeEl = dom.find('.lh-score__gauge', perfCategoryEl); | ||
const scoreGaugeWrapper = dom.find('.lh-gauge__wrapper', scoreGaugeEl); | ||
scoreGaugeWrapper.classList.add('lh-gauge__wrapper--huge'); | ||
// Remove Performance category title/description | ||
dom.find('.lh-category-header', perfCategoryEl).remove(); | ||
// Remove navigation links | ||
scoreGaugeWrapper.removeAttribute('href'); | ||
dom.find('.lh-permalink', perfCategoryEl).remove(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. impl-wise it really comes down to avoiding these two lines, so yah we can easily avoid these .remove's.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
this has its downsides, but since we have precedent with
I was hoping if I suggested it vaguely enough you'd assume I meant something in particular and bring that back as a possible solution :) |
||
|
||
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; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/** | ||
* @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; // do i need to do this? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
you tell me :P There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
apparently yes :) |
||
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.ok(result.perfCategoryEl instanceof document.defaultView.Element); | ||
assert.equal(typeof result.finalScreenshotDataUri, 'string'); | ||
|
||
assert.ok(result.finalScreenshotDataUri.startsWith('data:image/jpeg;base64,')); | ||
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.outerHTML.length > 50000, 'perfCategory HTML is populated'); | ||
}); | ||
|
||
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 perf category', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above? |
||
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); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove "keep"?