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 {assertDefined} from 'common/assert_utils';
18import {HierarchyTreeBuilder} from 'test/unit/hierarchy_tree_builder';
19import {LayerCompositionType} from 'trace/layer_composition_type';
20import {
21  DUPLICATE_CHIP,
22  GPU_CHIP,
23  HWC_CHIP,
24  MISSING_Z_PARENT_CHIP,
25  RELATIVE_Z_CHIP,
26  RELATIVE_Z_PARENT_CHIP,
27  VISIBLE_CHIP,
28} from 'viewers/common/chip';
29import {UiHierarchyTreeNode} from 'viewers/common/ui_hierarchy_tree_node';
30import {AddChips} from './add_chips';
31
32describe('AddChips', () => {
33  let hierarchyRoot: UiHierarchyTreeNode;
34  let operation: AddChips;
35
36  beforeEach(() => {
37    operation = new AddChips();
38  });
39
40  it('adds GPU_CHIP', () => {
41    hierarchyRoot = UiHierarchyTreeNode.from(
42      new HierarchyTreeBuilder()
43        .setId('test')
44        .setName('node')
45        .setChildren([
46          {
47            id: 1,
48            name: 'node',
49            properties: {compositionType: LayerCompositionType.GPU},
50          },
51        ])
52        .build(),
53    );
54
55    operation.apply(hierarchyRoot);
56    expect(
57      assertDefined(hierarchyRoot.getChildByName('node')).getChips(),
58    ).toEqual([GPU_CHIP]);
59  });
60
61  it('adds HWC_CHIP', () => {
62    hierarchyRoot = UiHierarchyTreeNode.from(
63      new HierarchyTreeBuilder()
64        .setId('test')
65        .setName('node')
66        .setChildren([
67          {
68            id: 1,
69            name: 'node',
70            properties: {compositionType: LayerCompositionType.HWC},
71          },
72        ])
73        .build(),
74    );
75
76    operation.apply(hierarchyRoot);
77    expect(
78      assertDefined(hierarchyRoot.getChildByName('node')).getChips(),
79    ).toEqual([HWC_CHIP]);
80  });
81
82  it('adds VISIBLE_CHIP', () => {
83    hierarchyRoot = UiHierarchyTreeNode.from(
84      new HierarchyTreeBuilder()
85        .setId('test')
86        .setName('node')
87        .setChildren([
88          {
89            id: 1,
90            name: 'node',
91            properties: {isComputedVisible: true},
92          },
93        ])
94        .build(),
95    );
96
97    operation.apply(hierarchyRoot);
98    expect(
99      assertDefined(hierarchyRoot.getChildByName('node')).getChips(),
100    ).toEqual([VISIBLE_CHIP]);
101  });
102
103  it('adds DUPLICATE_CHIP', () => {
104    hierarchyRoot = UiHierarchyTreeNode.from(
105      new HierarchyTreeBuilder()
106        .setId('test')
107        .setName('node')
108        .setChildren([
109          {
110            id: 1,
111            name: 'node',
112            properties: {isDuplicate: true},
113          },
114        ])
115        .build(),
116    );
117
118    operation.apply(hierarchyRoot);
119    expect(
120      assertDefined(hierarchyRoot.getChildByName('node')).getChips(),
121    ).toEqual([DUPLICATE_CHIP]);
122  });
123
124  it('adds RELATIVE_Z_CHIP', () => {
125    hierarchyRoot = UiHierarchyTreeNode.from(
126      new HierarchyTreeBuilder()
127        .setId('test')
128        .setName('node')
129        .setChildren([
130          {
131            id: 1,
132            name: 'node',
133            properties: {zOrderRelativeOf: 2},
134          },
135        ])
136        .build(),
137    );
138
139    operation.apply(hierarchyRoot);
140    expect(
141      assertDefined(hierarchyRoot.getChildByName('node')).getChips(),
142    ).toEqual([RELATIVE_Z_CHIP]);
143  });
144
145  it('adds MISSING_Z_PARENT_CHIP', () => {
146    hierarchyRoot = UiHierarchyTreeNode.from(
147      new HierarchyTreeBuilder()
148        .setId('test')
149        .setName('node')
150        .setChildren([
151          {
152            id: 1,
153            name: 'node',
154            properties: {zOrderRelativeOf: 2, isMissingZParent: true},
155          },
156        ])
157        .build(),
158    );
159
160    operation.apply(hierarchyRoot);
161    expect(
162      assertDefined(hierarchyRoot.getChildByName('node')).getChips(),
163    ).toEqual([RELATIVE_Z_CHIP, MISSING_Z_PARENT_CHIP]);
164  });
165
166  it('adds RELATIVE_Z_PARENT_CHIP', () => {
167    hierarchyRoot = UiHierarchyTreeNode.from(
168      new HierarchyTreeBuilder()
169        .setId('test')
170        .setName('node')
171        .setChildren([
172          {
173            id: 2,
174            name: 'parentNode',
175            properties: {id: 2, zOrderRelativeOf: -1},
176            children: [
177              {
178                id: 1,
179                name: 'node',
180                properties: {id: 1, zOrderRelativeOf: 2},
181              },
182            ],
183          },
184        ])
185        .build(),
186    );
187
188    operation.apply(hierarchyRoot);
189    const parentWithChips = assertDefined(
190      hierarchyRoot.getChildByName('parentNode'),
191    );
192    expect(parentWithChips.getChips()).toEqual([RELATIVE_Z_PARENT_CHIP]);
193    expect(
194      assertDefined(parentWithChips.getChildByName('node')).getChips(),
195    ).toEqual([RELATIVE_Z_CHIP]);
196  });
197});
198