Skip to content

Commit

Permalink
Xvfb screenshot diff test
Browse files Browse the repository at this point in the history
  • Loading branch information
benjajaja committed Mar 31, 2024
1 parent 5edec34 commit 733dc18
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 71 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ concurrency:
cancel-in-progress: true
env:
CLICOLOR_FORCE: 1

jobs:
ci:
name: CI
Expand Down Expand Up @@ -65,3 +66,38 @@ jobs:
run: cargo make readme
- name: Ensure no changes
run: git diff --exit-code .

xvfb:
name: Xvfb
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Install rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
profile: minimal
override: true
- uses: davidB/rust-cargo-make@v1
- name: Install terminals and tools
# xwd is in x11-apps, convert seems to be installed
run: sudo apt-get install -y x11-apps xterm
- name: Screenshot
run: cargo make screenshot-xvfb
- name: Upload screenshot
uses: actions/upload-artifact@v4
with:
name: screenshot.png
path: target/**/screenshot_*.png
if-no-files-found: error
- name: Diff
run: |
cargo install dify
cargo make screenshot-diff
- name: Upload diff
if: ${{ failure() }}
uses: actions/upload-artifact@v4
with:
name: diff.png
path: target/**/diff_*.png
90 changes: 26 additions & 64 deletions Makefile.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[env]
SCREENSHOT_TERM = "xterm -ti vt340 -fa DejaVu -fs 7 -bg black -fg white"
SCREENSHOT_TERM_NAME = "xterm"

[tasks.default]
alias = "ci"

Expand Down Expand Up @@ -53,18 +57,7 @@ args = [
"--features", "rustix,serde,crossterm",
]

# screenshot instructions for xmonad / tiling WMs:
# 1. cargo make screenshot-spawn-xterm
# 2. Float the new xterm window
# 3. In xterm: cargo make screenshot-capture
# 3b. If the resize caused a black screen, run last step again.

[tasks.screenshot]
dependencies = [
"screenshot-build",
"screenshot-capture-in-xterm",
"screenshot-diff",
]
# Screenshot tests

[tasks.screenshot-build]
command = "cargo"
Expand All @@ -75,52 +68,35 @@ args = [
"--features", "rustix,crossterm",
]

[tasks.demo-build]
command = "cargo"
args = [
"build",
"--example",
"demo",
"--features", "rustix,crossterm",
]


[tasks.screenshot-capture-in-xterm]
command = "cargo"
args = [
"make",
"screenshot-spawn-xterm",
"-e", "cargo make screenshot-capture",
[tasks.screenshot-xvfb]
dependencies = [
"screenshot-build",
]
script = '''
echo ${SCREENSHOT_TERM} ${PWD}/target/debug/examples/screenshot
xvfb-run -s "-screen 0 640x480x24" ${SCREENSHOT_TERM} ${PWD}/target/debug/examples/screenshot
'''

[tasks.screenshot-spawn-xterm]
command = "xterm"
[tasks.screenshot-diff]
command = "dify"
args = [
"-ti", "340",
"-fa", "DejaVu",
"-fs", "7",
"-bg", "black",
"-fg", "white",
"${@}"
"-t", "0.15",
"assets/screenshot_${SCREENSHOT_TERM_NAME}.png",
"target/screenshot_${SCREENSHOT_TERM_NAME}.png",
"-o", "target/diff_${SCREENSHOT_TERM_NAME}.png",
]

[tasks.screenshot-capture]
[tasks.screenshot-ci]
dependencies = [
"screenshot-build",
]
command = "menyoki"
args = [
"--quiet",
"capture",
"--countdown", "1",
# "--size", "200x300", # does not work with xmonad
# "--font", "-*-dejavu sans-*-*-*-*-7-*-*-*-*-*-*-*", # has no effect with xmonad
"./target/debug/examples/screenshot",
"png",
"save",
"target/screenshot.png",
"screenshot-xvfb",
"screenshot-diff",
]

[tasks.screenshot-update]
script = '''
cp target/screenshot_${SCREENSHOT_TERM_NAME}.png assets/screenshot_${SCREENSHOT_TERM_NAME}.png
'''

[tasks.screenshot-record]
dependencies = [
"demo-build",
Expand All @@ -138,17 +114,3 @@ args = [
"target/recording.gif",
]

[tasks.screenshot-diff]
command = "dify"
args = [
"-t", "0.15",
"assets/test_screenshot.png",
"target/screenshot.png",
"-o", "target/diff.png",
]

[tasks.xterm-stderr]
script = '''
cargo make screenshot-spawn-xterm -e "cargo run --example demo --features rustix,crossterm 2>$(tty)"
'''

4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ Compatibility and QA:

Terminal | Protocol | Fixed | Resize | Notes
----------|----------|-------|--------|-------
Xterm | `Sixel` | ✔️ | ✔️ | Run with `-ti 340` to make sure sixel support is enabled.
Xterm | `Sixel` | ✔️ | ✔️ | Run with `-ti 340` to make sure sixel support is enabled. [Latest Xvfb test screenshot](./assets/screenshot_xterm.png).
Foot | `Sixel` | ✔️ | ✔️ | Wayland.
kitty | `Kitty` | ✔️ | ✔️ |
Wezterm | `iTerm2` | ✔️ | ✔️ | Also would support `Sixel` and `Kitty`, but only `iTerm2` actually works bug-free.
Expand All @@ -144,6 +144,8 @@ Blackbox | `Sixel` | ❔ | ❔ | Untested.

Here, "Fixed" means the `Image` widget, and "Resize" is the `StatefulWidget`.

A basic screenshot test is run with xterm on Xvfb in the CI (or `cargo make screenshot-xvfb && cargo make screenshot-diff`).

Halfblocks should work in all terminals.

### Comparison:
Expand Down
Binary file added assets/screenshot_xterm.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed assets/test_screenshot.png
Binary file not shown.
33 changes: 27 additions & 6 deletions examples/screenshot.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{assert_eq, io};
use std::{
assert_eq, env, io,
process::{Command, Stdio},
};

use crossterm::{
execute,
Expand Down Expand Up @@ -38,10 +41,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut picker = Picker::from_termios()?;
picker.guess_protocol();
picker.background_color = Some(Rgb::<u8>([255, 0, 255]));
assert_eq!(
ASSERT_FONT_SIZE, picker.font_size,
"Font size must be fixed to a specific size",
);
if false {
assert_eq!(
ASSERT_FONT_SIZE, picker.font_size,
"Font size must be fixed to a specific size: {ASSERT_FONT_SIZE:?}",
);
}
let dyn_img = image::io::Reader::open("./assets/Ada.png")?.decode()?;
let image = picker.new_protocol(
dyn_img,
Expand All @@ -51,7 +56,23 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut app = App { image };

terminal.draw(|f| ui(f, &mut app))?;
std::thread::sleep(std::time::Duration::from_secs(3));
std::thread::sleep(std::time::Duration::from_secs(1)); // let the terminal actually draw.
let xwd = Command::new("xwd")
.args(["-root", "-silent"])
.stdout(Stdio::piped())
.spawn()
.expect("Failed to start xwd command");
let screenshot_term = env::var("SCREENSHOT_TERM_NAME").unwrap_or("unknown".to_string());
std::process::Command::new("convert")
.args([
"xwd:-",
&format!("png:./target/screenshot_{screenshot_term}.png"),
])
.stdin(xwd.stdout.expect("failed to get stdout"))
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.spawn()
.and_then(|mut child| child.wait())?;

// restore terminal
disable_raw_mode()?;
Expand Down

0 comments on commit 733dc18

Please sign in to comment.