2016-12-09 19:29:16 +01:00
|
|
|
# pylint: disable=C0103,C0111
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import json
|
|
|
|
import subprocess
|
|
|
|
import mock
|
|
|
|
|
|
|
|
import bumblebee.input
|
|
|
|
from bumblebee.input import I3BarInput
|
2016-12-17 07:43:38 +01:00
|
|
|
from tests.util import MockWidget, MockModule, assertPopen, assertMouseEvent, MockEpoll
|
2016-12-09 19:29:16 +01:00
|
|
|
|
|
|
|
class TestI3BarInput(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2016-12-09 22:28:04 +01:00
|
|
|
self.input = I3BarInput()
|
|
|
|
self.input.need_event = True
|
2016-12-09 19:29:16 +01:00
|
|
|
self.anyModule = MockModule()
|
|
|
|
self.anyWidget = MockWidget("test")
|
|
|
|
self.anyModule.id = "test-module"
|
|
|
|
self._called = 0
|
|
|
|
|
|
|
|
def callback(self, event):
|
|
|
|
self._called += 1
|
|
|
|
|
2016-12-17 07:43:38 +01:00
|
|
|
@mock.patch("select.epoll")
|
2016-12-09 19:29:16 +01:00
|
|
|
@mock.patch("sys.stdin")
|
2016-12-10 13:45:54 +01:00
|
|
|
def test_basic_read_event(self, mock_input, mock_select):
|
2016-12-17 07:43:38 +01:00
|
|
|
mock_input.readline.return_value = "somedata"
|
|
|
|
mock_input.fileno.return_value = 1
|
|
|
|
mock_select.return_value = MockEpoll()
|
2016-12-09 22:28:04 +01:00
|
|
|
self.input.start()
|
|
|
|
self.input.stop()
|
2016-12-09 19:29:16 +01:00
|
|
|
mock_input.readline.assert_any_call()
|
|
|
|
|
2016-12-17 07:43:38 +01:00
|
|
|
@mock.patch("select.epoll")
|
2016-12-09 19:29:16 +01:00
|
|
|
@mock.patch("sys.stdin")
|
2016-12-10 13:45:54 +01:00
|
|
|
def test_ignore_invalid_data(self, mock_input, mock_select):
|
2016-12-17 07:43:38 +01:00
|
|
|
mock_select.return_value = MockEpoll()
|
2016-12-09 19:29:16 +01:00
|
|
|
mock_input.readline.return_value = "garbage"
|
2016-12-09 22:28:04 +01:00
|
|
|
self.input.start()
|
|
|
|
self.assertEquals(self.input.alive(), True)
|
|
|
|
self.assertEquals(self.input.stop(), True)
|
2016-12-09 19:29:16 +01:00
|
|
|
mock_input.readline.assert_any_call()
|
|
|
|
|
2016-12-17 07:43:38 +01:00
|
|
|
@mock.patch("select.epoll")
|
2016-12-09 19:29:16 +01:00
|
|
|
@mock.patch("sys.stdin")
|
2016-12-10 13:45:54 +01:00
|
|
|
def test_ignore_invalid_event(self, mock_input, mock_select):
|
2016-12-17 07:43:38 +01:00
|
|
|
mock_select.return_value = MockEpoll()
|
2016-12-09 19:29:16 +01:00
|
|
|
mock_input.readline.return_value = json.dumps({
|
|
|
|
"name": None,
|
|
|
|
"instance": None,
|
2016-12-11 13:43:34 +01:00
|
|
|
"button": 1,
|
|
|
|
})
|
|
|
|
self.input.start()
|
|
|
|
self.assertEquals(self.input.alive(), True)
|
|
|
|
self.assertEquals(self.input.stop(), True)
|
|
|
|
mock_input.readline.assert_any_call()
|
|
|
|
|
2016-12-17 07:43:38 +01:00
|
|
|
@mock.patch("select.epoll")
|
2016-12-11 13:43:34 +01:00
|
|
|
@mock.patch("sys.stdin")
|
|
|
|
def test_ignore_partial_event(self, mock_input, mock_select):
|
2016-12-17 07:43:38 +01:00
|
|
|
mock_select.return_value = MockEpoll()
|
2016-12-11 13:43:34 +01:00
|
|
|
self.input.register_callback(None, button=1, cmd=self.callback)
|
|
|
|
mock_input.readline.return_value = json.dumps({
|
|
|
|
"button": 1,
|
2016-12-09 19:29:16 +01:00
|
|
|
})
|
2016-12-09 22:28:04 +01:00
|
|
|
self.input.start()
|
|
|
|
self.assertEquals(self.input.alive(), True)
|
|
|
|
self.assertEquals(self.input.stop(), True)
|
2016-12-09 19:29:16 +01:00
|
|
|
mock_input.readline.assert_any_call()
|
|
|
|
|
2016-12-17 07:43:38 +01:00
|
|
|
@mock.patch("select.epoll")
|
2016-12-09 19:29:16 +01:00
|
|
|
@mock.patch("sys.stdin")
|
2016-12-10 13:45:54 +01:00
|
|
|
def test_global_callback(self, mock_input, mock_select):
|
2016-12-09 22:28:04 +01:00
|
|
|
self.input.register_callback(None, button=1, cmd=self.callback)
|
2016-12-11 08:01:16 +01:00
|
|
|
assertMouseEvent(mock_input, None, mock_select, self, None,
|
|
|
|
bumblebee.input.LEFT_MOUSE, None, "someinstance")
|
2016-12-09 19:29:16 +01:00
|
|
|
self.assertTrue(self._called > 0)
|
|
|
|
|
2016-12-17 07:43:38 +01:00
|
|
|
@mock.patch("select.epoll")
|
2016-12-10 10:26:07 +01:00
|
|
|
@mock.patch("sys.stdin")
|
2016-12-10 13:45:54 +01:00
|
|
|
def test_remove_global_callback(self, mock_input, mock_select):
|
2016-12-10 10:26:07 +01:00
|
|
|
self.input.register_callback(None, button=1, cmd=self.callback)
|
|
|
|
self.input.deregister_callbacks(None)
|
2016-12-11 08:01:16 +01:00
|
|
|
assertMouseEvent(mock_input, None, mock_select, self, None,
|
|
|
|
bumblebee.input.LEFT_MOUSE, None, "someinstance")
|
2016-12-10 10:26:07 +01:00
|
|
|
self.assertTrue(self._called == 0)
|
|
|
|
|
2016-12-17 07:43:38 +01:00
|
|
|
@mock.patch("select.epoll")
|
2016-12-09 19:29:16 +01:00
|
|
|
@mock.patch("sys.stdin")
|
2016-12-10 13:45:54 +01:00
|
|
|
def test_global_callback_button_missmatch(self, mock_input, mock_select):
|
2016-12-11 08:01:16 +01:00
|
|
|
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")
|
2016-12-09 19:29:16 +01:00
|
|
|
self.assertTrue(self._called == 0)
|
|
|
|
|
2016-12-17 07:43:38 +01:00
|
|
|
@mock.patch("select.epoll")
|
2016-12-09 19:29:16 +01:00
|
|
|
@mock.patch("sys.stdin")
|
2016-12-10 13:45:54 +01:00
|
|
|
def test_module_callback(self, mock_input, mock_select):
|
2016-12-09 22:28:04 +01:00
|
|
|
self.input.register_callback(self.anyModule, button=1, cmd=self.callback)
|
2016-12-11 08:01:16 +01:00
|
|
|
assertMouseEvent(mock_input, None, mock_select, self, self.anyModule,
|
|
|
|
bumblebee.input.LEFT_MOUSE, None)
|
2016-12-09 19:29:16 +01:00
|
|
|
self.assertTrue(self._called > 0)
|
|
|
|
|
2016-12-17 07:43:38 +01:00
|
|
|
@mock.patch("select.epoll")
|
2016-12-10 10:26:07 +01:00
|
|
|
@mock.patch("sys.stdin")
|
2016-12-10 13:45:54 +01:00
|
|
|
def test_remove_module_callback(self, mock_input, mock_select):
|
2016-12-10 10:26:07 +01:00
|
|
|
self.input.register_callback(self.anyModule, button=1, cmd=self.callback)
|
|
|
|
self.input.deregister_callbacks(self.anyModule)
|
2016-12-11 08:01:16 +01:00
|
|
|
assertMouseEvent(mock_input, None, mock_select, self, None,
|
|
|
|
bumblebee.input.LEFT_MOUSE, None, self.anyWidget.id)
|
2016-12-10 10:26:07 +01:00
|
|
|
self.assertTrue(self._called == 0)
|
|
|
|
|
2016-12-17 07:43:38 +01:00
|
|
|
@mock.patch("select.epoll")
|
2016-12-09 19:29:16 +01:00
|
|
|
@mock.patch("sys.stdin")
|
2016-12-10 13:45:54 +01:00
|
|
|
def test_widget_callback(self, mock_input, mock_select):
|
2016-12-09 22:28:04 +01:00
|
|
|
self.input.register_callback(self.anyWidget, button=1, cmd=self.callback)
|
2016-12-11 08:01:16 +01:00
|
|
|
assertMouseEvent(mock_input, None, mock_select, self, None,
|
|
|
|
bumblebee.input.LEFT_MOUSE, None, self.anyWidget.id)
|
2016-12-09 19:29:16 +01:00
|
|
|
self.assertTrue(self._called > 0)
|
|
|
|
|
2016-12-17 07:43:38 +01:00
|
|
|
@mock.patch("select.epoll")
|
2016-12-09 19:29:16 +01:00
|
|
|
@mock.patch("subprocess.Popen")
|
|
|
|
@mock.patch("sys.stdin")
|
2016-12-10 13:45:54 +01:00
|
|
|
def test_widget_cmd_callback(self, mock_input, mock_output, mock_select):
|
2016-12-09 22:28:04 +01:00
|
|
|
self.input.register_callback(self.anyWidget, button=1, cmd="echo")
|
2016-12-11 08:01:16 +01:00
|
|
|
assertMouseEvent(mock_input, mock_output, mock_select, self, None,
|
|
|
|
bumblebee.input.LEFT_MOUSE, "echo", self.anyWidget.id)
|
2016-12-09 19:29:16 +01:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|