From 94cbd721b578355c16ad23b2c21e81c45376d08e Mon Sep 17 00:00:00 2001 From: Michael Keller Date: Tue, 11 Feb 2020 10:23:23 -0800 Subject: GazeDriverAwarenessSupplier: Updates tests Allowing injection of time source into GazeDriverAwarenessSupplier. Updating tests with explicit checking of values emitted. Bug: 143330523 Test: ExperimentalCarServiceTests Change-Id: I3c873b318c43261d41f2e2dda5896e6cd20e475e --- .../experimentalcar/GazeAttentionProcessor.java | 4 +-- .../GazeDriverAwarenessSupplier.java | 15 ++++---- .../GazeAttentionProcessorTest.java | 6 ++-- .../GazeDriverAwarenessSupplierTest.java | 41 ++++++++++++++++------ 4 files changed, 42 insertions(+), 24 deletions(-) (limited to 'experimental') diff --git a/experimental/service/src/com/android/experimentalcar/GazeAttentionProcessor.java b/experimental/service/src/com/android/experimentalcar/GazeAttentionProcessor.java index 8a5be98881..66e3a481a3 100644 --- a/experimental/service/src/com/android/experimentalcar/GazeAttentionProcessor.java +++ b/experimental/service/src/com/android/experimentalcar/GazeAttentionProcessor.java @@ -81,8 +81,8 @@ class GazeAttentionProcessor { // Save current timestamp for next pass. mLastTimestamp = timestamp; - logd(String.format( - "Attention [%f]->[%f]. Time=%d", startingAttention, mAttention, dtSeconds)); + logd(String.format("updateAttention(): Time=%1.2f. Attention [%f]->[%f]. ", + dtSeconds, startingAttention, mAttention)); return mAttention; } diff --git a/experimental/service/src/com/android/experimentalcar/GazeDriverAwarenessSupplier.java b/experimental/service/src/com/android/experimentalcar/GazeDriverAwarenessSupplier.java index 0762dd23d6..7fbacbf5de 100644 --- a/experimental/service/src/com/android/experimentalcar/GazeDriverAwarenessSupplier.java +++ b/experimental/service/src/com/android/experimentalcar/GazeDriverAwarenessSupplier.java @@ -61,16 +61,15 @@ public class GazeDriverAwarenessSupplier extends DriverAwarenessSupplierService private final GazeAttentionProcessor mProcessor; public GazeDriverAwarenessSupplier(Context context) { - mContext = context; - mTimeSource = new SystemTimeSource(); - mConfiguration = loadConfiguration(); - mProcessor = new GazeAttentionProcessor(mConfiguration); + this(context, new SystemTimeSource()); } @VisibleForTesting - private GazeDriverAwarenessSupplier(Context context, OccupantAwarenessManager oasManager) { - this(context); - mOasManager = oasManager; + GazeDriverAwarenessSupplier(Context context, ITimeSource timeSource) { + mContext = context; + mTimeSource = timeSource; + mConfiguration = loadConfiguration(); + mProcessor = new GazeAttentionProcessor(mConfiguration); } /** @@ -113,7 +112,7 @@ public class GazeDriverAwarenessSupplier extends DriverAwarenessSupplierService // Send an initial value once the provider is ready, as required by {link // IDriverAwarenessSupplierCallback}. - onDriverAwarenessUpdated( + emitAwarenessEvent( new DriverAwarenessEvent( mTimeSource.elapsedRealtime(), mConfiguration.initialValue)); } diff --git a/experimental/tests/experimentalcarservice_unit_test/src/com/android/experimentalcar/GazeAttentionProcessorTest.java b/experimental/tests/experimentalcarservice_unit_test/src/com/android/experimentalcar/GazeAttentionProcessorTest.java index 6c4f174159..82d60bbad4 100644 --- a/experimental/tests/experimentalcarservice_unit_test/src/com/android/experimentalcar/GazeAttentionProcessorTest.java +++ b/experimental/tests/experimentalcarservice_unit_test/src/com/android/experimentalcar/GazeAttentionProcessorTest.java @@ -31,10 +31,10 @@ import org.mockito.junit.MockitoJUnitRunner; @RunWith(MockitoJUnitRunner.class) public class GazeAttentionProcessorTest { - private static final long FRAME_TIME_MILLIS = 1000; + private static final long FRAME_TIME_MILLIS = 1000; // Milliseconds private static final float INITIAL_VALUE = 1.0f; - private static final float GROWTH_RATE = 0.5f; - private static final float DECAY_RATE = 0.4f; + private static final float GROWTH_RATE = 0.4f; + private static final float DECAY_RATE = 0.6f; private GazeAttentionProcessor mAttentionProcessor; diff --git a/experimental/tests/experimentalcarservice_unit_test/src/com/android/experimentalcar/GazeDriverAwarenessSupplierTest.java b/experimental/tests/experimentalcarservice_unit_test/src/com/android/experimentalcar/GazeDriverAwarenessSupplierTest.java index 122b336649..c1f7581ae8 100644 --- a/experimental/tests/experimentalcarservice_unit_test/src/com/android/experimentalcar/GazeDriverAwarenessSupplierTest.java +++ b/experimental/tests/experimentalcarservice_unit_test/src/com/android/experimentalcar/GazeDriverAwarenessSupplierTest.java @@ -16,9 +16,6 @@ package com.android.experimentalcar; -import static org.mockito.AdditionalMatchers.geq; -import static org.mockito.AdditionalMatchers.leq; -import static org.mockito.Mockito.any; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -38,6 +35,7 @@ import org.mockito.junit.MockitoJUnitRunner; @RunWith(MockitoJUnitRunner.class) public class GazeDriverAwarenessSupplierTest { + private static final long START_TIME_MILLIS = 1234L; private static final long FRAME_TIME_MILLIS = 1000L; private Context mSpyContext; @@ -45,11 +43,11 @@ public class GazeDriverAwarenessSupplierTest { private float mInitialValue; private float mGrowthRate; private float mDecayRate; + private FakeTimeSource mTimeSource; @Before public void setUp() throws Exception { mSpyContext = spy(InstrumentationRegistry.getInstrumentation().getTargetContext()); - mGazeSupplier = spy(new GazeDriverAwarenessSupplier(mSpyContext)); mInitialValue = mSpyContext @@ -59,6 +57,9 @@ public class GazeDriverAwarenessSupplierTest { mSpyContext.getResources().getFloat(R.fraction.driverAwarenessGazeModelGrowthRate); mDecayRate = mSpyContext.getResources().getFloat(R.fraction.driverAwarenessGazeModelDecayRate); + + mTimeSource = new FakeTimeSource(START_TIME_MILLIS); + mGazeSupplier = spy(new GazeDriverAwarenessSupplier(mSpyContext, mTimeSource)); } @Test @@ -66,7 +67,8 @@ public class GazeDriverAwarenessSupplierTest { // Supplier should return an initial callback after onReady(). mGazeSupplier.onReady(); - verify(mGazeSupplier).emitAwarenessEvent(new DriverAwarenessEvent(any(), mInitialValue)); + verify(mGazeSupplier) + .emitAwarenessEvent(new DriverAwarenessEvent(START_TIME_MILLIS, mInitialValue)); } @Test @@ -75,10 +77,11 @@ public class GazeDriverAwarenessSupplierTest { // attention supplier. mGazeSupplier.onReady(); - mGazeSupplier.processDetectionEvent(buildEmptyDetection(0)); + mGazeSupplier.processDetectionEvent(buildEmptyDetection(START_TIME_MILLIS)); // Should have exactly one call from the initial onReady(), but no further events. - verify(mGazeSupplier, times(1)).emitAwarenessEvent(new DriverAwarenessEvent(any(), any())); + verify(mGazeSupplier, times(1)) + .emitAwarenessEvent(new DriverAwarenessEvent(START_TIME_MILLIS, mInitialValue)); } @Test @@ -86,15 +89,23 @@ public class GazeDriverAwarenessSupplierTest { // Attention value should never exceed '1' no matter how long the driver looks on-road. mGazeSupplier.onReady(); - long timestamp = 0; + // Should have initial callback from onReady(). + verify(mGazeSupplier) + .emitAwarenessEvent(new DriverAwarenessEvent(START_TIME_MILLIS, mInitialValue)); + + long timestamp = START_TIME_MILLIS + FRAME_TIME_MILLIS; + float attention = mInitialValue; for (int i = 0; i < 100; i++) { OccupantAwarenessDetection detection = buildGazeDetection(timestamp, GazeDetection.VEHICLE_REGION_FORWARD_ROADWAY); mGazeSupplier.processDetectionEvent(detection); - verify(mGazeSupplier).emitAwarenessEvent(new DriverAwarenessEvent(any(), leq(1))); + verify(mGazeSupplier) + .emitAwarenessEvent(new DriverAwarenessEvent(timestamp, attention)); + // Increase attention, but not past 1. + attention = Math.min(attention + mGrowthRate, 1.0f); timestamp += FRAME_TIME_MILLIS; } } @@ -104,15 +115,23 @@ public class GazeDriverAwarenessSupplierTest { // Attention value should never fall below '0' no matter how long the driver looks off-road. mGazeSupplier.onReady(); - long timestamp = 0; + // Should have initial callback from onReady(). + verify(mGazeSupplier) + .emitAwarenessEvent(new DriverAwarenessEvent(START_TIME_MILLIS, mInitialValue)); + + long timestamp = START_TIME_MILLIS + FRAME_TIME_MILLIS; + float attention = mInitialValue; for (int i = 0; i < 100; i++) { OccupantAwarenessDetection detection = buildGazeDetection(timestamp, GazeDetection.VEHICLE_REGION_HEAD_UNIT_DISPLAY); mGazeSupplier.processDetectionEvent(detection); - verify(mGazeSupplier).emitAwarenessEvent(new DriverAwarenessEvent(any(), geq(0))); + verify(mGazeSupplier) + .emitAwarenessEvent(new DriverAwarenessEvent(timestamp, attention)); + // Decrement the attention, but not past 0. + attention = Math.max(attention - mDecayRate, 0); timestamp += FRAME_TIME_MILLIS; } } -- cgit v1.2.3