Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Unit tests for QgsMessageLog
  • Loading branch information
nyalldawson committed Jun 18, 2018
1 parent 72f7e5b commit 077eced
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 2 deletions.
3 changes: 2 additions & 1 deletion python/core/auto_generated/qgsmessagelog.sip.in
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion src/core/qgsmessagelog.h
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -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)
Expand Down
104 changes: 104 additions & 0 deletions 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()

0 comments on commit 077eced

Please sign in to comment.