Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions HiddenBarTests/HotKeyRegistryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,79 @@ final class HotKeyRegistryTests: XCTestCase {
registry.register(key: toggleKey) {}
XCTAssertEqual(registry.clashingTitle(for: combo, excluding: findKey), toggleKey)
}

// Given two shortcuts on one combination (macOS keeps the first and refuses the second silently)
// When the first is cleared
// Then the second works: every binding is made again on a change, not skipped as unchanged
func test_clearingTheOtherShortcutRevivesTheOneThatLostTheClash() {
let combo = shortcut("k", keyCode: 40)
let carbon = CarbonLikeBinder()
let registry = HotKeyRegistry(defaults: defaults, center: center) { carbon.bind($0, $1) }
store(combo, for: toggleKey)
store(combo, for: findKey)
var ran: [String] = []
registry.register(key: toggleKey, title: "Show or hide icons") { ran.append("toggle") }
registry.register(key: findKey, title: "Find a hidden icon") { ran.append("find") }
carbon.fire(combo)
XCTAssertEqual(ran.count, 1, "one of the two owns the combination")
let loser = ran[0] == "toggle" ? findKey : toggleKey
let winner = loser == findKey ? toggleKey : findKey

store(nil, for: winner)
ran = []
carbon.fire(combo)
XCTAssertEqual(ran, [loser == findKey ? "find" : "toggle"], "the survivor works once the other is cleared")
XCTAssertTrue(registry.isBound(loser))
}

// Given live shortcuts
// When an unrelated preference changes, then one changes
// Then the first touches nothing and the second makes every binding again
func test_onlyAShortcutChangeRebindsEverything() {
store(shortcut("k", keyCode: 40), for: toggleKey)
store(shortcut("f", keyCode: 3, shift: true), for: findKey)
registry.register(key: toggleKey) {}
registry.register(key: findKey) {}
XCTAssertEqual(binder.calls.count, 2)
center.post(name: .prefsChanged, object: nil)
XCTAssertEqual(binder.calls.count, 2)
store(shortcut("j", keyCode: 38), for: toggleKey)
XCTAssertEqual(binder.calls.count, 4)
XCTAssertEqual(Set(binder.liveBindings.map(\.keyCode)), [38, 3])
}
}

/// Stands in for Carbon: a combination already held by a live registration is
/// refused silently (the call "succeeds", the handler never fires, even after the
/// holder goes away).
private final class CarbonLikeBinder {
private final class Binding {
let combo: GlobalKeybindPreferences
let handler: () -> Void
let refused: Bool
init(_ combo: GlobalKeybindPreferences, refused: Bool, _ handler: @escaping () -> Void) {
self.combo = combo
self.refused = refused
self.handler = handler
}
}

private final class WeakBox {
weak var value: Binding?
init(_ value: Binding) { self.value = value }
}

private var registered: [WeakBox] = []

func bind(_ combo: GlobalKeybindPreferences, _ handler: @escaping () -> Void) -> AnyObject? {
let held = registered.compactMap(\.value).contains { !$0.refused && $0.combo.isSameCombo(as: combo) }
let binding = Binding(combo, refused: held, handler)
registered.append(WeakBox(binding))
return binding
}

/// Runs the handler of the live registration that owns the combination.
func fire(_ combo: GlobalKeybindPreferences) {
registered.compactMap(\.value).first { !$0.refused && $0.combo.isSameCombo(as: combo) }?.handler()
}
}
147 changes: 123 additions & 24 deletions HiddenBarTests/MenuBarEngineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2806,8 +2806,8 @@ final class PresentationModeEngineTests: XCTestCase {
arrowReadable = true
}

