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 | |
Brad Ebinger | a68a497 | 2020-01-30 17:31:23 -0800 | [diff] [blame] | 19 | import static org.mockito.Mockito.spy; |
| 20 | |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 21 | import android.os.Handler; |
Brad Ebinger | 32925b8 | 2016-12-02 13:01:49 -0800 | [diff] [blame] | 22 | import android.os.Looper; |
Sanket Padawe | d9a427e | 2017-06-08 12:03:07 -0700 | [diff] [blame] | 23 | import android.util.Log; |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 24 | |
Brad Ebinger | a15005b | 2020-04-29 15:20:38 -0700 | [diff] [blame] | 25 | import com.android.internal.telephony.PhoneConfigurationManager; |
| 26 | |
Sarah Chin | e04784a | 2022-10-31 20:32:34 -0700 | [diff] [blame] | 27 | import org.junit.After; |
| 28 | import org.junit.Before; |
tom hsu | 0b59d29 | 2022-09-29 23:49:21 +0800 | [diff] [blame] | 29 | import org.junit.Rule; |
| 30 | import org.mockito.junit.MockitoJUnit; |
| 31 | import org.mockito.junit.MockitoRule; |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 32 | |
Sarah Chin | e04784a | 2022-10-31 20:32:34 -0700 | [diff] [blame] | 33 | import java.lang.reflect.Field; |
| 34 | import java.util.HashMap; |
| 35 | import java.util.Iterator; |
| 36 | import java.util.LinkedList; |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 37 | import java.util.concurrent.CountDownLatch; |
Brad Ebinger | 63b6f5a | 2020-10-27 11:43:35 -0700 | [diff] [blame] | 38 | import java.util.concurrent.Executor; |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 39 | import java.util.concurrent.TimeUnit; |
| 40 | |
| 41 | /** |
| 42 | * Helper class to load Mockito Resources into a test. |
| 43 | */ |
| 44 | public class TelephonyTestBase { |
tom hsu | 0b59d29 | 2022-09-29 23:49:21 +0800 | [diff] [blame] | 45 | @Rule public final MockitoRule mocks = MockitoJUnit.rule(); |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 46 | |
Brad Ebinger | 792729e | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 47 | protected TestContext mContext; |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 48 | |
Sarah Chin | e04784a | 2022-10-31 20:32:34 -0700 | [diff] [blame] | 49 | private final HashMap<InstanceKey, Object> mOldInstances = new HashMap<>(); |
| 50 | private final LinkedList<InstanceKey> mInstanceKeys = new LinkedList<>(); |
| 51 | |
| 52 | @Before |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 53 | public void setUp() throws Exception { |
Brad Ebinger | 32925b8 | 2016-12-02 13:01:49 -0800 | [diff] [blame] | 54 | if (Looper.myLooper() == null) { |
| 55 | Looper.prepare(); |
| 56 | } |
Tomasz Wasilczyk | 1c8068b | 2024-10-29 15:10:04 -0700 | [diff] [blame^] | 57 | |
| 58 | mContext = spy(new TestContext()); |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Sarah Chin | e04784a | 2022-10-31 20:32:34 -0700 | [diff] [blame] | 61 | @After |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 62 | public void tearDown() throws Exception { |
Brad Ebinger | a15005b | 2020-04-29 15:20:38 -0700 | [diff] [blame] | 63 | // Ensure there are no static references to handlers after test completes. |
| 64 | PhoneConfigurationManager.unregisterAllMultiSimConfigChangeRegistrants(); |
Sarah Chin | e04784a | 2022-10-31 20:32:34 -0700 | [diff] [blame] | 65 | restoreInstances(); |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Brad Ebinger | 63b6f5a | 2020-10-27 11:43:35 -0700 | [diff] [blame] | 68 | protected final boolean waitForExecutorAction(Executor executor, long timeoutMillis) { |
| 69 | final CountDownLatch lock = new CountDownLatch(1); |
Brad Ebinger | 63b6f5a | 2020-10-27 11:43:35 -0700 | [diff] [blame] | 70 | executor.execute(() -> { |
Brad Ebinger | 63b6f5a | 2020-10-27 11:43:35 -0700 | [diff] [blame] | 71 | lock.countDown(); |
| 72 | }); |
| 73 | while (lock.getCount() > 0) { |
| 74 | try { |
| 75 | return lock.await(timeoutMillis, TimeUnit.MILLISECONDS); |
| 76 | } catch (InterruptedException e) { |
| 77 | // do nothing |
| 78 | } |
| 79 | } |
| 80 | return true; |
| 81 | } |
| 82 | |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 83 | protected final void waitForHandlerAction(Handler h, long timeoutMillis) { |
| 84 | final CountDownLatch lock = new CountDownLatch(1); |
| 85 | h.post(lock::countDown); |
| 86 | while (lock.getCount() > 0) { |
| 87 | try { |
| 88 | lock.await(timeoutMillis, TimeUnit.MILLISECONDS); |
| 89 | } catch (InterruptedException e) { |
| 90 | // do nothing |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | protected final void waitForHandlerActionDelayed(Handler h, long timeoutMillis, long delayMs) { |
| 96 | final CountDownLatch lock = new CountDownLatch(1); |
| 97 | h.postDelayed(lock::countDown, delayMs); |
| 98 | while (lock.getCount() > 0) { |
| 99 | try { |
| 100 | lock.await(timeoutMillis, TimeUnit.MILLISECONDS); |
| 101 | } catch (InterruptedException e) { |
| 102 | // do nothing |
| 103 | } |
| 104 | } |
| 105 | } |
Sanket Padawe | d9a427e | 2017-06-08 12:03:07 -0700 | [diff] [blame] | 106 | |
| 107 | protected void waitForMs(long ms) { |
| 108 | try { |
| 109 | Thread.sleep(ms); |
| 110 | } catch (InterruptedException e) { |
| 111 | Log.e("TelephonyTestBase", "InterruptedException while waiting: " + e); |
| 112 | } |
| 113 | } |
Brad Ebinger | 792729e | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 114 | |
Sarah Chin | e04784a | 2022-10-31 20:32:34 -0700 | [diff] [blame] | 115 | protected synchronized void replaceInstance(final Class c, final String instanceName, |
| 116 | final Object obj, final Object newValue) |
| 117 | throws Exception { |
| 118 | Field field = c.getDeclaredField(instanceName); |
| 119 | field.setAccessible(true); |
| 120 | |
| 121 | InstanceKey key = new InstanceKey(c, instanceName, obj); |
| 122 | if (!mOldInstances.containsKey(key)) { |
| 123 | mOldInstances.put(key, field.get(obj)); |
| 124 | mInstanceKeys.add(key); |
| 125 | } |
| 126 | field.set(obj, newValue); |
| 127 | } |
| 128 | |
| 129 | private synchronized void restoreInstances() throws Exception { |
| 130 | Iterator<InstanceKey> it = mInstanceKeys.descendingIterator(); |
| 131 | |
| 132 | while (it.hasNext()) { |
| 133 | InstanceKey key = it.next(); |
| 134 | Field field = key.mClass.getDeclaredField(key.mInstName); |
| 135 | field.setAccessible(true); |
| 136 | field.set(key.mObj, mOldInstances.get(key)); |
| 137 | } |
| 138 | |
| 139 | mInstanceKeys.clear(); |
| 140 | mOldInstances.clear(); |
| 141 | } |
| 142 | |
| 143 | private static class InstanceKey { |
| 144 | public final Class mClass; |
| 145 | public final String mInstName; |
| 146 | public final Object mObj; |
| 147 | InstanceKey(final Class c, final String instName, final Object obj) { |
| 148 | mClass = c; |
| 149 | mInstName = instName; |
| 150 | mObj = obj; |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public int hashCode() { |
| 155 | return (mClass.getName().hashCode() * 31 + mInstName.hashCode()) * 31; |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public boolean equals(Object obj) { |
| 160 | if (obj == null || obj.getClass() != getClass()) { |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | InstanceKey other = (InstanceKey) obj; |
| 165 | return (other.mClass == mClass && other.mInstName.equals(mInstName) |
| 166 | && other.mObj == mObj); |
| 167 | } |
| 168 | } |
| 169 | |
Brad Ebinger | 792729e | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 170 | protected TestContext getTestContext() { |
| 171 | return mContext; |
| 172 | } |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 173 | } |