From 003f7fd95715a873253f2d8ab48e1be57bf684ad Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sun, 1 Mar 2015 21:29:27 +0100 Subject: [PATCH] tests: Improve FakeTimer. --- qutebrowser/test/stubs.py | 50 +++++++++++++-- qutebrowser/test/test_stubs.py | 107 +++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+), 5 deletions(-) create mode 100644 qutebrowser/test/test_stubs.py diff --git a/qutebrowser/test/stubs.py b/qutebrowser/test/stubs.py index d022e1319..8b5419e6a 100644 --- a/qutebrowser/test/stubs.py +++ b/qutebrowser/test/stubs.py @@ -23,11 +23,10 @@ from unittest import mock -from PyQt5.QtCore import QPoint, QProcess +from PyQt5.QtCore import pyqtSignal, QPoint, QProcess, QObject from PyQt5.QtNetwork import QNetworkRequest from qutebrowser.config import configexc -from qutebrowser.utils import usertypes class ConfigStub: @@ -221,10 +220,51 @@ class FakeCommand: self.desc = desc -class FakeTimer(usertypes.Timer): +class FakeTimer(QObject): """Stub for a usertypes.Timer.""" + # pylint: disable=missing-docstring + + timeout_signal = pyqtSignal() + def __init__(self, parent=None, name=None): - super().__init__(parent, name) - self.blockSignals(True) + super().__init__(parent) + self.timeout = mock.Mock(spec=['connect', 'disconnect', 'emit']) + self.timeout.connect.side_effect = self.timeout_signal.connect + self.timeout.disconnect.side_effect = self.timeout_signal.disconnect + self.timeout.emit.side_effect = self._emit + self._started = False + self._singleshot = False + self._interval = 0 + self._name = name + + def __repr__(self): + return '<{} name={!r}>'.format(self.__class__.__name__, self._name) + + def _emit(self): + """Called when the timeout "signal" gets emitted.""" + if self._singleshot: + self._started = False + self.timeout_signal.emit() + + def setInterval(self, interval): + self._interval = interval + + def interval(self): + return self._interval + + def setSingleShot(self, singleshot): + self._singleshot = singleshot + + def singleShot(self): + return self._singleshot + + def start(self): + self._started = True + + def stop(self): + self._started = False + + def isActive(self): + return self._started diff --git a/qutebrowser/test/test_stubs.py b/qutebrowser/test/test_stubs.py new file mode 100644 index 000000000..476c43774 --- /dev/null +++ b/qutebrowser/test/test_stubs.py @@ -0,0 +1,107 @@ +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# Copyright 2015 Florian Bruhin (The Compiler) +# +# This file is part of qutebrowser. +# +# qutebrowser is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# qutebrowser is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with qutebrowser. If not, see . + + +"""Test test stubs.""" + +import unittest +from unittest import mock + +from qutebrowser.test import stubs + + +class TestFakeTimer(unittest.TestCase): + + """Test FakeTimer.""" + + def setUp(self): + self.timer = stubs.FakeTimer() + + def test_timeout(self): + """Test whether timeout calls the functions.""" + func = mock.Mock() + func2 = mock.Mock() + self.timer.timeout.connect(func) + self.timer.timeout.connect(func2) + self.assertFalse(func.called) + self.assertFalse(func2.called) + self.timer.timeout.emit() + func.assert_called_once_with() + func2.assert_called_once_with() + + def test_disconnect_all(self): + """Test disconnect without arguments.""" + func = mock.Mock() + self.timer.timeout.connect(func) + self.timer.timeout.disconnect() + self.timer.timeout.emit() + self.assertFalse(func.called) + + def test_disconnect_one(self): + """Test disconnect with a single argument.""" + func = mock.Mock() + self.timer.timeout.connect(func) + self.timer.timeout.disconnect(func) + self.timer.timeout.emit() + self.assertFalse(func.called) + + def test_disconnect_all_invalid(self): + """Test disconnecting with no connections.""" + with self.assertRaises(TypeError): + self.timer.timeout.disconnect() + + def test_disconnect_one_invalid(self): + """Test disconnecting with an invalid connection.""" + func1 = mock.Mock() + func2 = mock.Mock() + self.timer.timeout.connect(func1) + with self.assertRaises(TypeError): + self.timer.timeout.disconnect(func2) + self.assertFalse(func1.called) + self.assertFalse(func2.called) + self.timer.timeout.emit() + func1.assert_called_once_with() + + def test_singleshot(self): + """Test setting singleShot.""" + self.assertFalse(self.timer.singleShot()) + self.timer.setSingleShot(True) + self.assertTrue(self.timer.singleShot()) + self.timer.start() + self.assertTrue(self.timer.isActive()) + self.timer.timeout.emit() + self.assertFalse(self.timer.isActive()) + + def test_active(self): + """Test isActive.""" + self.assertFalse(self.timer.isActive()) + self.timer.start() + self.assertTrue(self.timer.isActive()) + self.timer.stop() + self.assertFalse(self.timer.isActive()) + + def test_interval(self): + """Test setting an interval.""" + self.assertEqual(self.timer.interval(), 0) + self.timer.setInterval(1000) + self.assertEqual(self.timer.interval(), 1000) + + +if __name__ == '__main__': + unittest.main()