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 * as fs from 'fs'; 16import * as net from 'net'; 17import * as libpng from 'node-libpng'; 18import * as path from 'path'; 19import * as pixelmatch from 'pixelmatch'; 20import * as puppeteer 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( 35 page: puppeteer.Page, minIdleMs?: number) { 36 minIdleMs = minIdleMs || 3000; 37 const tickMs = 250; 38 const timeoutMs = 60000; 39 const minIdleTicks = Math.ceil(minIdleMs / tickMs); 40 const timeoutTicks = Math.ceil(timeoutMs / tickMs); 41 let consecutiveIdleTicks = 0; 42 let reasons: string[] = []; 43 for (let ticks = 0; ticks < timeoutTicks; ticks++) { 44 await new Promise(r => setTimeout(r, tickMs)); 45 const isShowingMsg = !!(await page.$('.omnibox.message-mode')); 46 const isShowingAnim = !!(await page.$('.progress.progress-anim')); 47 const hasPendingRedraws = 48 await ( 49 await page.evaluateHandle('globals.rafScheduler.hasPendingRedraws')) 50 .jsonValue<number>(); 51 52 if (isShowingAnim || isShowingMsg || hasPendingRedraws) { 53 consecutiveIdleTicks = 0; 54 reasons = []; 55 if (isShowingAnim) { 56 reasons.push('showing progress animation'); 57 } 58 if (isShowingMsg) { 59 reasons.push('showing omnibox message'); 60 } 61 if (hasPendingRedraws) { 62 reasons.push('has pending redraws'); 63 } 64 continue; 65 } 66 if (++consecutiveIdleTicks >= minIdleTicks) { 67 return; 68 } 69 } 70 throw new Error( 71 `waitForPerfettoIdle() failed. Did not reach idle after ${ 72 timeoutMs} ms. ` + 73 `Reasons not considered idle: ${reasons.join(', ')}`); 74} 75 76export function getTestTracePath(fname: string): string { 77 const fPath = path.join('test', 'data', fname); 78 if (!fs.existsSync(fPath)) { 79 throw new Error('Could not locate trace file ' + fPath); 80 } 81 return fPath; 82} 83 84export async function compareScreenshots( 85 actualFilename: string, expectedFilename: string) { 86 if (!fs.existsSync(expectedFilename)) { 87 throw new Error( 88 `Could not find ${expectedFilename}. Run wih REBASELINE=1.`); 89 } 90 const actualImg = await libpng.readPngFile(actualFilename); 91 const expectedImg = await libpng.readPngFile(expectedFilename); 92 const {width, height} = actualImg; 93 expect(width).toEqual(expectedImg.width); 94 expect(height).toEqual(expectedImg.height); 95 const diffBuff = Buffer.alloc(actualImg.data.byteLength); 96 const diff = await pixelmatch( 97 actualImg.data, expectedImg.data, diffBuff, width, height, { 98 threshold: DIFF_PER_PIXEL_THRESHOLD 99 }); 100 if (diff > DIFF_MAX_PIXELS) { 101 const diffFilename = actualFilename.replace('.png', '-diff.png'); 102 libpng.writePngFile(diffFilename, diffBuff, {width, height}); 103 fail(`Diff test failed on ${diffFilename}, delta: ${diff} pixels`); 104 } 105 return diff; 106} 107 108 109// If the user has a trace_processor_shell --httpd instance open, bail out, 110// as that will invalidate the test loading different data. 111export async function failIfTraceProcessorHttpdIsActive() { 112 return new Promise<void>((resolve, reject) => { 113 const client = new net.Socket(); 114 client.connect(9001, '127.0.0.1', () => { 115 const err = 'trace_processor_shell --httpd detected on port 9001. ' + 116 'Bailing out as it interferes with the tests. ' + 117 'Please kill that and run the test again.'; 118 console.error(err); 119 client.destroy(); 120 reject(err); 121 }); 122 client.on('error', (e: {code: string}) => { 123 expect(e.code).toBe('ECONNREFUSED'); 124 resolve(); 125 }); 126 client.end(); 127 }); 128} 129