-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding ability to clear list of sent VirtualSensorEvents from ShadowV…
…irtualSensor Within a single test suite, if you send VirtualSensorEvents to a ShadowVirtualSensor in testA then call ShadowVirtualSensor.getSentEvents() in testB, then you will see stale data leak from the last test. This new method will help clear stale stale between tests. PiperOrigin-RevId: 621990558
- Loading branch information
Googler
authored and
Copybara-Service
committed
Apr 22, 2024
1 parent
d9d944c
commit 7acd17e
Showing
2 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
robolectric/src/test/java/org/robolectric/shadows/ShadowVirtualSensorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.robolectric.shadows; | ||
|
||
import static android.os.Build.VERSION_CODES.UPSIDE_DOWN_CAKE; | ||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import android.companion.virtual.sensor.VirtualSensor; | ||
import android.companion.virtual.sensor.VirtualSensorEvent; | ||
import android.hardware.Sensor; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.RobolectricTestRunner; | ||
import org.robolectric.annotation.Config; | ||
import org.robolectric.shadow.api.Shadow; | ||
|
||
/** Unit test for ShadowVirtualSensor. */ | ||
@Config(minSdk = UPSIDE_DOWN_CAKE) | ||
@RunWith(RobolectricTestRunner.class) | ||
public class ShadowVirtualSensorTest { | ||
|
||
@Test | ||
public void virtualSensor_getDeviceId() { | ||
VirtualSensor sensor = (VirtualSensor) createVirtualSensor(); | ||
((ShadowVirtualSensor) Shadow.extract(sensor)).setDeviceId(121); | ||
assertThat(sensor.getDeviceId()).isEqualTo(121); | ||
} | ||
|
||
@Test | ||
public void virtualSensor_getEvents() { | ||
VirtualSensor sensor = (VirtualSensor) createVirtualSensor(); | ||
VirtualSensorEvent event = | ||
new VirtualSensorEvent.Builder(new float[] {12f}) | ||
.setTimestampNanos(System.nanoTime()) | ||
.build(); | ||
|
||
sensor.sendEvent(event); | ||
|
||
assertThat(((ShadowVirtualSensor) Shadow.extract(sensor)).getSentEvents()) | ||
.containsExactly(event); | ||
} | ||
|
||
@Test | ||
public void virtualSensor_clearEvents() { | ||
VirtualSensor sensor = (VirtualSensor) createVirtualSensor(); | ||
ShadowVirtualSensor virtualSensor = (ShadowVirtualSensor) Shadow.extract(sensor); | ||
VirtualSensorEvent event = | ||
new VirtualSensorEvent.Builder(new float[] {12f}) | ||
.setTimestampNanos(System.nanoTime()) | ||
.build(); | ||
|
||
sensor.sendEvent(event); | ||
assertThat(virtualSensor.getSentEvents()).containsExactly(event); | ||
virtualSensor.clearSentEvents(); | ||
assertThat(virtualSensor.getSentEvents()).isEmpty(); | ||
} | ||
|
||
private /* VirtualSensor */ Object createVirtualSensor() { | ||
return new VirtualSensor(1, Sensor.TYPE_ACCELEROMETER, "some name", null, null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters