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 {assertDefined} from 'common/assert_utils';
18import {TimestampConverterUtils} from 'test/unit/timestamp_converter_utils';
19import {UnitTestUtils} from 'test/unit/utils';
20import {CoarseVersion} from 'trace/coarse_version';
21import {Parser} from 'trace/parser';
22import {TraceType} from 'trace/trace_type';
23import {PropertyTreeNode} from 'trace/tree_node/property_tree_node';
24
25describe('Perfetto ParserTransitions', () => {
26  describe('valid trace', () => {
27    let parser: Parser<PropertyTreeNode>;
28
29    beforeAll(async () => {
30      jasmine.addCustomEqualityTester(UnitTestUtils.timestampEqualityTester);
31      parser = await UnitTestUtils.getPerfettoParser(
32        TraceType.TRANSITION,
33        'traces/perfetto/shell_transitions_trace.perfetto-trace',
34      );
35    });
36
37    it('has expected trace type', () => {
38      expect(parser.getTraceType()).toEqual(TraceType.TRANSITION);
39    });
40
41    it('has expected coarse version', () => {
42      expect(parser.getCoarseVersion()).toEqual(CoarseVersion.LATEST);
43    });
44
45    it('provides timestamps', () => {
46      const expected = [
47        TimestampConverterUtils.makeRealTimestamp(1700572945845474854n),
48        TimestampConverterUtils.makeRealTimestamp(1700573425448299306n),
49        TimestampConverterUtils.makeRealTimestamp(1700573426522433299n),
50      ];
51      const actual = assertDefined(parser.getTimestamps()).slice(0, 3);
52      expect(actual).toEqual(expected);
53    });
54
55    it('decodes transition properties', async () => {
56      const entry = await parser.getEntry(1);
57      const wmDataNode = assertDefined(entry.getChildByName('wmData'));
58      const shellDataNode = assertDefined(entry.getChildByName('shellData'));
59
60      expect(entry.getChildByName('id')?.getValue()).toEqual(32n);
61      expect(
62        wmDataNode.getChildByName('createTimeNs')?.formattedValue(),
63      ).toEqual('2023-11-21, 13:30:25.428');
64      expect(wmDataNode.getChildByName('sendTimeNs')?.formattedValue()).toEqual(
65        '2023-11-21, 13:30:25.441',
66      );
67      expect(
68        wmDataNode.getChildByName('finishTimeNs')?.formattedValue(),
69      ).toEqual('2023-11-21, 13:30:25.970');
70      expect(entry.getChildByName('merged')?.getValue()).toBeFalse();
71      expect(entry.getChildByName('played')?.getValue()).toBeTrue();
72      expect(entry.getChildByName('aborted')?.getValue()).toBeFalse();
73
74      expect(
75        assertDefined(
76          wmDataNode.getChildByName('startingWindowRemoveTimeNs'),
77        ).formattedValue(),
78      ).toEqual('2023-11-21, 13:30:25.565');
79      expect(
80        assertDefined(
81          wmDataNode.getChildByName('startTransactionId'),
82        ).formattedValue(),
83      ).toEqual('5811090758076');
84      expect(
85        assertDefined(
86          wmDataNode.getChildByName('finishTransactionId'),
87        ).formattedValue(),
88      ).toEqual('5811090758077');
89      expect(
90        assertDefined(wmDataNode.getChildByName('type')).formattedValue(),
91      ).toEqual('OPEN');
92
93      const targets = assertDefined(
94        wmDataNode.getChildByName('targets'),
95      ).getAllChildren();
96      expect(targets.length).toEqual(2);
97      expect(
98        assertDefined(targets[0].getChildByName('layerId')).formattedValue(),
99      ).toEqual('398');
100      expect(
101        assertDefined(targets[1].getChildByName('layerId')).formattedValue(),
102      ).toEqual('47');
103      expect(
104        assertDefined(targets[0].getChildByName('mode')).formattedValue(),
105      ).toEqual('TO_FRONT');
106      expect(
107        assertDefined(targets[1].getChildByName('mode')).formattedValue(),
108      ).toEqual('TO_BACK');
109
110      expect(
111        assertDefined(
112          shellDataNode.getChildByName('dispatchTimeNs'),
113        ).formattedValue(),
114      ).toEqual('2023-11-21, 13:30:25.448');
115      expect(shellDataNode.getChildByName('mergeRequestTime')).toBeUndefined();
116      expect(shellDataNode.getChildByName('mergeTime')).toBeUndefined();
117      expect(shellDataNode.getChildByName('abortTimeNs')).toBeUndefined();
118      expect(shellDataNode.getChildByName('mergeTarget')).toBeUndefined();
119      expect(
120        assertDefined(shellDataNode.getChildByName('handler')).formattedValue(),
121      ).toEqual('com.android.wm.shell.transition.DefaultMixedHandler');
122    });
123  });
124});
125