xref: /aosp_15_r20/development/tools/winscope/src/test/unit/parser_builder.ts (revision 90c8c64db3049935a07c6143d7fd006e26f8ecca)
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 {TimestampConverterUtils} from 'test/unit/timestamp_converter_utils';
19import {
20  CustomQueryParserResultTypeMap,
21  CustomQueryType,
22} from 'trace/custom_query';
23import {Parser} from 'trace/parser';
24import {ParserMock} from 'trace/parser_mock';
25import {TraceType} from 'trace/trace_type';
26
27export class ParserBuilder<T> {
28  private type = TraceType.SURFACE_FLINGER;
29  private entries?: T[];
30  private timestamps?: Timestamp[];
31  private customQueryResult = new Map<CustomQueryType, {}>();
32  private descriptors = ['file descriptor'];
33  private noOffsets = false;
34  private isCorrupted = false;
35
36  setType(type: TraceType): this {
37    this.type = type;
38    return this;
39  }
40
41  setEntries(entries: T[]): this {
42    this.entries = entries;
43    return this;
44  }
45
46  setTimestamps(timestamps: Timestamp[]): this {
47    this.timestamps = timestamps;
48    return this;
49  }
50
51  setNoOffsets(value: boolean): this {
52    this.noOffsets = value;
53    return this;
54  }
55
56  setIsCorrupted(value: boolean): this {
57    this.isCorrupted = value;
58    return this;
59  }
60
61  setCustomQueryResult<Q extends CustomQueryType>(
62    type: Q,
63    result: CustomQueryParserResultTypeMap[Q],
64  ): this {
65    this.customQueryResult.set(type, result ?? {});
66    return this;
67  }
68
69  setDescriptors(descriptors: string[]): this {
70    this.descriptors = descriptors;
71    return this;
72  }
73
74  build(): Parser<T> {
75    if (!this.timestamps && !this.entries) {
76      throw new Error(
77        `Either the timestamps or the entries should be specified`,
78      );
79    }
80
81    if (!this.timestamps) {
82      this.timestamps = this.createTimestamps(this.entries as T[]);
83    }
84
85    if (!this.entries) {
86      this.entries = this.createEntries(this.timestamps);
87    }
88
89    if (this.entries.length !== this.timestamps.length) {
90      throw new Error(
91        'Entries and timestamps arrays must have the same length',
92      );
93    }
94
95    return new ParserMock(
96      this.type,
97      this.timestamps,
98      this.entries,
99      this.customQueryResult,
100      this.descriptors,
101      this.noOffsets,
102      this.isCorrupted,
103    );
104  }
105
106  private createTimestamps(entries: T[]): Timestamp[] {
107    const timestamps = new Array<Timestamp>();
108    for (let i = 0; i < entries.length; ++i) {
109      timestamps[i] = TimestampConverterUtils.makeRealTimestamp(BigInt(i));
110    }
111    return timestamps;
112  }
113
114  private createEntries(timestamps: Timestamp[]): T[] {
115    const entries = new Array<T>();
116    for (let i = 0; i < timestamps.length; ++i) {
117      entries.push(`entry-${i}` as unknown as T);
118    }
119    return entries;
120  }
121}
122