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

Commit c1793bd0 authored by Fiona Campbell's avatar Fiona Campbell Committed by Android (Google) Code Review
Browse files

Merge "Ensure BrightnessTracker is only "start()"ed once"

parents e8069174 14cdb170
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -220,6 +220,11 @@ public class BrightnessTracker {
    }

    private void backgroundStart(float initialBrightness) {
        synchronized (mDataCollectionLock) {
            if (mStarted) {
                return;
            }
        }
        if (DEBUG) {
            Slog.d(TAG, "Background start");
        }
@@ -250,6 +255,11 @@ public class BrightnessTracker {

    /** Stop listening for events */
    void stop() {
        synchronized (mDataCollectionLock) {
            if (!mStarted) {
                return;
            }
        }
        if (DEBUG) {
            Slog.d(TAG, "Stop");
        }
+24 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

@@ -885,6 +886,29 @@ public class BrightnessTrackerTest {
        assertNull(mInjector.mLightSensor);
    }

    @Test
    public void testOnlyOneReceiverRegistered() {
        assertNull(mInjector.mLightSensor);
        assertNull(mInjector.mSensorListener);
        startTracker(mTracker, 0.3f, false);

        assertNotNull(mInjector.mLightSensor);
        assertNotNull(mInjector.mSensorListener);
        Sensor registeredLightSensor = mInjector.mLightSensor;
        SensorEventListener registeredSensorListener = mInjector.mSensorListener;

        mTracker.start(0.3f);
        assertSame(registeredLightSensor, mInjector.mLightSensor);
        assertSame(registeredSensorListener, mInjector.mSensorListener);

        mTracker.stop();
        assertNull(mInjector.mLightSensor);
        assertNull(mInjector.mSensorListener);

        // mInjector asserts that we aren't removing a null receiver
        mTracker.stop();
    }

    private InputStream getInputStream(String data) {
        return new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));
    }