[9290b1a] | 1 | import sys |
---|
| 2 | import unittest |
---|
[7fb471d] | 3 | from unittest.mock import MagicMock |
---|
[9290b1a] | 4 | |
---|
[53c771e] | 5 | from PyQt5 import QtGui, QtWidgets |
---|
[9290b1a] | 6 | |
---|
| 7 | # set up import paths |
---|
| 8 | import path_prepare |
---|
| 9 | |
---|
| 10 | # Local |
---|
[83eb5208] | 11 | from sas.qtgui.Plotting.AddText import AddText |
---|
[9290b1a] | 12 | |
---|
[53c771e] | 13 | if not QtWidgets.QApplication.instance(): |
---|
| 14 | app = QtWidgets.QApplication(sys.argv) |
---|
[9290b1a] | 15 | |
---|
| 16 | class AddTextTest(unittest.TestCase): |
---|
| 17 | '''Test the AddText''' |
---|
| 18 | def setUp(self): |
---|
| 19 | '''Create the AddText''' |
---|
| 20 | self.widget = AddText(None) |
---|
| 21 | |
---|
| 22 | def tearDown(self): |
---|
| 23 | '''Destroy the GUI''' |
---|
| 24 | self.widget.close() |
---|
| 25 | self.widget = None |
---|
| 26 | |
---|
| 27 | def testDefaults(self): |
---|
| 28 | '''Test the GUI in its default state''' |
---|
[53c771e] | 29 | self.assertIsInstance(self.widget, QtWidgets.QDialog) |
---|
[9290b1a] | 30 | self.assertIsInstance(self.widget._font, QtGui.QFont) |
---|
| 31 | self.assertEqual(self.widget._color, "black") |
---|
| 32 | |
---|
| 33 | def testOnFontChange(self): |
---|
| 34 | '''Test the QFontDialog output''' |
---|
| 35 | font_1 = QtGui.QFont("Helvetica", 15) |
---|
[53c771e] | 36 | QtWidgets.QFontDialog.getFont = MagicMock(return_value=(font_1, True)) |
---|
[9290b1a] | 37 | # Call the method |
---|
| 38 | self.widget.onFontChange(None) |
---|
| 39 | # Check that the text field got the new font info |
---|
| 40 | self.assertEqual(self.widget.textEdit.currentFont(), font_1) |
---|
| 41 | |
---|
| 42 | # See that rejecting the dialog doesn't modify the font |
---|
| 43 | font_2 = QtGui.QFont("Arial", 9) |
---|
[53c771e] | 44 | QtWidgets.QFontDialog.getFont = MagicMock(return_value=(font_2, False)) |
---|
[9290b1a] | 45 | # Call the method |
---|
| 46 | self.widget.onFontChange(None) |
---|
| 47 | # Check that the text field retained the previous font info |
---|
| 48 | self.assertEqual(self.widget.textEdit.currentFont(), font_1) |
---|
| 49 | |
---|
| 50 | def testOnColorChange(self): |
---|
| 51 | ''' Test the QColorDialog output''' |
---|
| 52 | new_color = QtGui.QColor("red") |
---|
[53c771e] | 53 | QtWidgets.QColorDialog.getColor = MagicMock(return_value=new_color) |
---|
[9290b1a] | 54 | # Call the method |
---|
| 55 | self.widget.onColorChange(None) |
---|
| 56 | # Check that the text field got the new color info for text |
---|
| 57 | self.assertEqual(self.widget.textEdit.palette().color(QtGui.QPalette.Text), new_color) |
---|
| 58 | # ... and the hex value of this color is correct |
---|
| 59 | self.assertEqual(self.widget.color(), "#ff0000") |
---|
| 60 | |
---|
| 61 | if __name__ == "__main__": |
---|
| 62 | unittest.main() |
---|