Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-2d

Source of release 0.5.0

0.5.0 / tests / workspace_tests.luc

## The workspace headless: layers and picking, the view's fit and zoom, the
## checkerboard, documents and unsaved work, settings kept across runs.
from luce_ui.ui import Font
from luce_gpu.gpu import Frame
from workspace import Workspace
from luce_image.canvas import Canvas
from panels import Panels
from actions import Actions
from view import CanvasView
from view_aids import clamp_texel, texel_span
from adjustments import adjustments
from settings import Settings, Store
from settings_panel import SettingsDialog
from update_check import newer
from tool_memory import tools_path, restore_tools, remember_tools
from luce_std import paths, files
from layer_transfer import copy_layers, paste_layers, send_layers
from brush_dynamics import mapped
from luce_painting.brush_mask import BrushMask
from brush_library import brush_text
from luce_window import window
from test_input import near

pub func layers() -> unit!:
    let workspace = Workspace(640, 480)
    assert(workspace.canvas.width() == 640 and workspace.canvas.height() == 480 and workspace.canvas.layer_count() == 1)
    workspace.edits.add_layer()
    workspace.edits.add_layer()
    assert(workspace.canvas.layer_count() == 3 and workspace.layers.selected == 2)
    workspace.edits.toggle_visibility()
    assert(not workspace.canvas.layer_visible(2))
    workspace.edits.set_opacity(0.25)
    assert(workspace.canvas.layer_opacity(2) == 0.25)
    workspace.layers.select(0)
    workspace.edits.delete_layer()
    assert(workspace.canvas.layer_count() == 2 and workspace.layers.selected == 0)
    workspace.documents.create(32, 16)
    assert(workspace.canvas.width() == 32 and workspace.canvas.layer_count() == 1 and workspace.live.path == "")

pub func viewing() -> unit!:
    let workspace = Workspace(200, 100)
    workspace.viewport.fit(1000.0, 600.0)
    assert(workspace.viewport.zoom == 4.6 and workspace.viewport.offset_x == 40.0 and workspace.viewport.offset_y == 70.0)
    workspace.viewport.zoom_about(2.0, 40.0, 70.0)
    assert(workspace.viewport.zoom == 9.2 and workspace.viewport.offset_x == 40.0 and workspace.viewport.offset_y == 70.0)
    # Zooming and panning keep the first fit done, so the view does not fit again over them.
    assert(workspace.viewport.fitted)
    workspace.viewport.pan(5.0, 5.0)
    assert(workspace.viewport.fitted and workspace.viewport.zoom == 9.2)
    workspace.viewport.pan(-5.0, -5.0)
    workspace.viewport.zoom_about(0.5, 0.0, 0.0)
    assert(workspace.viewport.zoom == 4.6 and workspace.viewport.offset_x == 20.0 and workspace.viewport.offset_y == 35.0)
    workspace.viewport.pan(5.0, -5.0)
    assert(workspace.viewport.offset_x == 25.0 and workspace.viewport.offset_y == 30.0)
    workspace.viewport.actual_size(1000.0, 600.0)
    assert(workspace.viewport.zoom == 1.0 and workspace.viewport.offset_x == 400.0 and workspace.viewport.offset_y == 250.0)
    let view = CanvasView(workspace)
    view.layout().set_size(1000.0, 600.0)
    let frame = Frame(window.Size(1000.0, 600.0, 1000.0, 600.0, 1.0))
    let target = frame.target()
    view.draw(target)
    frame.close()

