Test getLatestVsyncEventData for late frame.
Bug: 205721584
Test: atest ChoreographerTest
Change-Id: I037ee90ffb8afe135c77dffc09a3cbd8369e1636
diff --git a/tests/tests/view/src/android/view/cts/ChoreographerTest.java b/tests/tests/view/src/android/view/cts/ChoreographerTest.java
index c74b4a1..5f7c433 100644
--- a/tests/tests/view/src/android/view/cts/ChoreographerTest.java
+++ b/tests/tests/view/src/android/view/cts/ChoreographerTest.java
@@ -25,7 +25,10 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
+import android.os.Handler;
+import android.os.Looper;
import android.os.SystemClock;
+import android.util.Log;
import android.view.Choreographer;
import androidx.test.annotation.UiThreadTest;
@@ -42,6 +45,7 @@
@MediumTest
@RunWith(AndroidJUnit4.class)
public class ChoreographerTest {
+ private static final String TAG = ChoreographerTest.class.getSimpleName();
private static final long NOMINAL_VSYNC_PERIOD = 16;
private static final long DELAY_PERIOD = NOMINAL_VSYNC_PERIOD * 5;
private static final long NANOS_PER_MS = 1000000;
@@ -417,6 +421,38 @@
}
}
+ @Test
+ public void testPostVsyncCallbackFrameDelayed() {
+ final Choreographer.VsyncCallback addedCallback = mock(
+ Choreographer.VsyncCallback.class);
+ long postTimeNanos = System.nanoTime();
+ mChoreographer.postVsyncCallback(addedCallback);
+
+ // The callback is posted and pending. Wait using the handler which is using the same UI
+ // thread as Choreographer, so that the thread is busy and the frame delayed logic can
+ // run for test.
+ long sleepTimeMs = NOMINAL_VSYNC_PERIOD * 3;
+ Looper looper = Looper.getMainLooper();
+ Handler handler = new Handler(looper);
+ handler.post(new Runnable(){
+ @Override
+ public void run() {
+ SystemClock.sleep(sleepTimeMs);
+ Log.d(TAG, "Slept for ms: " + sleepTimeMs);
+ }
+ });
+
+ ArgumentCaptor<Choreographer.FrameData> captor = ArgumentCaptor.forClass(
+ Choreographer.FrameData.class);
+ verify(addedCallback, timeout(NOMINAL_VSYNC_PERIOD * 10).times(1)).onVsync(
+ captor.capture());
+
+ Choreographer.FrameData frameData = captor.getValue();
+ long frameInterval = NOMINAL_VSYNC_PERIOD * 1000000;
+ assertTrue("Expected frame time updated to be later", frameData.getFrameTimeNanos()
+ > postTimeNanos + sleepTimeMs * 1000000 - frameInterval);
+ }
+
@Test(expected = IllegalArgumentException.class)
public void testPostNullVsyncCallback() {
mChoreographer.postVsyncCallback(null);