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
193 changes: 193 additions & 0 deletions Hidden Bar.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions Hidden Bar.xcodeproj/xcshareddata/xcschemes/Hidden Bar.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@
ReferencedContainer = "container:Hidden Bar.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "NO"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "B6ED1C7FB7AE7D98FD4143B2"
BuildableName = "HiddenTests.xctest"
BlueprintName = "HiddenTests"
ReferencedContainer = "container:Hidden Bar.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
Expand All @@ -38,6 +52,16 @@
ReferencedContainer = "container:Hidden Bar.xcodeproj">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "B6ED1C7FB7AE7D98FD4143B2"
BuildableName = "HiddenTests.xctest"
BlueprintName = "HiddenTests"
ReferencedContainer = "container:Hidden Bar.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
Expand Down
79 changes: 79 additions & 0 deletions HiddenTests/MenuBarExtraClassifierTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import XCTest
@testable import Hidden_Bar

private func makeExtra(appName: String, x: CGFloat, width: CGFloat = 22, height: CGFloat = 22, title: String? = nil) -> MenuBarExtraInfo {
MenuBarExtraInfo(
element: AXUIElementCreateApplication(0),
appName: appName,
appIcon: nil,
title: title,
pid: 0,
position: CGPoint(x: x, y: 0),
size: CGSize(width: width, height: height)
)
}

/// Contract: MenuBarExtraClassifier.classify sorts already-fetched menu-bar
/// extras into hidden-behind-notch vs visible buckets against a boundary,
/// independent of how those extras were fetched (the live AXUIElement
/// enumeration is a separate, non-unit-testable concern).
/// Serves: NotchOverflowController's overflow menu, which needs a correctly
/// ordered, correctly bucketed list to render.
/// Behaviors covered (4): left-of-boundary classifies hidden, at/right-of
/// boundary classifies visible, zero-size extras are excluded from both
/// buckets, and each bucket is ordered closest-to-boundary first.
/// Mock boundary: none — pure function over an array of value-type structs.
final class MenuBarExtraClassifierTests: XCTestCase {

// Given an extra positioned left of the boundary
// When classifying
// Then it lands in the hidden bucket
func test_extraLeftOfBoundary_isClassifiedHidden() {
let extra = makeExtra(appName: "LeftApp", x: 100)

let result = MenuBarExtraClassifier.classify([extra], boundary: 500)

XCTAssertEqual(result.hidden.map(\.appName), ["LeftApp"], "an extra left of the boundary must be hidden")
XCTAssertTrue(result.visible.isEmpty, "an extra left of the boundary must not also appear as visible")
}

// Given an extra positioned at or right of the boundary
// When classifying
// Then it lands in the visible bucket
func test_extraAtOrRightOfBoundary_isClassifiedVisible() {
let atBoundary = makeExtra(appName: "AtBoundary", x: 500)
let rightOfBoundary = makeExtra(appName: "RightApp", x: 900)

let result = MenuBarExtraClassifier.classify([atBoundary, rightOfBoundary], boundary: 500)

XCTAssertEqual(Set(result.visible.map(\.appName)), ["AtBoundary", "RightApp"],
"extras at or right of the boundary must be visible")
XCTAssertTrue(result.hidden.isEmpty)
}

// Given an extra with zero width or height (a non-rendering placeholder item)
// When classifying
// Then it is excluded from both buckets
func test_zeroSizeExtra_isExcludedFromBothBuckets() {
let zeroWidth = makeExtra(appName: "ZeroWidth", x: 100, width: 0)
let zeroHeight = makeExtra(appName: "ZeroHeight", x: 900, height: 0)

let result = MenuBarExtraClassifier.classify([zeroWidth, zeroHeight], boundary: 500)

XCTAssertTrue(result.hidden.isEmpty, "a zero-width extra is not a real rendered icon")
XCTAssertTrue(result.visible.isEmpty, "a zero-height extra is not a real rendered icon")
}

// Given multiple extras in each bucket
// When classifying
// Then each bucket is ordered closest-to-boundary first (descending X)
func test_multipleExtrasPerBucket_areOrderedClosestToBoundaryFirst() {
let far = makeExtra(appName: "Far", x: 50)
let near = makeExtra(appName: "Near", x: 450)

let result = MenuBarExtraClassifier.classify([far, near], boundary: 500)

XCTAssertEqual(result.hidden.map(\.appName), ["Near", "Far"],
"hidden extras must be ordered with the one closest to the notch boundary first")
}
}
80 changes: 80 additions & 0 deletions HiddenTests/NotchGeometryTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import XCTest
@testable import Hidden_Bar

