Skip to content

Commit

Permalink
fix(sdk): Collect only unique UIWindow references (#4159)
Browse files Browse the repository at this point in the history
When scene and app delegate holds reference to the same windows, it's added twice to the array.
  • Loading branch information
krystofwoldrich authored Jul 24, 2024
1 parent bb71736 commit e8c8a05
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Fixes

- Session replay crash when writing the replay (#4186)
- Collect only unique UIWindow references (#4159)

### Deprecated

Expand Down
4 changes: 2 additions & 2 deletions Sources/Sentry/SentryUIApplication.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ - (UIApplication *)sharedApplication
[SentryDependencyContainer.sharedInstance.dispatchQueueWrapper
dispatchSyncOnMainQueue:^{
UIApplication *app = [self sharedApplication];
NSMutableArray *result = [NSMutableArray array];
NSMutableSet *result = [NSMutableSet set];

if (@available(iOS 13.0, tvOS 13.0, *)) {
NSArray<UIScene *> *scenes = [self getApplicationConnectedScenes:app];
Expand All @@ -91,7 +91,7 @@ - (UIApplication *)sharedApplication
[result addObject:appDelegate.window];
}

windows = result;
windows = [result allObjects];
}
timeout:0.01];
return windows ?: @[];
Expand Down
37 changes: 37 additions & 0 deletions Tests/SentryTests/SentryUIApplicationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,43 @@ class SentryUIApplicationTests: XCTestCase {
XCTAssertEqual(sut.windows?.count, 1)
}

//Somehow this is running under iOS 12 and is breaking the test. Disabling it.
@available(iOS 13.0, tvOS 13.0, *)
func test_applicationWithScenesAndDelegateWithWindow_Unique() {
let sceneDelegate = TestUISceneDelegate()
sceneDelegate.window = UIWindow()
let scene1 = MockUIScene()
scene1.delegate = sceneDelegate

let delegate = TestApplicationDelegate()
delegate.window = UIWindow()

let sut = MockSentryUIApplicationTests()
sut.scenes = [scene1]
sut.appDelegate = delegate

XCTAssertEqual(sut.windows?.count, 2)
}

//Somehow this is running under iOS 12 and is breaking the test. Disabling it.
@available(iOS 13.0, tvOS 13.0, *)
func test_applicationWithScenesAndDelegateWithWindow_Same() {
let window = UIWindow()
let sceneDelegate = TestUISceneDelegate()
sceneDelegate.window = window
let scene1 = MockUIScene()
scene1.delegate = sceneDelegate

let delegate = TestApplicationDelegate()
delegate.window = window

let sut = MockSentryUIApplicationTests()
sut.scenes = [scene1]
sut.appDelegate = delegate

XCTAssertEqual(sut.windows?.count, 1)
}

//Somehow this is running under iOS 12 and is breaking the test. Disabling it.
@available(iOS 13.0, tvOS 13.0, *)
func test_applicationWithScenes_noWindow() {
Expand Down

0 comments on commit e8c8a05

Please sign in to comment.