|
| 1 | +import pytest |
| 2 | + |
| 3 | +from qtpy import QtWidgets, QtCore |
| 4 | + |
| 5 | +from mapmanagercore.data import getTiffChannel_1 |
| 6 | +from pymapmanager.interface import PyMapManagerApp |
| 7 | +from pymapmanager._logger import logger |
| 8 | + |
| 9 | +# this makes qapp be our PyMapManagerApp, it is derived from QApplication |
| 10 | +@pytest.fixture(scope="session") |
| 11 | +def qapp_cls(): |
| 12 | + return PyMapManagerApp |
| 13 | + |
| 14 | +def _get_all_actions(menu): |
| 15 | + """Recursevly get all action in a menu and its submenus. |
| 16 | + """ |
| 17 | + actions = [] |
| 18 | + for action in menu.actions(): |
| 19 | + # logger.error(f'action:{action}') |
| 20 | + if isinstance(action, QtWidgets.QAction) and action.isSeparator(): |
| 21 | + continue |
| 22 | + actions.append(action) |
| 23 | + # if isinstance(action, QtWidgets.QMenu): |
| 24 | + if action.menu(): |
| 25 | + # menuTitle = action.menu().title() |
| 26 | + # logger.info(f' recurse on menuTitle:{menuTitle}') |
| 27 | + actions.extend(_get_all_actions(action.menu())) |
| 28 | + return actions |
| 29 | + |
| 30 | +def test_menu_action(qtbot, qapp): |
| 31 | + """Go through all menus, find all nested actions and call action->trigger() |
| 32 | + """ |
| 33 | + # this function is calling menu to set log level !!! |
| 34 | + logger.setLevel('INFO') |
| 35 | + |
| 36 | + # need to open stack widget, app has no menus |
| 37 | + mmapPath = getTiffChannel_1() |
| 38 | + stackWidgetWindow = qapp.loadStackWidget(mmapPath) |
| 39 | + |
| 40 | + # menu_bar = stackWidgetWindow.menuBar() |
| 41 | + mainMenus = stackWidgetWindow._mainMenu |
| 42 | + |
| 43 | + for name, menu in mainMenus._menuDict.items(): |
| 44 | + menuTitle = menu.title() |
| 45 | + logger.info(f'=== Testing action name:{name} menuTitle:{menuTitle}') |
| 46 | + |
| 47 | + menu.aboutToShow.emit() |
| 48 | + |
| 49 | + if name == 'Plugins': |
| 50 | + logger.info(f'skipping "Plugins"') |
| 51 | + continue |
| 52 | + |
| 53 | + actions = _get_all_actions(menu) |
| 54 | + |
| 55 | + # logger.info(f'found {len(actions)} actions') |
| 56 | + for action in actions: |
| 57 | + _actionText = action.text() |
| 58 | + logger.info(f' trigger menuTitle:{menuTitle} _actionText:"{_actionText}"') |
| 59 | + if _actionText in ['Tiff File Ch1', |
| 60 | + 'Tiff File Ch2', |
| 61 | + 'mmap with spines and segments' |
| 62 | + ]: |
| 63 | + continue |
| 64 | + |
| 65 | + if not action.isEnabled(): |
| 66 | + logger.info(f' -->> _actionText:{_actionText} is not enabled') |
| 67 | + else: |
| 68 | + action.trigger() |
0 commit comments