|
| 1 | +package io.sentry.rnsentryandroidtester |
| 2 | + |
| 3 | +import io.sentry.Breadcrumb |
| 4 | +import io.sentry.SentryLevel |
| 5 | +import io.sentry.react.RNSentryReplayBreadcrumbConverter |
| 6 | +import io.sentry.rrweb.RRWebBreadcrumbEvent |
| 7 | +import io.sentry.rrweb.RRWebEventType |
| 8 | +import org.junit.Assert.assertEquals |
| 9 | +import org.junit.Test |
| 10 | +import org.junit.runner.RunWith |
| 11 | +import org.junit.runners.JUnit4 |
| 12 | + |
| 13 | +@RunWith(JUnit4::class) |
| 14 | +class RNSentryReplayBreadcrumbConverterTest { |
| 15 | + |
| 16 | + @Test |
| 17 | + fun convertNavigationBreadcrumb() { |
| 18 | + val converter = RNSentryReplayBreadcrumbConverter() |
| 19 | + val testBreadcrumb = Breadcrumb() |
| 20 | + testBreadcrumb.level = SentryLevel.INFO |
| 21 | + testBreadcrumb.type = "navigation" |
| 22 | + testBreadcrumb.category = "navigation" |
| 23 | + testBreadcrumb.setData("from", "HomeScreen") |
| 24 | + testBreadcrumb.setData("to", "ProfileScreen") |
| 25 | + val actual = converter.convert(testBreadcrumb) as RRWebBreadcrumbEvent |
| 26 | + |
| 27 | + assertRRWebBreadcrumbDefaults(actual) |
| 28 | + assertEquals(SentryLevel.INFO, actual.level) |
| 29 | + assertEquals("navigation", actual.category) |
| 30 | + assertEquals("HomeScreen", actual.data?.get("from")) |
| 31 | + assertEquals("ProfileScreen", actual.data?.get("to")) |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + fun convertNavigationBreadcrumbWithOnlyTo() { |
| 36 | + val converter = RNSentryReplayBreadcrumbConverter() |
| 37 | + val testBreadcrumb = Breadcrumb() |
| 38 | + testBreadcrumb.level = SentryLevel.INFO |
| 39 | + testBreadcrumb.type = "navigation" |
| 40 | + testBreadcrumb.category = "navigation" |
| 41 | + testBreadcrumb.setData("to", "ProfileScreen") |
| 42 | + val actual = converter.convert(testBreadcrumb) as RRWebBreadcrumbEvent |
| 43 | + |
| 44 | + assertRRWebBreadcrumbDefaults(actual) |
| 45 | + assertEquals(SentryLevel.INFO, actual.level) |
| 46 | + assertEquals("navigation", actual.category) |
| 47 | + assertEquals(null, actual.data?.get("from")) |
| 48 | + assertEquals("ProfileScreen", actual.data?.get("to")) |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + fun convertForegroundBreadcrumb() { |
| 53 | + val converter = RNSentryReplayBreadcrumbConverter() |
| 54 | + val testBreadcrumb = Breadcrumb() |
| 55 | + testBreadcrumb.type = "navigation" |
| 56 | + testBreadcrumb.category = "app.lifecycle" |
| 57 | + testBreadcrumb.setData("state", "foreground"); |
| 58 | + val actual = converter.convert(testBreadcrumb) as RRWebBreadcrumbEvent |
| 59 | + |
| 60 | + assertRRWebBreadcrumbDefaults(actual) |
| 61 | + assertEquals("app.foreground", actual.category) |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + fun convertBackgroundBreadcrumb() { |
| 66 | + val converter = RNSentryReplayBreadcrumbConverter() |
| 67 | + val testBreadcrumb = Breadcrumb() |
| 68 | + testBreadcrumb.type = "navigation" |
| 69 | + testBreadcrumb.category = "app.lifecycle" |
| 70 | + testBreadcrumb.setData("state", "background"); |
| 71 | + val actual = converter.convert(testBreadcrumb) as RRWebBreadcrumbEvent |
| 72 | + |
| 73 | + assertRRWebBreadcrumbDefaults(actual) |
| 74 | + assertEquals("app.background", actual.category) |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + fun doesNotConvertSentryEventBreadcrumb() { |
| 79 | + val converter = RNSentryReplayBreadcrumbConverter() |
| 80 | + val testBreadcrumb = Breadcrumb(); |
| 81 | + testBreadcrumb.category = "sentry.event" |
| 82 | + val actual = converter.convert(testBreadcrumb) |
| 83 | + assertEquals(null, actual) |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + fun doesNotConvertSentryTransactionBreadcrumb() { |
| 88 | + val converter = RNSentryReplayBreadcrumbConverter() |
| 89 | + val testBreadcrumb = Breadcrumb(); |
| 90 | + testBreadcrumb.category = "sentry.transaction" |
| 91 | + val actual = converter.convert(testBreadcrumb) |
| 92 | + assertEquals(null, actual) |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + fun convertTouchBreadcrumb() { |
| 97 | + val converter = RNSentryReplayBreadcrumbConverter() |
| 98 | + val testBreadcrumb = Breadcrumb() |
| 99 | + testBreadcrumb.level = SentryLevel.INFO |
| 100 | + testBreadcrumb.type = "user" |
| 101 | + testBreadcrumb.category = "touch" |
| 102 | + testBreadcrumb.message = "this won't be used for replay" |
| 103 | + testBreadcrumb.setData( |
| 104 | + "path", |
| 105 | + arrayListOf(mapOf( |
| 106 | + "element" to "element4", |
| 107 | + "file" to "file4"))) |
| 108 | + val actual = converter.convert(testBreadcrumb) as RRWebBreadcrumbEvent |
| 109 | + |
| 110 | + assertRRWebBreadcrumbDefaults(actual) |
| 111 | + assertEquals(SentryLevel.INFO, actual.level) |
| 112 | + assertEquals("ui.tap", actual.category) |
| 113 | + assertEquals(1, actual.data?.keys?.size) |
| 114 | + assertEquals( |
| 115 | + arrayListOf(mapOf( |
| 116 | + "element" to "element4", |
| 117 | + "file" to "file4")), |
| 118 | + actual.data?.get("path")) |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + fun doesNotConvertNullPath() { |
| 123 | + val actual = RNSentryReplayBreadcrumbConverter.getTouchPathMessage(null) |
| 124 | + assertEquals(null, actual) |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + fun doesNotConvertPathContainingNull() { |
| 129 | + val actual = RNSentryReplayBreadcrumbConverter.getTouchPathMessage(arrayListOf(arrayOfNulls<Any>(1))) |
| 130 | + assertEquals(null, actual) |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + fun doesNotConvertPathWithValuesMissingNameAndLevel() { |
| 135 | + val actual = RNSentryReplayBreadcrumbConverter.getTouchPathMessage(arrayListOf(mapOf( |
| 136 | + "element" to "element4", |
| 137 | + "file" to "file4"))) |
| 138 | + assertEquals(null, actual) |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + fun doesConvertValidPathExample1() { |
| 143 | + val actual = RNSentryReplayBreadcrumbConverter.getTouchPathMessage(listOf( |
| 144 | + mapOf("label" to "label0"), |
| 145 | + mapOf("name" to "name1"), |
| 146 | + mapOf("name" to "item2", "label" to "label2"), |
| 147 | + mapOf("name" to "item3", "label" to "label3", "element" to "element3"), |
| 148 | + mapOf("name" to "item4", "label" to "label4", "file" to "file4"), |
| 149 | + mapOf("name" to "item5", "label" to "label5", "element" to "element5", "file" to "file5"))) |
| 150 | + assertEquals("label3(element3) > label2 > name1 > label0", actual) |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + fun doesConvertValidPathExample2() { |
| 155 | + val actual = RNSentryReplayBreadcrumbConverter.getTouchPathMessage(listOf( |
| 156 | + mapOf("name" to "item2", "label" to "label2"), |
| 157 | + mapOf("name" to "item3", "label" to "label3", "element" to "element3"), |
| 158 | + mapOf("name" to "item4", "label" to "label4", "file" to "file4"), |
| 159 | + mapOf("name" to "item5", "label" to "label5", "element" to "element5", "file" to "file5"), |
| 160 | + mapOf("label" to "label6"), |
| 161 | + mapOf("name" to "name7"))) |
| 162 | + assertEquals("label5(element5, file5) > label4(file4) > label3(element3) > label2", actual) |
| 163 | + } |
| 164 | + |
| 165 | + private fun assertRRWebBreadcrumbDefaults(actual: RRWebBreadcrumbEvent) { |
| 166 | + assertEquals("default", actual.breadcrumbType) |
| 167 | + assertEquals(actual.breadcrumbTimestamp * 1000, actual.timestamp.toDouble(), 0.05) |
| 168 | + assert(actual.breadcrumbTimestamp > 0) |
| 169 | + } |
| 170 | +} |
0 commit comments