wren-linux / tools / test-borders.sh
#!/bin/bash
# Checks window borders with a real sway of its own (headless, nothing on your screen) and real
# foot windows, in the sway snippets install-base.sh writes and the theme.conf bin/theme writes:
# none on tiled windows, 1 pixel on floating ones in the theme's color, the pop-ups a rule floats
# and windows floated with Super+Shift+space included (sway gives default_floating_border only
# to windows that open floating). The border rules are bin/theme's, as update.sh runs
# parts/theme.sh again when it changes, never install-base.sh.
# Run by test.sh, or: bash tools/test-borders.sh
# shellcheck disable=SC2016 # the inner shells expand their own variables
HERE=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)
T=$(mktemp -d /tmp/wbd.XXXX) || { echo " FAIL couldn't make a temp folder (is /tmp full?)"; exit 1; } # (short: sway's socket path has a length limit)
fails=0
check() { # DESCRIPTION CONDITION-COMMAND...
local what=$1
shift
if "$@" >/dev/null 2>&1; then printf ' ok %s\n' "$what"; else printf ' FAIL %s\n' "$what"; fails=$((fails + 1)); fi
}
B=$HERE/install-base.sh
# snippet NAME: the text install-base.sh puts in that sway snippet
snippet() { sed -n "/^put_snippet $1 <<'EOF'\$/,/^EOF\$/p" "$B" | sed '1d;$d'; }
# theme LIGHT|DARK: the theme.conf bin/theme writes, in a temp home (pretend swaymsg and the rest)
mkdir -p "$T/fake"
for p in swaymsg makoctl notify-send gsettings; do printf '#!/bin/sh\nexit 0\n' >"$T/fake/$p"; done
printf '#!/bin/sh\nexit 1\n' >"$T/fake/pgrep"
chmod +x "$T/fake/"*
theme() {
local h=$T/home-$1
mkdir -p "$h/.local/share/wren/themes" "$h/.config/sway/config.d" "$h/.config/wren"
cp "$HERE/data/themes/dark" "$HERE/data/themes/light" "$h/.local/share/wren/themes/"
echo "$1" >"$h/.config/wren/theme"
env HOME="$h" XDG_CONFIG_HOME="$h/.config" PATH="$T/fake:$PATH" bash "$HERE/bin/theme" apply >/dev/null 2>&1
# (without the bar's colors: this sway has no bar)
sed '/^bar /,/^}/d' "$h/.config/sway/config.d/theme.conf"
}
theme dark >"$T/dark.conf"
theme light >"$T/light.conf"
# every window a Wren Linux rule floats
floated=$(sed -n 's/^for_window \[app_id="\([^"]*\)"\] floating enable.*/\1/p' "$B")
check "theme.conf gives a 1 pixel border to every window a Wren Linux rule floats (${floated//$'\n'/ })" \
bash -c 'r=$(sed -n "s/^for_window \[app_id=\"\(.*\)\"\] border pixel 1$/\1/p" "$1"); [ -n "$r" ] && [ -n "$2" ] &&
for a in $2; do grep -qE "$r" <<<"$a" || exit 1; done && ! grep -qE "$r" <<<"wren-update"' _ "$T/dark.conf" "$floated"
if ! command -v sway >/dev/null || ! command -v foot >/dev/null || ! command -v python3 >/dev/null; then
echo " (no sway, foot or python3 here: not tried in a real sway)"
rm -rf "$T"
[ "$fails" -eq 0 ]
exit
fi
export XDG_RUNTIME_DIR=$T WLR_BACKENDS=headless WLR_RENDERER=pixman WLR_LIBINPUT_NO_DEVICES=1
unset WAYLAND_DISPLAY SWAYSOCK
SWAY=
trap 'stop_sway; rm -rf "$T"' EXIT
stop_sway() { [ -z "$SWAY" ] || { swaymsg -q exit 2>/dev/null; kill "$SWAY" 2>/dev/null; wait "$SWAY" 2>/dev/null; SWAY=; }; }
# start_sway THEME: a sway with the stock key, install-base.sh's snippets and that theme's theme.conf
start_sway() {
{
printf 'xwayland disable\nset $mod Mod4\nbindsym $mod+Shift+space floating toggle\n'
snippet popups
snippet appearance
cat "$T/$1.conf"
} >"$T/config"
rm -f "$T"/sway-ipc.*.sock
sway -c "$T/config" >"$T/log" 2>&1 &
SWAY=$!
for _ in $(seq 50); do ls "$T"/sway-ipc.*.sock >/dev/null 2>&1 && break; /bin/sleep 0.2; done
SWAYSOCK=$(ls "$T"/sway-ipc.*.sock 2>/dev/null) && export SWAYSOCK || { echo " FAIL sway didn't start: $(tail -3 "$T/log")"; exit 1; }
}
start_sway dark
# border APP-ID: "floating|tiled BORDER WIDTH" of that window
border() {
swaymsg -t get_tree | python3 -c 'import json, sys
def walk(n, f):
if n.get("app_id") == sys.argv[1]: print("floating" if f else "tiled", n["border"], n["current_border_width"])
for c in n.get("nodes", []): walk(c, f)
for c in n.get("floating_nodes", []): walk(c, True)
walk(json.load(sys.stdin), False)' "$1"
}
# wait_for APP-ID: until sway has that window
wait_for() { for _ in $(seq 50); do [ -n "$(border "$1")" ] && return; /bin/sleep 0.2; done; }
swaymsg -q exec "foot --app-id=wren-run"
wait_for wren-run
swaymsg -q exec "foot --app-id=plain"
wait_for plain
check "a pop-up (the Wren menu's window): floating, 1 pixel border" test "$(border wren-run)" = "floating pixel 1"
check "a tiled window: no border" test "$(border plain)" = "tiled none 2"
# Super+Shift+space: the command the snippet binds to it, as sway would run it
bind=$(swaymsg -t get_config | python3 -c 'import json, sys; c = json.load(sys.stdin)["config"]
import re
print([m.group(1) for m in (re.match(r"bindsym (?:--no-warn )?\$mod\+Shift\+space (.*)", l) for l in c.splitlines()) if m][-1])')
swaymsg -q '[app_id=plain] focus'
swaymsg -q "$bind" 2>/dev/null
/bin/sleep 0.3
check "Super+Shift+space floats a tiled window with a 1 pixel border" test "$(border plain)" = "floating pixel 1"
swaymsg -q "$bind" 2>/dev/null
/bin/sleep 0.3
check "...and pressed again, it is tiled with no border" bash -c '[[ $1 == "tiled none "* ]]' _ "$(border plain)"
check "sway took the snippets without an error" bash -c '! grep -q "\[ERROR\].*config" "$1"' _ "$T/log"
# the stock config binds Super+Shift+space first: overwriting it without --no-warn, sway shows a
# warning bar at every reload (it did, for real; this sway has no screen to show one)
check "...and the theme's binding overwrites the stock one with --no-warn, so sway shows no warning bar" \
grep -q '^bindsym --no-warn \$mod+Shift+space ' "$T/dark.conf"
# color THEME: the active border color sway was given (client.focused's border)
color() {
swaymsg -t get_config | python3 -c 'import json, sys
print([l.split()[1] for l in json.load(sys.stdin)["config"].splitlines() if l.startswith("client.focused ")][-1])'
}
check "dark theme: the active window's border is white" test "$(color)" = "#ffffff"
stop_sway
start_sway light
swaymsg -q exec "foot --app-id=wren-keys"
wait_for wren-keys
check "light theme: a pop-up (the key sheet) has its 1 pixel border too" test "$(border wren-keys)" = "floating pixel 1"
check "...and the active window's border is black" test "$(color)" = "#000000"
[ "$fails" -eq 0 ]