test_backlight.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import pytest
  2. from acpi_backlight import Backlight
  3. # pylint: disable=protected-access
  4. # pylint: disable=redefined-outer-name; fixture
  5. def test_init_default():
  6. backlight = Backlight()
  7. assert backlight._acpi_dir_path == "/sys/class/backlight/intel_backlight"
  8. @pytest.mark.parametrize("name", ["intel_backlight", "other"])
  9. def test_init(name):
  10. backlight = Backlight(name=name)
  11. assert backlight._acpi_dir_path == "/sys/class/backlight/{}".format(name)
  12. @pytest.mark.parametrize(
  13. ("max_brightness", "brightness_absolute_str", "expected_brightness_relative"),
  14. (
  15. (100, "0", 0),
  16. (100, "100", 1),
  17. (100, "40", 0.4),
  18. (4096, "2048", 0.5),
  19. (4096, "3584", 0.875),
  20. ),
  21. )
  22. def test_brightness_relative_get(
  23. tmp_path, max_brightness, brightness_absolute_str, expected_brightness_relative
  24. ):
  25. backlight = Backlight()
  26. backlight._acpi_dir_path = str(tmp_path)
  27. tmp_path.joinpath("brightness").write_text(brightness_absolute_str)
  28. tmp_path.joinpath("max_brightness").write_text(str(max_brightness))
  29. assert backlight.brightness_relative == pytest.approx(expected_brightness_relative)
  30. @pytest.mark.parametrize(
  31. ("max_brightness", "brightness_relative", "expected_brightness_abs_str"),
  32. (
  33. (100, 0, "0"),
  34. (100, 1, "100"),
  35. (100, 0.4, "40"),
  36. (4096, 0.5, "2048"),
  37. (4096, 0.8, "3277"),
  38. (100, 1.1, "100"),
  39. (100, -0.1, "0"),
  40. ),
  41. )
  42. def test_brightness_relative_set(
  43. tmp_path, max_brightness, brightness_relative, expected_brightness_abs_str
  44. ):
  45. backlight = Backlight()
  46. backlight._acpi_dir_path = str(tmp_path)
  47. tmp_path.joinpath("max_brightness").write_text(str(max_brightness))
  48. backlight.brightness_relative = brightness_relative
  49. assert tmp_path.joinpath("brightness").read_text() == expected_brightness_abs_str