Skip to content

Commit bcec1bd

Browse files
add android tests
1 parent 4466b6f commit bcec1bd

File tree

5 files changed

+132
-44
lines changed

5 files changed

+132
-44
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package io.sentry.rnsentryandroidtester
2+
3+
import io.sentry.react.RNSentryReplayBreadcrumbConverter
4+
import org.junit.Assert.assertEquals
5+
import org.junit.Test
6+
import org.junit.runner.RunWith
7+
import org.junit.runners.JUnit4
8+
9+
@RunWith(JUnit4::class)
10+
class RNSentryReplayBreadcrumbConverterTest {
11+
12+
@Test
13+
fun doesNotConvertNullPath() {
14+
val actual = RNSentryReplayBreadcrumbConverter.getTouchPathMessage(null)
15+
assertEquals(null, actual)
16+
}
17+
18+
@Test
19+
fun doesNotConvertPathContainingNull() {
20+
val actual = RNSentryReplayBreadcrumbConverter.getTouchPathMessage(arrayListOf(arrayOfNulls<Any>(1)))
21+
assertEquals(null, actual)
22+
}
23+
24+
@Test
25+
fun doesNotConvertPathWithValuesMissingNameAndLevel() {
26+
val actual = RNSentryReplayBreadcrumbConverter.getTouchPathMessage(arrayListOf(mapOf(
27+
"element" to "element4",
28+
"file" to "file4")))
29+
assertEquals(null, actual)
30+
}
31+
32+
@Test
33+
fun doesConvertValidPathExample1() {
34+
val actual = RNSentryReplayBreadcrumbConverter.getTouchPathMessage(listOf(
35+
mapOf("label" to "label0"),
36+
mapOf("name" to "name1"),
37+
mapOf("name" to "item2", "label" to "label2"),
38+
mapOf("name" to "item3", "label" to "label3", "element" to "element3"),
39+
mapOf("name" to "item4", "label" to "label4", "file" to "file4"),
40+
mapOf("name" to "item5", "label" to "label5", "element" to "element5", "file" to "file5")))
41+
assertEquals("label3(element3) > label2 > name1 > label0", actual)
42+
}
43+
44+
@Test
45+
fun doesConvertValidPathExample2() {
46+
val actual = RNSentryReplayBreadcrumbConverter.getTouchPathMessage(listOf(
47+
mapOf("name" to "item2", "label" to "label2"),
48+
mapOf("name" to "item3", "label" to "label3", "element" to "element3"),
49+
mapOf("name" to "item4", "label" to "label4", "file" to "file4"),
50+
mapOf("name" to "item5", "label" to "label5", "element" to "element5", "file" to "file5"),
51+
mapOf("label" to "label6"),
52+
mapOf("name" to "name7")))
53+
assertEquals("label5(element5, file5) > label4(file4) > label3(element3) > label2", actual)
54+
}
55+
}

