Complete V2 acceptance checklist
This commit is contained in:
@@ -0,0 +1,195 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
|
||||
|
||||
import fitz
|
||||
from PIL import Image
|
||||
from PySide6.QtCore import QSettings
|
||||
from PySide6.QtWidgets import QApplication
|
||||
|
||||
PROJECT_ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(PROJECT_ROOT))
|
||||
|
||||
from pdfreaderlite.core.document import PdfDocument
|
||||
from pdfreaderlite.ui.main_window import MainWindow
|
||||
|
||||
|
||||
def create_text_pdf(path: Path, title: str = "PDFReaderLite V2") -> None:
|
||||
doc = fitz.open()
|
||||
try:
|
||||
for page_number in range(3):
|
||||
page = doc.new_page(width=420, height=560)
|
||||
page.insert_text((72, 72), f"{title} page {page_number + 1}")
|
||||
page.insert_text((72, 120), "annotation search filter jump properties")
|
||||
doc.save(path)
|
||||
finally:
|
||||
doc.close()
|
||||
|
||||
|
||||
def create_many_pages_pdf(path: Path, page_count: int = 30) -> None:
|
||||
doc = fitz.open()
|
||||
try:
|
||||
for page_number in range(page_count):
|
||||
page = doc.new_page(width=420, height=560)
|
||||
page.insert_text((72, 72), f"Large PDF page {page_number + 1}")
|
||||
for line in range(20):
|
||||
page.insert_text((72, 110 + line * 18), f"content line {line + 1}")
|
||||
doc.save(path)
|
||||
finally:
|
||||
doc.close()
|
||||
|
||||
|
||||
def create_scanned_pdf(path: Path, image_path: Path) -> None:
|
||||
doc = fitz.open()
|
||||
try:
|
||||
page = doc.new_page(width=420, height=560)
|
||||
page.insert_image(fitz.Rect(60, 80, 360, 360), filename=str(image_path))
|
||||
doc.save(path)
|
||||
finally:
|
||||
doc.close()
|
||||
|
||||
|
||||
def assert_annotations_survive_save(path: Path, image_path: Path) -> None:
|
||||
document = PdfDocument()
|
||||
document.open(path)
|
||||
document.add_text(0, "新增文本", font_size=18, text_color=(0.1, 0.2, 0.5))
|
||||
document.add_text_stamp(0, "已阅")
|
||||
document.add_cover_area(0, text="遮盖说明")
|
||||
document.add_area_highlight(1, (60, 100, 220, 150))
|
||||
document.add_area_line_mark(1, (60, 180, 220, 220), False)
|
||||
document.add_ink(2, [(80, 100), (120, 150), (180, 120)])
|
||||
document.insert_image_rect(0, image_path, (230, 260, 330, 330))
|
||||
document.save()
|
||||
document.close()
|
||||
|
||||
reopened = PdfDocument()
|
||||
reopened.open(path)
|
||||
annotations = reopened.annotations()
|
||||
assert len(annotations) >= 6
|
||||
assert any(item.content == "新增文本" for item in annotations)
|
||||
assert any(item.content == "已阅" for item in annotations)
|
||||
assert any(item.content == "遮盖" for item in annotations)
|
||||
assert reopened._page(0).get_images(full=True)
|
||||
reopened.close()
|
||||
|
||||
|
||||
def assert_scanned_pdf_tools(path: Path) -> None:
|
||||
document = PdfDocument()
|
||||
document.open(path)
|
||||
document.add_area_highlight(0, (80, 100, 250, 160))
|
||||
document.add_text_stamp(0, "批准")
|
||||
document.add_ink(0, [(90, 230), (130, 250), (220, 235)])
|
||||
document.save()
|
||||
document.close()
|
||||
|
||||
reopened = PdfDocument()
|
||||
reopened.open(path)
|
||||
annotations = reopened.annotations()
|
||||
assert len(annotations) >= 3
|
||||
assert any(item.content == "区域高亮" for item in annotations)
|
||||
assert any(item.content == "批准" for item in annotations)
|
||||
reopened.close()
|
||||
|
||||
|
||||
def assert_annotation_filter_search_and_properties(path: Path) -> None:
|
||||
document = PdfDocument()
|
||||
document.open(path)
|
||||
note = document.add_note(0, "filter-target")
|
||||
document.add_rectangle(1)
|
||||
document.add_ink(2, [(70, 90), (110, 130), (160, 110)])
|
||||
updated = document.update_annotation_style(
|
||||
note.page_index,
|
||||
note.xref,
|
||||
(0.2, 0.5, 0.9),
|
||||
2,
|
||||
0.65,
|
||||
"filter-target-updated",
|
||||
)
|
||||
assert updated and updated.content == "filter-target-updated"
|
||||
annotations = document.annotations()
|
||||
note_types = {"text", "freetext", "caret"}
|
||||
shape_types = {"square", "circle", "line", "polygon", "polyline"}
|
||||
assert [item for item in annotations if item.annotation_type.lower() in note_types]
|
||||
assert [item for item in annotations if item.annotation_type.lower() in shape_types]
|
||||
assert [item for item in annotations if "updated" in (item.content or "").lower()]
|
||||
document.close()
|
||||
|
||||
|
||||
def assert_main_window_modes_and_tabs(pdf_a: Path, pdf_b: Path, settings_dir: Path) -> None:
|
||||
QSettings.setDefaultFormat(QSettings.Format.IniFormat)
|
||||
QSettings.setPath(QSettings.Format.IniFormat, QSettings.Scope.UserScope, str(settings_dir))
|
||||
app = QApplication.instance() or QApplication([])
|
||||
app.setApplicationName("PDFReaderLiteAcceptance")
|
||||
app.setOrganizationName("PDFReaderLiteAcceptance")
|
||||
|
||||
window = MainWindow()
|
||||
try:
|
||||
window.open_file(pdf_a)
|
||||
window.document.add_note(0, "dirty tab")
|
||||
window._update_current_tab_title()
|
||||
assert window._session_has_unsaved_changes(window.sessions[0])
|
||||
|
||||
window.open_file(pdf_b)
|
||||
assert len(window.sessions) >= 2
|
||||
assert window._session_has_unsaved_changes(window.sessions[0])
|
||||
assert not window._session_has_unsaved_changes(window.sessions[1])
|
||||
|
||||
window.toggle_read_action.setChecked(True)
|
||||
assert window.toggle_read_action.isChecked()
|
||||
assert not window.right_tools.isVisible()
|
||||
assert window.selected_annotation is None
|
||||
assert window.selected_raster_id is None
|
||||
finally:
|
||||
for session in list(window.sessions):
|
||||
session.document.close()
|
||||
window.close()
|
||||
|
||||
|
||||
def assert_large_pdf_rendering(path: Path) -> None:
|
||||
document = PdfDocument()
|
||||
document.open(path)
|
||||
assert document.page_count >= 30
|
||||
for page_index in [0, 10, 20, document.page_count - 1]:
|
||||
page_image = document.render_page(page_index, 0.8)
|
||||
thumb_image = document.render_thumbnail(page_index, 140)
|
||||
assert page_image.width() > 0 and page_image.height() > 0
|
||||
assert thumb_image.width() > 0 and thumb_image.height() > 0
|
||||
document.close()
|
||||
|
||||
|
||||
def main() -> int:
|
||||
temp_dir = Path(tempfile.mkdtemp(prefix="pdfreaderlite_v2_"))
|
||||
try:
|
||||
settings_dir = temp_dir / "settings"
|
||||
settings_dir.mkdir()
|
||||
image_path = temp_dir / "image.png"
|
||||
text_pdf = temp_dir / "text.pdf"
|
||||
scanned_pdf = temp_dir / "scanned.pdf"
|
||||
second_pdf = temp_dir / "second.pdf"
|
||||
large_pdf = temp_dir / "large.pdf"
|
||||
Image.new("RGB", (240, 180), (90, 135, 220)).save(image_path)
|
||||
create_text_pdf(text_pdf)
|
||||
create_scanned_pdf(scanned_pdf, image_path)
|
||||
create_text_pdf(second_pdf, "Second tab")
|
||||
create_many_pages_pdf(large_pdf)
|
||||
|
||||
assert_annotations_survive_save(text_pdf, image_path)
|
||||
assert_scanned_pdf_tools(scanned_pdf)
|
||||
assert_annotation_filter_search_and_properties(second_pdf)
|
||||
assert_main_window_modes_and_tabs(text_pdf, second_pdf, settings_dir)
|
||||
assert_large_pdf_rendering(large_pdf)
|
||||
|
||||
print("PDFReaderLite V2 acceptance test passed.")
|
||||
return 0
|
||||
finally:
|
||||
shutil.rmtree(temp_dir, ignore_errors=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user