104 lines
3.5 KiB
Python
104 lines
3.5 KiB
Python
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
from PyQt6.QtCore import QObject, pyqtSignal
|
|
from PyQt6.QtGui import QTextDocument
|
|
from PyQt6.QtWidgets import QApplication
|
|
|
|
import room_widget
|
|
|
|
|
|
class PlayerSignals(QObject):
|
|
time_changed = pyqtSignal(int)
|
|
state_changed = pyqtSignal(bool, int)
|
|
|
|
|
|
@pytest.fixture
|
|
def room(qtbot, monkeypatch):
|
|
player = Mock(signals=PlayerSignals())
|
|
monkeypatch.setattr(room_widget, 'VLCSyncPlayer', lambda frame: player)
|
|
widget = room_widget.RoomWidget()
|
|
widget.username = 'Me'
|
|
qtbot.addWidget(widget)
|
|
widget.resize(1050, 500)
|
|
widget.show()
|
|
return widget
|
|
|
|
|
|
@pytest.mark.parametrize('text', ["It's someone's & my <message>",
|
|
"It's at 1:23 & <b>literal</b>",
|
|
'line one\nline two', 'literal ' entity'])
|
|
def test_message_rendering(room, text):
|
|
room.add_chat_message("<b>O'Brien</b>", text, 0)
|
|
bubble = room.chat_content_layout.itemAt(0).widget()
|
|
doc = QTextDocument()
|
|
doc.setHtml(bubble.text_lbl.text())
|
|
assert doc.toPlainText() == text
|
|
if '1:23' in text:
|
|
assert 'href="seek:1:23"' in bubble.text_lbl.text()
|
|
|
|
|
|
def test_bubbles_do_not_shrink_or_clip_when_overflowing(room, qtbot):
|
|
text = 'A wrapped message with several words. ' * 8
|
|
room.add_chat_message('Other', text, 0)
|
|
qtbot.wait(100)
|
|
first = room.chat_content_layout.itemAt(0).widget()
|
|
initial_height = first.height()
|
|
for i in range(35):
|
|
room.add_chat_message('Me' if i % 2 else 'Other', text, 0)
|
|
qtbot.wait(200)
|
|
assert room.chat_messages.verticalScrollBar().maximum() > 0
|
|
assert first.height() >= initial_height
|
|
for width in (220, 450, 280):
|
|
room.splitter.setSizes([room.width() - width, width])
|
|
qtbot.wait(100)
|
|
for i in range(room.chat_content_layout.count()):
|
|
bubble = room.chat_content_layout.itemAt(i).widget()
|
|
label = bubble.text_lbl
|
|
assert label.height() >= label.heightForWidth(label.width())
|
|
assert bubble.bubble.rect().contains(label.geometry())
|
|
|
|
|
|
def test_unread_alert_and_focus_clear(room, qtbot, monkeypatch):
|
|
alert = Mock()
|
|
monkeypatch.setattr(QApplication, 'alert', alert)
|
|
room.activateWindow()
|
|
room.setFocus()
|
|
qtbot.wait(50)
|
|
room.add_chat_message('Other', 'hello', 0)
|
|
assert room._unread_messages == 1
|
|
alert.assert_called_once()
|
|
room.add_chat_message('Me', 'reply', 0)
|
|
assert room._unread_messages == 1
|
|
room.chat_input.setFocus()
|
|
qtbot.wait(50)
|
|
assert room._unread_messages == 0
|
|
room.add_chat_message('Other', 'focused', 0)
|
|
assert room._unread_messages == 0
|
|
room.chat_container.hide()
|
|
room.add_chat_message('Other', 'hidden', 0)
|
|
assert room._unread_messages == 1
|
|
room.clear_chat()
|
|
assert room._unread_messages == 0
|
|
|
|
|
|
def test_popout_focus_and_wrapping(room, qtbot, monkeypatch):
|
|
monkeypatch.setattr(QApplication, 'alert', Mock())
|
|
room.popout_chat()
|
|
popout = room.popout_window
|
|
qtbot.addWidget(popout)
|
|
popout.activateWindow()
|
|
room.chat_input.setFocus()
|
|
qtbot.wait(100)
|
|
room.add_chat_message('Other', 'Wrapped popout text ' * 40, 0)
|
|
qtbot.wait(100)
|
|
assert room._unread_messages == 0
|
|
bubble = room.chat_content_layout.itemAt(0).widget()
|
|
assert bubble.text_lbl.height() >= bubble.text_lbl.heightForWidth(bubble.text_lbl.width())
|
|
room.activateWindow()
|
|
room.setFocus()
|
|
qtbot.wait(100)
|
|
room.add_chat_message('Other', 'Background popout', 0)
|
|
assert room._unread_messages == 1
|
|
popout.close()
|