portico/Tests/PorticoTests/BreakpointTests.swift

100 lines
3.6 KiB
Swift

import Testing
import Adw
@_spi(SGTKInternal) import Gtk
@_spi(Portico) import Portico
/// Tests the Portico breakpoint DSL and mount-time wiring.
@MainActor @Suite(.serialized) struct BreakpointTests {
@Test func conditionDSLBuildsTypedAndStringConditions() {
guard Gtk.initCheck() else { return }
let typed = Condition.maxWidth(400).condition
#expect(typed.toString().contains("max-width"))
let composed = Condition.and(.maxWidth(400), .maxHeight(300)).condition
#expect(composed.toString().contains("and"))
let parsed: Condition = "min-width: 200px"
#expect(parsed.condition.toString().contains("min-width"))
}
@Test func typedBreakpointSettersAcceptSupportedGValues() {
guard Gtk.initCheck() else { return }
let label = Gtk.Label(str: "wide")
let breakpoint = Adw.Breakpoint(condition: Condition.maxWidth(400).condition)
breakpoint.addSetter(object: label, property: "label", string: "compact")
breakpoint.addSetter(object: label, property: "visible", bool: false)
breakpoint.addSetter(object: label, property: "opacity", double: 0.5)
#expect(breakpoint.getCondition() != nil)
}
@Test func reactiveBreakpointMountRegistersApplyHandlers() {
guard Gtk.initCheck() else { return }
let active = StateBox(false)
let context = MountContext()
let view = BreakpointBin { Label(str: "content") }
.breakpoint("max-width: 400px", isActive: Binding(active))
let bin = AnyView(view).makeWidget(context) as! Adw.BreakpointBin
#expect(bin.getCurrentBreakpoint() == nil)
#expect(context.registry.isEmpty == false)
context.registry.teardown()
#expect(context.registry.isEmpty)
}
@Test func typedSetterResolvesWidgetRefBeforeBreakpointMount() {
guard Gtk.initCheck() else { return }
let labelRef = WidgetRef<Gtk.Label>()
let context = MountContext()
let view = BreakpointBin {
Label(str: "wide").ref(labelRef)
}
.breakpoint("max-width: 400px") {
Setter(labelRef, "label", string: "compact")
}
let bin = AnyView(view).makeWidget(context) as! Adw.BreakpointBin
#expect(labelRef.wrappedValue != nil)
#expect(bin.getCurrentBreakpoint() == nil)
context.registry.teardown()
#expect(context.registry.isEmpty)
}
@Test func configurationRegistersApplyAndUnapplyHandlers() {
guard Gtk.initCheck() else { return }
var configured = false
let context = MountContext()
let view = BreakpointBin { Label(str: "content") }
.breakpoint("max-width: 400px") { configuration in
configured = configuration.breakpoint.getCondition() != nil
configuration.onApply { }
configuration.onUnapply { }
}
_ = AnyView(view).makeWidget(context) as! Adw.BreakpointBin
#expect(configured)
#expect(context.registry.isEmpty == false)
context.registry.teardown()
#expect(context.registry.isEmpty)
}
@Test func currentBreakpointBindingInitializesAndTearsDown() {
guard Gtk.initCheck() else { return }
let current = StateBox<Breakpoint?>(nil)
let context = MountContext()
let view = BreakpointBin { Label(str: "content") }
.currentBreakpoint(Binding(current))
let bin = AnyView(view).makeWidget(context) as! Adw.BreakpointBin
#expect(bin.getCurrentBreakpoint() == nil)
#expect(current.peek() == nil)
context.registry.teardown()
#expect(context.registry.isEmpty)
}
}