Files
MonitorSwitcher/tests/test_switcher.py

95 lines
2.8 KiB
Python

from unittest.mock import MagicMock, patch
import pytest
from config.config_manager import ConfigManager
from core.switcher import MonitorSwitcher
@pytest.fixture
def config(tmp_path):
return ConfigManager(tmp_path / "config.json")
def _make_mock_monitor():
m = MagicMock()
m.__enter__ = MagicMock(return_value=m)
m.__exit__ = MagicMock(return_value=False)
m.vcp = MagicMock()
return m
def test_apply_profile_sends_vcp_to_both_monitors(config):
mon0 = _make_mock_monitor()
mon1 = _make_mock_monitor()
with patch("core.switcher.get_monitors", return_value=[mon0, mon1]):
sw = MonitorSwitcher(config)
sw.apply_profile("pc1_dp")
mon0.vcp.set_vcp_feature.assert_called_once_with(0x60, 15)
mon1.vcp.set_vcp_feature.assert_called_once_with(0x60, 15)
def test_apply_profile_hdmi(config):
mon0 = _make_mock_monitor()
mon1 = _make_mock_monitor()
with patch("core.switcher.get_monitors", return_value=[mon0, mon1]):
sw = MonitorSwitcher(config)
sw.apply_profile("pc2_hdmi")
mon0.vcp.set_vcp_feature.assert_called_once_with(0x60, 17)
mon1.vcp.set_vcp_feature.assert_called_once_with(0x60, 17)
def test_apply_profile_skips_missing_monitor(config):
mon0 = _make_mock_monitor()
with patch("core.switcher.get_monitors", return_value=[mon0]):
sw = MonitorSwitcher(config)
sw.apply_profile("pc1_dp")
mon0.vcp.set_vcp_feature.assert_called_once_with(0x60, 15)
def test_apply_profile_continues_on_monitor_error(config):
mon0 = _make_mock_monitor()
mon1 = _make_mock_monitor()
mon0.vcp.set_vcp_feature.side_effect = Exception("DDC/CI error")
with patch("core.switcher.get_monitors", return_value=[mon0, mon1]):
sw = MonitorSwitcher(config)
sw.apply_profile("pc1_dp")
mon1.vcp.set_vcp_feature.assert_called_once_with(0x60, 15)
def test_apply_profile_unknown_profile_does_nothing(config):
with patch("core.switcher.get_monitors", return_value=[]) as mock_gm:
sw = MonitorSwitcher(config)
sw.apply_profile("nonexistent")
mock_gm.assert_not_called()
def test_on_switch_callback_fires(config):
mon0 = _make_mock_monitor()
mon1 = _make_mock_monitor()
received = []
with patch("core.switcher.get_monitors", return_value=[mon0, mon1]):
sw = MonitorSwitcher(config)
sw.on_switch(lambda pid: received.append(pid))
sw.apply_profile("pc2_hdmi")
assert received == ["pc2_hdmi"]
def test_apply_profile_updates_active_profile(config):
mon0 = _make_mock_monitor()
mon1 = _make_mock_monitor()
with patch("core.switcher.get_monitors", return_value=[mon0, mon1]):
sw = MonitorSwitcher(config)
sw.apply_profile("pc2_hdmi")
assert config.get_active_profile_id() == "pc2_hdmi"