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

Commit a709ab9f authored by Rupesh Bansal's avatar Rupesh Bansal Committed by Android (Google) Code Review
Browse files

Merge "Fix brightness not being updated when RBC is toggled" into tm-dev

parents 721a2e8f 9475a86e
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -1057,6 +1057,7 @@ class AutomaticBrightnessController {


    public void recalculateSplines(boolean applyAdjustment, float[] adjustment) {
    public void recalculateSplines(boolean applyAdjustment, float[] adjustment) {
        mCurrentBrightnessMapper.recalculateSplines(applyAdjustment, adjustment);
        mCurrentBrightnessMapper.recalculateSplines(applyAdjustment, adjustment);
        updateAutoBrightness(true /*sendUpdate*/, false /*isManuallySet*/);
    }
    }


    private final class AutomaticBrightnessHandler extends Handler {
    private final class AutomaticBrightnessHandler extends Handler {
+59 −50
Original line number Original line Diff line number Diff line
@@ -30,6 +30,7 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.when;


import android.content.Context;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.hardware.Sensor;
import android.hardware.Sensor;
import android.hardware.SensorEventListener;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.hardware.SensorManager;
@@ -73,6 +74,7 @@ public class AutomaticBrightnessControllerTest {
    private TestLooper mTestLooper;
    private TestLooper mTestLooper;
    private Context mContext;
    private Context mContext;
    private AutomaticBrightnessController mController;
    private AutomaticBrightnessController mController;
    private Sensor mLightSensor;


    @Mock SensorManager mSensorManager;
    @Mock SensorManager mSensorManager;
    @Mock BrightnessMappingStrategy mBrightnessMappingStrategy;
    @Mock BrightnessMappingStrategy mBrightnessMappingStrategy;
@@ -84,12 +86,14 @@ public class AutomaticBrightnessControllerTest {
    @Mock BrightnessThrottler mBrightnessThrottler;
    @Mock BrightnessThrottler mBrightnessThrottler;


    @Before
    @Before
    public void setUp() {
    public void setUp() throws Exception {
        // Share classloader to allow package private access.
        // Share classloader to allow package private access.
        System.setProperty("dexmaker.share_classloader", "true");
        System.setProperty("dexmaker.share_classloader", "true");
        MockitoAnnotations.initMocks(this);
        MockitoAnnotations.initMocks(this);


        mLightSensor = TestUtils.createSensor(Sensor.TYPE_LIGHT, "Light Sensor");
        mContext = InstrumentationRegistry.getContext();
        mContext = InstrumentationRegistry.getContext();
        mController = setupController(mLightSensor);
    }
    }


    @After
    @After
@@ -101,11 +105,6 @@ public class AutomaticBrightnessControllerTest {
        }
        }
    }
    }


    private void advanceTime(long timeMs) {
        mClock.fastForward(timeMs);
        mTestLooper.dispatchAll();
    }

    private AutomaticBrightnessController setupController(Sensor lightSensor) {
    private AutomaticBrightnessController setupController(Sensor lightSensor) {
        mClock = new OffsettableClock.Stopped();
        mClock = new OffsettableClock.Stopped();
        mTestLooper = new TestLooper(mClock::now);
        mTestLooper = new TestLooper(mClock::now);
@@ -150,12 +149,9 @@ public class AutomaticBrightnessControllerTest {


    @Test
    @Test
    public void testNoHysteresisAtMinBrightness() throws Exception {
    public void testNoHysteresisAtMinBrightness() throws Exception {
        Sensor lightSensor = TestUtils.createSensor(Sensor.TYPE_LIGHT, "Light Sensor");
        mController = setupController(lightSensor);

        ArgumentCaptor<SensorEventListener> listenerCaptor =
        ArgumentCaptor<SensorEventListener> listenerCaptor =
                ArgumentCaptor.forClass(SensorEventListener.class);
                ArgumentCaptor.forClass(SensorEventListener.class);
        verify(mSensorManager).registerListener(listenerCaptor.capture(), eq(lightSensor),
        verify(mSensorManager).registerListener(listenerCaptor.capture(), eq(mLightSensor),
                eq(INITIAL_LIGHT_SENSOR_RATE * 1000), any(Handler.class));
                eq(INITIAL_LIGHT_SENSOR_RATE * 1000), any(Handler.class));
        SensorEventListener listener = listenerCaptor.getValue();
        SensorEventListener listener = listenerCaptor.getValue();


@@ -177,7 +173,7 @@ public class AutomaticBrightnessControllerTest {
                .thenReturn(1.0f);
                .thenReturn(1.0f);


        // Send new sensor value and verify
        // Send new sensor value and verify
        listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, (int) lux1));
        listener.onSensorChanged(TestUtils.createSensorEvent(mLightSensor, (int) lux1));
        assertEquals(normalizedBrightness1, mController.getAutomaticScreenBrightness(), 0.001f);
        assertEquals(normalizedBrightness1, mController.getAutomaticScreenBrightness(), 0.001f);


        // Set up system to return 0.0f (minimum possible brightness) as a brightness value
        // Set up system to return 0.0f (minimum possible brightness) as a brightness value
@@ -191,18 +187,15 @@ public class AutomaticBrightnessControllerTest {
                .thenReturn(normalizedBrightness2);
                .thenReturn(normalizedBrightness2);


        // Send new sensor value and verify
        // Send new sensor value and verify
        listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, (int) lux2));
        listener.onSensorChanged(TestUtils.createSensorEvent(mLightSensor, (int) lux2));
        assertEquals(normalizedBrightness2, mController.getAutomaticScreenBrightness(), 0.001f);
        assertEquals(normalizedBrightness2, mController.getAutomaticScreenBrightness(), 0.001f);
    }
    }


    @Test
    @Test
    public void testNoHysteresisAtMaxBrightness() throws Exception {
    public void testNoHysteresisAtMaxBrightness() throws Exception {
        Sensor lightSensor = TestUtils.createSensor(Sensor.TYPE_LIGHT, "Light Sensor");
        mController = setupController(lightSensor);

        ArgumentCaptor<SensorEventListener> listenerCaptor =
        ArgumentCaptor<SensorEventListener> listenerCaptor =
                ArgumentCaptor.forClass(SensorEventListener.class);
                ArgumentCaptor.forClass(SensorEventListener.class);
        verify(mSensorManager).registerListener(listenerCaptor.capture(), eq(lightSensor),
        verify(mSensorManager).registerListener(listenerCaptor.capture(), eq(mLightSensor),
                eq(INITIAL_LIGHT_SENSOR_RATE * 1000), any(Handler.class));
                eq(INITIAL_LIGHT_SENSOR_RATE * 1000), any(Handler.class));
        SensorEventListener listener = listenerCaptor.getValue();
        SensorEventListener listener = listenerCaptor.getValue();


@@ -223,7 +216,7 @@ public class AutomaticBrightnessControllerTest {
                .thenReturn(1.1f);
                .thenReturn(1.1f);


        // Send new sensor value and verify
        // Send new sensor value and verify
        listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, (int) lux1));
        listener.onSensorChanged(TestUtils.createSensorEvent(mLightSensor, (int) lux1));
        assertEquals(normalizedBrightness1, mController.getAutomaticScreenBrightness(), 0.001f);
        assertEquals(normalizedBrightness1, mController.getAutomaticScreenBrightness(), 0.001f);




@@ -238,23 +231,20 @@ public class AutomaticBrightnessControllerTest {
                .thenReturn(normalizedBrightness2);
                .thenReturn(normalizedBrightness2);


        // Send new sensor value and verify
        // Send new sensor value and verify
        listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, (int) lux2));
        listener.onSensorChanged(TestUtils.createSensorEvent(mLightSensor, (int) lux2));
        assertEquals(normalizedBrightness2, mController.getAutomaticScreenBrightness(), 0.001f);
        assertEquals(normalizedBrightness2, mController.getAutomaticScreenBrightness(), 0.001f);
    }
    }


    @Test
    @Test
    public void testUserAddUserDataPoint() throws Exception {
    public void testUserAddUserDataPoint() throws Exception {
        Sensor lightSensor = TestUtils.createSensor(Sensor.TYPE_LIGHT, "Light Sensor");
        mController = setupController(lightSensor);

        ArgumentCaptor<SensorEventListener> listenerCaptor =
        ArgumentCaptor<SensorEventListener> listenerCaptor =
                ArgumentCaptor.forClass(SensorEventListener.class);
                ArgumentCaptor.forClass(SensorEventListener.class);
        verify(mSensorManager).registerListener(listenerCaptor.capture(), eq(lightSensor),
        verify(mSensorManager).registerListener(listenerCaptor.capture(), eq(mLightSensor),
                eq(INITIAL_LIGHT_SENSOR_RATE * 1000), any(Handler.class));
                eq(INITIAL_LIGHT_SENSOR_RATE * 1000), any(Handler.class));
        SensorEventListener listener = listenerCaptor.getValue();
        SensorEventListener listener = listenerCaptor.getValue();


        // Sensor reads 1000 lux,
        // Sensor reads 1000 lux,
        listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, 1000));
        listener.onSensorChanged(TestUtils.createSensorEvent(mLightSensor, 1000));


        // User sets brightness to 100
        // User sets brightness to 100
        mController.configure(AUTO_BRIGHTNESS_ENABLED, null /* configuration */,
        mController.configure(AUTO_BRIGHTNESS_ENABLED, null /* configuration */,
@@ -266,18 +256,43 @@ public class AutomaticBrightnessControllerTest {
    }
    }


    @Test
    @Test
    public void testSwitchToIdleMappingStrategy() throws Exception {
    public void testRecalculateSplines() throws Exception {
        Sensor lightSensor = TestUtils.createSensor(Sensor.TYPE_LIGHT, "Light Sensor");
        // Enabling the light sensor, and setting the ambient lux to 1000
        mController = setupController(lightSensor);
        ArgumentCaptor<SensorEventListener> listenerCaptor =
                ArgumentCaptor.forClass(SensorEventListener.class);
        verify(mSensorManager).registerListener(listenerCaptor.capture(), eq(mLightSensor),
                eq(INITIAL_LIGHT_SENSOR_RATE * 1000), any(Handler.class));
        SensorEventListener listener = listenerCaptor.getValue();
        listener.onSensorChanged(TestUtils.createSensorEvent(mLightSensor, 1000));


        //Setting the brightnessFloat to 0.5f
        float currentBrightnessFloat = 0.5f;
        when(mBrightnessMappingStrategy.getBrightness(1000,
                null, ApplicationInfo.CATEGORY_UNDEFINED)).thenReturn(currentBrightnessFloat);
        mController.configure(AUTO_BRIGHTNESS_ENABLED, null /* configuration */,
                currentBrightnessFloat /* brightness */, false /* userChangedBrightness */,
                0 /* adjustment */, false /* userChanged */, DisplayPowerRequest.POLICY_BRIGHT);

        // Adjusting spline, and accordingly remapping the current 0.5f brightnessFloat to 0.3f
        float updatedBrightnessFloat = 0.3f;
        when(mBrightnessMappingStrategy.getBrightness(1000,
                null, ApplicationInfo.CATEGORY_UNDEFINED)).thenReturn(updatedBrightnessFloat);
        float[] adjustments = new float[]{0.2f, 0.5f};
        mController.recalculateSplines(true, adjustments);
        verify(mBrightnessMappingStrategy).recalculateSplines(true, adjustments);
        assertEquals(mController.getAutomaticScreenBrightness(), updatedBrightnessFloat, EPSILON);
    }

    @Test
    public void testSwitchToIdleMappingStrategy() throws Exception {
        ArgumentCaptor<SensorEventListener> listenerCaptor =
        ArgumentCaptor<SensorEventListener> listenerCaptor =
                ArgumentCaptor.forClass(SensorEventListener.class);
                ArgumentCaptor.forClass(SensorEventListener.class);
        verify(mSensorManager).registerListener(listenerCaptor.capture(), eq(lightSensor),
        verify(mSensorManager).registerListener(listenerCaptor.capture(), eq(mLightSensor),
                eq(INITIAL_LIGHT_SENSOR_RATE * 1000), any(Handler.class));
                eq(INITIAL_LIGHT_SENSOR_RATE * 1000), any(Handler.class));
        SensorEventListener listener = listenerCaptor.getValue();
        SensorEventListener listener = listenerCaptor.getValue();


        // Sensor reads 1000 lux,
        // Sensor reads 1000 lux,
        listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, 1000));
        listener.onSensorChanged(TestUtils.createSensorEvent(mLightSensor, 1000));


        // User sets brightness to 100
        // User sets brightness to 100
        mController.configure(AUTO_BRIGHTNESS_ENABLED, null /* configuration */,
        mController.configure(AUTO_BRIGHTNESS_ENABLED, null /* configuration */,
@@ -307,51 +322,48 @@ public class AutomaticBrightnessControllerTest {


    @Test
    @Test
    public void testAmbientLightHorizon() throws Exception {
    public void testAmbientLightHorizon() throws Exception {
        // create abc
        Sensor lightSensor = TestUtils.createSensor(Sensor.TYPE_LIGHT, "Light Sensor");
        mController = setupController(lightSensor);
        ArgumentCaptor<SensorEventListener> listenerCaptor =
        ArgumentCaptor<SensorEventListener> listenerCaptor =
                ArgumentCaptor.forClass(SensorEventListener.class);
                ArgumentCaptor.forClass(SensorEventListener.class);
        verify(mSensorManager).registerListener(listenerCaptor.capture(), eq(lightSensor),
        verify(mSensorManager).registerListener(listenerCaptor.capture(), eq(mLightSensor),
                eq(INITIAL_LIGHT_SENSOR_RATE * 1000), any(Handler.class));
                eq(INITIAL_LIGHT_SENSOR_RATE * 1000), any(Handler.class));
        SensorEventListener listener = listenerCaptor.getValue();
        SensorEventListener listener = listenerCaptor.getValue();


        long increment = 500;
        long increment = 500;
        // set autobrightness to low
        // set autobrightness to low
        // t = 0
        // t = 0
        listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, 0));
        listener.onSensorChanged(TestUtils.createSensorEvent(mLightSensor, 0));


        // t = 500
        // t = 500
        mClock.fastForward(increment);
        mClock.fastForward(increment);
        listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, 0));
        listener.onSensorChanged(TestUtils.createSensorEvent(mLightSensor, 0));


        // t = 1000
        // t = 1000
        mClock.fastForward(increment);
        mClock.fastForward(increment);
        listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, 0));
        listener.onSensorChanged(TestUtils.createSensorEvent(mLightSensor, 0));
        assertEquals(0.0f, mController.getAmbientLux(), EPSILON);
        assertEquals(0.0f, mController.getAmbientLux(), EPSILON);


        // t = 1500
        // t = 1500
        mClock.fastForward(increment);
        mClock.fastForward(increment);
        listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, 0));
        listener.onSensorChanged(TestUtils.createSensorEvent(mLightSensor, 0));
        assertEquals(0.0f, mController.getAmbientLux(), EPSILON);
        assertEquals(0.0f, mController.getAmbientLux(), EPSILON);


        // t = 2000
        // t = 2000
        // ensure that our reading is at 0.
        // ensure that our reading is at 0.
        mClock.fastForward(increment);
        mClock.fastForward(increment);
        listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, 0));
        listener.onSensorChanged(TestUtils.createSensorEvent(mLightSensor, 0));
        assertEquals(0.0f, mController.getAmbientLux(), EPSILON);
        assertEquals(0.0f, mController.getAmbientLux(), EPSILON);


        // t = 2500
        // t = 2500
        // first 10000 lux sensor event reading
        // first 10000 lux sensor event reading
        mClock.fastForward(increment);
        mClock.fastForward(increment);
        listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, 10000));
        listener.onSensorChanged(TestUtils.createSensorEvent(mLightSensor, 10000));
        assertTrue(mController.getAmbientLux() > 0.0f);
        assertTrue(mController.getAmbientLux() > 0.0f);
        assertTrue(mController.getAmbientLux() < 10000.0f);
        assertTrue(mController.getAmbientLux() < 10000.0f);


        // t = 3000
        // t = 3000
        // lux reading should still not yet be 10000.
        // lux reading should still not yet be 10000.
        mClock.fastForward(increment);
        mClock.fastForward(increment);
        listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, 10000));
        listener.onSensorChanged(TestUtils.createSensorEvent(mLightSensor, 10000));
        assertTrue(mController.getAmbientLux() > 0.0f);
        assertTrue(mController.getAmbientLux() > 0.0f);
        assertTrue(mController.getAmbientLux() < 10000.0f);
        assertTrue(mController.getAmbientLux() < 10000.0f);