pub func contents() -> unit!:
    let workspace = Workspace(300, 200)
    let font = Font()
    let actions = Actions()
    let panels = Panels(workspace, actions, font)
    actions.add_layer.trigger()
    assert(workspace.canvas.layer_count() == 2)
    panels.select_layer(0)
    assert(workspace.layers.selected == 0)
    actions.zoom_in.trigger()
    assert(workspace.viewport.zoom == 2.0)
    assert(not actions.undo.is_enabled() and not actions.crop.is_enabled())
    actions.select_all.trigger()
    assert(actions.crop.is_enabled())
    actions.tools[10].trigger()
    assert(workspace.tools.tool == "brush")
    actions.tools[5].trigger()
    assert(workspace.tools.tool == "wand")
    # The Crop tool's box starts around the whole canvas and goes with the tool.
    actions.tools[6].trigger()
    let crop = workspace.image.crop_box else trap("the crop tool shows its box")
    assert(workspace.tools.tool == "crop" and crop.width() == 300 and crop.height() == 200)
    actions.tools[5].trigger()
    assert(workspace.image.crop_box == none)
    # Brush Settings opens floating, once; docked, it stays where it was put.
    panels.open_brush()
    assert(panels.dock.is_floating(panels.brush.panel) and panels.dock.panel_count() == 6)
    panels.open_brush()
    assert(panels.dock.panel_count() == 6)
    panels.dock.move(panels.brush.panel, panels.properties.panel)
    panels.open_brush()
    assert(not panels.dock.is_floating(panels.brush.panel) and panels.dock.same_group(panels.brush.panel, panels.properties.panel))
    panels.close()

## Settings parse their defaults, survive a round trip through the file text,
## and reject a unit they do not know.
pub func settings_round_trip() -> unit!:
    let fresh = Settings()
    assert(fresh.canvas_width == 1280 and fresh.canvas_height == 800 and fresh.accent == "#e7a03f" and fresh.control_radius == 5.0)
    let edited = fresh.copy()
    edited.canvas_width = 640
    edited.accent = "#3f8ae7"
    edited.control_radius = 0.0
    assert(fresh.memory_usage == 60.0)
    edited.memory_usage = 40.0
    assert(fresh.scratch == "")
    edited.scratch = "D:\\Scratch\\luced"
    edited.shortcuts["save"] = "cmd+shift+s"
    edited.shortcuts["swap_colors"] = "none"
    let reread = Settings(edited.serialize())
    assert(reread.canvas_width == 640 and reread.canvas_height == 800 and reread.accent == "#3f8ae7" and reread.control_radius == 0.0)
    assert(reread.memory_usage == 40.0 and reread.scratch == "D:\\Scratch\\luced")
    assert(reread.shortcuts["save"] == "cmd+shift+s" and reread.shortcuts["swap_colors"] == "none" and reread.shortcuts.length == 2)
    assert(fresh.shortcuts.length == 0)
    var rejected = false
    _ = Settings("[view]\nunits = \"furlongs\"\n") catch failure:
        _ = failure
        rejected = true
        return
    assert(rejected)

## A checker piece of any fractional size and place maps to a source region of
## whole texels inside the 256-texel tile; a pan once rounded a sliver to zero width
## ("an image source region must lie inside its texture").
pub func checker_regions() -> unit!:
    assert(clamp_texel(-3) == 0 and clamp_texel(300) == 255 and clamp_texel(12) == 12)
    assert(texel_span(0, 0) == 1 and texel_span(255, 400) == 1 and texel_span(10, 20) == 10 and texel_span(0, 256) == 256)
    var start = 0
    while start < 256:
        var end = start - 2
        while end < 260:
            let span = texel_span(clamp_texel(start), end)
            assert(span >= 1 and clamp_texel(start) + span <= 256)
            end += 7
        start += 5

## Several layers selected, as Photoshop's Layers panel: Cmd-click adds or
## takes one out, Shift-click takes a run, a plain select keeps one.
pub func picking() -> unit!:
    let workspace = Workspace(64, 64)
    workspace.edits.add_layer()
    workspace.edits.add_layer()
    workspace.edits.add_layer()
    assert(workspace.canvas.layer_count() == 4 and workspace.layers.selected == 3)
    workspace.layers.toggle_picked(1)
    assert(workspace.layers.selected == 1 and workspace.layers.is_selected(3) and workspace.layers.is_selected(1) and not workspace.layers.is_selected(2))
    let ids = workspace.layers.selected_ids()
    assert(ids.length == 2 and ids[0] == workspace.canvas.layer_id(1) and ids[1] == workspace.canvas.layer_id(3))
    # Cmd-click again takes it out; the one the tools act on stays.
    workspace.layers.toggle_picked(3)
    assert(not workspace.layers.is_selected(3) and workspace.layers.selected_ids().length == 1)
    workspace.layers.pick_range(3)
    assert(workspace.layers.selected == 3 and workspace.layers.selected_ids().length == 3 and not workspace.layers.is_selected(0))
    workspace.layers.select(0)
    assert(workspace.layers.selected_ids().length == 1)
    # Deleting several at once keeps the rest.
    workspace.layers.pick_range(2)
    workspace.edits.delete_layer()
    assert(workspace.canvas.layer_count() == 1)

