test_textview.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. """Test textview, coverage 100%.
  2. Since all methods and functions create (or destroy) a ViewWindow, which
  3. is a widget containing a widget, etcetera, all tests must be gui tests.
  4. Using mock Text would not change this. Other mocks are used to retrieve
  5. information about calls.
  6. """
  7. from idlelib import textview as tv
  8. from test.support import requires
  9. requires('gui')
  10. import os
  11. import unittest
  12. from tkinter import Tk, TclError, CHAR, NONE, WORD
  13. from tkinter.ttk import Button
  14. from idlelib.idle_test.mock_idle import Func
  15. from idlelib.idle_test.mock_tk import Mbox_func
  16. def setUpModule():
  17. global root
  18. root = Tk()
  19. root.withdraw()
  20. def tearDownModule():
  21. global root
  22. root.update_idletasks()
  23. root.destroy()
  24. del root
  25. # If we call ViewWindow or wrapper functions with defaults
  26. # modal=True, _utest=False, test hangs on call to wait_window.
  27. # Have also gotten tk error 'can't invoke "event" command'.
  28. class VW(tv.ViewWindow): # Used in ViewWindowTest.
  29. transient = Func()
  30. grab_set = Func()
  31. wait_window = Func()
  32. # Call wrapper class VW with mock wait_window.
  33. class ViewWindowTest(unittest.TestCase):
  34. def setUp(self):
  35. VW.transient.__init__()
  36. VW.grab_set.__init__()
  37. VW.wait_window.__init__()
  38. def test_init_modal(self):
  39. view = VW(root, 'Title', 'test text')
  40. self.assertTrue(VW.transient.called)
  41. self.assertTrue(VW.grab_set.called)
  42. self.assertTrue(VW.wait_window.called)
  43. view.ok()
  44. def test_init_nonmodal(self):
  45. view = VW(root, 'Title', 'test text', modal=False)
  46. self.assertFalse(VW.transient.called)
  47. self.assertFalse(VW.grab_set.called)
  48. self.assertFalse(VW.wait_window.called)
  49. view.ok()
  50. def test_ok(self):
  51. view = VW(root, 'Title', 'test text', modal=False)
  52. view.destroy = Func()
  53. view.ok()
  54. self.assertTrue(view.destroy.called)
  55. del view.destroy # Unmask real function.
  56. view.destroy()
  57. class AutoHideScrollbarTest(unittest.TestCase):
  58. # Method set is tested in ScrollableTextFrameTest
  59. def test_forbidden_geometry(self):
  60. scroll = tv.AutoHideScrollbar(root)
  61. self.assertRaises(TclError, scroll.pack)
  62. self.assertRaises(TclError, scroll.place)
  63. class ScrollableTextFrameTest(unittest.TestCase):
  64. @classmethod
  65. def setUpClass(cls):
  66. cls.root = root = Tk()
  67. root.withdraw()
  68. @classmethod
  69. def tearDownClass(cls):
  70. cls.root.update_idletasks()
  71. cls.root.destroy()
  72. del cls.root
  73. def make_frame(self, wrap=NONE, **kwargs):
  74. frame = tv.ScrollableTextFrame(self.root, wrap=wrap, **kwargs)
  75. def cleanup_frame():
  76. frame.update_idletasks()
  77. frame.destroy()
  78. self.addCleanup(cleanup_frame)
  79. return frame
  80. def test_line1(self):
  81. frame = self.make_frame()
  82. frame.text.insert('1.0', 'test text')
  83. self.assertEqual(frame.text.get('1.0', '1.end'), 'test text')
  84. def test_horiz_scrollbar(self):
  85. # The horizontal scrollbar should be shown/hidden according to
  86. # the 'wrap' setting: It should only be shown when 'wrap' is
  87. # set to NONE.
  88. # wrap = NONE -> with horizontal scrolling
  89. frame = self.make_frame(wrap=NONE)
  90. self.assertEqual(frame.text.cget('wrap'), NONE)
  91. self.assertIsNotNone(frame.xscroll)
  92. # wrap != NONE -> no horizontal scrolling
  93. for wrap in [CHAR, WORD]:
  94. with self.subTest(wrap=wrap):
  95. frame = self.make_frame(wrap=wrap)
  96. self.assertEqual(frame.text.cget('wrap'), wrap)
  97. self.assertIsNone(frame.xscroll)
  98. class ViewFrameTest(unittest.TestCase):
  99. @classmethod
  100. def setUpClass(cls):
  101. cls.root = root = Tk()
  102. root.withdraw()
  103. cls.frame = tv.ViewFrame(root, 'test text')
  104. @classmethod
  105. def tearDownClass(cls):
  106. del cls.frame
  107. cls.root.update_idletasks()
  108. cls.root.destroy()
  109. del cls.root
  110. def test_line1(self):
  111. get = self.frame.text.get
  112. self.assertEqual(get('1.0', '1.end'), 'test text')
  113. # Call ViewWindow with modal=False.
  114. class ViewFunctionTest(unittest.TestCase):
  115. @classmethod
  116. def setUpClass(cls):
  117. cls.orig_error = tv.showerror
  118. tv.showerror = Mbox_func()
  119. @classmethod
  120. def tearDownClass(cls):
  121. tv.showerror = cls.orig_error
  122. del cls.orig_error
  123. def test_view_text(self):
  124. view = tv.view_text(root, 'Title', 'test text', modal=False)
  125. self.assertIsInstance(view, tv.ViewWindow)
  126. self.assertIsInstance(view.viewframe, tv.ViewFrame)
  127. view.viewframe.ok()
  128. def test_view_file(self):
  129. view = tv.view_file(root, 'Title', __file__, 'ascii', modal=False)
  130. self.assertIsInstance(view, tv.ViewWindow)
  131. self.assertIsInstance(view.viewframe, tv.ViewFrame)
  132. get = view.viewframe.textframe.text.get
  133. self.assertIn('Test', get('1.0', '1.end'))
  134. view.ok()
  135. def test_bad_file(self):
  136. # Mock showerror will be used; view_file will return None.
  137. view = tv.view_file(root, 'Title', 'abc.xyz', 'ascii', modal=False)
  138. self.assertIsNone(view)
  139. self.assertEqual(tv.showerror.title, 'File Load Error')
  140. def test_bad_encoding(self):
  141. p = os.path
  142. fn = p.abspath(p.join(p.dirname(__file__), '..', 'CREDITS.txt'))
  143. view = tv.view_file(root, 'Title', fn, 'ascii', modal=False)
  144. self.assertIsNone(view)
  145. self.assertEqual(tv.showerror.title, 'Unicode Decode Error')
  146. def test_nowrap(self):
  147. view = tv.view_text(root, 'Title', 'test', modal=False, wrap='none')
  148. text_widget = view.viewframe.textframe.text
  149. self.assertEqual(text_widget.cget('wrap'), 'none')
  150. # Call ViewWindow with _utest=True.
  151. class ButtonClickTest(unittest.TestCase):
  152. def setUp(self):
  153. self.view = None
  154. self.called = False
  155. def tearDown(self):
  156. if self.view:
  157. self.view.destroy()
  158. def test_view_text_bind_with_button(self):
  159. def _command():
  160. self.called = True
  161. self.view = tv.view_text(root, 'TITLE_TEXT', 'COMMAND', _utest=True)
  162. button = Button(root, text='BUTTON', command=_command)
  163. button.invoke()
  164. self.addCleanup(button.destroy)
  165. self.assertEqual(self.called, True)
  166. self.assertEqual(self.view.title(), 'TITLE_TEXT')
  167. self.assertEqual(self.view.viewframe.textframe.text.get('1.0', '1.end'),
  168. 'COMMAND')
  169. def test_view_file_bind_with_button(self):
  170. def _command():
  171. self.called = True
  172. self.view = tv.view_file(root, 'TITLE_FILE', __file__,
  173. encoding='ascii', _utest=True)
  174. button = Button(root, text='BUTTON', command=_command)
  175. button.invoke()
  176. self.addCleanup(button.destroy)
  177. self.assertEqual(self.called, True)
  178. self.assertEqual(self.view.title(), 'TITLE_FILE')
  179. get = self.view.viewframe.textframe.text.get
  180. with open(__file__) as f:
  181. self.assertEqual(get('1.0', '1.end'), f.readline().strip())
  182. f.readline()
  183. self.assertEqual(get('3.0', '3.end'), f.readline().strip())
  184. if __name__ == '__main__':
  185. unittest.main(verbosity=2)