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