RNSentryCocoaTester/RNSentryCocoaTesterTests/RNSentryCocoaUnitTesterTests.swift

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,21 @@ import XCTest
33
final class RNSentryCocoaUnitTesterTests: XCTestCase {
44

55
func testTouchMessageReturnsNilOnEmptyArray() throws {
6-
let converter = RNSentryReplayBreadcrumbConverter()
7-
let actual = converter.getTouchPathMessage(from: [])
6+
let actual = RNSentryReplayBreadcrumbConverter.getTouchPathMessage(from: [])
87
XCTAssertEqual(actual, nil);
98
}
109

1110
func testTouchMessageReturnsNilOnNilArray() throws {
12-
let converter = RNSentryReplayBreadcrumbConverter()
13-
let actual = converter.getTouchPathMessage(from: nil as [Any]?)
11+
let actual = RNSentryReplayBreadcrumbConverter.getTouchPathMessage(from: nil as [Any]?)
1412
XCTAssertEqual(actual, nil);
1513
}
16-
17-
func testTouchMessageReturnsNilOnMissingNameAndLavel() throws {
14+
15+
func testTouchMessageReturnsNilOnMissingNameAndLevel() throws {
1816
let testPath: [Any?] = [["element": "element4", "file": "file4"]]
19-
let converter = RNSentryReplayBreadcrumbConverter()
20-
let actual = converter.getTouchPathMessage(from: testPath as [Any])
17+
let actual = RNSentryReplayBreadcrumbConverter.getTouchPathMessage(from: testPath as [Any])
2118
XCTAssertEqual(actual, nil);
2219
}
23-
20+
2421
func testTouchMessageReturnsMessageOnValidPathExample1() throws {
2522
let testPath: [Any?] = [
2623
["label": "label0"],
@@ -30,11 +27,10 @@ final class RNSentryCocoaUnitTesterTests: XCTestCase {
3027
["name": "item4", "label": "label4", "file": "file4"],
3128
["name": "item5", "label": "label5", "element": "element5", "file": "file5"],
3229
]
33-
let converter = RNSentryReplayBreadcrumbConverter()
34-
let actual = converter.getTouchPathMessage(from: testPath as [Any])
30+
let actual = RNSentryReplayBreadcrumbConverter.getTouchPathMessage(from: testPath as [Any])
3531
XCTAssertEqual(actual, "label3(element3) > label2 > name1 > label0");
3632
}
37-
33+
3834
func testTouchMessageReturnsMessageOnValidPathExample2() throws {
3935
let testPath: [Any?] = [
4036
["name": "item2", "label": "label2"],
@@ -44,8 +40,7 @@ final class RNSentryCocoaUnitTesterTests: XCTestCase {
4440
["label": "label6"],
4541
["name": "name7"],
4642
]
47-
let converter = RNSentryReplayBreadcrumbConverter()
48-
let actual = converter.getTouchPathMessage(from: testPath as [Any])
43+
let actual = RNSentryReplayBreadcrumbConverter.getTouchPathMessage(from: testPath as [Any])
4944
XCTAssertEqual(actual, "label5(element5, file5) > label4(file4) > label3(element3) > label2");
5045
}
5146

android/src/main/java/io/sentry/react/RNSentryReplayBreadcrumbConverter.java

Lines changed: 65 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
import io.sentry.rrweb.RRWebBreadcrumbEvent;
77
import io.sentry.rrweb.RRWebSpanEvent;
88

9-
import java.util.ArrayList;
109
import java.util.HashMap;
1110
import org.jetbrains.annotations.NotNull;
1211
import org.jetbrains.annotations.Nullable;
1312
import org.jetbrains.annotations.TestOnly;
1413

15-
import java.util.HashMap;
14+
import java.util.List;
15+
import java.util.Map;
1616

1717
public final class RNSentryReplayBreadcrumbConverter extends DefaultReplayBreadcrumbConverter {
1818
public RNSentryReplayBreadcrumbConverter() {
@@ -59,31 +59,9 @@ public RNSentryReplayBreadcrumbConverter() {
5959
final RRWebBreadcrumbEvent rrWebBreadcrumb = new RRWebBreadcrumbEvent();
6060

6161
rrWebBreadcrumb.setCategory("ui.tap");
62-
ArrayList path = (ArrayList) breadcrumb.getData("path");
63-
if (path != null) {
64-
StringBuilder message = new StringBuilder();
65-
for (int i = Math.min(3, path.size()); i >= 0; i--) {
66-
HashMap item = (HashMap) path.get(i);
67-
message.append(item.get("name"));
68-
if (item.containsKey("element") || item.containsKey("file")) {
69-
message.append('(');
70-
if (item.containsKey("element")) {
71-
message.append(item.get("element"));
72-
if (item.containsKey("file")) {
73-
message.append(", ");
74-
message.append(item.get("file"));
75-
}
76-
} else if (item.containsKey("file")) {
77-
message.append(item.get("file"));
78-
}
79-
message.append(')');
80-
}
81-
if (i > 0) {
82-
message.append(" > ");
83-
}
84-
}
85-
rrWebBreadcrumb.setMessage(message.toString());
86-
}
62+
63+
rrWebBreadcrumb.setMessage(RNSentryReplayBreadcrumbConverter
64+
.getTouchPathMessage(breadcrumb.getData("path")));
8765

8866
rrWebBreadcrumb.setLevel(breadcrumb.getLevel());
8967
rrWebBreadcrumb.setData(breadcrumb.getData());
@@ -93,6 +71,66 @@ public RNSentryReplayBreadcrumbConverter() {
9371
return rrWebBreadcrumb;
9472
}
9573

74+
@TestOnly
75+
public static @Nullable String getTouchPathMessage(final @Nullable Object maybePath) {
76+
if (!(maybePath instanceof List)) {
77+
return null;
78+
}
79+
80+
final @NotNull List path = (List) maybePath;
81+
if (path.size() == 0) {
82+
return null;
83+
}
84+
85+
final @NotNull StringBuilder message = new StringBuilder();
86+
for (int i = Math.min(3, path.size() - 1); i >= 0; i--) {
87+
final @Nullable Object maybeItem = path.get(i);
88+
if (!(maybeItem instanceof Map)) {
89+
return null;
90+
}
91+
92+
final @NotNull Map item = (Map) maybeItem;
93+
final @Nullable Object maybeName = item.get("name");
94+
final @Nullable Object maybeLabel = item.get("label");
95+
boolean hasName = maybeName instanceof String;
96+
boolean hasLabel = maybeLabel instanceof String;
97+
if (!hasName && !hasLabel) {
98+
return null; // This again should never be allowed in JS, but to be safe we check it here
99+
}
100+
if (hasLabel) {
101+
message.append(maybeLabel);
102+
} else { // hasName is true
103+
message.append(maybeName);
104+
}
105+
106+
final @Nullable Object maybeElement = item.get("element");
107+
final @Nullable Object maybeFile = item.get("file");
108+
boolean hasElement = maybeElement instanceof String;
109+
boolean hasFile = maybeFile instanceof String;
110+
if (hasElement && hasFile) {
111+
message.append('(')
112+
.append(maybeElement)
113+
.append(", ")
114+
.append(maybeFile)
115+
.append(')');
116+
} else if (hasElement) {
117+
message.append('(')
118+
.append(maybeElement)
119+
.append(')');
120+
} else if (hasFile) {
121+
message.append('(')
122+
.append(maybeFile)
123+
.append(')');
124+
}
125+
126+
if (i > 0) {
127+
message.append(" > ");
128+
}
129+
}
130+
131+
return message.toString();
132+
}
133+
96134
@TestOnly
97135
public @Nullable RRWebEvent convertNetworkBreadcrumb(final @NotNull Breadcrumb breadcrumb) {
98136
final Double startTimestamp = breadcrumb.getData("start_timestamp") instanceof Number

ios/RNSentryReplayBreadcrumbConverter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
- (instancetype _Nonnull)init;
1010

11-
- (NSString* _Nullable) getTouchPathMessageFrom:(NSArray* _Nullable) path;
11+
+ (NSString* _Nullable) getTouchPathMessageFrom:(NSArray* _Nullable) path;
1212

1313
@end
1414
#endif

ios/RNSentryReplayBreadcrumbConverter.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ - (instancetype _Nonnull)init {
6767
}
6868

6969
NSMutableArray *path = [breadcrumb.data valueForKey:@"path"];
70-
NSString* message = [self getTouchPathMessageFrom:path];
70+
NSString* message = [RNSentryReplayBreadcrumbConverter getTouchPathMessageFrom:path];
7171

7272
return [SentrySessionReplayIntegration
7373
createBreadcrumbwithTimestamp:breadcrumb.timestamp
@@ -77,7 +77,7 @@ - (instancetype _Nonnull)init {
7777
data:breadcrumb.data];
7878
}
7979

80-
- (NSString* _Nullable) getTouchPathMessageFrom:(NSArray* _Nullable) path {
80+
+ (NSString* _Nullable) getTouchPathMessageFrom:(NSArray* _Nullable) path {
8181
if (path == nil) {
8282
return nil;
8383
}

0 commit comments

Comments
 (0)