Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f152b32a authored by Amit Mahajan's avatar Amit Mahajan
Browse files

Fix failing tests by cleaning up properly in tearDown.

Bug: 25691379
Change-Id: I829278ee371e9903e73891cee2a0986686d72ee5
parent 70db77ec
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -92,6 +92,7 @@ public class CallManagerTest extends TelephonyTest{
    @After
    public void tearDown() throws Exception {
        CallManager.getInstance().unregisterPhone(mPhone);
        super.tearDown();
    }

    @SmallTest @Test
+6 −3
Original line number Diff line number Diff line
@@ -47,8 +47,9 @@ public class GsmCdmaPhoneTest extends TelephonyTest {

        @Override
        public void onLooperPrepared() {
            mPhoneUT = new GsmCdmaPhone(mContextFixture.getTestDouble(), mSimulatedCommands,
                    mNotifier, true, 0, PhoneConstants.PHONE_TYPE_GSM, mTelephonyComponentFactory);
            mPhoneUT = new GsmCdmaPhone(mContext, mSimulatedCommands, mNotifier, true, 0,
                    PhoneConstants.PHONE_TYPE_GSM, mTelephonyComponentFactory);
            TelephonyTestUtils.waitForMs(50);
            setReady(true);
        }
    }
@@ -81,7 +82,9 @@ public class GsmCdmaPhoneTest extends TelephonyTest {

    @After
    public void tearDown() throws Exception {
        mPhoneUT.removeCallbacksAndMessages(null);
        mPhoneUT = null;
        super.tearDown();
    }

    @Test @SmallTest
@@ -98,7 +101,7 @@ public class GsmCdmaPhoneTest extends TelephonyTest {
        mSimulatedCommands.setVoiceRadioTech(ServiceState.RIL_RADIO_TECHNOLOGY_1xRTT);
        assertTrue(mPhoneUT.isPhoneTypeGsm());
        Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
        mContextFixture.getTestDouble().sendBroadcast(intent);
        mContext.sendBroadcast(intent);
        TelephonyTestUtils.waitForMs(50);
        assertTrue(mPhoneUT.isPhoneTypeCdmaLte());
    }
+5 −59
Original line number Diff line number Diff line
@@ -66,27 +66,15 @@ import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;

public class ServiceStateTrackerTest {
    private static final String TAG = "ServiceStateTrackerTest";

public class ServiceStateTrackerTest extends TelephonyTest {
    private static final String AMERICA_LA_TIME_ZONE = "America/Los_Angeles";
    private static final String KEY_TIME_ZONE = "time-zone";

    @Mock
    private GsmCdmaPhone mPhone;
    @Mock
    private UiccController mUiccController;
    @Mock
    private UiccCardApplication m3GPPUiccApp;
    @Mock
    private SIMRecords mSimRecords;
    @Mock
    private SubscriptionController mSubscriptionController;
    @Mock
    private SparseArray<TelephonyEventLog> mLogInstances;
    @Mock
    private TelephonyEventLog mTelephonyEventLog;
    @Mock
    private DcTracker mDct;
    @Mock
    private ProxyController mProxyController;
@@ -95,14 +83,9 @@ public class ServiceStateTrackerTest {
    @Mock
    IBinder mBinder;

    private SimulatedCommands mSimulatedCommands;
    private ContextFixture mContextFixture;
    private ServiceStateTracker sst;
    private TelephonyManager mTelephonyManager;

    private Object mLock = new Object();
    private boolean mReady = false;

    private class ServiceStateTrackerTestHandler extends HandlerThread {

        private ServiceStateTrackerTestHandler(String name) {
@@ -112,19 +95,7 @@ public class ServiceStateTrackerTest {
        @Override
        public void onLooperPrepared() {
            sst = new ServiceStateTracker(mPhone, mSimulatedCommands);
            synchronized (mLock) {
                mReady = true;
            }
        }
    }

    private void waitUntilReady() {
        while(true) {
            synchronized (mLock) {
                if (mReady) {
                    break;
                }
            }
            setReady(true);
        }
    }

@@ -132,12 +103,8 @@ public class ServiceStateTrackerTest {
    public void setUp() throws Exception {

        logd("ServiceStateTrackerTest +Setup!");
        MockitoAnnotations.initMocks(this);
        mContextFixture = new ContextFixture();
        mSimulatedCommands = new SimulatedCommands();
        super.setUp("ServiceStateTrackerTest");

        doReturn(mContextFixture.getTestDouble()).when(mPhone).getContext();
        doReturn(true).when(mPhone).getUnitTestMode();
        doReturn(true).when(mPhone).isPhoneTypeGsm();
        doReturn(true).when(mDct).isDisconnected();
        doReturn(m3GPPUiccApp).when(mUiccController).getUiccCardApplication(
@@ -148,29 +115,12 @@ public class ServiceStateTrackerTest {
                getSystemService(Context.TELEPHONY_SERVICE);

        //Use reflection to mock singleton
        Field field = SubscriptionController.class.getDeclaredField("sInstance");
        field.setAccessible(true);
        field.set(null, mSubscriptionController);

        //Use reflection to mock singleton
        field = UiccController.class.getDeclaredField("mInstance");
        field.setAccessible(true);
        field.set(null, mUiccController);

        // Replace ServiceManager.sCache so ServiceManager.getService can return a mocked
        // IBinder.
        field = ServiceManager.class.getDeclaredField("sCache");
        Field field = ServiceManager.class.getDeclaredField("sCache");
        field.setAccessible(true);
        field.set(null, mServiceCache);

        doReturn(mTelephonyEventLog).when(mLogInstances).get(anyInt());
        doReturn(mBinder).when(mServiceCache).get(anyString());

        // Use reflection to replace TelephonyEventLog.sInstances with our mocked mLogInstances
        field = TelephonyEventLog.class.getDeclaredField("sInstances");
        field.setAccessible(true);
        field.set(null, mLogInstances);

        field = ProxyController.class.getDeclaredField("sProxyController");
        field.setAccessible(true);
        field.set(null, mProxyController);
@@ -191,7 +141,6 @@ public class ServiceStateTrackerTest {
        int dds = SubscriptionManager.getDefaultDataSubscriptionId();
        doReturn(dds).when(mPhone).getSubId();

        mReady = false;
        new ServiceStateTrackerTestHandler(TAG).start();
        waitUntilReady();
        waitForMs(600);
@@ -201,6 +150,7 @@ public class ServiceStateTrackerTest {
    @After
    public void tearDown() throws Exception {
        sst = null;
        super.tearDown();
    }

    @Test
@@ -336,8 +286,4 @@ public class ServiceStateTrackerTest {
        assertEquals(2, cl.getLac());
        assertEquals(3, cl.getCid());
    }

    private static void logd(String s) {
        Log.d(TAG, s);
    }
}
+8 −1
Original line number Diff line number Diff line
@@ -22,6 +22,8 @@ import android.test.suitebuilder.annotation.SmallTest;
import java.io.UnsupportedEncodingException;
import static org.mockito.Mockito.doReturn;
import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

@@ -33,6 +35,11 @@ public class Sms7BitEncodingTranslatorTest extends TelephonyTest {
        logd("-Setup!");
    }

    @After
    public void tearDown() throws Exception {
        super.tearDown();
    }

    @Test
    @SmallTest
    public void testNoTranslate() {
@@ -69,7 +76,7 @@ public class Sms7BitEncodingTranslatorTest extends TelephonyTest {
    @SmallTest
    public void testCdmaTranslate() {

        TelephonyManager telephonyManager = TelephonyManager.from(mContextFixture.getTestDouble());
        TelephonyManager telephonyManager = TelephonyManager.from(mContext);
        doReturn(PhoneConstants.PHONE_TYPE_CDMA).when(telephonyManager).getCurrentPhoneType();

        String s = null;
+76 −25
Original line number Diff line number Diff line
@@ -17,22 +17,13 @@
package com.android.internal.telephony;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncResult;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IDeviceIdleController;
import android.os.Message;
import android.os.RegistrantList;
import android.provider.Settings;
import android.telephony.CarrierConfigManager;
import android.telephony.CellLocation;
import android.telephony.ServiceState;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.Log;
import android.util.SparseArray;

import com.android.ims.ImsManager;
import com.android.internal.telephony.cdma.CdmaSubscriptionSourceManager;
@@ -42,12 +33,8 @@ import com.android.internal.telephony.test.SimulatedCommandsVerifier;
import com.android.internal.telephony.uicc.IccCardProxy;
import com.android.internal.telephony.uicc.UiccController;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

@@ -58,7 +45,7 @@ public abstract class TelephonyTest {
    protected static String TAG;

    @Mock
    protected Phone mPhone;
    protected GsmCdmaPhone mPhone;
    @Mock
    protected ServiceStateTracker mSST;
    @Mock
@@ -97,11 +84,25 @@ public abstract class TelephonyTest {
    protected InboundSmsHandler mInboundSmsHandler;
    @Mock
    protected WspTypeDecoder mWspTypeDecoder;
    @Mock
    private SparseArray<TelephonyEventLog> mTelephonyEventLogInstances;
    @Mock
    private TelephonyEventLog mTelephonyEventLog;

    protected SimulatedCommands mSimulatedCommands;
    protected ContextFixture mContextFixture;
    protected Object mLock = new Object();
    protected boolean mReady;
    protected Context mContext;
    private Object mLock = new Object();
    private boolean mReady;

    private Object mOrigCallManager;
    private Object mOrigTelephonyComponentFactory;
    private Object mOrigUiccController;
    private Object mOrigCdmaSSM;
    private Object mOrigImsManagerInstances;
    private Object mOrigSubscriptionController;
    private Object mOrigTelephonyEventLogInstances;
    private Object mOrigTelephonyManager;

    protected void waitUntilReady() {
        while(true) {
@@ -122,31 +123,42 @@ public abstract class TelephonyTest {
    protected void setUp(String tag) throws Exception {
        TAG = tag;
        MockitoAnnotations.initMocks(this);
        //Use reflection to mock singleton
        //Use reflection to mock singletons
        Field field = CallManager.class.getDeclaredField("INSTANCE");
        field.setAccessible(true);
        mOrigCallManager = field.get(null);
        field.set(null, mCallManager);

        //Use reflection to mock singleton
        field = TelephonyComponentFactory.class.getDeclaredField("sInstance");
        field.setAccessible(true);
        mOrigTelephonyComponentFactory = field.get(null);
        field.set(null, mTelephonyComponentFactory);

        field = UiccController.class.getDeclaredField("mInstance");
        field.setAccessible(true);
        mOrigUiccController = field.get(null);
        field.set(null, mUiccController);

        //Use reflection to mock singleton
        field = CdmaSubscriptionSourceManager.class.getDeclaredField("sInstance");
        field.setAccessible(true);
        mOrigCdmaSSM = field.get(null);
        field.set(null, mCdmaSSM);

        //Use reflection to mock singleton
        field = ImsManager.class.getDeclaredField("sImsManagerInstances");
        field.setAccessible(true);
        mOrigImsManagerInstances = field.get(null);
        field.set(null, mImsManagerInstances);

        //Use reflection to mock singleton
        field = SubscriptionController.class.getDeclaredField("sInstance");
        field.setAccessible(true);
        mOrigSubscriptionController = field.get(null);
        field.set(null, mSubscriptionController);

        field = TelephonyEventLog.class.getDeclaredField("sInstances");
        field.setAccessible(true);
        mOrigTelephonyEventLogInstances = field.get(null);
        field.set(null, mTelephonyEventLogInstances);

        field = CdmaSubscriptionSourceManager.class.getDeclaredField(
                "mCdmaSubscriptionSourceChangedRegistrants");
        field.setAccessible(true);
@@ -158,12 +170,13 @@ public abstract class TelephonyTest {

        mSimulatedCommands = new SimulatedCommands();
        mContextFixture = new ContextFixture();
        mContext = mContextFixture.getTestDouble();
        mPhone.mCi = mSimulatedCommands;

        field = TelephonyManager.class.getDeclaredField("sInstance");
        field.setAccessible(true);
        field.set(null, mContextFixture.getTestDouble().
                getSystemService(Context.TELEPHONY_SERVICE));
        mOrigTelephonyManager = field.get(null);
        field.set(null, mContext.getSystemService(Context.TELEPHONY_SERVICE));

        doReturn(mSST).when(mTelephonyComponentFactory).
                makeServiceStateTracker(any(GsmCdmaPhone.class), any(CommandsInterface.class));
@@ -180,7 +193,8 @@ public abstract class TelephonyTest {
        doReturn(mWspTypeDecoder).when(mTelephonyComponentFactory).
                makeWspTypeDecoder(any(byte[].class));

        doReturn(mContextFixture.getTestDouble()).when(mPhone).getContext();
        doReturn(mContext).when(mPhone).getContext();
        doReturn(true).when(mPhone).getUnitTestMode();
        doReturn(mCdmaSSM).when(mTelephonyComponentFactory).
                getCdmaSubscriptionSourceManagerInstance(any(Context.class),
                        any(CommandsInterface.class), any(Handler.class),
@@ -190,9 +204,46 @@ public abstract class TelephonyTest {

        doReturn(mPhone).when(mInboundSmsHandler).getPhone();

        doReturn(mTelephonyEventLog).when(mTelephonyEventLogInstances).get(anyInt());

        setReady(false);
    }

    protected void tearDown() throws Exception {
        //Reset fields that were set using reflection
        Field field = CallManager.class.getDeclaredField("INSTANCE");
        field.setAccessible(true);
        field.set(null, mOrigCallManager);

        field = TelephonyComponentFactory.class.getDeclaredField("sInstance");
        field.setAccessible(true);
        field.set(null, mOrigTelephonyComponentFactory);

        field = UiccController.class.getDeclaredField("mInstance");
        field.setAccessible(true);
        field.set(null, mOrigUiccController);

        field = CdmaSubscriptionSourceManager.class.getDeclaredField("sInstance");
        field.setAccessible(true);
        field.set(null, mOrigCdmaSSM);

        field = ImsManager.class.getDeclaredField("sImsManagerInstances");
        field.setAccessible(true);
        field.set(null, mOrigImsManagerInstances);

        field = SubscriptionController.class.getDeclaredField("sInstance");
        field.setAccessible(true);
        field.set(null, mOrigSubscriptionController);

        field = TelephonyEventLog.class.getDeclaredField("sInstances");
        field.setAccessible(true);
        field.set(null, mOrigTelephonyEventLogInstances);

        field = TelephonyManager.class.getDeclaredField("sInstance");
        field.setAccessible(true);
        field.set(null, mOrigTelephonyManager);
    }

    protected static void logd(String s) {
        Log.d(TAG, s);
    }
Loading