Files

148 lines
5.6 KiB
Python

"""Unit tests for Chromium-only browser resolution."""
from __future__ import annotations
from pathlib import Path
from unittest.mock import MagicMock, patch
from chrome import (
PROJECT_ROOT,
_allow_google_chrome_fallback,
_browser_runs_in_background,
_bundled_chromedriver_for,
_google_chrome_fallback_paths,
_installed_chromium_paths,
_portable_chromium_executable,
_prefer_installed_chromium,
capture_fullscreen_screenshot,
find_chromium_executable,
)
def test_installed_paths_exclude_google():
installed = _installed_chromium_paths()
chrome_fb = _google_chrome_fallback_paths()
assert not any("Google" in p for p in installed)
assert any("Google" in p for p in chrome_fb)
def test_portable_chrome_win_preferred_over_installed():
portable = str(PROJECT_ROOT / "chrome-win" / "chrome-win" / "chrome.exe")
installed = r"C:\Program Files\Chromium\Application\chrome.exe"
def fake_isfile(path: str) -> bool:
return path in {portable, installed}
env = {"USERPROFILE": r"C:\Users\test", "LOCALAPPDATA": r"C:\Users\test\AppData\Local"}
with patch("chrome._portable_chromium_executable", return_value=portable):
with patch("chrome.os.path.isfile", side_effect=fake_isfile):
with patch.dict("os.environ", env, clear=True):
assert find_chromium_executable() == portable
def test_installed_used_when_portable_missing():
installed = r"C:\Program Files\Chromium\Application\chrome.exe"
def fake_isfile(path: str) -> bool:
return path == installed
env = {"USERPROFILE": r"C:\Users\test", "LOCALAPPDATA": r"C:\Users\test\AppData\Local"}
with patch("chrome._portable_chromium_executable", return_value=None):
with patch("chrome.os.path.isfile", side_effect=fake_isfile):
with patch.dict("os.environ", env, clear=True):
assert find_chromium_executable() == installed
def test_misf_use_installed_chrome_skips_portable():
portable = str(PROJECT_ROOT / "chrome-win" / "chrome-win" / "chrome.exe")
installed = r"C:\Program Files\Chromium\Application\chrome.exe"
def fake_isfile(path: str) -> bool:
return path in {portable, installed}
env = {
"MISF_USE_INSTALLED_CHROME": "1",
"USERPROFILE": r"C:\Users\test",
"LOCALAPPDATA": r"C:\Users\test\AppData\Local",
}
with patch("chrome._portable_chromium_executable", return_value=portable):
with patch("chrome.os.path.isfile", side_effect=fake_isfile):
with patch.dict("os.environ", env, clear=True):
assert find_chromium_executable() == installed
def test_find_chromium_can_use_google_with_allow_flag():
google_path = r"C:\Program Files\Google\Chrome\Application\chrome.exe"
env = {
"MISF_ALLOW_CHROME": "1",
"USERPROFILE": r"C:\Users\test",
"LOCALAPPDATA": r"C:\Users\test\AppData\Local",
}
with patch("chrome._portable_chromium_executable", return_value=None):
with patch("chrome.os.path.isfile", side_effect=lambda p: p == google_path):
with patch.dict("os.environ", env, clear=True):
assert find_chromium_executable() == google_path
def test_bundled_chromedriver_next_to_portable_chrome():
browser = PROJECT_ROOT / "chrome-win" / "chrome-win" / "chrome.exe"
if not browser.is_file():
return
driver = _bundled_chromedriver_for(str(browser))
assert driver is not None
assert driver.endswith("chromedriver.exe")
def test_portable_chromium_executable_on_disk():
exe = _portable_chromium_executable()
expected = PROJECT_ROOT / "chrome-win" / "chrome-win" / "chrome.exe"
if expected.is_file():
assert exe == str(expected)
def test_foreground_default_background_opt_in():
with patch.dict("os.environ", {}, clear=True):
assert _browser_runs_in_background() is False
with patch.dict("os.environ", {"MISF_BROWSER_BACKGROUND": "1"}, clear=True):
assert _browser_runs_in_background() is True
def test_allow_chrome_flag():
with patch.dict("os.environ", {}, clear=True):
assert _allow_google_chrome_fallback() is False
with patch.dict("os.environ", {"MISF_ALLOW_CHROME": "yes"}, clear=True):
assert _allow_google_chrome_fallback() is True
def test_prefer_installed_flag():
with patch.dict("os.environ", {}, clear=True):
assert _prefer_installed_chromium() is False
with patch.dict("os.environ", {"MISF_USE_INSTALLED_CHROME": "1"}, clear=True):
assert _prefer_installed_chromium() is True
def test_fullscreen_screenshot_maximizes_and_uses_desktop_when_not_black(tmp_path):
driver = MagicMock()
shot = tmp_path / "shot.png"
shot.write_bytes(b"\xff" * 100)
with patch.dict("os.environ", {}, clear=True):
with patch("chrome.capture_desktop_screenshot", return_value=True) as cap:
with patch("chrome._is_image_mostly_black", return_value=False):
assert capture_fullscreen_screenshot(driver, shot) is True
cap.assert_called_once_with(shot)
driver.maximize_window.assert_called()
def test_fullscreen_screenshot_falls_back_when_desktop_is_black(tmp_path):
driver = MagicMock()
shot = tmp_path / "shot.png"
shot.write_bytes(b"\x00" * 100)
with patch.dict("os.environ", {}, clear=True):
with patch("chrome.capture_desktop_screenshot", return_value=True):
with patch("chrome._is_image_mostly_black", return_value=True):
with patch("chrome._capture_browser_with_timestamp_bar", return_value=True) as fb:
assert capture_fullscreen_screenshot(driver, shot) is True
fb.assert_called_once()