• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 
17 package com.android.ims;
18 
19 import static org.mockito.ArgumentMatchers.any;
20 import static org.mockito.ArgumentMatchers.anyInt;
21 import static org.mockito.ArgumentMatchers.anyString;
22 import static org.mockito.Mockito.doAnswer;
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.spy;
26 
27 import android.content.BroadcastReceiver;
28 import android.content.ContentResolver;
29 import android.content.Context;
30 import android.content.IContentProvider;
31 import android.content.Intent;
32 import android.content.IntentFilter;
33 import android.content.pm.PackageManager;
34 import android.content.res.Resources;
35 import android.net.ConnectivityManager;
36 import android.os.PersistableBundle;
37 import android.telecom.TelecomManager;
38 import android.telephony.CarrierConfigManager;
39 import android.telephony.SubscriptionManager;
40 import android.telephony.TelephonyManager;
41 import android.telephony.ims.ImsManager;
42 import android.test.mock.MockContentResolver;
43 import android.test.mock.MockContext;
44 
45 import org.mockito.stubbing.Answer;
46 
47 import java.util.HashSet;
48 import java.util.concurrent.Executor;
49 
50 public class ContextFixture {
51 
52     private final Context mContext = spy(new FakeContext());
53 
54     private final TelephonyManager mTelephonyManager = mock(TelephonyManager.class);
55     private final ConnectivityManager mConnectivityManager = mock(ConnectivityManager.class);
56     private final CarrierConfigManager mCarrierConfigManager = mock(CarrierConfigManager.class);
57     private final PackageManager mPackageManager = mock(PackageManager.class);
58     private final SubscriptionManager mSubscriptionManager = mock(SubscriptionManager.class);
59     private final ImsManager mImsManager = mock(ImsManager.class);
60     private final Resources mResources = mock(Resources.class);
61 
62     private final PersistableBundle mBundle = new PersistableBundle();
63     private final HashSet<String> mSystemFeatures = new HashSet<>();
64     private final MockContentResolver mMockContentResolver = new MockContentResolver();
65 
ContextFixture()66     public ContextFixture() throws Exception {
67         doReturn(mBundle).when(mCarrierConfigManager).getConfigForSubId(anyInt());
68         doReturn(mBundle).when(mCarrierConfigManager).getConfig();
69 
70         doAnswer((Answer<Boolean>)
71                 invocation -> mSystemFeatures.contains((String) invocation.getArgument(0)))
72                 .when(mPackageManager).hasSystemFeature(any());
73 
74         doReturn(mResources).when(mPackageManager).getResourcesForApplication(anyString());
75         doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(anyInt());
76     }
77 
destroy()78     public void destroy() {
79     }
80 
81     public class FakeContext extends MockContext {
82         @Override
getResources()83         public Resources getResources() {
84             return mResources;
85         }
86 
87         @Override
getPackageManager()88         public PackageManager getPackageManager() {
89             return mPackageManager;
90         }
91 
92         @Override
getSystemService(String name)93         public Object getSystemService(String name) {
94             switch (name) {
95                 case Context.TELEPHONY_SERVICE:
96                     return mTelephonyManager;
97                 case Context.CARRIER_CONFIG_SERVICE:
98                     return mCarrierConfigManager;
99                 case Context.CONNECTIVITY_SERVICE:
100                     return mConnectivityManager;
101                 case Context.TELEPHONY_SUBSCRIPTION_SERVICE:
102                     return mSubscriptionManager;
103                 case Context.TELEPHONY_IMS_SERVICE:
104                     return mImsManager;
105                 default:
106                     return null;
107             }
108         }
109 
110         @Override
getSystemServiceName(Class<?> serviceClass)111         public String getSystemServiceName(Class<?> serviceClass) {
112             if (serviceClass == SubscriptionManager.class) {
113                 return Context.TELEPHONY_SUBSCRIPTION_SERVICE;
114             } else if (serviceClass == TelecomManager.class) {
115                 return Context.TELECOM_SERVICE;
116             } else if (serviceClass == ConnectivityManager.class) {
117                 return Context.CONNECTIVITY_SERVICE;
118             } else if (serviceClass == TelephonyManager.class) {
119                 return Context.TELEPHONY_SERVICE;
120             } else if (serviceClass == ImsManager.class) {
121                 return Context.TELEPHONY_IMS_SERVICE;
122             } else if (serviceClass == CarrierConfigManager.class) {
123                 return Context.CARRIER_CONFIG_SERVICE;
124             }
125             return super.getSystemServiceName(serviceClass);
126         }
127 
128         @Override
registerReceiver(BroadcastReceiver receiver, IntentFilter filter)129         public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
130             return null;
131         }
132 
133         @Override
unregisterReceiver(BroadcastReceiver receiver)134         public void unregisterReceiver(BroadcastReceiver receiver) {
135         }
136 
137         @Override
getContentResolver()138         public ContentResolver getContentResolver() {
139             return mMockContentResolver;
140         }
141 
142         @Override
getMainExecutor()143         public Executor getMainExecutor() {
144             return Runnable::run;
145         }
146 
147         @Override
getApplicationContext()148         public Context getApplicationContext() {
149             return mContext;
150         }
151     }
152 
getContext()153     public Context getContext() {
154         return mContext;
155     }
156 
getTestCarrierConfigBundle()157     public PersistableBundle getTestCarrierConfigBundle() {
158         return mBundle;
159     }
160 
addSystemFeature(String feature)161     public void addSystemFeature(String feature) {
162         mSystemFeatures.add(feature);
163     }
164 
removeSystemFeature(String feature)165     public void removeSystemFeature(String feature) {
166         mSystemFeatures.remove(feature);
167     }
168 }
169