1"""Test class for vtgui.py"""
2
3import pytest
4
5from qtpy import QtWidgets
6from qtpy import QtCore
7from qtpy import QtGui
8
9
10@pytest.mark.usefixtures('launcher')
11class TestLogger(object):
12    @pytest.fixture()
13    def logger(self, launcher):
14        return launcher.gui.logger
15
16    def test_write(self, logger):
17        text_cursor = logger.textCursor()
18
19        # Log regular message
20        logger.clear()
21        logger.write('Test text')
22        assert logger.toPlainText() == 'Test text'
23
24        # Log error message
25        logger.clear()
26        logger.write('\nError: test error text color')
27        fmt = text_cursor.charFormat()
28        color = fmt.foreground().color().name()
29        assert color == '#ff0000'
30
31        # Log warning message
32        logger.clear()
33        logger.write('\nWarning: test warning text color')
34        fmt = text_cursor.charFormat()
35        color = fmt.foreground().color().name()
36        assert color == '#f38908'
37
38    def test_setupContextMenu(self, launcher):
39        cm = launcher.gui.findChild(QtWidgets.QMenu, 'logger_context_menu')
40        menu_actions = cm.actions()
41        assert cm
42
43        actions = [a.objectName() for a in menu_actions
44                   if not (a.isSeparator() or a.menu())]
45        expected_actions = ['logger_copy_action', 'logger_clear_action',
46                            'logger_select_action']
47        assert sorted(actions) == sorted(expected_actions)
48
49        separators = [a for a in menu_actions if a.isSeparator()]
50        assert len(separators) == 1
51
52    def test_updateContextMenu(self, logger):
53        # Copy action enabled
54        logger.clear()
55        logger.write('Sample text')
56        logger.selectAll()
57        logger.updateContextMenu()
58        assert logger.copy_action.isEnabled()
59
60        # Copy action disabled
61        text_cursor = logger.textCursor()
62        text_cursor.clearSelection()
63        # Update the visible cursor (see qtextedit.html#textCursor docs)
64        logger.setTextCursor(text_cursor)
65        logger.updateContextMenu()
66        assert not logger.copy_action.isEnabled()
67
68        # Clear/select action enabled
69        logger.clear()
70        logger.write('Sample text')
71        logger.updateContextMenu()
72        assert logger.clear_action.isEnabled()
73        assert logger.select_action.isEnabled()
74
75        # Clear/select action disabled
76        logger.clear()
77        logger.updateContextMenu()
78        assert not logger.clear_action.isEnabled()
79        assert not logger.select_action.isEnabled()
80
81    def test_focusInEvent(self, launcher, logger):
82        # Give keyboard focus to the logger
83        # Notice that logger.setFocus() doesn't work if the widget is hidden
84        launcher.app.sendEvent(logger, QtGui.QFocusEvent(QtCore.QEvent.FocusIn))
85        # QTest.mouseClick(logger, QtCore.Qt.LeftButton)
86
87        # Check the logger configuration
88        assert logger.lineWidth() == 2
89        assert logger.frameShape() == QtWidgets.QFrame.Panel
90        assert logger.frameShadow() == QtWidgets.QFrame.Plain
91
92    def test_focusOutEvent(self, logger, launcher):
93        # Remove keyboard focus from the logger
94        launcher.app.sendEvent(logger,
95                               QtGui.QFocusEvent(QtCore.QEvent.FocusOut))
96
97        # Check the logger configuration
98        assert logger.lineWidth() == logger.frame_style['lwidth']
99        assert logger.frameShape() == logger.frame_style['shape']
100        assert logger.frameShadow() == logger.frame_style['shadow']
101