|
| 1 | +package io.sentry.android.replay |
| 2 | + |
| 3 | +import io.sentry.Breadcrumb |
| 4 | +import io.sentry.SentryLevel |
| 5 | +import io.sentry.SpanDataConvention |
| 6 | +import io.sentry.rrweb.RRWebBreadcrumbEvent |
| 7 | +import io.sentry.rrweb.RRWebSpanEvent |
| 8 | +import junit.framework.TestCase.assertEquals |
| 9 | +import junit.framework.TestCase.assertTrue |
| 10 | +import java.util.Date |
| 11 | +import kotlin.test.Test |
| 12 | +import kotlin.test.assertNull |
| 13 | + |
| 14 | +class DefaultReplayBreadcrumbConverterTest { |
| 15 | + class Fixture { |
| 16 | + fun getSut(): DefaultReplayBreadcrumbConverter { |
| 17 | + return DefaultReplayBreadcrumbConverter() |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + private val fixture = Fixture() |
| 22 | + |
| 23 | + @Test |
| 24 | + fun `returns null when no category`() { |
| 25 | + val converter = fixture.getSut() |
| 26 | + |
| 27 | + val breadcrumb = Breadcrumb(Date(123L)).apply { |
| 28 | + message = "message" |
| 29 | + } |
| 30 | + |
| 31 | + val rrwebEvent = converter.convert(breadcrumb) |
| 32 | + |
| 33 | + assertNull(rrwebEvent) |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + fun `convert RRWebSpanEvent`() { |
| 38 | + val converter = fixture.getSut() |
| 39 | + |
| 40 | + val breadcrumb = Breadcrumb(Date(123L)).apply { |
| 41 | + category = "http" |
| 42 | + data["url"] = "http://example.com" |
| 43 | + data["status_code"] = 404 |
| 44 | + data["method"] = "GET" |
| 45 | + data[SpanDataConvention.HTTP_START_TIMESTAMP] = 1234L |
| 46 | + data[SpanDataConvention.HTTP_END_TIMESTAMP] = 2234L |
| 47 | + data["http.response_content_length"] = 300 |
| 48 | + data["http.request_content_length"] = 400 |
| 49 | + } |
| 50 | + |
| 51 | + val rrwebEvent = converter.convert(breadcrumb) |
| 52 | + |
| 53 | + check(rrwebEvent is RRWebSpanEvent) |
| 54 | + assertEquals("resource.http", rrwebEvent.op) |
| 55 | + assertEquals("http://example.com", rrwebEvent.description) |
| 56 | + assertEquals(123L, rrwebEvent.timestamp) |
| 57 | + assertEquals(1.234, rrwebEvent.startTimestamp) |
| 58 | + assertEquals(2.234, rrwebEvent.endTimestamp) |
| 59 | + assertEquals(404, rrwebEvent.data!!["statusCode"]) |
| 60 | + assertEquals("GET", rrwebEvent.data!!["method"]) |
| 61 | + assertEquals(300, rrwebEvent.data!!["responseBodySize"]) |
| 62 | + assertEquals(400, rrwebEvent.data!!["requestBodySize"]) |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + fun `returns null if not eligible for RRWebSpanEvent`() { |
| 67 | + val converter = fixture.getSut() |
| 68 | + |
| 69 | + val breadcrumb = Breadcrumb(Date(123L)).apply { |
| 70 | + category = "http" |
| 71 | + data["status_code"] = 404 |
| 72 | + data["method"] = "GET" |
| 73 | + data[SpanDataConvention.HTTP_START_TIMESTAMP] = 1234L |
| 74 | + data[SpanDataConvention.HTTP_END_TIMESTAMP] = 2234L |
| 75 | + data["http.response_content_length"] = 300 |
| 76 | + data["http.request_content_length"] = 400 |
| 77 | + } |
| 78 | + |
| 79 | + val rrwebEvent = converter.convert(breadcrumb) |
| 80 | + |
| 81 | + assertNull(rrwebEvent) |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + fun `converts app lifecycle breadcrumbs`() { |
| 86 | + val converter = fixture.getSut() |
| 87 | + |
| 88 | + val breadcrumb = Breadcrumb(Date(123L)).apply { |
| 89 | + category = "app.lifecycle" |
| 90 | + type = "navigation" |
| 91 | + data["state"] = "background" |
| 92 | + } |
| 93 | + |
| 94 | + val rrwebEvent = converter.convert(breadcrumb) |
| 95 | + |
| 96 | + check(rrwebEvent is RRWebBreadcrumbEvent) |
| 97 | + assertEquals("app.background", rrwebEvent.category) |
| 98 | + assertEquals(123L, rrwebEvent.timestamp) |
| 99 | + assertEquals(0.123, rrwebEvent.breadcrumbTimestamp) |
| 100 | + assertEquals("default", rrwebEvent.breadcrumbType) |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + fun `converts device orientation breadcrumbs`() { |
| 105 | + val converter = fixture.getSut() |
| 106 | + |
| 107 | + val breadcrumb = Breadcrumb(Date(123L)).apply { |
| 108 | + category = "device.orientation" |
| 109 | + type = "navigation" |
| 110 | + data["position"] = "landscape" |
| 111 | + } |
| 112 | + |
| 113 | + val rrwebEvent = converter.convert(breadcrumb) |
| 114 | + |
| 115 | + check(rrwebEvent is RRWebBreadcrumbEvent) |
| 116 | + assertEquals("device.orientation", rrwebEvent.category) |
| 117 | + assertEquals("landscape", rrwebEvent.data!!["position"]) |
| 118 | + assertEquals(123L, rrwebEvent.timestamp) |
| 119 | + assertEquals(0.123, rrwebEvent.breadcrumbTimestamp) |
| 120 | + assertEquals("default", rrwebEvent.breadcrumbType) |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + fun `returns null if no position for orientation breadcrumbs`() { |
| 125 | + val converter = fixture.getSut() |
| 126 | + |
| 127 | + val breadcrumb = Breadcrumb(Date(123L)).apply { |
| 128 | + category = "device.orientation" |
| 129 | + type = "navigation" |
| 130 | + } |
| 131 | + |
| 132 | + val rrwebEvent = converter.convert(breadcrumb) |
| 133 | + |
| 134 | + assertNull(rrwebEvent) |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + fun `converts navigation breadcrumbs`() { |
| 139 | + val converter = fixture.getSut() |
| 140 | + |
| 141 | + val breadcrumb = Breadcrumb(Date(123L)).apply { |
| 142 | + category = "navigation" |
| 143 | + type = "navigation" |
| 144 | + data["state"] = "resumed" |
| 145 | + data["screen"] = "io.sentry.MainActivity" |
| 146 | + } |
| 147 | + |
| 148 | + val rrwebEvent = converter.convert(breadcrumb) |
| 149 | + |
| 150 | + check(rrwebEvent is RRWebBreadcrumbEvent) |
| 151 | + assertEquals("navigation", rrwebEvent.category) |
| 152 | + assertEquals("MainActivity", rrwebEvent.data!!["to"]) |
| 153 | + assertEquals(123L, rrwebEvent.timestamp) |
| 154 | + assertEquals(0.123, rrwebEvent.breadcrumbTimestamp) |
| 155 | + assertEquals("default", rrwebEvent.breadcrumbType) |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + fun `converts navigation breadcrumbs with destination`() { |
| 160 | + val converter = fixture.getSut() |
| 161 | + |
| 162 | + val breadcrumb = Breadcrumb(Date(123L)).apply { |
| 163 | + category = "navigation" |
| 164 | + type = "navigation" |
| 165 | + data["to"] = "/github" |
| 166 | + } |
| 167 | + |
| 168 | + val rrwebEvent = converter.convert(breadcrumb) |
| 169 | + |
| 170 | + check(rrwebEvent is RRWebBreadcrumbEvent) |
| 171 | + assertEquals("navigation", rrwebEvent.category) |
| 172 | + assertEquals("/github", rrwebEvent.data!!["to"]) |
| 173 | + assertEquals(123L, rrwebEvent.timestamp) |
| 174 | + assertEquals(0.123, rrwebEvent.breadcrumbTimestamp) |
| 175 | + assertEquals("default", rrwebEvent.breadcrumbType) |
| 176 | + } |
| 177 | + |
| 178 | +// @Test |
| 179 | +// fun `test convert with navigation and app lifecycle`() { |
| 180 | +// val breadcrumb = Breadcrumb().apply { |
| 181 | +// message = "message" |
| 182 | +// category = "navigation" |
| 183 | +// type = "navigation" |
| 184 | +// level = SentryLevel.ERROR |
| 185 | +// data["state"] = "resumed" |
| 186 | +// data["screen"] = "screen" |
| 187 | +// } |
| 188 | +// |
| 189 | +// val result = DefaultReplayBreadcrumbConverter().convert(breadcrumb) |
| 190 | +// |
| 191 | +// assertTrue(result is RRWebBreadcrumbEvent) |
| 192 | +// assertEquals("navigation", result.category) |
| 193 | +// assertEquals("navigation", result.type) |
| 194 | +// assertEquals(SentryLevel.ERROR, result.level) |
| 195 | +// assertEquals("resumed", result.data["state"]) |
| 196 | +// assertEquals("screen", result.data["screen"]) |
| 197 | +// } |
| 198 | +// |
| 199 | +// @Test |
| 200 | +// fun `test convert with navigation and device orientation`() { |
| 201 | +// val breadcrumb = Breadcrumb().apply { |
| 202 | +// message = "message" |
| 203 | +// category = "navigation" |
| 204 | +// type = "navigation" |
| 205 | +// level = SentryLevel.ERROR |
| 206 | +// data["position"] = "landscape" |
| 207 | +// } |
| 208 | +// |
| 209 | +// val result = DefaultReplayBreadcrumbConverter().convert(breadcrumb) |
| 210 | +// |
| 211 | +// assertTrue(result is RRWebBreadcrumbEvent) |
| 212 | +// assertEquals("navigation", result.category) |
| 213 | +// assertEquals("navigation", result.type) |
| 214 | +// assertEquals(SentryLevel.ERROR, result.level) |
| 215 | +// assertEquals("landscape", result.data["position"]) |
| 216 | +// } |
| 217 | +} |
0 commit comments