WM: Add PhoneWindowManagerLayoutTest

Test: runtest -x services/tests/servicestests/src/com/android/server/policy/PhoneWindowManagerLayoutTest.java
Change-Id: I85af4bdd023a274233362d42628de19483b0c4ea
This commit is contained in:
Adrian Roos
2017-11-24 19:39:12 +01:00
parent 9cc5b4f007
commit e1856fe710
8 changed files with 552 additions and 5 deletions

View File

@@ -108,7 +108,7 @@ public abstract class UEventObserver {
* UEventObserver after this call. Repeated calls have no effect.
*/
public final void stopObserving() {
final UEventThread t = getThread();
final UEventThread t = peekThread();
if (t != null) {
t.removeObserver(this);
}

View File

@@ -240,6 +240,7 @@ import android.view.inputmethod.InputMethodManagerInternal;
import com.android.internal.R;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.logging.MetricsLogger;
import com.android.internal.policy.IKeyguardDismissCallback;
import com.android.internal.policy.IShortcutService;
@@ -1053,7 +1054,8 @@ public class PhoneWindowManager implements WindowManagerPolicy {
private ImmersiveModeConfirmation mImmersiveModeConfirmation;
private SystemGesturesPointerEventListener mSystemGestures;
@VisibleForTesting
SystemGesturesPointerEventListener mSystemGestures;
IStatusBarService getStatusBarService() {
synchronized (mServiceAquireLock) {
@@ -2724,7 +2726,7 @@ public class PhoneWindowManager implements WindowManagerPolicy {
@Override
public void onConfigurationChanged() {
// TODO(multi-display): Define policy for secondary displays.
Context uiContext = ActivityThread.currentActivityThread().getSystemUiContext();
Context uiContext = getSystemUiContext();
final Resources res = uiContext.getResources();
mStatusBarHeight =
@@ -2765,6 +2767,11 @@ public class PhoneWindowManager implements WindowManagerPolicy {
}
}
@VisibleForTesting
Context getSystemUiContext() {
return ActivityThread.currentActivityThread().getSystemUiContext();
}
@Override
public int getMaxWallpaperLayer() {
return getWindowLayerFromTypeLw(TYPE_STATUS_BAR);

View File

@@ -213,7 +213,7 @@ public interface WindowManagerPolicy extends WindowManagerPolicyConstants {
*/
public void computeFrameLw(Rect parentFrame, Rect displayFrame,
Rect overlayFrame, Rect contentFrame, Rect visibleFrame, Rect decorFrame,
Rect stableFrame, Rect outsetFrame);
Rect stableFrame, @Nullable Rect outsetFrame);
/**
* Retrieve the current frame of the window that has been assigned by

View File

@@ -26,6 +26,7 @@ LOCAL_STATIC_JAVA_LIBRARIES := \
platform-test-annotations \
ShortcutManagerTestUtils \
truth-prebuilt \
testables \
testng
LOCAL_AIDL_INCLUDES := $(LOCAL_PATH)/aidl

View File

@@ -0,0 +1,252 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server.policy;
import android.annotation.Nullable;
import android.graphics.Point;
import android.graphics.Rect;
import android.view.Display;
import android.view.IApplicationToken;
import android.view.WindowManager;
public class FakeWindowState implements WindowManagerPolicy.WindowState {
public final Rect parentFrame = new Rect();
public final Rect displayFrame = new Rect();
public final Rect overscanFrame = new Rect();
public final Rect contentFrame = new Rect();
public final Rect visibleFrame = new Rect();
public final Rect decorFrame = new Rect();
public final Rect stableFrame = new Rect();
public Rect outsetFrame = new Rect();
public WindowManager.LayoutParams attrs;
public int displayId;
public boolean isVoiceInteraction;
public boolean inMultiWindowMode;
public boolean visible = true;
public int surfaceLayer = 1;
public boolean policyVisible = true;
@Override
public int getOwningUid() {
throw new UnsupportedOperationException("not implemented");
}
@Override
public String getOwningPackage() {
throw new UnsupportedOperationException("not implemented");
}
@Override
public void computeFrameLw(Rect parentFrame, Rect displayFrame, Rect overlayFrame,
Rect contentFrame, Rect visibleFrame, Rect decorFrame, Rect stableFrame,
@Nullable Rect outsetFrame) {
this.parentFrame.set(parentFrame);
this.displayFrame.set(displayFrame);
this.overscanFrame.set(overlayFrame);
this.contentFrame.set(contentFrame);
this.visibleFrame.set(visibleFrame);
this.decorFrame.set(decorFrame);
this.stableFrame.set(stableFrame);
this.outsetFrame = outsetFrame == null ? null : new Rect(outsetFrame);
}
@Override
public Rect getFrameLw() {
return parentFrame;
}
@Override
public Point getShownPositionLw() {
return new Point(parentFrame.left, parentFrame.top);
}
@Override
public Rect getDisplayFrameLw() {
return displayFrame;
}
@Override
public Rect getOverscanFrameLw() {
return overscanFrame;
}
@Override
public Rect getContentFrameLw() {
return contentFrame;
}
@Override
public Rect getVisibleFrameLw() {
return visibleFrame;
}
@Override
public boolean getGivenInsetsPendingLw() {
throw new UnsupportedOperationException("not implemented");
}
@Override
public Rect getGivenContentInsetsLw() {
throw new UnsupportedOperationException("not implemented");
}
@Override
public Rect getGivenVisibleInsetsLw() {
throw new UnsupportedOperationException("not implemented");
}
@Override
public WindowManager.LayoutParams getAttrs() {
return attrs;
}
@Override
public boolean getNeedsMenuLw(WindowManagerPolicy.WindowState bottom) {
throw new UnsupportedOperationException("not implemented");
}
@Override
public int getSystemUiVisibility() {
return attrs.systemUiVisibility | attrs.subtreeSystemUiVisibility;
}
@Override
public int getSurfaceLayer() {
return surfaceLayer;
}
@Override
public int getBaseType() {
throw new UnsupportedOperationException("not implemented");
}
@Override
public IApplicationToken getAppToken() {
throw new UnsupportedOperationException("not implemented");
}
@Override
public boolean isVoiceInteraction() {
return isVoiceInteraction;
}
@Override
public boolean hasAppShownWindows() {
throw new UnsupportedOperationException("not implemented");
}
@Override
public boolean isVisibleLw() {
return visible && policyVisible;
}
@Override
public boolean isDisplayedLw() {
return isVisibleLw();
}
@Override
public boolean isAnimatingLw() {
return false;
}
@Override
public boolean canAffectSystemUiFlags() {
throw new UnsupportedOperationException("not implemented");
}
@Override
public boolean isGoneForLayoutLw() {
throw new UnsupportedOperationException("not implemented");
}
@Override
public boolean isDrawnLw() {
return true;
}
@Override
public boolean hasDrawnLw() {
return true;
}
@Override
public boolean hideLw(boolean doAnimation) {
if (!policyVisible) {
return false;
}
policyVisible = false;
return true;
}
@Override
public boolean showLw(boolean doAnimation) {
if (policyVisible) {
return false;
}
policyVisible = true;
return true;
}
@Override
public boolean isAlive() {
return true;
}
@Override
public boolean isDefaultDisplay() {
return displayId == Display.DEFAULT_DISPLAY;
}
@Override
public boolean isDimming() {
throw new UnsupportedOperationException("not implemented");
}
@Override
public int getWindowingMode() {
throw new UnsupportedOperationException("not implemented");
}
@Override
public boolean isInMultiWindowMode() {
return inMultiWindowMode;
}
@Override
public int getRotationAnimationHint() {
throw new UnsupportedOperationException("not implemented");
}
@Override
public boolean isInputMethodWindow() {
throw new UnsupportedOperationException("not implemented");
}
@Override
public int getDisplayId() {
return displayId;
}
@Override
public boolean canAcquireSleepToken() {
throw new UnsupportedOperationException("not implemented");
}
}

View File

@@ -0,0 +1,97 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server.policy;
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS;
import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR;
import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_FORCE_DRAW_STATUS_BAR_BACKGROUND;
import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
import android.graphics.PixelFormat;
import android.platform.test.annotations.Presubmit;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import android.view.WindowManager;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
@SmallTest
@Presubmit
public class PhoneWindowManagerLayoutTest extends PhoneWindowManagerTestBase {
private FakeWindowState mAppWindow;
@Before
public void setUp() throws Exception {
mAppWindow = new FakeWindowState();
mAppWindow.attrs = new WindowManager.LayoutParams(MATCH_PARENT, MATCH_PARENT,
TYPE_APPLICATION,
FLAG_LAYOUT_IN_SCREEN | FLAG_LAYOUT_INSET_DECOR | FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS,
PixelFormat.TRANSLUCENT);
addStatusBar();
addNavigationBar();
}
@Test
public void layoutWindowLw_appDrawsBars() throws Exception {
mAppWindow.attrs.flags |= FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS;
mPolicy.addWindow(mAppWindow);
mPolicy.beginLayoutLw(mFrames, 0 /* UI mode */);
mPolicy.layoutWindowLw(mAppWindow, null, mFrames);
assertInsetByTopBottom(mAppWindow.parentFrame, 0, 0);
assertInsetByTopBottom(mAppWindow.stableFrame, STATUS_BAR_HEIGHT, NAV_BAR_HEIGHT);
assertInsetByTopBottom(mAppWindow.contentFrame, STATUS_BAR_HEIGHT, NAV_BAR_HEIGHT);
assertInsetByTopBottom(mAppWindow.decorFrame, 0, 0);
}
@Test
public void layoutWindowLw_appWontDrawBars() throws Exception {
mAppWindow.attrs.flags &= ~FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS;
mPolicy.addWindow(mAppWindow);
mPolicy.beginLayoutLw(mFrames, 0 /* UI mode */);
mPolicy.layoutWindowLw(mAppWindow, null, mFrames);
assertInsetByTopBottom(mAppWindow.parentFrame, 0, NAV_BAR_HEIGHT);
assertInsetByTopBottom(mAppWindow.stableFrame, STATUS_BAR_HEIGHT, NAV_BAR_HEIGHT);
assertInsetByTopBottom(mAppWindow.contentFrame, STATUS_BAR_HEIGHT, NAV_BAR_HEIGHT);
assertInsetByTopBottom(mAppWindow.decorFrame, STATUS_BAR_HEIGHT, NAV_BAR_HEIGHT);
}
@Test
public void layoutWindowLw_appWontDrawBars_forceStatus() throws Exception {
mAppWindow.attrs.flags &= ~FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS;
mAppWindow.attrs.privateFlags |= PRIVATE_FLAG_FORCE_DRAW_STATUS_BAR_BACKGROUND;
mPolicy.addWindow(mAppWindow);
mPolicy.beginLayoutLw(mFrames, 0 /* UI mode */);
mPolicy.layoutWindowLw(mAppWindow, null, mFrames);
assertInsetByTopBottom(mAppWindow.parentFrame, 0, NAV_BAR_HEIGHT);
assertInsetByTopBottom(mAppWindow.stableFrame, STATUS_BAR_HEIGHT, NAV_BAR_HEIGHT);
assertInsetByTopBottom(mAppWindow.contentFrame, STATUS_BAR_HEIGHT, NAV_BAR_HEIGHT);
assertInsetByTopBottom(mAppWindow.decorFrame, 0, NAV_BAR_HEIGHT);
}
}

View File

@@ -0,0 +1,189 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server.policy;
import static android.view.Surface.ROTATION_0;
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.WindowManager.LayoutParams.TYPE_NAVIGATION_BAR;
import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.os.IBinder;
import android.os.UserHandle;
import android.support.test.InstrumentationRegistry;
import android.testing.TestableResources;
import android.view.Display;
import android.view.DisplayInfo;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManagerGlobal;
import android.view.accessibility.AccessibilityManager;
import android.view.accessibility.IAccessibilityManager;
import com.android.server.policy.keyguard.KeyguardServiceDelegate;
import com.android.server.wm.DisplayFrames;
import org.junit.Before;
public class PhoneWindowManagerTestBase {
static final int DISPLAY_WIDTH = 500;
static final int DISPLAY_HEIGHT = 1000;
static final int STATUS_BAR_HEIGHT = 10;
static final int NAV_BAR_HEIGHT = 15;
TestablePhoneWindowManager mPolicy;
TestContextWrapper mContext;
DisplayFrames mFrames;
FakeWindowState mStatusBar;
FakeWindowState mNavigationBar;
@Before
public void setUpBase() throws Exception {
mContext = new TestContextWrapper(InstrumentationRegistry.getTargetContext());
mContext.getResourceMocker().addOverride(
com.android.internal.R.dimen.status_bar_height, STATUS_BAR_HEIGHT);
mContext.getResourceMocker().addOverride(
com.android.internal.R.dimen.navigation_bar_height, NAV_BAR_HEIGHT);
mContext.getResourceMocker().addOverride(
com.android.internal.R.dimen.navigation_bar_height_landscape, NAV_BAR_HEIGHT);
mContext.getResourceMocker().addOverride(
com.android.internal.R.dimen.navigation_bar_width, NAV_BAR_HEIGHT);
mPolicy = TestablePhoneWindowManager.create(mContext);
DisplayInfo info = new DisplayInfo();
info.logicalWidth = DISPLAY_WIDTH;
info.logicalHeight = DISPLAY_HEIGHT;
info.rotation = ROTATION_0;
mFrames = new DisplayFrames(Display.DEFAULT_DISPLAY, info);
}
public void addStatusBar() {
mStatusBar = new FakeWindowState();
mStatusBar.attrs = new WindowManager.LayoutParams(MATCH_PARENT, STATUS_BAR_HEIGHT,
TYPE_STATUS_BAR, 0 /* flags */, PixelFormat.TRANSLUCENT);
mStatusBar.attrs.gravity = Gravity.TOP;
mPolicy.addWindow(mStatusBar);
mPolicy.mLastSystemUiFlags |= View.STATUS_BAR_TRANSPARENT;
}
public void addNavigationBar() {
mNavigationBar = new FakeWindowState();
mNavigationBar.attrs = new WindowManager.LayoutParams(MATCH_PARENT, STATUS_BAR_HEIGHT,
TYPE_NAVIGATION_BAR, 0 /* flags */, PixelFormat.TRANSLUCENT);
mNavigationBar.attrs.gravity = Gravity.BOTTOM;
mPolicy.addWindow(mNavigationBar);
mPolicy.mHasNavigationBar = true;
mPolicy.mLastSystemUiFlags |= View.NAVIGATION_BAR_TRANSPARENT;
}
/** Asserts that {@code actual} is inset by the given amounts from the full display rect. */
public void assertInsetBy(Rect actual, int expectedInsetLeft, int expectedInsetTop,
int expectedInsetRight, int expectedInsetBottom) {
assertEquals(new Rect(expectedInsetLeft, expectedInsetTop,
DISPLAY_WIDTH - expectedInsetRight, DISPLAY_HEIGHT - expectedInsetBottom), actual);
}
/**
* Asserts that {@code actual} is inset by the given amounts from the full display rect.
*
* Convenience wrapper for when only the top and bottom inset are non-zero.
*/
public void assertInsetByTopBottom(Rect actual, int expectedInsetTop, int expectedInsetBottom) {
assertInsetBy(actual, 0, expectedInsetTop, 0, expectedInsetBottom);
}
static class TestContextWrapper extends ContextWrapper {
private final TestableResources mResourceMocker;
public TestContextWrapper(Context targetContext) {
super(targetContext);
mResourceMocker = new TestableResources(targetContext.getResources());
}
@Override
public int checkPermission(String permission, int pid, int uid) {
return PackageManager.PERMISSION_GRANTED;
}
@Override
public int checkPermission(String permission, int pid, int uid, IBinder callerToken) {
return PackageManager.PERMISSION_GRANTED;
}
@Override
public Resources getResources() {
return mResourceMocker.getResources();
}
public TestableResources getResourceMocker() {
return mResourceMocker;
}
}
static class TestablePhoneWindowManager extends PhoneWindowManager {
public TestablePhoneWindowManager() {
}
@Override
void initializeHdmiState() {
// Do nothing.
}
@Override
Context getSystemUiContext() {
return mContext;
}
void addWindow(WindowState state) {
if (state instanceof FakeWindowState) {
((FakeWindowState) state).surfaceLayer =
getWindowLayerFromTypeLw(state.getAttrs().type);
}
adjustWindowParamsLw(state, state.getAttrs(), true /* hasStatusBarPermission */);
assertEquals(WindowManagerGlobal.ADD_OKAY, prepareAddWindowLw(state, state.getAttrs()));
}
public static TestablePhoneWindowManager create(Context context) {
TestablePhoneWindowManager[] policy = new TestablePhoneWindowManager[1];
InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> {
policy[0] = new TestablePhoneWindowManager();
policy[0].mContext = context;
policy[0].mKeyguardDelegate = mock(KeyguardServiceDelegate.class);
policy[0].mAccessibilityManager = new AccessibilityManager(context,
mock(IAccessibilityManager.class), UserHandle.USER_CURRENT);
policy[0].mSystemGestures = mock(SystemGesturesPointerEventListener.class);
policy[0].onConfigurationChanged();
});
return policy[0];
}
}
}

View File

@@ -39,7 +39,8 @@ public class TestableResources {
private final Resources mResources;
private final SparseArray<Object> mOverrides = new SparseArray<>();
TestableResources(Resources realResources) {
/** Creates a TestableResources instance that calls through to the given real Resources. */
public TestableResources(Resources realResources) {
mResources = mock(Resources.class, withSettings()
.spiedInstance(realResources)
.defaultAnswer(this::answer));