[tests] Add specific tests for CPU module

* Check that the left mouse button action works
* Check that the format is OK

see #23
This commit is contained in:
Tobi-wan Kenobi 2016-12-09 22:28:04 +01:00
parent e72c25b0bc
commit 9ce7739efb
3 changed files with 74 additions and 30 deletions

37
tests/modules/test_cpu.py Normal file
View file

@ -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

View file

@ -7,12 +7,12 @@ import mock
import bumblebee.input import bumblebee.input
from bumblebee.input import I3BarInput from bumblebee.input import I3BarInput
from tests.util import MockWidget, MockModule from tests.util import MockWidget, MockModule, assertPopen
class TestI3BarInput(unittest.TestCase): class TestI3BarInput(unittest.TestCase):
def setUp(self): def setUp(self):
self.inp = I3BarInput() self.input = I3BarInput()
self.inp.need_event = True self.input.need_event = True
self.anyModule = MockModule() self.anyModule = MockModule()
self.anyWidget = MockWidget("test") self.anyWidget = MockWidget("test")
self.anyModule.id = "test-module" self.anyModule.id = "test-module"
@ -24,16 +24,16 @@ class TestI3BarInput(unittest.TestCase):
@mock.patch("sys.stdin") @mock.patch("sys.stdin")
def test_basic_read_event(self, mock_input): def test_basic_read_event(self, mock_input):
mock_input.readline.return_value = "" mock_input.readline.return_value = ""
self.inp.start() self.input.start()
self.inp.stop() self.input.stop()
mock_input.readline.assert_any_call() mock_input.readline.assert_any_call()
@mock.patch("sys.stdin") @mock.patch("sys.stdin")
def test_ignore_invalid_data(self, mock_input): def test_ignore_invalid_data(self, mock_input):
mock_input.readline.return_value = "garbage" mock_input.readline.return_value = "garbage"
self.inp.start() self.input.start()
self.assertEquals(self.inp.alive(), True) self.assertEquals(self.input.alive(), True)
self.assertEquals(self.inp.stop(), True) self.assertEquals(self.input.stop(), True)
mock_input.readline.assert_any_call() mock_input.readline.assert_any_call()
@mock.patch("sys.stdin") @mock.patch("sys.stdin")
@ -43,9 +43,9 @@ class TestI3BarInput(unittest.TestCase):
"instance": None, "instance": None,
"button": None, "button": None,
}) })
self.inp.start() self.input.start()
self.assertEquals(self.inp.alive(), True) self.assertEquals(self.input.alive(), True)
self.assertEquals(self.inp.stop(), True) self.assertEquals(self.input.stop(), True)
mock_input.readline.assert_any_call() mock_input.readline.assert_any_call()
@mock.patch("sys.stdin") @mock.patch("sys.stdin")
@ -55,9 +55,9 @@ class TestI3BarInput(unittest.TestCase):
"instance": "someinstance", "instance": "someinstance",
"button": bumblebee.input.LEFT_MOUSE, "button": bumblebee.input.LEFT_MOUSE,
}) })
self.inp.register_callback(None, button=1, cmd=self.callback) self.input.register_callback(None, button=1, cmd=self.callback)
self.inp.start() self.input.start()
self.assertEquals(self.inp.stop(), True) self.assertEquals(self.input.stop(), True)
mock_input.readline.assert_any_call() mock_input.readline.assert_any_call()
self.assertTrue(self._called > 0) self.assertTrue(self._called > 0)
@ -68,9 +68,9 @@ class TestI3BarInput(unittest.TestCase):
"instance": "someinstance", "instance": "someinstance",
"button": bumblebee.input.RIGHT_MOUSE, "button": bumblebee.input.RIGHT_MOUSE,
}) })
self.inp.register_callback(None, button=1, cmd=self.callback) self.input.register_callback(None, button=1, cmd=self.callback)
self.inp.start() self.input.start()
self.assertEquals(self.inp.stop(), True) self.assertEquals(self.input.stop(), True)
mock_input.readline.assert_any_call() mock_input.readline.assert_any_call()
self.assertTrue(self._called == 0) self.assertTrue(self._called == 0)
@ -81,9 +81,9 @@ class TestI3BarInput(unittest.TestCase):
"instance": None, "instance": None,
"button": bumblebee.input.LEFT_MOUSE, "button": bumblebee.input.LEFT_MOUSE,
}) })
self.inp.register_callback(self.anyModule, button=1, cmd=self.callback) self.input.register_callback(self.anyModule, button=1, cmd=self.callback)
self.inp.start() self.input.start()
self.assertEquals(self.inp.stop(), True) self.assertEquals(self.input.stop(), True)
mock_input.readline.assert_any_call() mock_input.readline.assert_any_call()
self.assertTrue(self._called > 0) self.assertTrue(self._called > 0)
@ -94,9 +94,9 @@ class TestI3BarInput(unittest.TestCase):
"instance": self.anyWidget.id, "instance": self.anyWidget.id,
"button": bumblebee.input.LEFT_MOUSE, "button": bumblebee.input.LEFT_MOUSE,
}) })
self.inp.register_callback(self.anyWidget, button=1, cmd=self.callback) self.input.register_callback(self.anyWidget, button=1, cmd=self.callback)
self.inp.start() self.input.start()
self.assertEquals(self.inp.stop(), True) self.assertEquals(self.input.stop(), True)
mock_input.readline.assert_any_call() mock_input.readline.assert_any_call()
self.assertTrue(self._called > 0) self.assertTrue(self._called > 0)
@ -108,13 +108,10 @@ class TestI3BarInput(unittest.TestCase):
"instance": self.anyWidget.id, "instance": self.anyWidget.id,
"button": bumblebee.input.LEFT_MOUSE, "button": bumblebee.input.LEFT_MOUSE,
}) })
self.inp.register_callback(self.anyWidget, button=1, cmd="echo") self.input.register_callback(self.anyWidget, button=1, cmd="echo")
self.inp.start() self.input.start()
self.assertEquals(self.inp.stop(), True) self.assertEquals(self.input.stop(), True)
mock_input.readline.assert_any_call() mock_input.readline.assert_any_call()
mock_output.assert_called_with(["echo"], assertPopen(mock_output, "echo")
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT
)
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -1,11 +1,21 @@
# pylint: disable=C0103,C0111,W0613 # pylint: disable=C0103,C0111,W0613
import shlex
import subprocess
from bumblebee.output import Widget from bumblebee.output import Widget
def assertWidgetAttributes(test, widget): def assertWidgetAttributes(test, widget):
test.assertTrue(isinstance(widget, Widget)) test.assertTrue(isinstance(widget, Widget))
test.assertTrue(hasattr(widget, "full_text")) 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): class MockInput(object):
def start(self): def start(self):
pass pass