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 import androidx.annotation.NonNull;
33 
34 /**
35  * Utility class used by resolver tests to create mock data
36  */
37 public class ResolverDataProvider {
38 
39     static private int USER_SOMEONE_ELSE = 10;
40 
createResolvedComponentInfo(int i)41     static ResolvedComponentInfo createResolvedComponentInfo(int i) {
42         return new ResolvedComponentInfo(
43                 createComponentName(i),
44                 createResolverIntent(i),
45                 createResolveInfo(i, UserHandle.USER_CURRENT));
46     }
47 
createResolvedComponentInfo(int i, UserHandle resolvedForUser)48     public static ResolvedComponentInfo createResolvedComponentInfo(int i,
49             UserHandle resolvedForUser) {
50         return new ResolvedComponentInfo(
51                 createComponentName(i),
52                 createResolverIntent(i),
53                 createResolveInfo(i, UserHandle.USER_CURRENT, resolvedForUser));
54     }
55 
createResolvedComponentInfo( ComponentName componentName, Intent intent)56     static ResolvedComponentInfo createResolvedComponentInfo(
57             ComponentName componentName, Intent intent) {
58         return new ResolvedComponentInfo(
59                 componentName,
60                 intent,
61                 createResolveInfo(componentName, UserHandle.USER_CURRENT));
62     }
63 
createResolvedComponentInfo( ComponentName componentName, Intent intent, UserHandle resolvedForUser)64     public static ResolvedComponentInfo createResolvedComponentInfo(
65             ComponentName componentName, Intent intent, UserHandle resolvedForUser) {
66         return new ResolvedComponentInfo(
67                 componentName,
68                 intent,
69                 createResolveInfo(componentName, UserHandle.USER_CURRENT, resolvedForUser));
70     }
71 
createResolvedComponentInfoWithOtherId(int i)72     static ResolvedComponentInfo createResolvedComponentInfoWithOtherId(int i) {
73         return new ResolvedComponentInfo(
74                 createComponentName(i),
75                 createResolverIntent(i),
76                 createResolveInfo(i, USER_SOMEONE_ELSE));
77     }
78 
createResolvedComponentInfoWithOtherId(int i, UserHandle resolvedForUser)79     public static ResolvedComponentInfo createResolvedComponentInfoWithOtherId(int i,
80                                                                                UserHandle resolvedForUser) {
81         return new ResolvedComponentInfo(
82                 createComponentName(i),
83                 createResolverIntent(i),
84                 createResolveInfo(i, USER_SOMEONE_ELSE, resolvedForUser));
85     }
86 
createResolvedComponentInfoWithOtherId(int i, int userId)87     static ResolvedComponentInfo createResolvedComponentInfoWithOtherId(int i, int userId) {
88         return new ResolvedComponentInfo(
89                 createComponentName(i),
90                 createResolverIntent(i),
91                 createResolveInfo(i, userId));
92     }
93 
createResolvedComponentInfoWithOtherId(int i, int userId, UserHandle resolvedForUser)94     public static ResolvedComponentInfo createResolvedComponentInfoWithOtherId(int i,
95             int userId, UserHandle resolvedForUser) {
96         return new ResolvedComponentInfo(
97                 createComponentName(i),
98                 createResolverIntent(i),
99                 createResolveInfo(i, userId, resolvedForUser));
100     }
101 
createComponentName(int i)102     public static ComponentName createComponentName(int i) {
103         final String name = "component" + i;
104         return new ComponentName("foo.bar." + name, name);
105     }
106 
createResolveInfo(int i, int userId)107     public static ResolveInfo createResolveInfo(int i, int userId) {
108         return createResolveInfo(i, userId, UserHandle.of(userId));
109     }
110 
createResolveInfo(int i, int userId, UserHandle resolvedForUser)111     public static ResolveInfo createResolveInfo(int i, int userId, UserHandle resolvedForUser) {
112         return createResolveInfo(createActivityInfo(i), userId, resolvedForUser);
113     }
114 
createResolveInfo(ComponentName componentName, int userId)115     public static ResolveInfo createResolveInfo(ComponentName componentName, int userId) {
116         return createResolveInfo(componentName, userId, UserHandle.of(userId));
117     }
118 
createResolveInfo( ComponentName componentName, int userId, UserHandle resolvedForUser)119     public static ResolveInfo createResolveInfo(
120             ComponentName componentName, int userId, UserHandle resolvedForUser) {
121         return createResolveInfo(createActivityInfo(componentName), userId, resolvedForUser);
122     }
123 
createResolveInfo( ActivityInfo activityInfo, int userId, UserHandle resolvedForUser)124     public static ResolveInfo createResolveInfo(
125             ActivityInfo activityInfo, int userId, UserHandle resolvedForUser) {
126         final ResolveInfo resolveInfo = new ResolveInfo();
127         resolveInfo.activityInfo = activityInfo;
128         resolveInfo.targetUserId = userId;
129         resolveInfo.userHandle = resolvedForUser;
130         return resolveInfo;
131     }
132 
createActivityInfo(int i)133     static ActivityInfo createActivityInfo(int i) {
134         ActivityInfo ai = new ActivityInfo();
135         ai.name = "activity_name" + i;
136         ai.packageName = "foo_bar" + i;
137         ai.enabled = true;
138         ai.exported = true;
139         ai.permission = null;
140         ai.applicationInfo = createApplicationInfo();
141         return ai;
142     }
143 
createActivityInfo(ComponentName componentName)144     static ActivityInfo createActivityInfo(ComponentName componentName) {
145         ActivityInfo ai = new ActivityInfo();
146         ai.name = componentName.getClassName();
147         ai.packageName = componentName.getPackageName();
148         ai.enabled = true;
149         ai.exported = true;
150         ai.permission = null;
151         ai.applicationInfo = createApplicationInfo();
152         ai.applicationInfo.packageName = componentName.getPackageName();
153         return ai;
154     }
155 
createApplicationInfo()156     static ApplicationInfo createApplicationInfo() {
157         ApplicationInfo ai = new ApplicationInfo();
158         ai.name = "app_name";
159         ai.packageName = "foo.bar";
160         ai.enabled = true;
161         return ai;
162     }
163 
164     static class PackageManagerMockedInfo {
165         public Context ctx;
166         public ApplicationInfo appInfo;
167         public ActivityInfo activityInfo;
168         public ResolveInfo resolveInfo;
169         public String setAppLabel;
170         public String setActivityLabel;
171         public String setResolveInfoLabel;
172     }
173 
174     /** Create a {@link PackageManagerMockedInfo} with all distinct labels. */
createPackageManagerMockedInfo(boolean hasOverridePermission)175     static PackageManagerMockedInfo createPackageManagerMockedInfo(boolean hasOverridePermission) {
176         return createPackageManagerMockedInfo(
177                 hasOverridePermission, "app_label", "activity_label", "resolve_info_label");
178     }
179 
createPackageManagerMockedInfo( boolean hasOverridePermission, String appLabel, String activityLabel, String resolveInfoLabel)180     static PackageManagerMockedInfo createPackageManagerMockedInfo(
181             boolean hasOverridePermission,
182             String appLabel,
183             String activityLabel,
184             String resolveInfoLabel) {
185         MockContext ctx = new MockContext() {
186             @Override
187             public PackageManager getPackageManager() {
188                 return new MockPackageManager() {
189                     @Override
190                     public int checkPermission(String permName, String pkgName) {
191                         if (hasOverridePermission) return PERMISSION_GRANTED;
192                         return PERMISSION_DENIED;
193                     }
194                 };
195             }
196 
197             @Override
198             public Resources getResources() {
199                 return new MockResources() {
200                     @NonNull
201                     @Override
202                     public String getString(int id) throws NotFoundException {
203                         if (id == 1) return appLabel;
204                         if (id == 2) return activityLabel;
205                         if (id == 3) return resolveInfoLabel;
206                         throw new NotFoundException();
207                     }
208                 };
209             }
210         };
211 
212         ApplicationInfo appInfo = new ApplicationInfo() {
213             @NonNull
214             @Override
215             public CharSequence loadLabel(@NonNull PackageManager pm) {
216                 return appLabel;
217             }
218         };
219         appInfo.labelRes = 1;
220 
221         ActivityInfo activityInfo = new ActivityInfo() {
222             @NonNull
223             @Override
224             public CharSequence loadLabel(@NonNull PackageManager pm) {
225                 return activityLabel;
226             }
227         };
228         activityInfo.labelRes = 2;
229         activityInfo.applicationInfo = appInfo;
230 
231         ResolveInfo resolveInfo = new ResolveInfo() {
232             @NonNull
233             @Override
234             public CharSequence loadLabel(@NonNull PackageManager pm) {
235                 return resolveInfoLabel;
236             }
237         };
238         resolveInfo.activityInfo = activityInfo;
239         resolveInfo.resolvePackageName = "super.fake.packagename";
240         resolveInfo.labelRes = 3;
241 
242         PackageManagerMockedInfo mockedInfo = new PackageManagerMockedInfo();
243         mockedInfo.activityInfo = activityInfo;
244         mockedInfo.appInfo = appInfo;
245         mockedInfo.ctx = ctx;
246         mockedInfo.resolveInfo = resolveInfo;
247         mockedInfo.setAppLabel = appLabel;
248         mockedInfo.setActivityLabel = activityLabel;
249         mockedInfo.setResolveInfoLabel = resolveInfoLabel;
250 
251         return mockedInfo;
252     }
253 
createResolverIntent(int i)254     static Intent createResolverIntent(int i) {
255         return new Intent("intentAction" + i);
256     }
257 }
258