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