Repository navigation
Expand file tree
/
Copy pathui-macos-lib.sh
More file actions
525 lines (502 loc) · 22 KB
/
Copy pathui-macos-lib.sh
File metadata and controls
525 lines (502 loc) · 22 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
# SPDX-License-Identifier: MIT
# Shared helpers for the macOS UI tests (tools/ui-macos-*.sh). Source this
# file; it is not a script. Compatible with the bash 3.2 and the BSD
# userland that macOS ships: no associative arrays, no ${var,,}, no mapfile,
# no GNU timeout, no GNU-only flags.
#
# The tests run the application's own binary (mitcad.app/Contents/MacOS/
# mitcad) with --screenshot: the app renders into an offscreen framebuffer
# and saves it, so nothing is clicked and no screen recording permission is
# needed. The window itself still opens, so a window server must be there: a
# logged-in user's session (Terminal, or SSH into a Mac with a user logged in
# at the console); see ui_gui_unavailable.
#
# Everything the app writes of its own goes to a private temporary
# directory (UI_ROOT: settings, recovery folder, projects folder, thumbnails,
# result store off), removed on exit, so the user's ~/Library/Preferences and
# Application Support are never read or changed. AppKit window restoration is
# disabled for each test launch; a previous crash cannot show a restore alert.
#
# Usage: ui_init <app> <out directory>, then ui_run_app <name> <arguments>...
# runs the app with its log in <out>/<name>.log. Variables set in front of
# the call (MITCAD_RESULT_STORE=dir ui_run_app ...) reach the app.
set -u
UI_APP=""
UI_HEADLESS_APP=""
UI_OUT=""
UI_ROOT=""
UI_PID=""
UI_EXIT=0
UI_SIGNAL=""
UI_TIMED_OUT=0
UI_ASSERTION_FAILED=0
UI_LOG_FILE=""
UI_FAILURES=0
# FD 9 is reserved for the per-user GUI lock. The shell and the actual
# application share its open file description until the app has exited.
UI_GUI_LOCK_FD_OPEN=0
UI_GUI_LOCK_HELD=0
UI_GUI_LOCK_WAITER=""
UI_STATS=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/ui-image-stats.py
# How the AppKit frontend presents the widgets that paint themselves
# (docs/native-macos-ui.md, Coverage).
UI_COVERAGE_LIST=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)/app/platform/macos/native-coverage.txt
# Seconds the app may take for one run (a screenshot run exits after 2 s
# plus the start-up and the recompute; the first start can be slow).
UI_TIMEOUT=${UI_TIMEOUT:-120}
ui_pass() { echo "ok $1"; }
ui_fail() { echo "FAIL: $1"; UI_FAILURES=$((UI_FAILURES + 1)); }
# ui_stop_app: ends the app of the current run, and only it. Its children
# (an import worker) go first.
ui_stop_app() {
if [ -n "$UI_PID" ] && kill -0 "$UI_PID" 2> /dev/null; then
pkill -TERM -P "$UI_PID" 2> /dev/null || true
kill -TERM "$UI_PID" 2> /dev/null || true
local i=0
while [ "$i" -lt 30 ] && kill -0 "$UI_PID" 2> /dev/null; do
sleep 0.1
i=$((i + 1))
done
if kill -0 "$UI_PID" 2> /dev/null; then
pkill -KILL -P "$UI_PID" 2> /dev/null || true
kill -KILL "$UI_PID" 2> /dev/null || true
fi
fi
}
# Kernel flock ownership survives the short Python helper: its inherited
# descriptor and this shell's FD 9 refer to the same open file description.
# Never unlink the file or unlock in Python; either would let another
# checkout enter while the real application still owns the GUI.
ui_gui_lock() {
if [ "$UI_GUI_LOCK_FD_OPEN" = 1 ]; then
ui_fail "a macOS UI run already owns or awaits the GUI lock"
return 1
fi
mkdir -p "$HOME/.mitcad" || return 1
exec 9> "$HOME/.mitcad/macos-ui.lock" || return 1
UI_GUI_LOCK_FD_OPEN=1
echo "Waiting for the shared macOS UI lock"
python3 -c 'import fcntl; fcntl.flock(9, fcntl.LOCK_EX)' 9>&9 &
UI_GUI_LOCK_WAITER=$!
local result=0
# A background helper plus wait keeps INT/TERM traps responsive while
# another checkout's app is running. No timeout belongs to this queue.
wait "$UI_GUI_LOCK_WAITER" || result=$?
UI_GUI_LOCK_WAITER=""
if [ "$result" -ne 0 ]; then
ui_gui_unlock
ui_fail "could not acquire the shared macOS UI lock (exit $result)"
return 1
fi
UI_GUI_LOCK_HELD=1
}
ui_gui_unlock() {
if [ -n "$UI_GUI_LOCK_WAITER" ]; then
kill -TERM "$UI_GUI_LOCK_WAITER" 2> /dev/null || true
wait "$UI_GUI_LOCK_WAITER" 2> /dev/null || true
UI_GUI_LOCK_WAITER=""
fi
if [ "$UI_GUI_LOCK_FD_OPEN" = 1 ]; then
exec 9>&-
UI_GUI_LOCK_FD_OPEN=0
fi
UI_GUI_LOCK_HELD=0
}
ui_cleanup() {
ui_stop_app
if [ -n "$UI_PID" ]; then
wait "$UI_PID" 2> /dev/null || true
UI_PID=""
fi
ui_gui_unlock
# The crash handler writes into the private data root. Keep its reports
# with the test's logs before removing that root, including on failure.
# A failed copy leaves the originals in place for diagnosis.
if [ -n "$UI_ROOT" ] && [ -d "$UI_ROOT/crashes" ]; then
mkdir -p "$UI_OUT/crashes" || return
local crash
for crash in "$UI_ROOT"/crashes/*.crash; do
[ -f "$crash" ] && [ ! -L "$crash" ] || continue
cp -p "$crash" "$UI_OUT/crashes/" || return
done
fi
[ -n "$UI_ROOT" ] && rm -rf "$UI_ROOT"
}
# ui_init app out: checks the app, makes the output directory and the
# private data root, and exports the environment of the tests.
ui_init() {
if [ "$(uname -s)" != Darwin ]; then
echo "the macOS UI tests run on macOS only" >&2
exit 2
fi
if [ ! -x "$1" ]; then
echo "FAIL: $1 is not an executable; pass .../mitcad.app/Contents/MacOS/mitcad" >&2
exit 2
fi
if ! command -v python3 > /dev/null; then
echo "FAIL: python3 is required (xcode-select --install, or brew install python)" >&2
exit 2
fi
UI_APP=$(cd "$(dirname "$1")" && pwd)/$(basename "$1")
mkdir -p "$2" || exit 2
UI_OUT=$(cd "$2" && pwd)
UI_ROOT=$(mktemp -d "${TMPDIR:-/tmp}/mitcad-ui.XXXXXX") || exit 2
trap ui_cleanup EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
if [ "${MITCAD_TEST_HEADLESS:-0}" = 1 ]; then
# Event-driven component tests need no compositor/input pacing. Keep
# explicit caller values; workflow defaults below may use these too.
export MITCAD_TEST_STEP_MS=${MITCAD_TEST_STEP_MS:-10}
export MITCAD_TEST_SETTLE_MS=${MITCAD_TEST_SETTLE_MS:-20}
fi
if [ "${MITCAD_TEST_HEADLESS:-0}" = 1 ] && [ "${QT_QPA_PLATFORM:-}" = offscreen ]; then
# A private virtual screen, independent of the console's resolution.
# The Qt plugin's default 800x800 screen clips normal CAD dialogs.
cat > "$UI_ROOT/offscreen.json" <<'JSON'
{"screens":[{"name":"component-tests","width":1920,"height":1200,"logicalDpi":96,"logicalBaseDpi":96,"dpr":1}]}
JSON
export QT_QPA_PLATFORM="offscreen:configfile=$UI_ROOT/offscreen.json"
# A bundled executable starts with AppKit's Regular activation policy,
# even with Qt offscreen; changing it in main can already be too late
# for another app's popup. A byte-identical standalone copy starts as
# a background executable. Its existing build RPATHs still resolve the
# same libraries; runtime guards require prohibited activation too.
local app_dir headless_dir headless_app sibling resource
app_dir=$(dirname "$UI_APP")
headless_dir=$UI_ROOT/headless/MacOS
mkdir -p "$headless_dir" || exit 2
headless_app=$headless_dir/mitcad-headless
# Preserve the real worker/tool and resource lookup relative to the
# executable. Keep Info.plist absent so AppKit starts background-only.
for sibling in mitcad-render mitcad-mcp-launcher mitcad-updater; do
if [ -e "$app_dir/$sibling" ]; then
ln -s "$app_dir/$sibling" "$headless_dir/$sibling" || exit 2
fi
done
for resource in Resources Frameworks PlugIns XPCServices; do
if [ -d "$app_dir/../$resource" ]; then
ln -s "$app_dir/../$resource" "$UI_ROOT/headless/$resource" || exit 2
fi
done
if ! { cp -c "$UI_APP" "$headless_app" 2>/dev/null || cp -p "$UI_APP" "$headless_app"; }; then
echo 'FAIL: could not make the standalone headless executable' >&2
exit 2
fi
if ! cmp -s "$UI_APP" "$headless_app"; then
echo 'FAIL: the standalone headless executable differs from the application' >&2
exit 2
fi
UI_HEADLESS_APP=$headless_app
echo 'Headless executable: byte-identical standalone copy (no application bundle)'
fi
# The app's own files in the root, not the user's ~/Library: Qt would use
# a plist of the user defaults for QSettings otherwise.
export MITCAD_SETTINGS_DIR=$UI_ROOT/settings
export MITCAD_AUTOSAVE_DIR=$UI_ROOT/autosave
export MITCAD_PROJECTS_DIR=$UI_ROOT/projects
export MITCAD_PRINT_LIBRARY_DIR=$UI_ROOT/print-library
export MITCAD_THUMBNAIL_DIR=$UI_ROOT/thumbnails
# Crash reports of the run's own (mitcad#62): none offered from the user's.
export MITCAD_CRASH_DIR=$UI_ROOT/crashes
# Local MCP discovery records and tokens stay with this run's data too.
export MITCAD_MCP_DISCOVERY_DIR=$UI_ROOT/mcp
mkdir -p "$MITCAD_SETTINGS_DIR" "$MITCAD_AUTOSAVE_DIR" "$MITCAD_PROJECTS_DIR" "$MITCAD_THUMBNAIL_DIR"
# The store of computed results (P7d) is off unless a run turns it on
# (MITCAD_RESULT_STORE=<directory> in front of ui_run_app).
export MITCAD_RESULT_STORE=off
unset MITCAD_RESULT_STORE_MIN_MS
# Qt on macOS logs to the unified log, not stderr, unless told to.
export QT_FORCE_STDERR_LOGGING=1
# Where faces and edges can be clicked, and the bodies' volumes.
export MITCAD_LOG_PICKS=1
export MITCAD_LOG_VOLUMES=1
# What typed-in text fields hold ("File dialog Save As: <path>").
export MITCAD_LOG_TYPED_TEXT=1
# An update banner can move the view between a logged pick and its click.
export MITCAD_NO_UPDATE_CHECK=1
# The first dimension of a drawn sketch scales nothing (mitcad#860), as in
# ui-test-lib.sh.
export MITCAD_SKETCH_FIRST_DIMENSION_SCALE=0
# A new sketch on a face starts empty (mitcad#2051), as in ui-test-lib.sh;
# a test of the face's outline sets it to 1 after ui_init.
export MITCAD_SKETCH_PROJECT_FACE=0
# No question whether to activate a picked face's component (mitcad#960),
# as in ui-test-lib.sh.
export MITCAD_ASK_ACTIVATE_COMPONENT=0
# Panels start from their defaults, not remembered values (mitcad#1028),
# as in ui-test-lib.sh.
export MITCAD_REMEMBER_PANEL_VALUES=0
# An empty design at the start, not the Start tab (mitcad#1258), as in
# ui-test-lib.sh.
export MITCAD_START_PAGE=0
# A new design in the exact isometric view with its origin hidden, not in
# the default view with the origin shown (mitcad#1683), as in ui-test-lib.sh.
export MITCAD_NEW_DESIGN_VIEW=0
# The interface in English whatever the Mac's language (mitcad#1460), as
# in ui-test-lib.sh.
export MITCAD_LANGUAGE=en
# Git's configuration of our own (empty), as in ui-test-lib.sh.
: > "$UI_ROOT/gitconfig"
export GIT_CONFIG_GLOBAL=$UI_ROOT/gitconfig
export GIT_CONFIG_NOSYSTEM=1
unset EMAIL GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL
}
# ui_gui_unavailable: the last run's log says that the app could not reach
# a window server or create an OpenGL context: a session without a
# desktop (a daemon, an SSH login without a user logged in at the
# console, a CI runner without a display). Not the app's fault.
ui_gui_unavailable() {
[ "$UI_EXIT" -ne 0 ] || return 1
[ "${MITCAD_TEST_HEADLESS:-0}" != 1 ] || return 1
# A real assertion is never an environmental skip, even if Qt also
# logged a missing context before the failed process exited.
if grep -Eq '^TestDriver: FAIL([[:space:]]|$)' "$UI_LOG_FILE"; then return 1; fi
grep -Eiq 'could not connect to (the )?(display|window ?server)|connection to the window server|WindowServer|CGSConnection|CGSInitialize|_RegisterApplication|no Qt platform plugin could be initialized|failed to create (an )?(open ?gl )?context|Failed to initialize QPA platform' \
"$UI_LOG_FILE"
}
# ui_log_tail: the end of the last run's log, indented.
ui_log_tail() {
echo " --- tail of $UI_LOG_FILE"
tail -n 60 "$UI_LOG_FILE" | sed 's/^/ | /'
}
# ui_crash_report: the run ended with a signal (a crash, or our own kill).
ui_crash_report() {
local name
name=$(kill -l "$UI_SIGNAL" 2> /dev/null)
ui_fail "$1: Mitcad ended with signal $UI_SIGNAL (${name:-?})"
ui_log_tail
# macOS has no gdb; the crash report of the system has the stack. Set
# MITCAD_UI_LLDB=1 to run the app under lldb --batch, which prints it.
local dir=$HOME/Library/Logs/DiagnosticReports report
report=$(ls -t "$dir"/mitcad* "$dir"/Mitcad* 2> /dev/null | head -n 1)
echo " crash reports: $dir${report:+ (newest: $report)}"
}
# ui_run_app name arguments...: runs the app until it exits, or ends it
# after UI_TIMEOUT seconds (macOS has no GNU timeout), with stdout and stderr in
# <out>/<name>.log (Qt messages go to stderr with QT_FORCE_STDERR_LOGGING).
# Sets UI_EXIT (the exit code; 128 + n after signal n), UI_SIGNAL,
# UI_TIMED_OUT, UI_ASSERTION_FAILED and UI_LOG_FILE. Records an assertion,
# timeout, crash or non-zero exit. A session without a window server ends the
# test with 77, ctest's SKIP (MITCAD_UI_REQUIRE_GUI=1 makes it a failure).
ui_run_app() {
local name=$1
shift
UI_LOG_FILE=$UI_OUT/$name.log
UI_TIMED_OUT=0
UI_ASSERTION_FAILED=0
UI_SIGNAL=""
rm -f "$UI_LOG_FILE"
# Cross-checkout exclusion starts before launching the app or counting
# UI_TIMEOUT. UI_PID stays the actual app (or the existing lldb mode).
if [ "${MITCAD_TEST_HEADLESS:-0}" = 1 ]; then
case " $* " in
*' --ui=native '*) ui_fail "headless tests cannot launch AppKit"; return 1 ;;
esac
case " $* " in
*' --ui=qt '*) ;;
*) ui_fail "headless tests require explicit --ui=qt"; return 1 ;;
esac
case "${QT_QPA_PLATFORM:-}" in
offscreen|offscreen:*) ;;
*) ui_fail "headless tests require Qt offscreen"; return 1 ;;
esac
if [ -z "${MITCAD_TEST_INPUT:-}" ]; then
ui_fail "headless tests require Qt offscreen and a UI script"; return 1
fi
elif ! ui_gui_lock; then return 1; fi
local run_app=${UI_HEADLESS_APP:-$UI_APP}
if [ "${MITCAD_UI_LLDB:-0}" = 1 ] && command -v lldb > /dev/null; then
lldb --batch -o run -o 'bt all' -- "$run_app" -ApplePersistenceIgnoreState YES "$@" > "$UI_LOG_FILE" 2>&1 &
else
"$run_app" -ApplePersistenceIgnoreState YES "$@" > "$UI_LOG_FILE" 2>&1 &
fi
UI_PID=$!
local ticks=0 limit=$((UI_TIMEOUT * 5)) assertion_tick=-1
while kill -0 "$UI_PID" 2> /dev/null; do
if [ "${MITCAD_UI_LLDB:-0}" != 1 ]; then
# A native modal loop can outlive the driver's failed assertion and
# Qt exit request. Match the actual diagnostic, never a traced script
# line containing that text, and give normal shutdown one second.
if [ "$assertion_tick" -lt 0 ] && [ -f "$UI_LOG_FILE" ] &&
grep -Eq '^TestDriver: FAIL([[:space:]]|$)' "$UI_LOG_FILE"; then
assertion_tick=$ticks
fi
if [ "$assertion_tick" -ge 0 ] && [ "$((ticks - assertion_tick))" -ge 5 ]; then
UI_ASSERTION_FAILED=1
ui_stop_app
break
fi
fi
if [ "$ticks" -ge "$limit" ]; then
# Keep the outer deadline even when the assertion arrived just now;
# the recorded assertion is still the reason this run failed.
if [ "$assertion_tick" -ge 0 ]; then UI_ASSERTION_FAILED=1; else UI_TIMED_OUT=1; fi
ui_stop_app
break
fi
sleep 0.2
ticks=$((ticks + 1))
done
# Preserve the application's failure for the report even when a caller
# enables errexit; wait must not terminate the harness first.
UI_EXIT=0
wait "$UI_PID" 2> /dev/null || UI_EXIT=$?
UI_PID=""
ui_gui_unlock
if [ "${MITCAD_UI_LLDB:-0}" = 1 ]; then
# lldb's own exit code says nothing: the app's is in its message, and
# no such message means it stopped on a signal.
local code
code=$(sed -n 's/.*Process [0-9]* exited with status = \([0-9]*\).*/\1/p' "$UI_LOG_FILE" | tail -n 1)
if [ -n "$code" ]; then UI_EXIT=$code; elif [ "$UI_TIMED_OUT" = 0 ]; then UI_EXIT=134; fi
fi
if [ "$UI_EXIT" -gt 128 ] && [ "$UI_TIMED_OUT" = 0 ] && [ "$UI_ASSERTION_FAILED" = 0 ]; then
UI_SIGNAL=$((UI_EXIT - 128))
fi
if [ "$UI_TIMED_OUT" = 0 ] && [ "$UI_ASSERTION_FAILED" = 0 ] && ui_gui_unavailable && [ "${MITCAD_UI_REQUIRE_GUI:-0}" != 1 ]; then
echo "SKIP: $name: no window server or OpenGL context in this session (MITCAD_UI_REQUIRE_GUI=1 to fail instead)"
ui_log_tail
exit 77
fi
if [ "$UI_ASSERTION_FAILED" = 1 ]; then
ui_fail "$name: TestDriver assertion failed; stopped its lingering app (exit code $UI_EXIT)"
ui_log_tail
elif [ "$UI_TIMED_OUT" = 1 ]; then
ui_fail "$name: Mitcad did not exit within $UI_TIMEOUT s"
ui_log_tail
elif [ -n "$UI_SIGNAL" ]; then
ui_crash_report "$name"
elif [ "$UI_EXIT" -ne 0 ]; then
ui_fail "$name: exit code $UI_EXIT"
ui_log_tail
elif [ "${MITCAD_TEST_HEADLESS:-0}" = 1 ]; then
local isolation_checks
isolation_checks=$(grep -Fc 'Headless macOS application verified: activation prohibited, inactive, no visible native windows' "$UI_LOG_FILE" || :)
if ! grep -Fq 'Headless UI test verified: Qt offscreen without an OpenGL context' "$UI_LOG_FILE" ||
[ "${isolation_checks:-0}" -lt 3 ]; then
ui_fail "$name: missing final headless Qt/no-OpenGL or macOS application isolation proof"
ui_log_tail
fi
fi
ui_expect_presented "$name"
}
# ui_expect_presented name: the AppKit presenter of the last run showed every
# widget it was given, or the widget's class is pending in the coverage list
# (app/platform/macos/native-coverage.txt). A widget it cannot present would
# otherwise be lost without a word (docs/native-macos-ui.md, Coverage).
ui_expect_presented() {
local line class
[ -f "$UI_LOG_FILE" ] || return 0
while IFS= read -r line; do
class=${line#*unpresented widget }
class=${class%% *}
class=${class##*::}
if ! grep -Eq "^$class[[:space:]]+pending([[:space:]]|\$)" "$UI_COVERAGE_LIST" 2> /dev/null; then
ui_fail "$1: the AppKit frontend cannot present ${line#*unpresented widget }; present it, or mark $class pending in app/platform/macos/native-coverage.txt"
fi
done < <(grep -E '^Native presenter: unpresented widget ' "$UI_LOG_FILE")
return 0
}
# ui_expect_log pattern description: the last run's log has a line matching
# the extended regular expression.
ui_expect_log() {
if grep -Eq -- "$1" "$UI_LOG_FILE"; then ui_pass "$2"; else ui_fail "$2: '$1' not in the log"; fi
}
# ui_expect_no_log text description: the log does not contain the text.
ui_expect_no_log() {
# The driver traces each input, including expect-never 'TestDriver: FAIL'.
# Only its actual diagnostic is a failure, with or without the caller's
# historical leading anchor. Other callers still ask for literal text.
case $1 in
'TestDriver: FAIL' | '^TestDriver: FAIL')
if grep -Eq '^TestDriver: FAIL([[:space:]]|$)' "$UI_LOG_FILE"; then
ui_fail "$2: driver failure in the log"
else
ui_pass "$2"
fi
;;
*)
if grep -Fq -- "$1" "$UI_LOG_FILE"; then ui_fail "$2: '$1' in the log"; else ui_pass "$2"; fi
;;
esac
}
# ui_log_value pattern: what the first line matching the regular expression
# has after it (sed, a basic one with the value in \(\)).
ui_log_value() {
sed -n "s/.*$1.*/\\1/p" "$UI_LOG_FILE" | head -n 1
}
# ui_gt a b: a > b for decimal numbers.
ui_gt() { awk -v a="$1" -v b="$2" 'BEGIN { exit !(a > b) }'; }
ui_lt() { awk -v a="$1" -v b="$2" 'BEGIN { exit !(a < b) }'; }
# ui_update_test_hooks file: ON when the build that made the file reads the
# update tests' environment variables (the CMake option
# MITCAD_UPDATE_TEST_HOOKS, mitcad#921), OFF when it does not; empty when
# unknown. UI_UPDATE_TEST_HOOKS (the ctest's setting) decides first, then
# the first CMakeCache.txt above the file (as in ui-test-lib.sh).
ui_update_test_hooks() {
local dir
if [ -n "${UI_UPDATE_TEST_HOOKS:-}" ]; then
case $UI_UPDATE_TEST_HOOKS in
ON | on | 1 | TRUE | true | YES | yes) echo ON ;;
*) echo OFF ;;
esac
return 0
fi
dir=$(cd "$(dirname "$1")" 2> /dev/null && pwd) || return 0
while [ -n "$dir" ] && [ "$dir" != / ]; do
if [ -f "$dir/CMakeCache.txt" ]; then
if grep -qiE '^MITCAD_UPDATE_TEST_HOOKS:BOOL=(ON|1|TRUE|YES|Y)$' "$dir/CMakeCache.txt"; then echo ON; else echo OFF; fi
return 0
fi
dir=$(dirname "$dir")
done
return 0
}
# ui_check_shot name: checks the screenshot <out>/<name>.png as the Windows
# test does (tools/ui-windows-test.ps1, Check-Shot): the light background,
# the body filling 20 % of the image or more in two lit faces at least, in
# the bluish grey of the body, and not a blank image. Sets UI_SHOT_OK (1 or
# 0), UI_SHOT_SHADES (the number of face shades) and UI_SHOT_RATIO (side
# faces relative to the top face; "none" with one shade).
ui_check_shot() {
local name=$1 path=$UI_OUT/$1.png json fields
UI_SHOT_OK=0
UI_SHOT_SHADES=0
UI_SHOT_RATIO=none
if [ ! -s "$path" ]; then
ui_fail "$name: no screenshot"
return
fi
if ! json=$(python3 "$UI_STATS" --shot "$path" 2> "$UI_OUT/$name.stats.err"); then
ui_fail "$name: the screenshot could not be analysed: $(cat "$UI_OUT/$name.stats.err")"
return
fi
fields=$(printf '%s' "$json" | python3 -c '
import json, sys
d = json.load(sys.stdin)
shades = ", ".join("%d rgb(%d,%d,%d) %.1f%%" % (s["luma"], s["r"], s["g"], s["b"], s["percent"])
for s in d["shades"])
ratio = "none" if d["side_ratio"] is None else "%.2f" % d["side_ratio"]
print("|".join([str(d["width"]), str(d["height"]), str(d["background"]),
"%.1f" % d["face_percent"], str(len(d["shades"])),
"1" if d["bluish"] else "0", ratio, str(d["colors"]), shades]))
')
local width height background faces count bluish ratio colors shades
IFS='|' read -r width height background faces count bluish ratio colors shades <<< "$fields"
echo " $name: ${width}x$height, background $background, faces $faces%: $shades"
UI_SHOT_SHADES=$count
UI_SHOT_RATIO=$ratio
# The floating chrome fits the body into the free area between its cards,
# so it covers a smaller part of the whole window than the docked one did.
if [ "$width" -lt 300 ] || [ "$height" -lt 200 ]; then ui_fail "$name: image too small"
elif [ "$colors" -lt 8 ]; then ui_fail "$name: the image is blank"
elif [ "$background" -lt 200 ]; then ui_fail "$name: background is not the light gradient"
elif ui_lt "$faces" 12; then ui_fail "$name: no body in the image"
elif [ "$count" -lt 2 ]; then ui_fail "$name: fewer than two lit faces"
elif [ "$bluish" != 1 ]; then ui_fail "$name: faces are not the bluish grey body colour"
else
ui_pass "$name: body drawn with $count face shades"
UI_SHOT_OK=1
fi
}