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 android.content.pm.cts.shortcut.device.common; 17 18 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.getDefaultLauncher; 19 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.list; 20 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.setDefaultLauncher; 21 22 import android.content.ComponentName; 23 import android.content.Context; 24 import android.content.pm.LauncherApps; 25 import android.content.pm.LauncherApps.ShortcutQuery; 26 import android.content.pm.ShortcutInfo; 27 import android.content.pm.ShortcutManager; 28 import android.graphics.drawable.Drawable; 29 import android.graphics.drawable.Icon; 30 import android.os.UserHandle; 31 import android.test.InstrumentationTestCase; 32 import android.text.TextUtils; 33 34 import java.util.List; 35 36 /** 37 * Base class for device side tests for the host test. 38 */ 39 public abstract class ShortcutManagerDeviceTestBase extends InstrumentationTestCase { 40 private ShortcutManager mManager; 41 private LauncherApps mLauncherApps; 42 43 private String mOriginalLauncher; 44 45 protected Context getContext() { 46 return getInstrumentation().getTargetContext(); 47 } 48 49 @Override 50 protected void setUp() throws Exception { 51 super.setUp(); 52 53 mOriginalLauncher = getDefaultLauncher(getInstrumentation()); 54 55 mManager = getContext().getSystemService(ShortcutManager.class); 56 mLauncherApps = getContext().getSystemService(LauncherApps.class); 57 } 58 59 @Override 60 protected void tearDown() throws Exception { 61 if (!TextUtils.isEmpty(mOriginalLauncher)) { 62 setDefaultLauncher(getInstrumentation(), mOriginalLauncher); 63 } 64 65 super.tearDown(); 66 } 67 68 protected ShortcutManager getManager() { 69 return mManager; 70 } 71 72 protected LauncherApps getLauncherApps() { 73 return mLauncherApps; 74 } 75 76 protected UserHandle getUserHandle() { 77 return android.os.Process.myUserHandle(); 78 } 79 80 protected void setAsDefaultLauncher(Class<?> clazz) { 81 setDefaultLauncher(getInstrumentation(), 82 getContext().getPackageName() + "/" + clazz.getName()); 83 } 84 85 protected Drawable getIconAsLauncher(String packageName, String shortcutId) { 86 final ShortcutQuery q = new ShortcutQuery() 87 .setQueryFlags(ShortcutQuery.FLAG_MATCH_DYNAMIC 88 | ShortcutQuery.FLAG_MATCH_MANIFEST 89 | ShortcutQuery.FLAG_MATCH_PINNED 90 | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY) 91 .setPackage(packageName) 92 .setShortcutIds(list(shortcutId)); 93 final List<ShortcutInfo> found = getLauncherApps().getShortcuts(q, getUserHandle()); 94 95 assertEquals("Shortcut not found", 1, found.size()); 96 97 return getLauncherApps().getShortcutIconDrawable(found.get(0), 0); 98 } 99 100 protected void assertIconDimensions(String packageName, 101 String shortcutId, Icon expectedIcon) { 102 final Drawable actual = getIconAsLauncher(packageName, shortcutId); 103 if (actual == null && expectedIcon == null) { 104 return; // okay 105 } 106 final Drawable expected = expectedIcon.loadDrawable(getContext()); 107 assertEquals(expected.getIntrinsicWidth(), actual.getIntrinsicWidth()); 108 assertEquals(expected.getIntrinsicHeight(), actual.getIntrinsicHeight()); 109 } 110 111 public ComponentName getActivity(String className) { 112 return new ComponentName(getContext(), getContext().getPackageName() + "." + className); 113 } 114 115 protected List<ShortcutInfo> getPackageShortcuts(String packageName) { 116 final ShortcutQuery q = new ShortcutQuery() 117 .setQueryFlags(ShortcutQuery.FLAG_MATCH_DYNAMIC 118 | ShortcutQuery.FLAG_MATCH_MANIFEST 119 | ShortcutQuery.FLAG_MATCH_PINNED) 120 .setPackage(packageName); 121 return getLauncherApps().getShortcuts(q, getUserHandle()); 122 } 123 } 124