178 lines
5.8 KiB
Python
178 lines
5.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Tests fuer Empfang-Popup-Unterdrueckung und Surface-State."""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import tempfile
|
|
import time
|
|
import unittest
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from aza_empfang_shell_surface import (
|
|
peer_matches_surface,
|
|
read_shell_surface_state,
|
|
shell_surface_state_path,
|
|
surface_indicates_active_direct_chat,
|
|
write_shell_surface_state,
|
|
)
|
|
|
|
|
|
class TestShellSurfaceState(unittest.TestCase):
|
|
def setUp(self):
|
|
self._td = tempfile.TemporaryDirectory()
|
|
self._patcher = patch(
|
|
"aza_empfang_shell_surface.shell_surface_state_path",
|
|
return_value=__import__("pathlib").Path(self._td.name) / "surface.json",
|
|
)
|
|
self._patcher.start()
|
|
|
|
def tearDown(self):
|
|
self._patcher.stop()
|
|
self._td.cleanup()
|
|
|
|
def test_write_and_read(self):
|
|
write_shell_surface_state(
|
|
{
|
|
"mode": "direct",
|
|
"peer_user_id": "user_a",
|
|
"document_visible": True,
|
|
"shell_minimized": False,
|
|
}
|
|
)
|
|
st = read_shell_surface_state()
|
|
self.assertIsNotNone(st)
|
|
self.assertEqual(st["peer_user_id"], "user_a")
|
|
self.assertEqual(st["mode"], "direct")
|
|
|
|
def test_peer_match(self):
|
|
st = {"peer_user_id": "u1", "external_peer_user_id": "ext1"}
|
|
self.assertTrue(peer_matches_surface("u1", st))
|
|
self.assertTrue(peer_matches_surface("ext1", st))
|
|
self.assertFalse(peer_matches_surface("other", st))
|
|
|
|
def test_active_direct_chat_requires_visible(self):
|
|
write_shell_surface_state(
|
|
{
|
|
"mode": "direct",
|
|
"peer_user_id": "peer_x",
|
|
"document_visible": False,
|
|
"shell_minimized": False,
|
|
}
|
|
)
|
|
self.assertFalse(surface_indicates_active_direct_chat("peer_x"))
|
|
|
|
def test_active_direct_chat_ok(self):
|
|
write_shell_surface_state(
|
|
{
|
|
"mode": "direct",
|
|
"peer_user_id": "peer_x",
|
|
"document_visible": True,
|
|
"shell_minimized": False,
|
|
}
|
|
)
|
|
self.assertTrue(surface_indicates_active_direct_chat("peer_x"))
|
|
|
|
def test_stale_state_ignored(self):
|
|
path = shell_surface_state_path()
|
|
path.write_text(
|
|
'{"ts": 1, "mode": "direct", "peer_user_id": "old", "document_visible": true}',
|
|
encoding="utf-8",
|
|
)
|
|
self.assertIsNone(read_shell_surface_state(max_age_sec=1.0))
|
|
|
|
|
|
class TestPopupSuppressLogic(unittest.TestCase):
|
|
def test_suppress_when_matching_chat_visible(self):
|
|
from aza_empfang_incoming_popup import should_suppress_incoming_popup
|
|
|
|
app = MagicMock()
|
|
app._empfang_webview_subprocess_alive.return_value = True
|
|
app._empfang_webview_resolve_shell_hwnd.return_value = 0
|
|
with patch(
|
|
"aza_empfang_incoming_popup.empfang_shell_hwnd_minimized",
|
|
return_value=False,
|
|
), patch(
|
|
"aza_empfang_incoming_popup.surface_indicates_active_direct_chat",
|
|
return_value=True,
|
|
):
|
|
self.assertTrue(should_suppress_incoming_popup(app, "peer_1"))
|
|
|
|
def test_no_suppress_when_chat_minimized(self):
|
|
from aza_empfang_incoming_popup import should_suppress_incoming_popup
|
|
|
|
app = MagicMock()
|
|
app._empfang_webview_subprocess_alive.return_value = True
|
|
with patch(
|
|
"aza_empfang_incoming_popup.empfang_shell_hwnd_minimized",
|
|
return_value=True,
|
|
):
|
|
self.assertFalse(should_suppress_incoming_popup(app, "peer_1"))
|
|
|
|
def test_no_suppress_without_webview(self):
|
|
from aza_empfang_incoming_popup import should_suppress_incoming_popup
|
|
|
|
app = MagicMock()
|
|
app._empfang_webview_subprocess_alive.return_value = False
|
|
self.assertFalse(should_suppress_incoming_popup(app, "peer_1"))
|
|
|
|
|
|
class TestSendDmPayload(unittest.TestCase):
|
|
def test_send_requires_peer_user_id(self):
|
|
from aza_empfang_incoming_popup import send_dm_text_to_peer
|
|
|
|
app = MagicMock()
|
|
app.get_practice_id.return_value = "prac_1"
|
|
app._empfang_self_user_id.return_value = "me"
|
|
app._empfang_self_user_id_resolve_now.return_value = "me"
|
|
ok, msg = send_dm_text_to_peer(app, "", "Hallo")
|
|
self.assertFalse(ok)
|
|
|
|
|
|
class TestPopupHost(unittest.TestCase):
|
|
def test_popup_parent_uses_host_when_main_withdrawn(self):
|
|
from aza_empfang_incoming_popup import empfang_popup_parent
|
|
|
|
app = MagicMock()
|
|
host = MagicMock()
|
|
with patch(
|
|
"aza_empfang_incoming_popup.empfang_main_window_usable",
|
|
return_value=False,
|
|
), patch(
|
|
"aza_empfang_incoming_popup.empfang_popup_host",
|
|
return_value=host,
|
|
) as mock_host:
|
|
self.assertIs(empfang_popup_parent(app), host)
|
|
mock_host.assert_called_once_with(app)
|
|
|
|
def test_popup_parent_uses_app_when_usable(self):
|
|
from aza_empfang_incoming_popup import empfang_popup_parent
|
|
|
|
app = MagicMock()
|
|
with patch(
|
|
"aza_empfang_incoming_popup.empfang_main_window_usable",
|
|
return_value=True,
|
|
):
|
|
self.assertIs(empfang_popup_parent(app), app)
|
|
|
|
|
|
class TestDiktatInsert(unittest.TestCase):
|
|
def test_insert_replaces_selection(self):
|
|
import tkinter as tk
|
|
from aza_empfang_incoming_popup import insert_diktat_at_cursor
|
|
|
|
root = tk.Tk()
|
|
root.withdraw()
|
|
try:
|
|
tw = tk.Text(root)
|
|
tw.insert("1.0", "alt text")
|
|
tw.tag_add("sel", "1.0", "1.3")
|
|
tw.mark_set("insert", "1.0")
|
|
insert_diktat_at_cursor(tw, "neu")
|
|
self.assertEqual(tw.get("1.0", "end").strip(), "neu text")
|
|
finally:
|
|
root.destroy()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|