Merge "Restore static instances after test completes" into tm-dev am: 5b5318a6ce
Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/services/Telephony/+/18629776
Change-Id: I0f294a1bb6d0a26b361dd568648f695dfeb17861
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/tests/src/com/android/phone/PhoneUtilsTest.java b/tests/src/com/android/phone/PhoneUtilsTest.java
index df3a0ac..eb4c248 100644
--- a/tests/src/com/android/phone/PhoneUtilsTest.java
+++ b/tests/src/com/android/phone/PhoneUtilsTest.java
@@ -96,11 +96,13 @@
when(mMockSubscriptionManager.getActiveSubscriptionInfo(
eq(mPhoneAccountHandleIdInteger))).thenReturn(mMockSubscriptionInfo);
when(mMockPhone.getSubId()).thenReturn(mPhoneAccountHandleIdInteger);
- setSinglePhone();
+ Phone[] mPhones = new Phone[] {mMockPhone};
+ replaceInstance(PhoneFactory.class, "sPhones", null, mPhones);
}
@After
public void tearDown() throws Exception {
+ restoreInstance(PhoneFactory.class, "sPhones", null);
}
protected synchronized void replaceInstance(final Class c, final String instanceName,
@@ -117,9 +119,16 @@
field.set(obj, newValue);
}
- private void setSinglePhone() throws Exception {
- Phone[] mPhones = new Phone[] {mMockPhone};
- replaceInstance(PhoneFactory.class, "sPhones", null, mPhones);
+ protected synchronized void restoreInstance(final Class c, final String instanceName,
+ final Object obj) throws Exception {
+ InstanceKey key = new InstanceKey(c, instanceName, obj);
+ if (mOldInstances.containsKey(key)) {
+ Field field = c.getDeclaredField(instanceName);
+ field.setAccessible(true);
+ field.set(obj, mOldInstances.get(key));
+ mOldInstances.remove(key);
+ mInstanceKeys.remove(key);
+ }
}
@Test