[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:
parent
e72c25b0bc
commit
9ce7739efb
3 changed files with 74 additions and 30 deletions
37
tests/modules/test_cpu.py
Normal file
37
tests/modules/test_cpu.py
Normal 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
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue