1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
# (C) Copyright 2004-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
"""
Test that menu and toolbar actions are triggered.
"""
from functools import partial
import unittest
import pyface
from pyface.action.schema.api import ActionSchema, SGroup, SMenu, SMenuBar, SToolBar
from traits.has_traits import HasTraits
from traits.trait_types import Bool
from traitsui.menu import Action, ActionGroup, Menu, MenuBar, ToolBar
from traitsui.item import Item
from traitsui.view import View
from traitsui.tests._tools import (
BaseTestMixin,
create_ui,
is_mac_os,
is_null,
requires_toolkit,
reraise_exceptions,
ToolkitName,
)
if is_null():
raise unittest.SkipTest("Not supported using the null backend")
TestAction = Action(
name="Test", action="test_clicked", tooltip="Click to test"
)
class DialogWithToolbar(HasTraits):
"""Test dialog with toolbar and menu."""
action_successful = Bool(False)
def test_clicked(self):
self.action_successful = True
menubar = MenuBar(Menu(ActionGroup(TestAction), name="&Test menu"))
toolbar = ToolBar(ActionGroup(TestAction))
traits_view = View(
Item(
label="Click the button on the toolbar or the menu item.\n"
"The 'Action successful' element should turn to True."
),
Item("action_successful", style="readonly"),
menubar=menubar,
toolbar=toolbar,
buttons=[TestAction, "OK"],
)
class DialogWithSchema(HasTraits):
"""Test dialog with toolbar and menu schemas."""
action_successful = Bool(False)
def test_clicked(self):
self.action_successful = True
menubar = SMenuBar(
SMenu(
SGroup(
ActionSchema(
action_factory=lambda **traits: TestAction,
),
),
name="&Test menu",
),
)
toolbar = SToolBar(
SGroup(
ActionSchema(
action_factory=lambda **traits: TestAction,
),
),
)
traits_view = View(
Item(
label="Click the button on the toolbar or the menu item.\n"
"The 'Action successful' element should turn to True."
),
Item("action_successful", style="readonly"),
menubar=menubar,
toolbar=toolbar,
buttons=[TestAction, "OK"],
)
# ----- qt helper functions
def _qt_trigger_action(container_class, ui):
toolbar = ui.control.findChild(container_class)
action = toolbar.actions()[0]
action.trigger()
def _qt_click_button(ui):
from pyface.qt.QtGui import QDialogButtonBox
bbox = ui.control.findChild(QDialogButtonBox)
button = bbox.buttons()[1]
button.click()
class TestActions(BaseTestMixin, unittest.TestCase):
def _test_actions(self, trigger_action_func):
"""Template test for wx, qt, menu, and toolbar testing."""
# Behavior: when clicking on a menu or toolbar action,
# the corresponding function should be executed
# create dialog with toolbar adn menu
dialog = DialogWithToolbar()
with reraise_exceptions(), create_ui(dialog) as ui:
# press toolbar or menu button
trigger_action_func(ui)
# verify that the action was triggered
self.assertTrue(dialog.action_successful)
# ----- Qt tests
@requires_toolkit([ToolkitName.qt])
def test_qt_toolbar_action(self):
# Behavior: when clicking on a toolbar action, the corresponding
# function should be executed
# Bug: in the Qt4 backend, a
# TypeError: perform() takes exactly 2 arguments (1 given) was raised
# instead
from pyface.ui.qt.action.tool_bar_manager import _ToolBar
qt_trigger_toolbar_action = partial(_qt_trigger_action, _ToolBar)
self._test_actions(qt_trigger_toolbar_action)
@requires_toolkit([ToolkitName.qt])
def test_qt_menu_action(self):
# Behavior: when clicking on a menu action, the corresponding function
# should be executed
# Bug: in the Qt4 backend, a
# TypeError: perform() takes exactly 2 arguments (1 given) was raised
# instead
from pyface.ui.qt.action.menu_manager import _Menu
qt_trigger_menu_action = partial(
_qt_trigger_action, _Menu
)
self._test_actions(qt_trigger_menu_action)
@requires_toolkit([ToolkitName.qt])
def test_qt_button_action(self):
# Behavior: when clicking on a button action, the corresponding
# function should be executed
# Bug: in the Qt4 backend, a
# TypeError: perform() takes exactly 2 arguments (1 given) was raised
# instead
self._test_actions(_qt_click_button)
# ----- wx tests
@unittest.skip(
"Problem with triggering toolbar actions. Issue #428 and #1843."
)
@requires_toolkit([ToolkitName.wx])
def test_wx_toolbar_action(self):
# Behavior: when clicking on a toolbar action, the corresponding
# function should be executed
import wx
def _wx_trigger_toolbar_action(ui):
# long road to get at the Id of the toolbar button
toolbar_control = ui.control.GetToolBar()
toolbar_item = toolbar_control.tool_bar_manager.groups[0].items[0]
toolbar_item_wrapper = toolbar_item._wrappers[0]
control_id = toolbar_item_wrapper.control_id
# build event that clicks the button
click_event = wx.CommandEvent(
wx.wxEVT_COMMAND_TOOL_CLICKED, control_id
)
# send the event to the toolbar
toolbar_control.ProcessEvent(click_event)
self._test_actions(_wx_trigger_toolbar_action)
@requires_toolkit([ToolkitName.wx])
def test_wx_button_action(self):
# Behavior: when clicking on a button action, the corresponding
# function should be executed
import wx
def _wx_trigger_button_action(ui):
# long road to get at the Id of the toolbar button
button_sizer = ui.control.GetSizer().GetChildren()[2].GetSizer()
button = button_sizer.GetChildren()[0].GetWindow()
control_id = button.GetId()
# build event that clicks the button
click_event = wx.CommandEvent(
wx.wxEVT_COMMAND_BUTTON_CLICKED, control_id
)
# send the event to the toolbar
ui.control.ProcessEvent(click_event)
self._test_actions(_wx_trigger_button_action)
# TODO: I couldn't find a way to press menu items programmatically for wx
class TestActionSchemas(BaseTestMixin, unittest.TestCase):
def setUp(self):
BaseTestMixin.setUp(self)
def tearDown(self):
BaseTestMixin.tearDown(self)
def _test_actions(self, trigger_action_func):
"""Template test for wx, qt, menu, and toolbar testing."""
# Behavior: when clicking on a menu or toolbar action,
# the corresponding function should be executed
# create dialog with toolbar adn menu
dialog = DialogWithSchema()
with reraise_exceptions(), create_ui(dialog) as ui:
# press toolbar or menu button
trigger_action_func(ui)
# verify that the action was triggered
self.assertTrue(dialog.action_successful)
# ----- Qt tests
@requires_toolkit([ToolkitName.qt])
def test_qt_toolbar_action(self):
# Behavior: when clicking on a toolbar action, the corresponding
# function should be executed
# Bug: in the Qt4 backend, a
# TypeError: perform() takes exactly 2 arguments (1 given) was raised
# instead
from pyface.ui.qt.action.tool_bar_manager import _ToolBar
qt_trigger_toolbar_action = partial(_qt_trigger_action, _ToolBar)
self._test_actions(qt_trigger_toolbar_action)
@requires_toolkit([ToolkitName.qt])
def test_qt_menu_action(self):
# Behavior: when clicking on a menu action, the corresponding function
# should be executed
# Bug: in the Qt4 backend, a
# TypeError: perform() takes exactly 2 arguments (1 given) was raised
# instead
from pyface.ui.qt.action.menu_manager import _Menu
qt_trigger_menu_action = partial(_qt_trigger_action, _Menu)
self._test_actions(qt_trigger_menu_action)
# ----- wx tests
@unittest.skipIf(
not is_mac_os,
"Problem with triggering toolbar actions on Linux and Windows. Issue #428.", # noqa: E501
)
@requires_toolkit([ToolkitName.wx])
def test_wx_toolbar_action(self):
# Behavior: when clicking on a toolbar action, the corresponding
# function should be executed
import wx
def _wx_trigger_toolbar_action(ui):
# long road to get at the Id of the toolbar button
toolbar_control = ui.control.GetToolBar()
toolbar_item = toolbar_control.tool_bar_manager.groups[0].items[0]
toolbar_item_wrapper = toolbar_item._wrappers[0]
control_id = toolbar_item_wrapper.control_id
# build event that clicks the button
click_event = wx.CommandEvent(
wx.wxEVT_COMMAND_TOOL_CLICKED, control_id
)
# send the event to the toolbar
toolbar_control.ProcessEvent(click_event)
self._test_actions(_wx_trigger_toolbar_action)
# TODO: I couldn't find a way to press menu items programmatically for wx
if __name__ == "__main__":
# Execute from command line for manual testing
vw = DialogWithToolbar()
vw.configure_traits()
|