• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.pm.ApplicationInfo;
20 import android.content.pm.PackageInfo;
21 import android.content.pm.PackageManager;
22 import android.content.pm.ProviderInfo;
23 import android.content.res.Resources;
24 import android.os.Binder;
25 import android.test.mock.MockPackageManager;
26 
27 import com.google.android.collect.Lists;
28 
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 
54     @Override
getNameForUid(int uid)55     public String getNameForUid(int uid) {
56         return "name-for-uid";
57     }
58 
59     @Override
getPackagesForUid(int uid)60     public String[] getPackagesForUid(int uid) {
61         if (mPackages != null) {
62             return new String[] { mPackages.get(0).packageName };
63         } else {
64             return new String[] { ContactsActor.sCallingPackage };
65         }
66     }
67 
68     @Override
getApplicationInfo(String packageName, int flags)69     public ApplicationInfo getApplicationInfo(String packageName, int flags) {
70         ApplicationInfo info = new ApplicationInfo();
71         Integer uid = mReverse.get(packageName);
72         info.uid = (uid != null) ? uid : -1;
73         return info;
74     }
75 
setInstalledPackages(List<PackageInfo> packages)76     public void setInstalledPackages(List<PackageInfo> packages) {
77         this.mPackages = packages;
78     }
79 
80     @Override
getInstalledPackages(int flags)81     public List<PackageInfo> getInstalledPackages(int flags) {
82         return mPackages;
83     }
84 
85     @Override
getPackageInfo(String packageName, int flags)86     public PackageInfo getPackageInfo(String packageName, int flags) throws NameNotFoundException {
87         for (PackageInfo info : mPackages) {
88             if (info.packageName.equals(packageName)) {
89                 return info;
90             }
91         }
92         throw new NameNotFoundException();
93     }
94 
95     @Override
getResourcesForApplication(String appPackageName)96     public Resources getResourcesForApplication(String appPackageName) {
97         return new ContactsMockResources();
98     }
99 
100     @Override
queryContentProviders(String processName, int uid, int flags)101     public List<ProviderInfo> queryContentProviders(String processName, int uid, int flags) {
102         final List<ProviderInfo> ret = Lists.newArrayList();
103         if (mPackages == null) return ret;
104         for (PackageInfo packageInfo : mPackages) {
105             if (packageInfo.providers == null) continue;
106             for (ProviderInfo providerInfo : packageInfo.providers) {
107                 ret.add(providerInfo);
108             }
109         }
110         return ret;
111     }
112 }
113