1 /*
2  * Copyright (C) 2017 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 android.server.wm;
18 
19 import static org.hamcrest.Matchers.greaterThan;
20 import static org.hamcrest.Matchers.is;
21 import static org.hamcrest.Matchers.lessThanOrEqualTo;
22 import static org.junit.Assert.assertThat;
23 import static org.junit.Assume.assumeThat;
24 
25 import android.app.Activity;
26 import android.platform.test.annotations.Presubmit;
27 import android.view.Display;
28 
29 import androidx.test.rule.ActivityTestRule;
30 
31 import org.junit.Rule;
32 import org.junit.Test;
33 
34 /**
35  * Build/Install/Run:
36  *     atest CtsWindowManagerDeviceTestCases:AspectRatioTests
37  */
38 @Presubmit
39 public class AspectRatioTests extends AspectRatioTestsBase {
40 
41     // The max. aspect ratio the test activities are using.
42     private static final float MAX_ASPECT_RATIO = 1.0f;
43 
44     // The min. aspect ratio the test activities are using.
45     private static final float MIN_ASPECT_RATIO = 3.0f;
46 
47     // Test target activity that has maxAspectRatio="true" and resizeableActivity="false".
48     public static class MaxAspectRatioActivity extends Activity {
49     }
50 
51     // Test target activity that has maxAspectRatio="1.0" and resizeableActivity="true".
52     public static class MaxAspectRatioResizeableActivity extends Activity {
53     }
54 
55     // Test target activity that has no maxAspectRatio defined and resizeableActivity="false".
56     public static class MaxAspectRatioUnsetActivity extends Activity {
57     }
58 
59     // Test target activity that has maxAspectRatio defined as
60     //   <meta-data android:name="android.max_aspect" android:value="1.0" />
61     // and resizeableActivity="false".
62     public static class MetaDataMaxAspectRatioActivity extends Activity {
63     }
64 
65     // Test target activity that has minAspectRatio="true" and resizeableActivity="false".
66     public static class MinAspectRatioActivity extends Activity {
67     }
68 
69     // Test target activity that has minAspectRatio="5.0" and resizeableActivity="true".
70     public static class MinAspectRatioResizeableActivity extends Activity {
71     }
72 
73     // Test target activity that has no minAspectRatio defined and resizeableActivity="false".
74     public static class MinAspectRatioUnsetActivity extends Activity {
75     }
76 
77     // Test target activity that has minAspectRatio="true", resizeableActivity="false",
78     // and screenOrientation="landscape".
79     public static class MinAspectRatioLandscapeActivity extends Activity {
80     }
81 
82     // Test target activity that has minAspectRatio="true", resizeableActivity="false",
83     // and screenOrientation="portrait".
84     public static class MinAspectRatioPortraitActivity extends Activity {
85     }
86 
87     @Rule
88     public ActivityTestRule<MaxAspectRatioActivity> mMaxAspectRatioActivity =
89             new ActivityTestRule<>(MaxAspectRatioActivity.class,
90                     false /* initialTouchMode */, false /* launchActivity */);
91 
92     @Rule
93     public ActivityTestRule<MaxAspectRatioResizeableActivity> mMaxAspectRatioResizeableActivity =
94             new ActivityTestRule<>(MaxAspectRatioResizeableActivity.class,
95                     false /* initialTouchMode */, false /* launchActivity */);
96 
97     @Rule
98     public ActivityTestRule<MetaDataMaxAspectRatioActivity> mMetaDataMaxAspectRatioActivity =
99             new ActivityTestRule<>(MetaDataMaxAspectRatioActivity.class,
100                     false /* initialTouchMode */, false /* launchActivity */);
101 
102     @Rule
103     public ActivityTestRule<MaxAspectRatioUnsetActivity> mMaxAspectRatioUnsetActivity =
104             new ActivityTestRule<>(MaxAspectRatioUnsetActivity.class,
105                     false /* initialTouchMode */, false /* launchActivity */);
106 
107     @Rule
108     public ActivityTestRule<MinAspectRatioActivity> mMinAspectRatioActivity =
109             new ActivityTestRule<>(MinAspectRatioActivity.class,
110                     false /* initialTouchMode */, false /* launchActivity */);
111 
112     @Rule
113     public ActivityTestRule<MinAspectRatioResizeableActivity> mMinAspectRatioResizeableActivity =
114             new ActivityTestRule<>(MinAspectRatioResizeableActivity.class,
115                     false /* initialTouchMode */, false /* launchActivity */);
116 
117     @Rule
118     public ActivityTestRule<MinAspectRatioUnsetActivity> mMinAspectRatioUnsetActivity =
119             new ActivityTestRule<>(MinAspectRatioUnsetActivity.class,
120                     false /* initialTouchMode */, false /* launchActivity */);
121 
122     @Rule
123     public ActivityTestRule<MinAspectRatioLandscapeActivity> mMinAspectRatioLandscapeActivity =
124             new ActivityTestRule<>(MinAspectRatioLandscapeActivity.class,
125                     false /* initialTouchMode */, false /* launchActivity */);
126 
127     @Rule
128     public ActivityTestRule<MinAspectRatioPortraitActivity> mMinAspectRatioPortraitActivity =
129             new ActivityTestRule<>(MinAspectRatioPortraitActivity.class,
130                     false /* initialTouchMode */, false /* launchActivity */);
131 
132     @Test
testMaxAspectRatio()133     public void testMaxAspectRatio() {
134         // Activity has a maxAspectRatio, assert that the actual ratio is less than that.
135         runAspectRatioTest(mMaxAspectRatioActivity, (actual, displayId, size) -> {
136             assertThat(actual, lessThanOrEqualTo(MAX_ASPECT_RATIO));
137         });
138     }
139 
140     @Test
testMetaDataMaxAspectRatio()141     public void testMetaDataMaxAspectRatio() {
142         // Activity has a maxAspectRatio, assert that the actual ratio is less than that.
143         runAspectRatioTest(mMetaDataMaxAspectRatioActivity, (actual, displayId, size) -> {
144             assertThat(actual, lessThanOrEqualTo(MAX_ASPECT_RATIO));
145         });
146     }
147 
148     @Test
testMaxAspectRatioResizeableActivity()149     public void testMaxAspectRatioResizeableActivity() {
150         // Since this activity is resizeable, its max aspect ratio should be ignored.
151         runAspectRatioTest(mMaxAspectRatioResizeableActivity, (actual, displayId, size) -> {
152             // TODO(b/69982434): Add ability to get native aspect ratio non-default display.
153             assumeThat(displayId, is(Display.DEFAULT_DISPLAY));
154 
155             final float defaultDisplayAspectRatio = getDefaultDisplayAspectRatio();
156             assertThat(actual, greaterThanOrEqualToInexact(defaultDisplayAspectRatio));
157         });
158     }
159 
160     @Test
testMaxAspectRatioUnsetActivity()161     public void testMaxAspectRatioUnsetActivity() {
162         // Since this activity didn't set an explicit maxAspectRatio, there should be no such
163         // ratio enforced.
164         runAspectRatioTest(mMaxAspectRatioUnsetActivity, (actual, displayId, size) -> {
165             // TODO(b/69982434): Add ability to get native aspect ratio non-default display.
166             assumeThat(displayId, is(Display.DEFAULT_DISPLAY));
167 
168             assertThat(actual, greaterThanOrEqualToInexact(getDefaultDisplayAspectRatio()));
169         });
170     }
171 
172     @Test
testMinAspectRatio()173     public void testMinAspectRatio() {
174         // Activity has a minAspectRatio, assert the ratio is at least that.
175         runAspectRatioTest(mMinAspectRatioActivity, (actual, displayId, size) -> {
176             assertThat(actual, greaterThanOrEqualToInexact(MIN_ASPECT_RATIO));
177         });
178     }
179 
180     @Test
testMinAspectRatioResizeableActivity()181     public void testMinAspectRatioResizeableActivity() {
182         // Since this activity is resizeable, the minAspectRatio should be ignored.
183         runAspectRatioTest(mMinAspectRatioResizeableActivity, (actual, displayId, size) -> {
184             // TODO(b/69982434): Add ability to get native aspect ratio non-default display.
185             assumeThat(displayId, is(Display.DEFAULT_DISPLAY));
186 
187             assertThat(actual, lessThanOrEqualToInexact(getDefaultDisplayAspectRatio()));
188         });
189     }
190 
191     @Test
testMinAspectRatioUnsetActivity()192     public void testMinAspectRatioUnsetActivity() {
193         // Since this activity didn't set an explicit minAspectRatio, there should be no such
194         // ratio enforced.
195         runAspectRatioTest(mMinAspectRatioUnsetActivity, (actual, displayId, size) -> {
196             // TODO(b/69982434): Add ability to get native aspect ratio non-default display.
197             assumeThat(displayId, is(Display.DEFAULT_DISPLAY));
198 
199             assertThat(actual, lessThanOrEqualToInexact(getDefaultDisplayAspectRatio()));
200         });
201     }
202 
203     @Test
testMinAspectLandscapeActivity()204     public void testMinAspectLandscapeActivity() {
205         // Activity has requested a fixed orientation, assert the orientation is that.
206         runAspectRatioTest(mMinAspectRatioLandscapeActivity, (actual, displayId, size) -> {
207             assertThat(actual, greaterThanOrEqualToInexact(MIN_ASPECT_RATIO));
208             assertThat(size.x, greaterThan(size.y));
209         });
210     }
211 
212     @Test
testMinAspectPortraitActivity()213     public void testMinAspectPortraitActivity() {
214         // Activity has requested a fixed orientation, assert the orientation is that.
215         runAspectRatioTest(mMinAspectRatioPortraitActivity, (actual, displayId, size) -> {
216             assertThat(actual, greaterThanOrEqualToInexact(MIN_ASPECT_RATIO));
217             assertThat(size.y, greaterThan(size.x));
218         });
219     }
220 }
221