Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.wm.utils; |
| 18 | |
| 19 | import static android.view.Surface.ROTATION_0; |
| 20 | import static android.view.Surface.ROTATION_180; |
| 21 | import static android.view.Surface.ROTATION_270; |
| 22 | import static android.view.Surface.ROTATION_90; |
| 23 | |
Adrian Roos | 41f7e9d | 2018-06-07 15:29:34 +0200 | [diff] [blame] | 24 | import static com.android.server.wm.utils.CoordinateTransforms.transformLogicalToPhysicalCoordinates; |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 25 | import static com.android.server.wm.utils.CoordinateTransforms.transformPhysicalToLogicalCoordinates; |
| 26 | |
Adrian Roos | 41f7e9d | 2018-06-07 15:29:34 +0200 | [diff] [blame] | 27 | |
| 28 | import static com.android.server.wm.utils.CoordinateTransforms.transformToRotation; |
| 29 | |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 30 | import static org.hamcrest.Matchers.is; |
| 31 | import static org.junit.Assert.*; |
| 32 | |
| 33 | import android.graphics.Matrix; |
| 34 | import android.graphics.Point; |
| 35 | import android.graphics.PointF; |
Adrian Roos | 41f7e9d | 2018-06-07 15:29:34 +0200 | [diff] [blame] | 36 | import android.view.DisplayInfo; |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 37 | |
| 38 | import org.junit.Before; |
| 39 | import org.junit.Rule; |
| 40 | import org.junit.Test; |
| 41 | import org.junit.rules.ErrorCollector; |
| 42 | |
| 43 | public 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 Roos | 41f7e9d | 2018-06-07 15:29:34 +0200 | [diff] [blame] | 49 | private final Matrix mMatrix2 = new Matrix(); |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 50 | |
| 51 | @Rule |
| 52 | public final ErrorCollector mErrorCollector = new ErrorCollector(); |
| 53 | |
| 54 | @Before |
| 55 | public void setUp() throws Exception { |
| 56 | mMatrix.setTranslate(0xdeadbeef, 0xdeadbeef); |
Adrian Roos | 41f7e9d | 2018-06-07 15:29:34 +0200 | [diff] [blame] | 57 | mMatrix2.setTranslate(0xbeefdead, 0xbeefdead); |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | @Test |
Adrian Roos | 41f7e9d | 2018-06-07 15:29:34 +0200 | [diff] [blame] | 61 | public void transformPhysicalToLogicalCoordinates_rot0() { |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 62 | transformPhysicalToLogicalCoordinates(ROTATION_0, W, H, mMatrix); |
| 63 | assertThat(mMatrix, is(Matrix.IDENTITY_MATRIX)); |
| 64 | } |
| 65 | |
| 66 | @Test |
Adrian Roos | 41f7e9d | 2018-06-07 15:29:34 +0200 | [diff] [blame] | 67 | public void transformPhysicalToLogicalCoordinates_rot90() { |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 68 | transformPhysicalToLogicalCoordinates(ROTATION_90, W, H, mMatrix); |
| 69 | |
Adrian Roos | 41f7e9d | 2018-06-07 15:29:34 +0200 | [diff] [blame] | 70 | checkPoint(0, 0).transformsTo(0, W); |
| 71 | checkPoint(W, H).transformsTo(H, 0); |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Test |
Adrian Roos | 41f7e9d | 2018-06-07 15:29:34 +0200 | [diff] [blame] | 75 | public void transformPhysicalToLogicalCoordinates_rot180() { |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 76 | transformPhysicalToLogicalCoordinates(ROTATION_180, W, H, mMatrix); |
| 77 | |
Adrian Roos | 41f7e9d | 2018-06-07 15:29:34 +0200 | [diff] [blame] | 78 | checkPoint(0, 0).transformsTo(W, H); |
| 79 | checkPoint(W, H).transformsTo(0, 0); |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Test |
Adrian Roos | 41f7e9d | 2018-06-07 15:29:34 +0200 | [diff] [blame] | 83 | public void transformPhysicalToLogicalCoordinates_rot270() { |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 84 | transformPhysicalToLogicalCoordinates(ROTATION_270, W, H, mMatrix); |
| 85 | |
Adrian Roos | 41f7e9d | 2018-06-07 15:29:34 +0200 | [diff] [blame] | 86 | checkPoint(0, 0).transformsTo(H, 0); |
| 87 | checkPoint(W, H).transformsTo(0, W); |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Adrian Roos | 41f7e9d | 2018-06-07 15:29:34 +0200 | [diff] [blame] | 90 | @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 Roos | 28c25e2 | 2018-05-31 18:07:28 +0200 | [diff] [blame] | 102 | } |
Adrian Roos | 41f7e9d | 2018-06-07 15:29:34 +0200 | [diff] [blame] | 103 | |
| 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 Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 191 | 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 Roos | 41f7e9d | 2018-06-07 15:29:34 +0200 | [diff] [blame] | 202 | public interface TransformPointAssertable { |
| 203 | void transformsTo(int x, int y); |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 204 | } |
| 205 | } |