Skip to content

Commit

Permalink
Added more tests, and the whole suit runs successfully
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinrenskers committed Jul 6, 2022
1 parent bc9d991 commit 8ce9385
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
8 changes: 6 additions & 2 deletions Sources/Sentry/SentrySystemEventBreadcrumbs.m
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ - (void)initTimezoneObserver

if (storedTimezoneOffset == nil) {
[self updateStoredTimezone];
} else if (storedTimezoneOffset.doubleValue != [NSTimeZone localTimeZone].secondsFromGMT) {
} else if (storedTimezoneOffset.doubleValue != self.currentDateProvider.timezoneOffset) {
[self timezoneEventTriggered:storedTimezoneOffset];
}

Expand All @@ -228,13 +228,17 @@ - (void)timezoneEventTriggered:(NSNumber *)storedTimezoneOffset
if (storedTimezoneOffset == nil) {
storedTimezoneOffset = [self.fileManager readTimezoneOffset];
}

SentryBreadcrumb *crumb = [[SentryBreadcrumb alloc] initWithLevel:kSentryLevelInfo
category:@"device.event"];

NSInteger offset = self.currentDateProvider.timezoneOffset;

crumb.type = @"system";
crumb.data = @{
@"action" : @"TIMEZONE_CHANGE",
@"previous_seconds_from_gmt" : storedTimezoneOffset,
@"current_seconds_from_gmt" : @(self.currentDateProvider.timezoneOffset)
@"current_seconds_from_gmt" : @(offset)
};
[SentrySDK addBreadcrumb:crumb];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class SentrySystemEventBreadcrumbsTest: XCTestCase {

init() {
options = Options()
options.dsn = TestConstants.dsnAsString(username: "SentrySessionTrackerTests")
options.dsn = TestConstants.dsnAsString(username: "SentrySystemEventBreadcrumbsTest")
options.releaseName = "SentrySessionTrackerIntegrationTests"
options.sessionTrackingIntervalMillis = 10_000
options.environment = "debug"
Expand Down Expand Up @@ -60,6 +60,8 @@ class SentrySystemEventBreadcrumbsTest: XCTestCase {
override func tearDown() {
super.tearDown()
clearTestState()
fixture.fileManager.deleteTimezoneOffset()
sut.stop()
}

func testBatteryLevelBreadcrumb() {
Expand Down Expand Up @@ -228,13 +230,32 @@ class SentrySystemEventBreadcrumbsTest: XCTestCase {
assertBreadcrumbAction(scope: scope, action: "UIApplicationUserDidTakeScreenshotNotification")
}

func testTimezoneChangeBreadcrumb() {
fixture.fileManager.deleteTimezoneOffset()
fixture.currentDateProvider.timezoneOffsetValue = 0
func testTimezoneFirstTimeNilNoBreadcrumb() {
fixture.currentDateProvider.timezoneOffsetValue = 7200

let scope = Scope()
sut = fixture.getSut(scope: scope, currentDevice: nil)

assertNoBreadcrumbAction(scope: scope, action: "TIMEZONE_CHANGE")
}

func testTimezoneChangeInitialBreadcrumb() {
fixture.fileManager.storeTimezoneOffset(0)
fixture.currentDateProvider.timezoneOffsetValue = 7200

let scope = Scope()
sut = fixture.getSut(scope: scope, currentDevice: nil)

assertBreadcrumbAction(scope: scope, action: "TIMEZONE_CHANGE") { data in
XCTAssertEqual(data["previous_seconds_from_gmt"] as? Int, 0)
XCTAssertEqual(data["current_seconds_from_gmt"] as? Int, 7200)
}
}

func testTimezoneChangeNoticiationBreadcrumb() {
let scope = Scope()
sut = fixture.getSut(scope: scope, currentDevice: nil)

fixture.currentDateProvider.timezoneOffsetValue = 7200

NotificationCenter.default.post(Notification(name: NSNotification.Name.NSSystemTimeZoneDidChange))
Expand All @@ -243,7 +264,7 @@ class SentrySystemEventBreadcrumbsTest: XCTestCase {
XCTAssertEqual(data["current_seconds_from_gmt"] as? Int, 7200)
}
}

private func assertBreadcrumbAction(scope: Scope, action: String, checks: (([String: Any]) -> Void)? = nil) {
let ser = scope.serialize()
if let breadcrumbs = ser["breadcrumbs"] as? [[String: Any]] {
Expand All @@ -265,6 +286,15 @@ class SentrySystemEventBreadcrumbsTest: XCTestCase {
XCTFail("no scope.breadcrumbs")
}
}

private func assertNoBreadcrumbAction(scope: Scope, action: String) {
let ser = scope.serialize()
if let breadcrumbs = ser["breadcrumbs"] as? [[String: Any]] {
if let crumb = breadcrumbs.first, let data = crumb["data"] as? [String: Any], data["action"] as? String == action {
XCTFail("unwanted breadcrumb found")
}
}
}

#endif
}

0 comments on commit 8ce9385

Please sign in to comment.