test_backlight.py 3.0 KB

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