Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 65f2472a authored by Chen Bai's avatar Chen Bai Committed by Android (Google) Code Review
Browse files

Merge changes from topic "cherrypicker-L09900000960673879:N49200001369201569" into udc-qpr-dev

* changes:
  phonewm: fix allapp tests for phone & improve stem key testing infra
  phonewm: give longer wait for open all-app test
  phonewm: test for opening all-app during oobe/setup
parents 4562ef27 b6adc1a6
Loading
Loading
Loading
Loading
+22 −16
Original line number Diff line number Diff line
@@ -536,10 +536,6 @@ public class PhoneWindowManager implements WindowManagerPolicy {
    int mShortPressOnSleepBehavior;
    int mShortPressOnWindowBehavior;
    int mPowerVolUpBehavior;
    int mShortPressOnStemPrimaryBehavior;
    int mDoublePressOnStemPrimaryBehavior;
    int mTriplePressOnStemPrimaryBehavior;
    int mLongPressOnStemPrimaryBehavior;
    boolean mStylusButtonsEnabled = true;
    boolean mHasSoftInput = false;
    boolean mHapticTextHandleEnabled;
@@ -553,6 +549,12 @@ public class PhoneWindowManager implements WindowManagerPolicy {
    int mSearchKeyBehavior;
    ComponentName mSearchKeyTargetActivity;

    // Key Behavior - Stem Primary
    private int mShortPressOnStemPrimaryBehavior;
    private int mDoublePressOnStemPrimaryBehavior;
    private int mTriplePressOnStemPrimaryBehavior;
    private int mLongPressOnStemPrimaryBehavior;

    private boolean mHandleVolumeKeysInWM;

    private boolean mPendingKeyguardOccluded;
@@ -1989,6 +1991,21 @@ public class PhoneWindowManager implements WindowManagerPolicy {
        Supplier<GlobalActions> getGlobalActionsFactory() {
            return () -> new GlobalActions(mContext, mWindowManagerFuncs);
        }

        KeyguardServiceDelegate getKeyguardServiceDelegate() {
            return new KeyguardServiceDelegate(mContext,
                    new StateCallback() {
                        @Override
                        public void onTrustedChanged() {
                            mWindowManagerFuncs.notifyKeyguardTrustedChanged();
                        }

                        @Override
                        public void onShowingChanged() {
                            mWindowManagerFuncs.onKeyguardShowingAndNotOccludedChanged();
                        }
                    });
        }
    }

    /** {@inheritDoc} */
@@ -2246,18 +2263,7 @@ public class PhoneWindowManager implements WindowManagerPolicy {

        mKeyguardDrawnTimeout = mContext.getResources().getInteger(
                com.android.internal.R.integer.config_keyguardDrawnTimeout);
        mKeyguardDelegate = new KeyguardServiceDelegate(mContext,
                new StateCallback() {
                    @Override
                    public void onTrustedChanged() {
                        mWindowManagerFuncs.notifyKeyguardTrustedChanged();
                    }

                    @Override
                    public void onShowingChanged() {
                        mWindowManagerFuncs.onKeyguardShowingAndNotOccludedChanged();
                    }
                });
        mKeyguardDelegate = injector.getKeyguardServiceDelegate();
        initKeyCombinationRules();
        initSingleKeyGestureRules();
        mSideFpsEventHandler = new SideFpsEventHandler(mContext, mHandler, mPowerManager);
+2 −1
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ import static android.view.KeyEvent.META_SHIFT_RIGHT_ON;

import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;

import static com.android.dx.mockito.inline.extended.ExtendedMockito.spy;
import static com.android.server.policy.WindowManagerPolicy.ACTION_PASS_TO_USER;

import static java.util.Collections.unmodifiableMap;
@@ -59,7 +60,7 @@ import java.util.Map;

class ShortcutKeyTestBase {
    TestPhoneWindowManager mPhoneWindowManager;
    final Context mContext = getInstrumentation().getTargetContext();
    final Context mContext = spy(getInstrumentation().getTargetContext());

    /** Modifier key to meta state */
    private static final Map<Integer, Integer> MODIFIER;
+102 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.KeyEvent.KEYCODE_STEM_PRIMARY;

import static com.android.dx.mockito.inline.extended.ExtendedMockito.doAnswer;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;
import static com.android.server.policy.PhoneWindowManager.SHORT_PRESS_PRIMARY_LAUNCH_ALL_APPS;

import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;

import android.content.Context;
import android.content.res.Resources;

import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;

/**
 * Test class for stem key gesture.
 *
 * Build/Install/Run:
 * atest WmTests:StemKeyGestureTests
 */
public class StemKeyGestureTests extends ShortcutKeyTestBase {
    @Mock private Resources mResources;

    /**
     * Stem single key should not launch behavior during set up.
     */
    @Test
    public void stemSingleKey_duringSetup_doNothing() {
        stemKeySetup(
                () -> overrideBehavior(
                        com.android.internal.R.integer.config_shortPressOnStemPrimaryBehavior,
                        SHORT_PRESS_PRIMARY_LAUNCH_ALL_APPS));
        mPhoneWindowManager.setKeyguardServiceDelegateIsShowing(false);
        mPhoneWindowManager.overrideIsUserSetupComplete(false);

        sendKey(KEYCODE_STEM_PRIMARY);

        mPhoneWindowManager.assertNotOpenAllAppView();
    }

    /**
     * Stem single key should launch all app after set up.
     */
    @Test
    public void stemSingleKey_AfterSetup_openAllApp() {
        stemKeySetup(
                () -> overrideBehavior(
                        com.android.internal.R.integer.config_shortPressOnStemPrimaryBehavior,
                        SHORT_PRESS_PRIMARY_LAUNCH_ALL_APPS));
        mPhoneWindowManager.setKeyguardServiceDelegateIsShowing(false);
        mPhoneWindowManager.overrideIsUserSetupComplete(true);

        sendKey(KEYCODE_STEM_PRIMARY);

        mPhoneWindowManager.assertOpenAllAppView();
    }

    private void stemKeySetup(Runnable behaviorOverrideRunnable) {
        super.tearDown();
        setupResourcesMock();
        behaviorOverrideRunnable.run();
        super.setUp();
    }

    private void setupResourcesMock() {
        Resources realResources = mContext.getResources();

        mResources = Mockito.mock(Resources.class);
        doReturn(mResources).when(mContext).getResources();

        doAnswer(invocation -> realResources.getXml((Integer) invocation.getArguments()[0]))
                .when(mResources).getXml(anyInt());
        doAnswer(invocation -> realResources.getString((Integer) invocation.getArguments()[0]))
                .when(mResources).getString(anyInt());
        doAnswer(invocation -> realResources.getBoolean((Integer) invocation.getArguments()[0]))
                .when(mResources).getBoolean(anyInt());
    }

    private void overrideBehavior(int resId, int expectedBehavior) {
        doReturn(expectedBehavior).when(mResources).getInteger(eq(resId));
    }
}
+40 −3
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.server.policy;

import static android.os.Build.HW_TIMEOUT_MULTIPLIER;
import static android.provider.Settings.Secure.VOLUME_HUSH_MUTE;
import static android.view.Display.DEFAULT_DISPLAY;
import static android.view.Display.STATE_ON;
@@ -43,8 +44,11 @@ import static com.android.server.policy.PhoneWindowManager.LONG_PRESS_POWER_SHUT
import static com.android.server.policy.PhoneWindowManager.POWER_VOLUME_UP_BEHAVIOR_MUTE;

import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.CALLS_REAL_METHODS;
import static org.mockito.Mockito.after;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mockingDetails;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.withSettings;
@@ -64,6 +68,7 @@ import android.os.HandlerThread;
import android.os.PowerManager;
import android.os.PowerManagerInternal;
import android.os.RemoteException;
import android.os.UserHandle;
import android.os.Vibrator;
import android.service.dreams.DreamManagerInternal;
import android.telecom.TelecomManager;
@@ -79,6 +84,7 @@ import com.android.server.LocalServices;
import com.android.server.input.InputManagerInternal;
import com.android.server.inputmethod.InputMethodManagerInternal;
import com.android.server.pm.UserManagerInternal;
import com.android.server.policy.keyguard.KeyguardServiceDelegate;
import com.android.server.statusbar.StatusBarManagerInternal;
import com.android.server.vr.VrManagerInternal;
import com.android.server.wm.ActivityTaskManagerInternal;
@@ -100,6 +106,8 @@ import java.util.function.Supplier;

class TestPhoneWindowManager {
    private static final long SHORTCUT_KEY_DELAY_MILLIS = 150;
    private static final long TEST_SINGLE_KEY_DELAY_MILLIS
            = SingleKeyGestureDetector.MULTI_PRESS_TIMEOUT + 1000L * HW_TIMEOUT_MULTIPLIER;

    private PhoneWindowManager mPhoneWindowManager;
    private Context mContext;
@@ -134,6 +142,8 @@ class TestPhoneWindowManager {

    @Mock private StatusBarManagerInternal mStatusBarManagerInternal;

    @Mock private KeyguardServiceDelegate mKeyguardServiceDelegate;

    private StaticMockitoSession mMockitoSession;
    private HandlerThread mHandlerThread;
    private Handler mHandler;
@@ -151,6 +161,10 @@ class TestPhoneWindowManager {
        Supplier<GlobalActions> getGlobalActionsFactory() {
            return () -> mGlobalActions;
        }

        KeyguardServiceDelegate getKeyguardServiceDelegate() {
            return mKeyguardServiceDelegate;
        }
    }

    TestPhoneWindowManager(Context context) {
@@ -158,12 +172,12 @@ class TestPhoneWindowManager {
        mHandlerThread = new HandlerThread("fake window manager");
        mHandlerThread.start();
        mHandler = new Handler(mHandlerThread.getLooper());
        mHandler.runWithScissors(()-> setUp(context),  0 /* timeout */);
        mContext = mockingDetails(context).isSpy() ? context : spy(context);
        mHandler.runWithScissors(this::setUp,  0 /* timeout */);
    }

    private void setUp(Context context) {
    private void setUp() {
        mPhoneWindowManager = spy(new PhoneWindowManager());
        mContext = spy(context);

        // Use stubOnly() to reduce memory usage if it doesn't need verification.
        final MockSettings spyStubOnly = withSettings().stubOnly()
@@ -251,6 +265,7 @@ class TestPhoneWindowManager {
        overrideLaunchAccessibility();
        doReturn(false).when(mPhoneWindowManager).keyguardOn();
        doNothing().when(mContext).startActivityAsUser(any(), any());
        doNothing().when(mContext).startActivityAsUser(any(), any(), any());
        Mockito.reset(mContext);
    }

@@ -381,6 +396,14 @@ class TestPhoneWindowManager {
        doNothing().when(mPhoneWindowManager).launchHomeFromHotKey(anyInt());
    }

    void overrideIsUserSetupComplete(boolean isCompleted) {
        doReturn(isCompleted).when(mPhoneWindowManager).isUserSetupComplete();
    }

    void setKeyguardServiceDelegateIsShowing(boolean isShowing) {
        doReturn(isShowing).when(mKeyguardServiceDelegate).isShowing();
    }

    /**
     * Below functions will check the policy behavior could be invoked.
     */
@@ -514,4 +537,18 @@ class TestPhoneWindowManager {
        waitForIdle();
        verify(mPhoneWindowManager).launchHomeFromHotKey(anyInt());
    }

    void assertOpenAllAppView() {
        waitForIdle();
        ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
        verify(mContext, timeout(TEST_SINGLE_KEY_DELAY_MILLIS))
                .startActivityAsUser(intentCaptor.capture(), isNull(), any(UserHandle.class));
        Assert.assertEquals(Intent.ACTION_ALL_APPS, intentCaptor.getValue().getAction());
    }

    void assertNotOpenAllAppView() {
        waitForIdle();
        verify(mContext, after(TEST_SINGLE_KEY_DELAY_MILLIS).never())
                .startActivityAsUser(any(Intent.class), any(), any(UserHandle.class));
    }
}