// `expandsWhenCaptureStarts` is the 1.11.2 option; presentation mode must lift
// for a camera or microphone whatever it says.
// `expandsWhenCaptureStarts` is the 1.12 option ("show the whole bar when a
// camera or microphone turns on"); presentation mode follows it.
private func makeEngine(expandsWhenCaptureStarts: Bool = true) -> NativeVisibilityEngine {
let items = self.items!
let engine = NativeVisibilityEngine(items: items, inventory: inventory, visibility: visibility, clock: clock,
Expand Down Expand Up @@ -2986,7 +2986,7 @@ final class PresentationModeEngineTests: XCTestCase {
// When a camera turns on
// Then the restriction lifts at once and the bar shows
func testCaptureStartLiftsTheRestrictionAtOnce() {
let engine = makeEngine(expandsWhenCaptureStarts: false)
let engine = makeEngine()
collapsed(engine)
engine.startPresentation()
let held = visibility.succeed(visibility.requests.count - 1)
Expand All @@ -3002,7 +3002,7 @@ final class PresentationModeEngineTests: XCTestCase {
// When the camera turns off
// Then hiding resumes only after the quiet period, with the strict list
func testResumesTwoAndAHalfSecondsAfterCaptureEnds() {
let engine = makeEngine(expandsWhenCaptureStarts: false)
let engine = makeEngine()
collapsed(engine)
engine.startPresentation()
visibility.succeed(visibility.requests.count - 1)
Expand All @@ -3024,7 +3024,7 @@ final class PresentationModeEngineTests: XCTestCase {
// When it stops and starts again inside the quiet period
// Then the bar does not flap: it stays shown until a full quiet period
func testFlappingCaptureNeverHidesEarly() {
let engine = makeEngine(expandsWhenCaptureStarts: false)
let engine = makeEngine()
collapsed(engine)
engine.startPresentation()
visibility.succeed(visibility.requests.count - 1)
Expand All @@ -3043,19 +3043,27 @@ final class PresentationModeEngineTests: XCTestCase {
XCTAssertEqual(visibility.requests.last?.bundles, strict)
}

// Given capture already running
// Given capture already running (a call under way), the switch on
// When the mode starts
// Then no strict list is ever requested: it begins paused
func testStartWhileCaptureIsActiveNeverHolds() {
// Then the strict list is held at once and nothing is lifted; a fresh capture start afterwards lifts once
func testStartWhileCaptureIsActiveStartsHidden() {
capture.isActive = true
let engine = makeEngine(expandsWhenCaptureStarts: false)
let held = collapsed(engine)
let engine = makeEngine()
collapsed(engine)
engine.startPresentation()

XCTAssertTrue(held.isInvalidated)
XCTAssertEqual(visibility.requests.last?.bundles, strict)
let held = visibility.succeed(visibility.requests.count - 1)
XCTAssertEqual(engine.currentPhase(), .presenting)
XCTAssertEqual(engine.state, .collapsed)
XCTAssertFalse(held.isInvalidated)
XCTAssertEqual(phases, [.presenting])

capture.set(false)
capture.set(true)
XCTAssertTrue(held.isInvalidated, "a fresh start while presenting lifts once")
XCTAssertEqual(engine.currentPhase(), .paused)
XCTAssertNil(engine.currentHold)
XCTAssertFalse(visibility.requests.contains { $0.bundles == strict })
XCTAssertEqual(phases, [.presenting, .paused])
}

// Given a bar with no layout to read (Accessibility off)
Expand Down Expand Up @@ -3204,28 +3212,119 @@ final class PresentationModeEngineTests: XCTestCase {
XCTAssertEqual(phases, [.presenting])
}

// Given the pause for a camera
// When the user (or the auto-hide timer) collapses the bar
// Then no list is held and the bar stays shown, until the pause ends
func testCollapseDuringThePauseStaysLifted() {
let engine = makeEngine(expandsWhenCaptureStarts: false)
// Given the pause for a camera that is still on
// When the user collapses the bar (the normal collapse)
// Then presentation hides again at once, with the strict list, and stays so while capture continues
func testCollapseDuringThePauseHidesAgain() {
let engine = makeEngine()
collapsed(engine)
engine.startPresentation()
visibility.succeed(visibility.requests.count - 1)
capture.set(true)
XCTAssertEqual(engine.state, .expanded)
let requests = visibility.requests.count

var results: [CollapseResult] = []
engine.collapse { results.append($0) }
XCTAssertEqual(visibility.requests.count, requests, "not even the normal list is held")
XCTAssertEqual(engine.state, .expanded)
XCTAssertEqual(visibility.requests.count, requests + 1)
XCTAssertEqual(visibility.requests.last?.bundles, strict)
visibility.succeed(visibility.requests.count - 1)
XCTAssertEqual(results, [.collapsed])
XCTAssertEqual(engine.state, .collapsed)
XCTAssertEqual(phases, [.presenting, .paused, .presenting])

time.advance(to: 60)
XCTAssertEqual(engine.currentPhase(), .presenting, "capture still on: no second pause")
XCTAssertEqual(visibility.requests.count, requests + 1)
}

// Given a camera that turns on while presenting, the switch on
// When the user presses "Hide again", and capture later ends and starts again
// Then it lifts once, hides on the user's word with capture still on, and a new start lifts again
func testSwitchOnLiftsOnceAndTheUserResumesDuringCapture() {
let engine = makeEngine()
collapsed(engine)
engine.startPresentation()
let held = visibility.succeed(visibility.requests.count - 1)

capture.set(true)
XCTAssertTrue(held.isInvalidated, "the start lifts once")
XCTAssertEqual(engine.currentPhase(), .paused)
XCTAssertNil(engine.currentHold)
XCTAssertEqual(results, [.unavailable])
XCTAssertEqual(phases, [.presenting, .paused], "a collapse that stays shown is not a failure")

engine.resumePresentationHiding()
XCTAssertEqual(visibility.requests.last?.bundles, strict, "hides again with the camera still on")
visibility.succeed(visibility.requests.count - 1)
XCTAssertEqual(engine.currentPhase(), .presenting)
XCTAssertEqual(engine.state, .collapsed)
XCTAssertEqual(phases, [.presenting, .paused, .presenting])

time.advance(to: 30)
capture.set(false)
time.advance(to: 2.7)
XCTAssertEqual(visibility.requests.last?.bundles, strict, "resuming hides again")
time.advance(to: 40)
XCTAssertEqual(phases, [.presenting, .paused, .presenting], "capture ending changes nothing after a user resume")
let requests = visibility.requests.count
capture.set(true)
XCTAssertEqual(engine.currentPhase(), .paused, "the next start lifts again")
XCTAssertEqual(visibility.requests.count, requests)
XCTAssertNil(engine.currentHold)
}

// Given the switch off
// When a camera turns on while presenting, or is already on when the mode starts
// Then nothing is lifted and the strict list stays: presentation never pauses
func testSwitchOffNeverPausesPresentation() {
let engine = makeEngine(expandsWhenCaptureStarts: false)
collapsed(engine)
engine.startPresentation()
let held = visibility.succeed(visibility.requests.count - 1)

capture.set(true)
XCTAssertFalse(held.isInvalidated)
XCTAssertEqual(engine.state, .collapsed)
XCTAssertEqual(engine.currentHold?.bundles, [])
XCTAssertEqual(phases, [.presenting])

engine.stopPresentation()
visibility.succeed(visibility.requests.count - 1)
capture.isActive = true
engine.startPresentation()
XCTAssertEqual(visibility.requests.last?.bundles, strict, "already on at start: still held")
XCTAssertEqual(engine.currentPhase(), .presenting)
}

// Given a presentation, an app the palette allowed, a clock click, then the microphone on and off
// When everything has settled
// Then the hold is the strict list alone: the palette's app went with the lift and does not return
func testPresentingPaletteAllowClockClickAndMicrophoneEndsOnTheStrictList() {
let engine = makeEngine()
collapsed(engine)
engine.startPresentation()
visibility.succeed(visibility.requests.count - 1)

var lifts: [PaletteLift] = []
engine.allowTemporarily("com.hidden") { lifts.append($0) }
XCTAssertEqual(visibility.requests.last?.bundles, [own, "com.hidden"])
visibility.succeed(visibility.requests.count - 1)
XCTAssertEqual(lifts, [.lifted])

let requests = visibility.requests.count
clock.click()
XCTAssertEqual(visibility.requests.count, requests, "a clock click does nothing while presenting")
XCTAssertTrue(clock.presses.isEmpty)

capture.set(true)
XCTAssertNil(engine.currentHold, "the lift for the microphone drops every list")
time.advance(to: 10)
capture.set(false)
time.advance(to: 13)
XCTAssertEqual(visibility.requests.last?.bundles, strict)
visibility.succeed(visibility.requests.count - 1)

XCTAssertEqual(engine.state, .collapsed)
XCTAssertEqual(engine.currentPhase(), .presenting)
XCTAssertEqual(engine.currentHold, NativeHold(systemItems: NativeVisibilityEngine.presentationSystemItems, bundles: []))
XCTAssertEqual(phases, [.presenting, .paused, .presenting])
}

// Given a presentation and a palette asking to open apps
Expand Down
24 changes: 15 additions & 9 deletions HiddenBarTests/PaletteModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,21 @@ final class PaletteModelTests: XCTestCase {
XCTAssertEqual(state.rows.map(\.candidate.name), ["Dropbox"])
}

// Given Return found an app that presentation mode keeps hidden
// When the palette shows it
// Then the sensitive state names the app, until the query changes
func test_sensitiveWhilePresenting_clearsOnTheNextKeystroke() {
var state = model([app("Slack")], query: "sla")
state.showSensitive(appName: "Slack")
XCTAssertEqual(state.content, .sensitiveWhilePresenting(appName: "Slack"))
state.setQuery("slac")
XCTAssertEqual(state.rows.map(\.candidate.name), ["Slack"])
// Given presentation mode keeps two apps hidden
// When the list is built, and the query names one of them
// Then they are not listed and no hint names them, whichever section they sit in
func test_sensitiveAppsLeaveTheListWhilePresenting() {
let environment = PaletteEnvironment(accessibilityTrusted: true, engineAvailable: true,
hiddenWhilePresenting: ["com.test.slack", "com.test.bear", "com.test.dropbox"])
let apps = [app("Slack"), app("Paste"), app("Bear", .visible), app("Dropbox", .alwaysHidden, running: false)]
XCTAssertEqual(model(apps, environment: environment).rows.map(\.candidate.name), ["Paste"])
XCTAssertEqual(model(apps, query: "sla", environment: environment).content,
.noMatch(query: "sla", shownApp: nil, quitApp: nil))
XCTAssertEqual(model(apps, query: "bear", environment: environment).content,
.noMatch(query: "bear", shownApp: nil, quitApp: nil), "a shown sensitive app is not named either")
XCTAssertEqual(model(apps, query: "drop", environment: environment).content,
.noMatch(query: "drop", shownApp: nil, quitApp: nil), "nor a quit one")
XCTAssertEqual(model(apps, query: "sla").rows.map(\.candidate.name), ["Slack"], "off presentation they list as before")
}

// MARK: results and selection
Expand Down
Loading