1
0

test_backlight.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import pathlib
  2. import unittest.mock
  3. import re
  4. import pytest
  5. from acpi_backlight import Backlight, backlight_eval
  6. # pylint: disable=protected-access
  7. # pylint: disable=redefined-outer-name; fixture
  8. def test_init_default():
  9. backlight = Backlight()
  10. assert backlight._acpi_dir_path.as_posix() == "/sys/class/backlight/intel_backlight"
  11. @pytest.mark.parametrize("name", ["intel_backlight", "other"])
  12. def test_init(name):
  13. backlight = Backlight(name=name)
  14. assert backlight._acpi_dir_path.as_posix() == "/sys/class/backlight/" + name
  15. def test__brightness_absolute_set_permission_denied() -> None:
  16. backlight = Backlight()
  17. with unittest.mock.patch(
  18. "pathlib.Path.open",
  19. side_effect=PermissionError(
  20. "[Errno 13] Permission denied:"
  21. " '/sys/class/backlight/intel_backlight/brightness'"
  22. ),
  23. ), pytest.raises(
  24. PermissionError,
  25. match=re.escape(
  26. "Insufficient permissions to set brightness of backlight."
  27. "\nConsider adding the following udev rules:"
  28. '\nACTION=="add", SUBSYSTEM=="backlight", KERNEL=="intel_backlight"'
  29. ', RUN+="/bin/chgrp video /sys$devpath/brightness"'
  30. '\nACTION=="add", SUBSYSTEM=="backlight", KERNEL=="intel_backlight"'
  31. ', RUN+="/bin/chmod g+w /sys$devpath/brightness"'
  32. ),
  33. ):
  34. backlight._brightness_absolute = 42
  35. @pytest.mark.parametrize(
  36. ("max_brightness", "brightness_absolute_str", "expected_brightness_relative"),
  37. (
  38. (100, "0", 0),
  39. (100, "100", 1),
  40. (100, "40", 0.4),
  41. (4096, "2048", 0.5),
  42. (4096, "3584", 0.875),
  43. ),
  44. )
  45. def test_brightness_relative_get(
  46. tmp_path: pathlib.Path,
  47. max_brightness,
  48. brightness_absolute_str,
  49. expected_brightness_relative,
  50. ):
  51. backlight = Backlight()
  52. backlight._acpi_dir_path = tmp_path
  53. tmp_path.joinpath("brightness").write_text(brightness_absolute_str)
  54. tmp_path.joinpath("max_brightness").write_text(str(max_brightness))
  55. assert backlight.brightness_relative == pytest.approx(expected_brightness_relative)
  56. @pytest.mark.parametrize(
  57. ("max_brightness", "brightness_relative", "expected_brightness_abs_str"),
  58. (
  59. (100, 0, "0"),
  60. (100, 1, "100"),
  61. (100, 0.4, "40"),
  62. (4096, 0.5, "2048"),
  63. (4096, 0.8, "3277"),
  64. (100, 1.1, "100"),
  65. (100, -0.1, "0"),
  66. ),
  67. )
  68. def test_brightness_relative_set(
  69. tmp_path: pathlib.Path,
  70. max_brightness,
  71. brightness_relative,
  72. expected_brightness_abs_str,
  73. ):
  74. backlight = Backlight()
  75. backlight._acpi_dir_path = tmp_path
  76. tmp_path.joinpath("max_brightness").write_text(str(max_brightness))
  77. backlight.brightness_relative = brightness_relative
  78. assert tmp_path.joinpath("brightness").read_text() == expected_brightness_abs_str
  79. @pytest.mark.parametrize(
  80. ("current_brightness_relative", "expr_str", "expected_new_brightness_relative"),
  81. (
  82. (100, "1", 1),
  83. (100, "0", 0),
  84. (100, "0.42", 0.42),
  85. (100, "1/4", 0.25),
  86. (100, "b * 0.5", 50),
  87. (100, "b / 2", 50),
  88. (100, "b + 21", 121),
  89. (100, "b - 21", 79),
  90. ),
  91. )
  92. def test_backlight_eval(
  93. current_brightness_relative, expr_str, expected_new_brightness_relative
  94. ):
  95. with unittest.mock.patch(
  96. "acpi_backlight.Backlight.brightness_relative",
  97. new_callable=unittest.mock.PropertyMock,
  98. ) as property_mock:
  99. property_mock.return_value = current_brightness_relative
  100. backlight_eval(expr_str)
  101. # args and kwargs properties were added in python3.8
  102. assert all(not call[1] for call in property_mock.call_args_list)
  103. setter_calls_args = [call[0] for call in property_mock.call_args_list if call[0]]
  104. assert len(setter_calls_args) == 1, setter_calls_args
  105. (new_brightness_relative,) = setter_calls_args[0]
  106. assert new_brightness_relative == pytest.approx(expected_new_brightness_relative)