| 1 |
# Unit tests for ax_value.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=too-many-public-methods |
| 22 |
# pylint: disable=wrong-import-position |
| 23 |
# pylint: disable=protected-access |
| 24 |
# pylint: disable=too-many-arguments |
| 25 |
# pylint: disable=too-many-positional-arguments |
| 26 |
# pylint: disable=import-outside-toplevel |
| 27 |
|
| 28 |
"""Unit tests for ax_value.py methods.""" |
| 29 |
|
| 30 |
from __future__ import annotations |
| 31 |
|
| 32 |
from typing import TYPE_CHECKING |
| 33 |
|
| 34 |
import gi |
| 35 |
import pytest |
| 36 |
|
| 37 |
gi.require_version("Atspi", "2.0") |
| 38 |
from gi.repository import Atspi, GLib |
| 39 |
|
| 40 |
if TYPE_CHECKING: |
| 41 |
from unittest.mock import MagicMock |
| 42 |
|
| 43 |
from .orca_test_context import OrcaTestContext |
| 44 |
|
| 45 |
|
| 46 |
@pytest.mark.unit |
| 47 |
class TestAXValue: |
| 48 |
"""Test AXValue class methods.""" |
| 49 |
|
| 50 |
def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, MagicMock]: |
| 51 |
"""Returns dependencies for ax_value module testing.""" |
| 52 |
|
| 53 |
additional_modules = ["orca.ax_utilities", "orca.ax_utilities_state"] |
| 54 |
essential_modules = test_context.setup_shared_dependencies(additional_modules) |
| 55 |
|
| 56 |
debug_mock = essential_modules["orca.debug"] |
| 57 |
debug_mock.print_tokens = test_context.Mock() |
| 58 |
debug_mock.LEVEL_INFO = 800 |
| 59 |
|
| 60 |
ax_object_class_mock = test_context.Mock() |
| 61 |
ax_object_class_mock.supports_value = test_context.Mock(return_value=True) |
| 62 |
ax_object_class_mock.get_attribute = test_context.Mock(return_value="") |
| 63 |
essential_modules["orca.ax_object"].AXObject = ax_object_class_mock |
| 64 |
|
| 65 |
ax_utilities_class_mock = test_context.Mock() |
| 66 |
ax_utilities_class_mock.is_indeterminate = test_context.Mock(return_value=False) |
| 67 |
essential_modules["orca.ax_utilities"].AXUtilities = ax_utilities_class_mock |
| 68 |
|
| 69 |
ax_utilities_state_class_mock = test_context.Mock() |
| 70 |
ax_utilities_state_class_mock.is_indeterminate = test_context.Mock(return_value=False) |
| 71 |
essential_modules[ |
| 72 |
"orca.ax_utilities_state" |
| 73 |
].AXUtilitiesState = ax_utilities_state_class_mock |
| 74 |
|
| 75 |
return essential_modules |
| 76 |
|
| 77 |
@pytest.mark.parametrize( |
| 78 |
"case", |
| 79 |
[ |
| 80 |
{ |
| 81 |
"id": "no_value_support", |
| 82 |
"supports_value": False, |
| 83 |
"atspi_return_value": 0.0, |
| 84 |
"should_raise_error": False, |
| 85 |
"expected_result": 0.0, |
| 86 |
}, |
| 87 |
{ |
| 88 |
"id": "success_case", |
| 89 |
"supports_value": True, |
| 90 |
"atspi_return_value": 42.5, |
| 91 |
"should_raise_error": False, |
| 92 |
"expected_result": 42.5, |
| 93 |
}, |
| 94 |
{ |
| 95 |
"id": "glib_error", |
| 96 |
"supports_value": True, |
| 97 |
"atspi_return_value": 0.0, |
| 98 |
"should_raise_error": True, |
| 99 |
"expected_result": 0.0, |
| 100 |
}, |
| 101 |
], |
| 102 |
ids=lambda case: case["id"], |
| 103 |
) |
| 104 |
def test_get_current_value( |
| 105 |
self, |
| 106 |
test_context: OrcaTestContext, |
| 107 |
case: dict, |
| 108 |
) -> None: |
| 109 |
"""Test AXValue.get_current_value.""" |
| 110 |
|
| 111 |
self._setup_dependencies(test_context) |
| 112 |
from orca.ax_object import AXObject |
| 113 |
from orca.ax_value import AXValue |
| 114 |
|
| 115 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 116 |
test_context.patch_object( |
| 117 |
AXObject, |
| 118 |
"supports_value", |
| 119 |
side_effect=lambda obj: case["supports_value"], |
| 120 |
) |
| 121 |
|
| 122 |
if case["should_raise_error"]: |
| 123 |
|
| 124 |
def raise_glib_error(obj): |
| 125 |
raise GLib.GError("Test error") |
| 126 |
|
| 127 |
test_context.patch_object( |
| 128 |
Atspi.Value, |
| 129 |
"get_current_value", |
| 130 |
side_effect=raise_glib_error, |
| 131 |
) |
| 132 |
else: |
| 133 |
test_context.patch_object( |
| 134 |
Atspi.Value, |
| 135 |
"get_current_value", |
| 136 |
side_effect=lambda obj: case["atspi_return_value"], |
| 137 |
) |
| 138 |
|
| 139 |
result = AXValue.get_current_value(mock_obj) |
| 140 |
assert result == case["expected_result"] |
| 141 |
|
| 142 |
@pytest.mark.parametrize( |
| 143 |
"case", |
| 144 |
[ |
| 145 |
{ |
| 146 |
"id": "valuetext_attribute", |
| 147 |
"supports_value": True, |
| 148 |
"valuetext_attr": "75%", |
| 149 |
"atspi_text": "", |
| 150 |
"should_raise_error": False, |
| 151 |
"current_value": 0.0, |
| 152 |
"expected_result": "75%", |
| 153 |
}, |
| 154 |
{ |
| 155 |
"id": "no_value_support", |
| 156 |
"supports_value": False, |
| 157 |
"valuetext_attr": "", |
| 158 |
"atspi_text": "", |
| 159 |
"should_raise_error": False, |
| 160 |
"current_value": 0.0, |
| 161 |
"expected_result": "", |
| 162 |
}, |
| 163 |
{ |
| 164 |
"id": "from_atspi", |
| 165 |
"supports_value": True, |
| 166 |
"valuetext_attr": "", |
| 167 |
"atspi_text": "50 percent", |
| 168 |
"should_raise_error": False, |
| 169 |
"current_value": 0.0, |
| 170 |
"expected_result": "50 percent", |
| 171 |
}, |
| 172 |
{ |
| 173 |
"id": "glib_error", |
| 174 |
"supports_value": True, |
| 175 |
"valuetext_attr": "", |
| 176 |
"atspi_text": "", |
| 177 |
"should_raise_error": True, |
| 178 |
"current_value": 42.0, |
| 179 |
"expected_result": "42", |
| 180 |
}, |
| 181 |
{ |
| 182 |
"id": "decimal_places", |
| 183 |
"supports_value": True, |
| 184 |
"valuetext_attr": "", |
| 185 |
"atspi_text": "", |
| 186 |
"should_raise_error": False, |
| 187 |
"current_value": 0.125, |
| 188 |
"expected_result": "0.125", |
| 189 |
}, |
| 190 |
], |
| 191 |
ids=lambda case: case["id"], |
| 192 |
) |
| 193 |
def test_get_current_value_text( # pylint: disable=too-many-locals |
| 194 |
self, |
| 195 |
test_context: OrcaTestContext, |
| 196 |
case: dict, |
| 197 |
) -> None: |
| 198 |
"""Test AXValue.get_current_value_text with various scenarios.""" |
| 199 |
|
| 200 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 201 |
|
| 202 |
def mock_get_attribute(obj, name, check_computed=True): # pylint: disable=unused-argument |
| 203 |
if name == "valuetext": |
| 204 |
return case["valuetext_attr"] |
| 205 |
return "" |
| 206 |
|
| 207 |
essential_modules["orca.ax_object"].AXObject.get_attribute = mock_get_attribute |
| 208 |
essential_modules["orca.ax_object"].AXObject.supports_value = test_context.Mock( |
| 209 |
return_value=case["supports_value"], |
| 210 |
) |
| 211 |
|
| 212 |
from orca.ax_object import AXObject |
| 213 |
from orca.ax_value import AXValue |
| 214 |
|
| 215 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 216 |
|
| 217 |
test_context.patch_object(AXObject, "get_attribute", side_effect=mock_get_attribute) |
| 218 |
test_context.patch_object( |
| 219 |
AXObject, |
| 220 |
"supports_value", |
| 221 |
side_effect=lambda obj: case["supports_value"], |
| 222 |
) |
| 223 |
|
| 224 |
if case["should_raise_error"]: |
| 225 |
|
| 226 |
def raise_glib_error(self): |
| 227 |
raise GLib.GError("Test error") |
| 228 |
|
| 229 |
test_context.patch_object(Atspi.Value, "get_text", side_effect=raise_glib_error) |
| 230 |
test_context.patch_object( |
| 231 |
AXValue, |
| 232 |
"get_current_value", |
| 233 |
side_effect=lambda obj: case["current_value"], |
| 234 |
) |
| 235 |
else: |
| 236 |
test_context.patch_object( |
| 237 |
Atspi.Value, |
| 238 |
"get_text", |
| 239 |
side_effect=lambda self: case["atspi_text"], |
| 240 |
) |
| 241 |
if case["current_value"] != 0.0: |
| 242 |
test_context.patch_object( |
| 243 |
AXValue, |
| 244 |
"get_current_value", |
| 245 |
side_effect=lambda obj: case["current_value"], |
| 246 |
) |
| 247 |
|
| 248 |
test_context.patch_object(Atspi.Value, "get_current_value", return_value=0.0) |
| 249 |
|
| 250 |
result = AXValue.get_current_value_text(mock_obj) |
| 251 |
assert result == case["expected_result"] |
| 252 |
|
| 253 |
@pytest.mark.parametrize( |
| 254 |
"case", |
| 255 |
[ |
| 256 |
{ |
| 257 |
"id": "no_value_support", |
| 258 |
"supports_value": False, |
| 259 |
"current_value": None, |
| 260 |
"is_indeterminate": False, |
| 261 |
"min_val": None, |
| 262 |
"max_val": None, |
| 263 |
"expected_result": None, |
| 264 |
}, |
| 265 |
{ |
| 266 |
"id": "indeterminate_state", |
| 267 |
"supports_value": True, |
| 268 |
"current_value": 0.0, |
| 269 |
"is_indeterminate": True, |
| 270 |
"min_val": None, |
| 271 |
"max_val": None, |
| 272 |
"expected_result": None, |
| 273 |
}, |
| 274 |
{ |
| 275 |
"id": "min_equals_max", |
| 276 |
"supports_value": True, |
| 277 |
"current_value": 50.0, |
| 278 |
"is_indeterminate": False, |
| 279 |
"min_val": 100.0, |
| 280 |
"max_val": 100.0, |
| 281 |
"expected_result": None, |
| 282 |
}, |
| 283 |
{ |
| 284 |
"id": "normal_calculation", |
| 285 |
"supports_value": True, |
| 286 |
"current_value": 75.0, |
| 287 |
"is_indeterminate": False, |
| 288 |
"min_val": 0.0, |
| 289 |
"max_val": 100.0, |
| 290 |
"expected_result": 75, |
| 291 |
}, |
| 292 |
], |
| 293 |
ids=lambda case: case["id"], |
| 294 |
) |
| 295 |
def test_get_value_as_percent( |
| 296 |
self, |
| 297 |
test_context, |
| 298 |
case: dict, |
| 299 |
) -> None: |
| 300 |
"""Test AXValue.get_value_as_percent.""" |
| 301 |
|
| 302 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 303 |
|
| 304 |
supports_value_mock = test_context.Mock(return_value=case["supports_value"]) |
| 305 |
is_indeterminate_mock = test_context.Mock(return_value=case["is_indeterminate"]) |
| 306 |
essential_modules["orca.ax_object"].AXObject.supports_value = supports_value_mock |
| 307 |
essential_modules[ |
| 308 |
"orca.ax_utilities_state" |
| 309 |
].AXUtilitiesState.is_indeterminate = is_indeterminate_mock |
| 310 |
|
| 311 |
from orca.ax_object import AXObject |
| 312 |
from orca.ax_utilities_state import AXUtilitiesState |
| 313 |
from orca.ax_value import AXValue |
| 314 |
|
| 315 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 316 |
|
| 317 |
test_context.patch_object( |
| 318 |
AXObject, |
| 319 |
"supports_value", |
| 320 |
side_effect=lambda obj: case["supports_value"], |
| 321 |
) |
| 322 |
|
| 323 |
current_val = case["current_value"] if case["current_value"] is not None else 0.0 |
| 324 |
min_val_default = case["min_val"] if case["min_val"] is not None else 0.0 |
| 325 |
max_val_default = case["max_val"] if case["max_val"] is not None else 100.0 |
| 326 |
|
| 327 |
test_context.patch_object( |
| 328 |
Atspi.Value, |
| 329 |
"get_current_value", |
| 330 |
side_effect=lambda obj: current_val, |
| 331 |
) |
| 332 |
test_context.patch_object( |
| 333 |
Atspi.Value, |
| 334 |
"get_minimum_value", |
| 335 |
side_effect=lambda obj: min_val_default, |
| 336 |
) |
| 337 |
test_context.patch_object( |
| 338 |
Atspi.Value, |
| 339 |
"get_maximum_value", |
| 340 |
side_effect=lambda obj: max_val_default, |
| 341 |
) |
| 342 |
|
| 343 |
test_context.patch_object( |
| 344 |
AXUtilitiesState, |
| 345 |
"is_indeterminate", |
| 346 |
side_effect=lambda obj: case["is_indeterminate"], |
| 347 |
) |
| 348 |
|
| 349 |
if case["current_value"] is not None: |
| 350 |
test_context.patch_object( |
| 351 |
AXValue, |
| 352 |
"get_current_value", |
| 353 |
side_effect=lambda obj: case["current_value"], |
| 354 |
) |
| 355 |
if case["min_val"] is not None: |
| 356 |
test_context.patch_object( |
| 357 |
AXValue, |
| 358 |
"get_minimum_value", |
| 359 |
side_effect=lambda obj: case["min_val"], |
| 360 |
) |
| 361 |
if case["max_val"] is not None: |
| 362 |
test_context.patch_object( |
| 363 |
AXValue, |
| 364 |
"get_maximum_value", |
| 365 |
side_effect=lambda obj: case["max_val"], |
| 366 |
) |
| 367 |
|
| 368 |
result = AXValue.get_value_as_percent(mock_obj) |
| 369 |
assert result == case["expected_result"] |
| 370 |
|
| 371 |
@pytest.mark.parametrize( |
| 372 |
"case", |
| 373 |
[ |
| 374 |
{ |
| 375 |
"id": "min_no_value_support", |
| 376 |
"method_name": "get_minimum_value", |
| 377 |
"supports_value": False, |
| 378 |
"atspi_return_value": None, |
| 379 |
"should_raise_error": False, |
| 380 |
"expected_result": 0.0, |
| 381 |
}, |
| 382 |
{ |
| 383 |
"id": "min_success_case", |
| 384 |
"method_name": "get_minimum_value", |
| 385 |
"supports_value": True, |
| 386 |
"atspi_return_value": 10.0, |
| 387 |
"should_raise_error": False, |
| 388 |
"expected_result": 10.0, |
| 389 |
}, |
| 390 |
{ |
| 391 |
"id": "min_glib_error", |
| 392 |
"method_name": "get_minimum_value", |
| 393 |
"supports_value": True, |
| 394 |
"atspi_return_value": None, |
| 395 |
"should_raise_error": True, |
| 396 |
"expected_result": 0.0, |
| 397 |
}, |
| 398 |
{ |
| 399 |
"id": "min_negative_values", |
| 400 |
"method_name": "get_minimum_value", |
| 401 |
"supports_value": True, |
| 402 |
"atspi_return_value": -50.5, |
| 403 |
"should_raise_error": False, |
| 404 |
"expected_result": -50.5, |
| 405 |
}, |
| 406 |
{ |
| 407 |
"id": "max_no_value_support", |
| 408 |
"method_name": "get_maximum_value", |
| 409 |
"supports_value": False, |
| 410 |
"atspi_return_value": None, |
| 411 |
"should_raise_error": False, |
| 412 |
"expected_result": 0.0, |
| 413 |
}, |
| 414 |
{ |
| 415 |
"id": "max_success_case", |
| 416 |
"method_name": "get_maximum_value", |
| 417 |
"supports_value": True, |
| 418 |
"atspi_return_value": 100.0, |
| 419 |
"should_raise_error": False, |
| 420 |
"expected_result": 100.0, |
| 421 |
}, |
| 422 |
{ |
| 423 |
"id": "max_glib_error", |
| 424 |
"method_name": "get_maximum_value", |
| 425 |
"supports_value": True, |
| 426 |
"atspi_return_value": None, |
| 427 |
"should_raise_error": True, |
| 428 |
"expected_result": 0.0, |
| 429 |
}, |
| 430 |
{ |
| 431 |
"id": "max_large_values", |
| 432 |
"method_name": "get_maximum_value", |
| 433 |
"supports_value": True, |
| 434 |
"atspi_return_value": 99999.99, |
| 435 |
"should_raise_error": False, |
| 436 |
"expected_result": 99999.99, |
| 437 |
}, |
| 438 |
], |
| 439 |
ids=lambda case: case["id"], |
| 440 |
) |
| 441 |
def test_get_minimum_maximum_value( |
| 442 |
self, |
| 443 |
test_context: OrcaTestContext, |
| 444 |
case: dict, |
| 445 |
) -> None: |
| 446 |
"""Test AXValue.get_minimum_value and get_maximum_value with various scenarios.""" |
| 447 |
|
| 448 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 449 |
from orca.ax_object import AXObject |
| 450 |
from orca.ax_value import AXValue |
| 451 |
|
| 452 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 453 |
test_context.patch_object( |
| 454 |
AXObject, |
| 455 |
"supports_value", |
| 456 |
side_effect=lambda obj: case["supports_value"], |
| 457 |
) |
| 458 |
|
| 459 |
if case["should_raise_error"]: |
| 460 |
|
| 461 |
def raise_glib_error(obj): # pylint: disable=unused-argument |
| 462 |
error_type = "minimum" if "minimum" in case["method_name"] else "maximum" |
| 463 |
raise GLib.GError(f"Test {error_type} value error") |
| 464 |
|
| 465 |
test_context.patch_object( |
| 466 |
Atspi.Value, |
| 467 |
case["method_name"], |
| 468 |
side_effect=raise_glib_error, |
| 469 |
) |
| 470 |
elif case["atspi_return_value"] is not None: |
| 471 |
test_context.patch_object( |
| 472 |
Atspi.Value, |
| 473 |
case["method_name"], |
| 474 |
side_effect=lambda obj: case["atspi_return_value"], |
| 475 |
) |
| 476 |
else: |
| 477 |
test_context.patch_object(Atspi.Value, case["method_name"], return_value=0.0) |
| 478 |
|
| 479 |
result = getattr(AXValue, case["method_name"])(mock_obj) |
| 480 |
assert result == case["expected_result"] |
| 481 |
|
| 482 |
if case["supports_value"]: |
| 483 |
if case["should_raise_error"]: |
| 484 |
essential_modules["orca.debug"].print_message.assert_called() |
| 485 |
else: |
| 486 |
essential_modules["orca.debug"].print_tokens.assert_called() |