Fix flaky TeleServices tests causing module errors

Some tests were using reflection to replace static instances but not
resetting the previous values, causing the device to remain in a bad
state. Add a replaceInstance and restoreInstances method to
TelephonyTestBase and have these classes extend it so cleanup can be
done in the @After method.
Fix flaky NotificationMgrTests causing module errors due to incomplete
mocks.
Also revert changes added to help debug b/256244889.

Test: atest TeleServiceTests
Bug: 256244889
Change-Id: Id758e207da862f450d5c90971cb3ea1bd2afe40d
diff --git a/src/com/android/phone/PhoneGlobals.java b/src/com/android/phone/PhoneGlobals.java
index f18535c..abbd816 100644
--- a/src/com/android/phone/PhoneGlobals.java
+++ b/src/com/android/phone/PhoneGlobals.java
@@ -81,7 +81,6 @@
 import java.io.PrintWriter;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
-import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -889,10 +888,8 @@
 
         boolean dataAllowed;
         boolean notAllowedDueToRoamingOff;
-        List<DataDisallowedReason> reasons = new ArrayList<>();
-        if (phone.getDataNetworkController() != null) {
-            reasons = phone.getDataNetworkController().getInternetDataDisallowedReasons();
-        }
+        List<DataDisallowedReason> reasons = phone.getDataNetworkController()
+                .getInternetDataDisallowedReasons();
         dataAllowed = reasons.isEmpty();
         notAllowedDueToRoamingOff = (reasons.size() == 1
                 && reasons.contains(DataDisallowedReason.ROAMING_DISABLED));
@@ -938,11 +935,7 @@
      * @return whether we have transitioned to dataRoaming
      */
     private boolean dataIsNowRoaming(int subId) {
-        if (getPhone(subId).getServiceState() == null) {
-            return false;
-        } else {
-            return getPhone(subId).getServiceState().getDataRoaming();
-        }
+        return getPhone(subId).getServiceState().getDataRoaming();
     }
 
     private void updateLimitedSimFunctionForDualSim() {
diff --git a/src/com/android/phone/PhoneInterfaceManager.java b/src/com/android/phone/PhoneInterfaceManager.java
index 3f05f0a..7c176ef 100755
--- a/src/com/android/phone/PhoneInterfaceManager.java
+++ b/src/com/android/phone/PhoneInterfaceManager.java
@@ -6804,12 +6804,7 @@
             int phoneId = mSubscriptionController.getPhoneId(subId);
             Phone phone = PhoneFactory.getPhone(phoneId);
             if (phone != null) {
-                boolean retVal;
-                if (phone.getDataSettingsManager() == null) {
-                    retVal = false;
-                } else {
-                    retVal = phone.getDataSettingsManager().isDataEnabled();
-                }
+                boolean retVal = phone.getDataSettingsManager().isDataEnabled();
                 if (DBG) log("isDataEnabled: " + retVal + ", subId=" + subId);
                 return retVal;
             } else {
diff --git a/tests/src/com/android/TelephonyTestBase.java b/tests/src/com/android/TelephonyTestBase.java
index ffda81b..d72d85e 100644
--- a/tests/src/com/android/TelephonyTestBase.java
+++ b/tests/src/com/android/TelephonyTestBase.java
@@ -24,10 +24,16 @@
 
 import com.android.internal.telephony.PhoneConfigurationManager;
 
+import org.junit.After;
+import org.junit.Before;
 import org.junit.Rule;
 import org.mockito.junit.MockitoJUnit;
 import org.mockito.junit.MockitoRule;
 
+import java.lang.reflect.Field;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedList;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.Executor;
 import java.util.concurrent.TimeUnit;
@@ -40,6 +46,10 @@
 
     protected TestContext mContext;
 
+    private final HashMap<InstanceKey, Object> mOldInstances = new HashMap<>();
+    private final LinkedList<InstanceKey> mInstanceKeys = new LinkedList<>();
+
+    @Before
     public void setUp() throws Exception {
         mContext = spy(new TestContext());
         // Set up the looper if it does not exist on the test thread.
@@ -56,9 +66,11 @@
         }
     }
 
+    @After
     public void tearDown() throws Exception {
         // Ensure there are no static references to handlers after test completes.
         PhoneConfigurationManager.unregisterAllMultiSimConfigChangeRegistrants();
+        restoreInstances();
     }
 
     protected final boolean waitForExecutorAction(Executor executor, long timeoutMillis) {
@@ -108,6 +120,61 @@
         }
     }
 
