blob: 40a10e04c893aec605c97672a35f9d6367a15eff [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
24import static com.android.server.wm.utils.CoordinateTransforms.transformPhysicalToLogicalCoordinates;
25
26import static org.hamcrest.Matchers.is;
27import static org.junit.Assert.*;
28
29import android.graphics.Matrix;
30import android.graphics.Point;
31import android.graphics.PointF;
32
33import org.junit.Before;
34import org.junit.Rule;
35import org.junit.Test;
36import org.junit.rules.ErrorCollector;
37
38public 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}