Skip to content

Commit defa023

Browse files
Treehugger RobotGerrit Code Review
Treehugger Robot
authored and
Gerrit Code Review
committed
Merge "Fix the flakiness in drawInputSurface_snapshotReturnsTheSame" into androidx-main
2 parents 92dbf9e + 0a16acc commit defa023

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

camera/camera-core/src/androidTest/java/androidx/camera/core/processing/OpenGlRendererTest.kt

+14-9
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@ import androidx.testutils.assertThrows
3737
import com.google.common.truth.Truth.assertThat
3838
import java.util.Locale
3939
import kotlin.coroutines.ContinuationInterceptor
40-
import kotlin.coroutines.resume
40+
import kotlinx.coroutines.CompletableDeferred
4141
import kotlinx.coroutines.CoroutineDispatcher
4242
import kotlinx.coroutines.android.asCoroutineDispatcher
4343
import kotlinx.coroutines.currentCoroutineContext
4444
import kotlinx.coroutines.runBlocking
45-
import kotlinx.coroutines.suspendCancellableCoroutine
4645
import kotlinx.coroutines.withContext
46+
import kotlinx.coroutines.withTimeoutOrNull
4747
import org.junit.After
48+
import org.junit.Assert.fail
4849
import org.junit.Before
4950
import org.junit.BeforeClass
5051
import org.junit.Rule
@@ -147,18 +148,22 @@ class OpenGlRendererTest {
147148
val inputSurface = Surface(surfaceTexture).apply {
148149
surfacesToRelease.add(this)
149150
}
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+
152157
// Draw bitmap to inputSurface.
158+
val inputImage = createBitmap(WIDTH, HEIGHT)
153159
val canvas = inputSurface.lockHardwareCanvas()
154160
canvas.drawBitmap(inputImage, 0f, 0f, null)
155161
inputSurface.unlockCanvasAndPost(canvas)
162+
156163
// 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.")
162167
surfaceTexture.updateTexImage()
163168

164169
// Act: take a snapshot.

0 commit comments

Comments
 (0)