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

Commit 61a583e3 authored by Philip Junker's avatar Philip Junker Committed by Android (Google) Code Review
Browse files

Merge "Add test case for opening light session with priority" into sc-dev

parents 36cb0aa1 a9d9c267
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
@@ -47,6 +47,9 @@ import org.mockito.MockitoAnnotations;
@SmallTest
public class LightsServiceTest {

    private static final int HIGH_PRIORITY = Integer.MAX_VALUE;
    private static final int DEFAULT_PRIORITY = 0;

    private final ILights mHal = new ILights.Stub() {
        @Override
        public void setLightState(int id, HwLightState state) {
@@ -188,4 +191,30 @@ public class LightsServiceTest {
        // Then the light should turn back off.
        assertThat(manager.getLightState(micLight).getColor()).isEqualTo(0);
    }

    @Test
    public void testControlLights_higherPriorityCallerWinsContention() throws Exception {
        LightsService service = new LightsService(mContext, () -> mHal, Looper.getMainLooper());
        LightsManager manager = new SystemLightsManager(mContext, service.mManagerService);
        Light micLight = manager.getLights().get(0);

        // The light should begin by being off.
        assertThat(manager.getLightState(micLight).getColor()).isEqualTo(TRANSPARENT);

        try (LightsManager.LightsSession session1 = manager.openSession(DEFAULT_PRIORITY)) {
            try (LightsManager.LightsSession session2 = manager.openSession(HIGH_PRIORITY)) {
                // When session1 and session2 both request the same light:
                session1.requestLights(
                        new Builder().addLight(micLight, new LightState(BLUE)).build());
                session2.requestLights(
                        new Builder().addLight(micLight, new LightState(WHITE)).build());
                // Then session2 should win because it has a higher priority.
                assertThat(manager.getLightState(micLight).getColor()).isEqualTo(WHITE);
            }
            // Then session1 should have its request go into effect.
            assertThat(manager.getLightState(micLight).getColor()).isEqualTo(BLUE);
        }
        // Then the light should turn off because there are no more sessions.
        assertThat(manager.getLightState(micLight).getColor()).isEqualTo(TRANSPARENT);
    }
}