• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2024 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 ANYf KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import {assertDefined} from 'common/assert_utils';
18import {TracePositionUpdate} from 'messaging/winscope_event';
19import {TimestampConverterUtils} from 'test/unit/timestamp_converter_utils';
20import {TracesBuilder} from 'test/unit/traces_builder';
21import {TraceBuilder} from 'test/unit/trace_builder';
22import {UnitTestUtils} from 'test/unit/utils';
23import {Traces} from 'trace/traces';
24import {TraceType} from 'trace/trace_type';
25import {PropertyTreeNode} from 'trace/tree_node/property_tree_node';
26import {Presenter} from './presenter';
27import {UiData} from './ui_data';
28
29describe('PresenterTransitions', () => {
30  it('is robust to empty trace', async () => {
31    const traces = new TracesBuilder()
32      .setEntries(TraceType.TRANSITION, [])
33      .build();
34    const trace = assertDefined(traces.getTrace(TraceType.TRANSITION));
35    let outputUiData: UiData | undefined;
36    const presenter = new Presenter(trace, traces, (data: UiData) => {
37      outputUiData = data;
38    });
39
40    await presenter.onAppEvent(
41      TracePositionUpdate.fromTimestamp(
42        TimestampConverterUtils.makeRealTimestamp(10n),
43      ),
44    );
45    expect(outputUiData).toEqual(UiData.EMPTY);
46  });
47
48  it('updates selected transition', async () => {
49    const parser = await UnitTestUtils.getPerfettoParser(
50      TraceType.TRANSITION,
51      'traces/perfetto/shell_transitions_trace.perfetto-trace',
52    );
53
54    const trace = new TraceBuilder<PropertyTreeNode>()
55      .setType(TraceType.TRANSITION)
56      .setParser(parser)
57      .build();
58
59    const traces = new Traces();
60    traces.addTrace(trace);
61
62    let outputUiData = UiData.EMPTY;
63    const presenter = new Presenter(trace, traces, (data: UiData) => {
64      outputUiData = data;
65    });
66
67    const entry = trace.getEntry(1);
68    await presenter.onAppEvent(TracePositionUpdate.fromTraceEntry(entry));
69
70    expect(outputUiData.entries.length).toEqual(4);
71
72    const selectedTransition = assertDefined(outputUiData.selectedTransition);
73    const wmData = assertDefined(selectedTransition.getChildByName('wmData'));
74    expect(wmData.getChildByName('id')?.formattedValue()).toEqual('32');
75    expect(wmData.getChildByName('type')?.formattedValue()).toEqual('OPEN');
76    expect(wmData.getChildByName('createTimeNs')?.formattedValue()).toEqual(
77      '2023-11-21, 13:30:25.428',
78    );
79  });
80});
81