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 {PropertyTreeBuilder} from 'test/unit/property_tree_builder';
18import {PropertySource} from 'trace/tree_node/property_tree_node';
19import {AddRootProperties} from './add_root_properties';
20
21describe('AddRootProperties', () => {
22  let operation: AddRootProperties;
23
24  beforeEach(() => {
25    operation = new AddRootProperties();
26  });
27
28  it('adds properties from wmData to root', () => {
29    const propertyRoot = new PropertyTreeBuilder()
30      .setIsRoot(true)
31      .setRootId('TransitionsTraceEntry')
32      .setName('transition')
33      .setChildren([
34        {
35          name: 'wmData',
36          children: [{name: 'id', value: 10}],
37        },
38        {name: 'shellData', value: null},
39        {name: 'duration', value: 5n, source: PropertySource.CALCULATED},
40      ])
41      .build();
42
43    const expectedRoot = new PropertyTreeBuilder()
44      .setIsRoot(true)
45      .setRootId('TransitionsTraceEntry')
46      .setName('transition')
47      .setChildren([
48        {
49          name: 'wmData',
50          children: [{name: 'id', value: 10}],
51        },
52        {name: 'shellData', value: null},
53        {name: 'duration', value: 5n, source: PropertySource.CALCULATED},
54        {name: 'id', value: 10, source: PropertySource.CALCULATED},
55      ])
56      .build();
57
58    operation.apply(propertyRoot);
59    expect(propertyRoot).toEqual(expectedRoot);
60  });
61
62  it('adds properties from shellData to root', () => {
63    const propertyRoot = new PropertyTreeBuilder()
64      .setIsRoot(true)
65      .setRootId('TransitionsTraceEntry')
66      .setName('transition')
67      .setChildren([
68        {name: 'wmData', value: null},
69        {
70          name: 'shellData',
71          children: [{name: 'id', value: 10}],
72        },
73      ])
74      .build();
75
76    const expectedRoot = new PropertyTreeBuilder()
77      .setIsRoot(true)
78      .setRootId('TransitionsTraceEntry')
79      .setName('transition')
80      .setChildren([
81        {name: 'wmData', value: null},
82        {
83          name: 'shellData',
84          children: [{name: 'id', value: 10}],
85        },
86        {name: 'id', value: 10, source: PropertySource.CALCULATED},
87      ])
88      .build();
89
90    operation.apply(propertyRoot);
91    expect(propertyRoot).toEqual(expectedRoot);
92  });
93
94  it('takes properties from wmData over shellData', () => {
95    const propertyRoot = new PropertyTreeBuilder()
96      .setIsRoot(true)
97      .setRootId('TransitionsTraceEntry')
98      .setName('transition')
99      .setChildren([
100        {
101          name: 'wmData',
102          children: [{name: 'id', value: 10}],
103        },
104        {
105          name: 'shellData',
106          children: [{name: 'id', value: 20}],
107        },
108        {name: 'aborted', value: true, source: PropertySource.CALCULATED},
109      ])
110      .build();
111
112    const expectedRoot = new PropertyTreeBuilder()
113      .setIsRoot(true)
114      .setRootId('TransitionsTraceEntry')
115      .setName('transition')
116      .setChildren([
117        {
118          name: 'wmData',
119          children: [{name: 'id', value: 10}],
120        },
121        {
122          name: 'shellData',
123          children: [{name: 'id', value: 20}],
124        },
125        {name: 'aborted', value: true, source: PropertySource.CALCULATED},
126        {name: 'id', value: 10, source: PropertySource.CALCULATED},
127      ])
128      .build();
129
130    operation.apply(propertyRoot);
131    expect(propertyRoot).toEqual(expectedRoot);
132  });
133});
134