portico/Tests/PorticoTests/ConvenienceInitTests.swift

149 lines
6 KiB
Swift

import Testing
import Adw
@_spi(SGTKInternal) import Gtk
@_spi(Portico) import Portico
@MainActor @Suite(.serialized) struct ConvenienceInitTests {
@Test func actionRowStaticAndInterpolatedInitializers() {
guard Gtk.initCheck() else { return }
let row = AnyView(ActionRow("Title", subtitle: "Sub", iconName: "go-home"))
.makeWidget(MountContext()) as! Adw.ActionRow
#expect(row.getTitle() == "Title")
#expect(row.getSubtitle() == "Sub")
#expect(row.getIconName() == "go-home")
let box = StateBox("One")
let reactive = AnyView(ActionRow("Count: \(box.get())"))
.makeWidget(MountContext()) as! Adw.ActionRow
#expect(reactive.getTitle() == "Count: One")
box.set("Two")
#expect(reactive.getTitle() == "Count: Two")
}
@Test func actionRowBindingInitializerIsTwoWay() {
guard Gtk.initCheck() else { return }
let titleBox = StateBox("Title")
let subtitleBox = StateBox("Sub")
let context = MountContext()
let row = AnyView(ActionRow(
Binding(titleBox), subtitle: Binding(subtitleBox)
)).makeWidget(context) as! Adw.ActionRow
#expect(row.getTitle() == "Title")
#expect(row.getSubtitle() == "Sub")
titleBox.set("New")
subtitleBox.set("New sub")
#expect(row.getTitle() == "New")
#expect(row.getSubtitle() == "New sub")
row.setTitle(title: "Widget title")
row.setSubtitle(subtitle: "Widget sub")
#expect(titleBox.peek() == "Widget title")
#expect(subtitleBox.peek() == "Widget sub")
}
@Test func switchEntryPasswordAndSpinMixedBindings() {
guard Gtk.initCheck() else { return }
let context = MountContext()
let activeBox = StateBox(false)
let switchRow = AnyView(SwitchRow("Wi-Fi", active: Binding(activeBox)))
.makeWidget(context) as! Adw.SwitchRow
activeBox.set(true)
#expect(switchRow.getActive() == true)
switchRow.setActive(isActive: false)
#expect(activeBox.peek() == false)
let textBox = StateBox("initial")
let entry = AnyView(EntryRow("Name", text: Binding(textBox)))
.makeWidget(context) as! Adw.EntryRow
textBox.set("changed")
#expect(entry.getText() == "changed")
entry.setText(text: "typed")
#expect(textBox.peek() == "typed")
let passwordBox = StateBox("secret")
let password = AnyView(PasswordEntryRow("Password", text: Binding(passwordBox)))
.makeWidget(context) as! Adw.PasswordEntryRow
password.setText(text: "updated")
#expect(passwordBox.peek() == "updated")
passwordBox.set("again")
#expect(password.getText() == "again")
let valueBox = StateBox(2.0)
let adjustment = Adjustment(value: 1, lower: 0, upper: 10, stepIncrement: 1, pageIncrement: 1, pageSize: 0)
let spin = AnyView(SpinRow("Zoom", adjustment: adjustment, value: Binding(valueBox)))
.makeWidget(context) as! Adw.SpinRow
#expect(spin.getValue() == 2.0)
spin.setValue(value: 4.0)
#expect(valueBox.peek() == 4.0)
}
@Test func bannerAndButtonRowInitializers() {
guard Gtk.initCheck() else { return }
let revealedBox = StateBox(false)
let context = MountContext()
let banner = AnyView(Banner("Offline", revealed: Binding(revealedBox)))
.makeWidget(context) as! Adw.Banner
#expect(banner.getTitle() == "Offline")
revealedBox.set(true)
#expect(banner.getRevealed() == true)
banner.setRevealed(revealed: false)
#expect(revealedBox.peek() == false)
let button = AnyView(ButtonRow("Save", startIconName: "document-save") { })
.makeWidget(context) as! Adw.ButtonRow
#expect(button.getTitle() == "Save")
#expect(button.getStartIconName() == "document-save")
}
@Test func expanderAndPreferencesGroupMountChildren() {
guard Gtk.initCheck() else { return }
let expander = AnyView(ExpanderRow("Advanced", subtitle: "More") {
Label("A")
Label("B")
}).makeWidget(MountContext()) as! Adw.ExpanderRow
#expect(expander.getTitle() == "Advanced")
#expect(expander.getSubtitle() == "More")
#expect(expander.getFirstChild() != nil)
let group = AnyView(PreferencesGroup("General", description: "d") {
ActionRow("One")
}).makeWidget(MountContext()) as! Adw.PreferencesGroup
#expect(group.getTitle() == "General")
#expect(group.getDescription() == "d")
#expect(group.getFirstChild() != nil)
}
@Test func statusPageMountsChildAndText() {
guard Gtk.initCheck() else { return }
let page = AnyView(StatusPage("Nothing here", description: "d", iconName: "face-smile") {
Label("x")
}).makeWidget(MountContext()) as! Adw.StatusPage
#expect(page.getTitle() == "Nothing here")
#expect(page.getDescription() == "d")
#expect(page.getIconName() == "face-smile")
#expect(page.getChild() != nil)
}
@Test func comboRowStaticSelectionCallback() {
guard Gtk.initCheck() else { return }
var selected: String?
let row = AnyView(ComboRow("Theme", items: ["Light", "Dark"]) {
selected = $0
}).makeWidget(MountContext()) as! Adw.ComboRow
#expect(row.getModel() != nil)
row.setSelected(position: 1)
#expect(selected == "Dark")
}
@Test func comboRowBindingItemsUpdatesSelectionCallback() {
guard Gtk.initCheck() else { return }
let titleBox = StateBox("Theme")
let itemsBox = StateBox(["A", "B"])
var selected: String?
let context = MountContext()
let row = AnyView(ComboRow(Binding(titleBox), items: Binding(itemsBox)) {
selected = $0
}).makeWidget(context) as! Adw.ComboRow
itemsBox.set(["X", "Y", "Z"])
row.setSelected(position: 2)
#expect(selected == "Z")
}
}