Files
MDHelper/tests/test_style_panel.py

52 lines
3.0 KiB
Python

import time
from PySide6.QtCore import QSettings, Qt
from mdhelper import main_window
from mdhelper.style_dialog import DocumentStyleEditorDialog
from mdhelper.style_sync import normalize_payload
def test_panel_is_lazy_and_tracks_unsaved_document_and_cursor(qt_application, tmp_path, monkeypatch):
settings = QSettings(str(tmp_path / 'settings.ini'), QSettings.Format.IniFormat)
monkeypatch.setattr(main_window, 'QSettings', lambda *_: settings)
path = tmp_path / 'note.md'; path.write_text('# Heading **strong**\n\nBody [link](#heading-strong)', encoding='utf-8')
window = main_window.MainWindow(tmp_path)
try:
assert not hasattr(window, 'style_panel')
window.open_document(path); window.edit_current_markdown()
editor = window.tabs.currentWidget()
window.show_style_panel(); panel = window.style_panel
panel.filter.setCurrentIndex(1); panel.refresh(force=True)
keys = {panel.listing.item(i).data(Qt.ItemDataRole.UserRole) for i in range(panel.listing.count())}
assert keys == {'h1', 'strong', 'paragraph', 'link'}
assert '标题 1' in panel.current.text() and '加粗' in panel.current.text()
cursor = editor.editor.textCursor(); cursor.setPosition(editor.source_text().index('Body')); editor.editor.setTextCursor(cursor)
panel.refresh(); assert '正文' in panel.current.text() and '超链接' in panel.current.text()
editor.replace_source_undoable('`inline`'); panel.refresh(force=True)
assert 'inline_code' in panel.used and 'h1' not in panel.used
editor.mark_saved(path, editor.source_text(), window._file_signature(path))
panel.hide(); assert not panel.timer.isActive()
assert path.read_text(encoding='utf-8').startswith('# Heading')
finally: window.close()
def test_modify_style_metadata_and_real_preview(qt_application):
dialog = DocumentStyleEditorDialog({'name': 'Test', 'rules': {'h1': {'font_size': 28, 'font_size_unit': 'pt'}}})
try:
assert dialog.actual_preview is None
dialog.select_target('h2')
dialog.style_name_edit.setText('自定义章节'); dialog.style_name_edit.editingFinished.emit()
dialog.based_on_box.setCurrentIndex(dialog.based_on_box.findData('h1'))
payload = dialog.style_payload()
assert normalize_payload(payload)['rules']['_metadata']['h2'] == {'name': '自定义章节', 'based_on': 'h1'}
dialog.tabs.setCurrentIndex(2)
assert dialog.actual_preview is not None
values = []; deadline = time.monotonic() + 10
while time.monotonic() < deadline:
qt_application.processEvents()
if not dialog.actual_preview.page().isLoading(): break
time.sleep(.01)
dialog.actual_preview.page().runJavaScript("getComputedStyle(document.querySelector('h2')).fontSize", values.append)
while not values and time.monotonic() < deadline: qt_application.processEvents(); time.sleep(.01)
assert values and abs(float(values[0].removesuffix('px')) - 28*96/72) < .1
finally: dialog.close()