Files
cthulhu/tests/test_wnck_session_regressions.py
2026-04-07 17:37:07 -04:00

67 lines
2.2 KiB
Python

import sys
import unittest
from pathlib import Path
from unittest import mock
import gi
gi.require_version("Gdk", "3.0")
gi.require_version("Gtk", "3.0")
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
from cthulhu import wnck_support
from cthulhu.plugins.OCR import plugin as ocr_plugin
class WnckSupportTests(unittest.TestCase):
def test_load_wnck_skips_import_when_session_is_wayland(self):
with mock.patch.object(
wnck_support.importlib,
"import_module",
side_effect=AssertionError("import_module should not be called"),
):
self.assertIsNone(wnck_support.load_wnck(session_type="wayland"))
def test_load_wnck_imports_wnck_when_session_is_x11(self):
fakeGi = mock.Mock()
fakeWnck = object()
def importModule(name):
if name == "gi":
return fakeGi
if name == "gi.repository.Wnck":
return fakeWnck
raise AssertionError(f"Unexpected import: {name}")
with mock.patch.object(wnck_support.importlib, "import_module", side_effect=importModule):
self.assertIs(wnck_support.load_wnck(session_type="x11"), fakeWnck)
fakeGi.require_version.assert_called_once_with("Wnck", "3.0")
class OCRWnckHandlingTests(unittest.TestCase):
def test_check_dependencies_does_not_fail_when_wnck_is_unavailable(self):
testPlugin = ocr_plugin.OCRDesktop.__new__(ocr_plugin.OCRDesktop)
with (
mock.patch.object(ocr_plugin, "PIL_AVAILABLE", True),
mock.patch.object(ocr_plugin, "PYTESSERACT_AVAILABLE", True),
mock.patch.object(ocr_plugin, "GTK_AVAILABLE", True),
mock.patch.object(ocr_plugin, "WNCK_AVAILABLE", False),
):
self.assertTrue(testPlugin._checkDependencies())
def test_screen_shot_window_returns_false_when_wnck_is_unavailable(self):
testPlugin = ocr_plugin.OCRDesktop.__new__(ocr_plugin.OCRDesktop)
with (
mock.patch.object(ocr_plugin, "GTK_AVAILABLE", True),
mock.patch.object(ocr_plugin, "WNCK_AVAILABLE", False),
):
self.assertFalse(testPlugin._screenShotWindow())
if __name__ == "__main__":
unittest.main()