@@ -360,39 +372,39 @@ public class AutomaticBrightnessControllerTest {
        // lux has been high (10000) for 1000ms.
        // lux has been high (10000) for 1000ms.
        // lux reading should be 10000
        // lux reading should be 10000
        // short horizon (ambient lux) is high, long horizon is still not high
        // short horizon (ambient lux) is high, long horizon is still not high
        listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, 10000));
        listener.onSensorChanged(TestUtils.createSensorEvent(mLightSensor, 10000));
        assertEquals(10000.0f, mController.getAmbientLux(), EPSILON);
        assertEquals(10000.0f, mController.getAmbientLux(), EPSILON);


        // t = 4000
        // t = 4000
        // stay high
        // stay high
        mClock.fastForward(increment);
        mClock.fastForward(increment);
        listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, 10000));
        listener.onSensorChanged(TestUtils.createSensorEvent(mLightSensor, 10000));
        assertEquals(10000.0f, mController.getAmbientLux(), EPSILON);
        assertEquals(10000.0f, mController.getAmbientLux(), EPSILON);


        // t = 4500
        // t = 4500
        Mockito.clearInvocations(mBrightnessMappingStrategy);
        Mockito.clearInvocations(mBrightnessMappingStrategy);
        mClock.fastForward(increment);
        mClock.fastForward(increment);
        // short horizon is high, long horizon is high too
        // short horizon is high, long horizon is high too
        listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, 10000));
        listener.onSensorChanged(TestUtils.createSensorEvent(mLightSensor, 10000));
        verify(mBrightnessMappingStrategy, times(1)).getBrightness(10000, null, -1);
        verify(mBrightnessMappingStrategy, times(1)).getBrightness(10000, null, -1);
        assertEquals(10000.0f, mController.getAmbientLux(), EPSILON);
        assertEquals(10000.0f, mController.getAmbientLux(), EPSILON);


        // t = 5000
        // t = 5000
        mClock.fastForward(increment);
        mClock.fastForward(increment);
        listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, 0));
        listener.onSensorChanged(TestUtils.createSensorEvent(mLightSensor, 0));
        assertTrue(mController.getAmbientLux() > 0.0f);
        assertTrue(mController.getAmbientLux() > 0.0f);
        assertTrue(mController.getAmbientLux() < 10000.0f);
        assertTrue(mController.getAmbientLux() < 10000.0f);


        // t = 5500
        // t = 5500
        mClock.fastForward(increment);
        mClock.fastForward(increment);
        listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, 0));
        listener.onSensorChanged(TestUtils.createSensorEvent(mLightSensor, 0));
        assertTrue(mController.getAmbientLux() > 0.0f);
        assertTrue(mController.getAmbientLux() > 0.0f);
        assertTrue(mController.getAmbientLux() < 10000.0f);
        assertTrue(mController.getAmbientLux() < 10000.0f);


        // t = 6000
        // t = 6000
        mClock.fastForward(increment);
        mClock.fastForward(increment);
        // ambient lux goes to 0
        // ambient lux goes to 0
        listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, 0));
        listener.onSensorChanged(TestUtils.createSensorEvent(mLightSensor, 0));
        assertEquals(0.0f, mController.getAmbientLux(), EPSILON);
        assertEquals(0.0f, mController.getAmbientLux(), EPSILON);
    }
    }


