portico/Tests/PorticoTests/InheritedModifierTests.swift

95 lines
3.2 KiB
Swift

import Testing
@_spi(SGTKInternal) import Gtk
import Adw
@_spi(Portico) import Portico
@MainActor @Suite(.serialized) struct InheritedModifierTests {
// MARK: - Lineage
/// EntryRow inherits modifiers from PreferencesRow, ListBoxRow, Widget, and Editable.
@Test func lineage() {
guard Gtk.initCheck() else { return }
let view = AnyView(
EntryRow()
.title("T")
.activatable(false)
.hexpand(true)
.text("x")
)
let w = view.makeWidget(MountContext()) as! Adw.EntryRow
#expect(w.getTitle() == "T") // Adw.PreferencesRow
#expect(w.getActivatable() == false) // Gtk.ListBoxRow
#expect(w.getHexpand() == true) // Gtk.Widget
#expect(w.text == "x") // Gtk.Editable
}
// MARK: - Two-way text binding
@Test func twoWayText() {
guard Gtk.initCheck() else { return }
let box = StateBox("hello")
let context = MountContext()
let entry = AnyView(Entry().text(Binding(box))).makeWidget(context) as! Gtk.Entry
#expect(entry.text == "hello")
box.set("world")
#expect(entry.text == "world")
entry.setText(text: "typed")
#expect(box.peek() == "typed")
}
// MARK: - Two-way toggle binding
@Test func twoWayToggle() {
guard Gtk.initCheck() else { return }
let box = StateBox(false)
let context = MountContext()
let toggle = AnyView(ToggleButton().active(Binding(box))).makeWidget(context) as! Gtk.ToggleButton
#expect(toggle.getActive() == false)
box.set(true)
#expect(toggle.getActive() == true)
toggle.setActive(isActive: false)
#expect(box.peek() == false)
}
// MARK: - No echo loop
@Test func noEchoLoop() {
guard Gtk.initCheck() else { return }
let box = StateBox("a")
let context = MountContext()
let entry = AnyView(Entry().text(Binding(box))).makeWidget(context) as! Gtk.Entry
box.set("a")
#expect(box.peek() == "a")
#expect(entry.text == "a")
}
// MARK: - Composite fallback
@Test func compositeFallback() {
guard Gtk.initCheck() else { return }
struct Wrapper: View {
var body: some View { Label(str: "x") }
}
let w = AnyView(Wrapper().hexpand(true)).makeWidget(MountContext())
#expect(w.getHexpand() == true)
}
// MARK: - Module-placement guarantee
/// `Adw.Window` descends from `Gtk.Window`, whose modifier extension is emitted
/// into the Portico module. This file deliberately does not import PorticoGtk,
/// so this fails to compile if that extension ever moves into PorticoGtk.
@Test func adwWindowSeesGtkWindowModifiers() {
guard Gtk.initCheck() else { return }
let w = AnyView(
Portico.Window().title("Inherited").resizable(false)
).makeWidget(MountContext())
guard let win = w as? Adw.Window else {
Issue.record("Expected Adw.Window, got \(type(of: w))")
return
}
#expect(win.getTitle() == "Inherited")
#expect(win.getResizable() == false)
}
}