Skip to content

Commit

Permalink
[BITAU-194] Fix issue with unit tests failing occasionally; Add tests…
Browse files Browse the repository at this point in the history
… for NotifcationCenterService (#1099)
  • Loading branch information
brant-livefront authored Oct 31, 2024
1 parent b4df4c6 commit bc0356a
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ actor DefaultAuthenticatorSyncService: NSObject, AuthenticatorSyncService {
/// The service used by the application to manage account state.
private let stateService: StateService

/// A Task to hold the subscription that waits for sync to be turned on/off.
private var syncSubscriber: Task<Void, Never>?

/// A Task to hold the subscription that waits for the vault to be locked/unlocked..
private var vaultSubscriber: Task<Void, Never>?

/// The service used by the application to manage vault access.
private let vaultTimeoutService: VaultTimeoutService

Expand Down Expand Up @@ -107,6 +113,11 @@ actor DefaultAuthenticatorSyncService: NSObject, AuthenticatorSyncService {
super.init()
}

deinit {
syncSubscriber?.cancel()
vaultSubscriber?.cancel()
}

// MARK: Public Methods

public func getTemporaryTotpItem() async -> AuthenticatorBridgeItemDataView? {
Expand All @@ -130,7 +141,7 @@ actor DefaultAuthenticatorSyncService: NSObject, AuthenticatorSyncService {
return
}

Task {
syncSubscriber = Task {
for await (userId, _) in await self.stateService.syncToAuthenticatorPublisher().values {
guard let userId else { continue }

Expand All @@ -141,7 +152,7 @@ actor DefaultAuthenticatorSyncService: NSObject, AuthenticatorSyncService {
}
}
}
Task {
vaultSubscriber = Task {
for await vaultStatus in await self.vaultTimeoutService.vaultLockStatusPublisher().values {
guard let vaultStatus else { continue }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,33 @@ protocol NotificationCenterService: AnyObject {
/// A default implementation of the `NotificationCenterService` which accesses the app's notification center.
///
class DefaultNotificationCenterService: NotificationCenterService {
// MARK: Properties

/// The NotificationCenter to use in subscribing to notifications.
let notificationCenter: NotificationCenter

// MARK: Initialization

/// Initialize a `DefaultNotificationCenterService`.
///
/// - Parameter notificationCenter: The NotificationCenter to use in subscribing to notifications.
///
init(notificationCenter: NotificationCenter = NotificationCenter.default) {
self.notificationCenter = notificationCenter
}

// MARK: Methods

func didEnterBackgroundPublisher() -> AsyncPublisher<AnyPublisher<Void, Never>> {
NotificationCenter.default
notificationCenter
.publisher(for: UIApplication.didEnterBackgroundNotification)
.map { _ in }
.eraseToAnyPublisher()
.values
}

func willEnterForegroundPublisher() -> AsyncPublisher<AnyPublisher<Void, Never>> {
NotificationCenter.default
notificationCenter
.publisher(for: UIApplication.willEnterForegroundNotification)
.map { _ in }
.eraseToAnyPublisher()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import XCTest

@testable import BitwardenShared

final class NotificationCenterServiceTests: BitwardenTestCase {
// MARK: Properties

var notificationCenter: NotificationCenter!
var subject: DefaultNotificationCenterService!

// MARK: Setup & Teardown

override func setUp() {
notificationCenter = NotificationCenter()
subject = DefaultNotificationCenterService(notificationCenter: notificationCenter)
}

override func tearDown() {
notificationCenter = nil
subject = nil
}

// MARK: Tests

/// `didEnterBackgroundPublisher` publishes a notification when the app enters the background.
func testDidEnterBackgroundPublisher() async throws {
var iterator = subject.didEnterBackgroundPublisher().makeAsyncIterator()
Task {
notificationCenter.post(
name: UIApplication.didEnterBackgroundNotification,
object: nil
)
}
let result: Void? = await iterator.next()

XCTAssertNotNil(result)
}

/// `willEnterForegroundPublisher` publishes a notification when the app will enter the foreground.
func testWillEnterForegroundPublisher() async throws {
var iterator = subject.willEnterForegroundPublisher().makeAsyncIterator()
Task {
notificationCenter.post(
name: UIApplication.willEnterForegroundNotification,
object: nil
)
}
let result: Void? = await iterator.next()

XCTAssertNotNil(result)
}
}

0 comments on commit bc0356a

Please sign in to comment.