+    protected synchronized void replaceInstance(final Class c, final String instanceName,
+            final Object obj, final Object newValue)
+            throws Exception {
+        Field field = c.getDeclaredField(instanceName);
+        field.setAccessible(true);
+
+        InstanceKey key = new InstanceKey(c, instanceName, obj);
+        if (!mOldInstances.containsKey(key)) {
+            mOldInstances.put(key, field.get(obj));
+            mInstanceKeys.add(key);
+        }
+        field.set(obj, newValue);
+    }
+
+    private synchronized void restoreInstances() throws Exception {
+        Iterator<InstanceKey> it = mInstanceKeys.descendingIterator();
+
+        while (it.hasNext()) {
+            InstanceKey key = it.next();
+            Field field = key.mClass.getDeclaredField(key.mInstName);
+            field.setAccessible(true);
+            field.set(key.mObj, mOldInstances.get(key));
+        }
+
+        mInstanceKeys.clear();
+        mOldInstances.clear();
+    }
+
+    private static class InstanceKey {
+        public final Class mClass;
+        public final String mInstName;
+        public final Object mObj;
+        InstanceKey(final Class c, final String instName, final Object obj) {
+            mClass = c;
+            mInstName = instName;
+            mObj = obj;
+        }
+
+        @Override
+        public int hashCode() {
+            return (mClass.getName().hashCode() * 31 + mInstName.hashCode()) * 31;
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (obj == null || obj.getClass() != getClass()) {
+                return false;
+            }
+
+            InstanceKey other = (InstanceKey) obj;
+            return (other.mClass == mClass && other.mInstName.equals(mInstName)
+                    && other.mObj == mObj);
+        }
+    }
+
     protected TestContext getTestContext() {
         return mContext;
     }
diff --git a/tests/src/com/android/phone/ImsStateCallbackControllerTest.java b/tests/src/com/android/phone/ImsStateCallbackControllerTest.java
index 60374bc..2bd87be 100644
--- a/tests/src/com/android/phone/ImsStateCallbackControllerTest.java
+++ b/tests/src/com/android/phone/ImsStateCallbackControllerTest.java
@@ -52,6 +52,7 @@
 import android.testing.TestableLooper;
 import android.util.Log;
 
+import com.android.TelephonyTestBase;
 import com.android.ims.FeatureConnector;
 import com.android.ims.ImsManager;
 import com.android.ims.RcsFeatureManager;
@@ -70,13 +71,12 @@
 import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
 
-import java.lang.reflect.Field;
 import java.util.concurrent.Executor;
 
 /**
  * Unit tests for RcsProvisioningMonitor
  */
-public class ImsStateCallbackControllerTest {
+public class ImsStateCallbackControllerTest extends TelephonyTestBase {
     private static final String TAG = "ImsStateCallbackControllerTest";
     private static final int FAKE_SUB_ID_BASE = 0x0FFFFFF0;
 
@@ -189,6 +189,7 @@
             mLooper.destroy();
             mLooper = null;
         }
+        super.tearDown();
     }
 
     @Test
@@ -952,13 +953,6 @@
         }
     }
 
