blob: fdc157b8859ad3bb47f9399ed05480ddebb299f3 [file] [log] [blame]
husaynhakeemad541fc2019-11-06 11:09:51 -08001/*
2 * Copyright 2019 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 androidx.camera.view;
18
19import static org.junit.Assert.assertEquals;
20
21import android.app.Activity;
22import android.content.Context;
23import android.graphics.Matrix;
24
25import androidx.camera.testing.CoreAppTestUtil;
26import androidx.camera.testing.fakes.FakeActivity;
27import androidx.test.annotation.UiThreadTest;
28import androidx.test.core.app.ApplicationProvider;
29import androidx.test.ext.junit.runners.AndroidJUnit4;
30import androidx.test.filters.LargeTest;
Trevor McGuireeb49fb92019-12-10 00:16:51 -080031import androidx.test.filters.Suppress;
husaynhakeemad541fc2019-11-06 11:09:51 -080032import androidx.test.rule.ActivityTestRule;
33
34import org.junit.Before;
35import org.junit.Rule;
36import org.junit.Test;
37import org.junit.runner.RunWith;
38
39/**
40 * Instrumented tests for {@link TransformableSurfaceView}.
41 */
42@LargeTest
Trevor McGuireeb49fb92019-12-10 00:16:51 -080043@Suppress
husaynhakeemad541fc2019-11-06 11:09:51 -080044@RunWith(AndroidJUnit4.class)
45public class TransformableSurfaceViewTest {
46
47 private static final int ANY_WIDTH = 160;
48 private static final int ANY_HEIGHT = 90;
49
50 @Rule
51 public ActivityTestRule<FakeActivity> mActivityTestRule =
52 new ActivityTestRule<>(FakeActivity.class);
53
54 private Context mContext;
55
56 @Before
57 public void setUp() {
58 CoreAppTestUtil.assumeCompatibleDevice();
59 mContext = ApplicationProvider.getApplicationContext();
60 }
61
62 @Test
63 @UiThreadTest
64 public void translateTransformation() throws Throwable {
65 final int translateX = 50;
66 final int translateY = 80;
67 final Matrix matrix = new Matrix();
68 matrix.setTranslate(translateX, translateY);
69
70 transformSurfaceView(matrix, ANY_WIDTH, ANY_HEIGHT, translateX, translateY);
71 }
72
73 @Test
74 @UiThreadTest
75 public void scaleFromTopLeftTransformation() throws Throwable {
76 final int scaleX = 2;
77 final int scaleY = 5;
78 final Matrix matrix = new Matrix();
79 matrix.setScale(scaleX, scaleY, 0, 0);
80
81 final int expectedWidth = scaleX * ANY_WIDTH;
82 final int expectedHeight = scaleY * ANY_HEIGHT;
83 final int expectedX = 0;
84 final int expectedY = 0;
85
86 transformSurfaceView(matrix, expectedWidth, expectedHeight, expectedX, expectedY);
87 }
88
89 @Test
90 @UiThreadTest
91 public void scaleFromCenterTransformation() throws Throwable {
92 final int scaleX = 2;
93 final int scaleY = 5;
94 final float centerX = ANY_WIDTH / 2f;
95 final float centerY = ANY_HEIGHT / 2f;
96 final Matrix matrix = new Matrix();
97 matrix.setScale(scaleX, scaleY, centerX, centerY);
98
99 final int expectedWidth = scaleX * ANY_WIDTH;
100 final int expectedHeight = scaleY * ANY_HEIGHT;
101 final int expectedX = (int) (centerX - scaleX * ANY_WIDTH / 2f);
102 final int expectedY = (int) (centerY - scaleY * ANY_HEIGHT / 2f);
103
104 transformSurfaceView(matrix, expectedWidth, expectedHeight, expectedX, expectedY);
105 }
106
107 @Test
108 @UiThreadTest
109 public void scaleFromTopLeftAndTranslateTransformation() throws Throwable {
110 final int scaleX = 2;
111 final int scaleY = 5;
112 final int translateX = 50;
113 final int translateY = 80;
114 final Matrix matrix = new Matrix();
115 matrix.setScale(scaleX, scaleY, 0, 0);
116 matrix.postTranslate(translateX, translateY);
117
118 final int expectedWidth = scaleX * ANY_WIDTH;
119 final int expectedHeight = scaleY * ANY_HEIGHT;
120
121 transformSurfaceView(matrix, expectedWidth, expectedHeight, translateX, translateY);
122 }
123
124 @Test
125 @UiThreadTest
126 public void scaleFromCenterAndTranslateTransformation() throws Throwable {
127 final int scaleX = 2;
128 final int scaleY = 5;
129 final int translateX = 50;
130 final int translateY = 80;
131 final float centerX = ANY_WIDTH / 2f;
132 final float centerY = ANY_HEIGHT / 2f;
133 final Matrix matrix = new Matrix();
134 matrix.setScale(scaleX, scaleY, centerX, centerY);
135 matrix.postTranslate(translateX, translateY);
136
137 final int expectedWidth = scaleX * ANY_WIDTH;
138 final int expectedHeight = scaleY * ANY_HEIGHT;
139 final int expectedX = (int) (translateX + centerX - scaleX * ANY_WIDTH / 2f);
140 final int expectedY = (int) (translateY + centerY - scaleY * ANY_HEIGHT / 2f);
141
142 transformSurfaceView(matrix, expectedWidth, expectedHeight, expectedX, expectedY);
143 }
144
145 @Test(expected = IllegalArgumentException.class)
146 @UiThreadTest
147 public void ignoreRotationTransformation() throws Throwable {
148 final Matrix matrix = new Matrix();
149 matrix.setRotate(-45);
150
151 transformSurfaceView(matrix, ANY_WIDTH, ANY_HEIGHT, 0, 0);
152 }
153
154 private void transformSurfaceView(final Matrix matrix, final int expectedWidth,
155 final int expectedHeight, final int expectedX, final int expectedY) throws Throwable {
156 final TransformableSurfaceView surfaceView = new TransformableSurfaceView(mContext);
157 surfaceView.layout(0, 0, ANY_WIDTH, ANY_HEIGHT);
158
159 final Activity activity = mActivityTestRule.getActivity();
160 mActivityTestRule.runOnUiThread(() -> activity.setContentView(surfaceView));
161
162 surfaceView.setTransform(matrix);
163
164 surfaceView.post(() -> {
165 assertEquals(expectedWidth, surfaceView.getWidth());
166 assertEquals(expectedHeight, surfaceView.getHeight());
167 assertEquals(expectedX, Math.round(surfaceView.getX()));
168 assertEquals(expectedY, Math.round(surfaceView.getY()));
169 });
170 }
171}