mirror of
https://github.com/home-assistant/iOS.git
synced 2026-06-16 13:26:27 -05:00
Fixes #1270. Sends a webhook update with the active status as 'off' when the app is terminating. This doesn't work if, of course, the app crashes or is force quit. In a normal Mac app, to do something when Termination is about to happen, you tell the system to hold on a sec, you do the thing, and then you tell the system you're done. Easy, right? Well, we're a Catalyst app, so no: it definitely is not that easy. Why should we have access to an `NSApplicationDelegate` instance to do this exchange? After quite a bit of investigation, I'm reasonably sure that `NSUIApplicationDelegate` does termination like so: if there are any existing `UIApplication` background tasks, delay termination, wait for them to complete, then complete termination. The trick then becomes trying to get a background task started before termination starts. However, there's a problem here: the `UIApplication.willTerminateNotification` will only fire when it's about to respond to the "terminate later" with "GO!" This means we need to get in there sooner. However, there's nothing else that we can access publicly that happens sooner. In fact, there isn't even a private notification that fires for this case that we could piggyback off of, not that that's likely to be stable going forward. One path that seemed hopeful was the `UIApplication.willResignActiveNotification` which does fire at the right time. However, it also fires when Hiding the app, so we can't use it as a "termination is gonna happen" indicator since we don't want to set active status to 'off' incorrectly. In the end, the easiest route I could get working is to swizzle the app delegate and when we're asked whether we should do a normal termination and use this to kick off the background task. Our teed-up background task for the webhook update, live-signaled by the active status changing, both prevents the termination and does the update. In my testing this works for every mechanism of clean termination I could think of: Quit menu item, "Quit" from Activity Monitor, system shutdown.
215 lines
7.8 KiB
Swift
215 lines
7.8 KiB
Swift
import Foundation
|
|
@testable import Shared
|
|
import XCTest
|
|
|
|
class ActiveStateManagerTests: XCTestCase {
|
|
var manager: ActiveStateManager!
|
|
var observer: MockActiveStateObserver!
|
|
var notificationCenter: NotificationCenter!
|
|
|
|
override func setUp() {
|
|
// pretend like we're in catalyst for these tests
|
|
Current.isCatalyst = true
|
|
|
|
notificationCenter = NotificationCenter.default
|
|
observer = MockActiveStateObserver()
|
|
manager = ActiveStateManager()
|
|
|
|
manager.register(observer: observer)
|
|
|
|
super.setUp()
|
|
}
|
|
|
|
override func tearDown() {
|
|
Current.isCatalyst = false
|
|
|
|
super.tearDown()
|
|
}
|
|
|
|
func testInitialStateIsActive() {
|
|
XCTAssertTrue(manager.canTrackActiveStatus)
|
|
XCTAssertTrue(manager.isActive)
|
|
XCTAssertFalse(manager.states.isFastUserSwitched)
|
|
XCTAssertFalse(manager.states.isIdle)
|
|
XCTAssertFalse(manager.states.isLocked)
|
|
XCTAssertFalse(manager.states.isScreensavering)
|
|
XCTAssertFalse(manager.states.isSleeping)
|
|
XCTAssertFalse(manager.states.isScreenOff)
|
|
XCTAssertEqual(manager.idleTimer?.isValid, true)
|
|
}
|
|
|
|
func testObserverRemoval() {
|
|
manager.unregister(observer: observer)
|
|
|
|
notificationCenter.post(name: .init(rawValue: "com.apple.screensaver.didstart"), object: nil)
|
|
XCTAssertFalse(observer.didUpdate)
|
|
|
|
manager.register(observer: observer)
|
|
notificationCenter.post(name: .init(rawValue: "com.apple.screensaver.didstop"), object: nil)
|
|
XCTAssertTrue(observer.didUpdate)
|
|
}
|
|
|
|
func testScreensaver() {
|
|
notificationCenter.post(name: .init(rawValue: "com.apple.screensaver.didstart"), object: nil)
|
|
XCTAssertTrue(observer.didUpdate)
|
|
XCTAssertFalse(manager.isActive)
|
|
XCTAssertTrue(manager.states.isScreensavering)
|
|
XCTAssertNil(manager.idleTimer)
|
|
observer.reset()
|
|
|
|
notificationCenter.post(name: .init(rawValue: "com.apple.screensaver.didstop"), object: nil)
|
|
XCTAssertTrue(observer.didUpdate)
|
|
XCTAssertTrue(manager.isActive)
|
|
XCTAssertFalse(manager.states.isScreensavering)
|
|
XCTAssertEqual(manager.idleTimer?.isValid, true)
|
|
}
|
|
|
|
func testLock() {
|
|
notificationCenter.post(name: .init(rawValue: "com.apple.screenIsLocked"), object: nil)
|
|
XCTAssertTrue(observer.didUpdate)
|
|
XCTAssertFalse(manager.isActive)
|
|
XCTAssertTrue(manager.states.isLocked)
|
|
XCTAssertNil(manager.idleTimer)
|
|
observer.reset()
|
|
|
|
notificationCenter.post(name: .init(rawValue: "com.apple.screenIsUnlocked"), object: nil)
|
|
XCTAssertTrue(observer.didUpdate)
|
|
XCTAssertTrue(manager.isActive)
|
|
XCTAssertFalse(manager.states.isLocked)
|
|
XCTAssertEqual(manager.idleTimer?.isValid, true)
|
|
}
|
|
|
|
func testSleep() {
|
|
notificationCenter.post(name: .init(rawValue: "NSWorkspaceWillSleepNotification"), object: nil)
|
|
XCTAssertTrue(observer.didUpdate)
|
|
XCTAssertFalse(manager.isActive)
|
|
XCTAssertTrue(manager.states.isSleeping)
|
|
XCTAssertNil(manager.idleTimer)
|
|
observer.reset()
|
|
|
|
notificationCenter.post(name: .init(rawValue: "NSWorkspaceDidWakeNotification"), object: nil)
|
|
XCTAssertTrue(observer.didUpdate)
|
|
XCTAssertTrue(manager.isActive)
|
|
XCTAssertFalse(manager.states.isSleeping)
|
|
XCTAssertEqual(manager.idleTimer?.isValid, true)
|
|
}
|
|
|
|
func testScreenOff() {
|
|
notificationCenter.post(name: .init(rawValue: "NSWorkspaceScreensDidSleepNotification"), object: nil)
|
|
XCTAssertTrue(observer.didUpdate)
|
|
XCTAssertFalse(manager.isActive)
|
|
XCTAssertTrue(manager.states.isScreenOff)
|
|
XCTAssertNil(manager.idleTimer)
|
|
observer.reset()
|
|
|
|
notificationCenter.post(name: .init(rawValue: "NSWorkspaceScreensDidWakeNotification"), object: nil)
|
|
XCTAssertTrue(observer.didUpdate)
|
|
XCTAssertTrue(manager.isActive)
|
|
XCTAssertFalse(manager.states.isScreenOff)
|
|
XCTAssertEqual(manager.idleTimer?.isValid, true)
|
|
}
|
|
|
|
func testFUS() {
|
|
notificationCenter.post(name: .init(rawValue: "NSWorkspaceSessionDidResignActiveNotification"), object: nil)
|
|
XCTAssertTrue(observer.didUpdate)
|
|
XCTAssertFalse(manager.isActive)
|
|
XCTAssertTrue(manager.states.isFastUserSwitched)
|
|
XCTAssertNil(manager.idleTimer)
|
|
observer.reset()
|
|
|
|
notificationCenter.post(name: .init(rawValue: "NSWorkspaceSessionDidBecomeActiveNotification"), object: nil)
|
|
XCTAssertTrue(observer.didUpdate)
|
|
XCTAssertTrue(manager.isActive)
|
|
XCTAssertFalse(manager.states.isFastUserSwitched)
|
|
XCTAssertEqual(manager.idleTimer?.isValid, true)
|
|
}
|
|
|
|
func testTerminate() {
|
|
notificationCenter.post(name: .init("NonMac_terminationWillBeginNotification"), object: nil)
|
|
XCTAssertTrue(observer.didUpdate)
|
|
XCTAssertFalse(manager.isActive)
|
|
XCTAssertTrue(manager.states.isTerminating)
|
|
XCTAssertNil(manager.idleTimer)
|
|
}
|
|
|
|
func testIdleTimeWithoutAnythingElse() {
|
|
Current.device.idleTime = { .init(value: 99, unit: .seconds) }
|
|
manager.minimumIdleTime = .init(value: 100, unit: .seconds)
|
|
XCTAssertNotNil(manager.idleTimer)
|
|
manager.idleTimer?.fire()
|
|
|
|
XCTAssertTrue(manager.isActive)
|
|
XCTAssertFalse(manager.states.isIdle)
|
|
|
|
Current.device.idleTime = { .init(value: 100, unit: .seconds) }
|
|
manager.idleTimer?.fire()
|
|
XCTAssertTrue(observer.didUpdate)
|
|
XCTAssertFalse(manager.isActive)
|
|
XCTAssertTrue(manager.states.isIdle)
|
|
XCTAssertNotNil(manager.idleTimer)
|
|
observer.reset()
|
|
|
|
Current.device.idleTime = { .init(value: 300, unit: .seconds) }
|
|
manager.idleTimer?.fire()
|
|
XCTAssertFalse(observer.didUpdate, "already posted for this idle period")
|
|
XCTAssertFalse(manager.isActive)
|
|
XCTAssertTrue(manager.states.isIdle)
|
|
XCTAssertNotNil(manager.idleTimer)
|
|
observer.reset()
|
|
|
|
Current.device.idleTime = { .init(value: 10, unit: .seconds) }
|
|
manager.idleTimer?.fire()
|
|
XCTAssertTrue(observer.didUpdate)
|
|
XCTAssertTrue(manager.isActive)
|
|
XCTAssertFalse(manager.states.isIdle)
|
|
XCTAssertNotNil(manager.idleTimer)
|
|
observer.reset()
|
|
}
|
|
|
|
func testIdleTimeThenAnotherEvent() {
|
|
Current.device.idleTime = { .init(value: 100, unit: .seconds) }
|
|
manager.minimumIdleTime = .init(value: 100, unit: .seconds)
|
|
XCTAssertNotNil(manager.idleTimer)
|
|
manager.idleTimer?.fire()
|
|
|
|
XCTAssertFalse(manager.isActive)
|
|
XCTAssertTrue(manager.states.isIdle)
|
|
XCTAssertTrue(observer.didUpdate)
|
|
|
|
notificationCenter.post(name: .init(rawValue: "NSWorkspaceSessionDidResignActiveNotification"), object: nil)
|
|
XCTAssertTrue(observer.didUpdate)
|
|
XCTAssertFalse(manager.isActive)
|
|
XCTAssertTrue(manager.states.isFastUserSwitched)
|
|
XCTAssertNil(manager.idleTimer)
|
|
observer.reset()
|
|
|
|
notificationCenter.post(name: .init(rawValue: "NSWorkspaceSessionDidBecomeActiveNotification"), object: nil)
|
|
XCTAssertTrue(observer.didUpdate)
|
|
XCTAssertFalse(manager.isActive)
|
|
XCTAssertFalse(manager.states.isFastUserSwitched)
|
|
XCTAssertTrue(manager.states.isIdle)
|
|
XCTAssertNotNil(manager.idleTimer)
|
|
observer.reset()
|
|
|
|
Current.device.idleTime = { .init(value: 99, unit: .seconds) }
|
|
manager.idleTimer?.fire()
|
|
XCTAssertTrue(observer.didUpdate)
|
|
XCTAssertTrue(manager.isActive)
|
|
XCTAssertFalse(manager.states.isIdle)
|
|
XCTAssertNotNil(manager.idleTimer)
|
|
observer.reset()
|
|
}
|
|
}
|
|
|
|
class MockActiveStateObserver: ActiveStateObserver {
|
|
var didUpdate = false
|
|
|
|
func reset() {
|
|
didUpdate = false
|
|
}
|
|
|
|
func activeStateDidChange(for manager: ActiveStateManager) {
|
|
didUpdate = true
|
|
}
|
|
}
|