Adrian Roos | e1856fe | 2017-11-24 19:39:12 +0100 | [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.policy; |
| 18 | |
| 19 | import static android.view.Surface.ROTATION_0; |
| 20 | import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; |
| 21 | import static android.view.WindowManager.LayoutParams.TYPE_NAVIGATION_BAR; |
| 22 | import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR; |
| 23 | |
| 24 | import static org.junit.Assert.assertEquals; |
| 25 | import static org.mockito.Mockito.mock; |
| 26 | |
| 27 | import android.content.Context; |
| 28 | import android.content.ContextWrapper; |
| 29 | import android.content.pm.PackageManager; |
| 30 | import android.content.res.Resources; |
| 31 | import android.graphics.PixelFormat; |
| 32 | import android.graphics.Rect; |
| 33 | import android.os.IBinder; |
| 34 | import android.os.UserHandle; |
| 35 | import android.support.test.InstrumentationRegistry; |
| 36 | import android.testing.TestableResources; |
| 37 | import android.view.Display; |
| 38 | import android.view.DisplayInfo; |
| 39 | import android.view.Gravity; |
| 40 | import android.view.View; |
| 41 | import android.view.WindowManager; |
| 42 | import android.view.WindowManagerGlobal; |
| 43 | import android.view.accessibility.AccessibilityManager; |
| 44 | import android.view.accessibility.IAccessibilityManager; |
| 45 | |
| 46 | import com.android.server.policy.keyguard.KeyguardServiceDelegate; |
| 47 | import com.android.server.wm.DisplayFrames; |
| 48 | |
| 49 | import org.junit.Before; |
| 50 | |
| 51 | public class PhoneWindowManagerTestBase { |
| 52 | static final int DISPLAY_WIDTH = 500; |
| 53 | static final int DISPLAY_HEIGHT = 1000; |
| 54 | |
| 55 | static final int STATUS_BAR_HEIGHT = 10; |
| 56 | static final int NAV_BAR_HEIGHT = 15; |
| 57 | |
| 58 | TestablePhoneWindowManager mPolicy; |
| 59 | TestContextWrapper mContext; |
| 60 | DisplayFrames mFrames; |
| 61 | |
| 62 | FakeWindowState mStatusBar; |
| 63 | FakeWindowState mNavigationBar; |
| 64 | |
| 65 | @Before |
| 66 | public void setUpBase() throws Exception { |
| 67 | mContext = new TestContextWrapper(InstrumentationRegistry.getTargetContext()); |
| 68 | mContext.getResourceMocker().addOverride( |
| 69 | com.android.internal.R.dimen.status_bar_height, STATUS_BAR_HEIGHT); |
| 70 | mContext.getResourceMocker().addOverride( |
| 71 | com.android.internal.R.dimen.navigation_bar_height, NAV_BAR_HEIGHT); |
| 72 | mContext.getResourceMocker().addOverride( |
| 73 | com.android.internal.R.dimen.navigation_bar_height_landscape, NAV_BAR_HEIGHT); |
| 74 | mContext.getResourceMocker().addOverride( |
| 75 | com.android.internal.R.dimen.navigation_bar_width, NAV_BAR_HEIGHT); |
| 76 | |
| 77 | mPolicy = TestablePhoneWindowManager.create(mContext); |
| 78 | |
| 79 | DisplayInfo info = new DisplayInfo(); |
| 80 | info.logicalWidth = DISPLAY_WIDTH; |
| 81 | info.logicalHeight = DISPLAY_HEIGHT; |
| 82 | info.rotation = ROTATION_0; |
| 83 | mFrames = new DisplayFrames(Display.DEFAULT_DISPLAY, info); |
| 84 | } |
| 85 | |
| 86 | public void addStatusBar() { |
| 87 | mStatusBar = new FakeWindowState(); |
| 88 | mStatusBar.attrs = new WindowManager.LayoutParams(MATCH_PARENT, STATUS_BAR_HEIGHT, |
| 89 | TYPE_STATUS_BAR, 0 /* flags */, PixelFormat.TRANSLUCENT); |
| 90 | mStatusBar.attrs.gravity = Gravity.TOP; |
| 91 | |
| 92 | mPolicy.addWindow(mStatusBar); |
| 93 | mPolicy.mLastSystemUiFlags |= View.STATUS_BAR_TRANSPARENT; |
| 94 | } |
| 95 | |
| 96 | public void addNavigationBar() { |
| 97 | mNavigationBar = new FakeWindowState(); |
| 98 | mNavigationBar.attrs = new WindowManager.LayoutParams(MATCH_PARENT, STATUS_BAR_HEIGHT, |
| 99 | TYPE_NAVIGATION_BAR, 0 /* flags */, PixelFormat.TRANSLUCENT); |
| 100 | mNavigationBar.attrs.gravity = Gravity.BOTTOM; |
| 101 | |
| 102 | mPolicy.addWindow(mNavigationBar); |
| 103 | mPolicy.mHasNavigationBar = true; |
| 104 | mPolicy.mLastSystemUiFlags |= View.NAVIGATION_BAR_TRANSPARENT; |
| 105 | } |
| 106 | |
| 107 | /** Asserts that {@code actual} is inset by the given amounts from the full display rect. */ |
| 108 | public void assertInsetBy(Rect actual, int expectedInsetLeft, int expectedInsetTop, |
| 109 | int expectedInsetRight, int expectedInsetBottom) { |
| 110 | assertEquals(new Rect(expectedInsetLeft, expectedInsetTop, |
| 111 | DISPLAY_WIDTH - expectedInsetRight, DISPLAY_HEIGHT - expectedInsetBottom), actual); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Asserts that {@code actual} is inset by the given amounts from the full display rect. |
| 116 | * |
| 117 | * Convenience wrapper for when only the top and bottom inset are non-zero. |
| 118 | */ |
| 119 | public void assertInsetByTopBottom(Rect actual, int expectedInsetTop, int expectedInsetBottom) { |
| 120 | assertInsetBy(actual, 0, expectedInsetTop, 0, expectedInsetBottom); |
| 121 | } |
| 122 | |
| 123 | static class TestContextWrapper extends ContextWrapper { |
| 124 | private final TestableResources mResourceMocker; |
| 125 | |
| 126 | public TestContextWrapper(Context targetContext) { |
| 127 | super(targetContext); |
| 128 | mResourceMocker = new TestableResources(targetContext.getResources()); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public int checkPermission(String permission, int pid, int uid) { |
| 133 | return PackageManager.PERMISSION_GRANTED; |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public int checkPermission(String permission, int pid, int uid, IBinder callerToken) { |
| 138 | return PackageManager.PERMISSION_GRANTED; |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public Resources getResources() { |
| 143 | return mResourceMocker.getResources(); |
| 144 | } |
| 145 | |
| 146 | public TestableResources getResourceMocker() { |
| 147 | return mResourceMocker; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | static class TestablePhoneWindowManager extends PhoneWindowManager { |
| 152 | |
| 153 | public TestablePhoneWindowManager() { |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | void initializeHdmiState() { |
| 158 | // Do nothing. |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | Context getSystemUiContext() { |
| 163 | return mContext; |
| 164 | } |
| 165 | |
| 166 | void addWindow(WindowState state) { |
| 167 | if (state instanceof FakeWindowState) { |
| 168 | ((FakeWindowState) state).surfaceLayer = |
| 169 | getWindowLayerFromTypeLw(state.getAttrs().type); |
| 170 | } |
| 171 | adjustWindowParamsLw(state, state.getAttrs(), true /* hasStatusBarPermission */); |
| 172 | assertEquals(WindowManagerGlobal.ADD_OKAY, prepareAddWindowLw(state, state.getAttrs())); |
| 173 | } |
| 174 | |
| 175 | public static TestablePhoneWindowManager create(Context context) { |
| 176 | TestablePhoneWindowManager[] policy = new TestablePhoneWindowManager[1]; |
| 177 | InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> { |
| 178 | policy[0] = new TestablePhoneWindowManager(); |
| 179 | policy[0].mContext = context; |
| 180 | policy[0].mKeyguardDelegate = mock(KeyguardServiceDelegate.class); |
| 181 | policy[0].mAccessibilityManager = new AccessibilityManager(context, |
| 182 | mock(IAccessibilityManager.class), UserHandle.USER_CURRENT); |
| 183 | policy[0].mSystemGestures = mock(SystemGesturesPointerEventListener.class); |
| 184 | policy[0].onConfigurationChanged(); |
| 185 | }); |
| 186 | return policy[0]; |
| 187 | } |
| 188 | } |
| 189 | } |