Bryce Lee | 7566d76 | 2017-03-30 09:34:15 -0700 | [diff] [blame] | 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 com.android.server.wm; |
| 18 | |
| 19 | import android.app.ActivityManager; |
Bryce Lee | 600dadd | 2017-07-25 10:48:42 -0700 | [diff] [blame] | 20 | import android.content.pm.ActivityInfo; |
Bryce Lee | 7566d76 | 2017-03-30 09:34:15 -0700 | [diff] [blame] | 21 | import android.content.res.Configuration; |
| 22 | import android.graphics.Rect; |
Bryce Lee | 7566d76 | 2017-03-30 09:34:15 -0700 | [diff] [blame] | 23 | import android.view.DisplayInfo; |
| 24 | import org.junit.Test; |
| 25 | |
| 26 | import android.platform.test.annotations.Presubmit; |
| 27 | import android.support.test.filters.SmallTest; |
| 28 | import android.support.test.runner.AndroidJUnit4; |
| 29 | |
| 30 | import static org.junit.Assert.assertEquals; |
Bryce Lee | c5fe0ee | 2017-06-30 09:03:09 -0700 | [diff] [blame] | 31 | import static org.junit.Assert.assertNotEquals; |
Bryce Lee | 7566d76 | 2017-03-30 09:34:15 -0700 | [diff] [blame] | 32 | import static org.junit.Assert.assertTrue; |
| 33 | |
| 34 | /** |
| 35 | * Test class to exercise logic related to {@link android.content.res.Configuration#appBounds}. |
| 36 | * |
| 37 | * Build/Install/Run: |
| 38 | * bit FrameworksServicesTests:com.android.server.wm.AppBoundsTests |
| 39 | */ |
| 40 | @SmallTest |
| 41 | @Presubmit |
| 42 | @org.junit.runner.RunWith(AndroidJUnit4.class) |
| 43 | public class AppBoundsTests extends WindowTestsBase { |
| 44 | private Rect mParentBounds; |
| 45 | |
| 46 | @Override |
| 47 | public void setUp() throws Exception { |
| 48 | super.setUp(); |
| 49 | mParentBounds = new Rect(10 /*left*/, 30 /*top*/, 80 /*right*/, 60 /*bottom*/); |
| 50 | } |
| 51 | |
| 52 | /** |
Bryce Lee | 600dadd | 2017-07-25 10:48:42 -0700 | [diff] [blame] | 53 | * Ensures that appBounds causes {@link android.content.pm.ActivityInfo.CONFIG_APP_BOUNDS} diff. |
| 54 | */ |
| 55 | @Test |
| 56 | public void testAppBoundsConfigurationDiff() { |
| 57 | final Configuration config = new Configuration(); |
| 58 | final Configuration config2 = new Configuration(); |
| 59 | config.appBounds = new Rect(0, 1, 1, 0); |
Bryce Lee | 0e4a6df | 2017-07-27 14:06:22 -0700 | [diff] [blame] | 60 | config2.appBounds = new Rect(1, 2, 2, 1); |
Bryce Lee | 600dadd | 2017-07-25 10:48:42 -0700 | [diff] [blame] | 61 | |
Bryce Lee | 0e4a6df | 2017-07-27 14:06:22 -0700 | [diff] [blame] | 62 | assertEquals(ActivityInfo.CONFIG_SCREEN_SIZE, config.diff(config2)); |
Bryce Lee | 658d984 | 2017-07-28 08:33:36 -0700 | [diff] [blame] | 63 | assertEquals(0, config.diffPublicOnly(config2)); |
Bryce Lee | 600dadd | 2017-07-25 10:48:42 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /** |
Bryce Lee | 7566d76 | 2017-03-30 09:34:15 -0700 | [diff] [blame] | 67 | * Ensures the configuration app bounds at the root level match the app dimensions. |
| 68 | */ |
| 69 | @Test |
| 70 | public void testRootConfigurationBounds() throws Exception { |
Wale Ogunwale | 11cc516 | 2017-04-25 20:29:13 -0700 | [diff] [blame] | 71 | final DisplayInfo info = mDisplayContent.getDisplayInfo(); |
Bryce Lee | 7566d76 | 2017-03-30 09:34:15 -0700 | [diff] [blame] | 72 | info.appWidth = 1024; |
| 73 | info.appHeight = 768; |
| 74 | |
Wale Ogunwale | 11cc516 | 2017-04-25 20:29:13 -0700 | [diff] [blame] | 75 | final Configuration config = sWm.computeNewConfiguration(mDisplayContent.getDisplayId()); |
Bryce Lee | 7566d76 | 2017-03-30 09:34:15 -0700 | [diff] [blame] | 76 | // The bounds should always be positioned in the top left. |
| 77 | assertEquals(config.appBounds.left, 0); |
| 78 | assertEquals(config.appBounds.top, 0); |
| 79 | |
| 80 | // The bounds should equal the defined app width and height |
| 81 | assertEquals(config.appBounds.width(), info.appWidth); |
| 82 | assertEquals(config.appBounds.height(), info.appHeight); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Ensures that bounds are clipped to their parent. |
| 87 | */ |
| 88 | @Test |
| 89 | public void testBoundsClipping() throws Exception { |
| 90 | final Rect shiftedBounds = new Rect(mParentBounds); |
| 91 | shiftedBounds.offset(10, 10); |
| 92 | final Rect expectedBounds = new Rect(mParentBounds); |
| 93 | expectedBounds.intersect(shiftedBounds); |
| 94 | testStackBoundsConfiguration(null /*stackId*/, mParentBounds, shiftedBounds, |
| 95 | expectedBounds); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Ensures that empty bounds are not propagated to the configuration. |
| 100 | */ |
| 101 | @Test |
| 102 | public void testEmptyBounds() throws Exception { |
| 103 | final Rect emptyBounds = new Rect(); |
| 104 | testStackBoundsConfiguration(null /*stackId*/, mParentBounds, emptyBounds, |
| 105 | null /*ExpectedBounds*/); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Ensures that bounds on freeform stacks are not clipped. |
| 110 | */ |
| 111 | @Test |
| 112 | public void testFreeFormBounds() throws Exception { |
| 113 | final Rect freeFormBounds = new Rect(mParentBounds); |
| 114 | freeFormBounds.offset(10, 10); |
| 115 | testStackBoundsConfiguration(ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID, |
| 116 | mParentBounds, freeFormBounds, freeFormBounds); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Ensures that fully contained bounds are not clipped. |
| 121 | */ |
| 122 | @Test |
| 123 | public void testContainedBounds() throws Exception { |
| 124 | final Rect insetBounds = new Rect(mParentBounds); |
| 125 | insetBounds.inset(5, 5, 5, 5); |
| 126 | testStackBoundsConfiguration(null /*stackId*/, mParentBounds, insetBounds, insetBounds); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Ensures that full screen free form bounds are clipped |
| 131 | */ |
| 132 | @Test |
| 133 | public void testFullScreenFreeFormBounds() throws Exception { |
Wale Ogunwale | 11cc516 | 2017-04-25 20:29:13 -0700 | [diff] [blame] | 134 | final Rect fullScreenBounds = new Rect(0, 0, mDisplayInfo.logicalWidth, |
| 135 | mDisplayInfo.logicalHeight); |
Bryce Lee | 7566d76 | 2017-03-30 09:34:15 -0700 | [diff] [blame] | 136 | testStackBoundsConfiguration(null /*stackId*/, mParentBounds, fullScreenBounds, |
| 137 | mParentBounds); |
| 138 | } |
| 139 | |
Bryce Lee | 7566d76 | 2017-03-30 09:34:15 -0700 | [diff] [blame] | 140 | private void testStackBoundsConfiguration(Integer stackId, Rect parentBounds, Rect bounds, |
| 141 | Rect expectedConfigBounds) { |
| 142 | final StackWindowController stackController = stackId != null ? |
Wale Ogunwale | 11cc516 | 2017-04-25 20:29:13 -0700 | [diff] [blame] | 143 | createStackControllerOnStackOnDisplay(stackId, mDisplayContent) |
| 144 | : createStackControllerOnDisplay(mDisplayContent); |
Bryce Lee | 7566d76 | 2017-03-30 09:34:15 -0700 | [diff] [blame] | 145 | |
Wale Ogunwale | 11cc516 | 2017-04-25 20:29:13 -0700 | [diff] [blame] | 146 | final Configuration parentConfig = mDisplayContent.getConfiguration(); |
Bryce Lee | 7566d76 | 2017-03-30 09:34:15 -0700 | [diff] [blame] | 147 | parentConfig.setAppBounds(parentBounds); |
| 148 | |
| 149 | final Configuration config = new Configuration(); |
| 150 | stackController.adjustConfigurationForBounds(bounds, null /*insetBounds*/, |
| 151 | new Rect() /*nonDecorBounds*/, new Rect() /*stableBounds*/, false /*overrideWidth*/, |
Wale Ogunwale | 11cc516 | 2017-04-25 20:29:13 -0700 | [diff] [blame] | 152 | false /*overrideHeight*/, mDisplayInfo.logicalDensityDpi, config, parentConfig); |
Bryce Lee | 7566d76 | 2017-03-30 09:34:15 -0700 | [diff] [blame] | 153 | // Assert that both expected and actual are null or are equal to each other |
| 154 | |
| 155 | assertTrue((expectedConfigBounds == null && config.appBounds == null) |
| 156 | || expectedConfigBounds.equals(config.appBounds)); |
| 157 | } |
Bryce Lee | c5fe0ee | 2017-06-30 09:03:09 -0700 | [diff] [blame] | 158 | |
| 159 | /** |
| 160 | * Ensures appBounds are considered in {@link Configuration#compareTo(Configuration)}. |
| 161 | */ |
| 162 | @Test |
| 163 | public void testConfigurationCompareTo() throws Exception { |
| 164 | final Configuration blankConfig = new Configuration(); |
| 165 | |
| 166 | final Configuration config1 = new Configuration(); |
| 167 | config1.appBounds = new Rect(1, 2, 3, 4); |
| 168 | |
| 169 | final Configuration config2 = new Configuration(config1); |
| 170 | |
| 171 | assertEquals(config1.compareTo(config2), 0); |
| 172 | |
| 173 | config2.appBounds.left = 0; |
| 174 | |
| 175 | // Different bounds |
| 176 | assertNotEquals(config1.compareTo(config2), 0); |
| 177 | |
| 178 | // No bounds |
| 179 | assertEquals(config1.compareTo(blankConfig), -1); |
| 180 | assertEquals(blankConfig.compareTo(config1), 1); |
| 181 | |
| 182 | } |
Bryce Lee | 7566d76 | 2017-03-30 09:34:15 -0700 | [diff] [blame] | 183 | } |