|
| 1 | +# A part of NonVisual Desktop Access (NVDA) |
| 2 | +# Copyright (C) 2023 NV Access Limited, Łukasz Golonka |
| 3 | +# This file may be used under the terms of the GNU General Public License, version 2 or later. |
| 4 | +# For more details see: https://www.gnu.org/licenses/gpl-2.0.html |
| 5 | + |
| 6 | +"""Set of unit tests veryfying behavior of scripts defined in ``globalCommands``.""" |
| 7 | + |
| 8 | +import unittest |
| 9 | + |
| 10 | +import config |
| 11 | +import globalCommands |
| 12 | +import inputCore |
| 13 | +import speech |
| 14 | + |
| 15 | + |
| 16 | +class _FakeInputGesture(inputCore.InputGesture): |
| 17 | + |
| 18 | + """An input gesture which does nothing, but can be passed to scripts.""" |
| 19 | + |
| 20 | + def _get_identifiers(self): |
| 21 | + """Implemented just to satisfy base class requirements, where this is defined as abstract.""" |
| 22 | + raise RuntimeError("Should not be required in tests.") |
| 23 | + |
| 24 | + |
| 25 | +class SpeechModeSwitching(unittest.TestCase): |
| 26 | + |
| 27 | + """Verifies that switching between speech modes with `NVDA+S` works. |
| 28 | +
|
| 29 | + Ideally we will also ensure that name of the new speech mode is presented to the user, |
| 30 | + but we don't yet track calls to ``speech.speak``, so can't make any assertions on what has been spoken. |
| 31 | + """ |
| 32 | + |
| 33 | + @staticmethod |
| 34 | + def _getCurrSpeechMode() -> speech.SpeechMode: |
| 35 | + """A convenience helper which retrieves currently set speech mode.""" |
| 36 | + return speech.getState().speechMode |
| 37 | + |
| 38 | + @staticmethod |
| 39 | + def _setDisabledSpeechModes(modesToExclude: tuple[speech.SpeechMode, ...]) -> None: |
| 40 | + """Disables given modes in the config.""" |
| 41 | + disabledModes: list[int] = [] |
| 42 | + for modeIndex, mode in enumerate(speech.SpeechMode): |
| 43 | + if mode in modesToExclude: |
| 44 | + disabledModes.append(modeIndex) |
| 45 | + if len(disabledModes) > len(speech.SpeechMode) - 2: |
| 46 | + raise RuntimeError("At least two modes have to be enabled.") |
| 47 | + config.conf["speech"]["excludedSpeechModes"] = disabledModes |
| 48 | + |
| 49 | + def setUp(self) -> None: |
| 50 | + self._origSpeechMode = speech.getState().speechMode |
| 51 | + self._defaultDisabledSpeechModes = config.conf["speech"]["excludedSpeechModes"] |
| 52 | + # The new speech mode is shown in Braille, but displaying messages requires WX to be initialized. |
| 53 | + # Just disable showing messages for these tests. |
| 54 | + self._oldShowBraileMessagesVal = config.conf["braille"]["showMessages"] |
| 55 | + config.conf["braille"]["showMessages"] = 0 # Disabled |
| 56 | + |
| 57 | + def tearDown(self) -> None: |
| 58 | + config.conf["speech"]["excludedSpeechModes"] = self._defaultDisabledSpeechModes |
| 59 | + speech.setSpeechMode(self._origSpeechMode) |
| 60 | + config.conf["braille"]["showMessages"] = self._oldShowBraileMessagesVal |
| 61 | + |
| 62 | + @staticmethod |
| 63 | + def _executeSpeechModeCycleScript(): |
| 64 | + globalCommands.commands.script_speechMode(_FakeInputGesture()) |
| 65 | + |
| 66 | + def test_cyclesThroughAllModesByDefault(self): |
| 67 | + """By default keyboard command should switch between all available speech modes.""" |
| 68 | + seenModes = set() |
| 69 | + for __ in range(len(speech.SpeechMode)): |
| 70 | + self._executeSpeechModeCycleScript() |
| 71 | + seenModes.add(self._getCurrSpeechMode()) |
| 72 | + self.assertEqual(seenModes, set(speech.SpeechMode)) |
| 73 | + # Just to make sure, verify that we returned to the mode set initially. |
| 74 | + self.assertEqual(self._getCurrSpeechMode(), self._origSpeechMode) |
| 75 | + |
| 76 | + def test_nextModeIsUsed(self): |
| 77 | + """Next speech mode is picked when the currently selected is not the last one.""" |
| 78 | + # Verify that expected mode is set initially. |
| 79 | + self.assertEqual(self._getCurrSpeechMode(), speech.SpeechMode.talk) |
| 80 | + self._executeSpeechModeCycleScript() |
| 81 | + self.assertEqual(self._getCurrSpeechMode(), speech.SpeechMode.onDemand) |
| 82 | + |
| 83 | + def test_cyclingWrapsNoMoreModes(self): |
| 84 | + """When the selected speech mode is last on the list, the next press should switch to a first one.""" |
| 85 | + speech.setSpeechMode(speech.SpeechMode.onDemand) |
| 86 | + self._executeSpeechModeCycleScript() |
| 87 | + self.assertEqual(self._getCurrSpeechMode(), speech.SpeechMode.off) |
| 88 | + |
| 89 | + def test_nextModePickedWhenEnabled(self): |
| 90 | + """When the next mode in the list is enabled, pressing the command once should switch to it.""" |
| 91 | + self._setDisabledSpeechModes((speech.SpeechMode.off, speech.SpeechMode.beeps)) |
| 92 | + self._executeSpeechModeCycleScript() |
| 93 | + self.assertEqual(self._getCurrSpeechMode(), speech.SpeechMode.onDemand) |
| 94 | + |
| 95 | + def test_nextModeDisabledMoreModesInTheList(self): |
| 96 | + """Next mode is disabled, yet there are more modes in the list, so no need to wrap to the first one.""" |
| 97 | + self._setDisabledSpeechModes((speech.SpeechMode.beeps,)) |
| 98 | + speech.setSpeechMode(speech.SpeechMode.off) |
| 99 | + self._executeSpeechModeCycleScript() |
| 100 | + self.assertEqual(self._getCurrSpeechMode(), speech.SpeechMode.talk) |
| 101 | + |
| 102 | + def test_nextModeDisabledNoMoreModes(self): |
| 103 | + """When the next mode is disabled, switching wraps to the first mode.""" |
| 104 | + self._setDisabledSpeechModes((speech.SpeechMode.onDemand,)) |
| 105 | + self._executeSpeechModeCycleScript() |
| 106 | + self.assertEqual(self._getCurrSpeechMode(), speech.SpeechMode.off) |
| 107 | + |
| 108 | + def test_nextModesDisabledFirstEnabledNotAtTheBeginning(self): |
| 109 | + """Script has to wrap, mode at the beginning of the list is disabled, first enabled one is picked.""" |
| 110 | + self._setDisabledSpeechModes((speech.SpeechMode.off, speech.SpeechMode.onDemand)) |
| 111 | + self._executeSpeechModeCycleScript() |
| 112 | + self.assertEqual(self._getCurrSpeechMode(), speech.SpeechMode.beeps) |
| 113 | + |
| 114 | + def test_onlyEnabledModesAvailableForSwitching(self): |
| 115 | + """Execute script multiple times and make sure we never switched to one of the disabled modes.""" |
| 116 | + self._setDisabledSpeechModes((speech.SpeechMode.off, speech.SpeechMode.onDemand)) |
| 117 | + seenModes = set() |
| 118 | + for __ in range(30): # Chosen arbitrarily, so that script wraps multipe times. |
| 119 | + self._executeSpeechModeCycleScript() |
| 120 | + seenModes.add(self._getCurrSpeechMode()) |
| 121 | + self.assertEqual(seenModes, {speech.SpeechMode.talk, speech.SpeechMode.beeps}) |
0 commit comments