[tests] Refactor i3barinput

Use the assertMouseEvent helper in i3barinput.

see #23
This commit is contained in:
Tobi-wan Kenobi 2016-12-11 08:01:16 +01:00
parent 1a4cddb0b6
commit 4bd13c2f63
2 changed files with 22 additions and 71 deletions

View file

@ -7,7 +7,7 @@ import mock
import bumblebee.input
from bumblebee.input import I3BarInput
from tests.util import MockWidget, MockModule, assertPopen
from tests.util import MockWidget, MockModule, assertPopen, assertMouseEvent
class TestI3BarInput(unittest.TestCase):
def setUp(self):
@ -57,109 +57,59 @@ class TestI3BarInput(unittest.TestCase):
@mock.patch("select.select")
@mock.patch("sys.stdin")
def test_global_callback(self, mock_input, mock_select):
mock_select.return_value = (1,2,3)
mock_input.readline.return_value = json.dumps({
"name": "somename",
"instance": "someinstance",
"button": bumblebee.input.LEFT_MOUSE,
})
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()
assertMouseEvent(mock_input, None, mock_select, self, None,
bumblebee.input.LEFT_MOUSE, None, "someinstance")
self.assertTrue(self._called > 0)
@mock.patch("select.select")
@mock.patch("sys.stdin")
def test_remove_global_callback(self, mock_input, mock_select):
mock_select.return_value = (1,2,3)
mock_input.readline.return_value = json.dumps({
"name": "somename",
"instance": "someinstance",
"button": bumblebee.input.LEFT_MOUSE,
})
self.input.register_callback(None, button=1, cmd=self.callback)
self.input.deregister_callbacks(None)
self.input.start()
self.assertEquals(self.input.stop(), True)
mock_input.readline.assert_any_call()
assertMouseEvent(mock_input, None, mock_select, self, None,
bumblebee.input.LEFT_MOUSE, None, "someinstance")
self.assertTrue(self._called == 0)
@mock.patch("select.select")
@mock.patch("sys.stdin")
def test_global_callback_button_missmatch(self, mock_input, mock_select):
mock_select.return_value = (1,2,3)
mock_input.readline.return_value = json.dumps({
"name": "somename",
"instance": "someinstance",
"button": bumblebee.input.RIGHT_MOUSE,
})
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.input.register_callback(self.anyModule, button=1, cmd=self.callback)
assertMouseEvent(mock_input, None, mock_select, self, None,
bumblebee.input.RIGHT_MOUSE, None, "someinstance")
self.assertTrue(self._called == 0)
@mock.patch("select.select")
@mock.patch("sys.stdin")
def test_module_callback(self, mock_input, mock_select):
mock_select.return_value = (1,2,3)
mock_input.readline.return_value = json.dumps({
"name": self.anyModule.id,
"instance": None,
"button": bumblebee.input.LEFT_MOUSE,
})
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()
assertMouseEvent(mock_input, None, mock_select, self, self.anyModule,
bumblebee.input.LEFT_MOUSE, None)
self.assertTrue(self._called > 0)
@mock.patch("select.select")
@mock.patch("sys.stdin")
def test_remove_module_callback(self, mock_input, mock_select):
mock_select.return_value = (1,2,3)
mock_input.readline.return_value = json.dumps({
"name": self.anyModule.id,
"instance": None,
"button": bumblebee.input.LEFT_MOUSE,
})
self.input.register_callback(self.anyModule, button=1, cmd=self.callback)
self.input.deregister_callbacks(self.anyModule)
self.input.start()
self.assertEquals(self.input.stop(), True)
mock_input.readline.assert_any_call()
assertMouseEvent(mock_input, None, mock_select, self, None,
bumblebee.input.LEFT_MOUSE, None, self.anyWidget.id)
self.assertTrue(self._called == 0)
@mock.patch("select.select")
@mock.patch("sys.stdin")
def test_widget_callback(self, mock_input, mock_select):
mock_select.return_value = (1,2,3)
mock_input.readline.return_value = json.dumps({
"name": "test",
"instance": self.anyWidget.id,
"button": bumblebee.input.LEFT_MOUSE,
})
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()
assertMouseEvent(mock_input, None, mock_select, self, None,
bumblebee.input.LEFT_MOUSE, None, self.anyWidget.id)
self.assertTrue(self._called > 0)
@mock.patch("select.select")
@mock.patch("subprocess.Popen")
@mock.patch("sys.stdin")
def test_widget_cmd_callback(self, mock_input, mock_output, mock_select):
mock_select.return_value = (1,2,3)
mock_input.readline.return_value = json.dumps({
"name": "test",
"instance": self.anyWidget.id,
"button": bumblebee.input.LEFT_MOUSE,
})
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()
assertPopen(mock_output, "echo")
assertMouseEvent(mock_input, mock_output, mock_select, self, None,
bumblebee.input.LEFT_MOUSE, "echo", self.anyWidget.id)
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -23,16 +23,17 @@ def assertStateContains(test, module, state):
module.update(module.widgets())
test.assertTrue(state in module.widgets()[0].state())
def assertMouseEvent(mock_input, mock_output, mock_select, engine, module, button, cmd):
def assertMouseEvent(mock_input, mock_output, mock_select, engine, module, button, cmd, instance_id=None):
mock_input.readline.return_value = json.dumps({
"name": module.id,
"name": module.id if module else "test",
"button": button,
"instance": None
"instance": instance_id
})
mock_select.return_value = (1, 2, 3)
engine.input.start()
engine.input.stop()
mock_input.readline.assert_any_call()
if cmd:
assertPopen(mock_output, cmd)
class MockInput(object):