|
| 1 | +package io.sentry.react |
| 2 | + |
| 3 | +import android.app.Activity |
| 4 | +import android.content.Context |
| 5 | +import androidx.test.core.app.ApplicationProvider |
| 6 | +import com.facebook.react.bridge.ReactApplicationContext |
| 7 | +import io.sentry.android.core.BuildInfoProvider |
| 8 | +import org.junit.Assert.assertNotNull |
| 9 | +import org.junit.Assert.assertNull |
| 10 | +import org.junit.Before |
| 11 | +import org.junit.Test |
| 12 | +import org.junit.runner.RunWith |
| 13 | +import org.mockito.Mockito.mock |
| 14 | +import org.mockito.MockitoAnnotations |
| 15 | +import org.mockito.kotlin.whenever |
| 16 | +import org.robolectric.RobolectricTestRunner |
| 17 | + |
| 18 | +@RunWith(RobolectricTestRunner::class) |
| 19 | +class RNSentryOnDrawReporterTest { |
| 20 | + companion object { |
| 21 | + private const val TTID_PREFIX = RNSentryOnDrawReporterManager.TTID_PREFIX |
| 22 | + private const val TTFD_PREFIX = RNSentryOnDrawReporterManager.TTFD_PREFIX |
| 23 | + private const val SPAN_ID = "test-span-id" |
| 24 | + private const val NEW_SPAN_ID = "new-test-span-id" |
| 25 | + } |
| 26 | + |
| 27 | + @Before |
| 28 | + fun setUp() { |
| 29 | + MockitoAnnotations.openMocks(this) |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + fun `when parentSpanId and timeToFullDisplay are set the next render timestamp is saved`() { |
| 34 | + val reporter = createTestRNSentryOnDrawReporterView() |
| 35 | + reporter.setFullDisplay(true) |
| 36 | + reporter.setParentSpanId(SPAN_ID) |
| 37 | + |
| 38 | + assertNotNull(RNSentryTimeToDisplay.popTimeToDisplayFor(TTFD_PREFIX + SPAN_ID)) |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + fun `when parentSpanId and timeToInitialDisplay are set the next render timestamp is saved`() { |
| 43 | + val reporter = createTestRNSentryOnDrawReporterView() |
| 44 | + reporter.setInitialDisplay(true) |
| 45 | + reporter.setParentSpanId(SPAN_ID) |
| 46 | + |
| 47 | + assertNotNull(RNSentryTimeToDisplay.popTimeToDisplayFor(TTID_PREFIX + SPAN_ID)) |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + fun `when parentSpanId and timeToFullDisplay are set the next render timestamp is saved - reversed order`() { |
| 52 | + val reporter = createTestRNSentryOnDrawReporterView() |
| 53 | + reporter.setParentSpanId(SPAN_ID) |
| 54 | + reporter.setFullDisplay(true) |
| 55 | + |
| 56 | + assertNotNull(RNSentryTimeToDisplay.popTimeToDisplayFor(TTFD_PREFIX + SPAN_ID)) |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + fun `when parentSpanId and timeToInitialDisplay are set the next render timestamp is saved - reversed order`() { |
| 61 | + val reporter = createTestRNSentryOnDrawReporterView() |
| 62 | + reporter.setParentSpanId(SPAN_ID) |
| 63 | + reporter.setInitialDisplay(true) |
| 64 | + |
| 65 | + assertNotNull(RNSentryTimeToDisplay.popTimeToDisplayFor(TTID_PREFIX + SPAN_ID)) |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + fun `when display flag and parentSpanId changes the next full display render is saved`() { |
| 70 | + val reporter = createTestRNSentryOnDrawReporterView() |
| 71 | + reporter.setFullDisplay(true) |
| 72 | + reporter.setParentSpanId(SPAN_ID) |
| 73 | + RNSentryTimeToDisplay.popTimeToDisplayFor(TTFD_PREFIX + SPAN_ID) |
| 74 | + |
| 75 | + reporter.setFullDisplay(false) |
| 76 | + reporter.setFullDisplay(true) |
| 77 | + reporter.setParentSpanId(NEW_SPAN_ID) |
| 78 | + assertNotNull(RNSentryTimeToDisplay.popTimeToDisplayFor(TTFD_PREFIX + NEW_SPAN_ID)) |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + fun `when display flag and parentSpanId changes the next initial display render is saved`() { |
| 83 | + val reporter = createTestRNSentryOnDrawReporterView() |
| 84 | + reporter.setInitialDisplay(true) |
| 85 | + reporter.setParentSpanId(SPAN_ID) |
| 86 | + RNSentryTimeToDisplay.popTimeToDisplayFor(TTID_PREFIX + SPAN_ID) |
| 87 | + |
| 88 | + reporter.setInitialDisplay(false) |
| 89 | + reporter.setInitialDisplay(true) |
| 90 | + reporter.setParentSpanId(NEW_SPAN_ID) |
| 91 | + assertNotNull(RNSentryTimeToDisplay.popTimeToDisplayFor(TTID_PREFIX + NEW_SPAN_ID)) |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + fun `when parentSpanId doesn't change the next full display render is not saved`() { |
| 96 | + val reporter = createTestRNSentryOnDrawReporterView() |
| 97 | + reporter.setFullDisplay(true) |
| 98 | + reporter.setParentSpanId(SPAN_ID) |
| 99 | + RNSentryTimeToDisplay.popTimeToDisplayFor(TTFD_PREFIX + SPAN_ID) |
| 100 | + |
| 101 | + reporter.setFullDisplay(false) |
| 102 | + reporter.setFullDisplay(true) |
| 103 | + reporter.setParentSpanId(SPAN_ID) |
| 104 | + assertNull(RNSentryTimeToDisplay.popTimeToDisplayFor(TTFD_PREFIX + SPAN_ID)) |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + fun `when parentSpanId doesn't change the next initial display render is not saved`() { |
| 109 | + val reporter = createTestRNSentryOnDrawReporterView() |
| 110 | + reporter.setInitialDisplay(true) |
| 111 | + reporter.setParentSpanId(SPAN_ID) |
| 112 | + RNSentryTimeToDisplay.popTimeToDisplayFor(TTID_PREFIX + SPAN_ID) |
| 113 | + |
| 114 | + reporter.setInitialDisplay(false) |
| 115 | + reporter.setInitialDisplay(true) |
| 116 | + reporter.setParentSpanId(SPAN_ID) |
| 117 | + assertNull(RNSentryTimeToDisplay.popTimeToDisplayFor(TTID_PREFIX + SPAN_ID)) |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + fun `when display flag doesn't change the next full display render is not saved`() { |
| 122 | + val reporter = createTestRNSentryOnDrawReporterView() |
| 123 | + reporter.setFullDisplay(true) |
| 124 | + reporter.setParentSpanId(SPAN_ID) |
| 125 | + RNSentryTimeToDisplay.popTimeToDisplayFor(TTFD_PREFIX + SPAN_ID) |
| 126 | + |
| 127 | + reporter.setFullDisplay(true) |
| 128 | + assertNull(RNSentryTimeToDisplay.popTimeToDisplayFor(TTFD_PREFIX + SPAN_ID)) |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + fun `when display flag doesn't change the next initial display render is not saved`() { |
| 133 | + val reporter = createTestRNSentryOnDrawReporterView() |
| 134 | + reporter.setInitialDisplay(true) |
| 135 | + reporter.setParentSpanId(SPAN_ID) |
| 136 | + RNSentryTimeToDisplay.popTimeToDisplayFor(TTID_PREFIX + SPAN_ID) |
| 137 | + |
| 138 | + reporter.setInitialDisplay(true) |
| 139 | + assertNull(RNSentryTimeToDisplay.popTimeToDisplayFor(TTID_PREFIX + SPAN_ID)) |
| 140 | + } |
| 141 | + |
| 142 | + class TestRNSentryOnDrawReporterView( |
| 143 | + context: Context, |
| 144 | + reactContext: ReactApplicationContext, |
| 145 | + buildInfo: BuildInfoProvider, |
| 146 | + ) : RNSentryOnDrawReporterManager.RNSentryOnDrawReporterView(context, reactContext, buildInfo) { |
| 147 | + override fun registerForNextDraw( |
| 148 | + activity: Activity, |
| 149 | + callback: Runnable, |
| 150 | + buildInfo: BuildInfoProvider, |
| 151 | + ) { |
| 152 | + callback.run() |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private fun createTestRNSentryOnDrawReporterView(): TestRNSentryOnDrawReporterView = |
| 157 | + TestRNSentryOnDrawReporterView(ApplicationProvider.getApplicationContext(), mockReactContext(), mockBuildInfo()) |
| 158 | + |
| 159 | + private fun mockReactContext(): ReactApplicationContext { |
| 160 | + val reactContext = mock<ReactApplicationContext>() |
| 161 | + whenever(reactContext.getCurrentActivity()).thenReturn(mock<Activity>()) |
| 162 | + return reactContext |
| 163 | + } |
| 164 | + |
| 165 | + private fun mockBuildInfo(): BuildInfoProvider = mock() |
| 166 | +} |
0 commit comments