231 lines
8.4 KiB
Python
231 lines
8.4 KiB
Python
"""Pure tests for the CODE/TERMINAL layout geometry.
|
|
|
|
No OpenCV here on purpose: the detection entry points import cv2 lazily, and CI
|
|
does not install it. Everything asserted below is decided by the pure helpers
|
|
(projections -> pane, stability, band fitting, sendcmd lines, filtergraph).
|
|
"""
|
|
from clipwright.layouts.code_layout import (
|
|
CAPTION_BAND_RATIO,
|
|
clamp_pane_width,
|
|
code_bands,
|
|
code_filtergraph,
|
|
fit_window,
|
|
follow_sendcmd_lines,
|
|
pane_from_projections,
|
|
stable_pane,
|
|
text_column_extent,
|
|
)
|
|
|
|
HD = (1920, 1080)
|
|
|
|
|
|
def _projections(row_span, col_span, h=1080, w=1920):
|
|
rows = [0.0] * h
|
|
cols = [0.0] * w
|
|
for i in range(*row_span):
|
|
rows[i] = 100.0
|
|
for i in range(*col_span):
|
|
cols[i] = 100.0
|
|
return rows, cols
|
|
|
|
|
|
class TestPaneFromProjections:
|
|
def test_locates_the_pane_block(self):
|
|
rows, cols = _projections((100, 400), (200, 1000))
|
|
assert pane_from_projections(rows, cols, *HD) == (200, 100, 800, 300)
|
|
|
|
def test_ignores_mass_below_the_threshold(self):
|
|
rows, cols = _projections((100, 400), (200, 1000))
|
|
# A stray edge column far from the pane is under 15% of the max.
|
|
cols[1500] = 10.0
|
|
assert pane_from_projections(rows, cols, *HD) == (200, 100, 800, 300)
|
|
|
|
def test_rejects_a_narrow_pane(self):
|
|
rows, cols = _projections((100, 400), (200, 500))
|
|
assert pane_from_projections(rows, cols, *HD) is None
|
|
|
|
def test_rejects_a_short_pane(self):
|
|
rows, cols = _projections((100, 200), (200, 1000))
|
|
assert pane_from_projections(rows, cols, *HD) is None
|
|
|
|
def test_empty_projections_are_none(self):
|
|
assert pane_from_projections([], [], *HD) is None
|
|
assert pane_from_projections([0, 0], [0, 0], *HD) is None
|
|
|
|
|
|
class TestTextColumnExtent:
|
|
def test_no_chrome_keeps_the_full_text_width(self):
|
|
cols = [0.0] * 2560
|
|
for i in range(100, 2400):
|
|
cols[i] = 100.0
|
|
x0, x1 = text_column_extent(cols, 2560)
|
|
assert 90 <= x0 <= 110
|
|
assert 2390 <= x1 <= 2410
|
|
|
|
def test_sidebar_and_minimap_are_masked(self):
|
|
cols = [0.0] * 2560
|
|
for i in range(0, 60): # activity bar
|
|
cols[i] = 90.0
|
|
for i in range(80, 400): # sidebar
|
|
cols[i] = 70.0
|
|
for i in range(500, 2050): # code text
|
|
cols[i] = 100.0
|
|
for i in range(2450, 2560): # minimap
|
|
cols[i] = 110.0
|
|
x0, x1 = text_column_extent(cols, 2560)
|
|
# Cut after the sidebar/editor border and before the minimap border.
|
|
assert 400 <= x0 <= 520
|
|
assert 2050 <= x1 <= 2450
|
|
|
|
def test_too_narrow_is_none(self):
|
|
cols = [0.0] * 2560
|
|
for i in range(0, 100):
|
|
cols[i] = 100.0
|
|
assert text_column_extent(cols, 2560) is None
|
|
|
|
|
|
class TestClampPaneWidth:
|
|
def test_narrows_from_the_left_by_default(self):
|
|
assert clamp_pane_width((300, 100, 2100, 1200), 1440) == (
|
|
300, 100, 1440, 1200)
|
|
|
|
def test_noop_when_it_already_fits(self):
|
|
assert clamp_pane_width((300, 100, 1200, 1200), 1440) == (
|
|
300, 100, 1200, 1200)
|
|
|
|
def test_centred_anchor_splits_the_difference(self):
|
|
assert clamp_pane_width((0, 0, 2000, 1000), 1000, anchor="center") == (
|
|
500, 0, 1000, 1000)
|
|
|
|
def test_odd_width_is_made_even(self):
|
|
_x, _y, w, _h = clamp_pane_width((0, 0, 2000, 1000), 1441)
|
|
assert w % 2 == 0 and w <= 1441
|
|
|
|
|
|
class TestStablePane:
|
|
def test_agreement_returns_the_median(self):
|
|
boxes = [(100, 100, 800, 300), (105, 102, 800, 300),
|
|
(98, 99, 802, 301), (300, 300, 800, 300)]
|
|
box = stable_pane(boxes, *HD)
|
|
assert box is not None
|
|
assert abs(box[0] - 100) <= 6 and abs(box[1] - 100) <= 4
|
|
|
|
def test_a_scattered_set_is_rejected(self):
|
|
boxes = [(0, 0, 400, 300), (1500, 800, 400, 300),
|
|
(700, 400, 400, 300)]
|
|
assert stable_pane(boxes, *HD) is None
|
|
|
|
def test_too_few_samples_is_rejected(self):
|
|
assert stable_pane([], *HD) is None
|
|
assert stable_pane([(100, 100, 800, 300)], *HD) is None
|
|
|
|
|
|
class TestCodeBands:
|
|
def test_bands_fill_the_output_and_are_even(self):
|
|
content_h, caption_h = code_bands(1920)
|
|
assert content_h + caption_h == 1920
|
|
assert content_h % 2 == 0 and caption_h % 2 == 0
|
|
|
|
def test_caption_reserve_is_about_fifteen_percent(self):
|
|
_, caption_h = code_bands(1920)
|
|
assert abs(caption_h / 1920 - CAPTION_BAND_RATIO) <= 0.01
|
|
|
|
def test_large_caption_ratio_still_leaves_a_band(self):
|
|
content_h, caption_h = code_bands(1000, 0.9)
|
|
assert content_h >= 2 and caption_h >= 2
|
|
assert content_h + caption_h == 1000
|
|
|
|
|
|
class TestFitWindow:
|
|
def test_short_pane_fits_without_follow(self):
|
|
crop_h, scaled_h, needs_follow = fit_window(300, 2.0, 1632)
|
|
assert crop_h == 300 and scaled_h == 600
|
|
assert needs_follow is False
|
|
|
|
def test_tall_pane_needs_a_follow_window(self):
|
|
crop_h, scaled_h, needs_follow = fit_window(2000, 2.7, 1632)
|
|
assert needs_follow is True
|
|
assert scaled_h == 1632
|
|
assert crop_h <= 2000 and crop_h % 2 == 0
|
|
|
|
def test_scaled_height_never_exceeds_the_band(self):
|
|
for pane_h, scale in ((300, 2.0), (2000, 2.7), (1080, 0.75)):
|
|
_, scaled_h, _ = fit_window(pane_h, scale, 1632)
|
|
assert scaled_h <= 1632 and scaled_h % 2 == 0
|
|
|
|
def test_degenerate_pane_is_clamped_not_zero(self):
|
|
crop_h, scaled_h, _ = fit_window(10, 0.01, 100)
|
|
assert crop_h >= 2 and scaled_h >= 2
|
|
|
|
|
|
class TestFollowSendcmd:
|
|
def test_dedupes_change_points_and_is_segment_relative(self):
|
|
follow = [(1.0, 100.0), (2.0, 100.0), (3.0, 200.0), (4.0, 200.0)]
|
|
lines = follow_sendcmd_lines(follow, 0, 100, 500, start=1.0)
|
|
assert len(lines) == 2
|
|
assert lines[0].startswith("0.0000")
|
|
assert lines[1].startswith("2.0000")
|
|
assert "y 50;" in lines[0]
|
|
assert "y 150;" in lines[1]
|
|
|
|
def test_window_is_clamped_to_the_pane(self):
|
|
lines = follow_sendcmd_lines(
|
|
[(0.0, -500.0), (1.0, 99999.0)], 100, 100, 500)
|
|
ys = [int(ln.split(" y ")[1].rstrip(";")) for ln in lines]
|
|
assert ys == [100, 500]
|
|
|
|
def test_points_before_the_segment_start_are_dropped(self):
|
|
follow = [(0.0, 100.0), (2.0, 300.0)]
|
|
lines = follow_sendcmd_lines(follow, 0, 100, 500, start=1.0)
|
|
assert len(lines) == 1
|
|
assert lines[0].startswith("1.0000")
|
|
|
|
|
|
class TestCodeFiltergraph:
|
|
def test_graph_crops_scales_and_overlays(self):
|
|
graph = code_filtergraph((200, 100, 1440, 800), 1080, 1920)
|
|
assert graph is not None
|
|
assert "crop=" in graph
|
|
assert "scale=1080:600" in graph
|
|
assert "overlay=x=0:y=" in graph
|
|
assert graph.endswith("[v]")
|
|
|
|
def test_no_follow_means_no_sendcmd(self):
|
|
graph = code_filtergraph((200, 100, 1440, 800), 1080, 1920, follow=None)
|
|
assert graph is not None
|
|
assert "sendcmd" not in graph
|
|
|
|
def test_follow_on_a_tall_pane_emits_sendcmd(self):
|
|
pane = (100, 0, 800, 4000)
|
|
graph = code_filtergraph(pane, 1080, 1920,
|
|
follow=[(0.0, 500.0), (2.0, 1500.0)])
|
|
assert graph is not None
|
|
assert "sendcmd=c=" in graph
|
|
assert "crop@c=" in graph
|
|
assert graph.endswith("[v]")
|
|
|
|
def test_follow_without_a_window_does_not_emit_sendcmd(self):
|
|
graph = code_filtergraph((200, 100, 1440, 800), 1080, 1920,
|
|
follow=[(0.0, 500.0)])
|
|
assert graph is not None
|
|
assert "sendcmd" not in graph
|
|
|
|
def test_min_scale_fallback_returns_none(self):
|
|
# out_w / pane_w = 1080 / 2000 = 0.54 < 0.75.
|
|
assert code_filtergraph((0, 0, 2000, 800), 1080, 1920) is None
|
|
|
|
def test_min_scale_can_be_overridden(self):
|
|
graph = code_filtergraph((0, 0, 2000, 800), 1080, 1920, min_scale=0.5)
|
|
assert graph is not None
|
|
|
|
def test_accepts_a_detector_dict(self):
|
|
pane = {"x": 200, "y": 100, "w": 1440, "h": 800, "mode": "editor"}
|
|
assert code_filtergraph(pane, 1080, 1920) is not None
|
|
|
|
def test_camera_box_adds_a_third_branch(self):
|
|
graph = code_filtergraph((200, 100, 1440, 800), 1080, 1920,
|
|
cam_box=(0, 0, 300, 200))
|
|
assert graph is not None
|
|
assert "split=3" in graph
|
|
assert "[cam]" in graph
|
|
assert graph.endswith("[v]")
|