|
4 | 4 |
|
5 | 5 | import os
|
6 | 6 | import pytest
|
| 7 | +import time |
7 | 8 |
|
8 | 9 | import platform
|
9 | 10 | from logging import ERROR
|
10 | 11 |
|
11 |
| -from qtpy import QtCore |
| 12 | +from qtpy import QtCore, QtGui |
12 | 13 | from qtpy.QtCore import QSize
|
13 | 14 | from qtpy.QtWidgets import QMenu, QAction
|
14 | 15 |
|
@@ -218,6 +219,149 @@ def check_command_output(command, expected_retcode, expected_stdout):
|
218 | 219 | assert pydm_shell_command.process is None
|
219 | 220 |
|
220 | 221 |
|
| 222 | +def test_long_running_command_shows_currently_running_text(qtbot): |
| 223 | + """ |
| 224 | + Test that the button is updated to indicate when a command is currently running. |
| 225 | + These indications are: |
| 226 | + - Prepend "(Running...) " to the button's text |
| 227 | + - Make button text italic |
| 228 | + - Disable button while cmd is running (unless allowMultipleExecutions is set True) |
| 229 | + - Set button icon to hourglass symbol |
| 230 | + And the button should be reset back to it's original state after the command is done. |
| 231 | + """ |
| 232 | + pydm_shell_command = PyDMShellCommand() |
| 233 | + qtbot.addWidget(pydm_shell_command) |
| 234 | + pydm_shell_command.stdout = TermOutputMode.HIDE |
| 235 | + |
| 236 | + # Long running cmd, which we will kill after checking button state while its running |
| 237 | + pydm_shell_command.commands = ["for i in {1..100}; do echo $i; sleep 1; done"] |
| 238 | + |
| 239 | + original_text = "Run Long Cmd" |
| 240 | + pydm_shell_command.setText(original_text) |
| 241 | + pydm_shell_command.runCommandsInFullShell = True |
| 242 | + |
| 243 | + # Execute the cmd |
| 244 | + qtbot.mouseClick(pydm_shell_command, QtCore.Qt.LeftButton) |
| 245 | + # Give the command some time to start |
| 246 | + qtbot.wait(100) # wait 0.1 seconds |
| 247 | + |
| 248 | + # Check icon is updated while cmd is running |
| 249 | + icon_size = QSize(16, 16) |
| 250 | + default_icon = IconFont().icon("hourglass-start") |
| 251 | + default_icon_pixmap = default_icon.pixmap(icon_size) |
| 252 | + curr_icon_pixmap = pydm_shell_command.icon().pixmap(icon_size) |
| 253 | + assert curr_icon_pixmap.toImage() == default_icon_pixmap.toImage() |
| 254 | + |
| 255 | + assert pydm_shell_command.font().italic() |
| 256 | + assert pydm_shell_command.text() == f"(Running...) {original_text}" |
| 257 | + |
| 258 | + assert not pydm_shell_command.isEnabled() |
| 259 | + |
| 260 | + # End process since we are done checking the 'running' state of the button |
| 261 | + if pydm_shell_command.process: |
| 262 | + pydm_shell_command.process.kill() |
| 263 | + |
| 264 | + # Wait a bit for things to update |
| 265 | + qtbot.wait(100) # Wait 0.1 seconds |
| 266 | + |
| 267 | + # Check icon is reverted back after cmd stops running |
| 268 | + default_icon = IconFont().icon("cog") |
| 269 | + default_icon_pixmap = default_icon.pixmap(icon_size) |
| 270 | + curr_icon_pixmap = pydm_shell_command.icon().pixmap(icon_size) |
| 271 | + assert curr_icon_pixmap.toImage() == default_icon_pixmap.toImage() |
| 272 | + |
| 273 | + assert not pydm_shell_command.font().italic() |
| 274 | + assert pydm_shell_command.text() == original_text |
| 275 | + |
| 276 | + assert pydm_shell_command.isEnabled() |
| 277 | + |
| 278 | + # This seems to stop some odd interactions during cleanup with pyside6 and ptest-qt |
| 279 | + pydm_shell_command.deleteLater() |
| 280 | + |
| 281 | + |
| 282 | +def test_long_running_command_shows_currently_running_text_dropdown(qtbot): |
| 283 | + """ |
| 284 | + Test that A button with multiple-cmds (so it has a drop-down menu) is updated to indicate |
| 285 | + a drop-down menu command is currently running. |
| 286 | + These indications are: |
| 287 | + - Prepend "(Submenu cmd running...) " to the button's text |
| 288 | + - Prepend "(Running...) " to the curr running drop-down item |
| 289 | + - Make button and curr running drop-down text italic |
| 290 | + - Disable button and all drop-down menu items while cmd is running (unless allowMultipleExecutions is set True) |
| 291 | + - Set both button and curr running drop-down icons to hourglass symbol |
| 292 | + And the button should be reset back to it's original state after the command is done. |
| 293 | + """ |
| 294 | + pydm_shell_command = PyDMShellCommand() |
| 295 | + qtbot.addWidget(pydm_shell_command) |
| 296 | + pydm_shell_command.stdout = TermOutputMode.SHOW |
| 297 | + |
| 298 | + original_button_text = "Run Long Cmd" |
| 299 | + # Text of the specific item in drop-down we want to check the state of |
| 300 | + original_action_text = "for i in {1..50}; do echo $i; sleep 1; done" |
| 301 | + pydm_shell_command.setText(original_button_text) |
| 302 | + pydm_shell_command.runCommandsInFullShell = True |
| 303 | + |
| 304 | + pydm_shell_command.commands = ["echo 'command 1'", "echo 'command 2'", original_action_text] |
| 305 | + |
| 306 | + # We need to execute the shell-cmd's mousePressEvent() so it can build the drop-down menu, |
| 307 | + # and right-click since left-click seems to cause this test to then display the drop-down menu |
| 308 | + # and just pauses waiting for user interaction. |
| 309 | + qtbot.mouseClick(pydm_shell_command, QtCore.Qt.RightButton) |
| 310 | + |
| 311 | + actions = pydm_shell_command.menu().actions() |
| 312 | + assert len(actions) >= 3 |
| 313 | + |
| 314 | + # Activate drop-down menu button cmd |
| 315 | + actions[2].trigger() |
| 316 | + # Wait a bit for things to update |
| 317 | + qtbot.wait(100) # Wait 0.1 seconds |
| 318 | + |
| 319 | + # Check button icon is changed while cmd is running |
| 320 | + icon_size = QSize(16, 16) |
| 321 | + default_icon = IconFont().icon("hourglass-start") |
| 322 | + default_icon_pixmap = default_icon.pixmap(icon_size) |
| 323 | + curr_icon_pixmap = pydm_shell_command.icon().pixmap(icon_size) |
| 324 | + assert curr_icon_pixmap.toImage() == default_icon_pixmap.toImage() |
| 325 | + |
| 326 | + assert pydm_shell_command.text() == f"(Submenu cmd running...) {original_button_text}" |
| 327 | + assert pydm_shell_command.font().italic() |
| 328 | + |
| 329 | + # Check state of action buttons in drop-down menu |
| 330 | + # Check icon is changed on curr running action button while cmd is running |
| 331 | + default_icon = IconFont().icon("hourglass-start") |
| 332 | + default_icon_pixmap = default_icon.pixmap(icon_size) |
| 333 | + curr_icon_pixmap = actions[2].icon().pixmap(icon_size) |
| 334 | + assert curr_icon_pixmap.toImage() == default_icon_pixmap.toImage() |
| 335 | + |
| 336 | + assert actions[2].text() == f"(Running...) {original_action_text}" |
| 337 | + assert actions[2].font().italic() |
| 338 | + |
| 339 | + assert all(not action.isEnabled() for action in actions) |
| 340 | + |
| 341 | + # End process since we are done checking the 'running' state of the button |
| 342 | + if pydm_shell_command.process: |
| 343 | + pydm_shell_command.process.kill() |
| 344 | + |
| 345 | + # Wait a bit for things to update |
| 346 | + qtbot.wait(100) # wait 0.1 seconds |
| 347 | + |
| 348 | + # Check button icon is reverted back after cmd stops running |
| 349 | + default_icon = IconFont().icon("cog") |
| 350 | + default_icon_pixmap = default_icon.pixmap(icon_size) |
| 351 | + curr_icon_pixmap = pydm_shell_command.icon().pixmap(icon_size) |
| 352 | + assert curr_icon_pixmap.toImage() == default_icon_pixmap.toImage() |
| 353 | + |
| 354 | + assert pydm_shell_command.text() == original_button_text |
| 355 | + assert not pydm_shell_command.font().italic() |
| 356 | + |
| 357 | + # Check drop-down menu icon is reverted |
| 358 | + actions[2].icon().isNull() # Drop-down menu cmds have no icon by default |
| 359 | + assert all(action.isEnabled() for action in actions) |
| 360 | + |
| 361 | + # This seems to stop some odd interactions during cleanup with pyside6 and ptest-qt |
| 362 | + pydm_shell_command.deleteLater() |
| 363 | + |
| 364 | + |
221 | 365 | @pytest.mark.parametrize(
|
222 | 366 | "allow_multiple",
|
223 | 367 | [
|
|
0 commit comments