From ae8c921fb5f0bc0d715699ee737f7671bccfd3a5 Mon Sep 17 00:00:00 2001 From: Andrew McKnight Date: Tue, 23 Aug 2022 11:59:32 -0800 Subject: [PATCH] ref: visibility of functions based on platform Some functions in TestNotificationCenter had no compiled implementation on macOS due to conditionals; instead, move the conditionals outside so the functions themselves aren't compiled at all. Delete a usage that appeared in a macOS-only test case; for all others, gate compilation of the calls. --- .../Session/SentrySessionGeneratorTests.swift | 2 - .../Session/SentrySessionTrackerTests.swift | 38 +++++----- .../Integrations/TestNotificationCenter.swift | 71 +++++++++---------- 3 files changed, 54 insertions(+), 57 deletions(-) diff --git a/Tests/SentryTests/Integrations/Session/SentrySessionGeneratorTests.swift b/Tests/SentryTests/Integrations/Session/SentrySessionGeneratorTests.swift index 8d9c68267a..0e39273b11 100644 --- a/Tests/SentryTests/Integrations/Session/SentrySessionGeneratorTests.swift +++ b/Tests/SentryTests/Integrations/Session/SentrySessionGeneratorTests.swift @@ -156,14 +156,12 @@ class SentrySessionGeneratorTests: XCTestCase { } private func goToForeground(forSeconds: TimeInterval = 0.2) { - TestNotificationCenter.willEnterForeground() TestNotificationCenter.didBecomeActive() delayNonBlocking(timeout: forSeconds) } private func goToBackground(forSeconds: TimeInterval = 0.2) { TestNotificationCenter.willResignActive() - TestNotificationCenter.didEnterBackground() delayNonBlocking(timeout: forSeconds) } diff --git a/Tests/SentryTests/Integrations/Session/SentrySessionTrackerTests.swift b/Tests/SentryTests/Integrations/Session/SentrySessionTrackerTests.swift index 7240958dc3..795b51a1fb 100644 --- a/Tests/SentryTests/Integrations/Session/SentrySessionTrackerTests.swift +++ b/Tests/SentryTests/Integrations/Session/SentrySessionTrackerTests.swift @@ -246,7 +246,9 @@ class SentrySessionTrackerTests: XCTestCase { // Background task is launched advanceTime(bySeconds: 30) +#if os(tvOS) || os(iOS) || targetEnvironment(macCatalyst) TestNotificationCenter.didEnterBackground() +#endif advanceTime(bySeconds: 9) // user opens app @@ -263,7 +265,9 @@ class SentrySessionTrackerTests: XCTestCase { // Background task is launched advanceTime(bySeconds: 1) +#if os(tvOS) || os(iOS) || targetEnvironment(macCatalyst) TestNotificationCenter.didEnterBackground() +#endif advanceTime(bySeconds: 1) // user opens app @@ -349,11 +353,14 @@ class SentrySessionTrackerTests: XCTestCase { } private func goToForeground() { - let _ = expectation(forNotification: UIApplication.willEnterForegroundNotification, object: nil) - let _ = expectation(forNotification: UIApplication.didBecomeActiveNotification, object: nil) + var expectations = [XCTestExpectation]() + expectations.append(expectation(forNotification: TestNotificationCenter.didBecomeActiveNotification, object: nil)) +#if os(tvOS) || os(iOS) || targetEnvironment(macCatalyst) + expectations.append(expectation(forNotification: TestNotificationCenter.willEnterForegroundNotification, object: nil)) TestNotificationCenter.willEnterForeground() +#endif TestNotificationCenter.didBecomeActive() - waitForExpectations(timeout: 3) + wait(for: expectations, timeout: 3) } private func goToBackground(forSeconds: TimeInterval) { @@ -363,35 +370,34 @@ class SentrySessionTrackerTests: XCTestCase { } private func goToBackground() { - let resignActive = expectation(forNotification: UIApplication.willResignActiveNotification, object: nil) - let enterBg = expectation(forNotification: UIApplication.didEnterBackgroundNotification, object: nil) + var expectations = [XCTestExpectation]() + expectations.append(expectation(forNotification: TestNotificationCenter.willResignActiveNotification, object: nil)) TestNotificationCenter.willResignActive() +#if os(tvOS) || os(iOS) || targetEnvironment(macCatalyst) + expectations.append(expectation(forNotification: TestNotificationCenter.didEnterBackgroundNotification, object: nil)) TestNotificationCenter.didEnterBackground() - wait(for: [resignActive, enterBg], timeout: 3) +#endif + wait(for: expectations, timeout: 3) } private func terminateApp() { - let willTerminate = expectation(forNotification: UIApplication.willTerminateNotification, object: nil) + let willTerminate = expectation(forNotification: TestNotificationCenter.willTerminateNotification, object: nil) TestNotificationCenter.willTerminate() wait(for: [willTerminate], timeout: 3) sut.stop() } - - private func resumeAppInBackground() { - let enterBg = expectation(forNotification: UIApplication.didEnterBackgroundNotification, object: nil) - TestNotificationCenter.didEnterBackground() - wait(for: [enterBg], timeout: 3) - } - + private func launchBackgroundTaskAppNotRunning() { sut.stop() fixture.setNewHubToSDK() sut = fixture.getSut() - + sut.start() - let enterBg = expectation(forNotification: UIApplication.didEnterBackgroundNotification, object: nil) +#if os(tvOS) || os(iOS) || targetEnvironment(macCatalyst) + let enterBg = expectation(forNotification: TestNotificationCenter.didEnterBackgroundNotification, object: nil) TestNotificationCenter.didEnterBackground() wait(for: [enterBg], timeout: 3) +#endif } private func captureError() { diff --git a/Tests/SentryTests/Integrations/TestNotificationCenter.swift b/Tests/SentryTests/Integrations/TestNotificationCenter.swift index df7e69941a..f0277ed559 100644 --- a/Tests/SentryTests/Integrations/TestNotificationCenter.swift +++ b/Tests/SentryTests/Integrations/TestNotificationCenter.swift @@ -1,69 +1,62 @@ import Foundation -#if os(tvOS) || os(iOS) +#if os(tvOS) || os(iOS) || targetEnvironment(macCatalyst) import UIKit #endif class TestNotificationCenter { - #if os(tvOS) || os(iOS) - private static let willEnterForegroundNotification = UIApplication.willEnterForegroundNotification - private static let didBecomeActiveNotification = UIApplication.didBecomeActiveNotification - private static let willResignActiveNotification = UIApplication.willResignActiveNotification - private static let didEnterBackgroundNotification = UIApplication.didEnterBackgroundNotification - private static let willTerminateNotification = UIApplication.willTerminateNotification - private static let didFinishLaunchingNotification = UIApplication.didFinishLaunchingNotification + #if os(tvOS) || os(iOS) || targetEnvironment(macCatalyst) + static let willEnterForegroundNotification = UIApplication.willEnterForegroundNotification + static let didBecomeActiveNotification = UIApplication.didBecomeActiveNotification + static let willResignActiveNotification = UIApplication.willResignActiveNotification + static let didEnterBackgroundNotification = UIApplication.didEnterBackgroundNotification + static let willTerminateNotification = UIApplication.willTerminateNotification + static let didFinishLaunchingNotification = UIApplication.didFinishLaunchingNotification #elseif os(macOS) - private static let didBecomeActiveNotification = NSApplication.didBecomeActiveNotification - private static let willResignActiveNotification = NSApplication.willResignActiveNotification - private static let willTerminateNotification = NSApplication.willTerminateNotification - private static let didFinishLaunchingNotification = NSApplication.didFinishLaunchingNotification + static let didBecomeActiveNotification = NSApplication.didBecomeActiveNotification + static let willResignActiveNotification = NSApplication.willResignActiveNotification + static let willTerminateNotification = NSApplication.willTerminateNotification + static let didFinishLaunchingNotification = NSApplication.didFinishLaunchingNotification #endif - static func willEnterForeground() { - #if os(tvOS) || os(iOS) - NotificationCenter.default.post(Notification(name: willEnterForegroundNotification)) - #endif - } +#if os(tvOS) || os(iOS) +#endif +#if os(tvOS) || os(iOS) || os(macOS) static func didBecomeActive() { - #if os(tvOS) || os(iOS) || os(macOS) NotificationCenter.default.post(Notification(name: didBecomeActiveNotification)) - #endif } - + static func willResignActive() { - #if os(tvOS) || os(iOS) || os(macOS) NotificationCenter.default.post(Notification(name: willResignActiveNotification)) - #endif - } - - static func didEnterBackground() { - #if os(tvOS) || os(iOS) - NotificationCenter.default.post(Notification(name: didEnterBackgroundNotification)) - #endif } static func willTerminate() { - #if os(tvOS) || os(iOS) || os(macOS) NotificationCenter.default.post(Notification(name: willTerminateNotification)) - #endif - } - - static func hybridSdkDidBecomeActive() { - NotificationCenter.default.post(name: Notification.Name("SentryHybridSdkDidBecomeActive"), object: nil) } - + static func didFinishLaunching() { - #if os(tvOS) || os(iOS) || os(macOS) NotificationCenter.default.post(Notification(name: didFinishLaunchingNotification)) - #endif + } +#endif + +#if os(tvOS) || os(iOS) || targetEnvironment(macCatalyst) + static func willEnterForeground() { + NotificationCenter.default.post(Notification(name: willEnterForegroundNotification)) + } + + static func didEnterBackground() { + NotificationCenter.default.post(Notification(name: didEnterBackgroundNotification)) } static func uiWindowDidBecomeVisible() { - #if os(tvOS) || os(iOS) || targetEnvironment(macCatalyst) NotificationCenter.default.post(Notification(name: UIWindow.didBecomeVisibleNotification)) - #endif + } +#endif + + static func hybridSdkDidBecomeActive() { + NotificationCenter.default.post(name: Notification.Name("SentryHybridSdkDidBecomeActive"), object: nil) } static func localeDidChange() {