1 /*
2  * Copyright (C) 2010 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 com.android.providers.contacts;
17 
18 import android.content.Context;
19 import android.content.Intent;
20 import android.content.pm.ApplicationInfo;
21 import android.content.pm.PackageInfo;
22 import android.content.pm.PackageManager;
23 import android.content.pm.ResolveInfo;
24 import android.content.res.Resources;
25 import android.os.Binder;
26 import android.test.mock.MockPackageManager;
27 
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
31 
32 /**
33  * Mock {@link PackageManager} that knows about a specific set of packages
34  * to help test security models. Because {@link Binder#getCallingUid()}
35  * can't be mocked, you'll have to find your mock-UID manually using your
36  * {@link Context#getPackageName()}.
37  */
38 public class ContactsMockPackageManager extends MockPackageManager {
39     private final HashMap<Integer, String> mForward = new HashMap<Integer, String>();
40     private final HashMap<String, Integer> mReverse = new HashMap<String, Integer>();
41     private List<PackageInfo> mPackages;
42 
ContactsMockPackageManager()43     public ContactsMockPackageManager() {
44     }
45 
46     /**
47      * Add a UID-to-package mapping, which is then stored internally.
48      */
addPackage(int packageUid, String packageName)49     public void addPackage(int packageUid, String packageName) {
50         mForward.put(packageUid, packageName);
51         mReverse.put(packageName, packageUid);
52     }
53 
removePackage(int packageUid)54     public void removePackage(int packageUid) {
55         final String packageName = mForward.remove(packageUid);
56         if (packageName != null) {
57             mReverse.remove(packageName);
58         }
59     }
60 
61     @Override
getNameForUid(int uid)62     public String getNameForUid(int uid) {
63         return "name-for-uid";
64     }
65 
66     @Override
getPackagesForUid(int uid)67     public String[] getPackagesForUid(int uid) {
68         final String packageName = mForward.get(uid);
69         if (packageName != null) {
70             return new String[] {packageName};
71         } else if (mPackages != null) {
72             return new String[] { mPackages.get(0).packageName };
73         } else {
74             return new String[] {};
75         }
76     }
77 
78     @Override
getApplicationInfo(String packageName, int flags)79     public ApplicationInfo getApplicationInfo(String packageName, int flags) {
80         ApplicationInfo info = new ApplicationInfo();
81         Integer uid = mReverse.get(packageName);
82         info.uid = (uid != null) ? uid : -1;
83         return info;
84     }
85 
setInstalledPackages(List<PackageInfo> packages)86     public void setInstalledPackages(List<PackageInfo> packages) {
87         this.mPackages = packages;
88     }
89 
90     @Override
getInstalledPackages(int flags)91     public List<PackageInfo> getInstalledPackages(int flags) {
92         return mPackages;
93     }
94 
95     @Override
getPackageInfo(String packageName, int flags)96     public PackageInfo getPackageInfo(String packageName, int flags) throws NameNotFoundException {
97         for (PackageInfo info : mPackages) {
98             if (info.packageName.equals(packageName)) {
99                 return info;
100             }
101         }
102         throw new NameNotFoundException();
103     }
104 
105     @Override
queryBroadcastReceivers(Intent intent, int flags)106     public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags) {
107         return new ArrayList<ResolveInfo>();
108     }
109 
110     @Override
getResourcesForApplication(String appPackageName)111     public Resources getResourcesForApplication(String appPackageName) {
112         return new ContactsMockResources();
113     }
114 }
115