Skip to content

Commit

Permalink
internal(hybrid): Add Replay Mask options to dict init (#4492)
Browse files Browse the repository at this point in the history
  • Loading branch information
krystofwoldrich authored Oct 31, 2024
1 parent e92acb3 commit 7cddee1
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- Improve frames tracker performance (#4469)
- Log a warning when dropping envelopes due to rate-limiting (#4463)
- Expose `SentrySessionReplayIntegration-Hybrid.h` as `private` (#4486)
- Add `maskedViewClasses` and `unmaskedViewClasses` to SentryReplayOptions init via dict (#4492)

## 8.39.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,11 @@ public class SentryReplayOptions: NSObject, SentryRedactOptions {
let maskAllText = (dictionary["maskAllText"] as? NSNumber)?.boolValue ?? true
let maskAllImages = (dictionary["maskAllImages"] as? NSNumber)?.boolValue ?? true
self.init(sessionSampleRate: sessionSampleRate, onErrorSampleRate: onErrorSampleRate, maskAllText: maskAllText, maskAllImages: maskAllImages)
self.maskedViewClasses = ((dictionary["maskedViewClasses"] as? NSArray) ?? []).compactMap({ element in
NSClassFromString((element as? String) ?? "")
})
self.unmaskedViewClasses = ((dictionary["unmaskedViewClasses"] as? NSArray) ?? []).compactMap({ element in
NSClassFromString((element as? String) ?? "")
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,155 @@ class SentryReplayOptionsTests: XCTestCase {
XCTAssertEqual(options.replayBitRate, 60_000)
XCTAssertEqual(options.sizeScale, 1.0)
}


func testInitFromDictOnErrorSampleRateAsDouble() {
let options = SentryReplayOptions(dictionary: [
"errorSampleRate": 0.44
])

XCTAssertEqual(options.onErrorSampleRate, 0.44)
}

func testInitFromDictOnErrorSampleRateMissing() {
let options = SentryReplayOptions(dictionary: [:])

XCTAssertEqual(options.onErrorSampleRate, 0)
}

func testInitFromDictOnErrorSampleRateAsString() {
let options = SentryReplayOptions(dictionary: [
"onErrorSampleRate": "0.44"
])

XCTAssertEqual(options.onErrorSampleRate, 0)
}

func testInitFromDictSessionSampleRateAsDouble() {
let options = SentryReplayOptions(dictionary: [
"sessionSampleRate": 0.44
])

XCTAssertEqual(options.sessionSampleRate, 0.44)
}

func testInitFromDictSessionSampleRateMissing() {
let options = SentryReplayOptions(dictionary: [:])

XCTAssertEqual(options.sessionSampleRate, 0)
}

func testInitFromDictSessionSampleRateAsString() {
let options = SentryReplayOptions(dictionary: [
"sessionSampleRate": "0.44"
])

XCTAssertEqual(options.sessionSampleRate, 0)
}

func testInitFromDictMaskedViewClasses() {
let options = SentryReplayOptions(dictionary: [
"maskedViewClasses": ["NSString"]
])

XCTAssertEqual(options.maskedViewClasses.count, 1)
XCTAssertEqual(ObjectIdentifier(options.maskedViewClasses.first!), ObjectIdentifier(NSString.self))
}

func testInitFromDictMaskedViewClassesAsString() {
let options = SentryReplayOptions(dictionary: [
"maskedViewClasses": "ExampleView1"
])

XCTAssertEqual(options.maskedViewClasses.count, 0)
}

func testInitFromDictMaskedViewClassesWithNumber() {
let options = SentryReplayOptions(dictionary: [
"maskedViewClasses": [123]
])

XCTAssertEqual(options.maskedViewClasses.count, 0)
}

func testInitFromDictUnmaskedViewClasses() {
let options = SentryReplayOptions(dictionary: [
"unmaskedViewClasses": ["NSString"]
])

XCTAssertEqual(options.unmaskedViewClasses.count, 1)
XCTAssertEqual(ObjectIdentifier(options.unmaskedViewClasses.first!), ObjectIdentifier(NSString.self))
}

func testInitFromDictUnmaskedViewClassesAsString() {
let options = SentryReplayOptions(dictionary: [
"unmaskedViewClasses": "invalid_value"
])

XCTAssertEqual(options.unmaskedViewClasses.count, 0)
}

func testInitFromDictUnmaskedViewClassesWithInvalidValues() {
let options = SentryReplayOptions(dictionary: [
"unmaskedViewClasses": [123, "not.class"] as [Any]
])

XCTAssertEqual(options.unmaskedViewClasses.count, 0)
}

func testInitFromDictMaskAllTextWithBool() {
let options = SentryReplayOptions(dictionary: [
"maskAllText": true
])
XCTAssertTrue(options.maskAllText)

let options2 = SentryReplayOptions(dictionary: [
"maskAllText": false
])
XCTAssertFalse(options2.maskAllText)
}

func testInitFromDictMaskAllTextWithString() {
let options = SentryReplayOptions(dictionary: [
"maskAllText": "invalid_value"
])
XCTAssertTrue(options.maskAllText)
}

func testInitFromDictMaskAllImagesWithBool() {
let options = SentryReplayOptions(dictionary: [
"maskAllImages": true
])
XCTAssertTrue(options.maskAllImages)

let options2 = SentryReplayOptions(dictionary: [
"maskAllImages": false
])
XCTAssertFalse(options2.maskAllImages)
}

func testInitFromDictMaskAllImagesWithString() {
let options = SentryReplayOptions(dictionary: [
"maskAllImages": "invalid_value"
])
XCTAssertTrue(options.maskAllImages)
}

func testInitFromDictWithMultipleOptions() {
let options = SentryReplayOptions(dictionary: [
"sessionSampleRate": 0.5,
"errorSampleRate": 0.8,
"maskAllText": false,
"maskedViewClasses": ["NSString", "not.a.class", 123] as [Any],
"unmaskedViewClasses": ["NSNumber", "invalid", true] as [Any]
])

XCTAssertEqual(options.sessionSampleRate, 0.5)
XCTAssertEqual(options.onErrorSampleRate, 0.8)
XCTAssertFalse(options.maskAllText)
XCTAssertTrue(options.maskAllImages)
XCTAssertEqual(options.maskedViewClasses.count, 1)
XCTAssertEqual(ObjectIdentifier(options.maskedViewClasses.first!), ObjectIdentifier(NSString.self))
XCTAssertEqual(options.unmaskedViewClasses.count, 1)
XCTAssertEqual(ObjectIdentifier(options.unmaskedViewClasses.first!), ObjectIdentifier(NSNumber.self))
}
}

0 comments on commit 7cddee1

Please sign in to comment.