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