1 /* 2 * Copyright (C) 2016 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.blockednumber; 17 18 import android.app.AppOpsManager; 19 import android.app.backup.BackupManager; 20 import android.content.ContentResolver; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.SharedPreferences; 24 import android.content.pm.PackageManager; 25 import android.content.pm.ProviderInfo; 26 import android.location.CountryDetector; 27 import android.os.UserManager; 28 import android.provider.BlockedNumberContract; 29 import android.telecom.TelecomManager; 30 import android.telephony.CarrierConfigManager; 31 import android.telephony.TelephonyManager; 32 import android.test.mock.MockContentResolver; 33 import android.test.mock.MockContext; 34 35 import org.mockito.Mock; 36 import org.mockito.MockitoAnnotations; 37 38 import java.util.ArrayList; 39 import java.util.HashMap; 40 import java.util.List; 41 42 public class MyMockContext extends MockContext { 43 @Mock 44 CountryDetector mCountryDetector; 45 @Mock 46 AppOpsManager mAppOpsManager; 47 @Mock 48 UserManager mUserManager; 49 @Mock 50 TelecomManager mTelecomManager; 51 @Mock 52 TelephonyManager mTelephonyManager; 53 @Mock 54 CarrierConfigManager mCarrierConfigManager; 55 @Mock 56 BackupManager mBackupManager; 57 58 private final HashMap<Class<?>, String> mSupportedServiceNamesByClass = 59 new HashMap<Class<?>, String>(); 60 private MockContentResolver mResolver; 61 private BlockedNumberProviderTestable mProvider; 62 private Context mRealTestContext; 63 final List<String> mIntentsBroadcasted = new ArrayList<>(); 64 MyMockContext(Context realTestContext)65 public MyMockContext(Context realTestContext) { 66 this.mRealTestContext = realTestContext; 67 } 68 69 @Override getSystemServiceName(Class<?> serviceClass)70 public String getSystemServiceName(Class<?> serviceClass) { 71 if (mSupportedServiceNamesByClass.containsKey(serviceClass)) { 72 return mSupportedServiceNamesByClass.get(serviceClass); 73 } 74 throw new UnsupportedOperationException(); 75 } 76 77 @Override getSystemService(String name)78 public Object getSystemService(String name) { 79 switch (name) { 80 case Context.COUNTRY_DETECTOR: 81 return mCountryDetector; 82 case Context.APP_OPS_SERVICE: 83 return mAppOpsManager; 84 case Context.USER_SERVICE: 85 return mUserManager; 86 case Context.TELECOM_SERVICE: 87 return mTelecomManager; 88 case Context.TELEPHONY_SERVICE: 89 return mTelephonyManager; 90 case Context.CARRIER_CONFIG_SERVICE: 91 return mCarrierConfigManager; 92 } 93 throw new UnsupportedOperationException("Service not supported: " + name); 94 } 95 96 @Override getContentResolver()97 public ContentResolver getContentResolver() { 98 return mResolver; 99 } 100 101 @Override checkCallingPermission(String permission)102 public int checkCallingPermission(String permission) { 103 return permission != null && (permission.equals("android.permission.READ_BLOCKED_NUMBERS") 104 || permission.equals("android.permission.WRITE_BLOCKED_NUMBERS")) 105 ? PackageManager.PERMISSION_GRANTED : PackageManager.PERMISSION_DENIED; 106 } 107 108 @Override getSharedPreferences(String name, int mode)109 public SharedPreferences getSharedPreferences(String name, int mode) { 110 return mRealTestContext.getSharedPreferences(name, mode); 111 } 112 113 @Override sendBroadcast(Intent intent, String receiverPermission)114 public void sendBroadcast(Intent intent, String receiverPermission) { 115 mIntentsBroadcasted.add(intent.getAction()); 116 } 117 initializeContext()118 public void initializeContext() { 119 registerServices(); 120 mResolver = new MockContentResolver(); 121 122 mProvider = new BlockedNumberProviderTestable(mBackupManager); 123 124 final ProviderInfo info = new ProviderInfo(); 125 info.authority = BlockedNumberContract.AUTHORITY; 126 mProvider.attachInfoForTesting(this, info); 127 128 mResolver.addProvider(BlockedNumberContract.AUTHORITY, mProvider); 129 130 SharedPreferences prefs = mRealTestContext.getSharedPreferences( 131 "block_number_provider_prefs", Context.MODE_PRIVATE); 132 prefs.edit().clear().commit(); 133 } 134 registerServices()135 private void registerServices() { 136 MockitoAnnotations.initMocks(this); 137 138 mSupportedServiceNamesByClass.put(CountryDetector.class, Context.COUNTRY_DETECTOR); 139 mSupportedServiceNamesByClass.put(AppOpsManager.class, Context.APP_OPS_SERVICE); 140 mSupportedServiceNamesByClass.put(UserManager.class, Context.USER_SERVICE); 141 mSupportedServiceNamesByClass.put(TelecomManager.class, Context.TELECOM_SERVICE); 142 mSupportedServiceNamesByClass.put(TelephonyManager.class, Context.TELEPHONY_SERVICE); 143 mSupportedServiceNamesByClass.put( 144 CarrierConfigManager.class, Context.CARRIER_CONFIG_SERVICE); 145 } 146 shutdown()147 public void shutdown() { 148 mProvider.shutdown(); 149 } 150 } 151 152