From 1b8385b33fa45fb1e0a4fe606c4e65af67c6f48e Mon Sep 17 00:00:00 2001 From: Tobi-wan Kenobi Date: Sat, 10 Dec 2016 14:31:18 +0100 Subject: [PATCH] [modules/brighness] Re-enable brightness module Add a module for reporting, increasing and decreasing the brightness of a display. see #23 --- bumblebee/modules/brightness.py | 33 +++++++++++++++ tests/modules/test_brightness.py | 72 ++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 bumblebee/modules/brightness.py create mode 100644 tests/modules/test_brightness.py diff --git a/bumblebee/modules/brightness.py b/bumblebee/modules/brightness.py new file mode 100644 index 0000000..56b5314 --- /dev/null +++ b/bumblebee/modules/brightness.py @@ -0,0 +1,33 @@ +# pylint: disable=C0111,R0903 + +"""Displays the brightness of a display + +Parameters: + * brightness.step: The amount of increase/decrease on scroll in % (defaults to 2) +""" + +import bumblebee.input +import bumblebee.output +import bumblebee.engine + +class Module(bumblebee.engine.Module): + def __init__(self, engine, config): + super(Module, self).__init__(engine, config, + bumblebee.output.Widget(full_text=self.brightness) + ) + self._brightness = 0 + + step = self.parameter("step", 2) + + engine.input.register_callback(self, button=bumblebee.input.WHEEL_UP, + cmd="xbacklight +{}%".format(step)) + engine.input.register_callback(self, button=bumblebee.input.WHEEL_DOWN, + cmd="xbacklight -{}%".format(step)) + + def brightness(self): + return "{:03.0f}%".format(self._brightness) + + def update(self, widgets): + self._brightness = float(bumblebee.util.execute("xbacklight -get")) + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/modules/test_brightness.py b/tests/modules/test_brightness.py new file mode 100644 index 0000000..9b0c105 --- /dev/null +++ b/tests/modules/test_brightness.py @@ -0,0 +1,72 @@ +# pylint: disable=C0103,C0111 + +import json +import unittest +import mock + +import bumblebee.input +from bumblebee.input import I3BarInput +from bumblebee.modules.brightness import Module +from tests.util import MockEngine, MockConfig, assertPopen + +class TestBrightnessModule(unittest.TestCase): + def setUp(self): + self.engine = MockEngine() + self.engine.input = I3BarInput() + self.engine.input.need_event = True + self.config = MockConfig() + self.module = Module(engine=self.engine, config={ "config": self.config }) + for widget in self.module.widgets(): + widget.link_module(self.module) + + @mock.patch("sys.stdout") + def test_format(self, mock_output): + for widget in self.module.widgets(): + self.assertEquals(len(widget.full_text()), len("100%")) + + @mock.patch("select.select") + @mock.patch("subprocess.Popen") + @mock.patch("sys.stdin") + def test_wheel_up(self, mock_input, mock_output, mock_select): + mock_input.readline.return_value = json.dumps({ + "name": self.module.id, + "button": bumblebee.input.WHEEL_UP, + "instance": None + }) + mock_select.return_value = (1,2,3) + self.engine.input.start() + self.engine.input.stop() + mock_input.readline.assert_any_call() + assertPopen(mock_output, "xbacklight +2%") + + @mock.patch("select.select") + @mock.patch("subprocess.Popen") + @mock.patch("sys.stdin") + def test_wheel_down(self, mock_input, mock_output, mock_select): + mock_input.readline.return_value = json.dumps({ + "name": self.module.id, + "button": bumblebee.input.WHEEL_DOWN, + "instance": None + }) + mock_select.return_value = (1,2,3) + self.engine.input.start() + self.engine.input.stop() + mock_input.readline.assert_any_call() + assertPopen(mock_output, "xbacklight -2%") + + @mock.patch("select.select") + @mock.patch("subprocess.Popen") + @mock.patch("sys.stdin") + def test_custom_step(self, mock_input, mock_output, mock_select): + self.config.set("brightness.step", "10") + module = Module(engine=self.engine, config={ "config": self.config }) + mock_input.readline.return_value = json.dumps({ + "name": module.id, + "button": bumblebee.input.WHEEL_DOWN, + "instance": None + }) + mock_select.return_value = (1,2,3) + self.engine.input.start() + self.engine.input.stop() + mock_input.readline.assert_any_call() + assertPopen(mock_output, "xbacklight -10%")