From e8c8a05b3d51478d65704fbc5ac652c96eabbe16 Mon Sep 17 00:00:00 2001 From: Krystof Woldrich <31292499+krystofwoldrich@users.noreply.github.com> Date: Wed, 24 Jul 2024 04:12:24 -0700 Subject: [PATCH] fix(sdk): Collect only unique UIWindow references (#4159) When scene and app delegate holds reference to the same windows, it's added twice to the array. --- CHANGELOG.md | 1 + Sources/Sentry/SentryUIApplication.m | 4 +- .../SentryUIApplicationTests.swift | 37 +++++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 119d41afd8e..53dddfd5e06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ ### Fixes - Session replay crash when writing the replay (#4186) +- Collect only unique UIWindow references (#4159) ### Deprecated diff --git a/Sources/Sentry/SentryUIApplication.m b/Sources/Sentry/SentryUIApplication.m index 7e660b87d87..cfc1c951634 100644 --- a/Sources/Sentry/SentryUIApplication.m +++ b/Sources/Sentry/SentryUIApplication.m @@ -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 *scenes = [self getApplicationConnectedScenes:app]; @@ -91,7 +91,7 @@ - (UIApplication *)sharedApplication [result addObject:appDelegate.window]; } - windows = result; + windows = [result allObjects]; } timeout:0.01]; return windows ?: @[]; diff --git a/Tests/SentryTests/SentryUIApplicationTests.swift b/Tests/SentryTests/SentryUIApplicationTests.swift index 51341247ca5..d7e68733c25 100644 --- a/Tests/SentryTests/SentryUIApplicationTests.swift +++ b/Tests/SentryTests/SentryUIApplicationTests.swift @@ -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() {