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