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     private final String packageName = "com.android.providers.blockednumber";
65 
MyMockContext(Context realTestContext)66     public MyMockContext(Context realTestContext) {
67         this.mRealTestContext = realTestContext;
68     }
69 
70     @Override
getSystemServiceName(Class<?> serviceClass)71     public String getSystemServiceName(Class<?> serviceClass) {
72         if (mSupportedServiceNamesByClass.containsKey(serviceClass)) {
73             return mSupportedServiceNamesByClass.get(serviceClass);
74         }
75         throw new UnsupportedOperationException();
76     }
77 
78     @Override
getSystemService(String name)79     public Object getSystemService(String name) {
80         switch (name) {
81             case Context.COUNTRY_DETECTOR:
82                 return mCountryDetector;
83             case Context.APP_OPS_SERVICE:
84                 return mAppOpsManager;
85             case Context.USER_SERVICE:
86                 return mUserManager;
87             case Context.TELECOM_SERVICE:
88                 return mTelecomManager;
89             case Context.TELEPHONY_SERVICE:
90                 return mTelephonyManager;
91             case Context.CARRIER_CONFIG_SERVICE:
92                 return mCarrierConfigManager;
93         }
94         throw new UnsupportedOperationException("Service not supported: " + name);
95     }
96 
97     @Override
getContentResolver()98     public ContentResolver getContentResolver() {
99         return mResolver;
100     }
101 
102     @Override
checkCallingPermission(String permission)103     public int checkCallingPermission(String permission) {
104         return permission != null && (permission.equals("android.permission.READ_BLOCKED_NUMBERS")
105                 || permission.equals("android.permission.WRITE_BLOCKED_NUMBERS"))
106                 ? PackageManager.PERMISSION_GRANTED : PackageManager.PERMISSION_DENIED;
107     }
108 
109     @Override
getSharedPreferences(String name, int mode)110     public SharedPreferences getSharedPreferences(String name, int mode) {
111         return mRealTestContext.getSharedPreferences(name, mode);
112     }
113 
114     @Override
sendBroadcast(Intent intent, String receiverPermission)115     public void sendBroadcast(Intent intent, String receiverPermission) {
116         mIntentsBroadcasted.add(intent.getAction());
117     }
118 
initializeContext()119     public void initializeContext() {
120         registerServices();
121         mResolver = new MockContentResolver();
122 
123         mProvider = new BlockedNumberProviderTestable(mBackupManager);
124 
125         final ProviderInfo info = new ProviderInfo();
126         info.authority = BlockedNumberContract.AUTHORITY;
127         mProvider.attachInfoForTesting(this, info);
128 
129         mResolver.addProvider(BlockedNumberContract.AUTHORITY, mProvider);
130 
131         SharedPreferences prefs = mRealTestContext.getSharedPreferences(
132                 "block_number_provider_prefs", Context.MODE_PRIVATE);
133         prefs.edit().clear().commit();
134     }
135 
registerServices()136     private void registerServices() {
137         MockitoAnnotations.initMocks(this);
138 
139         mSupportedServiceNamesByClass.put(CountryDetector.class, Context.COUNTRY_DETECTOR);
140         mSupportedServiceNamesByClass.put(AppOpsManager.class, Context.APP_OPS_SERVICE);
141         mSupportedServiceNamesByClass.put(UserManager.class, Context.USER_SERVICE);
142         mSupportedServiceNamesByClass.put(TelecomManager.class, Context.TELECOM_SERVICE);
143         mSupportedServiceNamesByClass.put(TelephonyManager.class, Context.TELEPHONY_SERVICE);
144         mSupportedServiceNamesByClass.put(
145                 CarrierConfigManager.class, Context.CARRIER_CONFIG_SERVICE);
146     }
147 
148     @Override
getOpPackageName()149     public String getOpPackageName() {
150         return packageName;
151     }
152 
shutdown()153     public void shutdown() {
154         mProvider.shutdown();
155     }
156 }
157 
158