harvest/game/tests/test_mobile.gd
Forrest aee60b0ba2 #35: interface scale, safe area, and the mobile test kit
MobilePort: ui_scale() (clampf(4.3 / the screen's shorter side in inches, 1, 1.8) times the
Interface size; 1 on desktop), a size-class fallback (below 500 pt = phone, 1.6) when the
density is 0 or implausible, safe_rect(ci) (the display's safe area mapped into ci's canvas
through the final transform), minimum_target() (44 pt in canvas pixels), and the `forced`
override for tests. The desktop preview (-- --mobile) plays the phone in the window:
--safe-area=l,t,r,b insets it, --dpi=N sets its density, and without --dpi a 16:9-or-wider
window is a 2.7 in phone (about 1.6x) and a squarer one a 6 in iPad (1x).

GameSettings: interface_size (0.8-1.3, saved under [touch]) and apply_scale(), which sets the
window's content_scale_factor (always 1 on desktop). PerfTest shows the screen, density, safe
area, and resulting scale, to check the density on a device.

Test kit: test_mobile (last in SUITES) runs game/tests/mobile/*_test.gd in name order and puts
the root window back between files; mobile/touch.gd sends touches through
Input.parse_input_event; scale_test covers phones, iPads, desktop, the safe rect, and the target;
sticks_test is the 8c81d81 driver's 24 checks. game/tools/capture_mobile.gd takes the phone
screenshots.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 14:40:06 -04:00

38 lines
1.5 KiB
GDScript

extends RefCounted
## The phone tests: every game/tests/mobile/*_test.gd (one file per task), in name order, each
## with the runner's check(). A test file extends RefCounted and has `func run(t) -> void` (it
## may await); it forces a phone or desktop with MobilePort.forced = 1 or 0, sends fingers with
## mobile/touch.gd, and before it returns sets MobilePort.forced back to -1 and frees what it
## added (checked here). Between files this puts the root window back as it was (its size and
## content_scale_factor, which a phone's GameSettings.apply changes), along with the desktop
## preview's density and insets (MobilePort.preview_dpi, preview_insets) and the pause.
## Run just these: -- --suite=test_mobile
const DIR := "res://game/tests/mobile/"
func run(t: GameTestRunner) -> void:
var files: Array[String] = []
for f in DirAccess.get_files_at(DIR):
if f.ends_with("_test.gd"):
files.append(f)
files.sort()
t.check(not files.is_empty(), "%s has tests" % DIR)
var suite := t.current
var size := t.root.size
var factor := t.root.content_scale_factor
for f in files:
var name := f.get_basename()
print("-- ", name)
t.current = "%s/%s" % [suite, name]
var test: Object = (load(DIR + f) as GDScript).new()
await test.call("run", t)
t.check(MobilePort.forced == -1, "it set MobilePort.forced back to -1")
MobilePort.forced = -1
MobilePort.preview_dpi = 0.0
MobilePort.preview_insets = []
t.root.size = size
t.root.content_scale_factor = factor
t.paused = false
await t.process_frame
t.current = suite