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

Commit 97e5b65a authored by Xiang Wang's avatar Xiang Wang
Browse files

Fix the filter for getTemperatureThresholds

Add null check and more unit tests for each case

Bug: b/268508448
Test: atest ThermalManagerServiceMockingTest
Change-Id: I28a41964c5ca5e4439c9f51bfe8b44a116f601f8
parent 881b1f60
Loading
Loading
Loading
Loading
+14 −3
Original line number Diff line number Diff line
@@ -741,6 +741,9 @@ public class ThermalManagerService extends SystemService {
                    final android.hardware.thermal.Temperature[] halRet =
                            shouldFilter ? mInstance.getTemperaturesWithType(type)
                                    : mInstance.getTemperatures();
                    if (halRet == null) {
                        return ret;
                    }
                    for (android.hardware.thermal.Temperature t : halRet) {
                        if (!Temperature.isValidStatus(t.throttlingStatus)) {
                            Slog.e(TAG, "Invalid temperature status " + t.throttlingStatus
@@ -774,6 +777,9 @@ public class ThermalManagerService extends SystemService {
                    final android.hardware.thermal.CoolingDevice[] halRet = shouldFilter
                            ? mInstance.getCoolingDevicesWithType(type)
                            : mInstance.getCoolingDevices();
                    if (halRet == null) {
                        return ret;
                    }
                    for (android.hardware.thermal.CoolingDevice t : halRet) {
                        if (!CoolingDevice.isValidType(t.type)) {
                            Slog.e(TAG, "Invalid cooling device type " + t.type + " from AIDL HAL");
@@ -806,9 +812,14 @@ public class ThermalManagerService extends SystemService {
                    final TemperatureThreshold[] halRet =
                            shouldFilter ? mInstance.getTemperatureThresholdsWithType(type)
                                    : mInstance.getTemperatureThresholds();

                    if (halRet == null) {
                        return ret;
                    }
                    if (shouldFilter) {
                        return Arrays.stream(halRet).filter(t -> t.type == type).collect(
                                Collectors.toList());
                    }
                    return Arrays.asList(halRet);
                } catch (IllegalArgumentException | IllegalStateException e) {
                    Slog.e(TAG, "Couldn't getTemperatureThresholds due to invalid status", e);
                } catch (RemoteException e) {
+156 −6
Original line number Diff line number Diff line
@@ -119,6 +119,34 @@ public class ThermalManagerServiceMockingTest {
                + expectedRet, expectedRet.containsAll(ret));
    }

    @Test
    public void getCurrentTemperatures_aidl() throws RemoteException {
        android.hardware.thermal.Temperature halT1 = new android.hardware.thermal.Temperature();
        halT1.type = TemperatureType.MODEM;
        halT1.name = "test1";
        halT1.throttlingStatus = ThrottlingSeverity.EMERGENCY;
        halT1.value = 99.0f;
        android.hardware.thermal.Temperature halT2 = new android.hardware.thermal.Temperature();
        halT2.name = "test2";
        halT2.type = TemperatureType.SOC;
        halT2.throttlingStatus = ThrottlingSeverity.NONE;

        Mockito.when(mAidlHalMock.getTemperatures()).thenReturn(
                new android.hardware.thermal.Temperature[]{
                        halT2, halT1
                });
        List<Temperature> ret = mAidlWrapper.getCurrentTemperatures(false, TemperatureType.UNKNOWN);
        Mockito.verify(mAidlHalMock, Mockito.times(1)).getTemperatures();

        Temperature expectedT1 = new Temperature(halT1.value, halT1.type, halT1.name,
                halT1.throttlingStatus);
        Temperature expectedT2 = new Temperature(halT2.value, halT2.type, halT2.name,
                halT2.throttlingStatus);
        List<Temperature> expectedRet = List.of(expectedT1, expectedT2);
        assertTrue("Got temperature list as " + ret + " with different values compared to "
                + expectedRet, expectedRet.containsAll(ret));
    }

    @Test
    public void getCurrentTemperatures_withFilter_aidl() throws RemoteException {
        android.hardware.thermal.Temperature halT1 = new android.hardware.thermal.Temperature();
@@ -153,6 +181,23 @@ public class ThermalManagerServiceMockingTest {
                + expectedRet, expectedRet.containsAll(ret));
    }

    @Test
    public void getCurrentTemperatures_nullResult_aidl() throws RemoteException {
        Mockito.when(mAidlHalMock.getTemperaturesWithType(Mockito.anyInt())).thenReturn(null);
        List<Temperature> ret = mAidlWrapper.getCurrentTemperatures(true,
                Temperature.TYPE_SOC);
        Mockito.verify(mAidlHalMock, Mockito.times(1)).getTemperaturesWithType(
                Temperature.TYPE_SOC);
        assertNotNull(ret);
        assertEquals(0, ret.size());

        Mockito.when(mAidlHalMock.getTemperatures()).thenReturn(null);
        ret = mAidlWrapper.getCurrentTemperatures(false, Temperature.TYPE_SOC);
        Mockito.verify(mAidlHalMock, Mockito.times(1)).getTemperatures();
        assertNotNull(ret);
        assertEquals(0, ret.size());
    }

    @Test
    public void getCurrentTemperatures_invalidStatus_aidl() throws RemoteException {
        android.hardware.thermal.Temperature halTInvalid =
@@ -209,6 +254,55 @@ public class ThermalManagerServiceMockingTest {
        assertEquals(0, ret.size());
    }

    @Test
    public void getCurrentCoolingDevices_aidl() throws RemoteException {
        android.hardware.thermal.CoolingDevice halC1 = new android.hardware.thermal.CoolingDevice();
        halC1.type = CoolingType.SPEAKER;
        halC1.name = "test1";
        halC1.value = 10;
        android.hardware.thermal.CoolingDevice halC2 = new android.hardware.thermal.CoolingDevice();
        halC2.type = CoolingType.MODEM;
        halC2.name = "test2";
        halC2.value = 110;

        Mockito.when(mAidlHalMock.getCoolingDevicesWithType(Mockito.anyInt())).thenReturn(
                new android.hardware.thermal.CoolingDevice[]{
                        halC1, halC2
                }
        );
        Mockito.when(mAidlHalMock.getCoolingDevices()).thenReturn(
                new android.hardware.thermal.CoolingDevice[]{
                        halC1, halC2
                }
        );
        List<CoolingDevice> ret = mAidlWrapper.getCurrentCoolingDevices(false,
                CoolingType.COMPONENT);
        Mockito.verify(mAidlHalMock, Mockito.times(1)).getCoolingDevices();

        CoolingDevice expectedC1 = new CoolingDevice(halC1.value, halC1.type, halC1.name);
        CoolingDevice expectedC2 = new CoolingDevice(halC2.value, halC2.type, halC2.name);
        List<CoolingDevice> expectedRet = List.of(expectedC1, expectedC2);
        assertTrue("Got cooling device list as " + ret + " with different values compared to "
                + expectedRet, expectedRet.containsAll(ret));
    }

    @Test
    public void getCurrentCoolingDevices_nullResult_aidl() throws RemoteException {
        Mockito.when(mAidlHalMock.getCoolingDevicesWithType(Mockito.anyInt())).thenReturn(null);
        List<CoolingDevice> ret = mAidlWrapper.getCurrentCoolingDevices(true,
                CoolingType.COMPONENT);
        Mockito.verify(mAidlHalMock, Mockito.times(1)).getCoolingDevicesWithType(
                CoolingType.COMPONENT);
        assertNotNull(ret);
        assertEquals(0, ret.size());

        Mockito.when(mAidlHalMock.getCoolingDevices()).thenReturn(null);
        ret = mAidlWrapper.getCurrentCoolingDevices(false, CoolingType.COMPONENT);
        Mockito.verify(mAidlHalMock, Mockito.times(1)).getCoolingDevices();
        assertNotNull(ret);
        assertEquals(0, ret.size());
    }

    @Test
    public void getCurrentCoolingDevices_withFilter_aidl() throws RemoteException {
        android.hardware.thermal.CoolingDevice halC1 = new android.hardware.thermal.CoolingDevice();
@@ -291,6 +385,44 @@ public class ThermalManagerServiceMockingTest {
        assertEquals(0, ret.size());
    }

    @Test
    public void getTemperatureThresholds_aidl() throws RemoteException {
        TemperatureThreshold halT1 = new TemperatureThreshold();
        halT1.name = "test1";
        halT1.type = Temperature.TYPE_SKIN;
        halT1.hotThrottlingThresholds = new float[]{1, 2, 3};
        halT1.coldThrottlingThresholds = new float[]{};

        TemperatureThreshold halT2 = new TemperatureThreshold();
        halT2.name = "test2";
        halT2.type = Temperature.TYPE_SOC;
        halT2.hotThrottlingThresholds = new float[]{};
        halT2.coldThrottlingThresholds = new float[]{3, 2, 1};

        Mockito.when(mAidlHalMock.getTemperatureThresholds()).thenReturn(
                new TemperatureThreshold[]{halT1, halT2}
        );
        List<TemperatureThreshold> ret = mAidlWrapper.getTemperatureThresholds(false,
                Temperature.TYPE_UNKNOWN);
        Mockito.verify(mAidlHalMock, Mockito.times(1)).getTemperatureThresholds();

        assertEquals("Got unexpected temperature thresholds size", 2, ret.size());
        TemperatureThreshold threshold = ret.get(0).type == Temperature.TYPE_SKIN ? ret.get(0)
                : ret.get(1);

        assertEquals(halT1.name, threshold.name);
        assertEquals(halT1.type, threshold.type);
        assertArrayEquals(halT1.hotThrottlingThresholds, threshold.hotThrottlingThresholds, 0.1f);
        assertArrayEquals(halT1.coldThrottlingThresholds, threshold.coldThrottlingThresholds, 0.1f);

        threshold = ret.get(0).type == Temperature.TYPE_SOC ? ret.get(0) : ret.get(1);

        assertEquals(halT2.name, threshold.name);
        assertEquals(halT2.type, threshold.type);
        assertArrayEquals(halT2.hotThrottlingThresholds, threshold.hotThrottlingThresholds, 0.1f);
        assertArrayEquals(halT2.coldThrottlingThresholds, threshold.coldThrottlingThresholds, 0.1f);
    }

    @Test
    public void getTemperatureThresholds_withFilter_aidl() throws RemoteException {
        TemperatureThreshold halT1 = new TemperatureThreshold();
@@ -300,18 +432,18 @@ public class ThermalManagerServiceMockingTest {
        halT1.coldThrottlingThresholds = new float[]{};

        TemperatureThreshold halT2 = new TemperatureThreshold();
        halT1.name = "test2";
        halT1.type = Temperature.TYPE_SOC;
        halT1.hotThrottlingThresholds = new float[]{};
        halT1.coldThrottlingThresholds = new float[]{3, 2, 1};
        halT2.name = "test2";
        halT2.type = Temperature.TYPE_SOC;
        halT2.hotThrottlingThresholds = new float[]{};
        halT2.coldThrottlingThresholds = new float[]{3, 2, 1};

        Mockito.when(mAidlHalMock.getTemperatureThresholdsWithType(Mockito.anyInt())).thenReturn(
                new TemperatureThreshold[]{halT1, halT2}
        );
        List<TemperatureThreshold> ret = mAidlWrapper.getTemperatureThresholds(true,
                Temperature.TYPE_SOC);
                Temperature.TYPE_SKIN);
        Mockito.verify(mAidlHalMock, Mockito.times(1)).getTemperatureThresholdsWithType(
                Temperature.TYPE_SOC);
                Temperature.TYPE_SKIN);

        assertEquals("Got unexpected temperature thresholds size", 1, ret.size());
        TemperatureThreshold threshold = ret.get(0);
@@ -321,6 +453,24 @@ public class ThermalManagerServiceMockingTest {
        assertArrayEquals(halT1.coldThrottlingThresholds, threshold.coldThrottlingThresholds, 0.1f);
    }

    @Test
    public void getTemperatureThresholds_nullResult_aidl() throws RemoteException {
        Mockito.when(mAidlHalMock.getTemperatureThresholdsWithType(Mockito.anyInt())).thenReturn(
                null);
        List<TemperatureThreshold> ret = mAidlWrapper.getTemperatureThresholds(true,
                Temperature.TYPE_SOC);
        Mockito.verify(mAidlHalMock, Mockito.times(1)).getTemperatureThresholdsWithType(
                Temperature.TYPE_SOC);
        assertNotNull(ret);
        assertEquals(0, ret.size());

        Mockito.when(mAidlHalMock.getTemperatureThresholds()).thenReturn(null);
        ret = mAidlWrapper.getTemperatureThresholds(false, Temperature.TYPE_SOC);
        Mockito.verify(mAidlHalMock, Mockito.times(1)).getTemperatureThresholds();
        assertNotNull(ret);
        assertEquals(0, ret.size());
    }

    @Test
    public void getTemperatureThresholds_illegalArgument_aidl() throws RemoteException {
        Mockito.when(mAidlHalMock.getTemperatureThresholdsWithType(Mockito.anyInt())).thenThrow(