/// Contract: NotchGeometry.computeBoundary must tell the notch-overflow
/// feature (#350) where the notch actually is, replacing the reviewer-flagged
/// `screen.frame.width / 8` heuristic that produced the wrong boundary on
/// screens narrower or wider than the size it was tuned against.
/// Serves: NotchOverflowController, which uses the boundary to classify
/// menu-bar extras as hidden-behind-notch vs visible.
/// Behaviors covered (4): no-notch detection, notch detection via
/// safeAreaInsets, boundary sourced from auxiliaryTopRightArea when present,
/// and the fallback boundary when a notch is reported but the aux rect is
/// unavailable (older OS on notch-capable hardware).
/// Mock boundary: none — this is a pure function over caller-supplied screen
/// geometry values, no NSScreen/AppKit call inside the function under test.
final class NotchGeometryTests: XCTestCase {

// Given a screen reporting no safe-area inset at the top
// When computing the notch boundary
// Then hasNotch is false
func test_screenWithoutSafeAreaInset_reportsNoNotch() {
let boundary = NotchGeometry.computeBoundary(
screenFrame: CGRect(x: 0, y: 0, width: 1728, height: 1117),
safeAreaInsetsTop: 0,
auxiliaryTopRightAreaMinX: nil
)

XCTAssertFalse(boundary.hasNotch, "a zero top safe-area inset means the screen has no notch")
}

// Given a screen reporting a positive top safe-area inset
// When computing the notch boundary
// Then hasNotch is true
func test_screenWithSafeAreaInset_reportsNotch() {
let boundary = NotchGeometry.computeBoundary(
screenFrame: CGRect(x: 0, y: 0, width: 1512, height: 982),
safeAreaInsetsTop: 32,
auxiliaryTopRightAreaMinX: 810
)

XCTAssertTrue(boundary.hasNotch, "a positive top safe-area inset means the screen has a notch")
}

// Given a notch screen with a known auxiliaryTopRightArea boundary
// When computing the notch boundary
// Then the threshold is exactly that boundary, not a fraction of screen width
func test_notchScreen_usesAuxiliaryTopRightAreaAsThreshold_notWidthFraction() {
// Two different screen widths sharing a real reported aux-area boundary
// regression-guards the exact bug flagged in review: the old
// `screen.frame.width / 8`-derived value would differ between these,
// even though the real hardware boundary here does not.
let narrowerScreen = NotchGeometry.computeBoundary(
screenFrame: CGRect(x: 0, y: 0, width: 1512, height: 982),
safeAreaInsetsTop: 32,
auxiliaryTopRightAreaMinX: 810
)
let widerScreen = NotchGeometry.computeBoundary(
screenFrame: CGRect(x: 0, y: 0, width: 1728, height: 1117),
safeAreaInsetsTop: 32,
auxiliaryTopRightAreaMinX: 810
)

XCTAssertEqual(narrowerScreen.overflowThresholdX, 810, "threshold must come straight from the reported aux-area rect")
XCTAssertEqual(widerScreen.overflowThresholdX, 810, "threshold must not scale with screen width when the real boundary doesn't")
}

// Given a notch is reported but no auxiliaryTopRightArea is available
// When computing the notch boundary
// Then it falls back to the documented default rather than 0 or a crash
func test_notchWithoutAuxiliaryArea_fallsBackToDefaultThreshold() {
let boundary = NotchGeometry.computeBoundary(
screenFrame: CGRect(x: 0, y: 0, width: 1728, height: 1117),
safeAreaInsetsTop: 32,
auxiliaryTopRightAreaMinX: nil
)

XCTAssertEqual(boundary.overflowThresholdX, NotchGeometry.fallbackThresholdX,
"missing aux-area data on a reported notch must degrade to the documented fallback")
}
}
113 changes: 113 additions & 0 deletions HiddenTests/NotchOverflowMenuBuilderTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import XCTest
@testable import Hidden_Bar

