Files
Copies/tests/test_gui_cut_helper.py
T

57 lines
2.4 KiB
Python

import os
import tempfile
import tkinter as tk
import unittest
from pathlib import Path
from types import SimpleNamespace
import pymupdf
from copienator_gui.cut_helper import CutHelper, PAGE_GAP
from copienator_gui.manual_resolution import ManualResolutionPanel
@unittest.skipUnless(os.environ.get("DISPLAY"), "requires a display")
class CutHelperTests(unittest.TestCase):
def test_cut_button_drag_to_page_gap_and_enter_only_produces_command(self):
with tempfile.TemporaryDirectory() as temporary:
directory = Path(temporary)
source = directory / "Copies" / "Copie16" / "A.pdf"
source.parent.mkdir(parents=True)
with pymupdf.open() as document:
for height in (200, 400):
page = document.new_page(width=300, height=height)
page.insert_text((20, 40), "Student answer")
document.save(source)
manual = directory / "manual_resolutions.txt"
manual.write_text("Copie16 A -> B|\n")
original = source.read_bytes()
root = tk.Tk()
try:
panel = ManualResolutionPanel(root, lambda: directory)
panel.pack()
root.update()
self.assertEqual(len(panel.cut_buttons), 1)
panel.cut_buttons[0].invoke()
helper = next(child for child in panel.winfo_children() if isinstance(child, CutHelper))
helper.canvas.yview_moveto(0)
root.update()
helper.move_bar(SimpleNamespace(y=200 * helper.scale + PAGE_GAP / 2))
self.assertIn("entre les pages 1 et 2", helper.caption.get())
helper.keep.set(2)
helper.mode.set("x")
helper.focus_force()
root.update()
helper.event_generate("<Return>")
root.update()
self.assertFalse(helper.winfo_exists())
self.assertEqual(panel.cut_result.get(), "Copie16 A c{33.333333}2x B|")
panel.copy_cut_command()
self.assertEqual(root.clipboard_get(), panel.cut_result.get())
self.assertEqual(source.read_bytes(), original)
self.assertEqual(manual.read_text(), "Copie16 A -> B|\n")
panel.reload()
self.assertEqual(len(panel.cut_buttons), 1)
finally:
root.destroy()