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