Files
Jake WriterandClaude Opus 5.5 6c3850f60a Remove dead and duplicated code across the launchers, scripts and CI
Values both launchers hard-coded (marker fonts, the Windows 11 fonts, the
Apple Silicon and Intel Mac core and panel tables, plausible DPR and core
counts, the macOS MacinTalk/Eloquence voices, the cache prefs) now live in
python/src/camoufox/launcher-constants.json, which the TypeScript package
ships as a data file. gen-fonts-json.py and verify-fonts.py read the same
file, so the drifted PingFang marker copy is gone (fonts.json regenerates
byte-identical) and verify-fonts no longer ast-parses fingerprints.py.

- Drop the duplicate fpgen model pin under browser/scripts/data; the TS
  test checks the fpgen package's MODEL_PIN against the Python copy.
- Move pin-fpgen-model.py and clean-fingerprint-data.py to python/scripts.
- Delete the font licence report (font-metrics/licences.py and its saved
  output, open-fonts.txt): the bundle ships as built, so the report checks
  nothing.
- Delete unreferenced files: cleanfonts.sh, python/examples,
  typescript/renovate.json.
- Delete dead code: classify_installs, remove_mmdb, write_upstream_sh,
  bump_release, unused test constants and fixture helpers, the
  _virtual_display attribute writes, package.py's pre-groups font copy (a
  missing groups.json now fails).
- Share require_bundle, zip_entries, gh_headers and parse_shard instead of
  keeping copies; fold GitHubDownloader into CamoufoxFetcher in both
  launchers.
- TS raises ValueError where Python does, and `camoufox version` prints the
  fpgen package version as Python does.
- Python tests get src/ from pytest's pythonpath instead of sys.path
  inserts; skips that hid untested paths now assert or test the behaviour.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 13:38:55 -06:00

110 lines
3.9 KiB
Python

"""
Tests for camoufox.display -- probing the host monitor in CSS pixels.
Guards daijro/camoufox#425: with Windows display scaling enabled, screeninfo
reports physical pixels while Firefox lays windows out in CSS pixels, so the
browser window opened larger than the screen.
Run with:
cd python && python -m pytest tests/test_display.py -v
"""
from types import SimpleNamespace
import pytest
from camoufox import display
def monitor(width, height, x=0, y=0):
return SimpleNamespace(width=width, height=height, x=x, y=y)
@pytest.fixture
def monitors(monkeypatch):
"""Stub out screeninfo with an explicit monitor list."""
def _set(*found):
monkeypatch.setattr(display, "get_monitors", lambda: list(found))
return _set
class TestLargestDisplay:
def test_reports_unscaled_display_verbatim(self, monkeypatch, monitors):
monkeypatch.setattr(display, "OS_NAME", "lin")
monitors(monitor(1920, 1080))
assert display.largest_display() == (1920, 1080)
@pytest.mark.parametrize(
("dpi", "expected"),
[(96, (1920, 1080)), (120, (1536, 864)), (144, (1280, 720)), (192, (960, 540))],
)
def test_windows_scaling_converts_to_css_pixels(
self, monkeypatch, monitors, dpi, expected
):
monkeypatch.setattr(display, "OS_NAME", "win")
monkeypatch.setattr(display, "_windows_monitor_dpi", lambda m: dpi)
monitors(monitor(1920, 1080))
assert display.largest_display() == expected
def test_scaling_is_windows_only(self, monkeypatch, monitors):
"""macOS and X11 already report CSS pixels; never rescale them."""
monkeypatch.setattr(display, "_windows_monitor_dpi", lambda m: 192)
monitors(monitor(1920, 1080))
for os_name in ("mac", "lin"):
monkeypatch.setattr(display, "OS_NAME", os_name)
assert display.largest_display() == (1920, 1080)
def test_falls_back_to_1x_when_dpi_lookup_fails(self, monkeypatch, monitors):
def unavailable(_):
raise OSError("GetDpiForMonitor failed")
monkeypatch.setattr(display, "OS_NAME", "win")
monkeypatch.setattr(display, "_windows_monitor_dpi", unavailable)
monitors(monitor(1920, 1080))
assert display.largest_display() == (1920, 1080)
def test_falls_back_to_1x_on_nonsense_dpi(self, monkeypatch, monitors):
monkeypatch.setattr(display, "OS_NAME", "win")
monkeypatch.setattr(display, "_windows_monitor_dpi", lambda m: 0)
monitors(monitor(1920, 1080))
assert display.largest_display() == (1920, 1080)
def test_picks_the_roomiest_monitor(self, monkeypatch, monitors):
monkeypatch.setattr(display, "OS_NAME", "lin")
monitors(monitor(1280, 720), monitor(2560, 1440), monitor(1920, 1080))
assert display.largest_display() == (2560, 1440)
def test_none_when_no_monitors(self, monitors):
monitors()
assert display.largest_display() is None
def test_none_when_enumeration_raises(self, monkeypatch):
def boom():
raise RuntimeError("no display")
monkeypatch.setattr(display, "get_monitors", boom)
assert display.largest_display() is None
class TestHasDisplay:
@pytest.mark.parametrize("os_name", ["win", "mac"])
def test_always_present_off_linux(self, monkeypatch, os_name):
"""Regression: keying off DISPLAY alone skipped Windows/macOS entirely."""
monkeypatch.setattr(display, "OS_NAME", os_name)
assert display.has_display({}) is True
@pytest.mark.parametrize(
("env", "expected"),
[
({}, False),
({"DISPLAY": ":0"}, True),
({"WAYLAND_DISPLAY": "wayland-0"}, True),
({"DISPLAY": ""}, False),
],
)
def test_linux_requires_a_session(self, monkeypatch, env, expected):
monkeypatch.setattr(display, "OS_NAME", "lin")
assert display.has_display(env) is expected