test_backlight.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import pathlib
  2. import unittest.mock
  3. import pytest
  4. from acpi_backlight import Backlight, backlight_eval
  5. # pylint: disable=protected-access
  6. # pylint: disable=redefined-outer-name; fixture
  7. def test_init_default():
  8. backlight = Backlight()
  9. assert backlight._acpi_dir_path.as_posix() == "/sys/class/backlight/intel_backlight"
  10. @pytest.mark.parametrize("name", ["intel_backlight", "other"])
  11. def test_init(name):
  12. backlight = Backlight(name=name)
  13. assert backlight._acpi_dir_path.as_posix() == "/sys/class/backlight/" + name
  14. @pytest.mark.parametrize(
  15. ("max_brightness", "brightness_absolute_str", "expected_brightness_relative"),
  16. (
  17. (100, "0", 0),
  18. (100, "100", 1),
  19. (100, "40", 0.4),
  20. (4096, "2048", 0.5),
  21. (4096, "3584", 0.875),
  22. ),
  23. )
  24. def test_brightness_relative_get(
  25. tmp_path: pathlib.Path,
  26. max_brightness,
  27. brightness_absolute_str,
  28. expected_brightness_relative,
  29. ):
  30. backlight = Backlight()
  31. backlight._acpi_dir_path = tmp_path
  32. tmp_path.joinpath("brightness").write_text(brightness_absolute_str)
  33. tmp_path.joinpath("max_brightness").write_text(str(max_brightness))
  34. assert backlight.brightness_relative == pytest.approx(expected_brightness_relative)
  35. @pytest.mark.parametrize(
  36. ("max_brightness", "brightness_relative", "expected_brightness_abs_str"),
  37. (
  38. (100, 0, "0"),
  39. (100, 1, "100"),
  40. (100, 0.4, "40"),
  41. (4096, 0.5, "2048"),
  42. (4096, 0.8, "3277"),
  43. (100, 1.1, "100"),
  44. (100, -0.1, "0"),
  45. ),
  46. )
  47. def test_brightness_relative_set(
  48. tmp_path: pathlib.Path,
  49. max_brightness,
  50. brightness_relative,
  51. expected_brightness_abs_str,
  52. ):
  53. backlight = Backlight()
  54. backlight._acpi_dir_path = tmp_path
  55. tmp_path.joinpath("max_brightness").write_text(str(max_brightness))
  56. backlight.brightness_relative = brightness_relative
  57. assert tmp_path.joinpath("brightness").read_text() == expected_brightness_abs_str
  58. @pytest.mark.parametrize(
  59. ("current_brightness_relative", "expr_str", "expected_new_brightness_relative"),
  60. (
  61. (100, "1", 1),
  62. (100, "0", 0),
  63. (100, "0.42", 0.42),
  64. (100, "1/4", 0.25),
  65. (100, "b * 0.5", 50),
  66. (100, "b / 2", 50),
  67. (100, "b + 21", 121),
  68. (100, "b - 21", 79),
  69. ),
  70. )
  71. def test_backlight_eval(
  72. current_brightness_relative, expr_str, expected_new_brightness_relative
  73. ):
  74. with unittest.mock.patch(
  75. "acpi_backlight.Backlight.brightness_relative",
  76. new_callable=unittest.mock.PropertyMock,
  77. ) as property_mock:
  78. property_mock.return_value = current_brightness_relative
  79. backlight_eval(expr_str)
  80. # args and kwargs properties were added in python3.8
  81. assert all(not call[1] for call in property_mock.call_args_list)
  82. setter_calls_args = [call[0] for call in property_mock.call_args_list if call[0]]
  83. assert len(setter_calls_args) == 1, setter_calls_args
  84. (new_brightness_relative,) = setter_calls_args[0]
  85. assert new_brightness_relative == pytest.approx(expected_new_brightness_relative)