blob: d72d85efbfbe5bdd17af87b3006bed31a68c7a28 [file] [log] [blame]
Brad Ebinger12ec7f22016-05-05 10:40:57 -07001/*
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
17package com.android;
18
Brad Ebingera68a4972020-01-30 17:31:23 -080019import static org.mockito.Mockito.spy;
20
Brad Ebinger12ec7f22016-05-05 10:40:57 -070021import android.os.Handler;
Brad Ebinger32925b82016-12-02 13:01:49 -080022import android.os.Looper;
Sanket Padawed9a427e2017-06-08 12:03:07 -070023import android.util.Log;
Brad Ebinger12ec7f22016-05-05 10:40:57 -070024
Brad Ebingera15005b2020-04-29 15:20:38 -070025import com.android.internal.telephony.PhoneConfigurationManager;
26
Sarah Chine04784a2022-10-31 20:32:34 -070027import org.junit.After;
28import org.junit.Before;
tom hsu0b59d292022-09-29 23:49:21 +080029import org.junit.Rule;
30import org.mockito.junit.MockitoJUnit;
31import org.mockito.junit.MockitoRule;
Brad Ebinger12ec7f22016-05-05 10:40:57 -070032
Sarah Chine04784a2022-10-31 20:32:34 -070033import java.lang.reflect.Field;
34import java.util.HashMap;
35import java.util.Iterator;
36import java.util.LinkedList;
Brad Ebinger12ec7f22016-05-05 10:40:57 -070037import java.util.concurrent.CountDownLatch;
Brad Ebinger63b6f5a2020-10-27 11:43:35 -070038import java.util.concurrent.Executor;
Brad Ebinger12ec7f22016-05-05 10:40:57 -070039import java.util.concurrent.TimeUnit;
40
41/**
42 * Helper class to load Mockito Resources into a test.
43 */
44public class TelephonyTestBase {
tom hsu0b59d292022-09-29 23:49:21 +080045 @Rule public final MockitoRule mocks = MockitoJUnit.rule();
Brad Ebinger12ec7f22016-05-05 10:40:57 -070046
Brad Ebinger792729e2019-12-09 16:12:57 -080047 protected TestContext mContext;
Brad Ebinger12ec7f22016-05-05 10:40:57 -070048
Sarah Chine04784a2022-10-31 20:32:34 -070049 private final HashMap<InstanceKey, Object> mOldInstances = new HashMap<>();
50 private final LinkedList<InstanceKey> mInstanceKeys = new LinkedList<>();
51
52 @Before
Brad Ebinger12ec7f22016-05-05 10:40:57 -070053 public void setUp() throws Exception {
Brad Ebingera68a4972020-01-30 17:31:23 -080054 mContext = spy(new TestContext());
Brad Ebinger32925b82016-12-02 13:01:49 -080055 // Set up the looper if it does not exist on the test thread.
56 if (Looper.myLooper() == null) {
57 Looper.prepare();
Brad Ebinger5a803552017-08-17 16:47:25 -070058 // Wait until the looper is not null anymore
59 for(int i = 0; i < 5; i++) {
60 if (Looper.myLooper() != null) {
61 break;
62 }
63 Looper.prepare();
64 Thread.sleep(100);
65 }
Brad Ebinger32925b82016-12-02 13:01:49 -080066 }
Brad Ebinger12ec7f22016-05-05 10:40:57 -070067 }
68
Sarah Chine04784a2022-10-31 20:32:34 -070069 @After
Brad Ebinger12ec7f22016-05-05 10:40:57 -070070 public void tearDown() throws Exception {
Brad Ebingera15005b2020-04-29 15:20:38 -070071 // Ensure there are no static references to handlers after test completes.
72 PhoneConfigurationManager.unregisterAllMultiSimConfigChangeRegistrants();
Sarah Chine04784a2022-10-31 20:32:34 -070073 restoreInstances();
Brad Ebinger12ec7f22016-05-05 10:40:57 -070074 }
75
Brad Ebinger63b6f5a2020-10-27 11:43:35 -070076 protected final boolean waitForExecutorAction(Executor executor, long timeoutMillis) {
77 final CountDownLatch lock = new CountDownLatch(1);
Brad Ebinger63b6f5a2020-10-27 11:43:35 -070078 executor.execute(() -> {
Brad Ebinger63b6f5a2020-10-27 11:43:35 -070079 lock.countDown();
80 });
81 while (lock.getCount() > 0) {
82 try {
83 return lock.await(timeoutMillis, TimeUnit.MILLISECONDS);
84 } catch (InterruptedException e) {
85 // do nothing
86 }
87 }
88 return true;
89 }
90
Brad Ebinger12ec7f22016-05-05 10:40:57 -070091 protected final void waitForHandlerAction(Handler h, long timeoutMillis) {
92 final CountDownLatch lock = new CountDownLatch(1);
93 h.post(lock::countDown);
94 while (lock.getCount() > 0) {
95 try {
96 lock.await(timeoutMillis, TimeUnit.MILLISECONDS);
97 } catch (InterruptedException e) {
98 // do nothing
99 }
100 }
101 }
102
103 protected final void waitForHandlerActionDelayed(Handler h, long timeoutMillis, long delayMs) {
104 final CountDownLatch lock = new CountDownLatch(1);
105 h.postDelayed(lock::countDown, delayMs);
106 while (lock.getCount() > 0) {
107 try {
108 lock.await(timeoutMillis, TimeUnit.MILLISECONDS);
109 } catch (InterruptedException e) {
110 // do nothing
111 }
112 }
113 }
Sanket Padawed9a427e2017-06-08 12:03:07 -0700114
115 protected void waitForMs(long ms) {
116 try {
117 Thread.sleep(ms);
118 } catch (InterruptedException e) {
119 Log.e("TelephonyTestBase", "InterruptedException while waiting: " + e);
120 }
121 }
Brad Ebinger792729e2019-12-09 16:12:57 -0800122
Sarah Chine04784a2022-10-31 20:32:34 -0700123 protected synchronized void replaceInstance(final Class c, final String instanceName,
124 final Object obj, final Object newValue)
125 throws Exception {
126 Field field = c.getDeclaredField(instanceName);
127 field.setAccessible(true);
128
129 InstanceKey key = new InstanceKey(c, instanceName, obj);
130 if (!mOldInstances.containsKey(key)) {
131 mOldInstances.put(key, field.get(obj));
132 mInstanceKeys.add(key);
133 }
134 field.set(obj, newValue);
135 }
136
137 private synchronized void restoreInstances() throws Exception {
138 Iterator<InstanceKey> it = mInstanceKeys.descendingIterator();
139
140 while (it.hasNext()) {
141 InstanceKey key = it.next();
142 Field field = key.mClass.getDeclaredField(key.mInstName);
143 field.setAccessible(true);
144 field.set(key.mObj, mOldInstances.get(key));
145 }
146
147 mInstanceKeys.clear();
148 mOldInstances.clear();
149 }
150
151 private static class InstanceKey {
152 public final Class mClass;
153 public final String mInstName;
154 public final Object mObj;
155 InstanceKey(final Class c, final String instName, final Object obj) {
156 mClass = c;
157 mInstName = instName;
158 mObj = obj;
159 }
160
161 @Override
162 public int hashCode() {
163 return (mClass.getName().hashCode() * 31 + mInstName.hashCode()) * 31;
164 }
165
166 @Override
167 public boolean equals(Object obj) {
168 if (obj == null || obj.getClass() != getClass()) {
169 return false;
170 }
171
172 InstanceKey other = (InstanceKey) obj;
173 return (other.mClass == mClass && other.mInstName.equals(mInstName)
174 && other.mObj == mObj);
175 }
176 }
177
Brad Ebinger792729e2019-12-09 16:12:57 -0800178 protected TestContext getTestContext() {
179 return mContext;
180 }
Brad Ebinger12ec7f22016-05-05 10:40:57 -0700181}