"""The stats overlay's drawing (framegen/hud.py), headless: every style renders to an RGBA image from made-up numbers, the numbers keep their places as digits change, and colours follow the levels.""" import pathlib import sys import numpy as np import pytest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1] / "devtools")) from framegen import hud, stats as S, ui_draw as U from render_stats_samples import sample_collector, text_for # made-up numbers, labelled as such @pytest.fixture(scope="module") def coll(): return sample_collector() @pytest.mark.parametrize("style", ["minimal", "detailed", "bar", "graphs"]) def test_every_style_renders_an_rgba_image(coll, style): t = text_for(coll, style) if style == "graphs": img = hud.graphs(t, [8.3] * 100, 1.0, 0.8) elif style == "bar": img = hud.bar(t, 1280, 1.0, 0.8) assert img.shape[1] == 1280 and img.shape[0] < 40 # as wide as the picture, one line else: img = hud.stats(t, 1.0, 0.8) assert img.dtype == np.uint8 and img.shape[2] == 4 and img[..., 3].max() == 255 def test_bigger_ui_scales_every_style(coll): for style in ("minimal", "detailed"): a, b = hud.stats(text_for(coll, style), 1.0), hud.stats(text_for(coll, style), 2.0) assert b.shape[1] > 1.8 * a.shape[1] and b.shape[0] > 1.8 * a.shape[0] def test_detailed_is_a_card_with_rows_and_minimal_a_pill(coll): mini, det = hud.stats(text_for(coll, "minimal"), 1.0), hud.stats(text_for(coll, "detailed"), 1.0) assert det.shape[0] > 4 * mini.shape[0] @pytest.mark.parametrize("style", ["minimal", "detailed", "bar"]) def test_the_overlay_does_not_change_size_as_digits_change(style): """Fixed-width numbers: 8.1 -> 11.9 ms and 99 -> 120 fps move nothing.""" sizes = set() for fps, ft, jit in ((99.0, 8.1, 0.4), (120.0, 11.9, 1.7), (61.0, 9.8, 0.9)): c = sample_collector() snap = c.snapshot(game_fps=30.0) snap.update(fps=fps, ft_avg=ft, jitter=jit, delay=28.0, low1=61.0, low01=47.0, ft_worst=21.0) items = S.wanted(style, lambda k: "auto") head = [i for i in items if i in ("fps", "game", "delay")] if style == "detailed": cells, rows = S.all_cells(snap, head, True, True), S.all_cells(snap, [i for i in items if i not in head], True, False) else: cells, rows = S.all_cells(snap, items, True, True), () t = text_for(c, style) t = type(t).make(t.parts, "good", "", None, cells=cells, rows=rows) img = hud.bar(t, 1920, 1.0) if style == "bar" else hud.stats(t, 1.0) sizes.add(img.shape) assert len(sizes) == 1 def test_values_sit_right_aligned_in_their_slots(): """A value with fewer digits ends at the same x as one with more.""" def ends(value): cell = S.Cell("frametime", "frametime", value, "ms", "", "88.8") t = __import__("framegen.io", fromlist=["MiniText"]).MiniText.make([("x", "main")], "good", cells=[cell]) img = hud.stats(t, 1.0, 1.0) cols = np.where((img[..., :3] > 200).all(axis=2).any(axis=0))[0] # bright (text) columns return img.shape[1], int(cols.max()) w1, e1 = ends("9.2") w2, e2 = ends("12.4") assert w1 == w2 assert abs(e1 - e2) <= 12 # the unit ("ms") after the slot, not the value, ends the line def test_colours_follow_the_levels(): def count(level, colour): cell = S.Cell("fps", "", "120", "fps", level, "888") t = __import__("framegen.io", fromlist=["MiniText"]).MiniText.make([("x", "main")], "", cells=[cell]) img = hud.stats(t, 2.0, 1.0)[..., :3] return int((np.abs(img.astype(int) - np.array(colour)).max(axis=2) < 12).sum()) assert count("good", U.GOOD) > 30 and count("bad", U.BAD) > 30 and count("tight", U.TIGHT) > 30 assert count("good", U.BAD) == 0 and count("bad", U.GOOD) == 0 assert count("", U.GOOD) == 0 and count("", U.BAD) == 0 def test_a_narrow_bar_leaves_out_what_does_not_fit(coll): t = text_for(coll, "bar") wide, narrow = hud.bar(t, 1920, 1.0), hud.bar(t, 500, 1.0) assert narrow.shape[1] == 500 ink = lambda img: int((np.abs(img[..., :3].astype(int) - np.array(U.BG)).max(axis=2) > 40).sum()) # noqa: E731 assert ink(narrow) < ink(wide) assert narrow[:, -10:, :3].tolist() == [[list(U.BG)] * 10] * narrow.shape[0] # nothing runs off the right end def test_the_result_card_has_the_big_numbers_and_a_row_per_stage(coll): summ = coll.summary(reason="sample") plain = hud.card(summ, 1.0, 1.0) summ["benchmark"] = {"seconds": 40} bench = hud.card(summ, 1.0, 1.0, "~/somewhere/summit-benchmark.txt") assert plain.shape[1] == 680 and bench.shape[0] > plain.shape[0] # (the "saved" line) assert plain[0, 0, 3] == 0 # rounded corners summ["stages"] = {} assert hud.card(summ, 1.0, 1.0).shape[0] < plain.shape[0] def test_headless_renders_write_pngs(coll, tmp_path): from PIL import Image for name, img in {"minimal": hud.stats(text_for(coll, "minimal"), 1.0, 0.8), "bar": hud.bar(text_for(coll, "bar"), 1280, 1.0)}.items(): Image.fromarray(img).save(tmp_path / f"{name}.png") assert Image.open(tmp_path / f"{name}.png").size == (img.shape[1], img.shape[0])