Brad Ebinger | dc555b4 | 2019-12-09 16:12:57 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2019 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; |
| 18 | |
| 19 | import static org.mockito.ArgumentMatchers.anyInt; |
| 20 | import static org.mockito.Mockito.doReturn; |
| 21 | |
| 22 | import android.content.BroadcastReceiver; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
| 25 | import android.content.IntentFilter; |
| 26 | import android.os.Handler; |
| 27 | import android.os.PersistableBundle; |
| 28 | import android.telecom.TelecomManager; |
| 29 | import android.telephony.CarrierConfigManager; |
| 30 | import android.telephony.SubscriptionManager; |
| 31 | import android.telephony.TelephonyManager; |
| 32 | import android.test.mock.MockContext; |
| 33 | |
| 34 | import org.mockito.Mock; |
| 35 | import org.mockito.MockitoAnnotations; |
| 36 | |
| 37 | public class TestContext extends MockContext { |
| 38 | |
| 39 | @Mock CarrierConfigManager mMockCarrierConfigManager; |
| 40 | @Mock TelecomManager mMockTelecomManager; |
| 41 | @Mock TelephonyManager mMockTelephonyManager; |
| 42 | @Mock SubscriptionManager mMockSubscriptionManager; |
| 43 | |
| 44 | private PersistableBundle mCarrierConfig = new PersistableBundle(); |
| 45 | |
| 46 | public TestContext() { |
| 47 | MockitoAnnotations.initMocks(this); |
| 48 | doReturn(mCarrierConfig).when(mMockCarrierConfigManager).getConfigForSubId(anyInt()); |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public Context getApplicationContext() { |
| 53 | return this; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public String getPackageName() { |
| 58 | return "com.android.phone.tests"; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public String getFeatureId() { |
| 63 | return ""; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { |
| 68 | return null; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, int flags) { |
| 73 | return null; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, |
| 78 | String broadcastPermission, Handler scheduler) { |
| 79 | return null; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, |
| 84 | String broadcastPermission, Handler scheduler, int flags) { |
| 85 | return null; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public Object getSystemService(String name) { |
| 90 | switch (name) { |
| 91 | case (Context.CARRIER_CONFIG_SERVICE) : { |
| 92 | return mMockCarrierConfigManager; |
| 93 | } |
| 94 | case (Context.TELECOM_SERVICE) : { |
| 95 | return mMockTelecomManager; |
| 96 | } |
| 97 | case (Context.TELEPHONY_SERVICE) : { |
| 98 | return mMockTelephonyManager; |
| 99 | } |
| 100 | case (Context.TELEPHONY_SUBSCRIPTION_SERVICE) : { |
| 101 | return mMockSubscriptionManager; |
| 102 | } |
| 103 | } |
| 104 | return null; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public String getSystemServiceName(Class<?> serviceClass) { |
| 109 | if (serviceClass == CarrierConfigManager.class) { |
| 110 | return Context.CARRIER_CONFIG_SERVICE; |
| 111 | } |
| 112 | if (serviceClass == TelecomManager.class) { |
| 113 | return Context.TELECOM_SERVICE; |
| 114 | } |
| 115 | if (serviceClass == TelephonyManager.class) { |
| 116 | return Context.TELEPHONY_SERVICE; |
| 117 | } |
| 118 | if (serviceClass == SubscriptionManager.class) { |
| 119 | return Context.TELEPHONY_SUBSCRIPTION_SERVICE; |
| 120 | } |
| 121 | return null; |
| 122 | } |
| 123 | |
| 124 | public PersistableBundle getCarrierConfig() { |
| 125 | return mCarrierConfig; |
| 126 | } |
| 127 | } |