Skip to content

Commit

Permalink
ref: visibility of functions based on platform
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
armcknight committed Aug 23, 2022
1 parent edd4706 commit ae8c921
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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() {
Expand Down
71 changes: 32 additions & 39 deletions Tests/SentryTests/Integrations/TestNotificationCenter.swift
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down

0 comments on commit ae8c921

Please sign in to comment.