Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Tomasz Wasilczyk | 7db47c2 | 2024-10-25 13:26:53 -0700 | [diff] [blame] | 19 | import static org.junit.Assert.assertNotNull; |
Brad Ebinger | a68a497 | 2020-01-30 17:31:23 -0800 | [diff] [blame] | 20 | import static org.mockito.Mockito.spy; |
Tomasz Wasilczyk | 7db47c2 | 2024-10-25 13:26:53 -0700 | [diff] [blame] | 21 | import static org.mockito.Mockito.doCallRealMethod; |
| 22 | import static org.mockito.Mockito.doReturn; |
Brad Ebinger | a68a497 | 2020-01-30 17:31:23 -0800 | [diff] [blame] | 23 | |
Tomasz Wasilczyk | 7db47c2 | 2024-10-25 13:26:53 -0700 | [diff] [blame] | 24 | import android.content.ContextWrapper; |
| 25 | import android.content.res.Resources; |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 26 | import android.os.Handler; |
Brad Ebinger | 32925b8 | 2016-12-02 13:01:49 -0800 | [diff] [blame] | 27 | import android.os.Looper; |
Sanket Padawe | d9a427e | 2017-06-08 12:03:07 -0700 | [diff] [blame] | 28 | import android.util.Log; |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 29 | |
Tomasz Wasilczyk | 7db47c2 | 2024-10-25 13:26:53 -0700 | [diff] [blame] | 30 | import androidx.test.InstrumentationRegistry; |
| 31 | |
| 32 | import com.android.internal.telephony.GsmCdmaPhone; |
| 33 | import com.android.internal.telephony.Phone; |
Brad Ebinger | a15005b | 2020-04-29 15:20:38 -0700 | [diff] [blame] | 34 | import com.android.internal.telephony.PhoneConfigurationManager; |
Tomasz Wasilczyk | 7db47c2 | 2024-10-25 13:26:53 -0700 | [diff] [blame] | 35 | import com.android.internal.telephony.PhoneFactory; |
| 36 | import com.android.internal.telephony.data.DataConfigManager; |
| 37 | import com.android.internal.telephony.data.DataNetworkController; |
Tomasz Wasilczyk | f7b28c5 | 2024-11-11 15:55:27 -0800 | [diff] [blame] | 38 | import com.android.internal.telephony.metrics.MetricsCollector; |
| 39 | import com.android.internal.telephony.metrics.PersistAtomsStorage; |
Tomasz Wasilczyk | 7db47c2 | 2024-10-25 13:26:53 -0700 | [diff] [blame] | 40 | import com.android.phone.PhoneGlobals; |
| 41 | import com.android.phone.PhoneInterfaceManager; |
Brad Ebinger | a15005b | 2020-04-29 15:20:38 -0700 | [diff] [blame] | 42 | |
Sarah Chin | e04784a | 2022-10-31 20:32:34 -0700 | [diff] [blame] | 43 | import org.junit.After; |
| 44 | import org.junit.Before; |
tom hsu | 0b59d29 | 2022-09-29 23:49:21 +0800 | [diff] [blame] | 45 | import org.junit.Rule; |
Tomasz Wasilczyk | 7db47c2 | 2024-10-25 13:26:53 -0700 | [diff] [blame] | 46 | import org.mockito.Mock; |
| 47 | import org.mockito.Mockito; |
tom hsu | 0b59d29 | 2022-09-29 23:49:21 +0800 | [diff] [blame] | 48 | import org.mockito.junit.MockitoJUnit; |
| 49 | import org.mockito.junit.MockitoRule; |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 50 | |
Sarah Chin | e04784a | 2022-10-31 20:32:34 -0700 | [diff] [blame] | 51 | import java.lang.reflect.Field; |
Tomasz Wasilczyk | 7db47c2 | 2024-10-25 13:26:53 -0700 | [diff] [blame] | 52 | import java.util.Collections; |
Sarah Chin | e04784a | 2022-10-31 20:32:34 -0700 | [diff] [blame] | 53 | import java.util.HashMap; |
| 54 | import java.util.Iterator; |
| 55 | import java.util.LinkedList; |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 56 | import java.util.concurrent.CountDownLatch; |
Brad Ebinger | 63b6f5a | 2020-10-27 11:43:35 -0700 | [diff] [blame] | 57 | import java.util.concurrent.Executor; |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 58 | import java.util.concurrent.TimeUnit; |
| 59 | |
| 60 | /** |
Tomasz Wasilczyk | 7db47c2 | 2024-10-25 13:26:53 -0700 | [diff] [blame] | 61 | * Helper class to load Mockito Resources into Telephony unit tests. |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 62 | */ |
| 63 | public class TelephonyTestBase { |
tom hsu | 0b59d29 | 2022-09-29 23:49:21 +0800 | [diff] [blame] | 64 | @Rule public final MockitoRule mocks = MockitoJUnit.rule(); |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 65 | |
Brad Ebinger | 792729e | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 66 | protected TestContext mContext; |
Tomasz Wasilczyk | 7db47c2 | 2024-10-25 13:26:53 -0700 | [diff] [blame] | 67 | @Mock protected PhoneGlobals mPhoneGlobals; |
| 68 | @Mock protected GsmCdmaPhone mPhone; |
| 69 | @Mock protected DataNetworkController mDataNetworkController; |
Tomasz Wasilczyk | f7b28c5 | 2024-11-11 15:55:27 -0800 | [diff] [blame] | 70 | @Mock private MetricsCollector mMetricsCollector; |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 71 | |
Sarah Chin | e04784a | 2022-10-31 20:32:34 -0700 | [diff] [blame] | 72 | private final HashMap<InstanceKey, Object> mOldInstances = new HashMap<>(); |
| 73 | private final LinkedList<InstanceKey> mInstanceKeys = new LinkedList<>(); |
| 74 | |
| 75 | @Before |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 76 | public void setUp() throws Exception { |
Brad Ebinger | 32925b8 | 2016-12-02 13:01:49 -0800 | [diff] [blame] | 77 | if (Looper.myLooper() == null) { |
| 78 | Looper.prepare(); |
| 79 | } |
Tomasz Wasilczyk | 1c8068b | 2024-10-29 15:10:04 -0700 | [diff] [blame] | 80 | |
Tomasz Wasilczyk | 7db47c2 | 2024-10-25 13:26:53 -0700 | [diff] [blame] | 81 | doCallRealMethod().when(mPhoneGlobals).getBaseContext(); |
| 82 | doCallRealMethod().when(mPhoneGlobals).getResources(); |
| 83 | doCallRealMethod().when(mPhone).getServiceState(); |
| 84 | |
Tomasz Wasilczyk | 1c8068b | 2024-10-29 15:10:04 -0700 | [diff] [blame] | 85 | mContext = spy(new TestContext()); |
Tomasz Wasilczyk | 7db47c2 | 2024-10-25 13:26:53 -0700 | [diff] [blame] | 86 | doReturn(mContext).when(mPhone).getContext(); |
| 87 | replaceInstance(ContextWrapper.class, "mBase", mPhoneGlobals, mContext); |
| 88 | |
| 89 | Resources resources = InstrumentationRegistry.getTargetContext().getResources(); |
| 90 | assertNotNull(resources); |
| 91 | doReturn(resources).when(mContext).getResources(); |
| 92 | |
| 93 | replaceInstance(Handler.class, "mLooper", mPhone, Looper.myLooper()); |
| 94 | replaceInstance(PhoneFactory.class, "sMadeDefaults", null, true); |
| 95 | replaceInstance(PhoneFactory.class, "sPhone", null, mPhone); |
| 96 | replaceInstance(PhoneFactory.class, "sPhones", null, new Phone[] {mPhone}); |
| 97 | replaceInstance(PhoneGlobals.class, "sMe", null, mPhoneGlobals); |
Tomasz Wasilczyk | f7b28c5 | 2024-11-11 15:55:27 -0800 | [diff] [blame] | 98 | replaceInstance(PhoneFactory.class, "sMetricsCollector", null, mMetricsCollector); |
| 99 | |
| 100 | doReturn(Mockito.mock(PersistAtomsStorage.class)).when(mMetricsCollector).getAtomsStorage(); |
Tomasz Wasilczyk | 7db47c2 | 2024-10-25 13:26:53 -0700 | [diff] [blame] | 101 | |
| 102 | doReturn(mDataNetworkController).when(mPhone).getDataNetworkController(); |
| 103 | doReturn(Collections.emptyList()).when(mDataNetworkController) |
| 104 | .getInternetDataDisallowedReasons(); |
| 105 | doReturn(Mockito.mock(DataConfigManager.class)).when(mDataNetworkController) |
| 106 | .getDataConfigManager(); |
| 107 | |
| 108 | mPhoneGlobals.phoneMgr = Mockito.mock(PhoneInterfaceManager.class); |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Sarah Chin | e04784a | 2022-10-31 20:32:34 -0700 | [diff] [blame] | 111 | @After |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 112 | public void tearDown() throws Exception { |
Brad Ebinger | a15005b | 2020-04-29 15:20:38 -0700 | [diff] [blame] | 113 | // Ensure there are no static references to handlers after test completes. |
| 114 | PhoneConfigurationManager.unregisterAllMultiSimConfigChangeRegistrants(); |
Sarah Chin | e04784a | 2022-10-31 20:32:34 -0700 | [diff] [blame] | 115 | restoreInstances(); |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Brad Ebinger | 63b6f5a | 2020-10-27 11:43:35 -0700 | [diff] [blame] | 118 | protected final boolean waitForExecutorAction(Executor executor, long timeoutMillis) { |
| 119 | final CountDownLatch lock = new CountDownLatch(1); |
Brad Ebinger | 63b6f5a | 2020-10-27 11:43:35 -0700 | [diff] [blame] | 120 | executor.execute(() -> { |
Brad Ebinger | 63b6f5a | 2020-10-27 11:43:35 -0700 | [diff] [blame] | 121 | lock.countDown(); |
| 122 | }); |
| 123 | while (lock.getCount() > 0) { |
| 124 | try { |
| 125 | return lock.await(timeoutMillis, TimeUnit.MILLISECONDS); |
| 126 | } catch (InterruptedException e) { |
| 127 | // do nothing |
| 128 | } |
| 129 | } |
| 130 | return true; |
| 131 | } |
| 132 | |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 133 | protected final void waitForHandlerAction(Handler h, long timeoutMillis) { |
| 134 | final CountDownLatch lock = new CountDownLatch(1); |
| 135 | h.post(lock::countDown); |
| 136 | while (lock.getCount() > 0) { |
| 137 | try { |
| 138 | lock.await(timeoutMillis, TimeUnit.MILLISECONDS); |
| 139 | } catch (InterruptedException e) { |
| 140 | // do nothing |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | protected final void waitForHandlerActionDelayed(Handler h, long timeoutMillis, long delayMs) { |
| 146 | final CountDownLatch lock = new CountDownLatch(1); |
| 147 | h.postDelayed(lock::countDown, delayMs); |
| 148 | while (lock.getCount() > 0) { |
| 149 | try { |
| 150 | lock.await(timeoutMillis, TimeUnit.MILLISECONDS); |
| 151 | } catch (InterruptedException e) { |
| 152 | // do nothing |
| 153 | } |
| 154 | } |
| 155 | } |
Sanket Padawe | d9a427e | 2017-06-08 12:03:07 -0700 | [diff] [blame] | 156 | |
| 157 | protected void waitForMs(long ms) { |
| 158 | try { |
| 159 | Thread.sleep(ms); |
| 160 | } catch (InterruptedException e) { |
| 161 | Log.e("TelephonyTestBase", "InterruptedException while waiting: " + e); |
| 162 | } |
| 163 | } |
Brad Ebinger | 792729e | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 164 | |
Sarah Chin | e04784a | 2022-10-31 20:32:34 -0700 | [diff] [blame] | 165 | protected synchronized void replaceInstance(final Class c, final String instanceName, |
| 166 | final Object obj, final Object newValue) |
| 167 | throws Exception { |
| 168 | Field field = c.getDeclaredField(instanceName); |
| 169 | field.setAccessible(true); |
| 170 | |
| 171 | InstanceKey key = new InstanceKey(c, instanceName, obj); |
| 172 | if (!mOldInstances.containsKey(key)) { |
| 173 | mOldInstances.put(key, field.get(obj)); |
| 174 | mInstanceKeys.add(key); |
| 175 | } |
| 176 | field.set(obj, newValue); |
| 177 | } |
| 178 | |
| 179 | private synchronized void restoreInstances() throws Exception { |
| 180 | Iterator<InstanceKey> it = mInstanceKeys.descendingIterator(); |
| 181 | |
| 182 | while (it.hasNext()) { |
| 183 | InstanceKey key = it.next(); |
| 184 | Field field = key.mClass.getDeclaredField(key.mInstName); |
| 185 | field.setAccessible(true); |
| 186 | field.set(key.mObj, mOldInstances.get(key)); |
| 187 | } |
| 188 | |
| 189 | mInstanceKeys.clear(); |
| 190 | mOldInstances.clear(); |
| 191 | } |
| 192 | |
| 193 | private static class InstanceKey { |
| 194 | public final Class mClass; |
| 195 | public final String mInstName; |
| 196 | public final Object mObj; |
| 197 | InstanceKey(final Class c, final String instName, final Object obj) { |
| 198 | mClass = c; |
| 199 | mInstName = instName; |
| 200 | mObj = obj; |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public int hashCode() { |
| 205 | return (mClass.getName().hashCode() * 31 + mInstName.hashCode()) * 31; |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public boolean equals(Object obj) { |
| 210 | if (obj == null || obj.getClass() != getClass()) { |
| 211 | return false; |
| 212 | } |
| 213 | |
| 214 | InstanceKey other = (InstanceKey) obj; |
| 215 | return (other.mClass == mClass && other.mInstName.equals(mInstName) |
| 216 | && other.mObj == mObj); |
| 217 | } |
| 218 | } |
| 219 | |
Brad Ebinger | 792729e | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 220 | protected TestContext getTestContext() { |
| 221 | return mContext; |
| 222 | } |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 223 | } |