| 1 |
# Unit tests for sound_presenter.py methods. |
| 2 |
# |
| 3 |
# Copyright 2025 Igalia, S.L. |
| 4 |
# Author: Joanmarie Diggs <[email protected]> |
| 5 |
# |
| 6 |
# This library is free software; you can redistribute it and/or |
| 7 |
# modify it under the terms of the GNU Lesser General Public |
| 8 |
# License as published by the Free Software Foundation; either |
| 9 |
# version 2.1 of the License, or (at your option) any later version. |
| 10 |
# |
| 11 |
# This library is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
# Lesser General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU Lesser General Public |
| 17 |
# License along with this library; if not, write to the |
| 18 |
# Free Software Foundation, Inc., Franklin Street, Fifth Floor, |
| 19 |
# Boston MA 02110-1301 USA. |
| 20 |
|
| 21 |
# pylint: disable=wrong-import-position |
| 22 |
# pylint: disable=import-outside-toplevel |
| 23 |
# pylint: disable=protected-access |
| 24 |
|
| 25 |
"""Unit tests for sound_presenter.py methods.""" |
| 26 |
|
| 27 |
from __future__ import annotations |
| 28 |
|
| 29 |
from typing import TYPE_CHECKING |
| 30 |
|
| 31 |
import pytest |
| 32 |
|
| 33 |
if TYPE_CHECKING: |
| 34 |
from unittest.mock import MagicMock |
| 35 |
|
| 36 |
from .orca_test_context import OrcaTestContext |
| 37 |
|
| 38 |
|
| 39 |
@pytest.mark.unit |
| 40 |
class TestSoundPresenter: |
| 41 |
"""Test SoundPresenter class methods.""" |
| 42 |
|
| 43 |
def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, MagicMock]: |
| 44 |
"""Set up mocks for sound_presenter dependencies.""" |
| 45 |
|
| 46 |
essential_modules = test_context.setup_shared_dependencies([]) |
| 47 |
|
| 48 |
debug_mock = essential_modules["orca.debug"] |
| 49 |
debug_mock.print_message = test_context.Mock() |
| 50 |
debug_mock.LEVEL_INFO = 800 |
| 51 |
|
| 52 |
dbus_service_mock = essential_modules["orca.dbus_service"] |
| 53 |
controller_mock = test_context.Mock() |
| 54 |
controller_mock.register_decorated_module = test_context.Mock() |
| 55 |
dbus_service_mock.get_remote_controller = test_context.Mock(return_value=controller_mock) |
| 56 |
dbus_service_mock.getter = lambda func: func |
| 57 |
dbus_service_mock.setter = lambda func: func |
| 58 |
|
| 59 |
essential_modules["controller"] = controller_mock |
| 60 |
|
| 61 |
from orca import gsettings_registry |
| 62 |
|
| 63 |
registry = gsettings_registry.get_registry() |
| 64 |
registry.clear_runtime_values() |
| 65 |
|
| 66 |
return essential_modules |
| 67 |
|
| 68 |
def test_initialization_registers_dbus_module(self, test_context: OrcaTestContext) -> None: |
| 69 |
"""Test that SoundPresenter registers itself with D-Bus on initialization.""" |
| 70 |
|
| 71 |
mocks = self._setup_dependencies(test_context) |
| 72 |
|
| 73 |
from orca import sound_presenter |
| 74 |
|
| 75 |
presenter = sound_presenter.SoundPresenter() |
| 76 |
mocks["controller"].register_decorated_module.assert_called_with( |
| 77 |
"SoundPresenter", |
| 78 |
presenter, |
| 79 |
) |
| 80 |
|
| 81 |
def test_get_sound_is_enabled_returns_setting(self, test_context: OrcaTestContext) -> None: |
| 82 |
"""Test get_sound_is_enabled returns the enableSound setting.""" |
| 83 |
|
| 84 |
self._setup_dependencies(test_context) |
| 85 |
|
| 86 |
from orca.sound_presenter import SoundPresenter |
| 87 |
|
| 88 |
presenter = SoundPresenter() |
| 89 |
assert presenter.get_sound_is_enabled() is True |
| 90 |
|
| 91 |
presenter.set_sound_is_enabled(False) |
| 92 |
assert presenter.get_sound_is_enabled() is False |
| 93 |
|
| 94 |
def test_set_sound_is_enabled_updates_setting(self, test_context: OrcaTestContext) -> None: |
| 95 |
"""Test set_sound_is_enabled updates the enableSound setting.""" |
| 96 |
|
| 97 |
self._setup_dependencies(test_context) |
| 98 |
|
| 99 |
from orca.sound_presenter import SoundPresenter |
| 100 |
|
| 101 |
presenter = SoundPresenter() |
| 102 |
result = presenter.set_sound_is_enabled(False) |
| 103 |
|
| 104 |
assert result is True |
| 105 |
assert presenter.get_sound_is_enabled() is False |
| 106 |
|
| 107 |
def test_get_sound_volume_returns_setting(self, test_context: OrcaTestContext) -> None: |
| 108 |
"""Test get_sound_volume returns the soundVolume setting.""" |
| 109 |
|
| 110 |
self._setup_dependencies(test_context) |
| 111 |
|
| 112 |
from orca.sound_presenter import SoundPresenter |
| 113 |
|
| 114 |
presenter = SoundPresenter() |
| 115 |
presenter.set_sound_volume(0.75) |
| 116 |
assert presenter.get_sound_volume() == 0.75 |
| 117 |
|
| 118 |
def test_set_sound_volume_updates_setting(self, test_context: OrcaTestContext) -> None: |
| 119 |
"""Test set_sound_volume updates the soundVolume setting.""" |
| 120 |
|
| 121 |
self._setup_dependencies(test_context) |
| 122 |
|
| 123 |
from orca.sound_presenter import SoundPresenter |
| 124 |
|
| 125 |
presenter = SoundPresenter() |
| 126 |
result = presenter.set_sound_volume(0.8) |
| 127 |
|
| 128 |
assert result is True |
| 129 |
assert presenter.get_sound_volume() == 0.8 |
| 130 |
|
| 131 |
def test_get_beep_progress_bar_updates_returns_setting( |
| 132 |
self, |
| 133 |
test_context: OrcaTestContext, |
| 134 |
) -> None: |
| 135 |
"""Test get_beep_progress_bar_updates returns the beepProgressBarUpdates setting.""" |
| 136 |
|
| 137 |
self._setup_dependencies(test_context) |
| 138 |
|
| 139 |
from orca.sound_presenter import SoundPresenter |
| 140 |
|
| 141 |
presenter = SoundPresenter() |
| 142 |
presenter.set_beep_progress_bar_updates(True) |
| 143 |
assert presenter.get_beep_progress_bar_updates() is True |
| 144 |
|
| 145 |
presenter.set_beep_progress_bar_updates(False) |
| 146 |
assert presenter.get_beep_progress_bar_updates() is False |
| 147 |
|
| 148 |
def test_set_beep_progress_bar_updates_updates_setting( |
| 149 |
self, |
| 150 |
test_context: OrcaTestContext, |
| 151 |
) -> None: |
| 152 |
"""Test set_beep_progress_bar_updates updates the beepProgressBarUpdates setting.""" |
| 153 |
|
| 154 |
self._setup_dependencies(test_context) |
| 155 |
|
| 156 |
from orca.sound_presenter import SoundPresenter |
| 157 |
|
| 158 |
presenter = SoundPresenter() |
| 159 |
result = presenter.set_beep_progress_bar_updates(True) |
| 160 |
|
| 161 |
assert result is True |
| 162 |
assert presenter.get_beep_progress_bar_updates() is True |
| 163 |
|
| 164 |
def test_get_progress_bar_beep_interval_returns_setting( |
| 165 |
self, |
| 166 |
test_context: OrcaTestContext, |
| 167 |
) -> None: |
| 168 |
"""Test get_progress_bar_beep_interval returns the progressBarBeepInterval setting.""" |
| 169 |
|
| 170 |
self._setup_dependencies(test_context) |
| 171 |
|
| 172 |
from orca.sound_presenter import SoundPresenter |
| 173 |
|
| 174 |
presenter = SoundPresenter() |
| 175 |
presenter.set_progress_bar_beep_interval(10) |
| 176 |
assert presenter.get_progress_bar_beep_interval() == 10 |
| 177 |
|
| 178 |
def test_set_progress_bar_beep_interval_updates_setting( |
| 179 |
self, |
| 180 |
test_context: OrcaTestContext, |
| 181 |
) -> None: |
| 182 |
"""Test set_progress_bar_beep_interval updates the progressBarBeepInterval setting.""" |
| 183 |
|
| 184 |
self._setup_dependencies(test_context) |
| 185 |
|
| 186 |
from orca.sound_presenter import SoundPresenter |
| 187 |
|
| 188 |
presenter = SoundPresenter() |
| 189 |
result = presenter.set_progress_bar_beep_interval(15) |
| 190 |
|
| 191 |
assert result is True |
| 192 |
assert presenter.get_progress_bar_beep_interval() == 15 |
| 193 |
|
| 194 |
def test_get_progress_bar_beep_verbosity_returns_setting( |
| 195 |
self, |
| 196 |
test_context: OrcaTestContext, |
| 197 |
) -> None: |
| 198 |
"""Test get_progress_bar_beep_verbosity returns the progressBarBeepVerbosity setting.""" |
| 199 |
|
| 200 |
self._setup_dependencies(test_context) |
| 201 |
|
| 202 |
from orca.sound_presenter import SoundPresenter |
| 203 |
|
| 204 |
presenter = SoundPresenter() |
| 205 |
presenter.set_progress_bar_beep_verbosity(2) |
| 206 |
assert presenter.get_progress_bar_beep_verbosity() == 2 |
| 207 |
|
| 208 |
def test_set_progress_bar_beep_verbosity_updates_setting( |
| 209 |
self, |
| 210 |
test_context: OrcaTestContext, |
| 211 |
) -> None: |
| 212 |
"""Test set_progress_bar_beep_verbosity updates the progressBarBeepVerbosity setting.""" |
| 213 |
|
| 214 |
self._setup_dependencies(test_context) |
| 215 |
|
| 216 |
from orca.sound_presenter import SoundPresenter |
| 217 |
|
| 218 |
presenter = SoundPresenter() |
| 219 |
result = presenter.set_progress_bar_beep_verbosity(2) |
| 220 |
|
| 221 |
assert result is True |
| 222 |
assert presenter.get_progress_bar_beep_verbosity() == 2 |
| 223 |
|
| 224 |
def test_get_presenter_returns_singleton(self, test_context: OrcaTestContext) -> None: |
| 225 |
"""Test get_presenter returns the module-level presenter instance.""" |
| 226 |
|
| 227 |
self._setup_dependencies(test_context) |
| 228 |
|
| 229 |
from orca import sound_presenter |
| 230 |
|
| 231 |
presenter1 = sound_presenter.get_presenter() |
| 232 |
presenter2 = sound_presenter.get_presenter() |
| 233 |
|
| 234 |
assert presenter1 is presenter2 |
| 235 |
assert isinstance(presenter1, sound_presenter.SoundPresenter) |