From 9ce7739efbb339f7d955bb459d704bb9020ecd40 Mon Sep 17 00:00:00 2001 From: Tobi-wan Kenobi Date: Fri, 9 Dec 2016 22:28:04 +0100 Subject: [PATCH] [tests] Add specific tests for CPU module * Check that the left mouse button action works * Check that the format is OK see #23 --- tests/modules/test_cpu.py | 37 +++++++++++++++++++++++++ tests/test_i3barinput.py | 57 +++++++++++++++++++-------------------- tests/util.py | 10 +++++++ 3 files changed, 74 insertions(+), 30 deletions(-) create mode 100644 tests/modules/test_cpu.py diff --git a/tests/modules/test_cpu.py b/tests/modules/test_cpu.py new file mode 100644 index 0000000..4bce1e2 --- /dev/null +++ b/tests/modules/test_cpu.py @@ -0,0 +1,37 @@ +# pylint: disable=C0103,C0111 + +import json +import unittest +import mock + +import bumblebee.input +from bumblebee.input import I3BarInput +from bumblebee.modules.cpu import Module +from tests.util import MockEngine, assertPopen + +class TestCPUModule(unittest.TestCase): + def setUp(self): + self.engine = MockEngine() + self.engine.input = I3BarInput() + self.engine.input.need_event = True + self.module = Module(engine=self.engine, config={}) + + @mock.patch("sys.stdout") + def test_format(self, mock_output): + for widget in self.module.widgets(): + self.assertEquals(len(widget.full_text()), len("00.00%")) + + @mock.patch("subprocess.Popen") + @mock.patch("sys.stdin") + def test_leftclick(self, mock_input, mock_output): + mock_input.readline.return_value = json.dumps({ + "name": self.module.id, + "button": bumblebee.input.LEFT_MOUSE, + "instance": None + }) + self.engine.input.start() + self.engine.input.stop() + mock_input.readline.assert_any_call() + assertPopen(mock_output, "gnome-system-monitor") + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/test_i3barinput.py b/tests/test_i3barinput.py index 7841e1a..08fa036 100644 --- a/tests/test_i3barinput.py +++ b/tests/test_i3barinput.py @@ -7,12 +7,12 @@ import mock import bumblebee.input from bumblebee.input import I3BarInput -from tests.util import MockWidget, MockModule +from tests.util import MockWidget, MockModule, assertPopen class TestI3BarInput(unittest.TestCase): def setUp(self): - self.inp = I3BarInput() - self.inp.need_event = True + self.input = I3BarInput() + self.input.need_event = True self.anyModule = MockModule() self.anyWidget = MockWidget("test") self.anyModule.id = "test-module" @@ -24,16 +24,16 @@ class TestI3BarInput(unittest.TestCase): @mock.patch("sys.stdin") def test_basic_read_event(self, mock_input): mock_input.readline.return_value = "" - self.inp.start() - self.inp.stop() + self.input.start() + self.input.stop() mock_input.readline.assert_any_call() @mock.patch("sys.stdin") def test_ignore_invalid_data(self, mock_input): mock_input.readline.return_value = "garbage" - self.inp.start() - self.assertEquals(self.inp.alive(), True) - self.assertEquals(self.inp.stop(), True) + self.input.start() + self.assertEquals(self.input.alive(), True) + self.assertEquals(self.input.stop(), True) mock_input.readline.assert_any_call() @mock.patch("sys.stdin") @@ -43,9 +43,9 @@ class TestI3BarInput(unittest.TestCase): "instance": None, "button": None, }) - self.inp.start() - self.assertEquals(self.inp.alive(), True) - self.assertEquals(self.inp.stop(), True) + self.input.start() + self.assertEquals(self.input.alive(), True) + self.assertEquals(self.input.stop(), True) mock_input.readline.assert_any_call() @mock.patch("sys.stdin") @@ -55,9 +55,9 @@ class TestI3BarInput(unittest.TestCase): "instance": "someinstance", "button": bumblebee.input.LEFT_MOUSE, }) - self.inp.register_callback(None, button=1, cmd=self.callback) - self.inp.start() - self.assertEquals(self.inp.stop(), True) + self.input.register_callback(None, button=1, cmd=self.callback) + self.input.start() + self.assertEquals(self.input.stop(), True) mock_input.readline.assert_any_call() self.assertTrue(self._called > 0) @@ -68,9 +68,9 @@ class TestI3BarInput(unittest.TestCase): "instance": "someinstance", "button": bumblebee.input.RIGHT_MOUSE, }) - self.inp.register_callback(None, button=1, cmd=self.callback) - self.inp.start() - self.assertEquals(self.inp.stop(), True) + self.input.register_callback(None, button=1, cmd=self.callback) + self.input.start() + self.assertEquals(self.input.stop(), True) mock_input.readline.assert_any_call() self.assertTrue(self._called == 0) @@ -81,9 +81,9 @@ class TestI3BarInput(unittest.TestCase): "instance": None, "button": bumblebee.input.LEFT_MOUSE, }) - self.inp.register_callback(self.anyModule, button=1, cmd=self.callback) - self.inp.start() - self.assertEquals(self.inp.stop(), True) + self.input.register_callback(self.anyModule, button=1, cmd=self.callback) + self.input.start() + self.assertEquals(self.input.stop(), True) mock_input.readline.assert_any_call() self.assertTrue(self._called > 0) @@ -94,9 +94,9 @@ class TestI3BarInput(unittest.TestCase): "instance": self.anyWidget.id, "button": bumblebee.input.LEFT_MOUSE, }) - self.inp.register_callback(self.anyWidget, button=1, cmd=self.callback) - self.inp.start() - self.assertEquals(self.inp.stop(), True) + self.input.register_callback(self.anyWidget, button=1, cmd=self.callback) + self.input.start() + self.assertEquals(self.input.stop(), True) mock_input.readline.assert_any_call() self.assertTrue(self._called > 0) @@ -108,13 +108,10 @@ class TestI3BarInput(unittest.TestCase): "instance": self.anyWidget.id, "button": bumblebee.input.LEFT_MOUSE, }) - self.inp.register_callback(self.anyWidget, button=1, cmd="echo") - self.inp.start() - self.assertEquals(self.inp.stop(), True) + self.input.register_callback(self.anyWidget, button=1, cmd="echo") + self.input.start() + self.assertEquals(self.input.stop(), True) mock_input.readline.assert_any_call() - mock_output.assert_called_with(["echo"], - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT - ) + assertPopen(mock_output, "echo") # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/util.py b/tests/util.py index 1c244c8..7881c99 100644 --- a/tests/util.py +++ b/tests/util.py @@ -1,11 +1,21 @@ # pylint: disable=C0103,C0111,W0613 +import shlex +import subprocess + from bumblebee.output import Widget def assertWidgetAttributes(test, widget): test.assertTrue(isinstance(widget, Widget)) test.assertTrue(hasattr(widget, "full_text")) +def assertPopen(output, cmd): + res = shlex.split(cmd) + output.assert_called_with(res, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT + ) + class MockInput(object): def start(self): pass