xref: /aosp_15_r20/development/tools/winscope/src/test/unit/traces_builder.ts (revision 90c8c64db3049935a07c6143d7fd006e26f8ecca)
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 {Timestamp} from 'common/time';
18import {FrameMap} from 'trace/frame_map';
19import {Traces} from 'trace/traces';
20import {TraceType} from 'trace/trace_type';
21import {TraceBuilder} from './trace_builder';
22
23export class TracesBuilder {
24  private readonly traceBuilders = new Map<TraceType, TraceBuilder<{}>>();
25
26  setEntries(
27    type: TraceType,
28    entries: Array<{}>,
29    descriptors?: string[],
30  ): TracesBuilder {
31    const builder = this.getOrCreateTraceBuilder(type);
32    builder.setEntries(entries);
33    if (descriptors) builder.setDescriptors(descriptors);
34    return this;
35  }
36
37  setTimestamps(
38    type: TraceType,
39    timestamps: Timestamp[],
40    descriptors?: string[],
41  ): TracesBuilder {
42    const builder = this.getOrCreateTraceBuilder(type);
43    builder.setTimestamps(timestamps);
44    if (descriptors) builder.setDescriptors(descriptors);
45    return this;
46  }
47
48  setFrameMap(type: TraceType, frameMap: FrameMap | undefined): TracesBuilder {
49    const builder = this.getOrCreateTraceBuilder(type);
50    builder.setFrameMap(frameMap);
51    return this;
52  }
53
54  build(): Traces {
55    const traces = new Traces();
56    this.traceBuilders.forEach((builder) => {
57      traces.addTrace(builder.build());
58    });
59    return traces;
60  }
61
62  private getOrCreateTraceBuilder(type: TraceType): TraceBuilder<{}> {
63    let builder = this.traceBuilders.get(type);
64    if (!builder) {
65      builder = new TraceBuilder<{}>();
66      builder.setType(type);
67      this.traceBuilders.set(type, builder);
68    }
69    return builder;
70  }
71}
72