@@ -37,14 +37,15 @@ import androidx.testutils.assertThrows
37
37
import com.google.common.truth.Truth.assertThat
38
38
import java.util.Locale
39
39
import kotlin.coroutines.ContinuationInterceptor
40
- import kotlin .coroutines.resume
40
+ import kotlinx .coroutines.CompletableDeferred
41
41
import kotlinx.coroutines.CoroutineDispatcher
42
42
import kotlinx.coroutines.android.asCoroutineDispatcher
43
43
import kotlinx.coroutines.currentCoroutineContext
44
44
import kotlinx.coroutines.runBlocking
45
- import kotlinx.coroutines.suspendCancellableCoroutine
46
45
import kotlinx.coroutines.withContext
46
+ import kotlinx.coroutines.withTimeoutOrNull
47
47
import org.junit.After
48
+ import org.junit.Assert.fail
48
49
import org.junit.Before
49
50
import org.junit.BeforeClass
50
51
import org.junit.Rule
@@ -147,18 +148,22 @@ class OpenGlRendererTest {
147
148
val inputSurface = Surface (surfaceTexture).apply {
148
149
surfacesToRelease.add(this )
149
150
}
150
- // Create Bitmap for drawing
151
- val inputImage = createBitmap(WIDTH , HEIGHT )
151
+ // Listen for OnFrameAvailable updates before drawing.
152
+ val deferredOnFrameAvailable = CompletableDeferred <Unit >()
153
+ surfaceTexture.setOnFrameAvailableListener({
154
+ deferredOnFrameAvailable.complete(Unit )
155
+ }, Handler (Looper .getMainLooper()))
156
+
152
157
// Draw bitmap to inputSurface.
158
+ val inputImage = createBitmap(WIDTH , HEIGHT )
153
159
val canvas = inputSurface.lockHardwareCanvas()
154
160
canvas.drawBitmap(inputImage, 0f , 0f , null )
155
161
inputSurface.unlockCanvasAndPost(canvas)
162
+
156
163
// Wait for frame available and update texture.
157
- suspendCancellableCoroutine { continuation ->
158
- surfaceTexture.setOnFrameAvailableListener({
159
- continuation.resume(Unit )
160
- }, Handler (Looper .getMainLooper()))
161
- }
164
+ withTimeoutOrNull(5_000 ) {
165
+ deferredOnFrameAvailable.await()
166
+ } ? : fail(" Timed out waiting for SurfaceTexture frame available." )
162
167
surfaceTexture.updateTexImage()
163
168
164
169
// Act: take a snapshot.
0 commit comments