1// Copyright (C) 2021 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15import fs from 'fs'; 16import net from 'net'; 17import path from 'path'; 18import pixelmatch from 'pixelmatch'; 19import {PNG} from 'pngjs'; 20import {Page} from 'puppeteer'; 21 22// These constants have been hand selected by comparing the diffs of screenshots 23// between Linux on Mac. Unfortunately font-rendering is platform-specific. 24// Even though we force the same antialiasing and hinting settings, some minimal 25// differences exist. 26const DIFF_PER_PIXEL_THRESHOLD = 0.35; 27const DIFF_MAX_PIXELS = 50; 28 29// Waits for the Perfetto UI to be quiescent, using a union of heuristics: 30// - Check that the progress bar is not animating. 31// - Check that the omnibox is not showing a message. 32// - Check that no redraws are pending in our RAF scheduler. 33// - Check that all the above is satisfied for |minIdleMs| consecutive ms. 34export async function waitForPerfettoIdle(page: Page, minIdleMs?: number) { 35 minIdleMs = minIdleMs || 3000; 36 const tickMs = 250; 37 const timeoutMs = 60000; 38 const minIdleTicks = Math.ceil(minIdleMs / tickMs); 39 const timeoutTicks = Math.ceil(timeoutMs / tickMs); 40 let consecutiveIdleTicks = 0; 41 let reasons: string[] = []; 42 for (let ticks = 0; ticks < timeoutTicks; ticks++) { 43 await new Promise((r) => setTimeout(r, tickMs)); 44 const isShowingMsg = !!(await page.$('.omnibox.message-mode')); 45 const isShowingAnim = !!(await page.$('.progress.progress-anim')); 46 const hasPendingRedraws = 47 await ( 48 await page.evaluateHandle('globals.rafScheduler.hasPendingRedraws')) 49 .jsonValue(); 50 51 if (isShowingAnim || isShowingMsg || hasPendingRedraws) { 52 consecutiveIdleTicks = 0; 53 reasons = []; 54 if (isShowingAnim) { 55 reasons.push('showing progress animation'); 56 } 57 if (isShowingMsg) { 58 reasons.push('showing omnibox message'); 59 } 60 if (hasPendingRedraws) { 61 reasons.push('has pending redraws'); 62 } 63 continue; 64 } 65 if (++consecutiveIdleTicks >= minIdleTicks) { 66 return; 67 } 68 } 69 throw new Error( 70 `waitForPerfettoIdle() failed. Did not reach idle after ${ 71 timeoutMs} ms. ` + 72 `Reasons not considered idle: ${reasons.join(', ')}`); 73} 74 75export function getTestTracePath(fname: string): string { 76 const fPath = path.join('test', 'data', fname); 77 if (!fs.existsSync(fPath)) { 78 throw new Error('Could not locate trace file ' + fPath); 79 } 80 return fPath; 81} 82 83export async function compareScreenshots( 84 reportPath: string, actualFilename: string, expectedFilename: string) { 85 if (!fs.existsSync(expectedFilename)) { 86 throw new Error( 87 `Could not find ${expectedFilename}. Run wih REBASELINE=1.`); 88 } 89 const actualImg = PNG.sync.read(fs.readFileSync(actualFilename)); 90 const expectedImg = PNG.sync.read(fs.readFileSync(expectedFilename)); 91 const {width, height} = actualImg; 92 expect(width).toEqual(expectedImg.width); 93 expect(height).toEqual(expectedImg.height); 94 const diffPng = new PNG({width, height}); 95 const diff = await pixelmatch( 96 actualImg.data, expectedImg.data, diffPng.data, width, height, { 97 threshold: DIFF_PER_PIXEL_THRESHOLD, 98 }); 99 if (diff > DIFF_MAX_PIXELS) { 100 const diffFilename = actualFilename.replace('.png', '-diff.png'); 101 fs.writeFileSync(diffFilename, PNG.sync.write(diffPng)); 102 fs.appendFileSync( 103 reportPath, 104 `${path.basename(actualFilename)};${path.basename(diffFilename)}\n`); 105 fail(`Diff test failed on ${diffFilename}, delta: ${diff} pixels`); 106 } 107 return diff; 108} 109 110 111// If the user has a trace_processor_shell --httpd instance open, bail out, 112// as that will invalidate the test loading different data. 113export async function failIfTraceProcessorHttpdIsActive() { 114 return new Promise<void>((resolve, reject) => { 115 const client = new net.Socket(); 116 client.connect(9001, '127.0.0.1', () => { 117 const err = 'trace_processor_shell --httpd detected on port 9001. ' + 118 'Bailing out as it interferes with the tests. ' + 119 'Please kill that and run the test again.'; 120 console.error(err); 121 client.destroy(); 122 reject(err); 123 }); 124 client.on('error', (e: {code: string}) => { 125 expect(e.code).toBe('ECONNREFUSED'); 126 resolve(); 127 }); 128 client.end(); 129 }); 130} 131