1// Copyright (C) 2024 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 {Time} from '../base/time'; 16import {EngineBase} from '../trace_processor/engine'; 17import {AppImpl} from './app_impl'; 18import {TraceImpl} from './trace_impl'; 19import {TraceInfoImpl} from './trace_info_impl'; 20 21export interface FakeTraceImplArgs { 22 // If true suppresses exceptions when trying to issue a query. This is to 23 // catch bugs where we are trying to query an empty instance. However some 24 // unittests need to do so. Default: false. 25 allowQueries?: boolean; 26} 27 28let appImplInitialized = false; 29 30export function initializeAppImplForTesting(): AppImpl { 31 if (!appImplInitialized) { 32 appImplInitialized = true; 33 AppImpl.initialize({initialRouteArgs: {}}); 34 } 35 return AppImpl.instance; 36} 37 38// For testing purposes only. 39export function createFakeTraceImpl(args: FakeTraceImplArgs = {}) { 40 initializeAppImplForTesting(); 41 const fakeTraceInfo: TraceInfoImpl = { 42 source: {type: 'URL', url: ''}, 43 traceTitle: '', 44 traceUrl: '', 45 start: Time.fromSeconds(0), 46 end: Time.fromSeconds(10), 47 realtimeOffset: Time.ZERO, 48 utcOffset: Time.ZERO, 49 traceTzOffset: Time.ZERO, 50 cpus: [], 51 importErrors: 0, 52 traceType: 'proto', 53 hasFtrace: false, 54 uuid: '', 55 cached: false, 56 downloadable: false, 57 }; 58 return TraceImpl.createInstanceForCore( 59 AppImpl.instance, 60 new FakeEngine(args.allowQueries ?? false), 61 fakeTraceInfo, 62 ); 63} 64 65class FakeEngine extends EngineBase { 66 readonly mode = 'WASM'; 67 id: string = 'TestEngine'; 68 69 constructor(private allowQueries: boolean) { 70 super(); 71 } 72 73 rpcSendRequestBytes(_data: Uint8Array) { 74 if (!this.allowQueries) { 75 throw new Error( 76 'FakeEngine.query() should never be reached. ' + 77 'If this is a unittest, try adding {allowQueries: true} to the ' + 78 'createFakeTraceImpl() call.', 79 ); 80 } 81 } 82 83 [Symbol.dispose]() {} 84} 85