You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

273 lines
8.9 KiB

  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. """Contains test cases for the widgets.py module."""
  4. from __future__ import unicode_literals
  5. import sys
  6. import os.path
  7. import unittest
  8. PATH = os.path.realpath(os.path.abspath(__file__))
  9. sys.path.insert(0, os.path.dirname(os.path.dirname(PATH)))
  10. try:
  11. import wx
  12. import mock
  13. from youtube_dl_gui.widgets import (
  14. ListBoxWithHeaders,
  15. CustomComboBox,
  16. ListBoxPopup
  17. )
  18. except ImportError as error:
  19. print error
  20. sys.exit(1)
  21. class TestListBoxWithHeaders(unittest.TestCase):
  22. """Test cases for the ListBoxWithHeaders widget."""
  23. def setUp(self):
  24. self.app = wx.App()
  25. self.frame = wx.Frame(None)
  26. self.listbox = ListBoxWithHeaders(self.frame)
  27. self.listbox.add_header("Header")
  28. self.listbox.add_items(["item%s" % i for i in xrange(10)])
  29. def tearDown(self):
  30. self.frame.Destroy()
  31. def test_find_string_header_found(self):
  32. self.assertEqual(self.listbox.FindString("Header"), 0)
  33. def test_find_string_header_not_found(self):
  34. self.assertEqual(self.listbox.FindString("Header2"), wx.NOT_FOUND)
  35. def test_find_string_item_found(self):
  36. self.assertEqual(self.listbox.FindString("item1"), 2)
  37. def test_find_string_item_not_found(self):
  38. self.assertEqual(self.listbox.FindString("item"), wx.NOT_FOUND)
  39. def test_get_string_header(self):
  40. self.assertEqual(self.listbox.GetString(0), "Header")
  41. def test_get_string_item(self):
  42. self.assertEqual(self.listbox.GetString(10), "item9")
  43. def test_get_string_item_not_found(self):
  44. self.assertEqual(self.listbox.GetString(11), "")
  45. def test_get_string_item_negative_index(self):
  46. self.assertEqual(self.listbox.GetString(-1), "")
  47. def test_insert_items(self):
  48. self.listbox.SetSelection(1)
  49. self.listbox.InsertItems(["new_item1", "new_item2"], 1)
  50. self.assertEqual(self.listbox.GetString(1), "new_item1")
  51. self.assertEqual(self.listbox.GetString(2), "new_item2")
  52. self.assertEqual(self.listbox.GetString(3), "item0")
  53. self.assertTrue(self.listbox.IsSelected(3)) # Old selection + 2
  54. def test_set_selection_header(self):
  55. self.listbox.SetSelection(0)
  56. self.assertFalse(self.listbox.IsSelected(0))
  57. def test_set_selection_item_valid_index(self):
  58. self.listbox.SetSelection(1)
  59. self.assertEqual(self.listbox.GetSelection(), 1)
  60. def test_set_selection_item_invalid_index(self):
  61. self.listbox.SetSelection(1)
  62. self.assertEqual(self.listbox.GetSelection(), 1)
  63. self.listbox.SetSelection(wx.NOT_FOUND)
  64. self.assertEqual(self.listbox.GetSelection(), wx.NOT_FOUND)
  65. def test_set_string_item(self):
  66. self.listbox.SetString(1, "item_mod0")
  67. self.assertEqual(self.listbox.GetString(1), "item_mod0")
  68. def test_set_string_header(self):
  69. self.listbox.SetString(0, "New header")
  70. self.assertEqual(self.listbox.GetString(0), "New header")
  71. # Make sure that the header is not selectable
  72. self.listbox.SetSelection(0)
  73. self.assertFalse(self.listbox.IsSelected(0))
  74. def test_set_string_selection_header(self):
  75. self.assertFalse(self.listbox.SetStringSelection("Header"))
  76. self.assertFalse(self.listbox.IsSelected(0))
  77. def test_set_string_selection_item(self):
  78. self.assertTrue(self.listbox.SetStringSelection("item1"))
  79. self.assertTrue(self.listbox.IsSelected(2))
  80. def test_get_string_selection(self):
  81. self.listbox.SetSelection(1)
  82. self.assertEqual(self.listbox.GetStringSelection(), "item0")
  83. def test_get_string_selection_empty(self):
  84. self.assertEqual(self.listbox.GetStringSelection(), "")
  85. # wx.ItemContainer methods
  86. def test_append(self):
  87. self.listbox.Append("item666")
  88. self.assertEqual(self.listbox.GetString(11), "item666")
  89. def test_append_items(self):
  90. self.listbox.AppendItems(["new_item1", "new_item2"])
  91. self.assertEqual(self.listbox.GetString(11), "new_item1")
  92. self.assertEqual(self.listbox.GetString(12), "new_item2")
  93. def test_clear(self):
  94. self.listbox.Clear()
  95. self.assertEqual(self.listbox.GetItems(), [])
  96. def test_delete(self):
  97. self.listbox.Delete(0)
  98. self.assertEqual(self.listbox.GetString(0), "item0")
  99. # Test item selection
  100. self.listbox.SetSelection(0)
  101. self.assertTrue(self.listbox.IsSelected(0))
  102. # Test object extra methods
  103. def test_add_header(self):
  104. self.listbox.add_header("Header2")
  105. self.listbox.SetSelection(11)
  106. self.assertFalse(self.listbox.IsSelected(11))
  107. @mock.patch("wx.ListBox.Append")
  108. def test_add_item_with_prefix(self, mock_append):
  109. self.listbox.add_item("new_item")
  110. mock_append.assert_called_once_with(ListBoxWithHeaders.TEXT_PREFIX + "new_item")
  111. @mock.patch("wx.ListBox.Append")
  112. def test_add_item_without_prefix(self, mock_append):
  113. self.listbox.add_item("new_item", with_prefix=False)
  114. mock_append.assert_called_once_with("new_item")
  115. @mock.patch("wx.ListBox.AppendItems")
  116. def test_add_items_with_prefix(self, mock_append):
  117. self.listbox.add_items(["new_item1", "new_item2"])
  118. mock_append.assert_called_once_with([ListBoxWithHeaders.TEXT_PREFIX + "new_item1",
  119. ListBoxWithHeaders.TEXT_PREFIX + "new_item2"])
  120. @mock.patch("wx.ListBox.AppendItems")
  121. def test_add_items_without_prefix(self, mock_append):
  122. self.listbox.add_items(["new_item1", "new_item2"], with_prefix=False)
  123. mock_append.assert_called_once_with(["new_item1", "new_item2"])
  124. class TestCustomComboBox(unittest.TestCase):
  125. """Test cases for the CustomComboBox widget."""
  126. def setUp(self):
  127. self.app = wx.App()
  128. self.frame = wx.Frame(None)
  129. self.combobox = CustomComboBox(self.frame)
  130. # Call directly the ListBoxWithHeaders methods
  131. self.combobox.listbox.GetControl().add_header("Header")
  132. self.combobox.listbox.GetControl().add_items(["item%s" % i for i in xrange(10)])
  133. def tearDown(self):
  134. self.frame.Destroy()
  135. def test_init(self):
  136. combobox = CustomComboBox(self.frame, -1, "item1", choices=["item0", "item1", "item2"])
  137. self.assertEqual(combobox.GetValue(), "item1")
  138. self.assertEqual(combobox.GetCount(), 3)
  139. self.assertEqual(combobox.GetSelection(), 1)
  140. # wx.ComboBox methods
  141. # Not all of them since most of them are calls to ListBoxWithHeaders
  142. # methods and we already have tests for those
  143. def test_is_list_empty_false(self):
  144. self.assertFalse(self.combobox.IsListEmpty())
  145. def test_is_list_empty_true(self):
  146. self.combobox.Clear()
  147. self.assertTrue(self.combobox.IsListEmpty())
  148. def test_is_text_empty_false(self):
  149. self.combobox.SetValue("somevalue")
  150. self.assertFalse(self.combobox.IsTextEmpty())
  151. def test_is_text_empty_true(self):
  152. self.assertTrue(self.combobox.IsTextEmpty())
  153. def test_set_selection_item(self):
  154. self.combobox.SetSelection(1)
  155. self.assertEqual(self.combobox.GetSelection(), 1)
  156. self.assertEqual(self.combobox.GetValue(), "item0")
  157. def test_set_selection_header(self):
  158. self.combobox.SetSelection(0)
  159. self.assertEqual(self.combobox.GetSelection(), wx.NOT_FOUND)
  160. self.assertEqual(self.combobox.GetValue(), "")
  161. def test_set_string_selection_item(self):
  162. self.combobox.SetStringSelection("item0")
  163. self.assertEqual(self.combobox.GetStringSelection(), "item0")
  164. self.assertEqual(self.combobox.GetValue(), "item0")
  165. def test_set_string_selection_header(self):
  166. self.combobox.SetStringSelection("Header")
  167. self.assertEqual(self.combobox.GetStringSelection(), "")
  168. self.assertEqual(self.combobox.GetValue(), "")
  169. def test_set_string_selection_invalid_string(self):
  170. self.combobox.SetStringSelection("abcde")
  171. self.assertEqual(self.combobox.GetStringSelection(), "")
  172. self.assertEqual(self.combobox.GetValue(), "")
  173. # wx.ItemContainer methods
  174. def test_clear(self):
  175. self.combobox.SetValue("value")
  176. self.combobox.Clear()
  177. self.assertEqual(self.combobox.GetCount(), 0)
  178. self.assertTrue(self.combobox.IsTextEmpty())
  179. def test_append(self):
  180. self.combobox.Append("item10")
  181. self.assertEqual(self.combobox.GetCount(), 12)
  182. def test_append_items(self):
  183. self.combobox.AppendItems(["item10", "item11"])
  184. self.assertEqual(self.combobox.GetCount(), 13)
  185. def test_delete(self):
  186. self.combobox.Delete(1)
  187. self.assertEqual(self.combobox.GetString(1), "item1")
  188. # wx.TextEntry methods
  189. def test_get_value(self):
  190. self.combobox.SetValue("value")
  191. self.assertEqual(self.combobox.GetValue(), "value")
  192. def main():
  193. unittest.main()
  194. if __name__ == '__main__':
  195. main()