From 13770d96053e3d6fdc0d23271956e324c76c3745 Mon Sep 17 00:00:00 2001 From: Thiago Holanda Date: Wed, 25 Nov 2026 14:16:25 +0100 Subject: [PATCH 1/6] Add main structure for tests --- .claude/skills/dessen-app/SKILL.md | 20 +- .gitignore | 4 +- AGENTS.md | 59 ++- Makefile | 28 +- apps/xpui/Cargo.lock | 118 +++++ apps/xpui/Cargo.toml | 5 +- docs/writing-apps.md | 30 +- host/DesktopServices.cpp | 12 +- host/Snapshot.h | 75 +++ host/TraceTarget.h | 88 +++- host/main.cpp | 44 +- tools/readstats_fixture.py | 2 +- tools/screen_test.py | 764 +++++++++++++++++++++++++++++ 13 files changed, 1217 insertions(+), 32 deletions(-) create mode 100644 apps/xpui/Cargo.lock create mode 100644 host/Snapshot.h create mode 100644 tools/screen_test.py diff --git a/.claude/skills/dessen-app/SKILL.md b/.claude/skills/dessen-app/SKILL.md index 20af8cf..698d94d 100644 --- a/.claude/skills/dessen-app/SKILL.md +++ b/.claude/skills/dessen-app/SKILL.md @@ -111,7 +111,22 @@ and the reference implementation is `apps/xpui/nook` (Book Nook). In short: back to front with a paper edge; one grey for material and shadow; sizes measured from the box; tens of draw calls per piece. - **Done means looked at:** render each screen in the simulator, and leave a - `make test` check that opens every screen and comes back. + screen-test walk for every screen (below). + +## Testing an xpui app + +An xpui app is not done without screen tests and, for its logic, unit tests. +The recipe is in [`AGENTS.md`](../../../AGENTS.md#recipe-test-an-xpui-app) and the +reference in [`tests/xpui/README.md`](../../../tests/xpui/README.md). In short: + +- `tests/xpui/.scenarios` (copy `nook.scenarios`): a walk per screen and + state, each with `expect="..."`, tapping by label (`@Label`) where it can. +- `make xpui-screens` writes the goldens on the first run and fails; open every + PNG, then run again. Commit the goldens with the app. +- When a screen fails after a change: read what moved, look at + `build/screen-diffs/`, fix it or (if meant) `make xpui-bless` and say so. + Never bless unseen; never raise `SCREEN_TOLERANCE`. +- Logic that draws nothing: a crate in `tests/xpui/unit/` (copy `lightsout/`). ## The API surface @@ -203,4 +218,7 @@ make sd # stage build/sd/plugins// for an SD card make rust # build + run the Rust example (needs the Rust toolchain) make zig # build + run the Zig example (needs Zig) make size # ESP32-C3 flash cost of the VM + binding +make xpui-screens # xpui screen tests (pictures + positions, 3 sizes) +make xpui-bless # accept deliberate screen changes, after looking +make xpui-unit # xpui host unit tests ``` diff --git a/.gitignore b/.gitignore index 55a90dd..e1bb17c 100644 --- a/.gitignore +++ b/.gitignore @@ -11,8 +11,10 @@ docs/proposed-capabilities.md # Generated by Cargo # will have compiled files and executables sdk/rust/target +# The SDK is a library: its users resolve its dependencies. The apps' and the +# tests' lockfiles are committed: they pin the xpui the screen goldens were +# drawn with (tests/xpui/README.md). sdk/rust/Cargo.lock -Cargo.lock debug/ target/ **/*.rs.bk diff --git a/AGENTS.md b/AGENTS.md index d052640..dcad5c1 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -50,7 +50,10 @@ the **crosspoint-reader** repo on branch `feat/dessen-apps`, behind interpreter and binding are tiny on purpose. Add a call only when a real app needs it; don't speculatively mirror the whole SDK. 5. **Leave a test.** Non-trivial logic gets one runnable check (a self-checking - app or a `test_host.cpp` assertion) wired into `make test`. + app or a `test_host.cpp` assertion) wired into `make test`. An **xpui app** + gets screen tests for every screen and unit tests for its logic; see + [Recipe: test an xpui app](#recipe-test-an-xpui-app). Never bless a golden + you have not opened, and never raise the tolerance to make a test pass. ## Build & test @@ -61,8 +64,17 @@ make test RUST=no # C only, when Rust (riscv32im target) or xpui-backends is a make size # ESP32-C3 flash cost of the VM and the binding zig build # the Zig apps with Zig alone (build.zig, also read by zls); zig build run -Dapp=hello make FREEINK_SDK=/path/to/freeink-sdk test # if the SDK isn't a sibling dir +make xpui-screens # the xpui screen tests only: pictures + element positions, 3 sizes +make xpui-bless # rewrite the goldens after a deliberate change (look first!) +make xpui-standards # what each xpui app still breaks of the screen standards +make xpui-unit # the xpui apps' host unit tests only ``` +`make test` is what CI runs (`.github/workflows/test.yml`, on every pull +request); it fails on any screen, element, standard or unit test failure, and +uploads the screen diffs as an artifact. More in +[`tests/xpui/README.md`](tests/xpui/README.md#in-ci). + The RISC-V compiler is expected at `~/.platformio/tools/toolchain-riscv32-esp/`. Note PlatformIO sometimes removes the copy under `packages/`; the Makefile uses the stable `tools/` path. @@ -97,6 +109,48 @@ the stable `tools/` path. manifest it checks; exit 103 = the board cannot drive the app). 7. Add a check and run `make test`. +## Recipe: test an xpui app + +Every xpui app (new or changed) is covered by **screen tests**: walks that +drive it to each screen, drawn with FreeInk's real renderer and fonts at +480×800, 800×480 and 1448×1072, compared with committed PNG goldens and +element positions within 1 px. Pure logic gets **unit tests** on the host. +Full reference: [`tests/xpui/README.md`](tests/xpui/README.md). + +For a **new** app: + +1. Add it to `XPUI_APPS` in the `Makefile` and to `apps/xpui/Cargo.toml`. +2. Write `tests/xpui/.scenarios`, copying `tests/xpui/nook.scenarios`. + One walk per reachable screen and state: the root screen (per board if it + differs), every pushed screen (`pushed`) and both ways back (`@back`, `b`), + every dialog and each answer, empty/error/old-firmware states, a step of + the main loop by keys and by touch, and leaving the app. +3. Give every walk `expect="..."`: text only its final screen shows. Tap by + label (`@New`, `@"Keep Playing"`); a pixel tap (`t173,291`) only where + nothing is labelled, one walk per size (`sizes=480x800`). +4. Run `make xpui-standards` and list what it passes on `standards =` (a new + app should pass `bar taps actions`). +5. Run `make xpui-screens`. The first run writes the goldens and **fails on + purpose**. Open every new PNG in `tests/xpui/golden//` and confirm it + shows the screen the walk names; run again, it must pass. +6. If the app has files that draw nothing, add a crate in `tests/xpui/unit/` + (copy `unit/lightsout/`; it `#[path]`-includes the app's files) and add it + to the workspace members. +7. `make test`. Commit the scenarios, the goldens and the unit tests with the + app. + +For a **change** to how an app draws (a refactor, a new xpui component, a +framework migration): + +1. `make xpui-screens` passes before you start. +2. After the change, each failure names what moved (`text 'How to Play' moved + +0,+2`) and writes a diff image to `build/screen-diffs//` (red = moved + beyond tolerance). Look at it. +3. Unintended: fix the code. Intended: tell the user what moved and why, then + `make xpui-bless`, open the changed PNGs, and commit them with the code. +4. A walk that now fails "did not reach its screen" means its tap missed the + new layout: re-aim the tap, don't delete the walk. + ## Recipe: add a capability (one new call) When an app needs something FreeInk/CrossPoint can do but no call exposes: @@ -162,7 +216,8 @@ checklist, is [`docs/writing-apps.md`](docs/writing-apps.md#screen-standards); 9. **E-paper.** Redraw only on change; the same state draws the same picture; no animation. 10. **Look at it.** Check each screen in the simulator before calling it done, - and leave a `make test` check that opens every screen and comes back. + and leave a screen-test walk for every screen + ([Recipe: test an xpui app](#recipe-test-an-xpui-app)). Game graphics follow [`docs/drawing-icons.md`](docs/drawing-icons.md). ## When NOT to proceed without asking diff --git a/Makefile b/Makefile index c14dfd4..056c6ec 100644 --- a/Makefile +++ b/Makefile @@ -37,6 +37,9 @@ endif GEN = sdk/dessen_sysnums.h sdk/dessen_calls.h freeink/dessen_dispatch.inc HOSTFLAGS = -std=c++17 -O2 -Wall -Wextra -Ivm -Isdk -Ifreeink -Ihost $(OPENSSL_CFLAGS) -I$(FREEINK_SDK)/libs/ui/FreeInkUI/include +# The desktop runner's snapshots (host/Snapshot.h) draw with FreeInk's +# framebuffer target and its gallery fonts, and write PNGs with zlib. +SNAPFLAGS = -I$(FREEINK_SDK)/libs/ui/FreeInkUI/tools BINDING = sdk/dessen_abi.h freeink/DessenFreeInk.cpp freeink/DessenFreeInk.h freeink/DessenServices.h host/DesktopServices.cpp host/DesktopServices.h host/TraceTarget.h host/Manifest.h vm/dessen_vm.h $(GEN) all: build/dessen build/test_freeink $(APPS:%=build/%.bin) @@ -54,8 +57,9 @@ build/dessen_vm.o: vm/dessen_vm.c vm/dessen_vm.h sdk/dessen_abi.h $(GEN) @mkdir -p build cc -O2 -Wall -Wextra -Ivm -Isdk -c vm/dessen_vm.c -o $@ -build/dessen: host/main.cpp build/dessen_vm.o $(BINDING) - c++ $(HOSTFLAGS) host/main.cpp freeink/DessenFreeInk.cpp host/DesktopServices.cpp build/dessen_vm.o $(OPENSSL_LIBS) -o $@ +build/dessen: host/main.cpp host/Snapshot.h build/dessen_vm.o $(BINDING) + c++ $(HOSTFLAGS) $(SNAPFLAGS) host/main.cpp freeink/DessenFreeInk.cpp host/DesktopServices.cpp \ + $(FREEINK_SDK)/libs/ui/FreeInkUI/src/FreeInkUI.cpp build/dessen_vm.o $(OPENSSL_LIBS) -lz -o $@ build/test_freeink: freeink/test_host.cpp build/dessen_vm.o $(BINDING) c++ $(HOSTFLAGS) freeink/test_host.cpp freeink/DessenFreeInk.cpp host/DesktopServices.cpp build/dessen_vm.o $(OPENSSL_LIBS) -o $@ @@ -202,8 +206,26 @@ xpui: $(XPUI_APPS:%=build/%_xpui.bin) build/dessen printf nnc | build/dessen build/home_xpui.bin | grep -q 'launch reader /books/neuromancer.epub' build/dessen build/home_xpui.bin .scenarios` + (with `expect=` text that proves it got there), + and the standards it meets on its `standards =` line + ([tests/xpui/README.md](../tests/xpui/README.md)). - [ ] Heap and stack measured with every screen open (`make xpui-peaks`). ## Constraints to design around @@ -428,6 +432,30 @@ in this repo: Wire either into the `test:` target so `make test` covers your app. +The xpui apps have their own net, for changing how they draw without changing +what they show ([tests/xpui/README.md](../tests/xpui/README.md)): + +3. **Screen tests:** walks through every screen, at three screen sizes, drawn + with FreeInk's own framebuffer target and real fonts. Each screen's picture + and the position of every text and button are compared with committed + goldens, within 1 px, and checked against the screen standards + (`make xpui-screens`; `make xpui-bless` after a deliberate change; + `make xpui-standards` for what each app still breaks). +4. **Unit tests on the host:** the apps' logic, compiled from their own files + against a scriptable stand-in for the SDK (`make xpui-unit`). + +A new xpui app ships with both. The steps (a walk per screen, `expect=`, +looking at every golden before committing it) are in +[tests/xpui/README.md](../tests/xpui/README.md#testing-a-new-xpui-app-checklist); +what CI needs to run them is under [In CI](../tests/xpui/README.md#in-ci). + +The desktop runner takes, besides the keys and taps above, `T,;` (a +long press) and `^ v < >` (swipes). `DESSEN_TRACE=detail` prints every argument +of every draw call, `DESSEN_SNAPSHOT=` draws with FreeInk's +framebuffer target and real fonts and writes the last frame there, +`DESSEN_NOW=` stops the clock, and `DESSEN_SEED=` makes the +random numbers the same on every run. + ## Reacting to firmware events The firmware emits a small set of events (`reader.open`, `reader.exit`, diff --git a/host/DesktopServices.cpp b/host/DesktopServices.cpp index 9e6fd38..33915e2 100644 --- a/host/DesktopServices.cpp +++ b/host/DesktopServices.cpp @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -182,10 +183,17 @@ DesktopServices::DesktopServices(std::string fsRoot, std::string appDir, std::st : fsRoot_(std::move(fsRoot)), appDir_(std::move(appDir)), netRoot_(std::move(netRoot)) {} // ---- core ---- -uint32_t DesktopServices::timeNow() { return static_cast(::time(nullptr)); } +// DESSEN_NOW= stops the clock there, so a trace golden draws +// the same status line and "today" on every run (tools/screen_test.py). +static time_t desktopNow() { + const char* fixed = getenv("DESSEN_NOW"); + return fixed ? static_cast(strtoll(fixed, nullptr, 10)) : ::time(nullptr); +} + +uint32_t DesktopServices::timeNow() { return static_cast(desktopNow()); } int DesktopServices::clock(uint8_t* out) { - const time_t now = ::time(nullptr); + const time_t now = desktopNow(); struct tm t; localtime_r(&now, &t); const unsigned year = static_cast(t.tm_year + 1900); diff --git a/host/Snapshot.h b/host/Snapshot.h new file mode 100644 index 0000000..9381963 --- /dev/null +++ b/host/Snapshot.h @@ -0,0 +1,75 @@ +// The desktop runner's picture of the screen: FreeInk's own framebuffer +// target (DisplayTarget: real glyphs, dithered grays, bitmaps), and the last +// frame the app refreshed written as a 1-bit PNG. DESSEN_SNAPSHOT= +// turns it on; tools/screen_test.py compares the pictures. +#pragma once +#include +#include + +#include +#include +#include + +// The fonts the pictures are drawn with: FreeInk's bundled Noto Sans for body +// text (34 px lines, the size of a device's body font) and its gallery fonts +// for the small (18 px) and title (43 px) slots. Not the firmware's own fonts, +// but real glyph metrics: text that stops fitting shows. +#include // $(FREEINK_SDK)/libs/ui/FreeInkUI/tools +#include + +struct Snapshot { + int16_t width, height, rowBytes; + std::vector fb, shown; + freeink::ui::DisplayTarget target; + + // A white page the size of the screen, drawn as is (no rotation). + Snapshot(int16_t w, int16_t h) + : width(w), height(h), rowBytes(static_cast((w + 7) / 8)), fb(static_cast(rowBytes) * h, 0xFF), + target(fb.data(), w, h, rowBytes, freeink::ui::Orientation::LandscapeCounterClockwise) { + target.setFont(0, freeink::ui::kNotoSansSmallFont); + target.setFont(1, freeink::ui::kNotoSansFont); + target.setFont(2, freeink::ui::kKeyboardGalleryFont); + } + + void refreshed() { shown = fb; } + + // The last refreshed frame as a PNG: 1-bit grayscale, a set bit white, as in + // the framebuffer. False when nothing was refreshed or the file fails. + bool write(const char* path) const { + if (shown.empty()) return false; + std::vector raw; + raw.reserve(shown.size() + height); + for (int16_t y = 0; y < height; ++y) { + raw.push_back(0); // filter: none + raw.insert(raw.end(), shown.begin() + y * rowBytes, shown.begin() + (y + 1) * rowBytes); + } + uLongf packedLen = compressBound(raw.size()); + std::vector packed(packedLen); + if (compress2(packed.data(), &packedLen, raw.data(), raw.size(), 9) != Z_OK) return false; + packed.resize(packedLen); + FILE* f = fopen(path, "wb"); + if (!f) return false; + static const uint8_t magic[] = {0x89, 'P', 'N', 'G', '\r', '\n', 0x1a, '\n'}; + fwrite(magic, 1, sizeof magic, f); + const uint8_t header[] = {be(width, 3), be(width, 2), be(width, 1), be(width, 0), be(height, 3), be(height, 2), + be(height, 1), be(height, 0), 1 /* depth */, 0 /* gray */, 0, 0, + 0}; + chunk(f, "IHDR", header, sizeof header); + chunk(f, "IDAT", packed.data(), packed.size()); + chunk(f, "IEND", nullptr, 0); + return fclose(f) == 0; + } + + private: + static uint8_t be(uint32_t v, int byte) { return static_cast(v >> (8 * byte)); } + static void chunk(FILE* f, const char* type, const uint8_t* data, size_t len) { + const uint8_t size[] = {be(len, 3), be(len, 2), be(len, 1), be(len, 0)}; + fwrite(size, 1, 4, f); + fwrite(type, 1, 4, f); + if (len) fwrite(data, 1, len, f); + uLong crc = crc32(0, reinterpret_cast(type), 4); + if (len) crc = crc32(crc, data, static_cast(len)); + const uint8_t sum[] = {be(crc, 3), be(crc, 2), be(crc, 1), be(crc, 0)}; + fwrite(sum, 1, 4, f); + } +}; diff --git a/host/TraceTarget.h b/host/TraceTarget.h index 440d848..8e60658 100644 --- a/host/TraceTarget.h +++ b/host/TraceTarget.h @@ -1,8 +1,11 @@ // A DrawTarget that records every drawing call as a line of text. The desktop -// runner prints them; the tests assert on them. +// runner prints them; the tests assert on them. With `also` set (the runner's +// DESSEN_SNAPSHOT), every call is drawn there too and text is measured with +// its fonts, so the trace and the picture agree. #pragma once #include +#include #include #include #include @@ -24,24 +27,83 @@ struct TraceTarget final : freeink::ui::DrawTarget { return std::to_string(r.x) + "," + std::to_string(r.y) + " " + std::to_string(r.width) + "x" + std::to_string(r.height); } - freeink::ui::Size measureText(freeink::ui::FontId, const char* text, freeink::ui::TextStyle) const override { + freeink::ui::DrawTarget* also = nullptr; + + freeink::ui::Size measureText(freeink::ui::FontId font, const char* text, freeink::ui::TextStyle style) const override { + if (also) return also->measureText(font, text, style); return freeink::ui::Size{static_cast(8 * strlen(text)), 16}; } - int16_t lineHeight(freeink::ui::FontId) const override { return 16; } + int16_t lineHeight(freeink::ui::FontId font) const override { return also ? also->lineHeight(font) : 16; } + Rect inkBounds(freeink::ui::FontId font, const char* text, freeink::ui::TextStyle style) const override { + return also ? also->inkBounds(font, text, style) : DrawTarget::inkBounds(font, text, style); + } Rect clip_{0, 0, 32767, 32767}; Rect clipRect() const override { return clip_; } - bool setClipRect(Rect r) override { clip_ = r; add("clip " + at(r)); return true; } - void fill(Rect r, Paint p, uint8_t, uint8_t) override { add("fill " + at(r) + " c" + std::to_string(static_cast(p.color)) + (p.kind == freeink::ui::PaintKind::Dither ? " d" : "")); } - void stroke(Rect r, Paint, uint8_t width, uint8_t, uint8_t) override { add("stroke " + at(r) + " w" + std::to_string(width)); } - void line(Point from, Point to, uint8_t, Paint) override { + bool setClipRect(Rect r) override { + clip_ = r; + if (also) also->setClipRect(r); + add("clip " + at(r)); + return true; + } + // detail: print every argument, in a fixed order, for the screen tests + // (tools/screen_test.py, DESSEN_TRACE=detail). Off, the short format the + // tests in freeink/test_host.cpp and the Makefile greps read. + bool detail = false; + static std::string n(int v) { return std::to_string(v); } + static std::string pt(Point p) { return n(p.x) + "," + n(p.y); } + static std::string ink(Paint p) { + return "c" + n(static_cast(p.color)) + (p.kind == freeink::ui::PaintKind::Dither ? " d" : ""); + } + static std::string round(uint8_t radius, uint8_t corners) { + return radius ? " r" + n(radius) + "/" + n(corners) : ""; + } + + void fill(Rect r, Paint p, uint8_t radius, uint8_t corners) override { + if (also) also->fill(r, p, radius, corners); + add("fill " + at(r) + " " + ink(p) + (detail ? round(radius, corners) : "")); + } + void stroke(Rect r, Paint p, uint8_t width, uint8_t radius, uint8_t corners) override { + if (also) also->stroke(r, p, width, radius, corners); + add("stroke " + at(r) + " w" + n(width) + (detail ? " " + ink(p) + round(radius, corners) : "")); + } + void line(Point from, Point to, uint8_t width, Paint p) override { lines++; - if (echo) printf("line %d,%d - %d,%d\n", from.x, from.y, to.x, to.y); + if (also) also->line(from, to, width, p); + if (detail) add("line " + pt(from) + " - " + pt(to) + " w" + n(width) + " " + ink(p)); + else if (echo) printf("line %d,%d - %d,%d\n", from.x, from.y, to.x, to.y); + } + void triangle(Point a, Point b, Point c, Paint p) override { + if (also) also->triangle(a, b, c, p); + add(detail ? "triangle " + pt(a) + " " + pt(b) + " " + pt(c) + " " + ink(p) : "triangle"); + } + void text(Rect r, const char* text, freeink::ui::TextStyle s) override { + if (also) also->text(r, text, s); + if (!detail) return add("text " + n(r.x) + "," + n(r.y) + " " + text); + static const char* const align[] = {"left", "center", "right"}; + const unsigned a = static_cast(s.align); + add("text " + at(r) + " f" + n(s.font) + " " + (a < 3 ? align[a] : "?") + " c" + n(static_cast(s.color)) + + " m" + n(s.maxLines) + (s.bold ? " bold" : "") + (s.inverted ? " inv" : "") + + (s.rotation != freeink::ui::Rotation::None ? " rot" + n(static_cast(s.rotation)) : "") + + (also ? " ink " + at(inkOf(r, text, s)) : "") + " | " + text); } - void triangle(Point, Point, Point, Paint) override { add("triangle"); } - void text(Rect r, const char* text, freeink::ui::TextStyle) override { - add("text " + std::to_string(r.x) + "," + std::to_string(r.y) + " " + text); + // Where the lines of a text call land, as `also` lays them out (the same + // layoutText its text() uses): the place a reader sees, for the snapshot + // tests' element check. Empty when nothing fits. + Rect inkOf(Rect r, const char* text, freeink::ui::TextStyle s) const { + int x0 = 32767, y0 = 32767, x1 = -32768, y1 = -32768; + freeink::ui::layoutText(*also, r, text, s, [&](const char*, Rect box) { + x0 = std::min(x0, box.x), x1 = std::max(x1, box.x + box.width); + y0 = std::min(y0, box.y), y1 = std::max(y1, box.y + box.height); + }); + if (x1 < x0) return Rect{r.x, r.y, 0, 0}; + return Rect{static_cast(x0), static_cast(y0), static_cast(x1 - x0), + static_cast(y1 - y0)}; } - void bitmap(Rect r, freeink::ui::BitmapRef b, freeink::ui::BitmapMode, Paint, freeink::ui::Rotation) override { - add("bitmap " + at(r) + " src " + std::to_string(b.width) + "x" + std::to_string(b.height)); + void bitmap(Rect r, freeink::ui::BitmapRef b, freeink::ui::BitmapMode mode, Paint p, freeink::ui::Rotation rot) override { + if (also) also->bitmap(r, b, mode, p, rot); + add("bitmap " + at(r) + " src " + n(b.width) + "x" + n(b.height) + + (detail ? " m" + n(static_cast(mode)) + " " + ink(p) + + (rot != freeink::ui::Rotation::None ? " rot" + n(static_cast(rot)) : "") + : "")); } }; diff --git a/host/main.cpp b/host/main.cpp index 887e275..7579f4f 100644 --- a/host/main.cpp +++ b/host/main.cpp @@ -6,7 +6,8 @@ // // The board has buttons and no touch, unless DESSEN_BOARD says touch, buttons // or both; DESSEN_SCREEN=x sets the panel (800x480). `t,;` on -// stdin taps there. +// stdin taps there, `T,;` long-presses there, and ^ v < > swipe up, +// down, left and right. // // Measuring: DESSEN_COVERS=1 makes full-size covers, and DESSEN_PAINT=" " fills the heap arena and the stack area @@ -18,12 +19,14 @@ #include #include #include +#include #include #include #include "DesktopServices.h" #include "DessenFreeInk.h" #include "Manifest.h" +#include "Snapshot.h" #include "TraceTarget.h" int main(int argc, char** argv) { @@ -68,7 +71,12 @@ int main(int argc, char** argv) { dessen::FreeInkHost host; if (!host.load(mem, static_cast(len), sizeof mem)) return fprintf(stderr, "not a valid dessen app\n"), 2; - host.setRandom([]() -> uint32_t { return arc4random(); }); + // DESSEN_SEED=: the binding's own xorshift from seed n, so a run is the + // same every time (tools/screen_test.py); otherwise the OS's randomness. + static uint32_t seed = 0; + if (const char* s = getenv("DESSEN_SEED")) seed = static_cast(strtoul(s, nullptr, 0)) | 1; + host.setRandom(seed ? +[]() -> uint32_t { return seed ^= seed << 13, seed ^= seed >> 17, seed ^= seed << 5; } + : +[]() -> uint32_t { return arc4random(); }); // Capability services: storage enabled by DESSEN_FS_ROOT, network // from DESSEN_NET_ROOT fixtures when set, crypto via libcrypto. const char* fsRoot = getenv("DESSEN_FS_ROOT"); @@ -88,12 +96,29 @@ int main(int argc, char** argv) { TraceTarget target; target.echo = true; + // DESSEN_TRACE=detail: every argument of every draw call (tools/screen_test.py). + target.detail = getenv("DESSEN_TRACE") && !strcmp(getenv("DESSEN_TRACE"), "detail"); + // DESSEN_SNAPSHOT=: draw through FreeInk's framebuffer target as + // well, measure text with its fonts, and write the last refreshed frame to + // the file (host/Snapshot.h). In detail, the fonts' line heights come first. + const char* snapshotPath = getenv("DESSEN_SNAPSHOT"); + std::unique_ptr snapshot; + if (snapshotPath) { + snapshot = std::make_unique(screen.width, screen.height); + target.also = &snapshot->target; + if (target.detail) + printf("# line heights %d %d %d\n", target.lineHeight(0), target.lineHeight(1), target.lineHeight(2)); + } freeink::ui::InputSnapshot input; + dessen::FreeInkHost::Swipe swipe = dessen::FreeInkHost::SwipeNone; const clock_t t0 = clock(); for (;;) { - const dessen::State state = host.step(target, screen, input, dessen::FreeInkHost::SwipeNone, 1000000); - input = {}; - if (host.takeRefreshRequest()) puts("refresh"); + const dessen::State state = host.step(target, screen, input, swipe, 1000000); + input = {}, swipe = dessen::FreeInkHost::SwipeNone; + if (host.takeRefreshRequest()) { + puts("refresh"); + if (snapshot) snapshot->refreshed(); + } if (state == dessen::State::Launch) { if (host.launch() == dessen::Launch::Home) { printf("[launch home %d]\n", host.launchHomeDest()); @@ -106,15 +131,22 @@ int main(int argc, char** argv) { } break; } + if (state == dessen::State::Exited && target.detail) printf("[exit %d]\n", host.exitCode()); if (state == dessen::State::Exited || state == dessen::State::Faulted) break; if (state != dessen::State::Waiting) continue; const int c = getchar(); if (c == EOF) break; input.confirm = c == 'c', input.back = c == 'b', input.prev = c == 'p', input.next = c == 'n'; - if (c == 't' && scanf("%hd,%hd;", &input.touchX, &input.touchY) == 2) input.touchReleased = true; + if ((c == 't' || c == 'T') && scanf("%hd,%hd;", &input.touchX, &input.touchY) == 2) + input.touchReleased = true, input.longPress = c == 'T'; + if (c == '^') swipe = dessen::FreeInkHost::SwipeUp; + if (c == 'v') swipe = dessen::FreeInkHost::SwipeDown; + if (c == '<') swipe = dessen::FreeInkHost::SwipeLeft; + if (c == '>') swipe = dessen::FreeInkHost::SwipeRight; } const double secs = static_cast(clock() - t0) / CLOCKS_PER_SEC; fflush(stdout); + if (snapshot && !snapshot->write(snapshotPath)) fprintf(stderr, "no snapshot written to %s\n", snapshotPath); fprintf(stderr, "%llu instructions, %.3fs\n", static_cast(host.vm().icount), secs); if (paint) { diff --git a/tools/readstats_fixture.py b/tools/readstats_fixture.py index 2998797..2da9442 100644 --- a/tools/readstats_fixture.py +++ b/tools/readstats_fixture.py @@ -8,7 +8,7 @@ """ import json, os, sys, time -now = int(time.time()) +now = int(os.environ.get("DESSEN_NOW") or time.time()) # the screen tests fix the clock tz = time.localtime().tm_gmtoff // 60 DUNE, HAND = "d" * 32, "a" * 32 diff --git a/tools/screen_test.py b/tools/screen_test.py new file mode 100644 index 0000000..fc8691f --- /dev/null +++ b/tools/screen_test.py @@ -0,0 +1,764 @@ +#!/usr/bin/env python3 +"""Screen tests for the xpui apps: pictures, element positions and the screen +standards, from walks through every screen. + +Usage: screen_test.py [--update] [--only APP[/WALK]] [--show] SCENARIO_FILE... + screen_test.py --standards SCENARIO_FILE... + +Each scenario file (tests/xpui/.scenarios) lists walks through one app. +Every walk runs on the desktop runner (build/dessen) at each screen size, in a +fixed world (the clock, the time zone and the random numbers), drawn through +FreeInk's own framebuffer target with real fonts (DESSEN_SNAPSHOT). The screen +it ends on is compared with its goldens in tests/xpui/golden//: + + @x.png the picture. Fails when a pixel of ink has no ink + within TOLERANCE px of it in the other picture; a diff + image is written to build/screen-diffs/. + @x.trace the draw calls, with where each text's words land. + Every text and button is found again by its label and + must be within TOLERANCE px of where it was, the same + size, and neither missing nor new. The calls themselves + may change (a new framework draws the same picture + differently): that is only a note. + + - A missing golden is written and FAILS, so nobody commits a picture they + never looked at. Look at it, then run again. + - --update (or UPDATE_GOLDENS=1) rewrites the goldens that differ. + - --standards checks every walk against every standard, declared or not, + and lists what each app still breaks. It compares no goldens and always + succeeds. + +Every frame of every walk must also stay on the screen and give each text a +box at least its font's line tall (shorter draws nothing on a device). The +screen standards an app declares (`standards = ...`) are checked on the +screen each walk ends on. + +Scenario file format, one walk per line: + + # a comment + app = nook_xpui the image: build/.bin + manifest = apps/nook_xpui.json optional, passed as DESSEN_MANIFEST + board = touch default board for every walk (both) + sizes = 480x800 800x480 1448x1072 default sizes + standards = bar taps actions the standards checked (see STANDARDS) + walk help [board=touch] [fs=readstats] [pushed] [skip=fit] [expect="How to"] : @? @back + +Events: c b p n (Confirm, Back, Prev, Next), ^ v < > (swipes), +t, (a tap; x and y may be a percentage of the screen, 50%), +T, (a long press), @