diff --git a/tests/modules/test_brightness.py b/tests/modules/test_brightness.py index 9b0c105..0594fc3 100644 --- a/tests/modules/test_brightness.py +++ b/tests/modules/test_brightness.py @@ -7,7 +7,7 @@ import mock import bumblebee.input from bumblebee.input import I3BarInput from bumblebee.modules.brightness import Module -from tests.util import MockEngine, MockConfig, assertPopen +from tests.util import MockEngine, MockConfig, assertPopen, assertMouseEvent class TestBrightnessModule(unittest.TestCase): def setUp(self): @@ -28,31 +28,19 @@ class TestBrightnessModule(unittest.TestCase): @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%") + assertMouseEvent(mock_input, mock_output, mock_select, self.engine, + self.module, bumblebee.input.WHEEL_UP, + "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%") + assertMouseEvent(mock_input, mock_output, mock_select, self.engine, + self.module, bumblebee.input.WHEEL_DOWN, + "xbacklight -2%" + ) @mock.patch("select.select") @mock.patch("subprocess.Popen") @@ -60,13 +48,9 @@ class TestBrightnessModule(unittest.TestCase): 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%") + assertMouseEvent(mock_input, mock_output, mock_select, self.engine, + module, bumblebee.input.WHEEL_DOWN, + "xbacklight -10%" + ) + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/modules/test_cpu.py b/tests/modules/test_cpu.py index 52b7967..a00d2c2 100644 --- a/tests/modules/test_cpu.py +++ b/tests/modules/test_cpu.py @@ -7,7 +7,7 @@ import mock import bumblebee.input from bumblebee.input import I3BarInput from bumblebee.modules.cpu import Module -from tests.util import MockEngine, MockConfig, assertPopen +from tests.util import MockEngine, MockConfig, assertPopen, assertMouseEvent, assertStateContains class TestCPUModule(unittest.TestCase): def setUp(self): @@ -16,8 +16,6 @@ class TestCPUModule(unittest.TestCase): 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): @@ -28,31 +26,23 @@ class TestCPUModule(unittest.TestCase): @mock.patch("subprocess.Popen") @mock.patch("sys.stdin") def test_leftclick(self, mock_input, mock_output, mock_select): - mock_input.readline.return_value = json.dumps({ - "name": self.module.id, - "button": bumblebee.input.LEFT_MOUSE, - "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, "gnome-system-monitor") + assertMouseEvent(mock_input, mock_output, mock_select, self.engine, + self.module, bumblebee.input.LEFT_MOUSE, + "gnome-system-monitor" + ) @mock.patch("psutil.cpu_percent") def test_warning(self, mock_psutil): self.config.set("cpu.critical", "20") self.config.set("cpu.warning", "18") mock_psutil.return_value = 19.0 - self.module.update(self.module.widgets()) - self.assertEquals(self.module.widgets()[0].state(), ["warning"]) + assertStateContains(self, self.module, "warning") @mock.patch("psutil.cpu_percent") def test_critical(self, mock_psutil): self.config.set("cpu.critical", "20") self.config.set("cpu.warning", "19") mock_psutil.return_value = 21.0 - self.module.update(self.module.widgets()) - self.assertEquals(self.module.widgets()[0].state(), ["critical"]) + assertStateContains(self, self.module, "critical") # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/modules/test_disk.py b/tests/modules/test_disk.py index 4d3909c..77cf286 100644 --- a/tests/modules/test_disk.py +++ b/tests/modules/test_disk.py @@ -7,7 +7,7 @@ import mock import bumblebee.input from bumblebee.input import I3BarInput from bumblebee.modules.disk import Module -from tests.util import MockEngine, MockConfig, assertPopen +from tests.util import MockEngine, MockConfig, assertPopen, assertStateContains class MockVFS(object): def __init__(self, perc): @@ -23,8 +23,6 @@ class TestDiskModule(unittest.TestCase): self.config = MockConfig() self.config.set("disk.path", "somepath") self.module = Module(engine=self.engine, config={"config": self.config}) - for widget in self.module.widgets(): - widget.link_module(self.module) @mock.patch("select.select") @mock.patch("subprocess.Popen") @@ -46,16 +44,13 @@ class TestDiskModule(unittest.TestCase): self.config.set("disk.critical", "80") self.config.set("disk.warning", "70") mock_stat.return_value = MockVFS(75.0) - self.module.update(self.module.widgets()) - self.assertEquals(self.module.widgets()[0].state(), ["warning"]) + assertStateContains(self, self.module, "warning") @mock.patch("os.statvfs") def test_warning(self, mock_stat): self.config.set("disk.critical", "80") self.config.set("disk.warning", "70") mock_stat.return_value = MockVFS(85.0) - self.module.update(self.module.widgets()) - self.assertEquals(self.module.widgets()[0].state(), ["critical"]) - + assertStateContains(self, self.module, "critical") # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/util.py b/tests/util.py index fded9f2..4a0e16f 100644 --- a/tests/util.py +++ b/tests/util.py @@ -1,5 +1,6 @@ # pylint: disable=C0103,C0111,W0613 +import json import shlex import subprocess @@ -16,6 +17,24 @@ def assertPopen(output, cmd): stderr=subprocess.STDOUT ) +def assertStateContains(test, module, state): + for widget in module.widgets(): + widget.link_module(module) + module.update(module.widgets()) + test.assertTrue(state in module.widgets()[0].state()) + +def assertMouseEvent(mock_input, mock_output, mock_select, engine, module, button, cmd): + mock_input.readline.return_value = json.dumps({ + "name": module.id, + "button": button, + "instance": None + }) + mock_select.return_value = (1,2,3) + engine.input.start() + engine.input.stop() + mock_input.readline.assert_any_call() + assertPopen(mock_output, cmd) + class MockInput(object): def start(self): pass