From 077ecedfc525dd1f164d2d4450fdcc7b4fdacd4a Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Mon, 18 Jun 2018 11:53:02 +1000 Subject: [PATCH] Unit tests for QgsMessageLog --- .../core/auto_generated/qgsmessagelog.sip.in | 3 +- src/core/qgsmessagelog.h | 3 +- tests/src/python/CMakeLists.txt | 1 + tests/src/python/test_qgsmessagelog.py | 104 ++++++++++++++++++ 4 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 tests/src/python/test_qgsmessagelog.py diff --git a/python/core/auto_generated/qgsmessagelog.sip.in b/python/core/auto_generated/qgsmessagelog.sip.in index 9ceb627d88a..3792bbe14ca 100644 --- a/python/core/auto_generated/qgsmessagelog.sip.in +++ b/python/core/auto_generated/qgsmessagelog.sip.in @@ -56,7 +56,8 @@ value for the message. void messageReceived( bool received ); %Docstring -Emitted whenever the log receives a message which has the ``notifyUser`` flag as true. +Emitted whenever the log receives a message which is not a Qgis.Info level message +and which has the ``notifyUser`` flag as true. If QgsMessageLogNotifyBlocker objects have been created then this signal may be temporarily suppressed. diff --git a/src/core/qgsmessagelog.h b/src/core/qgsmessagelog.h index 5f782aec48c..ea7834639c3 100644 --- a/src/core/qgsmessagelog.h +++ b/src/core/qgsmessagelog.h @@ -68,7 +68,8 @@ class CORE_EXPORT QgsMessageLog : public QObject //TODO QGIS 4.0 - remove received argument /** - * Emitted whenever the log receives a message which has the \a notifyUser flag as true. + * Emitted whenever the log receives a message which is not a Qgis::Info level message + * and which has the \a notifyUser flag as true. * * If QgsMessageLogNotifyBlocker objects have been created then this signal may be * temporarily suppressed. diff --git a/tests/src/python/CMakeLists.txt b/tests/src/python/CMakeLists.txt index c74cb9db0a7..b757683f1f2 100644 --- a/tests/src/python/CMakeLists.txt +++ b/tests/src/python/CMakeLists.txt @@ -113,6 +113,7 @@ ADD_PYTHON_TEST(PyQgsMapRendererCache test_qgsmaprenderercache.py) ADD_PYTHON_TEST(PyQgsMapThemeCollection test_qgsmapthemecollection.py) ADD_PYTHON_TEST(PyQgsMapUnitScale test_qgsmapunitscale.py) ADD_PYTHON_TEST(PyQgsMargins test_qgsmargins.py) +ADD_PYTHON_TEST(PyQgsMessageLog test_qgsmessagelog.py) ADD_PYTHON_TEST(PyQgsMetadataBase test_qgsmetadatabase.py) ADD_PYTHON_TEST(PyQgsMetadataWidget test_qgsmetadatawidget.py) ADD_PYTHON_TEST(PyQgsMemoryProvider test_provider_memory.py) diff --git a/tests/src/python/test_qgsmessagelog.py b/tests/src/python/test_qgsmessagelog.py new file mode 100644 index 00000000000..ee40ffb95da --- /dev/null +++ b/tests/src/python/test_qgsmessagelog.py @@ -0,0 +1,104 @@ +# -*- coding: utf-8 -*- +"""QGIS Unit tests for QgsMessageLog. + +.. note:: This program 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 2 of the License, or +(at your option) any later version. +""" +__author__ = 'Nyall Dawson' +__date__ = '18/06/2018' +__copyright__ = 'Copyright 2018, The QGIS Project' +# This will get replaced with a git SHA1 when you do a git archive +__revision__ = '$Format:%H$' + +import qgis # NOQA + +from qgis.core import (Qgis, + QgsApplication, + QgsMessageLog, + QgsMessageLogNotifyBlocker) + +from qgis.PyQt.QtTest import QSignalSpy + +from qgis.testing import start_app, unittest +from utilities import (unitTestDataPath) + +app = start_app() +TEST_DATA_DIR = unitTestDataPath() + + +class TestQgsMessageLog(unittest.TestCase): + + def testSignals(self): + local_log = QgsMessageLog() + app_log = QgsApplication.messageLog() + + # signals should only be emitted by application log, not local log + local_spy = QSignalSpy(local_log.messageReceived) + local_spy_received = QSignalSpy(local_log.messageReceived[bool]) + app_spy = QSignalSpy(app_log.messageReceived) + app_spy_received = QSignalSpy(app_log.messageReceived[bool]) + + local_log.logMessage('test', 'tag', Qgis.Info, notifyUser=True) + self.assertEqual(len(local_spy), 0) + self.assertEqual(len(local_spy_received), 0) + self.assertEqual(len(app_spy), 1) + self.assertEqual(app_spy[-1], ['test', 'tag', Qgis.Info]) + # info message, so messageReceived(bool) should not be emited + self.assertEqual(len(app_spy_received), 0) + + local_log.logMessage('test', 'tag', Qgis.Warning, notifyUser=True) + self.assertEqual(len(local_spy), 0) + self.assertEqual(len(local_spy_received), 0) + self.assertEqual(len(app_spy), 2) + self.assertEqual(app_spy[-1], ['test', 'tag', Qgis.Warning]) + # warning message, so messageReceived(bool) should be emited + self.assertEqual(len(app_spy_received), 1) + + local_log.logMessage('test', 'tag', Qgis.Warning, notifyUser=False) + self.assertEqual(len(local_spy), 0) + self.assertEqual(len(local_spy_received), 0) + self.assertEqual(len(app_spy), 3) + # notifyUser was False + self.assertEqual(len(app_spy_received), 1) + + def testBlocker(self): + local_log = QgsMessageLog() + app_log = QgsApplication.messageLog() + + spy = QSignalSpy(app_log.messageReceived) + spy_received = QSignalSpy(app_log.messageReceived[bool]) + + local_log.logMessage('test', 'tag', Qgis.Warning, notifyUser=True) + self.assertEqual(len(spy), 1) + self.assertEqual(spy[-1], ['test', 'tag', Qgis.Warning]) + self.assertEqual(len(spy_received), 1) + + # block notifications + b = QgsMessageLogNotifyBlocker() + local_log.logMessage('test', 'tag', Qgis.Warning, notifyUser=True) + self.assertEqual(len(spy), 2) # should not be blocked + self.assertEqual(len(spy_received), 1) # should be blocked + + # another blocker + b2 = QgsMessageLogNotifyBlocker() + local_log.logMessage('test', 'tag', Qgis.Warning, notifyUser=True) + self.assertEqual(len(spy), 3) # should not be blocked + self.assertEqual(len(spy_received), 1) # should be blocked + + del b + # still blocked because of b2 + local_log.logMessage('test', 'tag', Qgis.Warning, notifyUser=True) + self.assertEqual(len(spy), 4) # should not be blocked + self.assertEqual(len(spy_received), 1) # should be blocked + + del b2 + # not blocked + local_log.logMessage('test', 'tag', Qgis.Warning, notifyUser=True) + self.assertEqual(len(spy), 5) # should not be blocked + self.assertEqual(len(spy_received), 2) # should not be blocked + + +if __name__ == '__main__': + unittest.main()