1/* 2 * Copyright (C) 2023 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 {Timestamp} from 'common/time'; 18import {CoarseVersion} from './coarse_version'; 19import {CustomQueryParserResultTypeMap, CustomQueryType} from './custom_query'; 20import {AbsoluteEntryIndex, EntriesRange} from './index_types'; 21import {Parser} from './parser'; 22import {TraceType} from './trace_type'; 23 24export class ParserMock<T> implements Parser<T> { 25 constructor( 26 private readonly type: TraceType, 27 private readonly timestamps: Timestamp[], 28 private readonly entries: T[], 29 private readonly customQueryResult: Map<CustomQueryType, object>, 30 private readonly descriptors: string[], 31 private readonly noOffsets: boolean, 32 private readonly isCorrupted: boolean, 33 ) { 34 if (timestamps.length !== entries.length) { 35 throw new Error(`Timestamps and entries must have the same length`); 36 } 37 } 38 39 getTraceType(): TraceType { 40 return this.type; 41 } 42 43 getLengthEntries(): number { 44 return this.entries.length; 45 } 46 47 getCoarseVersion(): CoarseVersion { 48 return CoarseVersion.MOCK; 49 } 50 51 createTimestamps() { 52 throw new Error('Not implemented'); 53 } 54 55 getRealToMonotonicTimeOffsetNs(): bigint | undefined { 56 return this.noOffsets ? undefined : 0n; 57 } 58 59 getRealToBootTimeOffsetNs(): bigint | undefined { 60 return this.noOffsets ? undefined : 0n; 61 } 62 63 getTimestamps(): Timestamp[] { 64 return this.timestamps; 65 } 66 67 getEntry(index: AbsoluteEntryIndex): Promise<T> { 68 if (this.isCorrupted) throw new Error('Corrupted trace'); 69 return Promise.resolve(this.entries[index]); 70 } 71 72 customQuery<Q extends CustomQueryType>( 73 type: Q, 74 entriesRange: EntriesRange, 75 ): Promise<CustomQueryParserResultTypeMap[Q]> { 76 let result = this.customQueryResult.get(type); 77 if (result === undefined) { 78 throw new Error( 79 `This mock was not configured to support custom query type '${type}'. Something missing in your test set up?`, 80 ); 81 } 82 if ( 83 type !== CustomQueryType.SF_LAYERS_ID_AND_NAME && 84 Array.isArray(result) 85 ) { 86 result = result.slice(entriesRange.start, entriesRange.end); 87 } 88 return Promise.resolve(result) as Promise< 89 CustomQueryParserResultTypeMap[Q] 90 >; 91 } 92 93 getDescriptors(): string[] { 94 return this.descriptors; 95 } 96} 97