blob: 432cfc7a22eeef4864cdd0d3beee03cf00f3d09b [file] [log] [blame]
Bryce Lee7566d762017-03-30 09:34:15 -07001/*
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
17package com.android.server.wm;
18
19import android.app.ActivityManager;
Bryce Lee600dadd2017-07-25 10:48:42 -070020import android.content.pm.ActivityInfo;
Bryce Lee7566d762017-03-30 09:34:15 -070021import android.content.res.Configuration;
22import android.graphics.Rect;
Bryce Lee7566d762017-03-30 09:34:15 -070023import android.view.DisplayInfo;
24import org.junit.Test;
25
26import android.platform.test.annotations.Presubmit;
27import android.support.test.filters.SmallTest;
28import android.support.test.runner.AndroidJUnit4;
29
30import static org.junit.Assert.assertEquals;
Bryce Leec5fe0ee2017-06-30 09:03:09 -070031import static org.junit.Assert.assertNotEquals;
Bryce Lee7566d762017-03-30 09:34:15 -070032import 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)
43public 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 Lee600dadd2017-07-25 10:48:42 -070053 * 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 Lee0e4a6df2017-07-27 14:06:22 -070060 config2.appBounds = new Rect(1, 2, 2, 1);
Bryce Lee600dadd2017-07-25 10:48:42 -070061
Bryce Lee0e4a6df2017-07-27 14:06:22 -070062 assertEquals(ActivityInfo.CONFIG_SCREEN_SIZE, config.diff(config2));
Bryce Lee658d9842017-07-28 08:33:36 -070063 assertEquals(0, config.diffPublicOnly(config2));
Bryce Lee600dadd2017-07-25 10:48:42 -070064 }
65
66 /**
Bryce Lee7566d762017-03-30 09:34:15 -070067 * Ensures the configuration app bounds at the root level match the app dimensions.
68 */
69 @Test
70 public void testRootConfigurationBounds() throws Exception {
Wale Ogunwale11cc5162017-04-25 20:29:13 -070071 final DisplayInfo info = mDisplayContent.getDisplayInfo();
Bryce Lee7566d762017-03-30 09:34:15 -070072 info.appWidth = 1024;
73 info.appHeight = 768;
74
Wale Ogunwale11cc5162017-04-25 20:29:13 -070075 final Configuration config = sWm.computeNewConfiguration(mDisplayContent.getDisplayId());
Bryce Lee7566d762017-03-30 09:34:15 -070076 // 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 Ogunwale11cc5162017-04-25 20:29:13 -0700134 final Rect fullScreenBounds = new Rect(0, 0, mDisplayInfo.logicalWidth,
135 mDisplayInfo.logicalHeight);
Bryce Lee7566d762017-03-30 09:34:15 -0700136 testStackBoundsConfiguration(null /*stackId*/, mParentBounds, fullScreenBounds,
137 mParentBounds);
138 }
139
Bryce Lee7566d762017-03-30 09:34:15 -0700140 private void testStackBoundsConfiguration(Integer stackId, Rect parentBounds, Rect bounds,
141 Rect expectedConfigBounds) {
142 final StackWindowController stackController = stackId != null ?
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700143 createStackControllerOnStackOnDisplay(stackId, mDisplayContent)
144 : createStackControllerOnDisplay(mDisplayContent);
Bryce Lee7566d762017-03-30 09:34:15 -0700145
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700146 final Configuration parentConfig = mDisplayContent.getConfiguration();
Bryce Lee7566d762017-03-30 09:34:15 -0700147 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 Ogunwale11cc5162017-04-25 20:29:13 -0700152 false /*overrideHeight*/, mDisplayInfo.logicalDensityDpi, config, parentConfig);
Bryce Lee7566d762017-03-30 09:34:15 -0700153 // 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 Leec5fe0ee2017-06-30 09:03:09 -0700158
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 Lee7566d762017-03-30 09:34:15 -0700183}