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 android.server.wm.app.Components.MAX_ASPECT_RATIO_ACTIVITY;
20 import static android.server.wm.app.Components.MAX_ASPECT_RATIO_RESIZABLE_ACTIVITY;
21 import static android.server.wm.app.Components.MAX_ASPECT_RATIO_UNSET_ACTIVITY;
22 import static android.server.wm.app.Components.META_DATA_MAX_ASPECT_RATIO_ACTIVITY;
23 import static android.server.wm.app.Components.MIN_ASPECT_RATIO_ACTIVITY;
24 import static android.server.wm.app.Components.MIN_ASPECT_RATIO_LANDSCAPE_ACTIVITY;
25 import static android.server.wm.app.Components.MIN_ASPECT_RATIO_PORTRAIT_ACTIVITY;
26 import static android.server.wm.app.Components.MIN_ASPECT_RATIO_UNSET_ACTIVITY;
27 
28 import static org.hamcrest.Matchers.greaterThan;
29 import static org.hamcrest.Matchers.is;
30 import static org.hamcrest.Matchers.lessThanOrEqualTo;
31 import static org.junit.Assert.assertThat;
32 import static org.junit.Assume.assumeThat;
33 
34 import android.platform.test.annotations.Presubmit;
35 import android.view.Display;
36 
37 import org.junit.Test;
38 
39 /**
40  * Build/Install/Run:
41  *     atest CtsWindowManagerDeviceTestCases:AspectRatioTests
42  */
43 @Presubmit
44 public class AspectRatioTests extends AspectRatioTestsBase {
45 
46     // The max. aspect ratio the test activities are using.
47     private static final float MAX_ASPECT_RATIO = 1.0f;
48 
49     // The min. aspect ratio the test activities are using.
50     private static final float MIN_ASPECT_RATIO = 3.0f;
51 
52     @Test
testMaxAspectRatio()53     public void testMaxAspectRatio() {
54         // Activity has a maxAspectRatio, assert that the actual ratio is less than that.
55         runAspectRatioTest(MAX_ASPECT_RATIO_ACTIVITY,
56                 (actual, displayId, activitySize, displaySize) -> {
57             assertThat(actual, lessThanOrEqualTo(MAX_ASPECT_RATIO));
58         });
59     }
60 
61     @Test
testMetaDataMaxAspectRatio()62     public void testMetaDataMaxAspectRatio() {
63         // Activity has a maxAspectRatio, assert that the actual ratio is less than that.
64         runAspectRatioTest(META_DATA_MAX_ASPECT_RATIO_ACTIVITY,
65                 (actual, displayId, activitySize, displaySize) -> {
66             assertThat(actual, lessThanOrEqualTo(MAX_ASPECT_RATIO));
67         });
68     }
69 
70     @Test
testMaxAspectRatioResizeableActivity()71     public void testMaxAspectRatioResizeableActivity() {
72         // Since this activity is resizeable, its max aspect ratio should be ignored.
73         runAspectRatioTest(MAX_ASPECT_RATIO_RESIZABLE_ACTIVITY,
74                 (actual, displayId, activitySize, displaySize) -> {
75             // TODO(b/69982434): Add ability to get native aspect ratio non-default display.
76             assumeThat(displayId, is(Display.DEFAULT_DISPLAY));
77 
78             final float defaultDisplayAspectRatio =
79                     getDisplayAspectRatio(MAX_ASPECT_RATIO_RESIZABLE_ACTIVITY);
80             assertThat(actual, greaterThanOrEqualToInexact(defaultDisplayAspectRatio));
81         });
82     }
83 
84     @Test
testMaxAspectRatioUnsetActivity()85     public void testMaxAspectRatioUnsetActivity() {
86         // Since this activity didn't set an explicit maxAspectRatio, there should be no such
87         // ratio enforced.
88         runAspectRatioTest(MAX_ASPECT_RATIO_UNSET_ACTIVITY,
89                 (actual, displayId, activitySize, displaySize) -> {
90             // TODO(b/69982434): Add ability to get native aspect ratio non-default display.
91             assumeThat(displayId, is(Display.DEFAULT_DISPLAY));
92 
93             assertThat(actual, greaterThanOrEqualToInexact(
94                     getDisplayAspectRatio(MAX_ASPECT_RATIO_UNSET_ACTIVITY)));
95         });
96     }
97 
98     @Test
testMinAspectRatio()99     public void testMinAspectRatio() {
100         // Activity has a minAspectRatio, assert the ratio is at least that.
101         runAspectRatioTest(MIN_ASPECT_RATIO_ACTIVITY,
102                 (actual, displayId, activitySize, displaySize) -> {
103             assertThat(actual, greaterThanOrEqualToInexact(MIN_ASPECT_RATIO));
104         });
105     }
106 
107     @Test
testMinAspectRatioUnsetActivity()108     public void testMinAspectRatioUnsetActivity() {
109         // Since this activity didn't set an explicit minAspectRatio, there should be no such
110         // ratio enforced.
111         runAspectRatioTest(MIN_ASPECT_RATIO_UNSET_ACTIVITY,
112                 (actual, displayId, activitySize, displaySize) -> {
113             // TODO(b/69982434): Add ability to get native aspect ratio non-default display.
114             assumeThat(displayId, is(Display.DEFAULT_DISPLAY));
115 
116             assertThat(actual, lessThanOrEqualToInexact(
117                     getDisplayAspectRatio(MIN_ASPECT_RATIO_UNSET_ACTIVITY)));
118         });
119     }
120 
121     @Test
testMinAspectLandscapeActivity()122     public void testMinAspectLandscapeActivity() {
123         // Activity has requested a fixed orientation, assert the orientation is that.
124         runAspectRatioTest(MIN_ASPECT_RATIO_LANDSCAPE_ACTIVITY,
125                 (actual, displayId, activitySize, displaySize) -> {
126             assertThat(activitySize.x, greaterThan(activitySize.y));
127             // Since activities must fit within the bounds of the display and they should respect
128             // the minimal size, there is an aspect ratio limit that an activity cannot exceed even
129             // if set in the app manifest. In such scenarios, we won't expect the aspect ratio to
130             // be respected.
131             int maxAspectRatioForDisplay = displaySize.x
132                     / getMinimalTaskSize(MIN_ASPECT_RATIO_LANDSCAPE_ACTIVITY);
133             if (MIN_ASPECT_RATIO <= maxAspectRatioForDisplay) {
134                 // The display size is large enough to support the desired aspect ratio
135                 // without violating the minimal size restriction.
136                 assertThat(actual, greaterThanOrEqualToInexact(MIN_ASPECT_RATIO));
137             }
138         });
139     }
140 
141     @Test
testMinAspectPortraitActivity()142     public void testMinAspectPortraitActivity() {
143         runAspectRatioTest(MIN_ASPECT_RATIO_PORTRAIT_ACTIVITY,
144                 (actual, displayId, activitySize, displaySize) -> {
145             assertThat(activitySize.y, greaterThan(activitySize.x));
146             // Since activities must fit within the bounds of the display and they should respect
147             // the minimal size, there is an aspect ratio limit that an activity cannot exceed even
148             // if set in the app manifest. In such scenarios, we won't expect the aspect ratio to
149             // be respected.
150             int maxAspectRatioForDisplay = displaySize.y
151                     / getMinimalTaskSize(MIN_ASPECT_RATIO_PORTRAIT_ACTIVITY);
152             if (MIN_ASPECT_RATIO <= maxAspectRatioForDisplay) {
153                 // The display size is large enough to support the desired aspect ratio
154                 // without violating the minimal size restriction.
155                 assertThat(actual, greaterThanOrEqualToInexact(MIN_ASPECT_RATIO));
156             }
157         });
158     }
159 }
160