Files
mentor-pdf-exporter/tests/test_ui_compact.py
T

240 lines
11 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from pathlib import Path
import pytest
from PySide6.QtCore import QPoint, Qt
from PySide6.QtWidgets import QLabel, QPushButton
from pypdf import PdfWriter
from mentor_pdf_export.exporter import Exporter
from mentor_pdf_export.models import ExportResult
from mentor_pdf_export.ui import MainWindow
@pytest.fixture
def compact_window(qtbot, tmp_path):
window = MainWindow(
root=tmp_path, output=tmp_path / "pdfs",
runs_root=tmp_path.parent / (tmp_path.name + "-runs"),
)
qtbot.addWidget(window)
window.show()
qtbot.waitUntil(lambda: window.isVisible())
return window
def visible_labels(window):
return [label.text() for label in window.findChildren(QLabel) if label.isVisible()]
@pytest.mark.parametrize("mapping,theme,strict", [
("arial", "reference", False), ("simsun", "high_contrast", True),
("preserve", "original", False), ("msyh", "mono", True),
])
def test_no_explanatory_paragraphs_in_any_profile(compact_window, mapping, theme, strict):
window = compact_window
window.font_mapping_combo.setCurrentIndex(window.font_mapping_combo.findData(mapping))
window.print_theme_combo.setCurrentIndex(window.print_theme_combo.findData(theme))
window.strict_source_check.setChecked(strict)
forbidden = (
"直接导出原工程", "依赖库", "默认工程参考配色", "发布前", "兼容范围",
"源文件变化", "原生引擎可能", "有效许可证", "不创建副本", "样本",
)
labels = visible_labels(window)
assert not [text for text in labels if any(phrase in text for phrase in forbidden)]
assert all(not label.wordWrap() for label in window.findChildren(QLabel) if label.isVisible())
assert not any("引擎" in text for text in labels if "导出引擎" != text)
def test_default_geometry_prioritizes_queue_and_compact_log(compact_window, qtbot):
window = compact_window
window.resize(1200, 820)
qtbot.wait(30)
assert window.table.height() >= 330
assert 60 <= window.log.height() <= 100
assert window.table.height() >= 3 * window.log.height()
assert window.table.visibleRegion().boundingRect().height() == window.table.height()
assert window.font_mapping_combo.currentData() == "arial"
assert window.print_theme_combo.currentData() == "reference"
assert window.legacy_gbk_check.isChecked()
assert window.options().legacy_gbk
def test_path_rows_and_option_columns_are_aligned(compact_window):
window = compact_window
paths = (window.root_edit, window.output_edit, window.engine_edit)
lefts = [widget.mapTo(window, QPoint()).x() for widget in paths]
assert len(set(lefts)) == 1
assert len({widget.width() for widget in paths}) == 1
assert len({widget.height() for widget in paths}) == 1
buttons = [button for button in window.paths_group.findChildren(QPushButton)
if button.text() in {"选择目录", "选择引擎"}]
assert len(buttons) == 3
assert len({button.width() for button in buttons}) == 1
for first, second in ((window.print_theme_combo, window.color_combo),
(window.font_mapping_combo, window.scope_combo)):
assert first.mapTo(window, QPoint()).x() == second.mapTo(window, QPoint()).x()
assert first.height() == second.height()
for widget in (*paths, window.print_theme_combo, window.font_mapping_combo,
window.scope_combo, window.timeout_spin):
assert window.rect().contains(widget.mapTo(window, widget.rect().bottomRight()))
@pytest.mark.parametrize("kind", ["scan", "batch"])
def test_busy_queue_scrolls_without_checkbox_or_settings_changes(compact_window, project_factory, qtbot, kind):
window = compact_window
for number in range(60):
project_factory(f"board-{number:02d}")
window.scan_projects()
qtbot.waitUntil(lambda: window.table.rowCount() == 60 and not window.busy, timeout=10000)
qtbot.waitUntil(lambda: window.table.verticalScrollBar().maximum() > 0)
before = [window.table.item(row, 0).checkState() for row in range(60)]
window._set_busy(True, kind)
try:
assert window.table.isEnabled()
assert not window.options_group.isEnabled()
assert not window.root_edit.isEnabled()
assert not window.strict_source_check.isEnabled()
assert not window.stroke_check.isEnabled()
scrollbar = window.table.verticalScrollBar()
scrollbar.setFocus()
qtbot.keyClick(scrollbar, Qt.Key_End)
assert scrollbar.maximum() > 0 and scrollbar.value() == scrollbar.maximum()
qtbot.keyClick(scrollbar, Qt.Key_Home)
assert scrollbar.value() == 0
window.table.setCurrentCell(0, 0)
qtbot.mouseClick(window.table.viewport(), Qt.LeftButton,
pos=window.table.visualItemRect(window.table.item(0, 0)).center())
qtbot.keyClick(window.table, Qt.Key_Space)
assert [window.table.item(row, 0).checkState() for row in range(60)] == before
finally:
window._set_busy(False, kind)
assert window.strict_source_check.isEnabled()
assert window.stroke_check.isEnabled()
def test_worker_log_filters_bookkeeping_but_keeps_results_and_errors(compact_window, project_factory, tmp_path, qtbot):
window = compact_window
project_factory()
detail = "字体校验异常:无法读取字体映射"
bookkeeping = (
"核验原工程;直接读取,不创建工程副本或备份",
"原生引擎已启动,PID=1234", "等待原生引擎,已用时 10 秒",
"直接读取原工程;参考配色仅覆盖导出子进程配置,字体修复仅处理输出 PDF",
"字体/配色完成:转换 10 段文字,修复 2 段编码,中文回退 2 段",
"发布前验证 PDF 结构、页数与源工程内容指纹",
f"源工程路径:{tmp_path}",
)
def runner(args, cwd, logs, cancel, timeout, on_event=None, *, env=None):
for message in (*bookkeeping, detail):
on_event(message)
writer = PdfWriter()
writer.add_blank_page(width=500, height=400)
with Path(args[args.index("-a") + 1]).open("wb") as stream:
writer.write(stream)
return 0
window.exporter_factory = lambda engine, runs: Exporter(engine, runs, runner=runner)
engine = tmp_path / "sch2pdf.exe"
engine.write_bytes(b"fake")
window.engine_edit.setText(str(engine))
window.scan_projects()
qtbot.waitUntil(lambda: window.table.rowCount() == 1 and not window.busy, timeout=10000)
window.start_export()
qtbot.waitUntil(lambda: not window.busy, timeout=10000)
text = window.log.toPlainText()
assert not any(message in text for message in bookkeeping)
assert "PID=" not in text and "批量报告:" not in text
assert "扫描" in text and "board" in text and "成功" in text and "队列结束" in text
assert detail in text
assert len(text.splitlines()) <= 8
def test_detailed_failure_reason_stays_in_tooltip_without_growing_layout(compact_window, project_factory, qtbot):
window = compact_window
project_factory()
window.scan_projects()
qtbot.waitUntil(lambda: window.table.rowCount() == 1 and not window.busy, timeout=10000)
height = window.table.height()
detail = "PDF 字体校验失败:缺少字体资源\n" + "缺失对象 123;" * 90
window._item_result(0, ExportResult(project="board", status="failed", message=detail))
window._scan_failed(detail)
qtbot.wait(20)
assert window.table.item(0, 4).toolTip() == detail
assert "缺少字体资源" in window.table.item(0, 4).text()
assert len(window.table.item(0, 4).text()) <= 160
assert window.status_label.toolTip() == detail
assert "字体校验失败" in window.status_label.text()
assert len(window.status_label.text()) <= 160 and "\n" not in window.status_label.text()
assert window.table.height() == height
assert all(len(line) <= 180 for line in window.log.toPlainText().splitlines())
def test_advanced_controls_are_collapsed_and_reachable_without_queue_reset(compact_window, project_factory, qtbot):
window = compact_window
for number in range(45):
project_factory(f"advanced-{number:02d}")
window.scan_projects()
qtbot.waitUntil(lambda: window.table.rowCount() == 45 and not window.busy, timeout=10000)
scrollbar = window.table.verticalScrollBar()
qtbot.waitUntil(lambda: scrollbar.maximum() > 0)
scrollbar.setValue(scrollbar.maximum() // 2)
position = scrollbar.value()
item = window.table.item(25, 0)
assert not window.advanced_panel.isVisible()
assert not window.strict_source_check.isVisible()
qtbot.mouseClick(window.advanced_button, Qt.LeftButton)
qtbot.waitUntil(lambda: window.advanced_panel.isVisible())
assert window.strict_source_check.isVisible() and window.stroke_check.isVisible()
qtbot.mouseClick(window.strict_source_check, Qt.LeftButton)
assert window.options().strict_source
assert window.table.item(25, 0) is item and scrollbar.value() == position
window._set_busy(True)
assert not window.advanced_button.isEnabled()
assert not window.strict_source_check.isEnabled()
window._set_busy(False)
qtbot.mouseClick(window.advanced_button, Qt.LeftButton)
qtbot.waitUntil(lambda: not window.advanced_panel.isVisible())
assert window.options().strict_source
assert window.table.item(25, 0) is item and scrollbar.value() == position
@pytest.mark.parametrize("message", [
"无法写入批量记录:权限不足", "PDF warning: missing glyph", "Unable to read PDF resources",
])
def test_worker_actionable_messages_are_not_filtered(compact_window, message):
compact_window._worker_message(message)
assert message in compact_window.log.toPlainText()
def test_selected_only_retry_passes_full_scanned_list_to_worker(compact_window, project_factory, qtbot, monkeypatch, tmp_path):
from unittest.mock import MagicMock
window = compact_window
for relative, name in (("left/Work", "Work"), ("right/Work", "Work"),
("left/Cert", "Cert"), ("right/Cert", "Cert")):
project_factory(relative, name=name)
window.scan_projects()
qtbot.waitUntil(lambda: window.table.rowCount() == 4 and not window.busy, timeout=10000)
window.table.item(0, 3).setText("失败")
window.select_failed()
assert window.selected_count() == 1
captured = {}
worker = MagicMock()
def make_worker(items, output, options, exporter, parent=None, *, all_projects=None):
captured.update(items=items, all_projects=all_projects, parent=parent)
return worker
monkeypatch.setattr("mentor_pdf_export.ui.BatchWorker", make_worker)
window.exporter_factory = lambda engine, runs: object()
engine = tmp_path / "sch2pdf.exe"
engine.write_bytes(b"fake")
window.engine_edit.setText(str(engine))
window.start_export()
try:
worker.start.assert_called_once()
assert captured["items"] == [(0, window.projects[0])]
assert captured["all_projects"] is window.projects
assert len(captured["all_projects"]) == 4
assert captured["parent"] is window
finally:
window._set_busy(False)