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 | |
| 24 | import static com.android.server.wm.utils.CoordinateTransforms.transformPhysicalToLogicalCoordinates; |
| 25 | |
| 26 | import static org.hamcrest.Matchers.is; |
| 27 | import static org.junit.Assert.*; |
| 28 | |
| 29 | import android.graphics.Matrix; |
| 30 | import android.graphics.Point; |
| 31 | import android.graphics.PointF; |
| 32 | |
| 33 | import org.junit.Before; |
| 34 | import org.junit.Rule; |
| 35 | import org.junit.Test; |
| 36 | import org.junit.rules.ErrorCollector; |
| 37 | |
| 38 | public class CoordinateTransformsTest { |
| 39 | |
| 40 | private static final int W = 200; |
| 41 | private static final int H = 400; |
| 42 | |
| 43 | private final Matrix mMatrix = new Matrix(); |
| 44 | |
| 45 | @Rule |
| 46 | public final ErrorCollector mErrorCollector = new ErrorCollector(); |
| 47 | |
| 48 | @Before |
| 49 | public void setUp() throws Exception { |
| 50 | mMatrix.setTranslate(0xdeadbeef, 0xdeadbeef); |
| 51 | } |
| 52 | |
| 53 | @Test |
| 54 | public void transformPhysicalToLogicalCoordinates_rot0() throws Exception { |
| 55 | transformPhysicalToLogicalCoordinates(ROTATION_0, W, H, mMatrix); |
| 56 | assertThat(mMatrix, is(Matrix.IDENTITY_MATRIX)); |
| 57 | } |
| 58 | |
| 59 | @Test |
| 60 | public void transformPhysicalToLogicalCoordinates_rot90() throws Exception { |
| 61 | transformPhysicalToLogicalCoordinates(ROTATION_90, W, H, mMatrix); |
| 62 | |
| 63 | checkDevicePoint(0, 0).mapsToLogicalPoint(0, W); |
| 64 | checkDevicePoint(W, H).mapsToLogicalPoint(H, 0); |
| 65 | } |
| 66 | |
| 67 | @Test |
| 68 | public void transformPhysicalToLogicalCoordinates_rot180() throws Exception { |
| 69 | transformPhysicalToLogicalCoordinates(ROTATION_180, W, H, mMatrix); |
| 70 | |
| 71 | checkDevicePoint(0, 0).mapsToLogicalPoint(W, H); |
| 72 | checkDevicePoint(W, H).mapsToLogicalPoint(0, 0); |
| 73 | } |
| 74 | |
| 75 | @Test |
| 76 | public void transformPhysicalToLogicalCoordinates_rot270() throws Exception { |
| 77 | transformPhysicalToLogicalCoordinates(ROTATION_270, W, H, mMatrix); |
| 78 | |
| 79 | checkDevicePoint(0, 0).mapsToLogicalPoint(H, 0); |
| 80 | checkDevicePoint(W, H).mapsToLogicalPoint(0, W); |
| 81 | } |
| 82 | |
| 83 | private DevicePointAssertable checkDevicePoint(int x, int y) { |
| 84 | final Point devicePoint = new Point(x, y); |
| 85 | final float[] fs = new float[] {x, y}; |
| 86 | mMatrix.mapPoints(fs); |
| 87 | final PointF transformedPoint = new PointF(fs[0], fs[1]); |
| 88 | |
| 89 | return (expectedX, expectedY) -> { |
| 90 | mErrorCollector.checkThat("t(" + devicePoint + ")", |
| 91 | transformedPoint, is(new PointF(expectedX, expectedY))); |
| 92 | }; |
| 93 | } |
| 94 | |
| 95 | public interface DevicePointAssertable { |
| 96 | void mapsToLogicalPoint(int x, int y); |
| 97 | } |
| 98 | } |