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