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 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 {TimeDuration} from 'common/time_duration';
18import {PropertyTreeBuilder} from 'test/unit/property_tree_builder';
19import {TimestampConverterUtils} from 'test/unit/timestamp_converter_utils';
20import {TIMESTAMP_NODE_FORMATTER} from 'trace/tree_node/formatters';
21import {PropertySource} from 'trace/tree_node/property_tree_node';
22import {AddDuration} from './add_duration';
23
24describe('AddDuration', () => {
25  let operation: AddDuration;
26  const TIMESTAMP_10 = TimestampConverterUtils.makeRealTimestamp(10n);
27  const TIMESTAMP_30 = TimestampConverterUtils.makeRealTimestamp(30n);
28
29  beforeEach(() => {
30    operation = new AddDuration();
31  });
32
33  it('adds duration based on send and finish time', () => {
34    const propertyRoot = new PropertyTreeBuilder()
35      .setIsRoot(true)
36      .setRootId('TransitionsTraceEntry')
37      .setName('transition')
38      .setChildren([
39        {
40          name: 'wmData',
41          children: [
42            {name: 'sendTimeNs', value: TIMESTAMP_10},
43            {name: 'finishTimeNs', value: TIMESTAMP_30},
44          ],
45        },
46      ])
47      .build();
48
49    const expectedRoot = new PropertyTreeBuilder()
50      .setIsRoot(true)
51      .setRootId('TransitionsTraceEntry')
52      .setName('transition')
53      .setChildren([
54        {
55          name: 'wmData',
56          children: [
57            {name: 'sendTimeNs', value: TIMESTAMP_10},
58            {name: 'finishTimeNs', value: TIMESTAMP_30},
59          ],
60        },
61        {
62          name: 'duration',
63          value: new TimeDuration(20n),
64          source: PropertySource.CALCULATED,
65          formatter: TIMESTAMP_NODE_FORMATTER,
66        },
67      ])
68      .build();
69
70    operation.apply(propertyRoot);
71    expect(propertyRoot).toEqual(expectedRoot);
72  });
73
74  it('does not add duration due to missing send time', () => {
75    const propertyRoot = new PropertyTreeBuilder()
76      .setIsRoot(true)
77      .setRootId('TransitionsTraceEntry')
78      .setName('transition')
79      .setChildren([
80        {
81          name: 'wmData',
82          children: [{name: 'finishTimeNs', value: TIMESTAMP_30}],
83        },
84      ])
85      .build();
86
87    const expectedRoot = new PropertyTreeBuilder()
88      .setIsRoot(true)
89      .setRootId('TransitionsTraceEntry')
90      .setName('transition')
91      .setChildren([
92        {
93          name: 'wmData',
94          children: [{name: 'finishTimeNs', value: TIMESTAMP_30}],
95        },
96      ])
97      .build();
98
99    operation.apply(propertyRoot);
100    expect(propertyRoot).toEqual(expectedRoot);
101  });
102
103  it('does not add duration due to missing finish time', () => {
104    const propertyRoot = new PropertyTreeBuilder()
105      .setIsRoot(true)
106      .setRootId('TransitionsTraceEntry')
107      .setName('transition')
108      .setChildren([
109        {name: 'wmData', children: [{name: 'sendTimeNs', value: TIMESTAMP_10}]},
110      ])
111      .build();
112
113    const expectedRoot = new PropertyTreeBuilder()
114      .setIsRoot(true)
115      .setRootId('TransitionsTraceEntry')
116      .setName('transition')
117      .setChildren([
118        {name: 'wmData', children: [{name: 'sendTimeNs', value: TIMESTAMP_10}]},
119      ])
120      .build();
121
122    operation.apply(propertyRoot);
123    expect(propertyRoot).toEqual(expectedRoot);
124  });
125});
126