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

This commit is contained in:
Fiona Campbell
2022-10-18 17:15:01 +00:00
committed by Android (Google) Code Review
2 changed files with 34 additions and 0 deletions

View File

@@ -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");
}

View File

@@ -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));
}