/dat/tests/test_gui.py

https://gitlab.com/rbax81/DAT · Python · 301 lines · 281 code · 5 blank · 15 comment · 0 complexity · fa9d41136e67d2db2f7d530504a78ac1 MD5 · raw file

  1. """Tests for the dat.gui package.
  2. """
  3. import unittest
  4. import warnings
  5. import dat.tests
  6. from dat.tests import CallRecorder
  7. class Test_gui(unittest.TestCase):
  8. def test_translate(self):
  9. """Tests the translate() mechanism.
  10. Doesn't actually checks that anything gets translated.
  11. """
  12. from PyQt4 import QtCore
  13. old_translate = QtCore.QCoreApplication.translate
  14. try:
  15. QtCore.QCoreApplication.translate = cr = CallRecorder(
  16. old_translate)
  17. from dat.gui import translate
  18. tr = translate(Test_gui)
  19. self.assertIsNotNone(tr)
  20. msg = u"Don't translate "
  21. msg += u"me (unittest)" # there is no way xgettext can find this
  22. self.assertEqual(tr(msg), msg)
  23. call1 = (
  24. [
  25. 'dat.tests.test_gui.Test_gui',
  26. msg,
  27. None,
  28. QtCore.QCoreApplication.UnicodeUTF8],
  29. dict())
  30. self.assertEqual(cr.calls, [call1])
  31. tr = translate('this test')
  32. self.assertEqual(tr(msg, "disambiguation"), msg)
  33. call2 = (
  34. [
  35. 'this test',
  36. msg,
  37. "disambiguation",
  38. QtCore.QCoreApplication.UnicodeUTF8],
  39. dict())
  40. self.assertEqual(cr.calls, [call1, call2])
  41. finally:
  42. QtCore.QCoreApplication.translate = old_translate
  43. def test_notifications(self):
  44. """Tests the NotificationDispatcher class.
  45. """
  46. from dat.gui.application import NotificationDispatcher
  47. nd = NotificationDispatcher()
  48. class C(object):
  49. pass
  50. with warnings.catch_warnings(record=True) as w:
  51. warnings.resetwarnings()
  52. notif1 = CallRecorder()
  53. nd.register_notification('notif_A', notif1)
  54. # registered to non-existing notification
  55. self.assertEqual(len(w), 1)
  56. notif2 = CallRecorder()
  57. nd.register_notification('notif_A', notif2)
  58. nd.send_notification('notif_A', 'hello', 'world', rep=42)
  59. first_call = (['hello', 'world'], dict(rep=42))
  60. self.assertEqual(notif1.calls, [first_call])
  61. self.assertEqual(notif2.calls, [first_call])
  62. nd.unregister_notification('notif_A', notif2)
  63. nd.send_notification('notif_A', 'second', nb=2)
  64. second_call = (['second'], dict(nb=2))
  65. self.assertEqual(notif1.calls, [first_call, second_call])
  66. self.assertEqual(notif2.calls, [first_call])
  67. nd.builderWindow = C()
  68. viewA, viewB = C(), C()
  69. nd.builderWindow.current_view = viewA
  70. fakeWindow = C()
  71. nd.create_notification('notif_B')
  72. self.assertEqual(len(w), 1)
  73. nd.create_notification('notif_B')
  74. # notification created twice
  75. self.assertEqual(len(w), 2)
  76. nd.create_notification('notif_B', window=fakeWindow)
  77. nd.create_notification('notif_B', view=viewA)
  78. nd.create_notification('notif_B', view=viewB)
  79. notif3 = CallRecorder()
  80. nd.register_notification('notif_B', notif3,
  81. window=fakeWindow)
  82. notif4 = CallRecorder()
  83. self.assertEqual(len(w), 2)
  84. nd.register_notification('notif_B', notif4,
  85. window=nd.builderWindow)
  86. # registered to non-existing notification
  87. self.assertEqual(len(w), 3)
  88. notif5 = CallRecorder()
  89. nd.register_notification('notif_B', notif5)
  90. notif6 = CallRecorder()
  91. nd.register_notification('notif_B', notif6,
  92. view=viewA)
  93. notif7 = CallRecorder()
  94. nd.register_notification('notif_B', notif7,
  95. view=viewB)
  96. nd.send_notification('notif_B', 'third')
  97. third_call = (['third'], dict())
  98. self.assertEqual(notif1.calls, [first_call, second_call])
  99. self.assertEqual(notif2.calls, [first_call])
  100. self.assertEqual(notif3.calls, [])
  101. self.assertEqual(notif4.calls, [third_call])
  102. self.assertEqual(notif5.calls, [third_call])
  103. self.assertEqual(notif6.calls, [third_call])
  104. self.assertEqual(notif7.calls, [])
  105. self.assertEqual(len(w), 3)
  106. nd.unregister_notification('notif_A', notif7)
  107. # unregistered non-registered method
  108. self.assertEqual(len(w), 4)
  109. nd.unregister_notification('notif_B', notif6,
  110. view=viewA)
  111. nd.send_notification('notif_B', nb=4)
  112. self.assertEqual(len(w), 4)
  113. self.assertEqual(notif6.calls, [third_call])
  114. class Test_advancedlineedit(unittest.TestCase):
  115. def setUp(self):
  116. self._app = dat.tests.setup_application()
  117. def tearDown(self):
  118. self._app.quit()
  119. self._app = None
  120. def test_validation(self):
  121. """Tests the validation logic.
  122. """
  123. from dat.gui.generic import AdvancedLineEdit
  124. le = AdvancedLineEdit("test",
  125. validate=lambda t: t == "a")
  126. self._app.processEvents()
  127. self.assertEqual(le.text(), "test")
  128. self.assertFalse(le.isValid())
  129. self.assertEqual(le._choose_color(), "#DDAAAA")
  130. le.setText("a")
  131. self._app.processEvents()
  132. self.assertTrue(le.isValid())
  133. self.assertEqual(le._choose_color(), "#AADDAA")
  134. def test_default(self):
  135. """Tests the default value logic.
  136. """
  137. from dat.gui.generic import AdvancedLineEdit
  138. le = AdvancedLineEdit("test",
  139. default="a")
  140. self._app.processEvents()
  141. self.assertEqual(le.text(), "test")
  142. self.assertFalse(le.isDefault())
  143. self.assertEqual(le._choose_color(), "#FFFFFF")
  144. le.reset()
  145. self._app.processEvents()
  146. self.assertEqual(le.text(), "a")
  147. self.assertTrue(le.isDefault())
  148. self.assertEqual(le._choose_color(), "#AAAADD")
  149. def test_basic(self):
  150. """Tests the widget without validation or default value.
  151. """
  152. from dat.gui.generic import AdvancedLineEdit
  153. le = AdvancedLineEdit("test")
  154. self._app.processEvents()
  155. self.assertEqual(le.text(), "test")
  156. self.assertEqual(le._choose_color(), "#FFFFFF")
  157. le.setText("42")
  158. self._app.processEvents()
  159. self.assertEqual(le.text(), "42")
  160. self.assertEqual(le._choose_color(), "#FFFFFF")
  161. def test_both_diff(self):
  162. """Tests the widget with both validation and default value.
  163. """
  164. from dat.gui.generic import AdvancedLineEdit
  165. le = AdvancedLineEdit("test",
  166. default="b",
  167. validate=lambda t: t == "a")
  168. self._app.processEvents()
  169. self.assertEqual(le.text(), "test")
  170. self.assertFalse(le.isDefault())
  171. self.assertFalse(le.isValid())
  172. self.assertEqual(le._choose_color(), "#DDAAAA")
  173. le.reset()
  174. self._app.processEvents()
  175. self.assertEqual(le.text(), "b")
  176. self.assertTrue(le.isDefault())
  177. self.assertFalse(le.isValid())
  178. self.assertEqual(le._choose_color(), "#DDAAAA")
  179. le.setText("a")
  180. self._app.processEvents()
  181. self.assertFalse(le.isDefault())
  182. self.assertTrue(le.isValid())
  183. self.assertEqual(le._choose_color(), "#AADDAA")
  184. def test_both_diff_flag_default(self):
  185. """Tests that the correct logic gets used with COLOR_DEFAULTVALUE.
  186. """
  187. from dat.gui.generic import AdvancedLineEdit
  188. le = AdvancedLineEdit("test",
  189. default="b",
  190. validate=lambda t: t == "a",
  191. flags=AdvancedLineEdit.COLOR_DEFAULTVALUE)
  192. self._app.processEvents()
  193. self.assertFalse(le.isDefault())
  194. self.assertFalse(le.isValid())
  195. self.assertEqual(le._choose_color(), "#FFFFFF")
  196. le.reset()
  197. self._app.processEvents()
  198. self.assertEqual(le.text(), "b")
  199. self.assertTrue(le.isDefault())
  200. self.assertFalse(le.isValid())
  201. self.assertEqual(le._choose_color(), "#AAAADD")
  202. def test_both_diff_flag_valid(self):
  203. """Tests that the correct logic gets used with COLOR_VALIDITY.
  204. """
  205. from dat.gui.generic import AdvancedLineEdit
  206. le = AdvancedLineEdit("test",
  207. default="b",
  208. validate=lambda t: t == "b",
  209. flags=AdvancedLineEdit.COLOR_VALIDITY)
  210. self._app.processEvents()
  211. self.assertFalse(le.isDefault())
  212. self.assertFalse(le.isValid())
  213. self.assertEqual(le._choose_color(), "#DDAAAA")
  214. le.reset()
  215. self._app.processEvents()
  216. self.assertEqual(le.text(), "b")
  217. self.assertTrue(le.isDefault())
  218. self.assertTrue(le.isValid())
  219. self.assertEqual(le._choose_color(), "#AADDAA")
  220. def test_both_same(self):
  221. """Tests the logic when the value is valid and the default.
  222. """
  223. from dat.gui.generic import AdvancedLineEdit
  224. le = AdvancedLineEdit("test",
  225. default="a",
  226. validate=lambda t: t == "a")
  227. self._app.processEvents()
  228. self.assertEqual(le.text(), "test")
  229. self.assertFalse(le.isDefault())
  230. self.assertFalse(le.isValid())
  231. self.assertEqual(le._choose_color(), "#DDAAAA")
  232. le.setText("a")
  233. self._app.processEvents()
  234. self.assertTrue(le.isDefault())
  235. self.assertTrue(le.isValid())
  236. self.assertEqual(le._choose_color(), "#AAAADD")
  237. def test_follow(self):
  238. """Tests changing the default value when FOLLOW_DEFAULT_UPDATE is set.
  239. """
  240. from dat.gui.generic import AdvancedLineEdit
  241. le = AdvancedLineEdit("test",
  242. default="a",
  243. flags=AdvancedLineEdit.FOLLOW_DEFAULT_UPDATE)
  244. self._app.processEvents()
  245. self.assertFalse(le.isDefault())
  246. le.setDefault("b")
  247. self._app.processEvents()
  248. self.assertFalse(le.isDefault())
  249. self.assertEqual(le.text(), "test")
  250. le.setText("b")
  251. self._app.processEvents()
  252. self.assertTrue(le.isDefault())
  253. le.setDefault("c")
  254. self._app.processEvents()
  255. self.assertEqual(le.text(), "c")
  256. self.assertTrue(le.isDefault())
  257. le.setDefault("c")
  258. self._app.processEvents()
  259. self.assertTrue(le.isDefault())