Check Application state before sending RR (#1960)

* check the state

* better handling

* protocol to mock the application state

* test improvement
This commit is contained in:
Mauro
2023-10-25 19:16:37 +02:00
committed by GitHub
parent af76be2426
commit d457736673
14 changed files with 235 additions and 106 deletions
+8 -72
View File
@@ -35,7 +35,7 @@ class BackgroundTaskTests: XCTestCase {
func testInitAndStop() {
let service = UIKitBackgroundTaskService {
UIApplication.mockHealty
ApplicationMock.default
}
guard let task = service.startBackgroundTask(withName: Constants.bgTaskName) else {
XCTFail("Failed to setup test conditions")
@@ -53,7 +53,7 @@ class BackgroundTaskTests: XCTestCase {
func testNotReusableInit() {
let service = UIKitBackgroundTaskService {
UIApplication.mockHealty
ApplicationMock.default
}
// create two not reusable task with the same name
@@ -70,7 +70,7 @@ class BackgroundTaskTests: XCTestCase {
func testReusableInit() {
let service = UIKitBackgroundTaskService {
UIApplication.mockHealty
ApplicationMock.default
}
// create two reusable task with the same name
@@ -91,7 +91,7 @@ class BackgroundTaskTests: XCTestCase {
func testMultipleStops() {
let service = UIKitBackgroundTaskService {
UIApplication.mockHealty
ApplicationMock.default
}
// create two reusable task with the same name
@@ -114,7 +114,7 @@ class BackgroundTaskTests: XCTestCase {
func testNotValidReuse() {
let service = UIKitBackgroundTaskService {
UIApplication.mockHealty
ApplicationMock.default
}
// create two reusable task with the same name
@@ -136,7 +136,7 @@ class BackgroundTaskTests: XCTestCase {
func testValidReuse() {
let service = UIKitBackgroundTaskService {
UIApplication.mockHealty
ApplicationMock.default
}
// create two reusable task with the same name
@@ -162,7 +162,7 @@ class BackgroundTaskTests: XCTestCase {
func testBrokenApp() {
let service = UIKitBackgroundTaskService {
UIApplication.mockBroken
ApplicationMock.mockBroken
}
// create two reusable task with the same name
@@ -173,7 +173,7 @@ class BackgroundTaskTests: XCTestCase {
func testNoTimeApp() {
let service = UIKitBackgroundTaskService {
UIApplication.mockAboutToSuspend
ApplicationMock.mockAboutToSuspend
}
// create two reusable task with the same name
@@ -182,67 +182,3 @@ class BackgroundTaskTests: XCTestCase {
XCTAssertNil(task, "Task should not be created")
}
}
private extension UIApplication {
static var mockHealty: ApplicationProtocol {
MockApplication()
}
static var mockBroken: ApplicationProtocol {
MockApplication(withState: .inactive,
backgroundTimeRemaining: 0,
allowTasks: false)
}
static var mockAboutToSuspend: ApplicationProtocol {
MockApplication(withState: .background,
backgroundTimeRemaining: 2,
allowTasks: false)
}
}
private class MockApplication: ApplicationProtocol {
let applicationState: UIApplication.State
let backgroundTimeRemaining: TimeInterval
private let allowTasks: Bool
init(withState applicationState: UIApplication.State = .active,
backgroundTimeRemaining: TimeInterval = 10,
allowTasks: Bool = true) {
self.applicationState = applicationState
self.backgroundTimeRemaining = backgroundTimeRemaining
self.allowTasks = allowTasks
}
private static var bgTaskIdentifier = 0
private var bgTasks: [UIBackgroundTaskIdentifier: Bool] = [:]
func beginBackgroundTask(expirationHandler handler: (() -> Void)?) -> UIBackgroundTaskIdentifier {
guard allowTasks else {
return .invalid
}
return beginBackgroundTask(withName: nil, expirationHandler: handler)
}
func beginBackgroundTask(withName taskName: String?, expirationHandler handler: (() -> Void)?) -> UIBackgroundTaskIdentifier {
guard allowTasks else {
return .invalid
}
Self.bgTaskIdentifier += 1
let identifier = UIBackgroundTaskIdentifier(rawValue: Self.bgTaskIdentifier)
bgTasks[identifier] = true
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
handler?()
}
return identifier
}
func endBackgroundTask(_ identifier: UIBackgroundTaskIdentifier) {
guard allowTasks else {
return
}
bgTasks.removeValue(forKey: identifier)
}
}
+6 -3
View File
@@ -32,7 +32,8 @@ class PillContextTests: XCTestCase {
roomProxy: proxyMock,
appSettings: ServiceLocator.shared.settings,
analytics: ServiceLocator.shared.analytics,
userIndicatorController: ServiceLocator.shared.userIndicatorController)
userIndicatorController: ServiceLocator.shared.userIndicatorController,
application: ApplicationMock.default)
let context = PillContext(roomContext: mock.context, data: PillTextAttachmentData(type: .user(userID: id), font: .preferredFont(forTextStyle: .body)))
XCTAssertFalse(context.viewState.isOwnMention)
@@ -64,7 +65,8 @@ class PillContextTests: XCTestCase {
roomProxy: proxyMock,
appSettings: ServiceLocator.shared.settings,
analytics: ServiceLocator.shared.analytics,
userIndicatorController: ServiceLocator.shared.userIndicatorController)
userIndicatorController: ServiceLocator.shared.userIndicatorController,
application: ApplicationMock.default)
let context = PillContext(roomContext: mock.context, data: PillTextAttachmentData(type: .user(userID: id), font: .preferredFont(forTextStyle: .body)))
XCTAssertTrue(context.viewState.isOwnMention)
@@ -83,7 +85,8 @@ class PillContextTests: XCTestCase {
roomProxy: proxyMock,
appSettings: ServiceLocator.shared.settings,
analytics: ServiceLocator.shared.analytics,
userIndicatorController: ServiceLocator.shared.userIndicatorController)
userIndicatorController: ServiceLocator.shared.userIndicatorController,
application: ApplicationMock.default)
let context = PillContext(roomContext: mock.context, data: PillTextAttachmentData(type: .allUsers, font: .preferredFont(forTextStyle: .body)))
XCTAssertTrue(context.viewState.isOwnMention)
@@ -55,7 +55,8 @@ class RoomScreenViewModelTests: XCTestCase {
roomProxy: RoomProxyMock(with: .init(displayName: "")),
appSettings: ServiceLocator.shared.settings,
analytics: ServiceLocator.shared.analytics,
userIndicatorController: userIndicatorControllerMock)
userIndicatorController: userIndicatorControllerMock,
application: ApplicationMock.default)
// Then the messages should be grouped together.
XCTAssertEqual(viewModel.state.timelineViewState.itemViewStates[0].groupStyle, .first, "Nothing should prevent the first message from being grouped.")
@@ -89,7 +90,8 @@ class RoomScreenViewModelTests: XCTestCase {
roomProxy: RoomProxyMock(with: .init(displayName: "")),
appSettings: ServiceLocator.shared.settings,
analytics: ServiceLocator.shared.analytics,
userIndicatorController: userIndicatorControllerMock)
userIndicatorController: userIndicatorControllerMock,
application: ApplicationMock.default)
// Then the messages should be grouped by sender.
XCTAssertEqual(viewModel.state.timelineViewState.itemViewStates[0].groupStyle, .single, "A message should not be grouped when the sender changes.")
@@ -121,7 +123,8 @@ class RoomScreenViewModelTests: XCTestCase {
roomProxy: RoomProxyMock(with: .init(displayName: "")),
appSettings: ServiceLocator.shared.settings,
analytics: ServiceLocator.shared.analytics,
userIndicatorController: userIndicatorControllerMock)
userIndicatorController: userIndicatorControllerMock,
application: ApplicationMock.default)
// Then the first message should not be grouped but the other two should.
XCTAssertEqual(viewModel.state.timelineViewState.itemViewStates[0].groupStyle, .single, "When the first message has reactions it should not be grouped.")
@@ -150,7 +153,8 @@ class RoomScreenViewModelTests: XCTestCase {
roomProxy: RoomProxyMock(with: .init(displayName: "")),
appSettings: ServiceLocator.shared.settings,
analytics: ServiceLocator.shared.analytics,
userIndicatorController: userIndicatorControllerMock)
userIndicatorController: userIndicatorControllerMock,
application: ApplicationMock.default)
// Then the first and second messages should be grouped and the last one should not.
XCTAssertEqual(viewModel.state.timelineViewState.itemViewStates[0].groupStyle, .first, "Nothing should prevent the first message from being grouped.")
@@ -179,7 +183,8 @@ class RoomScreenViewModelTests: XCTestCase {
roomProxy: RoomProxyMock(with: .init(displayName: "")),
appSettings: ServiceLocator.shared.settings,
analytics: ServiceLocator.shared.analytics,
userIndicatorController: userIndicatorControllerMock)
userIndicatorController: userIndicatorControllerMock,
application: ApplicationMock.default)
// Then the messages should be grouped together.
XCTAssertEqual(viewModel.state.timelineViewState.itemViewStates[0].groupStyle, .first, "Nothing should prevent the first message from being grouped.")
@@ -204,7 +209,8 @@ class RoomScreenViewModelTests: XCTestCase {
roomProxy: roomProxyMock,
appSettings: ServiceLocator.shared.settings,
analytics: ServiceLocator.shared.analytics,
userIndicatorController: userIndicatorControllerMock)
userIndicatorController: userIndicatorControllerMock,
application: ApplicationMock.default)
viewModel.actions
.sink { action in
switch action {
@@ -243,7 +249,8 @@ class RoomScreenViewModelTests: XCTestCase {
roomProxy: roomProxyMock,
appSettings: ServiceLocator.shared.settings,
analytics: ServiceLocator.shared.analytics,
userIndicatorController: userIndicatorControllerMock)
userIndicatorController: userIndicatorControllerMock,
application: ApplicationMock.default)
viewModel.actions
.sink { action in
@@ -283,7 +290,8 @@ class RoomScreenViewModelTests: XCTestCase {
roomProxy: roomProxyMock,
appSettings: ServiceLocator.shared.settings,
analytics: ServiceLocator.shared.analytics,
userIndicatorController: userIndicatorControllerMock)
userIndicatorController: userIndicatorControllerMock,
application: ApplicationMock.default)
viewModel.actions
.sink { _ in
XCTFail("Should not receive any action")
@@ -315,7 +323,8 @@ class RoomScreenViewModelTests: XCTestCase {
roomProxy: roomProxyMock,
appSettings: ServiceLocator.shared.settings,
analytics: ServiceLocator.shared.analytics,
userIndicatorController: userIndicatorControllerMock)
userIndicatorController: userIndicatorControllerMock,
application: ApplicationMock.default)
viewModel.context.send(viewAction: .retrySend(itemID: .init(timelineID: UUID().uuidString, transactionID: "test retry send id")))
@@ -335,7 +344,8 @@ class RoomScreenViewModelTests: XCTestCase {
roomProxy: roomProxyMock,
appSettings: ServiceLocator.shared.settings,
analytics: ServiceLocator.shared.analytics,
userIndicatorController: userIndicatorControllerMock)
userIndicatorController: userIndicatorControllerMock,
application: ApplicationMock.default)
viewModel.context.send(viewAction: .retrySend(itemID: .random))
@@ -354,7 +364,8 @@ class RoomScreenViewModelTests: XCTestCase {
roomProxy: roomProxyMock,
appSettings: ServiceLocator.shared.settings,
analytics: ServiceLocator.shared.analytics,
userIndicatorController: userIndicatorControllerMock)
userIndicatorController: userIndicatorControllerMock,
application: ApplicationMock.default)
viewModel.context.send(viewAction: .cancelSend(itemID: .init(timelineID: UUID().uuidString, transactionID: "test cancel send id")))
@@ -374,7 +385,8 @@ class RoomScreenViewModelTests: XCTestCase {
roomProxy: roomProxyMock,
appSettings: ServiceLocator.shared.settings,
analytics: ServiceLocator.shared.analytics,
userIndicatorController: userIndicatorControllerMock)
userIndicatorController: userIndicatorControllerMock,
application: ApplicationMock.default)
viewModel.context.send(viewAction: .cancelSend(itemID: .random))
@@ -512,6 +524,7 @@ class RoomScreenViewModelTests: XCTestCase {
appSettings: ServiceLocator.shared.settings,
analytics: ServiceLocator.shared.analytics,
userIndicatorController: userIndicatorControllerMock,
application: ApplicationMock.default,
notificationCenterProtocol: notificationCenter)
return (viewModel, roomProxy, timelineController, notificationCenter)