blob: f82b01224f969c5a174cdeed794c254138018e34 [file] [log] [blame]
Adrian Roos1cf585052018-01-03 18:43:27 +01001/*
2 * Copyright (C) 2018 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
17package com.android.server.wm.utils;
18
19import static android.view.Surface.ROTATION_0;
20import static android.view.Surface.ROTATION_180;
21import static android.view.Surface.ROTATION_270;
22import static android.view.Surface.ROTATION_90;
23
Adrian Roos41f7e9d2018-06-07 15:29:34 +020024import static com.android.server.wm.utils.CoordinateTransforms.transformLogicalToPhysicalCoordinates;
Adrian Roos1cf585052018-01-03 18:43:27 +010025import static com.android.server.wm.utils.CoordinateTransforms.transformPhysicalToLogicalCoordinates;
26
Adrian Roos41f7e9d2018-06-07 15:29:34 +020027
28import static com.android.server.wm.utils.CoordinateTransforms.transformToRotation;
29
Adrian Roos1cf585052018-01-03 18:43:27 +010030import static org.hamcrest.Matchers.is;
31import static org.junit.Assert.*;
32
33import android.graphics.Matrix;
34import android.graphics.Point;
35import android.graphics.PointF;
Adrian Roos41f7e9d2018-06-07 15:29:34 +020036import android.view.DisplayInfo;
Adrian Roos1cf585052018-01-03 18:43:27 +010037
38import org.junit.Before;
39import org.junit.Rule;
40import org.junit.Test;
41import org.junit.rules.ErrorCollector;
42
43public class CoordinateTransformsTest {
44
45 private static final int W = 200;
46 private static final int H = 400;
47
48 private final Matrix mMatrix = new Matrix();
Adrian Roos41f7e9d2018-06-07 15:29:34 +020049 private final Matrix mMatrix2 = new Matrix();
Adrian Roos1cf585052018-01-03 18:43:27 +010050
51 @Rule
52 public final ErrorCollector mErrorCollector = new ErrorCollector();
53
54 @Before
55 public void setUp() throws Exception {
56 mMatrix.setTranslate(0xdeadbeef, 0xdeadbeef);
Adrian Roos41f7e9d2018-06-07 15:29:34 +020057 mMatrix2.setTranslate(0xbeefdead, 0xbeefdead);
Adrian Roos1cf585052018-01-03 18:43:27 +010058 }
59
60 @Test
Adrian Roos41f7e9d2018-06-07 15:29:34 +020061 public void transformPhysicalToLogicalCoordinates_rot0() {
Adrian Roos1cf585052018-01-03 18:43:27 +010062 transformPhysicalToLogicalCoordinates(ROTATION_0, W, H, mMatrix);
63 assertThat(mMatrix, is(Matrix.IDENTITY_MATRIX));
64 }
65
66 @Test
Adrian Roos41f7e9d2018-06-07 15:29:34 +020067 public void transformPhysicalToLogicalCoordinates_rot90() {
Adrian Roos1cf585052018-01-03 18:43:27 +010068 transformPhysicalToLogicalCoordinates(ROTATION_90, W, H, mMatrix);
69
Adrian Roos41f7e9d2018-06-07 15:29:34 +020070 checkPoint(0, 0).transformsTo(0, W);
71 checkPoint(W, H).transformsTo(H, 0);
Adrian Roos1cf585052018-01-03 18:43:27 +010072 }
73
74 @Test
Adrian Roos41f7e9d2018-06-07 15:29:34 +020075 public void transformPhysicalToLogicalCoordinates_rot180() {
Adrian Roos1cf585052018-01-03 18:43:27 +010076 transformPhysicalToLogicalCoordinates(ROTATION_180, W, H, mMatrix);
77
Adrian Roos41f7e9d2018-06-07 15:29:34 +020078 checkPoint(0, 0).transformsTo(W, H);
79 checkPoint(W, H).transformsTo(0, 0);
Adrian Roos1cf585052018-01-03 18:43:27 +010080 }
81
82 @Test
Adrian Roos41f7e9d2018-06-07 15:29:34 +020083 public void transformPhysicalToLogicalCoordinates_rot270() {
Adrian Roos1cf585052018-01-03 18:43:27 +010084 transformPhysicalToLogicalCoordinates(ROTATION_270, W, H, mMatrix);
85
Adrian Roos41f7e9d2018-06-07 15:29:34 +020086 checkPoint(0, 0).transformsTo(H, 0);
87 checkPoint(W, H).transformsTo(0, W);
Adrian Roos1cf585052018-01-03 18:43:27 +010088 }
89
Adrian Roos41f7e9d2018-06-07 15:29:34 +020090 @Test
91 public void transformLogicalToPhysicalCoordinates_rot0() {
92 transformLogicalToPhysicalCoordinates(ROTATION_0, W, H, mMatrix);
93 assertThat(mMatrix, is(Matrix.IDENTITY_MATRIX));
94 }
95
96 @Test
97 public void transformLogicalToPhysicalCoordinates_rot90() {
98 transformLogicalToPhysicalCoordinates(ROTATION_90, W, H, mMatrix);
99
100 checkPoint(0, W).transformsTo(0, 0);
101 checkPoint(H, 0).transformsTo(W, H);
Adrian Roos28c25e22018-05-31 18:07:28 +0200102 }
Adrian Roos41f7e9d2018-06-07 15:29:34 +0200103
104 @Test
105 public void transformLogicalToPhysicalCoordinates_rot180() {
106 transformLogicalToPhysicalCoordinates(ROTATION_180, W, H, mMatrix);
107
108 checkPoint(W, H).transformsTo(0, 0);
109 checkPoint(0, 0).transformsTo(W, H);
110 }
111
112 @Test
113 public void transformLogicalToPhysicalCoordinates_rot270() {
114 transformLogicalToPhysicalCoordinates(ROTATION_270, W, H, mMatrix);
115
116 checkPoint(H, 0).transformsTo(0, 0);
117 checkPoint(0, W).transformsTo(W, H);
118 }
119
120 @Test
121 public void transformLogicalToPhysicalCoordinatesIsInverse_rot0() {
122 transformLogicalToPhysicalCoordinates(ROTATION_0, W, H, mMatrix);
123 transformPhysicalToLogicalCoordinates(ROTATION_0, W, H, mMatrix2);
124
125 assertMatricesAreInverses(mMatrix, mMatrix2);
126 }
127
128 @Test
129 public void transformLogicalToPhysicalCoordinatesIsInverse_rot90() {
130 transformLogicalToPhysicalCoordinates(ROTATION_90, W, H, mMatrix);
131 transformPhysicalToLogicalCoordinates(ROTATION_90, W, H, mMatrix2);
132
133 assertMatricesAreInverses(mMatrix, mMatrix2);
134 }
135
136 @Test
137 public void transformLogicalToPhysicalCoordinatesIsInverse_rot180() {
138 transformLogicalToPhysicalCoordinates(ROTATION_180, W, H, mMatrix);
139 transformPhysicalToLogicalCoordinates(ROTATION_180, W, H, mMatrix2);
140
141 assertMatricesAreInverses(mMatrix, mMatrix2);
142 }
143
144 @Test
145 public void transformLogicalToPhysicalCoordinatesIsInverse_rot270() {
146 transformLogicalToPhysicalCoordinates(ROTATION_270, W, H, mMatrix);
147 transformPhysicalToLogicalCoordinates(ROTATION_270, W, H, mMatrix2);
148
149 assertMatricesAreInverses(mMatrix, mMatrix2);
150 }
151
152 @Test
153 public void transformBetweenRotations_rot180_rot270() {
154 // W,H are flipped, because they need to be given in the new orientation, i.e. ROT_270.
155 transformToRotation(ROTATION_180, ROTATION_270, H, W, mMatrix);
156
157 checkPoint(0, 0).transformsTo(0, W);
158 checkPoint(W, H).transformsTo(H, 0);
159 }
160
161 @Test
162 public void transformBetweenRotations_rot90_rot0() {
163 transformToRotation(ROTATION_180, ROTATION_270, W, H, mMatrix);
164
165 checkPoint(0, 0).transformsTo(0, H);
166 // H,W is bottom right in ROT_90
167 checkPoint(H, W).transformsTo(W, 0);
168 }
169
170 @Test
171 public void transformBetweenRotations_displayInfo() {
172 final DisplayInfo di = new DisplayInfo();
173 di.rotation = ROTATION_90;
174 di.logicalWidth = H; // dimensions are flipped in ROT_90
175 di.logicalHeight = W;
176 transformToRotation(ROTATION_180, ROTATION_270, di, mMatrix);
177
178 // W,H are flipped, because they need to be given in the new orientation, i.e. ROT_270.
179 transformToRotation(ROTATION_180, ROTATION_270, H, W, mMatrix2);
180
181 assertEquals(mMatrix2, mMatrix);
182 }
183
184 private void assertMatricesAreInverses(Matrix matrix, Matrix matrix2) {
185 final Matrix concat = new Matrix();
186 concat.setConcat(matrix, matrix2);
187 assertTrue("expected identity, but was: " + concat, concat.isIdentity());
188 }
189
190 private TransformPointAssertable checkPoint(int x, int y) {
Adrian Roos1cf585052018-01-03 18:43:27 +0100191 final Point devicePoint = new Point(x, y);
192 final float[] fs = new float[] {x, y};
193 mMatrix.mapPoints(fs);
194 final PointF transformedPoint = new PointF(fs[0], fs[1]);
195
196 return (expectedX, expectedY) -> {
197 mErrorCollector.checkThat("t(" + devicePoint + ")",
198 transformedPoint, is(new PointF(expectedX, expectedY)));
199 };
200 }
201
Adrian Roos41f7e9d2018-06-07 15:29:34 +0200202 public interface TransformPointAssertable {
203 void transformsTo(int x, int y);
Adrian Roos1cf585052018-01-03 18:43:27 +0100204 }
205}