@@ -427,12 +439,9 @@ public class AutomaticBrightnessControllerTest {


    @Test
    @Test
    public void testBrightnessGetsThrottled() throws Exception {
    public void testBrightnessGetsThrottled() throws Exception {
        Sensor lightSensor = TestUtils.createSensor(Sensor.TYPE_LIGHT, "Light Sensor");
        mController = setupController(lightSensor);

        ArgumentCaptor<SensorEventListener> listenerCaptor =
        ArgumentCaptor<SensorEventListener> listenerCaptor =
                ArgumentCaptor.forClass(SensorEventListener.class);
                ArgumentCaptor.forClass(SensorEventListener.class);
        verify(mSensorManager).registerListener(listenerCaptor.capture(), eq(lightSensor),
        verify(mSensorManager).registerListener(listenerCaptor.capture(), eq(mLightSensor),
                eq(INITIAL_LIGHT_SENSOR_RATE * 1000), any(Handler.class));
                eq(INITIAL_LIGHT_SENSOR_RATE * 1000), any(Handler.class));
        SensorEventListener listener = listenerCaptor.getValue();
        SensorEventListener listener = listenerCaptor.getValue();


@@ -447,7 +456,7 @@ public class AutomaticBrightnessControllerTest {
                .thenReturn(normalizedBrightness);
                .thenReturn(normalizedBrightness);


        // Sensor reads 100 lux. We should get max brightness.
        // Sensor reads 100 lux. We should get max brightness.
        listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, (int) lux));
        listener.onSensorChanged(TestUtils.createSensorEvent(mLightSensor, (int) lux));
        assertEquals(BRIGHTNESS_MAX_FLOAT, mController.getAutomaticScreenBrightness(), 0.0f);
        assertEquals(BRIGHTNESS_MAX_FLOAT, mController.getAutomaticScreenBrightness(), 0.0f);


        // Apply throttling and notify ABC (simulates DisplayPowerController#updatePowerState())
        // Apply throttling and notify ABC (simulates DisplayPowerController#updatePowerState())