|
| 1 | +"""Tests for the PSK Picker's right-click context menu.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | +from unittest.mock import patch |
| 7 | + |
| 8 | +import pytest |
| 9 | +from PySide6.QtCore import QPoint |
| 10 | +from PySide6.QtGui import QDesktopServices |
| 11 | +from PySide6.QtWidgets import QMenu, QTreeWidgetItem |
| 12 | + |
| 13 | +from gui.psk_picker import PskPickerPanel |
| 14 | + |
| 15 | +pytestmark = pytest.mark.qt |
| 16 | + |
| 17 | + |
| 18 | +def _capture_menu_actions(panel: PskPickerPanel, item: QTreeWidgetItem) -> list[str]: |
| 19 | + """Drive `_popup_context_menu` and return the list of action labels. |
| 20 | + Mirrors the same pattern used in the unpacker preview-menu tests.""" |
| 21 | + captured: list[list[str]] = [] |
| 22 | + orig_init = QMenu.__init__ |
| 23 | + |
| 24 | + def hooked(self, *args, **kwargs): |
| 25 | + orig_init(self, *args, **kwargs) |
| 26 | + def _rec(*a, **k): |
| 27 | + captured.append([action.text() for action in self.actions()]) |
| 28 | + return None |
| 29 | + self.exec = _rec # type: ignore[assignment] |
| 30 | + |
| 31 | + QMenu.__init__ = hooked # type: ignore[assignment] |
| 32 | + try: |
| 33 | + panel._popup_context_menu(item, QPoint(0, 0)) |
| 34 | + finally: |
| 35 | + QMenu.__init__ = orig_init # type: ignore[assignment] |
| 36 | + return captured[0] if captured else [] |
| 37 | + |
| 38 | + |
| 39 | +def _seed_picker(panel: PskPickerPanel, paths: list[Path]) -> list[QTreeWidgetItem]: |
| 40 | + """Populate the picker with given paths and return the leaf tree items.""" |
| 41 | + panel._all_paths = list(paths) |
| 42 | + panel._categories = [("TestCat", "TestSub") for _ in paths] |
| 43 | + panel._rebuild_tree() |
| 44 | + leaves: list[QTreeWidgetItem] = [] |
| 45 | + root = panel._tree.invisibleRootItem() |
| 46 | + for ci in range(root.childCount()): |
| 47 | + cat = root.child(ci) |
| 48 | + for si in range(cat.childCount()): |
| 49 | + sub = cat.child(si) |
| 50 | + for li in range(sub.childCount()): |
| 51 | + leaves.append(sub.child(li)) |
| 52 | + return leaves |
| 53 | + |
| 54 | + |
| 55 | +def test_menu_for_leaf_offers_preview_and_reveal(qtbot, tmp_path: Path): |
| 56 | + panel = PskPickerPanel() |
| 57 | + qtbot.addWidget(panel) |
| 58 | + psk = tmp_path / "Mesh.psk" |
| 59 | + psk.write_bytes(b"") |
| 60 | + leaves = _seed_picker(panel, [psk]) |
| 61 | + assert leaves, "expected at least one leaf item after rebuild" |
| 62 | + |
| 63 | + actions = _capture_menu_actions(panel, leaves[0]) |
| 64 | + assert actions == ["Preview Mesh", "Open containing folder"] |
| 65 | + |
| 66 | + |
| 67 | +def test_menu_skipped_for_category_header(qtbot, tmp_path: Path): |
| 68 | + """Category/subcategory headers aren't in `_item_to_idx`; the menu builder |
| 69 | + bails before any QMenu actions are added.""" |
| 70 | + panel = PskPickerPanel() |
| 71 | + qtbot.addWidget(panel) |
| 72 | + psk = tmp_path / "Mesh.psk" |
| 73 | + psk.write_bytes(b"") |
| 74 | + _seed_picker(panel, [psk]) |
| 75 | + |
| 76 | + cat_item = panel._tree.invisibleRootItem().child(0) |
| 77 | + actions = _capture_menu_actions(panel, cat_item) |
| 78 | + assert actions == [] |
| 79 | + |
| 80 | + |
| 81 | +def test_preview_action_emits_signal_with_psk_path(qtbot, tmp_path: Path): |
| 82 | + panel = PskPickerPanel() |
| 83 | + qtbot.addWidget(panel) |
| 84 | + psk = tmp_path / "Mesh.psk" |
| 85 | + psk.write_bytes(b"") |
| 86 | + leaves = _seed_picker(panel, [psk]) |
| 87 | + |
| 88 | + # Build the menu, find "Preview Mesh", trigger it, assert signal payload. |
| 89 | + captured: list[QMenu] = [] |
| 90 | + orig_init = QMenu.__init__ |
| 91 | + |
| 92 | + def hooked(self, *args, **kwargs): |
| 93 | + orig_init(self, *args, **kwargs) |
| 94 | + captured.append(self) |
| 95 | + self.exec = lambda *a, **k: None # type: ignore[assignment] |
| 96 | + |
| 97 | + QMenu.__init__ = hooked # type: ignore[assignment] |
| 98 | + try: |
| 99 | + with qtbot.waitSignal(panel.mesh_preview_requested, timeout=2000) as blocker: |
| 100 | + panel._popup_context_menu(leaves[0], QPoint(0, 0)) |
| 101 | + menu = captured[0] |
| 102 | + preview_action = next(a for a in menu.actions() if a.text() == "Preview Mesh") |
| 103 | + preview_action.trigger() |
| 104 | + finally: |
| 105 | + QMenu.__init__ = orig_init # type: ignore[assignment] |
| 106 | + |
| 107 | + assert blocker.args[0] == str(psk) |
| 108 | + |
| 109 | + |
| 110 | +def test_reveal_action_opens_parent_folder_via_qdesktopservices(qtbot, tmp_path: Path): |
| 111 | + panel = PskPickerPanel() |
| 112 | + qtbot.addWidget(panel) |
| 113 | + psk = tmp_path / "sub" / "Mesh.psk" |
| 114 | + psk.parent.mkdir(parents=True, exist_ok=True) |
| 115 | + psk.write_bytes(b"") |
| 116 | + leaves = _seed_picker(panel, [psk]) |
| 117 | + |
| 118 | + captured: list[QMenu] = [] |
| 119 | + orig_init = QMenu.__init__ |
| 120 | + |
| 121 | + def hooked(self, *args, **kwargs): |
| 122 | + orig_init(self, *args, **kwargs) |
| 123 | + captured.append(self) |
| 124 | + self.exec = lambda *a, **k: None # type: ignore[assignment] |
| 125 | + |
| 126 | + QMenu.__init__ = hooked # type: ignore[assignment] |
| 127 | + try: |
| 128 | + with patch.object(QDesktopServices, "openUrl") as mock_open: |
| 129 | + panel._popup_context_menu(leaves[0], QPoint(0, 0)) |
| 130 | + menu = captured[0] |
| 131 | + reveal_action = next( |
| 132 | + a for a in menu.actions() if a.text() == "Open containing folder" |
| 133 | + ) |
| 134 | + reveal_action.trigger() |
| 135 | + finally: |
| 136 | + QMenu.__init__ = orig_init # type: ignore[assignment] |
| 137 | + |
| 138 | + assert mock_open.called |
| 139 | + url = mock_open.call_args[0][0] |
| 140 | + # QUrl.fromLocalFile produces file:// URLs; just check the local path. |
| 141 | + assert Path(url.toLocalFile()) == psk.parent |
0 commit comments