1/* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17import {ParserFactory} from 'parsers/parser_factory'; 18import {TracesParserFactory} from 'parsers/traces_parser_factory'; 19import {CommonTestUtils} from 'test/common/utils'; 20import {LayerTraceEntry, WindowManagerState} from 'trace/flickerlib/common'; 21import {Parser} from 'trace/parser'; 22import {TimestampType} from 'trace/timestamp'; 23import {Trace} from 'trace/trace'; 24import {TraceFile} from 'trace/trace_file'; 25import {TraceType} from 'trace/trace_type'; 26 27class UnitTestUtils extends CommonTestUtils { 28 static async getTraceFromFile(filename: string): Promise<Trace<object>> { 29 const parser = await UnitTestUtils.getParser(filename); 30 31 const trace = Trace.newUninitializedTrace(parser); 32 trace.init( 33 parser.getTimestamps(TimestampType.REAL) !== undefined 34 ? TimestampType.REAL 35 : TimestampType.ELAPSED 36 ); 37 return trace; 38 } 39 40 static async getParser(filename: string): Promise<Parser<object>> { 41 const file = new TraceFile(await CommonTestUtils.getFixtureFile(filename), undefined); 42 const [parsers, errors] = await new ParserFactory().createParsers([file]); 43 expect(parsers.length).toEqual(1); 44 return parsers[0].parser; 45 } 46 47 static async getTracesParser(filenames: string[]): Promise<Parser<object>> { 48 const parsers = await Promise.all( 49 filenames.map((filename) => UnitTestUtils.getParser(filename)) 50 ); 51 const tracesParsers = await new TracesParserFactory().createParsers(parsers); 52 expect(tracesParsers.length).toEqual(1); 53 return tracesParsers[0]; 54 } 55 56 static async getWindowManagerState(): Promise<WindowManagerState> { 57 return UnitTestUtils.getTraceEntry('traces/elapsed_timestamp/WindowManager.pb'); 58 } 59 60 static async getLayerTraceEntry(): Promise<LayerTraceEntry> { 61 return await UnitTestUtils.getTraceEntry('traces/elapsed_timestamp/SurfaceFlinger.pb'); 62 } 63 64 static async getMultiDisplayLayerTraceEntry(): Promise<LayerTraceEntry> { 65 return await UnitTestUtils.getTraceEntry( 66 'traces/elapsed_and_real_timestamp/SurfaceFlinger_multidisplay.pb' 67 ); 68 } 69 70 static async getImeTraceEntries(): Promise<Map<TraceType, any>> { 71 let surfaceFlingerEntry: LayerTraceEntry | undefined; 72 { 73 const parser = await UnitTestUtils.getParser('traces/ime/SurfaceFlinger_with_IME.pb'); 74 surfaceFlingerEntry = await parser.getEntry(5, TimestampType.ELAPSED); 75 } 76 77 let windowManagerEntry: WindowManagerState | undefined; 78 { 79 const parser = await UnitTestUtils.getParser('traces/ime/WindowManager_with_IME.pb'); 80 windowManagerEntry = await parser.getEntry(2, TimestampType.ELAPSED); 81 } 82 83 const entries = new Map<TraceType, any>(); 84 entries.set( 85 TraceType.INPUT_METHOD_CLIENTS, 86 await UnitTestUtils.getTraceEntry('traces/ime/InputMethodClients.pb') 87 ); 88 entries.set( 89 TraceType.INPUT_METHOD_MANAGER_SERVICE, 90 await UnitTestUtils.getTraceEntry('traces/ime/InputMethodManagerService.pb') 91 ); 92 entries.set( 93 TraceType.INPUT_METHOD_SERVICE, 94 await UnitTestUtils.getTraceEntry('traces/ime/InputMethodService.pb') 95 ); 96 entries.set(TraceType.SURFACE_FLINGER, surfaceFlingerEntry); 97 entries.set(TraceType.WINDOW_MANAGER, windowManagerEntry); 98 99 return entries; 100 } 101 102 private static async getTraceEntry(filename: string) { 103 const parser = await UnitTestUtils.getParser(filename); 104 return parser.getEntry(0, TimestampType.ELAPSED); 105 } 106} 107 108export {UnitTestUtils}; 109