[tests] Add unit tests for brightness module

This commit is contained in:
Tobi-wan Kenobi 2017-03-05 11:24:30 +01:00
parent 463850eddc
commit 36848770a5
2 changed files with 85 additions and 0 deletions

View file

@ -12,6 +12,22 @@ import random
def rand(cnt): def rand(cnt):
return "".join(random.choice("abcdefghijklmnopqrstuvwxyz0123456789") for i in range(cnt)) return "".join(random.choice("abcdefghijklmnopqrstuvwxyz0123456789") for i in range(cnt))
def epoll_mock(module=""):
if len(module) > 0: module = "{}.".format(module)
stdin = mock.patch("{}sys.stdin".format(module))
select = mock.patch("{}select".format(module))
epoll = mock.Mock()
stdin_mock = stdin.start()
select_mock = select.start()
stdin_mock.fileno.return_value = 1
select_mock.epoll.return_value = epoll
epoll.poll.return_value = [(stdin_mock.fileno.return_value, 100)]
return stdin, select, stdin_mock, select_mock
def mouseEvent(stdin, button, inp, module=None, instance=None): def mouseEvent(stdin, button, inp, module=None, instance=None):
stdin.readline.return_value = json.dumps({ stdin.readline.return_value = json.dumps({
"name": module.id if module else rand(10), "name": module.id if module else rand(10),

View file

@ -0,0 +1,69 @@
# pylint: disable=C0103,C0111
import mock
import unittest
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
import tests.mocks as mocks
import bumblebee.input
from bumblebee.config import Config
from bumblebee.engine import Engine
from bumblebee.input import I3BarInput, WHEEL_UP, WHEEL_DOWN
from bumblebee.modules.brightness import Module
class TestBrightnessModule(unittest.TestCase):
def setUp(self):
self._stdout = mock.patch("sys.stdout", new_callable=StringIO)
self.stdout = self._stdout.start()
self._stdin, self._select, self.stdin, self.select = mocks.epoll_mock("bumblebee.input")
self.popen = mocks.MockPopen()
self.config = Config()
self.input = I3BarInput()
self.engine = mock.Mock()
self.engine.input = self.input
self.input.need_event = True
self.module = Module(engine=self.engine, config={ "config": self.config })
for widget in self.module.widgets():
widget.link_module(self.module)
self.anyWidget = widget
def tearDown(self):
self._stdout.stop()
self._stdin.stop()
self._select.stop()
self.popen.cleanup()
def test_format(self):
for widget in self.module.widgets():
self.assertEquals(len(widget.full_text()), len("100%"))
def test_wheel_up(self):
mocks.mouseEvent(stdin=self.stdin, button=WHEEL_UP, inp=self.input, module=self.module)
self.popen.assert_call("xbacklight +2%")
def test_wheel_down(self):
mocks.mouseEvent(stdin=self.stdin, button=WHEEL_DOWN, inp=self.input, module=self.module)
self.popen.assert_call("xbacklight -2%")
def test_custom_step(self):
self.config.set("brightness.step", "10")
module = Module(engine=self.engine, config={"config": self.config})
mocks.mouseEvent(stdin=self.stdin, button=WHEEL_DOWN, inp=self.input, module=module)
self.popen.assert_call("xbacklight -10%")
def test_update(self):
self.popen.mock.communicate.return_value = ("20.0", None)
self.module.update_all()
self.assertEquals(self.module.brightness(self.anyWidget), "020%")
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4