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.internal.app; 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 class ResolverDataProvider { 36 37 static private int USER_SOMEONE_ELSE = 10; 38 createResolvedComponentInfo(int i, UserHandle resolvedForUser)39 static ResolverActivity.ResolvedComponentInfo createResolvedComponentInfo(int i, 40 UserHandle resolvedForUser) { 41 return new ResolverActivity.ResolvedComponentInfo( 42 createComponentName(i), 43 createResolverIntent(i), 44 createResolveInfo(i, UserHandle.USER_CURRENT, resolvedForUser)); 45 } 46 createResolvedComponentInfoWithOtherId(int i, UserHandle resolvedForUser)47 static ResolverActivity.ResolvedComponentInfo createResolvedComponentInfoWithOtherId(int i, 48 UserHandle resolvedForUser) { 49 return new ResolverActivity.ResolvedComponentInfo(createComponentName(i), 50 createResolverIntent(i), createResolveInfo(i, USER_SOMEONE_ELSE, resolvedForUser)); 51 } 52 createResolvedComponentInfoWithOtherId(int i, int userId, UserHandle resolvedForUser)53 static ResolverActivity.ResolvedComponentInfo createResolvedComponentInfoWithOtherId(int i, 54 int userId, UserHandle resolvedForUser) { 55 return new ResolverActivity.ResolvedComponentInfo(createComponentName(i), 56 createResolverIntent(i), createResolveInfo(i, userId, resolvedForUser)); 57 } 58 createComponentName(int i)59 static ComponentName createComponentName(int i) { 60 final String name = "component" + i; 61 return new ComponentName("foo.bar." + name, name); 62 } 63 createResolveInfo(int i, int userId, UserHandle resolvedForUser)64 static ResolveInfo createResolveInfo(int i, int userId, UserHandle resolvedForUser) { 65 final ResolveInfo resolveInfo = new ResolveInfo(); 66 resolveInfo.activityInfo = createActivityInfo(i); 67 resolveInfo.targetUserId = userId; 68 resolveInfo.userHandle = resolvedForUser; 69 return resolveInfo; 70 } 71 createActivityInfo(int i)72 static ActivityInfo createActivityInfo(int i) { 73 ActivityInfo ai = new ActivityInfo(); 74 ai.name = "activity_name" + i; 75 ai.packageName = "foo_bar" + i; 76 ai.enabled = true; 77 ai.exported = true; 78 ai.permission = null; 79 ai.applicationInfo = createApplicationInfo(); 80 return ai; 81 } 82 createApplicationInfo()83 static ApplicationInfo createApplicationInfo() { 84 ApplicationInfo ai = new ApplicationInfo(); 85 ai.name = "app_name"; 86 ai.packageName = "foo.bar"; 87 ai.enabled = true; 88 return ai; 89 } 90 91 static class PackageManagerMockedInfo { 92 public Context ctx; 93 public ApplicationInfo appInfo; 94 public ActivityInfo activityInfo; 95 public ResolveInfo resolveInfo; 96 public String setAppLabel; 97 public String setActivityLabel; 98 public String setResolveInfoLabel; 99 } 100 createPackageManagerMockedInfo(boolean hasOverridePermission)101 static PackageManagerMockedInfo createPackageManagerMockedInfo(boolean hasOverridePermission) { 102 final String appLabel = "app_label"; 103 final String activityLabel = "activity_label"; 104 final String resolveInfoLabel = "resolve_info_label"; 105 106 MockContext ctx = new MockContext() { 107 @Override 108 public PackageManager getPackageManager() { 109 return new MockPackageManager() { 110 @Override 111 public int checkPermission(String permName, String pkgName) { 112 if (hasOverridePermission) return PERMISSION_GRANTED; 113 return PERMISSION_DENIED; 114 } 115 }; 116 } 117 118 @Override 119 public Resources getResources() { 120 return new MockResources() { 121 @Override 122 public String getString(int id) throws NotFoundException { 123 if (id == 1) return appLabel; 124 if (id == 2) return activityLabel; 125 if (id == 3) return resolveInfoLabel; 126 return null; 127 } 128 }; 129 } 130 }; 131 132 ApplicationInfo appInfo = new ApplicationInfo() { 133 @Override 134 public CharSequence loadLabel(PackageManager pm) { 135 return appLabel; 136 } 137 }; 138 appInfo.labelRes = 1; 139 140 ActivityInfo activityInfo = new ActivityInfo() { 141 @Override 142 public CharSequence loadLabel(PackageManager pm) { 143 return activityLabel; 144 } 145 }; 146 activityInfo.labelRes = 2; 147 activityInfo.applicationInfo = appInfo; 148 149 ResolveInfo resolveInfo = new ResolveInfo() { 150 @Override 151 public CharSequence loadLabel(PackageManager pm) { 152 return resolveInfoLabel; 153 } 154 }; 155 resolveInfo.activityInfo = activityInfo; 156 resolveInfo.resolvePackageName = "super.fake.packagename"; 157 resolveInfo.labelRes = 3; 158 159 PackageManagerMockedInfo mockedInfo = new PackageManagerMockedInfo(); 160 mockedInfo.activityInfo = activityInfo; 161 mockedInfo.appInfo = appInfo; 162 mockedInfo.ctx = ctx; 163 mockedInfo.resolveInfo = resolveInfo; 164 mockedInfo.setAppLabel = appLabel; 165 mockedInfo.setActivityLabel = activityLabel; 166 mockedInfo.setResolveInfoLabel = resolveInfoLabel; 167 168 return mockedInfo; 169 } 170 createResolverIntent(int i)171 static Intent createResolverIntent(int i) { 172 return new Intent("intentAction" + i); 173 } 174 }