menu_descriptor_tests.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. # coding=utf-8
  2. # pystray
  3. # Copyright (C) 2016-2020 Moses Palmér
  4. #
  5. # This program is free software: you can redistribute it and/or modify it under
  6. # the terms of the GNU Lesser General Public License as published by the Free
  7. # Software Foundation, either version 3 of the License, or (at your option) any
  8. # later version.
  9. #
  10. # This program is distributed in the hope that it will be useful, but WITHOUT
  11. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  13. # details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. import unittest
  18. import pystray
  19. from pystray import Menu as menu
  20. from pystray import MenuItem as item
  21. from . import separator, true
  22. class MenuDescriptorTests(unittest.TestCase):
  23. def test_item_name(self):
  24. """Tests that the text is set correctly.
  25. """
  26. self.assertEqual(
  27. 'Test entry',
  28. item('Test entry', None).text)
  29. def test_item_call(self):
  30. """Tests that calling a menu item works as expected.
  31. """
  32. data = []
  33. item('', lambda: data.append(True))(None)
  34. self.assertEqual(
  35. [True],
  36. data)
  37. def test_item_call_various_args(self):
  38. """Tests that calling a menu item works as expected.
  39. """
  40. item('', lambda: None)(None)
  41. item('', lambda a: a)(None)
  42. item('', lambda a, b: (a, b))(None)
  43. with self.assertRaises(ValueError):
  44. item('', lambda a, b, c: (a, b, ))
  45. def test_menu_construct(self):
  46. """Tests menu construction.
  47. """
  48. self.assertEqual(
  49. '''
  50. Test entry one
  51. Test entry two
  52. Test entry three
  53. Test entry four''',
  54. '\n' + str(menu(
  55. item('Test entry one', None),
  56. item('Test entry two', None),
  57. item('Test entry three', None),
  58. item('Test entry four', None))))
  59. def test_menu_construct_from_generator(self):
  60. """Tests menu construction.
  61. """
  62. self.assertEqual(
  63. '''
  64. Test entry 1
  65. Test entry 2
  66. Test entry 3
  67. Test entry 4''',
  68. '\n' + str(menu(lambda: (
  69. item('Test entry %d' % (i + 1), None)
  70. for i in range(4)))))
  71. def test_menu_construct_with_submenu(self):
  72. """Tests menu construction.
  73. """
  74. self.assertEqual(
  75. '''
  76. Test entry 1
  77. Test entry 2 =>
  78. Test entry 3
  79. Test entry 4
  80. Test entry 5''',
  81. '\n' + str(menu(
  82. item('Test entry 1', None),
  83. item('Test entry 2', menu(
  84. item('Test entry 3', None),
  85. item('Test entry 4', None))),
  86. item('Test entry 5', None))))
  87. def test_menu_separator(self):
  88. """Tests menu construction with separators.
  89. """
  90. # Separators at the head and tail are ignored
  91. self.assertEqual(
  92. '''
  93. Test entry one
  94. - - - -
  95. Test entry two''',
  96. '\n' + str(menu(
  97. separator(),
  98. separator(),
  99. item('Test entry one', None),
  100. separator(),
  101. item('Test entry hidden', None, visible=False),
  102. separator(),
  103. item('Test entry two', None),
  104. separator(),
  105. separator())))
  106. def test_menu_default_none(self):
  107. """Tests that an invalid number of default menu items fails.
  108. """
  109. self.assertIsNone(
  110. menu(
  111. item('one', true, default=False),
  112. item('two', true, default=False))(None))
  113. def test_menu_default_callable(self):
  114. """Tests that the default menu item is activated when calling the menu.
  115. """
  116. self.assertEqual(
  117. 'test result',
  118. menu(
  119. item('one', lambda: 'test result', default=True))(None))
  120. def test_menu_visible_submenu(self):
  121. """Tests that ``visible`` is correctly set when a submenu is set.
  122. """
  123. self.assertTrue(
  124. item('Test', menu(
  125. item('Item', None)), visible=True).visible)
  126. self.assertFalse(
  127. item('Test', menu(
  128. item('Item', None)), visible=False).visible)
  129. self.assertFalse(
  130. item('Test', menu(
  131. item('Item', None, visible=False)), visible=True).visible)
  132. def test_menu_enabled_default(self):
  133. """Tests that a menu item is enabled by default.
  134. """
  135. self.assertTrue(
  136. item(
  137. 'one', lambda: 'test result').enabled)
  138. def test_menu_enabled_callable(self):
  139. """Tests that the enabled state can be set.
  140. """
  141. self.assertFalse(
  142. item(
  143. 'one', lambda: 'test result', enabled=False).enabled)
  144. def test_menu_checked_none(self):
  145. """Tests that not providing a value for ``default`` works.
  146. """
  147. self.assertFalse(
  148. item('Test', None).checked)
  149. def test_menu_checked_non_callable(self):
  150. """Tests that not passing a callable as checked fails.
  151. """
  152. with self.assertRaises(ValueError):
  153. item('Test', None, checked=False)
  154. def test_menu_checked_non_callable(self):
  155. """Tests that not providing a value for ``default`` works.
  156. """
  157. self.assertTrue(
  158. item('Test', None, checked=true).checked)
  159. def test_menu_radio_uncheckable(self):
  160. """Tests that not providing a value for ``checked`` for uncheckable
  161. items works.
  162. """
  163. self.assertFalse(
  164. item('Test', None).radio)
  165. self.assertFalse(
  166. item('Test', None, radio=False).radio)
  167. self.assertFalse(
  168. item('Test', None, radio=True).radio)
  169. def test_menu_radio_checkable(self):
  170. """Tests that providing a value for ``checked`` for checkable items
  171. works.
  172. """
  173. self.assertFalse(
  174. item('Test', None, checked=true).radio)
  175. self.assertTrue(
  176. item('Test', None, checked=true, radio=True).radio)
  177. self.assertTrue(
  178. item('Test', None, checked=true, radio=true).radio)