Skip to content

Commit 077eced

Browse files
committedJun 18, 2018
Unit tests for QgsMessageLog
1 parent 72f7e5b commit 077eced

File tree

4 files changed

+109
-2
lines changed

4 files changed

+109
-2
lines changed
 

‎python/core/auto_generated/qgsmessagelog.sip.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ value for the message.
5656

5757
void messageReceived( bool received );
5858
%Docstring
59-
Emitted whenever the log receives a message which has the ``notifyUser`` flag as true.
59+
Emitted whenever the log receives a message which is not a Qgis.Info level message
60+
and which has the ``notifyUser`` flag as true.
6061

6162
If QgsMessageLogNotifyBlocker objects have been created then this signal may be
6263
temporarily suppressed.

‎src/core/qgsmessagelog.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ class CORE_EXPORT QgsMessageLog : public QObject
6868
//TODO QGIS 4.0 - remove received argument
6969

7070
/**
71-
* Emitted whenever the log receives a message which has the \a notifyUser flag as true.
71+
* Emitted whenever the log receives a message which is not a Qgis::Info level message
72+
* and which has the \a notifyUser flag as true.
7273
*
7374
* If QgsMessageLogNotifyBlocker objects have been created then this signal may be
7475
* temporarily suppressed.

‎tests/src/python/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ ADD_PYTHON_TEST(PyQgsMapRendererCache test_qgsmaprenderercache.py)
113113
ADD_PYTHON_TEST(PyQgsMapThemeCollection test_qgsmapthemecollection.py)
114114
ADD_PYTHON_TEST(PyQgsMapUnitScale test_qgsmapunitscale.py)
115115
ADD_PYTHON_TEST(PyQgsMargins test_qgsmargins.py)
116+
ADD_PYTHON_TEST(PyQgsMessageLog test_qgsmessagelog.py)
116117
ADD_PYTHON_TEST(PyQgsMetadataBase test_qgsmetadatabase.py)
117118
ADD_PYTHON_TEST(PyQgsMetadataWidget test_qgsmetadatawidget.py)
118119
ADD_PYTHON_TEST(PyQgsMemoryProvider test_provider_memory.py)
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# -*- coding: utf-8 -*-
2+
"""QGIS Unit tests for QgsMessageLog.
3+
4+
.. note:: This program is free software; you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation; either version 2 of the License, or
7+
(at your option) any later version.
8+
"""
9+
__author__ = 'Nyall Dawson'
10+
__date__ = '18/06/2018'
11+
__copyright__ = 'Copyright 2018, The QGIS Project'
12+
# This will get replaced with a git SHA1 when you do a git archive
13+
__revision__ = '$Format:%H$'
14+
15+
import qgis # NOQA
16+
17+
from qgis.core import (Qgis,
18+
QgsApplication,
19+
QgsMessageLog,
20+
QgsMessageLogNotifyBlocker)
21+
22+
from qgis.PyQt.QtTest import QSignalSpy
23+
24+
from qgis.testing import start_app, unittest
25+
from utilities import (unitTestDataPath)
26+
27+
app = start_app()
28+
TEST_DATA_DIR = unitTestDataPath()
29+
30+
31+
class TestQgsMessageLog(unittest.TestCase):
32+
33+
def testSignals(self):
34+
local_log = QgsMessageLog()
35+
app_log = QgsApplication.messageLog()
36+
37+
# signals should only be emitted by application log, not local log
38+
local_spy = QSignalSpy(local_log.messageReceived)
39+
local_spy_received = QSignalSpy(local_log.messageReceived[bool])
40+
app_spy = QSignalSpy(app_log.messageReceived)
41+
app_spy_received = QSignalSpy(app_log.messageReceived[bool])
42+
43+
local_log.logMessage('test', 'tag', Qgis.Info, notifyUser=True)
44+
self.assertEqual(len(local_spy), 0)
45+
self.assertEqual(len(local_spy_received), 0)
46+
self.assertEqual(len(app_spy), 1)
47+
self.assertEqual(app_spy[-1], ['test', 'tag', Qgis.Info])
48+
# info message, so messageReceived(bool) should not be emited
49+
self.assertEqual(len(app_spy_received), 0)
50+
51+
local_log.logMessage('test', 'tag', Qgis.Warning, notifyUser=True)
52+
self.assertEqual(len(local_spy), 0)
53+
self.assertEqual(len(local_spy_received), 0)
54+
self.assertEqual(len(app_spy), 2)
55+
self.assertEqual(app_spy[-1], ['test', 'tag', Qgis.Warning])
56+
# warning message, so messageReceived(bool) should be emited
57+
self.assertEqual(len(app_spy_received), 1)
58+
59+
local_log.logMessage('test', 'tag', Qgis.Warning, notifyUser=False)
60+
self.assertEqual(len(local_spy), 0)
61+
self.assertEqual(len(local_spy_received), 0)
62+
self.assertEqual(len(app_spy), 3)
63+
# notifyUser was False
64+
self.assertEqual(len(app_spy_received), 1)
65+
66+
def testBlocker(self):
67+
local_log = QgsMessageLog()
68+
app_log = QgsApplication.messageLog()
69+
70+
spy = QSignalSpy(app_log.messageReceived)
71+
spy_received = QSignalSpy(app_log.messageReceived[bool])
72+
73+
local_log.logMessage('test', 'tag', Qgis.Warning, notifyUser=True)
74+
self.assertEqual(len(spy), 1)
75+
self.assertEqual(spy[-1], ['test', 'tag', Qgis.Warning])
76+
self.assertEqual(len(spy_received), 1)
77+
78+
# block notifications
79+
b = QgsMessageLogNotifyBlocker()
80+
local_log.logMessage('test', 'tag', Qgis.Warning, notifyUser=True)
81+
self.assertEqual(len(spy), 2) # should not be blocked
82+
self.assertEqual(len(spy_received), 1) # should be blocked
83+
84+
# another blocker
85+
b2 = QgsMessageLogNotifyBlocker()
86+
local_log.logMessage('test', 'tag', Qgis.Warning, notifyUser=True)
87+
self.assertEqual(len(spy), 3) # should not be blocked
88+
self.assertEqual(len(spy_received), 1) # should be blocked
89+
90+
del b
91+
# still blocked because of b2
92+
local_log.logMessage('test', 'tag', Qgis.Warning, notifyUser=True)
93+
self.assertEqual(len(spy), 4) # should not be blocked
94+
self.assertEqual(len(spy_received), 1) # should be blocked
95+
96+
del b2
97+
# not blocked
98+
local_log.logMessage('test', 'tag', Qgis.Warning, notifyUser=True)
99+
self.assertEqual(len(spy), 5) # should not be blocked
100+
self.assertEqual(len(spy_received), 2) # should not be blocked
101+
102+
103+
if __name__ == '__main__':
104+
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.