|
| 1 | +""" |
| 2 | +Unit tests for individual bitmessageqt widgets. |
| 3 | +
|
| 4 | +These tests need a display (real or virtual via Xvfb/xvfb-run) and PyQt4, |
| 5 | +but do NOT require the full bitmessage backend, database, or network. |
| 6 | +Each test creates only the minimal widget under test. |
| 7 | +""" |
| 8 | +import sys |
| 9 | +import unittest |
| 10 | + |
| 11 | +try: |
| 12 | + from PyQt4 import QtCore, QtGui, QtTest |
| 13 | + has_qt = True |
| 14 | +except ImportError: |
| 15 | + has_qt = False |
| 16 | + |
| 17 | + |
| 18 | +def get_app(): |
| 19 | + """Return existing QApplication or create a new one""" |
| 20 | + return QtGui.QApplication.instance() or QtGui.QApplication(sys.argv) |
| 21 | + |
| 22 | + |
| 23 | +@unittest.skipUnless(has_qt, "requires PyQt4") |
| 24 | +class TestSafeHTMLParser(unittest.TestCase): |
| 25 | + """Test the SafeHTMLParser used for message rendering""" |
| 26 | + |
| 27 | + def test_unescape(self): |
| 28 | + """Undo urlencoding""" |
| 29 | + from bitmessageqt.safehtmlparser import SafeHTMLParser |
| 30 | + parser = SafeHTMLParser() |
| 31 | + parser.reset() |
| 32 | + parser.reset_safe() |
| 33 | + parser.feed("<b>hello</b> & world") |
| 34 | + self.assertIn("hello", parser.sanitised) |
| 35 | + self.assertNotIn("<script", parser.sanitised) |
| 36 | + |
| 37 | + |
| 38 | +@unittest.skipUnless(has_qt, "requires PyQt4") |
| 39 | +class TestMessageView(unittest.TestCase): |
| 40 | + """Test the MessageView widget in isolation""" |
| 41 | + |
| 42 | + def setUp(self): |
| 43 | + self.app = get_app() |
| 44 | + |
| 45 | + def test_create_messageview(self): |
| 46 | + """MessageView widget can be instantiated""" |
| 47 | + from bitmessageqt.messageview import MessageView |
| 48 | + widget = MessageView(None) |
| 49 | + self.assertIsNotNone(widget) |
| 50 | + |
| 51 | + @staticmethod |
| 52 | + def test_messageview_set_content(): |
| 53 | + """MessageView.setContent should not crash""" |
| 54 | + from bitmessageqt.messageview import MessageView |
| 55 | + widget = MessageView(None) |
| 56 | + widget.setContent("Hello, this is a <b>test</b> message.") |
| 57 | + |
| 58 | + |
| 59 | +@unittest.skipUnless(has_qt, "requires PyQt4") |
| 60 | +class TestAddressValidator(unittest.TestCase): |
| 61 | + """Test the AddressValidator""" |
| 62 | + |
| 63 | + def setUp(self): |
| 64 | + self.app = get_app() |
| 65 | + |
| 66 | + def test_create_validator(self): |
| 67 | + """AddressValidator can be instantiated with a buttonBox""" |
| 68 | + from bitmessageqt.addressvalidator import AddressValidator |
| 69 | + line_edit = QtGui.QLineEdit() |
| 70 | + # AddressValidator.setParams() reads the Ok button label on init, |
| 71 | + # so a real QDialogButtonBox is the simplest way to satisfy that. |
| 72 | + button_box = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Ok) |
| 73 | + validator = AddressValidator( |
| 74 | + line_edit, buttonBox=button_box) |
| 75 | + self.assertIsNotNone(validator) |
| 76 | + self.assertFalse(validator.isValid) |
| 77 | + |
| 78 | + |
| 79 | +@unittest.skipUnless(has_qt, "requires PyQt4") |
| 80 | +class TestLanguageBox(unittest.TestCase): |
| 81 | + """Test the language selection combobox""" |
| 82 | + |
| 83 | + def setUp(self): |
| 84 | + self.app = get_app() |
| 85 | + |
| 86 | + def test_create_languagebox(self): |
| 87 | + """LanguageBox can be instantiated""" |
| 88 | + from bitmessageqt.languagebox import LanguageBox |
| 89 | + parent = QtGui.QWidget() |
| 90 | + combo = LanguageBox(parent) |
| 91 | + self.assertIsNotNone(combo) |
| 92 | + self.assertGreater(combo.count(), 0) |
0 commit comments