private func makeExtra(appName: String, x: CGFloat, title: String? = nil) -> MenuBarExtraInfo {
MenuBarExtraInfo(
element: AXUIElementCreateApplication(0),
appName: appName,
appIcon: nil,
title: title,
pid: 0,
position: CGPoint(x: x, y: 0),
size: CGSize(width: 22, height: 22)
)
}

/// Contract: NotchOverflowController.buildOverflowMenu must render a
/// correct, inspectable NSMenu from already-classified extras. NSMenu/
/// NSMenuItem construction runs headlessly (no window needed), so this is
/// unit-testable despite living in an AppKit-heavy controller.
/// Serves: the user browsing the overflow menu to find and reach an icon
/// that macOS silently dropped behind the notch.
/// Behaviors covered (5): empty state when nothing is found, section headers
/// carry correct counts, section headers render at full-contrast label color
/// (not the dim secondary/default-disabled style), an extra with an empty
/// title falls back to the app name, and a hidden extra's title is visually
/// distinguished by glyph and weight from a visible one.
/// Mock boundary: extras and boundary are injected directly, bypassing the
/// live AXUIElement enumeration (getAllMenuBarExtras), which is not
/// unit-testable without a real Accessibility-permitted process.
final class NotchOverflowMenuBuilderTests: XCTestCase {

private var controller: NotchOverflowController!

override func setUp() {
super.setUp()
controller = NotchOverflowController()
}

// Given no extras were found
// When building the overflow menu
// Then it shows a single disabled empty-state item
func test_noExtrasFound_showsDisabledEmptyStateItem() {
let menu = controller.buildOverflowMenu(extras: [], boundary: 500)

let emptyStateItem = menu.items.first { $0.title.contains("No items found") }
XCTAssertNotNil(emptyStateItem, "an empty result must surface a message explaining why, not a blank menu")
XCTAssertEqual(emptyStateItem?.isEnabled, false, "the empty-state message is informational, not actionable")
}

// Given a mix of hidden and visible extras
// When building the overflow menu
// Then the section headers report the correct counts
func test_mixOfHiddenAndVisibleExtras_headersShowCorrectCounts() {
let extras = [
makeExtra(appName: "Hidden1", x: 100),
makeExtra(appName: "Hidden2", x: 150),
makeExtra(appName: "Visible1", x: 900),
]

let menu = controller.buildOverflowMenu(extras: extras, boundary: 500)

let hiddenHeader = menu.items.first { $0.title.contains("Hidden Behind Notch") }
let visibleHeader = menu.items.first { $0.title.contains("Visible") && !$0.title.contains("Hidden") }
XCTAssertEqual(hiddenHeader?.title.contains("2"), true, "two hidden extras must be reflected in the header count")
XCTAssertEqual(visibleHeader?.title.contains("1"), true, "one visible extra must be reflected in the header count")
}

// Given a section header (the top title, or a hidden/visible count header)
// When it's rendered
// Then it uses full-contrast labelColor, not secondaryLabelColor or
// AppKit's default dimmed disabled-item style, since the dim style read
// poorly against a dark selection highlight
func test_sectionHeaders_useFullContrastLabelColor_notDimmed() {
let extras = [makeExtra(appName: "HiddenApp", x: 100)]

let menu = controller.buildOverflowMenu(extras: extras, boundary: 500)

let header = menu.items.first { $0.title.contains("Hidden Behind Notch") }
let color = header?.attributedTitle?.attribute(.foregroundColor, at: 0, effectiveRange: nil) as? NSColor
XCTAssertEqual(color, NSColor.labelColor, "a section header must use the normal full-contrast label color")
}

// Given an extra with an empty AX title
// When building its menu item
// Then the item falls back to the app's name
func test_extraWithEmptyTitle_fallsBackToAppName() {
let extras = [makeExtra(appName: "FallbackApp", x: 900, title: "")]

let menu = controller.buildOverflowMenu(extras: extras, boundary: 500)

let item = menu.items.first { $0.representedObject is MenuBarExtraInfo }
XCTAssertEqual(item?.title, "FallbackApp", "an empty AX title must fall back to the owning app's name")
}

// Given a hidden extra
// When building its menu item
// Then it's distinguished by a warning glyph and bold weight, not color
// alone (color-only signaling gives colorblind users nothing, and reads
// poorly against a dark selection highlight)
func test_hiddenExtra_isDistinguishedByGlyphAndWeight_notColorAlone() {
let extras = [makeExtra(appName: "HiddenApp", x: 100)]

let menu = controller.buildOverflowMenu(extras: extras, boundary: 500)

let item = menu.items.first { $0.representedObject is MenuBarExtraInfo }
XCTAssertEqual(item?.attributedTitle?.string, "\u{26A0}\u{FE0F} HiddenApp",
"a hidden extra's title must be prefixed with a warning glyph")
let font = item?.attributedTitle?.attribute(.font, at: 0, effectiveRange: nil) as? NSFont
XCTAssertEqual(font, NSFont.boldSystemFont(ofSize: 13), "a hidden extra's title must be bold")
let color = item?.attributedTitle?.attribute(.foregroundColor, at: 0, effectiveRange: nil) as? NSColor
XCTAssertEqual(color, NSColor.labelColor, "a hidden extra's title must stay at the normal legible label color")
}
}
52 changes: 52 additions & 0 deletions HiddenTests/NotchOverflowPreferencesTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import XCTest
@testable import Hidden_Bar

