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

Commit e4f21283 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "ModifierShortcutManager: Add more shortcut test cases." into main

parents 93378a02 36b6a85a
Loading
Loading
Loading
Loading
+28 −25
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.server.policy;

import android.annotation.Nullable;
import android.annotation.SuppressLint;
import android.app.role.RoleManager;
import android.content.ActivityNotFoundException;
@@ -248,31 +249,7 @@ public class ModifierShortcutManager {
                                + " className=" + className + " shortcutChar=" + shortcutChar);
                        continue;
                    }
                    ComponentName componentName = new ComponentName(packageName, className);
                    try {
                        mPackageManager.getActivityInfo(componentName,
                                PackageManager.MATCH_DIRECT_BOOT_AWARE
                                        | PackageManager.MATCH_DIRECT_BOOT_UNAWARE
                                        | PackageManager.MATCH_UNINSTALLED_PACKAGES);
                    } catch (PackageManager.NameNotFoundException e) {
                        String[] packages = mPackageManager.canonicalToCurrentPackageNames(
                                new String[] { packageName });
                        componentName = new ComponentName(packages[0], className);
                        try {
                            mPackageManager.getActivityInfo(componentName,
                                    PackageManager.MATCH_DIRECT_BOOT_AWARE
                                            | PackageManager.MATCH_DIRECT_BOOT_UNAWARE
                                            | PackageManager.MATCH_UNINSTALLED_PACKAGES);
                        } catch (PackageManager.NameNotFoundException e1) {
                            Log.w(TAG, "Unable to add bookmark: " + packageName
                                    + "/" + className + " not found.");
                            continue;
                        }
                    }

                    intent = new Intent(Intent.ACTION_MAIN);
                    intent.addCategory(Intent.CATEGORY_LAUNCHER);
                    intent.setComponent(componentName);
                    intent = resolveComponentNameIntent(packageName, className);
                } else if (categoryName != null) {
                    if (roleName != null) {
                        Log.w(TAG, "Cannot specify role bookmark when category is present for"
@@ -310,6 +287,32 @@ public class ModifierShortcutManager {
        }
    }

    @Nullable
    private Intent resolveComponentNameIntent(String packageName, String className) {
        int flags = PackageManager.MATCH_DIRECT_BOOT_UNAWARE
                | PackageManager.MATCH_DIRECT_BOOT_AWARE
                | PackageManager.MATCH_UNINSTALLED_PACKAGES;
        ComponentName componentName = new ComponentName(packageName, className);
        try {
            mPackageManager.getActivityInfo(componentName, flags);
        } catch (PackageManager.NameNotFoundException e) {
            String[] packages = mPackageManager.canonicalToCurrentPackageNames(
                    new String[] { packageName });
            componentName = new ComponentName(packages[0], className);
            try {
                mPackageManager.getActivityInfo(componentName, flags);
            } catch (PackageManager.NameNotFoundException e1) {
                Log.w(TAG, "Unable to add bookmark: " + packageName
                        + "/" + className + " not found.");
                return null;
            }
        }
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setComponent(componentName);
        return intent;
    }

    void registerShortcutKey(long shortcutCode, IShortcutService shortcutService)
            throws RemoteException {
        IShortcutService service = mShortcutKeyServices.get(shortcutCode);
+36 −1
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2024 The Android Open Source Project
<!-- Copyright 2024 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.
@@ -14,6 +14,8 @@
     limitations under the License.
-->
<bookmarks>
    <!-- the key combinations for the following shortcuts must be in sync
         with the key combinations sent by the test in ModifierShortcutTests.java -->
    <bookmark
        role="android.app.role.BROWSER"
        shortcut="b" />
@@ -38,4 +40,37 @@
    <bookmark
        category="android.intent.category.APP_CALCULATOR"
        shortcut="u" />

    <!-- The following shortcuts will not be invoked by tests but are here to
         provide test coverage of parsing the different types of shortcut. -->
    <bookmark
        package="com.test"
        class="com.test.BookmarkTest"
        shortcut="a" />
    <bookmark
        package="com.test2"
        class="com.test.BookmarkTest"
        shortcut="d" />

    <bookmark
        role="android.app.role.BROWSER"
        shortcut="b"
        shift="true" />
    <bookmark
        category="android.intent.category.APP_CONTACTS"
        shortcut="c"
        shift="true" />
    <bookmark
        package="com.test"
        class="com.test.BookmarkTest"
        shortcut="a"
        shift="true" />

    <!-- It's intended that this package/class will NOT resolve so we test the resolution
         failure case. -->
    <bookmark
        package="com.test3"
        class="com.test.BookmarkTest"
        shortcut="f" />

</bookmarks>
+38 −2
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 * Copyright 2024 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.
@@ -19,15 +19,22 @@ package com.android.server.policy;
import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;

import static org.junit.Assert.assertEquals;
import static org.mockito.AdditionalMatchers.aryEq;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.anyObject;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.os.Handler;
@@ -58,27 +65,56 @@ public class ModifierShortcutManagerTests {
    private Handler mHandler;
    private Context mContext;
    private Resources mResources;
    private PackageManager mPackageManager;

    @Before
    public void setUp() {
        mHandler = new Handler(Looper.getMainLooper());
        mContext = spy(getInstrumentation().getTargetContext());
        mResources = spy(mContext.getResources());
        mPackageManager = spy(mContext.getPackageManager());

        XmlResourceParser testBookmarks = mResources.getXml(
                com.android.frameworks.wmtests.R.xml.bookmarks);

        when(mContext.getResources()).thenReturn(mResources);
        when(mContext.getPackageManager()).thenReturn(mPackageManager);
        when(mResources.getXml(R.xml.bookmarks)).thenReturn(testBookmarks);
        try {
            // Keep packageName / className in sync with
            // services/tests/wmtests/res/xml/bookmarks.xml
            ActivityInfo testActivityInfo = new ActivityInfo();
            testActivityInfo.applicationInfo = new ApplicationInfo();
            testActivityInfo.packageName =
                    testActivityInfo.applicationInfo.packageName = "com.test";

            doReturn(testActivityInfo).when(mPackageManager).getActivityInfo(
                    eq(new ComponentName("com.test", "com.test.BookmarkTest")), anyInt());
            doThrow(new PackageManager.NameNotFoundException("com.test3")).when(mPackageManager)
                    .getActivityInfo(eq(new ComponentName("com.test3", "com.test.BookmarkTest")),
                        anyInt());
        } catch (PackageManager.NameNotFoundException ignored) { }
        doReturn(new String[] { "com.test" }).when(mPackageManager)
                .canonicalToCurrentPackageNames(aryEq(new String[] { "com.test2" }));


        mModifierShortcutManager = new ModifierShortcutManager(mContext, mHandler);
    }

    @Test
    public void test_getApplicationLaunchKeyboardShortcuts() {
        // Expected values here determined by the number of shortcuts defined in
        // services/tests/wmtests/res/xml/bookmarks.xml

        // Total valid shortcuts.
        KeyboardShortcutGroup group =
                mModifierShortcutManager.getApplicationLaunchKeyboardShortcuts(-1);
        assertEquals(8, group.getItems().size());
        assertEquals(13, group.getItems().size());

        // Total valid shift shortcuts.
        assertEquals(3, group.getItems().stream()
                .filter(s -> s.getModifiers() == (KeyEvent.META_SHIFT_ON | KeyEvent.META_META_ON))
                .count());
    }

    @Test