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 {IDENTITY_MATRIX} from 'common/geometry_types'; 19import {TransformType} from 'parsers/surface_flinger/transform_utils'; 20import {PropertyTreeBuilder} from 'test/unit/property_tree_builder'; 21import {TreeNodeUtils} from 'test/unit/tree_node_utils'; 22import { 23 BUFFER_FORMATTER, 24 COLOR_FORMATTER, 25 DEFAULT_PROPERTY_FORMATTER, 26 EMPTY_ARRAY_STRING, 27 EMPTY_OBJ_STRING, 28 LAYER_ID_FORMATTER, 29 MATRIX_FORMATTER, 30 POSITION_FORMATTER, 31 RECT_FORMATTER, 32 REGION_FORMATTER, 33 SIZE_FORMATTER, 34 TRANSFORM_FORMATTER, 35} from './formatters'; 36import {PropertySource, PropertyTreeNode} from './property_tree_node'; 37 38describe('Formatters', () => { 39 describe('PropertyFormatter', () => { 40 it('translates simple values correctly', () => { 41 expect( 42 DEFAULT_PROPERTY_FORMATTER.format( 43 new PropertyTreeNode('', '', PropertySource.PROTO, 12345), 44 ), 45 ).toEqual('12345'); 46 expect( 47 DEFAULT_PROPERTY_FORMATTER.format( 48 new PropertyTreeNode('', '', PropertySource.PROTO, 'test_string'), 49 ), 50 ).toEqual('test_string'); 51 expect( 52 DEFAULT_PROPERTY_FORMATTER.format( 53 new PropertyTreeNode('', '', PropertySource.PROTO, 0.1234), 54 ), 55 ).toEqual('0.123'); 56 expect( 57 DEFAULT_PROPERTY_FORMATTER.format( 58 new PropertyTreeNode('', '', PropertySource.PROTO, 1.5), 59 ), 60 ).toEqual('1.500'); 61 }); 62 63 it('translates values with toString method correctly', () => { 64 expect( 65 DEFAULT_PROPERTY_FORMATTER.format( 66 new PropertyTreeNode('', '', PropertySource.PROTO, BigInt(123)), 67 ), 68 ).toEqual('123'); 69 }); 70 71 it('translates default values correctly', () => { 72 expect( 73 DEFAULT_PROPERTY_FORMATTER.format( 74 new PropertyTreeNode('', '', PropertySource.PROTO, []), 75 ), 76 ).toEqual(EMPTY_ARRAY_STRING); 77 expect( 78 DEFAULT_PROPERTY_FORMATTER.format( 79 new PropertyTreeNode('', '', PropertySource.PROTO, false), 80 ), 81 ).toEqual('false'); 82 expect( 83 DEFAULT_PROPERTY_FORMATTER.format( 84 new PropertyTreeNode('', '', PropertySource.PROTO, null), 85 ), 86 ).toEqual('null'); 87 }); 88 }); 89 90 describe('ColorFormatter', () => { 91 it('translates empty color to string correctly', () => { 92 expect( 93 COLOR_FORMATTER.format(TreeNodeUtils.makeColorNode(-1, -1, -1, 1)), 94 ).toEqual(`${EMPTY_OBJ_STRING}, alpha: 1`); 95 expect( 96 COLOR_FORMATTER.format(TreeNodeUtils.makeColorNode(1, 1, 1, 0)), 97 ).toEqual(`${EMPTY_OBJ_STRING}, alpha: 0`); 98 }); 99 100 it('translates non-empty color to string correctly', () => { 101 expect( 102 COLOR_FORMATTER.format(TreeNodeUtils.makeColorNode(1, 2, 3, 1)), 103 ).toEqual('(1, 2, 3, 1)'); 104 expect( 105 COLOR_FORMATTER.format(TreeNodeUtils.makeColorNode(1, 2, 3, 0.608)), 106 ).toEqual('(1, 2, 3, 0.608)'); 107 }); 108 109 it('translates rgb color without alpha to string correctly (transactions)', () => { 110 expect( 111 COLOR_FORMATTER.format(TreeNodeUtils.makeColorNode(1, 2, 3, undefined)), 112 ).toEqual('(1, 2, 3)'); 113 expect( 114 COLOR_FORMATTER.format( 115 TreeNodeUtils.makeColorNode(0.106, 0.203, 0.313, undefined), 116 ), 117 ).toEqual('(0.106, 0.203, 0.313)'); 118 }); 119 }); 120 121 describe('RectFormatter', () => { 122 it('translates empty rect to string correctly', () => { 123 expect( 124 RECT_FORMATTER.format(TreeNodeUtils.makeRectNode(0, 0, -1, -1)), 125 ).toEqual(EMPTY_OBJ_STRING); 126 expect( 127 RECT_FORMATTER.format(TreeNodeUtils.makeRectNode(0, 0, 0, 0)), 128 ).toEqual(EMPTY_OBJ_STRING); 129 }); 130 131 it('translates non-empty rect to string correctly', () => { 132 expect( 133 RECT_FORMATTER.format(TreeNodeUtils.makeRectNode(0, 0, 1, 1)), 134 ).toEqual('(0, 0) - (1, 1)'); 135 expect( 136 RECT_FORMATTER.format(TreeNodeUtils.makeRectNode(0, 0, 10, 10)), 137 ).toEqual('(0, 0) - (10, 10)'); 138 expect( 139 RECT_FORMATTER.format( 140 TreeNodeUtils.makeRectNode(0, 1.6431, 10456.9086, 10), 141 ), 142 ).toEqual('(0, 1.643) - (10456.909, 10)'); 143 }); 144 }); 145 146 describe('BufferFormatter', () => { 147 it('translates buffer to string correctly', () => { 148 const buffer = TreeNodeUtils.makeBufferNode(); 149 expect(BUFFER_FORMATTER.format(buffer)).toEqual( 150 'w: 1, h: 0, stride: 0, format: 1', 151 ); 152 }); 153 }); 154 155 describe('LayerIdFormatter', () => { 156 it('translates -1 id correctly', () => { 157 expect( 158 LAYER_ID_FORMATTER.format( 159 new PropertyTreeNode('', '', PropertySource.PROTO, -1), 160 ), 161 ).toEqual('none'); 162 }); 163 164 it('translates valid id correctly', () => { 165 expect( 166 LAYER_ID_FORMATTER.format( 167 new PropertyTreeNode('', '', PropertySource.PROTO, 1), 168 ), 169 ).toEqual('1'); 170 expect( 171 LAYER_ID_FORMATTER.format( 172 new PropertyTreeNode('', '', PropertySource.PROTO, -10), 173 ), 174 ).toEqual('-10'); 175 }); 176 }); 177 178 describe('MatrixFormatter', () => { 179 it('translates matrix correctly', () => { 180 expect( 181 MATRIX_FORMATTER.format( 182 TreeNodeUtils.makeMatrixNode( 183 IDENTITY_MATRIX.dsdx, 184 IDENTITY_MATRIX.dtdx, 185 IDENTITY_MATRIX.dsdy, 186 IDENTITY_MATRIX.dtdy, 187 ), 188 ), 189 ).toEqual('dsdx: 1, dtdx: 0, dsdy: 0, dtdy: 1'); 190 expect( 191 MATRIX_FORMATTER.format( 192 TreeNodeUtils.makeMatrixNode(0.4, 100, 1, 0.1232), 193 ), 194 ).toEqual('dsdx: 0.400, dtdx: 100, dsdy: 1, dtdy: 0.123'); 195 expect( 196 MATRIX_FORMATTER.format(TreeNodeUtils.makeMatrixNode(0, 0, 0, 0)), 197 ).toEqual('null'); 198 expect( 199 MATRIX_FORMATTER.format( 200 TreeNodeUtils.makePropertyNode('test node', 'transform', { 201 dsdx: 1, 202 dtdx: 0, 203 dsdy: 0, 204 dtdy: 1, 205 tx: 5, 206 ty: 10, 207 }), 208 ), 209 ).toEqual('dsdx: 1, dtdx: 0, dsdy: 0, dtdy: 1, tx: 5, ty: 10'); 210 }); 211 }); 212 213 describe('TransformFormatter', () => { 214 it('translates type correctly', () => { 215 expect( 216 TRANSFORM_FORMATTER.format( 217 TreeNodeUtils.makeTransformNode(TransformType.EMPTY), 218 ), 219 ).toEqual('IDENTITY'); 220 expect( 221 TRANSFORM_FORMATTER.format( 222 TreeNodeUtils.makeTransformNode(TransformType.TRANSLATE_VAL), 223 ), 224 ).toEqual('TRANSLATE'); 225 expect( 226 TRANSFORM_FORMATTER.format( 227 TreeNodeUtils.makeTransformNode(TransformType.SCALE_VAL), 228 ), 229 ).toEqual('SCALE'); 230 expect( 231 TRANSFORM_FORMATTER.format( 232 TreeNodeUtils.makeTransformNode(TransformType.FLIP_H_VAL), 233 ), 234 ).toEqual('IDENTITY|FLIP_H'); 235 expect( 236 TRANSFORM_FORMATTER.format( 237 TreeNodeUtils.makeTransformNode(TransformType.FLIP_V_VAL), 238 ), 239 ).toEqual('IDENTITY|FLIP_V'); 240 expect( 241 TRANSFORM_FORMATTER.format( 242 TreeNodeUtils.makeTransformNode(TransformType.ROT_90_VAL), 243 ), 244 ).toEqual('IDENTITY|ROT_90'); 245 expect( 246 TRANSFORM_FORMATTER.format( 247 TreeNodeUtils.makeTransformNode(TransformType.ROT_INVALID_VAL), 248 ), 249 ).toEqual('IDENTITY|ROT_INVALID'); 250 }); 251 }); 252 253 describe('SizeFormatter', () => { 254 it('translates size correctly', () => { 255 expect(SIZE_FORMATTER.format(TreeNodeUtils.makeSizeNode(1, 2))).toEqual( 256 '1 x 2', 257 ); 258 }); 259 }); 260 261 describe('PositionFormatter', () => { 262 it('translates position correctly', () => { 263 expect( 264 POSITION_FORMATTER.format(TreeNodeUtils.makePositionNode(1, 2)), 265 ).toEqual('x: 1, y: 2'); 266 expect( 267 POSITION_FORMATTER.format(TreeNodeUtils.makePositionNode(1.5, 2.2916)), 268 ).toEqual('x: 1.500, y: 2.292'); 269 }); 270 }); 271 272 describe('RegionFormatter', () => { 273 it('translates region correctly', () => { 274 const region = new PropertyTreeBuilder() 275 .setRootId('test node') 276 .setName('region') 277 .setChildren([{name: 'rect', value: []}]) 278 .build(); 279 280 const rectNode = assertDefined(region.getChildByName('rect')); 281 rectNode.addOrReplaceChild(TreeNodeUtils.makeRectNode(0, 0, 1080, 2340)); 282 283 expect(REGION_FORMATTER.format(region)).toEqual( 284 'SkRegion((0, 0, 1080, 2340))', 285 ); 286 }); 287 }); 288}); 289