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; |
James.cf Lin | cdad386 | 2020-02-25 15:55:03 +0800 | [diff] [blame] | 23 | import android.content.ContentResolver; |
Brad Ebinger | dc555b4 | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.content.IntentFilter; |
| 27 | import android.os.Handler; |
| 28 | import android.os.PersistableBundle; |
| 29 | import android.telecom.TelecomManager; |
| 30 | import android.telephony.CarrierConfigManager; |
| 31 | import android.telephony.SubscriptionManager; |
| 32 | import android.telephony.TelephonyManager; |
James.cf Lin | cdad386 | 2020-02-25 15:55:03 +0800 | [diff] [blame] | 33 | import android.telephony.ims.ImsManager; |
Brad Ebinger | dc555b4 | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 34 | import android.test.mock.MockContext; |
| 35 | |
| 36 | import org.mockito.Mock; |
| 37 | import org.mockito.MockitoAnnotations; |
| 38 | |
Brad Ebinger | a68a497 | 2020-01-30 17:31:23 -0800 | [diff] [blame] | 39 | import java.util.concurrent.Executor; |
| 40 | |
Brad Ebinger | dc555b4 | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 41 | public class TestContext extends MockContext { |
| 42 | |
| 43 | @Mock CarrierConfigManager mMockCarrierConfigManager; |
| 44 | @Mock TelecomManager mMockTelecomManager; |
| 45 | @Mock TelephonyManager mMockTelephonyManager; |
| 46 | @Mock SubscriptionManager mMockSubscriptionManager; |
James.cf Lin | cdad386 | 2020-02-25 15:55:03 +0800 | [diff] [blame] | 47 | @Mock ImsManager mMockImsManager; |
Brad Ebinger | dc555b4 | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 48 | |
| 49 | private PersistableBundle mCarrierConfig = new PersistableBundle(); |
| 50 | |
| 51 | public TestContext() { |
| 52 | MockitoAnnotations.initMocks(this); |
| 53 | doReturn(mCarrierConfig).when(mMockCarrierConfigManager).getConfigForSubId(anyInt()); |
| 54 | } |
| 55 | |
| 56 | @Override |
Brad Ebinger | a68a497 | 2020-01-30 17:31:23 -0800 | [diff] [blame] | 57 | public Executor getMainExecutor() { |
| 58 | // Just run on current thread |
| 59 | return Runnable::run; |
| 60 | } |
| 61 | |
| 62 | @Override |
Brad Ebinger | dc555b4 | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 63 | public Context getApplicationContext() { |
| 64 | return this; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public String getPackageName() { |
| 69 | return "com.android.phone.tests"; |
| 70 | } |
| 71 | |
| 72 | @Override |
Philip P. Moltmann | 8d34f0c | 2020-03-05 16:24:02 -0800 | [diff] [blame] | 73 | public String getAttributionTag() { |
Brad Ebinger | dc555b4 | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 74 | return ""; |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { |
| 79 | return null; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, int flags) { |
| 84 | return null; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, |
| 89 | String broadcastPermission, Handler scheduler) { |
| 90 | return null; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, |
| 95 | String broadcastPermission, Handler scheduler, int flags) { |
| 96 | return null; |
| 97 | } |
| 98 | |
| 99 | @Override |
James.cf Lin | cdad386 | 2020-02-25 15:55:03 +0800 | [diff] [blame] | 100 | public ContentResolver getContentResolver() { |
| 101 | return null; |
| 102 | } |
| 103 | |
| 104 | @Override |
Brad Ebinger | dc555b4 | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 105 | public Object getSystemService(String name) { |
| 106 | switch (name) { |
| 107 | case (Context.CARRIER_CONFIG_SERVICE) : { |
| 108 | return mMockCarrierConfigManager; |
| 109 | } |
| 110 | case (Context.TELECOM_SERVICE) : { |
| 111 | return mMockTelecomManager; |
| 112 | } |
| 113 | case (Context.TELEPHONY_SERVICE) : { |
| 114 | return mMockTelephonyManager; |
| 115 | } |
| 116 | case (Context.TELEPHONY_SUBSCRIPTION_SERVICE) : { |
| 117 | return mMockSubscriptionManager; |
| 118 | } |
James.cf Lin | cdad386 | 2020-02-25 15:55:03 +0800 | [diff] [blame] | 119 | case(Context.TELEPHONY_IMS_SERVICE) : { |
| 120 | return mMockImsManager; |
| 121 | } |
Brad Ebinger | dc555b4 | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 122 | } |
| 123 | return null; |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public String getSystemServiceName(Class<?> serviceClass) { |
| 128 | if (serviceClass == CarrierConfigManager.class) { |
| 129 | return Context.CARRIER_CONFIG_SERVICE; |
| 130 | } |
| 131 | if (serviceClass == TelecomManager.class) { |
| 132 | return Context.TELECOM_SERVICE; |
| 133 | } |
| 134 | if (serviceClass == TelephonyManager.class) { |
| 135 | return Context.TELEPHONY_SERVICE; |
| 136 | } |
| 137 | if (serviceClass == SubscriptionManager.class) { |
| 138 | return Context.TELEPHONY_SUBSCRIPTION_SERVICE; |
| 139 | } |
| 140 | return null; |
| 141 | } |
| 142 | |
| 143 | public PersistableBundle getCarrierConfig() { |
| 144 | return mCarrierConfig; |
| 145 | } |
| 146 | } |