1 /* 2 * Copyright (C) 2008 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 17 package com.android.intentresolver; 18 19 import android.content.ComponentName; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.pm.ActivityInfo; 23 import android.content.pm.ApplicationInfo; 24 import android.content.pm.PackageManager; 25 import android.content.pm.ResolveInfo; 26 import android.content.res.Resources; 27 import android.os.UserHandle; 28 import android.test.mock.MockContext; 29 import android.test.mock.MockPackageManager; 30 import android.test.mock.MockResources; 31 32 /** 33 * Utility class used by resolver tests to create mock data 34 */ 35 public class ResolverDataProvider { 36 37 static private int USER_SOMEONE_ELSE = 10; 38 createResolvedComponentInfo(int i)39 static ResolvedComponentInfo createResolvedComponentInfo(int i) { 40 return new ResolvedComponentInfo( 41 createComponentName(i), 42 createResolverIntent(i), 43 createResolveInfo(i, UserHandle.USER_CURRENT)); 44 } 45 createResolvedComponentInfo( ComponentName componentName, Intent intent)46 static ResolvedComponentInfo createResolvedComponentInfo( 47 ComponentName componentName, Intent intent) { 48 return new ResolvedComponentInfo( 49 componentName, 50 intent, 51 createResolveInfo(componentName, UserHandle.USER_CURRENT)); 52 } 53 createResolvedComponentInfoWithOtherId(int i)54 static ResolvedComponentInfo createResolvedComponentInfoWithOtherId(int i) { 55 return new ResolvedComponentInfo( 56 createComponentName(i), 57 createResolverIntent(i), 58 createResolveInfo(i, USER_SOMEONE_ELSE)); 59 } 60 createResolvedComponentInfoWithOtherId(int i, int userId)61 static ResolvedComponentInfo createResolvedComponentInfoWithOtherId(int i, int userId) { 62 return new ResolvedComponentInfo( 63 createComponentName(i), 64 createResolverIntent(i), 65 createResolveInfo(i, userId)); 66 } 67 createComponentName(int i)68 public static ComponentName createComponentName(int i) { 69 final String name = "component" + i; 70 return new ComponentName("foo.bar." + name, name); 71 } 72 createResolveInfo(int i, int userId)73 public static ResolveInfo createResolveInfo(int i, int userId) { 74 final ResolveInfo resolveInfo = new ResolveInfo(); 75 resolveInfo.activityInfo = createActivityInfo(i); 76 resolveInfo.targetUserId = userId; 77 return resolveInfo; 78 } 79 createResolveInfo(ComponentName componentName, int userId)80 public static ResolveInfo createResolveInfo(ComponentName componentName, int userId) { 81 final ResolveInfo resolveInfo = new ResolveInfo(); 82 resolveInfo.activityInfo = createActivityInfo(componentName); 83 resolveInfo.targetUserId = userId; 84 return resolveInfo; 85 } 86 createActivityInfo(int i)87 static ActivityInfo createActivityInfo(int i) { 88 ActivityInfo ai = new ActivityInfo(); 89 ai.name = "activity_name" + i; 90 ai.packageName = "foo_bar" + i; 91 ai.enabled = true; 92 ai.exported = true; 93 ai.permission = null; 94 ai.applicationInfo = createApplicationInfo(); 95 return ai; 96 } 97 createActivityInfo(ComponentName componentName)98 static ActivityInfo createActivityInfo(ComponentName componentName) { 99 ActivityInfo ai = new ActivityInfo(); 100 ai.name = componentName.getClassName(); 101 ai.packageName = componentName.getPackageName(); 102 ai.enabled = true; 103 ai.exported = true; 104 ai.permission = null; 105 ai.applicationInfo = createApplicationInfo(); 106 ai.applicationInfo.packageName = componentName.getPackageName(); 107 return ai; 108 } 109 createApplicationInfo()110 static ApplicationInfo createApplicationInfo() { 111 ApplicationInfo ai = new ApplicationInfo(); 112 ai.name = "app_name"; 113 ai.packageName = "foo.bar"; 114 ai.enabled = true; 115 return ai; 116 } 117 118 static class PackageManagerMockedInfo { 119 public Context ctx; 120 public ApplicationInfo appInfo; 121 public ActivityInfo activityInfo; 122 public ResolveInfo resolveInfo; 123 public String setAppLabel; 124 public String setActivityLabel; 125 public String setResolveInfoLabel; 126 } 127 128 /** Create a {@link PackageManagerMockedInfo} with all distinct labels. */ createPackageManagerMockedInfo(boolean hasOverridePermission)129 static PackageManagerMockedInfo createPackageManagerMockedInfo(boolean hasOverridePermission) { 130 return createPackageManagerMockedInfo( 131 hasOverridePermission, "app_label", "activity_label", "resolve_info_label"); 132 } 133 createPackageManagerMockedInfo( boolean hasOverridePermission, String appLabel, String activityLabel, String resolveInfoLabel)134 static PackageManagerMockedInfo createPackageManagerMockedInfo( 135 boolean hasOverridePermission, 136 String appLabel, 137 String activityLabel, 138 String resolveInfoLabel) { 139 MockContext ctx = new MockContext() { 140 @Override 141 public PackageManager getPackageManager() { 142 return new MockPackageManager() { 143 @Override 144 public int checkPermission(String permName, String pkgName) { 145 if (hasOverridePermission) return PERMISSION_GRANTED; 146 return PERMISSION_DENIED; 147 } 148 }; 149 } 150 151 @Override 152 public Resources getResources() { 153 return new MockResources() { 154 @Override 155 public String getString(int id) throws NotFoundException { 156 if (id == 1) return appLabel; 157 if (id == 2) return activityLabel; 158 if (id == 3) return resolveInfoLabel; 159 return null; 160 } 161 }; 162 } 163 }; 164 165 ApplicationInfo appInfo = new ApplicationInfo() { 166 @Override 167 public CharSequence loadLabel(PackageManager pm) { 168 return appLabel; 169 } 170 }; 171 appInfo.labelRes = 1; 172 173 ActivityInfo activityInfo = new ActivityInfo() { 174 @Override 175 public CharSequence loadLabel(PackageManager pm) { 176 return activityLabel; 177 } 178 }; 179 activityInfo.labelRes = 2; 180 activityInfo.applicationInfo = appInfo; 181 182 ResolveInfo resolveInfo = new ResolveInfo() { 183 @Override 184 public CharSequence loadLabel(PackageManager pm) { 185 return resolveInfoLabel; 186 } 187 }; 188 resolveInfo.activityInfo = activityInfo; 189 resolveInfo.resolvePackageName = "super.fake.packagename"; 190 resolveInfo.labelRes = 3; 191 192 PackageManagerMockedInfo mockedInfo = new PackageManagerMockedInfo(); 193 mockedInfo.activityInfo = activityInfo; 194 mockedInfo.appInfo = appInfo; 195 mockedInfo.ctx = ctx; 196 mockedInfo.resolveInfo = resolveInfo; 197 mockedInfo.setAppLabel = appLabel; 198 mockedInfo.setActivityLabel = activityLabel; 199 mockedInfo.setResolveInfoLabel = resolveInfoLabel; 200 201 return mockedInfo; 202 } 203 createResolverIntent(int i)204 static Intent createResolverIntent(int i) { 205 return new Intent("intentAction" + i); 206 } 207 } 208