← All repositories

reportage

A life-logging app for iPhone

git clone https://code.inqk.net/reportage.git


Tests/Unit/AppLockTests.swift (4893B)

1import Foundation
2import Testing
3
4@testable import Reportage
5
6@MainActor
7@Suite("AppLock")
8struct AppLockTests {
9
10    /// Answers each request for the owner with `answer`, counting them.
11    @MainActor
12    private final class Owner {
13        var answer = true
14        var asked = 0
15    }
16
17    private let defaults = UserDefaults(suiteName: "AppLockTests-\(UUID().uuidString)")!
18    private let owner = Owner()
19    private let start = Date(timeIntervalSince1970: 1_000)
20
21    private func makeLock() -> AppLock {
22        let owner = owner
23        return AppLock(defaults: defaults) { _ in
24            owner.asked += 1
25            return owner.answer
26        }
27    }
28
29    /// A lock that's turned on, unlocked, with `delay`.
30    private func unlockedLock(delay: AppLockSettings.Delay = .immediately) async -> AppLock {
31        let lock = makeLock()
32        await lock.setEnabled(true)
33        lock.setDelay(delay)
34        return lock
35    }
36
37    /// Lets the request for the owner made on becoming active run.
38    private func settle() async {
39        for _ in 0..<10 { await Task.yield() }
40    }
41
42    @Test("The delay decides whether a return from the background locks")
43    func delay() {
44        var settings = AppLockSettings(isEnabled: true, delay: .oneMinute)
45        #expect(!settings.locks(leftAt: start, now: start.addingTimeInterval(59)))
46        #expect(settings.locks(leftAt: start, now: start.addingTimeInterval(60)))
47        settings.delay = .immediately
48        #expect(settings.locks(leftAt: start, now: start))
49        settings.isEnabled = false
50        #expect(!settings.locks(leftAt: start, now: start.addingTimeInterval(3_600)))
51    }
52
53    @Test("Turning the lock on or off asks for the owner, and is kept")
54    func enabling() async {
55        let lock = makeLock()
56        #expect(!lock.isLocked)
57
58        owner.answer = false
59        await lock.setEnabled(true)
60        #expect(!lock.settings.isEnabled)
61
62        owner.answer = true
63        await lock.setEnabled(true)
64        lock.setDelay(.fiveMinutes)
65        #expect(lock.settings.isEnabled)
66        #expect(!lock.isLocked)
67        #expect(owner.asked == 2)
68        #expect(AppLockSettings.load(from: defaults) == AppLockSettings(isEnabled: true, delay: .fiveMinutes))
69
70        // A new launch starts locked.
71        #expect(makeLock().isLocked)
72    }
73
74    @Test("Becoming active without leaving the app doesn't lock it")
75    func inactiveOnly() async {
76        let lock = await unlockedLock()
77        lock.didBecomeInactive()
78        lock.didBecomeActive(now: start)
79        #expect(!lock.isLocked)
80        #expect(!lock.isCovered)
81    }
82
83    @Test("A return within the delay uncovers the app without asking")
84    func withinDelay() async {
85        let lock = await unlockedLock(delay: .oneMinute)
86        lock.didEnterBackground(now: start)
87        #expect(lock.isCovered)
88        lock.didBecomeActive(now: start.addingTimeInterval(30))
89        #expect(!lock.isLocked)
90        #expect(!lock.isCovered)
91        #expect(owner.asked == 1)
92    }
93
94    @Test("A return within the delay uncovers the app as soon as it's inactive")
95    func uncoversEarly() async {
96        let lock = await unlockedLock(delay: .fiveMinutes)
97        lock.didEnterBackground(now: start)
98        lock.didBecomeInactive(now: start.addingTimeInterval(60))
99        #expect(!lock.isCovered)
100        lock.didBecomeActive(now: start.addingTimeInterval(61))
101        #expect(!lock.isLocked)
102        #expect(!lock.isCovered)
103    }
104
105    @Test("A return after the delay locks as soon as it's inactive, and asks once active")
106    func locksEarly() async {
107        let lock = await unlockedLock()
108        lock.didEnterBackground(now: start)
109        lock.didBecomeInactive(now: start.addingTimeInterval(1))
110        #expect(lock.isLocked)
111        #expect(lock.isCovered)
112        await settle()
113        #expect(owner.asked == 1)
114        lock.didBecomeActive(now: start.addingTimeInterval(2))
115        await settle()
116        #expect(owner.asked == 2)
117    }
118
119    @Test("A return after the delay locks and asks once, and unlocking uncovers")
120    func afterDelay() async {
121        let lock = await unlockedLock(delay: .oneMinute)
122        owner.answer = false
123        lock.didEnterBackground(now: start)
124        lock.didBecomeActive(now: start.addingTimeInterval(61))
125        #expect(lock.isLocked)
126        #expect(lock.isCovered)
127        await settle()
128        // Once to turn the lock on, and once on returning.
129        #expect(owner.asked == 2)
130        #expect(lock.isLocked)
131
132        // Face ID's prompt makes the app inactive and active again, which
133        // doesn't ask again.
134        lock.didBecomeInactive()
135        lock.didBecomeActive(now: start.addingTimeInterval(62))
136        await settle()
137        #expect(owner.asked == 2)
138
139        owner.answer = true
140        await lock.unlock()
141        #expect(!lock.isLocked)
142        #expect(!lock.isCovered)
143    }
144}