Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brustolin committed Nov 2, 2023
1 parent 5007caa commit 75e0ce9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
23 changes: 13 additions & 10 deletions Sources/Sentry/SentryUIApplication.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#import "SentryUIApplication.h"
#import "SentryDependencyContainer.h"
#import "SentryNSNotificationCenterWrapper.h"

#if SENTRY_HAS_UIKIT

Expand All @@ -11,15 +13,16 @@ @implementation SentryUIApplication {
- (instancetype)init
{
if (self = [super init]) {
[NSNotificationCenter.defaultCenter addObserver:self
selector:@selector(didEnterBackground)
name:UIApplicationDidEnterBackgroundNotification
object:nil];

[NSNotificationCenter.defaultCenter addObserver:self
selector:@selector(didBecomeActive)
name:UIApplicationDidBecomeActiveNotification
object:nil];

[SentryDependencyContainer.sharedInstance.notificationCenterWrapper
addObserver:self
selector:@selector(didEnterBackground)
name:UIApplicationDidEnterBackgroundNotification];

[SentryDependencyContainer.sharedInstance.notificationCenterWrapper
addObserver:self
selector:@selector(didBecomeActive)
name:UIApplicationDidBecomeActiveNotification];
// We store the application state when the app is initialized
// and we keep track of its changes by the notifications
// this way we avoid calling sharedApplication in a background thread
Expand All @@ -30,7 +33,7 @@ - (instancetype)init

- (void)dealloc
{
[NSNotificationCenter.defaultCenter removeObserver:self];
[SentryDependencyContainer.sharedInstance.notificationCenterWrapper removeObserver:self];
}

- (UIApplication *)sharedApplication
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import Foundation
import SentryTestUtils

@objcMembers public class TestNSNotificationCenterWrapper: SentryNSNotificationCenterWrapper {

var ignoreRemoveObserver = false

var addObserverInvocations = Invocations<(observer: Any, selector: Selector, name: NSNotification.Name)>()
public var addObserverInvocationsCount: Int {
return addObserverInvocations.count
Expand All @@ -24,6 +27,8 @@ import SentryTestUtils
return removeObserverInvocations.count
}
public override func removeObserver(_ observer: Any) {
removeObserverInvocations.record(observer)
if ignoreRemoveObserver == false {
removeObserverInvocations.record(observer)

Check warning on line 31 in Tests/SentryTests/Helper/TestNSNotificationCenterWrapper.swift

View check run for this annotation

Codecov / codecov/patch

Tests/SentryTests/Helper/TestNSNotificationCenterWrapper.swift#L31

Added line #L31 was not covered by tests
}
}
}
26 changes: 26 additions & 0 deletions Tests/SentryTests/SentryUIApplicationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,32 @@ class SentryUIApplicationTests: XCTestCase {

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

@available(iOS 13.0, tvOS 13.0, *)
func test_ApplicationState() {
let notificationCenter = TestNSNotificationCenterWrapper()
notificationCenter.ignoreRemoveObserver = true
SentryDependencyContainer.sharedInstance().notificationCenterWrapper = notificationCenter

let sut = MockSentryUIApplicationTests()
XCTAssertEqual(sut.applicationState, .active)

notificationCenter.addObserverInvocations.invocations.forEach { (observer: Any, selector: Selector, name: NSNotification.Name) in
if name == UIApplication.didEnterBackgroundNotification {
sut.perform(selector, with: observer)
}
}

XCTAssertEqual(sut.applicationState, .background)

notificationCenter.addObserverInvocations.invocations.forEach { (observer: Any, selector: Selector, name: NSNotification.Name) in
if name == UIApplication.didBecomeActiveNotification {
sut.perform(selector, with: observer)
}
}

XCTAssertEqual(sut.applicationState, .active)
}

private class TestApplicationDelegate: NSObject, UIApplicationDelegate {
var window: UIWindow?
Expand Down

0 comments on commit 75e0ce9

Please sign in to comment.