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
35  setType(type: TraceType): this {
36    this.type = type;
37    return this;
38  }
39
40  setEntries(entries: T[]): this {
41    this.entries = entries;
42    return this;
43  }
44
45  setTimestamps(timestamps: Timestamp[]): this {
46    this.timestamps = timestamps;
47    return this;
48  }
49
50  setNoOffsets(value: boolean): this {
51    this.noOffsets = value;
52    return this;
53  }
54
55  setCustomQueryResult<Q extends CustomQueryType>(
56    type: Q,
57    result: CustomQueryParserResultTypeMap[Q],
58  ): this {
59    this.customQueryResult.set(type, result);
60    return this;
61  }
62
63  setDescriptors(descriptors: string[]): this {
64    this.descriptors = descriptors;
65    return this;
66  }
67
68  build(): Parser<T> {
69    if (!this.timestamps && !this.entries) {
70      throw new Error(
71        `Either the timestamps or the entries should be specified`,
72      );
73    }
74
75    if (!this.timestamps) {
76      this.timestamps = this.createTimestamps(this.entries as T[]);
77    }
78
79    if (!this.entries) {
80      this.entries = this.createEntries(this.timestamps);
81    }
82
83    if (this.entries.length !== this.timestamps.length) {
84      throw new Error(
85        'Entries and timestamps arrays must have the same length',
86      );
87    }
88
89    return new ParserMock(
90      this.type,
91      this.timestamps,
92      this.entries,
93      this.customQueryResult,
94      this.descriptors,
95      this.noOffsets,
96    );
97  }
98
99  private createTimestamps(entries: T[]): Timestamp[] {
100    const timestamps = new Array<Timestamp>();
101    for (let i = 0; i < entries.length; ++i) {
102      timestamps[i] = TimestampConverterUtils.makeRealTimestamp(BigInt(i));
103    }
104    return timestamps;
105  }
106
107  private createEntries(timestamps: Timestamp[]): T[] {
108    const entries = new Array<T>();
109    for (let i = 0; i < timestamps.length; ++i) {
110      entries.push(`entry-${i}` as unknown as T);
111    }
112    return entries;
113  }
114}
115