|
| 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