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

View file

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