[tests/i3bar-input] Add input tests for i3bar protocol

Also, replaced the MockModule with a generic mock object.
This commit is contained in:
Tobi-wan Kenobi 2017-03-05 08:35:15 +01:00
parent cdbddcfff7
commit 70f138b97b
3 changed files with 37 additions and 15 deletions

View file

@ -1,14 +1,31 @@
# pylint: disable=C0103,C0111 # pylint: disable=C0103,C0111
import mock import mock
import json
import shlex import shlex
import subprocess import subprocess
from bumblebee.output import Widget from bumblebee.output import Widget
import random, string
def rand(cnt):
return "".join(random.choice(string.lowercase) for i in range(cnt))
def mouseEvent(stdin, button, inp, module=None, instance=None):
stdin.readline.return_value = json.dumps({
"name": module.id if module else rand(10),
"button": button,
"instance": instance
})
inp.start()
inp.stop()
stdin.readline.assert_any_call()
class MockPopen(object): class MockPopen(object):
def __init__(self, module): def __init__(self, module=""):
self._patch = mock.patch("{}.subprocess.Popen".format(module)) if len(module) > 0: module = "{}.".format(module)
self._patch = mock.patch("{}subprocess.Popen".format(module))
self._popen = self._patch.start() self._popen = self._patch.start()
self.mock = mock.Mock() self.mock = mock.Mock()
# for a nicer, more uniform interface # for a nicer, more uniform interface
@ -72,18 +89,16 @@ class MockEngine(object):
class MockWidget(Widget): class MockWidget(Widget):
def __init__(self, text): def __init__(self, text):
super(MockWidget, self).__init__(text) super(MockWidget, self).__init__(text)
self._text = text
self.module = None self.module = None
self.attr_state = ["state-default"] self.attr_state = ["state-default"]
self.id = "none" self.id = rand(10)
def state(self): self.full_text(text)
return self.attr_state
# def state(self):
# return self.attr_state
def update(self, widgets): def update(self, widgets):
pass pass
def full_text(self):
return self._text
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -25,8 +25,10 @@ class TestI3BarInput(unittest.TestCase):
epoll.poll.return_value = [(self.stdin.fileno.return_value, 2)] epoll.poll.return_value = [(self.stdin.fileno.return_value, 2)]
self.anyModule = mocks.MockModule() self.anyModule = mock.Mock()
self.anotherModule = mocks.MockModule() self.anyModule.id = mocks.rand(10)
self.anotherModule = mock.Mock()
self.anotherModule.id = mocks.rand(10)
self.anyWidget = mocks.MockWidget("some-widget") self.anyWidget = mocks.MockWidget("some-widget")
self.anotherWidget = mocks.MockWidget("another-widget") self.anotherWidget = mocks.MockWidget("another-widget")
self.anyData = self.invalidData = "any data" self.anyData = self.invalidData = "any data"

View file

@ -1,5 +1,6 @@
# pylint: disable=C0103,C0111,W0703 # pylint: disable=C0103,C0111,W0703
import mock
import unittest import unittest
from bumblebee.theme import Theme from bumblebee.theme import Theme
from bumblebee.error import ThemeLoadError from bumblebee.error import ThemeLoadError
@ -14,9 +15,15 @@ class TestTheme(unittest.TestCase):
self.themedWidget = MockWidget("bla") self.themedWidget = MockWidget("bla")
self.theme = Theme(self.validThemeName) self.theme = Theme(self.validThemeName)
self.cycleTheme = Theme("test_cycle") self.cycleTheme = Theme("test_cycle")
self.anyModule = mock.Mock()
self.anyWidget = MockWidget("bla") self.anyWidget = MockWidget("bla")
self.anotherWidget = MockWidget("blub") self.anotherWidget = MockWidget("blub")
self.anyModule.state.return_value = "state-default"
self.anyWidget.link_module(self.anyModule)
self.themedWidget.link_module(self.anyModule)
data = self.theme.data() data = self.theme.data()
self.widgetTheme = "test-widget" self.widgetTheme = "test-widget"
self.themedWidget.module = self.widgetTheme self.themedWidget.module = self.widgetTheme
@ -103,11 +110,9 @@ class TestTheme(unittest.TestCase):
self.assertEquals(theme.fg(self.anyWidget), data["defaults"]["fg"]) self.assertEquals(theme.fg(self.anyWidget), data["defaults"]["fg"])
self.assertEquals(theme.bg(self.anyWidget), data["defaults"]["bg"]) self.assertEquals(theme.bg(self.anyWidget), data["defaults"]["bg"])
self.anyWidget.attr_state = ["critical"] self.anyModule.state.return_value = "critical"
self.assertEquals(theme.fg(self.anyWidget), data["defaults"]["critical"]["fg"]) self.assertEquals(theme.fg(self.anyWidget), data["defaults"]["critical"]["fg"])
self.assertEquals(theme.bg(self.anyWidget), data["defaults"]["critical"]["bg"]) self.assertEquals(theme.bg(self.anyWidget), data["defaults"]["critical"]["bg"])
self.themedWidget.attr_state = ["critical"]
self.assertEquals(theme.fg(self.themedWidget), data[self.widgetTheme]["critical"]["fg"]) self.assertEquals(theme.fg(self.themedWidget), data[self.widgetTheme]["critical"]["fg"])
# if elements are missing in the state theme, they are taken from the # if elements are missing in the state theme, they are taken from the
# widget theme instead (i.e. no fallback to a more general state theme) # widget theme instead (i.e. no fallback to a more general state theme)
@ -119,7 +124,7 @@ class TestTheme(unittest.TestCase):
def test_list(self): def test_list(self):
theme = self.theme theme = self.theme
data = theme.data()[self.widgetTheme]["cycle-test"]["fg"] data = theme.data()[self.widgetTheme]["cycle-test"]["fg"]
self.themedWidget.attr_state = ["cycle-test"] self.anyModule.state.return_value = "cycle-test"
self.assertTrue(len(data) > 1) self.assertTrue(len(data) > 1)
for idx in range(0, len(data)): for idx in range(0, len(data)):