summaryrefslogtreecommitdiff
path: root/gui/test/restart.py
blob: a9447b54942ff29e2004be96e03509a1362e12d5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python3
"""Restart live targets and keep native window/backing sizes independent of font zoom."""
import argparse
from pathlib import Path
import re
import struct
import subprocess
import tempfile

ROOT = Path(__file__).resolve().parents[2]
p = argparse.ArgumentParser(description=__doc__)
p.add_argument('--gui', type=Path, default=ROOT / 'gui/zig-out/bin/ztrace-gui')
p.add_argument('--native', action='store_true', help='Exercise an actual SDL window and its resize feedback')
a = p.parse_args()
a.gui = a.gui.resolve()
with tempfile.TemporaryDirectory(prefix='ztrace-restart-') as directory:
    out = Path(directory)
    script = out / 'check.script'
    commands = '''type :launch /bin/sleep 1
key return
await stops 1
console goto memory 0x1234
console set origin 1234
key z
console continue
await status exited
# Restart after exit through the toolbar, then while running through the shortcut.
click 710 54
await launches 2
await status stopped
expect stops 1
expect moment 0
expect nav[2].place.follow true
expect lens_count 0
expect origin 0
key c
await status running
hold left_shift
key r
release left_shift
await launches 3
await status stopped
expect stops 1
# A stopped target can restart too; the command and button share this path.
type :restart
key return
await launches 4
await status stopped
expect stops 1
'''
    images = []
    for zoom in (100, 90, 60, 200, 100):
        shot = out / f'zoom-{len(images)}.png'
        images.append(shot)
        commands += f'console set zoom {zoom}\nframes 3\nscreenshot {shot}\n'
        if zoom == 60:
            # Button coordinates are physical pixels even after font scaling.
            commands += 'click 425 35\nawait launches 5\nawait status stopped\nexpect zoom 60\n'
        if zoom == 200:
            # Tiny overlays reveal the new lens's header and keep its close control reachable.
            commands += 'key z\nexpect lens_count 1\nframes 30\nclick 250 406\nexpect lens_count 2\nexpect lenses[1].cycle true\nframes 30\nclick 700 406\nexpect lens_count 1\nkey escape\n'
    # Native glass shares the canvas: changing font size must not resize it.
    commands += 'move 0 0\nkey z\nframes 10\n'
    for cycle in range(3):
        for zoom in (60, 200, 100):
            shot = out / f'glass-{cycle}-{zoom}.png'
            images.append(shot)
            commands += f'console set zoom {zoom}\nframes 5\nscreenshot {shot}\n'
    commands += 'key escape\n'
    commands += '''hold left_ctrl
key minus
release left_ctrl
expect zoom 90
frames 60
hold left_ctrl
key equals
release left_ctrl
expect zoom 100
hold left_ctrl
key 0
release left_ctrl
expect zoom 100
expect-no-log CommandFailed
expect-no-log NoLaunchCommand
'''
    script.write_text(commands)
    args = [str(a.gui), '--fresh', '--size', '800x700', '--state', str(out / 'session.state'), '--script', str(script)]
    if not a.native:
        args.append('--headless')
    run = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
    stdout, stderr = run.communicate(timeout=90)
    assert run.returncode == 0, (stdout, stderr)
    dimensions = [struct.unpack('>II', shot.read_bytes()[16:24]) for shot in images]
    assert all(size == dimensions[0] for size in dimensions), ('zoom resized the window/backing', dimensions)
    assert dimensions[0] == (800, 700), dimensions
    recordings = list(Path('/tmp').glob(f'ztrace-gui-{run.pid}-*.ztrace'))
    assert len(recordings) == 5, recordings
    pids = []
    for recording in recordings:
        dump = subprocess.check_output([str(a.gui.parent / 'ztrace'), 'dump', str(recording)], text=True)
        pid = int(re.search(r'tid=(\d+)', dump)[1])
        pids.append(pid)
        assert not Path(f'/proc/{pid}').exists(), ('old target survived restart/close', pid)
        recording.unlink()
    assert len(set(pids)) == 5, pids
    print(('Native' if a.native else 'Headless') + ': restart after exit/running/stopped, fresh recordings, scaled button hits, stable 800x700 backing at 60–200% passed')