## The adjustments' table: every kind at its engine number, its values laid
## out page by page then toggles, and the engine's numbers read back as shown.
pub func adjustment_table() -> unit!:
    let table = adjustments()
    var index = 0
    for adjustment in table:
        assert(adjustment.kind == index and adjustment.defaults().length == adjustment.value_count())
        index += 1
    let levels = table[3]
    assert(levels.value_count() == 20 and levels.defaults()[1] == 255.0 and levels.defaults()[6] == 255.0)
    let numbers = levels.engine(levels.defaults())
    assert(near(numbers[1], 1.0) and near(numbers[2], 1.0) and near(numbers[7], 1.0))
    let back = levels.shown_of(numbers)
    assert(near(back[1], 255.0) and near(back[2], 1.0))
    # Hue/Saturation: seven ranges of three, then Colorize; Color Balance keeps luminosity by default.
    assert(table[1].value_count() == 22 and table[10].defaults()[9] == 1.0 and table[7].layerable)

## Unsaved work: a changed tab asks before it closes and quitting asks too;
## Save writes an .l2d and clears the mark; exporting leaves both alone.
pub func unsaved_changes(arguments: list[str]) -> unit!:
    # The runner passes a scratch folder to write in.
    let folder = arguments[arguments.length - 1]
    let workspace = Workspace(64, 48)
    workspace.canvas.bind_headless()
    let panels = Panels(workspace, Actions(), Font())
    assert(not workspace.documents.dirty(0) and panels.files.can_quit())
    workspace.edits.add_layer()
    assert(workspace.documents.dirty(0))
    panels.files.close_document_at(0)
    assert(panels.files.confirm.is_open() and workspace.documents.count() == 1 and workspace.canvas.layer_count() == 2)
    assert(not panels.files.can_quit())
    assert(workspace.documents.export_name(".png") == "Untitled.png")
    workspace.documents.save(folder + "/unsaved")
    assert(workspace.live.path == folder + "/unsaved.l2d" and not workspace.documents.dirty(0))
    assert(workspace.documents.export_name(".jpg") == "unsaved.jpg")
    workspace.documents.export(folder + "/unsaved.png")
    assert(workspace.live.path == folder + "/unsaved.l2d" and not workspace.documents.dirty(0) and panels.files.can_quit())
    # Nothing past the canvas: Reveal All has nothing to do.
    assert(not workspace.image.reveal_all())
    # The saved file opens in a tab of its own, its two layers back, and the
    # layer selected when it was saved selected again.
    workspace.layers.select(0)
    workspace.documents.save(folder + "/copy")
    workspace.documents.open(folder + "/unsaved.l2d")
    assert(workspace.documents.count() == 2 and workspace.canvas.layer_count() == 2 and workspace.live.path == folder + "/unsaved.l2d")
    assert(workspace.layers.selected == 1)
    workspace.documents.open(folder + "/copy.l2d")
    assert(workspace.layers.selected == 0)
    panels.close()

## Whole layers: copied with nothing selected they paste above the selected
## layer; cut, they go; dragged onto another tab, they land there centred.
pub func whole_layers() -> unit!:
    let workspace = Workspace(40, 30)
    workspace.canvas.bind_headless()
    workspace.edits.add_layer()
    workspace.previews.set_style_value(0, 1.0)
    copy_layers(workspace, false)
    assert(Canvas.copied_layers() == 1)
    paste_layers(workspace, false)
    assert(workspace.canvas.layer_count() == 3 and workspace.layers.selected == 2 and workspace.canvas.style_value(2, 0) == 1.0)
    copy_layers(workspace, true)
    assert(workspace.canvas.layer_count() == 2)
    paste_layers(workspace, true)
    assert(workspace.canvas.layer_count() == 3)
    let source = workspace.canvas
    workspace.documents.create(80, 60)
    assert(workspace.documents.current == 1)
    workspace.documents.switch_to(0)
    send_layers(workspace, 1)
    assert(workspace.documents.current == 1 and workspace.canvas.layer_count() == 2 and workspace.canvas.style_value(1, 0) == 1.0)
    assert(source.layer_count() == 3)