-    private static void replaceInstance(final Class c,
-            final String instanceName, final Object obj, final Object newValue) throws Exception {
-        Field field = c.getDeclaredField(instanceName);
-        field.setAccessible(true);
-        field.set(obj, newValue);
-    }
-
     private void makeFakeActiveSubIds(int count) {
         final int[] subIds = new int[count];
         for (int i = 0; i < count; i++) {
diff --git a/tests/src/com/android/phone/NotificationMgrTest.java b/tests/src/com/android/phone/NotificationMgrTest.java
index a6ee276..bd5ca1d 100644
--- a/tests/src/com/android/phone/NotificationMgrTest.java
+++ b/tests/src/com/android/phone/NotificationMgrTest.java
@@ -69,9 +69,12 @@
 import android.testing.AndroidTestingRunner;
 import android.testing.TestableLooper;
 
+import com.android.TelephonyTestBase;
 import com.android.internal.telephony.Phone;
 import com.android.internal.telephony.PhoneConstants;
 import com.android.internal.telephony.PhoneFactory;
+import com.android.internal.telephony.data.DataNetworkController;
+import com.android.internal.telephony.data.DataSettingsManager;
 import com.android.internal.telephony.util.NotificationChannelController;
 
 import org.junit.Before;
@@ -81,15 +84,14 @@
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 
-import java.lang.reflect.Field;
+import java.util.Collections;
 
 /**
  * Unit Test for NotificationMgr
  */
 @RunWith(AndroidTestingRunner.class)
 @TestableLooper.RunWithLooper
-public class NotificationMgrTest {
-
+public class NotificationMgrTest extends TelephonyTestBase {
     private static final int TEST_SUB_ID = 1;
     private static final long SERIAL_NUMBER_OF_USER = 1234567L;
     private static final String TEST_LABEL_CF = "test_call_forwarding";
@@ -111,8 +113,11 @@
     @Mock NotificationManager mNotificationManager;
     @Mock SubscriptionInfo mSubscriptionInfo;
     @Mock Resources mResources;
+    @Mock Context mMockedContext;
     @Mock ServiceState mServiceState;
     @Mock CarrierConfigManager mCarrierConfigManager;
+    @Mock DataNetworkController mDataNetworkController;
+    @Mock DataSettingsManager mDataSettingsManager;
 
     private Phone[] mPhones;
     private NotificationMgr mNotificationMgr;
@@ -123,6 +128,14 @@
         mPhones = new Phone[]{mPhone};
         replaceInstance(PhoneFactory.class, "sPhones", null, mPhones);
         when(mPhone.getPhoneType()).thenReturn(PhoneConstants.PHONE_TYPE_GSM);
+        when(mPhone.getContext()).thenReturn(mMockedContext);
+        when(mMockedContext.getResources()).thenReturn(mResources);
+        when(mPhone.getServiceState()).thenReturn(mServiceState);
+        when(mPhone.getDataNetworkController()).thenReturn(mDataNetworkController);
+        when(mDataNetworkController.getInternetDataDisallowedReasons()).thenReturn(
+                Collections.emptyList());
+        when(mPhone.getDataSettingsManager()).thenReturn(mDataSettingsManager);
+        when(mDataSettingsManager.isDataEnabledForReason(anyInt())).thenReturn(true);
         when(mApp.getSharedPreferences(anyString(), anyInt())).thenReturn(mSharedPreferences);
 
         when(mApp.getPackageName()).thenReturn(TEST_PACKAGE_NAME);
@@ -157,8 +170,7 @@
     }
 
     @Test
-    public void testUpdateCfi_visible_noActiveSubscription_notificationNeverSent()
-            throws Exception {
+    public void testUpdateCfi_visible_noActiveSubscription_notificationNeverSent() {
         // Given no active subscription available
         when(mSubscriptionManager.getActiveSubscriptionInfo(eq(TEST_SUB_ID))).thenReturn(null);
 
@@ -170,7 +182,7 @@
     }
 
     @Test
-    public void testUpdateCfi_visible_hasActiveSub_singleSIM_notificationSent() throws Exception {
+    public void testUpdateCfi_visible_hasActiveSub_singleSIM_notificationSent() {
         when(mTelephonyManager.getPhoneCount()).thenReturn(1);
         when(mSubscriptionManager.getActiveSubscriptionInfo(eq(TEST_SUB_ID))).thenReturn(
                 mSubscriptionInfo);
@@ -181,8 +193,7 @@
     }
 
     @Test
-    public void testUpdateCfi_visible_hasActiveSub_multiSIM_notificationSentWithoutDisplayName()
-            throws Exception {
+    public void testUpdateCfi_visible_hasActiveSub_multiSIM_notificationSentWithoutDisplayName() {
         when(mTelephonyManager.getPhoneCount()).thenReturn(2);
         when(mSubscriptionManager.getActiveSubscriptionInfo(eq(TEST_SUB_ID))).thenReturn(
                 mSubscriptionInfo);
@@ -194,8 +205,7 @@
     }
 
     @Test
-    public void testUpdateCfi_visible_hasActiveSub_multiSIM_notificationSentWithDisplayName()
-            throws Exception {
+    public void testUpdateCfi_visible_hasActiveSub_multiSIM_notificationSentWithDisplayName() {
         when(mTelephonyManager.getPhoneCount()).thenReturn(2);
         when(mSubscriptionManager.getActiveSubscriptionInfo(eq(TEST_SUB_ID))).thenReturn(
                 mSubscriptionInfo);
@@ -207,8 +217,7 @@
     }
 
     @Test
-    public void testUpdateCfi_invisible_hasUnmanagedProfile_notificationCanceled()
-            throws Exception {
+    public void testUpdateCfi_invisible_hasUnmanagedProfile_notificationCanceled() {
         when(mUserManager.isManagedProfile(anyInt())).thenReturn(false);
 
         mNotificationMgr.updateCfi(TEST_SUB_ID, /*visible=*/false, /*isFresh=*/false);
@@ -217,8 +226,7 @@
     }
 
     @Test
-    public void testUpdateCfi_invisible_allProfilesAreManaged_notificationNeverCanceled()
-            throws Exception {
+    public void testUpdateCfi_invisible_allProfilesAreManaged_notificationNeverCanceled() {
         when(mUserManager.isManagedProfile(anyInt())).thenReturn(true);
 
         mNotificationMgr.updateCfi(TEST_SUB_ID, /*visible=*/false, /*isFresh=*/false);
@@ -227,7 +235,7 @@
     }
 
     @Test
-    public void testShowDataRoamingNotification_roamingOn() throws Exception {
+    public void testShowDataRoamingNotification_roamingOn() {
         mNotificationMgr.showDataRoamingNotification(TEST_SUB_ID, /*roamingOn=*/true);
 
         verifyNotificationSentWithChannelId(
@@ -235,7 +243,7 @@
     }
 
     @Test
-    public void testShowDataRoamingNotification_roamingOff() throws Exception {
+    public void testShowDataRoamingNotification_roamingOff() {
         mNotificationMgr.showDataRoamingNotification(TEST_SUB_ID, /*roamingOn=*/false);
 
         verifyNotificationSentWithChannelId(
@@ -250,8 +258,7 @@
     }
 
     @Test
-    public void testUpdateNetworkSelection_justOutOfService_notificationNeverSent()
-            throws Exception {
+    public void testUpdateNetworkSelection_justOutOfService_notificationNeverSent() {
         prepareResourcesForNetworkSelection();
 
         mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID);
@@ -265,8 +272,7 @@
     }
 
     @Test
-    public void testUpdateNetworkSelection_oosEnoughTime_selectionVisibleToUser_notificationSent()
-            throws Exception {
+    public void testUpdateNetworkSelection_oosEnoughTime_selectionVisibleToUser_notificationSent() {
         prepareResourcesForNetworkSelection();
         when(mTelephonyManager.isManualNetworkSelectionAllowed()).thenReturn(true);
         PersistableBundle config = new PersistableBundle();
@@ -288,8 +294,7 @@
     }
 
     @Test
-    public void testUpdateNetworkSelection_invalidSubscription_notificationNotSent()
-            throws Exception {
+    public void testUpdateNetworkSelection_invalidSubscription_notificationNotSent() {
         prepareResourcesForNetworkSelection();
         when(mTelephonyManager.isManualNetworkSelectionAllowed()).thenReturn(true);
         PersistableBundle config = new PersistableBundle();
@@ -312,8 +317,7 @@
     }
 
     @Test
-    public void testUpdateNetworkSelection_nullCarrierConfig_notificationNotSent()
-            throws Exception {
+    public void testUpdateNetworkSelection_nullCarrierConfig_notificationNotSent() {
         prepareResourcesForNetworkSelection();
 
         when(mCarrierConfigManager.getConfigForSubId(TEST_SUB_ID)).thenReturn(null);
@@ -329,8 +333,7 @@
     }
 
     @Test
-    public void testUpdateNetworkSelection_userNotAllowedToChooseOperator_notificationNotSent()
-            throws Exception {
+    public void testUpdateNetworkSelection_userNotAllowedToChooseOperator_notificationNotSent() {
         prepareResourcesForNetworkSelection();
 
         PersistableBundle config = new PersistableBundle();
@@ -353,8 +356,8 @@
     }
 
     @Test
-    public void testUpdateNetworkSelection_OverrideHideCarrierNetworkSelection_notificationNotSent()
-            throws Exception {
+    public void
+            testUpdateNetworkSelection_OverrideHideCarrierNetworkSelection_notificationNotSent() {
         prepareResourcesForNetworkSelection();
 
         PersistableBundle config = new PersistableBundle();
@@ -401,8 +404,7 @@
     }
 
     @Test
-    public void testUpdateNetworkSelection_worldMode_userSetLTE_notificationNotSent()
-            throws Exception {
+    public void testUpdateNetworkSelection_worldMode_userSetLTE_notificationNotSent() {
         prepareResourcesForNetworkSelection();
 
         PersistableBundle config = new PersistableBundle();
@@ -431,8 +433,7 @@
     }
 
     @Test
-    public void testUpdateNetworkSelection_worldMode_userSetTDSCDMA_notSupported_notifNotSent()
-            throws Exception {
+    public void testUpdateNetworkSelection_worldMode_userSetTDSCDMA_notSupported_notifNotSent() {
         prepareResourcesForNetworkSelection();
 
         PersistableBundle config = new PersistableBundle();
@@ -462,8 +463,7 @@
     }
 
     @Test
-    public void testUpdateNetworkSelection_worldMode_userSetWCDMA_notificationSent()
-            throws Exception {
+    public void testUpdateNetworkSelection_worldMode_userSetWCDMA_notificationSent() {
         prepareResourcesForNetworkSelection();
 
         PersistableBundle config = new PersistableBundle();
@@ -494,8 +494,7 @@
     }
 
     @Test
-    public void testUpdateNetworkSelection_worldPhone_networkSelectionNotHide_notificationSent()
-            throws Exception {
+    public void testUpdateNetworkSelection_worldPhone_networkSelectionNotHide_notificationSent() {
         prepareResourcesForNetworkSelection();
 
         PersistableBundle config = new PersistableBundle();
@@ -519,8 +518,7 @@
     }
 
     @Test
-    public void testUpdateNetworkSelection_gsmBasicOptionOn_notificationSent()
-            throws Exception {
+    public void testUpdateNetworkSelection_gsmBasicOptionOn_notificationSent() {
         prepareResourcesForNetworkSelection();
 
         PersistableBundle config = new PersistableBundle();
@@ -545,8 +543,7 @@
     }
 
     @Test
-    public void testUpdateNetworkSelection_gsmBasicOptionOff_notificationNotSent()
-            throws Exception {
+    public void testUpdateNetworkSelection_gsmBasicOptionOff_notificationNotSent() {
         prepareResourcesForNetworkSelection();
 
         PersistableBundle config = new PersistableBundle();
@@ -569,8 +566,7 @@
     }
 
     @Test
-    public void testShowLimitedSimFunctionWarningNotification_forTheFirstTime_notificationSent()
-            throws Exception {
+    public void testShowLimitedSimFunctionWarningNotification_forTheFirstTime_notificationSent() {
         when(mResources.getText(R.string.limited_sim_function_notification_message)).thenReturn(
                 CARRIER_NAME);
         when(mResources.getText(
@@ -584,8 +580,8 @@
     }
 
     @Test
-    public void testShowLimitedSimFunctionWarningNotification_consecutiveCall_notificationSentOnce()
-            throws Exception {
+    public void
+            testShowLimitedSimFunctionWarningNotification_consecutiveCall_notificationSentOnce() {
         when(mResources.getText(R.string.limited_sim_function_notification_message)).thenReturn(
                 CARRIER_NAME);
         when(mResources.getText(
@@ -602,8 +598,7 @@
     }
 
     @Test
-    public void testDismissLimitedSimFunctionWarningNotification_noShowCalledBefore_noCancelSent()
-            throws Exception {
+    public void testDismissLimitedSimFunctionWarningNotification_noShowCalledBefore_noCancelSent() {
         // showLimitedSimFunctionWarningNotification was never called before
 
         mNotificationMgr.dismissLimitedSimFunctionWarningNotification(TEST_SUB_ID);
@@ -612,8 +607,7 @@
     }
 
     @Test
-    public void testDismissLimitedSimFunctionWarningNotification_showCalledBefore_cancelSent()
-            throws Exception {
+    public void testDismissLimitedSimFunctionWarningNotification_showCalledBefore_cancelSent() {
         when(mResources.getText(R.string.limited_sim_function_notification_message)).thenReturn(
                 CARRIER_NAME);
         when(mResources.getText(
@@ -651,11 +645,4 @@
         when(mApp.getString(R.string.mobile_network_settings_class)).thenReturn(
                 MOBILE_NETWORK_SELECTION_CLASS);
     }
-
-    private static void replaceInstance(final Class c,
-            final String instanceName, final Object obj, final Object newValue) throws Exception {
-        Field field = c.getDeclaredField(instanceName);
-        field.setAccessible(true);
-        field.set(obj, newValue);
-    }
 }
diff --git a/tests/src/com/android/phone/PhoneUtilsTest.java b/tests/src/com/android/phone/PhoneUtilsTest.java
index eb4c248..521a0bb 100644
--- a/tests/src/com/android/phone/PhoneUtilsTest.java
+++ b/tests/src/com/android/phone/PhoneUtilsTest.java
@@ -28,23 +28,19 @@
 
 import androidx.test.runner.AndroidJUnit4;
 
+import com.android.TelephonyTestBase;
 import com.android.internal.telephony.GsmCdmaPhone;
 import com.android.internal.telephony.Phone;
 import com.android.internal.telephony.PhoneFactory;
 
-import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 
-import java.lang.reflect.Field;
-import java.util.ArrayList;
-import java.util.HashMap;
-
 @RunWith(AndroidJUnit4.class)
-public class PhoneUtilsTest {
+public class PhoneUtilsTest extends TelephonyTestBase {
     @Mock
     private SubscriptionManager mMockSubscriptionManager;
     @Mock
@@ -59,37 +55,6 @@
     private PhoneAccountHandle mPhoneAccountHandleTest = new PhoneAccountHandle(
             PSTN_CONNECTION_SERVICE_COMPONENT, mPhoneAccountHandleIdString);
 
-    private HashMap<InstanceKey, Object> mOldInstances = new HashMap<InstanceKey, Object>();
-
-    private ArrayList<InstanceKey> mInstanceKeys = new ArrayList<InstanceKey>();
-
-    private static class InstanceKey {
-        public final Class mClass;
-        public final String mInstName;
-        public final Object mObj;
-        InstanceKey(final Class c, final String instName, final Object obj) {
-            mClass = c;
-            mInstName = instName;
-            mObj = obj;
-        }
-
-        @Override
-        public int hashCode() {
-            return (mClass.getName().hashCode() * 31 + mInstName.hashCode()) * 31;
-        }
-
-        @Override
-        public boolean equals(Object obj) {
-            if (obj == null || !(obj instanceof InstanceKey)) {
-                return false;
-            }
-
-            InstanceKey other = (InstanceKey) obj;
-            return (other.mClass == mClass && other.mInstName.equals(mInstName)
-                    && other.mObj == mObj);
-        }
-    }
-
     @Before
     public void setUp() throws Exception {
         MockitoAnnotations.initMocks(this);
@@ -100,37 +65,6 @@
         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,
-            final Object obj, final Object newValue)
-            throws Exception {
-        Field field = c.getDeclaredField(instanceName);
-        field.setAccessible(true);
-
-        InstanceKey key = new InstanceKey(c, instanceName, obj);
-        if (!mOldInstances.containsKey(key)) {
-            mOldInstances.put(key, field.get(obj));
-            mInstanceKeys.add(key);
-        }
-        field.set(obj, newValue);
-    }
-
-    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
     public void testIsPhoneAccountActive() throws Exception {
         assertTrue(PhoneUtils.isPhoneAccountActive(
diff --git a/tests/src/com/android/services/telephony/DisconnectCauseUtilTest.java b/tests/src/com/android/services/telephony/DisconnectCauseUtilTest.java
index 28a7b02..969622a 100644
--- a/tests/src/com/android/services/telephony/DisconnectCauseUtilTest.java
+++ b/tests/src/com/android/services/telephony/DisconnectCauseUtilTest.java
@@ -38,19 +38,14 @@
 import com.android.internal.telephony.PhoneFactory;
 import com.android.phone.common.R;
 
-import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 
-import java.lang.reflect.Field;
-import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.Locale;
 
-
 @RunWith(AndroidJUnit4.class)
 public class DisconnectCauseUtilTest extends TelephonyTestBase {
 
@@ -60,42 +55,11 @@
 
     // dynamic
     private Context mContext;
-    private HashMap<InstanceKey, Object> mOldInstances = new HashMap<InstanceKey, Object>();
-    private ArrayList<InstanceKey> mInstanceKeys = new ArrayList<InstanceKey>();
 
     //Mocks
     @Mock
     private GsmCdmaPhone mMockPhone;
 
-    // inner classes
-    private static class InstanceKey {
-        public final Class mClass;
-        public final String mInstName;
-        public final Object mObj;
-
-        InstanceKey(final Class c, final String instName, final Object obj) {
-            mClass = c;
-            mInstName = instName;
-            mObj = obj;
-        }
-
-        @Override
-        public int hashCode() {
-            return (mClass.getName().hashCode() * 31 + mInstName.hashCode()) * 31;
-        }
-
-        @Override
-        public boolean equals(Object obj) {
-            if (obj == null || !(obj instanceof InstanceKey)) {
-                return false;
-            }
-
-            InstanceKey other = (InstanceKey) obj;
-            return (other.mClass == mClass && other.mInstName.equals(mInstName)
-                    && other.mObj == mObj);
-        }
-    }
-
     @Before
     public void setUp() throws Exception {
         super.setUp();
@@ -106,15 +70,6 @@
         setSinglePhone();
     }
 
-    @After
-    public void tearDown() throws Exception {
-        // restoreInstance.
-        // Not doing so will potentially "confuse" other tests with the mocked instance
-        restoreInstance(PhoneFactory.class, "sPhones", null);
-        super.tearDown();
-    }
-
-
     /**
      * Verifies that a call drop due to loss of WIFI results in a disconnect cause of error and that
      * the label, description and tone are all present.
@@ -176,33 +131,6 @@
         replaceInstance(PhoneFactory.class, "sPhones", null, mPhones);
     }
 
-
-    protected synchronized void replaceInstance(final Class c, final String instanceName,
-            final Object obj, final Object newValue)
-            throws Exception {
-        Field field = c.getDeclaredField(instanceName);
-        field.setAccessible(true);
-
-        InstanceKey key = new InstanceKey(c, instanceName, obj);
-        if (!mOldInstances.containsKey(key)) {
-            mOldInstances.put(key, field.get(obj));
-            mInstanceKeys.add(key);
-        }
-        field.set(obj, newValue);
-    }
-
-    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);
-        }
-    }
-
     private Resources getResourcesForLocale(Context context, Locale locale) {
         Configuration config = new Configuration();
         config.setToDefaults();