[tests/cmus] Add tests for cmus mouse interaction

see #23
This commit is contained in:
Tobi-wan Kenobi 2016-12-10 08:37:04 +01:00
parent b1ec41f905
commit 38a42e4a77
3 changed files with 38 additions and 3 deletions

View file

@ -39,6 +39,11 @@ class Module(object):
"""Return the widgets to draw for this module""" """Return the widgets to draw for this module"""
return self._widgets return self._widgets
def widget(self, name):
for widget in self._widgets:
if widget.name == name:
return widget
def update(self, widgets): def update(self, widgets):
"""By default, update() is a NOP""" """By default, update() is a NOP"""
pass pass

View file

@ -27,7 +27,6 @@ class I3BarInput(object):
"""Process incoming events from the i3bar""" """Process incoming events from the i3bar"""
def __init__(self): def __init__(self):
self.running = True self.running = True
self._thread = threading.Thread(target=read_input, args=(self,))
self._callbacks = {} self._callbacks = {}
self.clean_exit = False self.clean_exit = False
self.global_id = str(uuid.uuid4()) self.global_id = str(uuid.uuid4())
@ -36,17 +35,24 @@ class I3BarInput(object):
def start(self): def start(self):
"""Start asynchronous input processing""" """Start asynchronous input processing"""
self.has_event = False
self.running = True
self._thread = threading.Thread(target=read_input, args=(self,))
self._thread.start() self._thread.start()
def alive(self): def alive(self):
"""Check whether the input processing is still active""" """Check whether the input processing is still active"""
return self._thread.is_alive() return self._thread.is_alive()
def _wait(self):
while not self.has_event:
time.sleep(0.1)
self.has_event = False
def stop(self): def stop(self):
"""Stop asynchronous input processing""" """Stop asynchronous input processing"""
if self.need_event: if self.need_event:
while not self.has_event: self._wait()
time.sleep(0.1)
self.running = False self.running = False
self._thread.join() self._thread.join()
return self.clean_exit return self.clean_exit

View file

@ -12,6 +12,8 @@ from tests.util import MockEngine, MockConfig, assertPopen
class TestCmusModule(unittest.TestCase): class TestCmusModule(unittest.TestCase):
def setUp(self): def setUp(self):
self.engine = MockEngine() self.engine = MockEngine()
self.engine.input = I3BarInput()
self.engine.input.need_event = True
self.module = Module(engine=self.engine, config={"config": MockConfig()}) self.module = Module(engine=self.engine, config={"config": MockConfig()})
@mock.patch("subprocess.Popen") @mock.patch("subprocess.Popen")
@ -27,4 +29,26 @@ class TestCmusModule(unittest.TestCase):
def test_widgets(self): def test_widgets(self):
self.assertTrue(len(self.module.widgets()), 5) self.assertTrue(len(self.module.widgets()), 5)
@mock.patch("subprocess.Popen")
@mock.patch("sys.stdin")
def test_interaction(self, mock_input, mock_output):
events = [
{"widget": "cmus.shuffle", "action": "cmus-remote -S"},
{"widget": "cmus.repeat", "action": "cmus-remote -R"},
{"widget": "cmus.next", "action": "cmus-remote -n"},
{"widget": "cmus.prev", "action": "cmus-remote -r"},
{"widget": "cmus.main", "action": "cmus-remote -u"},
]
for event in events:
mock_input.readline.return_value = json.dumps({
"name": self.module.id,
"button": bumblebee.input.LEFT_MOUSE,
"instance": self.module.widget(event["widget"]).id
})
self.engine.input.start()
self.engine.input.stop()
mock_input.readline.assert_any_call()
assertPopen(mock_output, event["action"])
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4