/// Contract: Preferences.notchOverflowEnabled and AppDelegate's default
/// values must give the user an explicit, persistent, discoverable way to
/// turn the notch-overflow feature off — the exact gap the reviewer on #350
/// flagged ("should we have a way to enable/disable this feature
/// manually?") that the original PR never actually wired into any UI.
/// Serves: the Preferences checkbox (PreferencesViewController) and
/// StatusBarController's context menu, which reads this preference fresh
/// every time getContextMenu() builds a new menu for the next presentation
/// (see showContextMenu(from:)) rather than needing a push notification to
/// stay in sync.
/// Behaviors covered (2): fresh-install default is `true`, and setting the
/// preference persists the new value.
/// Mock boundary: none — exercises the real `UserDefaults.standard`, saving/
/// restoring prior state around each test since this preference is
/// process-wide shared state, matching how the rest of this codebase's
/// Preferences enum already works (no DI seam exists there to mock against).
final class NotchOverflowPreferencesTests: XCTestCase {

private var priorValue: Bool!

override func setUp() {
super.setUp()
priorValue = UserDefaults.standard.bool(forKey: UserDefaults.Key.notchOverflowEnabled)
}

override func tearDown() {
UserDefaults.standard.set(priorValue, forKey: UserDefaults.Key.notchOverflowEnabled)
super.tearDown()
}

// Given a fresh install (defaults never registered by the user)
// When reading the app's declared default preference values
// Then notchOverflowEnabled defaults to true
func test_defaultPreferenceValues_notchOverflowEnabledDefaultsToTrue() {
let defaults = AppDelegate.defaultPreferenceValues

XCTAssertEqual(defaults[UserDefaults.Key.notchOverflowEnabled] as? Bool, true,
"a first-time user should see notch overflow available without opting in")
}

// Given the preference is set to false
// When reading it back
// Then it reports false (the setter/getter round-trip persists correctly)
func test_settingPreferenceFalse_persistsAndReadsBackFalse() {
Preferences.notchOverflowEnabled = false

XCTAssertFalse(Preferences.notchOverflowEnabled, "the preference must persist the value it was set to")
}
}
Loading