Skip to content

Commit

Permalink
FIX #707: replace ChoreographerCompat with Choreographer
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-saia-datadog committed Jul 26, 2024
1 parent 5630e33 commit b8ed00d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

package com.datadog.reactnative

import com.facebook.react.modules.core.ChoreographerCompat
import android.view.Choreographer

internal class FrameRateProvider(
reactFrameRateCallback: ((Double) -> Unit),
Expand All @@ -30,9 +30,9 @@ internal class FrameRateProvider(
internal class FpsFrameCallback(
private val reactFrameRateCallback: ((Double) -> Unit),
private val uiThreadExecutor: UiThreadExecutor
) : ChoreographerCompat.FrameCallback() {
) : Choreographer.FrameCallback {

private var choreographer: ChoreographerCompat? = null
private var choreographer: Choreographer? = null
private var lastFrameTime = -1L

override fun doFrame(time: Long) {
Expand All @@ -45,14 +45,14 @@ internal class FpsFrameCallback(

fun start() {
uiThreadExecutor.runOnUiThread {
choreographer = ChoreographerCompat.getInstance()
choreographer = Choreographer.getInstance()
choreographer?.postFrameCallback(this@FpsFrameCallback)
}
}

fun stop() {
uiThreadExecutor.runOnUiThread {
choreographer = ChoreographerCompat.getInstance()
choreographer = Choreographer.getInstance()
choreographer?.removeFrameCallback(this@FpsFrameCallback)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import com.datadog.tools.unit.toReadableMap
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.modules.core.ChoreographerCompat
import fr.xgouchet.elmyr.Forge
import fr.xgouchet.elmyr.annotation.AdvancedForgery
import fr.xgouchet.elmyr.annotation.BoolForgery
Expand Down Expand Up @@ -81,13 +80,6 @@ import org.mockito.kotlin.verifyNoInteractions
import org.mockito.kotlin.whenever
import org.mockito.quality.Strictness

fun mockChoreographerCompatInstance(mock: ChoreographerCompat = mock()) {
ChoreographerCompat::class.java.setStaticValue(
"sInstance",
mock
)
}

fun mockChoreographerInstance(mock: Choreographer = mock()) {
Choreographer::class.java.setStaticValue(
"sThreadInstance",
Expand Down Expand Up @@ -135,9 +127,6 @@ internal class DdSdkTest {
@Mock
lateinit var mockChoreographer: Choreographer

@Mock
lateinit var mockChoreographerCompat: ChoreographerCompat

@BeforeEach
fun `set up`() {
val mockLooper = mock<Looper>()
Expand All @@ -148,10 +137,8 @@ internal class DdSdkTest {
whenever(mockRumMonitor._getInternal()) doReturn mockRumInternalProxy

doNothing().whenever(mockChoreographer).postFrameCallback(any())
doNothing().whenever(mockChoreographerCompat).postFrameCallback(any())

mockChoreographerInstance(mockChoreographer)
mockChoreographerCompatInstance(mockChoreographerCompat)

whenever(mockReactContext.applicationContext) doReturn mockContext
whenever(mockContext.packageName) doReturn "packageName"
Expand Down Expand Up @@ -1581,8 +1568,9 @@ internal class DdSdkTest {
.hasField("featureConfiguration") {
it.hasFieldEqualTo("vitalsMonitorUpdateFrequency", VitalsUpdateFrequency.RARE)
}
argumentCaptor<ChoreographerCompat.FrameCallback> {
verify(mockChoreographerCompat).postFrameCallback(capture())

argumentCaptor<Choreographer.FrameCallback> {
verify(mockChoreographer).postFrameCallback(capture())
assertThat(firstValue).isInstanceOf(FpsFrameCallback::class.java)
}
}
Expand All @@ -1592,7 +1580,7 @@ internal class DdSdkTest {
@Forgery configuration: DdSdkConfiguration
) {
// Given
doThrow(IllegalStateException()).whenever(mockChoreographerCompat).postFrameCallback(any())
doThrow(IllegalStateException()).whenever(mockChoreographer).postFrameCallback(any())
val bridgeConfiguration = configuration.copy(
vitalsUpdateFrequency = "NEVER",
longTaskThresholdMs = 0.0
Expand Down Expand Up @@ -1620,7 +1608,7 @@ internal class DdSdkTest {
.hasField("featureConfiguration") {
it.hasFieldEqualTo("vitalsMonitorUpdateFrequency", VitalsUpdateFrequency.NEVER)
}
verifyNoInteractions(mockChoreographerCompat)
verifyNoInteractions(mockChoreographer)
}

@Test
Expand Down Expand Up @@ -1660,8 +1648,8 @@ internal class DdSdkTest {
.hasField("featureConfiguration") {
it.hasFieldEqualTo("vitalsMonitorUpdateFrequency", VitalsUpdateFrequency.AVERAGE)
}
argumentCaptor<ChoreographerCompat.FrameCallback> {
verify(mockChoreographerCompat).postFrameCallback(capture())
argumentCaptor<Choreographer.FrameCallback> {
verify(mockChoreographer).postFrameCallback(capture())
assertThat(firstValue).isInstanceOf(FpsFrameCallback::class.java)

// When
Expand Down Expand Up @@ -1698,8 +1686,8 @@ internal class DdSdkTest {
testedBridgeSdk.initialize(bridgeConfiguration.toReadableJavaOnlyMap(), mockPromise)

// Then
argumentCaptor<ChoreographerCompat.FrameCallback> {
verify(mockChoreographerCompat).postFrameCallback(capture())
argumentCaptor<Choreographer.FrameCallback> {
verify(mockChoreographer).postFrameCallback(capture())

// When
firstValue.doFrame(timestampNs)
Expand Down Expand Up @@ -1735,8 +1723,8 @@ internal class DdSdkTest {
testedBridgeSdk.initialize(bridgeConfiguration.toReadableJavaOnlyMap(), mockPromise)

// Then
argumentCaptor<ChoreographerCompat.FrameCallback> {
verify(mockChoreographerCompat).postFrameCallback(capture())
argumentCaptor<Choreographer.FrameCallback> {
verify(mockChoreographer).postFrameCallback(capture())

// When
firstValue.doFrame(timestampNs)
Expand Down

0 comments on commit b8ed00d

Please sign in to comment.