## Tool options written when the window closes come back when it opens.
pub func tool_options_kept() -> unit!:
    let path = tools_path(paths.join(files.home_directory(), ".luced-2d-test"))
    let first = Workspace(40, 30)
    first.tools.tool = "lasso"
    first.tools.brush.diameter = 77.0
    first.tools.selecting.feather = 9
    first.tools.typing.family = "Menlo"
    first.tools.gradient_radial = true
    remember_tools(first, path)
    let second = Workspace(40, 30)
    restore_tools(second, path)
    assert(second.tools.tool == "lasso" and second.tools.brush.diameter == 77.0 and second.tools.selecting.feather == 9 and second.tools.typing.family == "Menlo" and second.tools.gradient_radial)
    # The brush in hand comes back whole: its dynamics, tip and texture, and the library brush it was.
    first.tools.brush.dynamics[3] = mapped(3)
    first.tools.brush.dynamics[2] = mapped(8, 0.2, 0.5, 30)
    let tip = BrushMask.create(20, 10)
    tip.dab(5.0, 5.0, 4.0, 1.0, 1.0)
    first.tools.brush.tip = tip
    let texture = BrushMask.create(8, 8)
    texture.clear(1.0)
    first.tools.brush.texture_image = texture
    first.tools.brush.texture = 4
    first.tools.brush.preset = "My \"Ink\""
    first.tools.brush.folder = "my-ink"
    remember_tools(first, path)
    let fourth = Workspace(40, 30)
    restore_tools(fourth, path)
    assert(fourth.tools.brush.preset == "My \"Ink\"" and fourth.tools.brush.folder == "my-ink" and fourth.tools.brush.texture == 4)
    assert(brush_text(fourth.tools.brush, "x") == brush_text(first.tools.brush, "x"))
    let back = fourth.tools.brush.tip else trap("the tip comes back")
    assert(back.coverage() == tip.coverage() and fourth.tools.brush.texture_image != none)
    # What the file gets wrong keeps its default.
    files.write_text(path, "[brush]\ndiameter = -5\n[tool]\ncurrent = \"nothing\"\n")
    let third = Workspace(40, 30)
    restore_tools(third, path)
    assert(third.tools.brush.diameter == 32.0 and third.tools.tool == "brush")

## The update feed tells of a later release only.
pub func update_versions() -> unit!:
    assert(newer("0.1.58", "0.1.57") and newer("0.2.0", "0.1.99") and newer("1.0.0", "0.9.9"))
    assert(not newer("0.1.57", "0.1.57") and not newer("0.1.9", "0.1.10") and not newer("error: offline", "0.1.57") and not newer("", "0.1.57"))

## HEIC photos and camera raws open, as the other pictures.
pub func opens_photos() -> unit!:
    let workspace = Workspace(16, 16)
    for name in ["IMG_0001.HEIC", "a.heif", "b.dng", "c.CR2", "c.cr3", "d.nef", "e.arw", "f.orf", "g.rw2", "h.pef", "i.raf"]:
        assert(workspace.documents.can_open(name) and workspace.documents.can_place(name))
    assert(not workspace.documents.can_open("notes.txt"))

## Save takes what a text field shows even when Enter was never pressed.
pub func settings_save_pending(arguments: list[str]) -> unit!:
    let store = Store(arguments[arguments.length - 1])
    let dialog = SettingsDialog(Workspace(300, 200), Actions(), Font(), store)
    dialog.open()
    dialog.scratch_field.set_text("/tmp/luced-scratch-test")
    dialog.save()
    assert(store.load().scratch == "/tmp/luced-scratch-test")
    dialog.open()
    dialog.scratch_field.set_text("")
    dialog.save()
    assert(store.load().scratch == "")