Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | package com.android.server.pm; |
| 17 | |
| 18 | import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.set; |
| 19 | |
| 20 | import android.app.Activity; |
| 21 | import android.content.ComponentName; |
| 22 | import android.content.pm.ActivityInfo; |
| 23 | import android.content.pm.ApplicationInfo; |
| 24 | import android.content.pm.PackageInfo; |
| 25 | import android.content.pm.ShortcutServiceInternal; |
| 26 | import android.content.res.XmlResourceParser; |
| 27 | import android.os.Looper; |
| 28 | import android.test.suitebuilder.annotation.SmallTest; |
| 29 | |
| 30 | import com.android.server.LocalServices; |
| 31 | |
| 32 | import java.util.List; |
| 33 | import java.util.Set; |
| 34 | |
| 35 | /** |
| 36 | * Unit tests for all the IPackageManager related methods in {@link ShortcutService}. |
| 37 | * |
| 38 | * All the tests here actually talks to the real IPackageManager, so we can't test complicated |
| 39 | * cases. Instead we just make sure they all work reasonably without at least crashing. |
| 40 | */ |
| 41 | @SmallTest |
| 42 | public class ShortcutManagerTest5 extends BaseShortcutManagerTest { |
| 43 | private ShortcutService mShortcutService; |
| 44 | |
| 45 | private String mMyPackage; |
| 46 | private int mMyUserId; |
| 47 | |
| 48 | public static class ShortcutEnabled extends Activity { |
| 49 | } |
| 50 | |
| 51 | public static class ShortcutDisabled extends Activity { |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | protected void setUp() throws Exception { |
| 56 | super.setUp(); |
| 57 | |
| 58 | LocalServices.removeServiceForTest(ShortcutServiceInternal.class); |
| 59 | mShortcutService = new ShortcutService(getTestContext(), Looper.getMainLooper(), |
| 60 | /* onyForPackageManagerApis */ true); |
| 61 | |
| 62 | mMyPackage = getTestContext().getPackageName(); |
| 63 | mMyUserId = android.os.Process.myUserHandle().getIdentifier(); |
| 64 | } |
| 65 | |
| 66 | public void testGetPackageUid() { |
| 67 | assertTrue(mShortcutService.injectGetPackageUid( |
| 68 | mMyPackage, mMyUserId) != 0); |
| 69 | |
| 70 | assertEquals(-1, mShortcutService.injectGetPackageUid( |
| 71 | "no.such.package", mMyUserId)); |
| 72 | } |
| 73 | |
| 74 | public void testGetPackageInfo() { |
| 75 | PackageInfo pi = mShortcutService.getPackageInfo( |
| 76 | mMyPackage, mMyUserId, /*signature*/ false); |
| 77 | assertEquals(mMyPackage, pi.packageName); |
| 78 | assertNull(pi.signatures); |
Dan Cashman | 5c9f527e | 2018-04-03 16:42:23 -0700 | [diff] [blame] | 79 | assertNull(pi.signingInfo); |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 80 | |
| 81 | pi = mShortcutService.getPackageInfo( |
| 82 | mMyPackage, mMyUserId, /*signature*/ true); |
| 83 | assertEquals(mMyPackage, pi.packageName); |
Michal Karpinski | 528c3e5 | 2018-02-07 17:47:10 +0000 | [diff] [blame] | 84 | assertNull(pi.signatures); |
Dan Cashman | 5c9f527e | 2018-04-03 16:42:23 -0700 | [diff] [blame] | 85 | assertNotNull(pi.signingInfo); |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 86 | |
| 87 | pi = mShortcutService.getPackageInfo( |
| 88 | "no.such.package", mMyUserId, /*signature*/ true); |
| 89 | assertNull(pi); |
| 90 | } |
| 91 | |
| 92 | public void testGetApplicationInfo() { |
| 93 | ApplicationInfo ai = mShortcutService.getApplicationInfo( |
| 94 | mMyPackage, mMyUserId); |
| 95 | assertEquals(mMyPackage, ai.packageName); |
| 96 | |
| 97 | ai = mShortcutService.getApplicationInfo( |
| 98 | "no.such.package", mMyUserId); |
| 99 | assertNull(ai); |
| 100 | } |
| 101 | |
| 102 | public void testGetActivityInfoWithMetadata() { |
| 103 | // Disabled activity |
| 104 | ActivityInfo ai = mShortcutService.getActivityInfoWithMetadata( |
| 105 | new ComponentName(mMyPackage, "ShortcutDisabled"), mMyUserId); |
| 106 | assertNull(ai); |
| 107 | |
| 108 | // Nonexistent |
| 109 | ai = mShortcutService.getActivityInfoWithMetadata( |
| 110 | new ComponentName("no.such.package", "ShortcutDisabled"), mMyUserId); |
| 111 | assertNull(ai); |
| 112 | |
| 113 | // Existent, with no metadata. |
| 114 | ai = mShortcutService.getActivityInfoWithMetadata( |
| 115 | new ComponentName(mMyPackage, "a.ShortcutEnabled"), mMyUserId); |
| 116 | assertEquals(mMyPackage, ai.packageName); |
| 117 | assertEquals("a.ShortcutEnabled", ai.name); |
| 118 | assertNull(ai.loadXmlMetaData(getTestContext().getPackageManager(), |
| 119 | "android.app.shortcuts")); |
| 120 | |
| 121 | // Existent, with a shortcut metadata. |
| 122 | ai = mShortcutService.getActivityInfoWithMetadata( |
| 123 | new ComponentName(mMyPackage, "a.Shortcut1"), mMyUserId); |
| 124 | assertEquals(mMyPackage, ai.packageName); |
| 125 | assertEquals("a.Shortcut1", ai.name); |
| 126 | XmlResourceParser meta = ai.loadXmlMetaData(getTestContext().getPackageManager(), |
| 127 | "android.app.shortcuts"); |
| 128 | assertNotNull(meta); |
| 129 | meta.close(); |
| 130 | } |
| 131 | |
| 132 | public void testGetInstalledPackages() { |
| 133 | List<PackageInfo> apks = mShortcutService.getInstalledPackages(mMyUserId); |
| 134 | |
| 135 | Set<String> expectedPackages = set("com.android.settings", mMyPackage); |
| 136 | for (PackageInfo pi : apks) { |
| 137 | expectedPackages.remove(pi.packageName); |
| 138 | } |
| 139 | assertEquals(set(), expectedPackages); |
| 140 | } |
| 141 | |
| 142 | public void testGetDefaultMainActivity() { |
| 143 | ComponentName cn = mShortcutService.injectGetDefaultMainActivity( |
| 144 | "com.android.settings", mMyUserId); |
| 145 | |
| 146 | assertEquals( |
| 147 | ComponentName.unflattenFromString("com.android.settings/.Settings"), |
| 148 | cn); |
| 149 | |
| 150 | // This package has no main activity. |
| 151 | assertNull(mShortcutService.injectGetDefaultMainActivity( |
| 152 | mMyPackage, mMyUserId)); |
| 153 | |
| 154 | // Nonexistent. |
| 155 | assertNull(mShortcutService.injectGetDefaultMainActivity( |
| 156 | "no.such.package", mMyUserId)); |
| 157 | } |
| 158 | |
| 159 | public void testIsMainActivity() { |
| 160 | assertTrue(mShortcutService.injectIsMainActivity( |
| 161 | ComponentName.unflattenFromString("com.android.settings/.Settings"), mMyUserId)); |
| 162 | assertFalse(mShortcutService.injectIsMainActivity( |
| 163 | ComponentName.unflattenFromString("com.android.settings/.xxx"), mMyUserId)); |
| 164 | assertFalse(mShortcutService.injectIsMainActivity( |
| 165 | ComponentName.unflattenFromString("no.such.package/.xxx"), mMyUserId)); |
| 166 | |
| 167 | assertFalse(mShortcutService.injectIsMainActivity( |
| 168 | new ComponentName(mMyPackage, "a.DisabledMain"), mMyUserId)); |
| 169 | assertFalse(mShortcutService.injectIsMainActivity( |
| 170 | new ComponentName(mMyPackage, "a.UnexportedMain"), mMyUserId)); |
| 171 | |
| 172 | } |
| 173 | |
| 174 | public void testGetMainActivities() { |
| 175 | assertEquals(1, mShortcutService.injectGetMainActivities( |
| 176 | "com.android.settings", mMyUserId).size()); |
| 177 | |
| 178 | // This APK has no main activities. |
| 179 | assertEquals(0, mShortcutService.injectGetMainActivities( |
| 180 | mMyPackage, mMyUserId).size()); |
| 181 | } |
| 182 | |
| 183 | public void testIsActivityEnabledAndExported() { |
| 184 | assertTrue(mShortcutService.injectIsActivityEnabledAndExported( |
| 185 | ComponentName.unflattenFromString("com.android.settings/.Settings"), mMyUserId)); |
| 186 | assertFalse(mShortcutService.injectIsActivityEnabledAndExported( |
| 187 | ComponentName.unflattenFromString("com.android.settings/.xxx"), mMyUserId)); |
| 188 | assertFalse(mShortcutService.injectIsActivityEnabledAndExported( |
| 189 | ComponentName.unflattenFromString("no.such.package/.xxx"), mMyUserId)); |
| 190 | |
| 191 | assertTrue(mShortcutService.injectIsActivityEnabledAndExported( |
| 192 | new ComponentName(mMyPackage, "com.android.server.pm.ShortcutTestActivity"), |
| 193 | mMyUserId)); |
| 194 | |
| 195 | assertTrue(mShortcutService.injectIsActivityEnabledAndExported( |
| 196 | new ComponentName(mMyPackage, "a.ShortcutEnabled"), mMyUserId)); |
| 197 | |
| 198 | assertFalse(mShortcutService.injectIsActivityEnabledAndExported( |
| 199 | new ComponentName(mMyPackage, "a.ShortcutDisabled"), mMyUserId)); |
| 200 | assertFalse(mShortcutService.injectIsActivityEnabledAndExported( |
| 201 | new ComponentName(mMyPackage, "a.ShortcutUnexported"), mMyUserId)); |
| 202 | |
| 203 | } |
| 204 | } |