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

Commit b20534d9 authored by Brad Ebinger's avatar Brad Ebinger
Browse files

Fix leak due to PhoneConfigurationManager

1) Ensure the registrants in PhoneConfigurationManager are cleared after tests
to prevent memory leaks.
2) Convert CarrierAppUtilsTest to JUnit4 so it respects the @Ignore tag properly.

Bug: 155320231
Test: atest FrameworksTelephonyTests TeleServiceTest
Change-Id: I4cf7dca0b82c743a8132925dce0d3edd63659353
parent d84d3e0c
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -397,6 +397,13 @@ public class PhoneConfigurationManager {
        sMultiSimConfigChangeRegistrants.remove(h);
    }

    /**
     * Unregister for all multi-SIM configuration change events.
     */
    public static void unregisterAllMultiSimConfigChangeRegistrants() {
        sMultiSimConfigChangeRegistrants.removeAll();
    }

    private void broadcastMultiSimConfigChange(int numOfActiveModems) {
        log("broadcastSimSlotNumChange numOfActiveModems" + numOfActiveModems);
        // Notify internal registrants first.
+9 −0
Original line number Diff line number Diff line
@@ -606,6 +606,15 @@ public class ImsResolver implements ImsServiceController.ImsServiceControllerCal
        bindCarrierServicesIfAvailable();
    }

    /**
     * Destroys this ImsResolver. Used for tearing down static resources during testing.
     */
    @VisibleForTesting
    public void destroy() {
        PhoneConfigurationManager.unregisterForMultiSimConfigChange(mHandler);
        mHandler.removeCallbacksAndMessages(null);
    }

    // Only start the bind if there is an existing Carrier Configuration. Otherwise, wait for
    // carrier config changed.
    private void bindCarrierServicesIfAvailable() {
+9 −5
Original line number Diff line number Diff line
@@ -23,15 +23,19 @@ import android.os.Bundle;
import android.os.UserHandle;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.test.InstrumentationTestCase;
import android.test.mock.MockContentProvider;
import android.test.mock.MockContentResolver;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.ArrayMap;
import android.util.ArraySet;

import androidx.test.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
@@ -39,7 +43,8 @@ import org.mockito.MockitoAnnotations;
import java.util.ArrayList;
import java.util.List;

public class CarrierAppUtilsTest extends InstrumentationTestCase {
@RunWith(AndroidJUnit4.class)
public class CarrierAppUtilsTest {
    private static final String CARRIER_APP = "com.example.carrier";
    private static final ArraySet<String> CARRIER_APPS = new ArraySet<>();
    static {
@@ -62,11 +67,10 @@ public class CarrierAppUtilsTest extends InstrumentationTestCase {
    private SettingsMockContentProvider mContentProvider;
    private MockContentResolver mContentResolver;

    @Override
    @Before
    public void setUp() throws Exception {
        super.setUp();
        System.setProperty("dexmaker.dexcache",
                getInstrumentation().getTargetContext().getCacheDir().getPath());
                InstrumentationRegistry.getTargetContext().getCacheDir().getPath());
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        MockitoAnnotations.initMocks(this);

+2 −0
Original line number Diff line number Diff line
@@ -689,6 +689,8 @@ public abstract class TelephonyTest {
    }

    protected void tearDown() throws Exception {
        // Ensure there are no references to handlers between tests.
        PhoneConfigurationManager.unregisterAllMultiSimConfigChangeRegistrants();
        // unmonitor TestableLooper for TelephonyTest class
        if (mTestableLooper != null) {
            unmonitorTestableLooper(mTestableLooper);
+1 −0
Original line number Diff line number Diff line
@@ -127,6 +127,7 @@ public class ImsResolverTest extends ImsTestBase {
    @After
    @Override
    public void tearDown() throws Exception {
        mTestImsResolver.destroy();
        mTestImsResolver = null;
        mLooper.destroy();
        mLooper = null;