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