Skip to content

Commit

Permalink
Update tests to assert user notification issuance
Browse files Browse the repository at this point in the history
  • Loading branch information
ps2 committed Jul 26, 2022
1 parent bed1a02 commit 47adaa4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 25 deletions.
4 changes: 2 additions & 2 deletions Loop/Managers/Alerts/AlertManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public final class AlertManager {

public init(alertPresenter: AlertPresenter,
modalAlertIssuer: AlertIssuer? = nil,
userNotificationAlertIssuer: AlertIssuer? = nil,
userNotificationAlertIssuer: AlertIssuer,
fileManager: FileManager = FileManager.default,
alertStore: AlertStore? = nil,
expireAfter: TimeInterval = 24 /* hours */ * 60 /* minutes */ * 60 /* seconds */,
Expand All @@ -73,7 +73,7 @@ public final class AlertManager {
self.alertPresenter = alertPresenter
self.modalAlertIssuer = modalAlertIssuer ?? InAppModalAlertIssuer(alertPresenter: alertPresenter, alertManagerResponder: self)
self.userNotificationAlertIssuer = userNotificationAlertIssuer
handlers = [self.modalAlertIssuer, userNotificationAlertIssuer].compactMap { $0 }
handlers = [self.modalAlertIssuer, userNotificationAlertIssuer]

bluetoothProvider.addBluetoothObserver(self, queue: .main)

Expand Down
66 changes: 43 additions & 23 deletions LoopTests/Managers/Alerts/AlertManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ class AlertManagerTests: XCTestCase {

var mockFileManager: MockFileManager!
var mockPresenter: MockPresenter!
var mockIssuer: MockIssuer!
var mockModalIssuer: MockIssuer!
var mockUserNotificationIssuer: MockIssuer!
var mockAlertStore: MockAlertStore!
var alertManager: AlertManager!
var isInBackground = true
Expand All @@ -160,10 +161,12 @@ class AlertManagerTests: XCTestCase {
override func setUp() {
mockFileManager = MockFileManager()
mockPresenter = MockPresenter()
mockIssuer = MockIssuer()
mockModalIssuer = MockIssuer()
mockUserNotificationIssuer = MockIssuer()
mockAlertStore = MockAlertStore()
alertManager = AlertManager(alertPresenter: mockPresenter,
modalAlertIssuer: mockIssuer,
modalAlertIssuer: mockModalIssuer,
userNotificationAlertIssuer: mockUserNotificationIssuer,
fileManager: mockFileManager,
alertStore: mockAlertStore,
bluetoothProvider: MockBluetoothProvider())
Expand All @@ -175,14 +178,18 @@ class AlertManagerTests: XCTestCase {

func testIssueAlertOnHandlerCalled() {
alertManager.issueAlert(mockAlert)
XCTAssertEqual(mockAlert.identifier, mockIssuer.issuedAlert?.identifier)
XCTAssertNil(mockIssuer.retractedAlertIdentifier)
XCTAssertEqual(mockAlert.identifier, mockModalIssuer.issuedAlert?.identifier)
XCTAssertEqual(mockAlert.identifier, mockUserNotificationIssuer.issuedAlert?.identifier)
XCTAssertNil(mockModalIssuer.retractedAlertIdentifier)
XCTAssertNil(mockUserNotificationIssuer.retractedAlertIdentifier)
}

func testRetractAlertOnHandlerCalled() {
alertManager.retractAlert(identifier: mockAlert.identifier)
XCTAssertNil(mockIssuer.issuedAlert)
XCTAssertEqual(mockAlert.identifier, mockIssuer.retractedAlertIdentifier)
XCTAssertNil(mockModalIssuer.issuedAlert)
XCTAssertNil(mockUserNotificationIssuer.issuedAlert)
XCTAssertEqual(mockAlert.identifier, mockModalIssuer.retractedAlertIdentifier)
XCTAssertEqual(mockAlert.identifier, mockUserNotificationIssuer.retractedAlertIdentifier)
}

func testAlertResponderAcknowledged() {
Expand Down Expand Up @@ -235,12 +242,14 @@ class AlertManagerTests: XCTestCase {
mockAlertStore.storedAlerts = [StoredAlert(from: alert, context: mockAlertStore.managedObjectContext)]

alertManager = AlertManager(alertPresenter: mockPresenter,
modalAlertIssuer: mockIssuer,
modalAlertIssuer: mockModalIssuer,
userNotificationAlertIssuer: mockUserNotificationIssuer,
fileManager: mockFileManager,
alertStore: mockAlertStore,
bluetoothProvider: MockBluetoothProvider())
alertManager.playbackAlertsFromPersistence()
XCTAssertEqual(alert, mockIssuer.issuedAlert)
XCTAssertEqual(alert, mockModalIssuer.issuedAlert)
XCTAssertNil(mockUserNotificationIssuer.issuedAlert)
}
}

Expand All @@ -254,13 +263,15 @@ class AlertManagerTests: XCTestCase {
storedAlert.issuedDate = date
mockAlertStore.storedAlerts = [storedAlert]
alertManager = AlertManager(alertPresenter: mockPresenter,
modalAlertIssuer: mockIssuer,
modalAlertIssuer: mockModalIssuer,
userNotificationAlertIssuer: mockUserNotificationIssuer,
fileManager: mockFileManager,
alertStore: mockAlertStore,
bluetoothProvider: MockBluetoothProvider())
alertManager.playbackAlertsFromPersistence()
let expected = Alert(identifier: Self.mockIdentifier, foregroundContent: content, backgroundContent: content, trigger: .immediate)
XCTAssertEqual(expected, mockIssuer.issuedAlert)
XCTAssertEqual(expected, mockModalIssuer.issuedAlert)
XCTAssertNil(mockUserNotificationIssuer.issuedAlert)
}
}

Expand All @@ -274,7 +285,8 @@ class AlertManagerTests: XCTestCase {
storedAlert.issuedDate = date
mockAlertStore.storedAlerts = [storedAlert]
alertManager = AlertManager(alertPresenter: mockPresenter,
modalAlertIssuer: mockIssuer,
modalAlertIssuer: mockModalIssuer,
userNotificationAlertIssuer: mockUserNotificationIssuer,
fileManager: mockFileManager,
alertStore: mockAlertStore,
bluetoothProvider: MockBluetoothProvider())
Expand All @@ -283,12 +295,12 @@ class AlertManagerTests: XCTestCase {
// The trigger for this should be `.delayed` by "something less than 15 seconds",
// but the exact value depends on the speed of executing this test.
// As long as it is <= 15 seconds, we call it good.
XCTAssertNotNil(mockIssuer.issuedAlert)
switch mockIssuer.issuedAlert?.trigger {
XCTAssertNotNil(mockModalIssuer.issuedAlert)
switch mockModalIssuer.issuedAlert?.trigger {
case .some(.delayed(let interval)):
XCTAssertLessThanOrEqual(interval, 15.0)
default:
XCTFail("Wrong trigger \(String(describing: mockIssuer.issuedAlert?.trigger))")
XCTFail("Wrong trigger \(String(describing: mockModalIssuer.issuedAlert?.trigger))")
}
}
}
Expand All @@ -303,13 +315,15 @@ class AlertManagerTests: XCTestCase {
storedAlert.issuedDate = date
mockAlertStore.storedAlerts = [storedAlert]
alertManager = AlertManager(alertPresenter: mockPresenter,
modalAlertIssuer: mockIssuer,
modalAlertIssuer: mockModalIssuer,
userNotificationAlertIssuer: mockUserNotificationIssuer,
fileManager: mockFileManager,
alertStore: mockAlertStore,
bluetoothProvider: MockBluetoothProvider())
alertManager.playbackAlertsFromPersistence()

XCTAssertEqual(alert, mockIssuer.issuedAlert)
XCTAssertEqual(alert, mockModalIssuer.issuedAlert)
XCTAssertNil(mockUserNotificationIssuer.issuedAlert)
}
}

Expand All @@ -323,7 +337,8 @@ class AlertManagerTests: XCTestCase {
storedAlert.issuedDate = date
mockAlertStore.storedAlerts = [storedAlert]
alertManager = AlertManager(alertPresenter: mockPresenter,
modalAlertIssuer: mockIssuer,
modalAlertIssuer: mockModalIssuer,
userNotificationAlertIssuer: mockUserNotificationIssuer,
fileManager: mockFileManager,
alertStore: mockAlertStore,
bluetoothProvider: MockBluetoothProvider())
Expand All @@ -344,7 +359,8 @@ class AlertManagerTests: XCTestCase {
storedAlert.issuedDate = date
mockAlertStore.storedAlerts = [storedAlert]
alertManager = AlertManager(alertPresenter: mockPresenter,
modalAlertIssuer: mockIssuer,
modalAlertIssuer: mockModalIssuer,
userNotificationAlertIssuer: mockUserNotificationIssuer,
fileManager: mockFileManager,
alertStore: mockAlertStore,
bluetoothProvider: MockBluetoothProvider())
Expand All @@ -365,7 +381,8 @@ class AlertManagerTests: XCTestCase {
storedAlert.issuedDate = date
mockAlertStore.storedAlerts = [storedAlert]
alertManager = AlertManager(alertPresenter: mockPresenter,
modalAlertIssuer: mockIssuer,
modalAlertIssuer: mockModalIssuer,
userNotificationAlertIssuer: mockUserNotificationIssuer,
fileManager: mockFileManager,
alertStore: mockAlertStore,
bluetoothProvider: MockBluetoothProvider())
Expand All @@ -387,7 +404,8 @@ class AlertManagerTests: XCTestCase {
foregroundContent: content, backgroundContent: content, trigger: .repeating(repeatInterval: 60.0))
mockAlertStore.storedAlerts = []
alertManager = AlertManager(alertPresenter: mockPresenter,
modalAlertIssuer: mockIssuer,
modalAlertIssuer: mockModalIssuer,
userNotificationAlertIssuer: mockUserNotificationIssuer,
fileManager: mockFileManager,
alertStore: mockAlertStore,
bluetoothProvider: MockBluetoothProvider())
Expand All @@ -400,11 +418,13 @@ class AlertManagerTests: XCTestCase {

func testScheduleAlertForWorkoutReminder() {
alertManager.presetActivated(context: .legacyWorkout, duration: .indefinite)
XCTAssertEqual(AlertManager.workoutOverrideReminderAlertIdentifier, mockIssuer.issuedAlert?.identifier)
XCTAssertEqual(AlertManager.workoutOverrideReminderAlertIdentifier, mockModalIssuer.issuedAlert?.identifier)
XCTAssertEqual(AlertManager.workoutOverrideReminderAlertIdentifier, mockUserNotificationIssuer.issuedAlert?.identifier)
XCTAssertEqual(AlertManager.workoutOverrideReminderAlertIdentifier, mockAlertStore.issuedAlert?.identifier)

alertManager.presetDeactivated(context: .legacyWorkout)
XCTAssertEqual(AlertManager.workoutOverrideReminderAlertIdentifier, mockIssuer.retractedAlertIdentifier)
XCTAssertEqual(AlertManager.workoutOverrideReminderAlertIdentifier, mockModalIssuer.retractedAlertIdentifier)
XCTAssertEqual(AlertManager.workoutOverrideReminderAlertIdentifier, mockUserNotificationIssuer.retractedAlertIdentifier)
XCTAssertEqual(AlertManager.workoutOverrideReminderAlertIdentifier, mockAlertStore.retractededAlertIdentifier)
}

Expand Down

0 comments on commit 47adaa4

Please sign in to comment.