xref: /aosp_15_r20/development/tools/winscope/src/parsers/raw_data_utils_test.ts (revision 90c8c64db3049935a07c6143d7fd006e26f8ecca)
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 {PropertyTreeBuilder} from 'test/unit/property_tree_builder';
19import {TreeNodeUtils} from 'test/unit/tree_node_utils';
20import {RawDataUtils} from './raw_data_utils';
21
22describe('RawDataUtils', () => {
23  it('identifies color', () => {
24    const color = TreeNodeUtils.makeColorNode(0, 0, 0, 1);
25    expect(RawDataUtils.isColor(color)).toBeTrue();
26
27    const colorOnlyA = TreeNodeUtils.makeColorNode(
28      undefined,
29      undefined,
30      undefined,
31      1,
32    );
33    expect(RawDataUtils.isColor(colorOnlyA)).toBeTrue();
34  });
35
36  it('identifies rect', () => {
37    const rect = TreeNodeUtils.makeRectNode(0, 0, 1, 1);
38    expect(RawDataUtils.isRect(rect)).toBeTrue();
39
40    const rectLeftTop = TreeNodeUtils.makeRectNode(0, 0, undefined, undefined);
41    expect(RawDataUtils.isRect(rectLeftTop)).toBeTrue();
42
43    const rectRightBottom = TreeNodeUtils.makeRectNode(
44      undefined,
45      undefined,
46      1,
47      1,
48    );
49    expect(RawDataUtils.isRect(rectRightBottom)).toBeTrue();
50  });
51
52  it('identifies buffer', () => {
53    const buffer = TreeNodeUtils.makeBufferNode();
54    expect(RawDataUtils.isBuffer(buffer)).toBeTrue();
55  });
56
57  it('identifies size', () => {
58    const size = TreeNodeUtils.makeSizeNode(0, 0);
59    expect(RawDataUtils.isSize(size)).toBeTrue();
60    expect(RawDataUtils.isBuffer(size)).toBeFalse();
61
62    const sizeOnlyW = TreeNodeUtils.makeSizeNode(0, undefined);
63    expect(RawDataUtils.isSize(sizeOnlyW)).toBeTrue();
64
65    const sizeOnlyH = TreeNodeUtils.makeSizeNode(undefined, 0);
66    expect(RawDataUtils.isSize(sizeOnlyH)).toBeTrue();
67
68    const notSize = new PropertyTreeBuilder()
69      .setRootId('test node')
70      .setName('size')
71      .setChildren([
72        {name: 'w', value: 0},
73        {name: 'h', value: 0},
74        {name: 'x', value: 0},
75        {name: 'y', value: 0},
76      ])
77      .build();
78
79    expect(RawDataUtils.isSize(notSize)).toBeFalse();
80  });
81
82  it('identifies position', () => {
83    const pos = TreeNodeUtils.makePositionNode(0, 0);
84    expect(RawDataUtils.isPosition(pos)).toBeTrue();
85    expect(RawDataUtils.isRect(pos)).toBeFalse();
86
87    const posOnlyX = TreeNodeUtils.makePositionNode(0, undefined);
88    expect(RawDataUtils.isPosition(posOnlyX)).toBeTrue();
89
90    const posOnlyY = TreeNodeUtils.makePositionNode(undefined, 0);
91    expect(RawDataUtils.isPosition(posOnlyY)).toBeTrue();
92
93    const notPos = new PropertyTreeBuilder()
94      .setRootId('test node')
95      .setName('pos')
96      .setChildren([
97        {name: 'w', value: 0},
98        {name: 'h', value: 0},
99        {name: 'x', value: 0},
100        {name: 'y', value: 0},
101      ])
102      .build();
103
104    expect(RawDataUtils.isPosition(notPos)).toBeFalse();
105  });
106
107  it('identifies region', () => {
108    const region = new PropertyTreeBuilder()
109      .setRootId('test node')
110      .setName('region')
111      .setChildren([{name: 'rect', value: []}])
112      .build();
113    expect(RawDataUtils.isRegion(region)).toBeTrue();
114
115    const rectNode = assertDefined(region.getChildByName('rect'));
116    rectNode.addOrReplaceChild(
117      TreeNodeUtils.makeRectNode(0, 0, 1, 1, rectNode.id),
118    );
119    rectNode.addOrReplaceChild(
120      TreeNodeUtils.makeRectNode(0, 0, undefined, undefined, rectNode.id),
121    );
122    rectNode.addOrReplaceChild(
123      TreeNodeUtils.makeRectNode(undefined, undefined, 1, 1, rectNode.id),
124    );
125    expect(RawDataUtils.isRegion(region)).toBeTrue();
126
127    rectNode.addOrReplaceChild(TreeNodeUtils.makeColorNode(0, 0, 0, 0));
128    expect(RawDataUtils.isRegion(region)).toBeFalse();
129  });
130
131  describe('identifies empty object', () => {
132    it('rect', () => {
133      const rectWithUndefinedValues = TreeNodeUtils.makeRectNode(
134        0,
135        0,
136        undefined,
137        undefined,
138      );
139      expect(RawDataUtils.isEmptyObj(rectWithUndefinedValues)).toBeTrue();
140
141      const rectAllZeroValues = TreeNodeUtils.makeRectNode(0, 0, 0, 0);
142      expect(RawDataUtils.isEmptyObj(rectAllZeroValues)).toBeTrue();
143
144      const rectWithMinusOneValues = TreeNodeUtils.makeRectNode(0, 0, -1, -1);
145      expect(RawDataUtils.isEmptyObj(rectWithMinusOneValues)).toBeTrue();
146    });
147
148    it('color', () => {
149      const bMinusOne = TreeNodeUtils.makeColorNode(153, 23, -1, 1);
150      expect(RawDataUtils.isEmptyObj(bMinusOne)).toBeTrue();
151
152      const rgbMinusOne = TreeNodeUtils.makeColorNode(-1, -1, -1, 0.9);
153      expect(RawDataUtils.isEmptyObj(rgbMinusOne)).toBeTrue();
154
155      const alphaZero = TreeNodeUtils.makeColorNode(1, 1, 1, 0);
156      expect(RawDataUtils.isEmptyObj(alphaZero)).toBeTrue();
157    });
158  });
159
160  describe('identifies non-empty object', () => {
161    it('rect', () => {
162      const rect = TreeNodeUtils.makeRectNode(0, 0, 1, 1);
163      expect(RawDataUtils.isEmptyObj(rect)).toBeFalse();
164    });
165
166    it('color', () => {
167      const color = TreeNodeUtils.makeColorNode(0, 8, 0, 1);
168      expect(RawDataUtils.isEmptyObj(color)).toBeFalse();
169
170      const missingB = TreeNodeUtils.makeColorNode(153, 23, undefined, 1);
171      expect(RawDataUtils.isEmptyObj(missingB)).toBeFalse();
172
173      const rgbZeroAlphaNonZero = TreeNodeUtils.makeColorNode(0, 0, 0, 0.7);
174      expect(RawDataUtils.isEmptyObj(rgbZeroAlphaNonZero)).toBeFalse();
175
176      const rgbZeroAlphaOne = TreeNodeUtils.makeColorNode(0, 0, 0, 1);
177      expect(RawDataUtils.isEmptyObj(rgbZeroAlphaOne)).toBeFalse();
178    });
179  });
180});
181