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 {com} from 'protos/windowmanager/latest/static';
19import {HierarchyTreeBuilder} from 'test/unit/hierarchy_tree_builder';
20import {TreeNodeUtils} from 'test/unit/tree_node_utils';
21import {TraceRect} from 'trace/trace_rect';
22import {TraceRectBuilder} from 'trace/trace_rect_builder';
23import {HierarchyTreeNode} from 'trace/tree_node/hierarchy_tree_node';
24import {RectsComputation} from './rects_computation';
25
26describe('RectsComputation', () => {
27  let hierarchyRoot: HierarchyTreeNode;
28  let computation: RectsComputation;
29  let displayContent: HierarchyTreeNode;
30
31  beforeEach(() => {
32    hierarchyRoot = new HierarchyTreeBuilder()
33      .setId('WindowManagerState')
34      .setName('root')
35      .setChildren([
36        {
37          id: 1,
38          name: 'Test Display',
39          properties: {
40            id: 1,
41            name: 'Test Display',
42            displayInfo: {
43              logicalWidth: 5,
44              logicalHeight: 5,
45            },
46          } as com.android.server.wm.IDisplayContentProto,
47        },
48      ])
49      .build();
50    displayContent = assertDefined(
51      hierarchyRoot.getChildByName('Test Display'),
52    );
53    computation = new RectsComputation();
54  });
55
56  it('makes display rects', () => {
57    const expectedDisplayRects = [
58      new TraceRectBuilder()
59        .setX(0)
60        .setY(0)
61        .setWidth(5)
62        .setHeight(5)
63        .setId('1 Test Display')
64        .setName('Display - Test Display')
65        .setCornerRadius(0)
66        .setDepth(0)
67        .setGroupId(1)
68        .setIsVisible(false)
69        .setIsDisplay(true)
70        .setIsVirtual(false)
71        .build(),
72    ];
73
74    computation.setRoot(hierarchyRoot).executeInPlace();
75    expect(displayContent.getRects()).toEqual(expectedDisplayRects);
76  });
77
78  it('makes window state rects', () => {
79    const state1Node = TreeNodeUtils.makeHierarchyNode({
80      id: 'WindowState',
81      name: 'state1',
82      displayId: 1,
83      isComputedVisible: true,
84      windowFrames: {
85        frame: {left: 0, top: 0, right: 1, bottom: 1},
86      },
87      attributes: {
88        alpha: 0.5,
89      },
90    } as com.android.server.wm.IWindowStateProto);
91
92    state1Node.addOrReplaceChild(
93      TreeNodeUtils.makeHierarchyNode({
94        id: 'WindowState',
95        name: 'state2',
96        displayId: 1,
97        isComputedVisible: false,
98        windowFrames: {
99          frame: {left: 0, top: 0, right: 2, bottom: 2},
100        },
101        attributes: {
102          alpha: 0.5,
103        },
104      } as com.android.server.wm.IWindowStateProto),
105    );
106    displayContent.addOrReplaceChild(state1Node);
107
108    const expectedRects: TraceRect[] = [
109      new TraceRectBuilder()
110        .setX(0)
111        .setY(0)
112        .setWidth(1)
113        .setHeight(1)
114        .setId('WindowState state1')
115        .setName('state1')
116        .setCornerRadius(0)
117        .setDepth(1)
118        .setGroupId(1)
119        .setIsVisible(true)
120        .setIsDisplay(false)
121        .setIsVirtual(false)
122        .setOpacity(0.5)
123        .build(),
124
125      new TraceRectBuilder()
126        .setX(0)
127        .setY(0)
128        .setWidth(2)
129        .setHeight(2)
130        .setId('WindowState state2')
131        .setName('state2')
132        .setCornerRadius(0)
133        .setDepth(2)
134        .setGroupId(1)
135        .setIsVisible(false)
136        .setIsDisplay(false)
137        .setIsVirtual(false)
138        .setOpacity(0.5)
139        .build(),
140    ];
141
142    computation.setRoot(hierarchyRoot).executeInPlace();
143
144    const rects: TraceRect[] = [];
145    hierarchyRoot.forEachNodeDfs((node) => {
146      const nodeRects = node.getRects();
147      if (nodeRects && nodeRects.every((rect) => !rect.isDisplay)) {
148        rects.push(...nodeRects);
149      }
150    });
151
152    expect(rects).toEqual(expectedRects);
153  });
154});
155