|
1 | 1 | """Test util, coverage 100%""" |
2 | 2 |
|
| 3 | +import sys |
3 | 4 | import unittest |
| 5 | +from unittest import mock |
| 6 | +from test.support import requires |
| 7 | +from test.support.isolation import runInSubprocess |
| 8 | +import tkinter |
| 9 | +from tkinter import EventType |
4 | 10 | from idlelib import util |
| 11 | +from idlelib.idle_test.mock_tk import Event |
5 | 12 |
|
6 | 13 |
|
7 | 14 | class UtilTest(unittest.TestCase): |
| 15 | + |
8 | 16 | def test_extensions(self): |
9 | 17 | for extension in {'.pyi', '.py', '.pyw'}: |
10 | 18 | self.assertIn(extension, util.py_extensions) |
11 | 19 |
|
| 20 | + @unittest.skipUnless(sys.platform == 'win32', 'Windows only') |
| 21 | + @runInSubprocess() |
| 22 | + def test_fix_win_hidpi(self): |
| 23 | + # Awareness is process-wide and cannot be undone. |
| 24 | + import ctypes |
| 25 | + PROCESS_DPI_UNAWARE = 0 |
| 26 | + util.fix_win_hidpi() |
| 27 | + awareness = ctypes.c_int() |
| 28 | + ctypes.OleDLL('shcore').GetProcessDpiAwareness( |
| 29 | + None, ctypes.byref(awareness)) |
| 30 | + self.assertNotEqual(awareness.value, PROCESS_DPI_UNAWARE) |
| 31 | + |
| 32 | + |
| 33 | +class WheelTest(unittest.TestCase): |
| 34 | + "Test the wheel functions with a widget on this display." |
| 35 | + |
| 36 | + @classmethod |
| 37 | + def setUpClass(cls): |
| 38 | + requires('gui') |
| 39 | + cls.root = tkinter.Tk() |
| 40 | + cls.root.withdraw() |
| 41 | + |
| 42 | + @classmethod |
| 43 | + def tearDownClass(cls): |
| 44 | + cls.root.destroy() |
| 45 | + del cls.root |
| 46 | + |
| 47 | + def setUp(self): |
| 48 | + self.text = tkinter.Text(self.root) |
| 49 | + self.addCleanup(self.text.destroy) |
| 50 | + |
| 51 | + def test_x11_buttons(self): |
| 52 | + # Only X11 before Tk 8.7 sends the wheel as button events. |
| 53 | + text = self.text |
| 54 | + if text._windowingsystem == 'x11' and tkinter.TkVersion < 8.7: |
| 55 | + self.assertTrue(util.x11_buttons(text)) |
| 56 | + else: |
| 57 | + self.assertFalse(util.x11_buttons(text)) |
| 58 | + |
| 59 | + def test_bind_wheel(self): |
| 60 | + # The events Tk sends here are the ones bound. |
| 61 | + text = self.text |
| 62 | + util.bind_wheel(text, util.wheel_event) |
| 63 | + if util.x11_buttons(text): |
| 64 | + self.assertEqual(sorted(text.bind()), |
| 65 | + ['<Button-4>', '<Button-5>']) |
| 66 | + else: |
| 67 | + self.assertEqual(sorted(text.bind()), ['<MouseWheel>']) |
| 68 | + |
| 69 | + |
| 70 | +class WheelEventTest(unittest.TestCase): |
| 71 | + "Test the direction and the amount of the scroll." |
| 72 | + |
| 73 | + # An unmapped widget has no height and does not scroll by lines, |
| 74 | + # so record the yview call instead of a real scroll. |
| 75 | + def event(self, event_type, delta=0, num='??'): |
| 76 | + # Tk leaves num '??' for a wheel event and delta 0 for a button. |
| 77 | + return Event(type=event_type, delta=delta, num=num, |
| 78 | + widget=mock.Mock()) |
| 79 | + |
| 80 | + def scroll(self, event, widget=None): |
| 81 | + "Return the arguments of the yview call." |
| 82 | + self.assertEqual(util.wheel_event(event, widget), 'break') |
| 83 | + scrolled = event.widget if widget is None else widget |
| 84 | + scrolled.yview.assert_called_once() |
| 85 | + return scrolled.yview.call_args.args |
| 86 | + |
| 87 | + def test_mousewheel(self): |
| 88 | + # Delta is positive for up on all systems. |
| 89 | + for delta in 120, 1, 1200: |
| 90 | + self.assertEqual(self.scroll(self.event(EventType.MouseWheel, |
| 91 | + delta)), |
| 92 | + ('scroll', -5, 'units')) |
| 93 | + self.assertEqual(self.scroll(self.event(EventType.MouseWheel, |
| 94 | + -delta)), |
| 95 | + ('scroll', 5, 'units')) |
| 96 | + |
| 97 | + def test_buttons(self): |
| 98 | + self.assertEqual(self.scroll(self.event(EventType.ButtonPress, num=4)), |
| 99 | + ('scroll', -5, 'units')) |
| 100 | + self.assertEqual(self.scroll(self.event(EventType.ButtonPress, num=5)), |
| 101 | + ('scroll', 5, 'units')) |
| 102 | + |
| 103 | + def test_widget_argument(self): |
| 104 | + # A tree label scrolls the canvas, not itself. |
| 105 | + event = self.event(EventType.MouseWheel, 120) |
| 106 | + canvas = mock.Mock() |
| 107 | + self.assertEqual(self.scroll(event, canvas), ('scroll', -5, 'units')) |
| 108 | + event.widget.yview.assert_not_called() |
| 109 | + |
| 110 | + |
| 111 | +class FixTest(unittest.TestCase): |
| 112 | + "Test the fix_ functions, which need a display." |
| 113 | + |
| 114 | + @classmethod |
| 115 | + def setUpClass(cls): |
| 116 | + requires('gui') |
| 117 | + cls.root = tkinter.Tk() |
| 118 | + cls.root.withdraw() |
| 119 | + |
| 120 | + @classmethod |
| 121 | + def tearDownClass(cls): |
| 122 | + cls.root.destroy() |
| 123 | + del cls.root |
| 124 | + |
| 125 | + def test_fix_scaling(self): |
| 126 | + from tkinter import font |
| 127 | + root = self.root |
| 128 | + scaling = root.tk.call('tk', 'scaling') # No Misc.tk_scaling yet. |
| 129 | + self.addCleanup(root.tk.call, 'tk', 'scaling', scaling) |
| 130 | + # Both fonts go with the root; Font.delete_font is a flag. |
| 131 | + pixels = font.Font(root=root, name='TestPixelFont', size=-16) |
| 132 | + points = font.Font(root=root, name='TestPointFont', size=12) |
| 133 | + |
| 134 | + root.tk.call('tk', 'scaling', 1.0) |
| 135 | + util.fix_scaling(root) # No scaling, no change. |
| 136 | + self.assertEqual(int(pixels['size']), -16) |
| 137 | + |
| 138 | + root.tk.call('tk', 'scaling', 2.0) |
| 139 | + util.fix_scaling(root) # A size in pixels becomes one in points. |
| 140 | + self.assertEqual(int(pixels['size']), 12) # round(-0.75 * -16) |
| 141 | + self.assertEqual(int(points['size']), 12) # Points are left alone. |
| 142 | + |
| 143 | + def test_fix_word_breaks(self): |
| 144 | + root = self.root |
| 145 | + util.fix_word_breaks(root) |
| 146 | + self.assertEqual(root.tk.call('set', 'tcl_wordchars'), r'\w') |
| 147 | + self.assertEqual(root.tk.call('set', 'tcl_nonwordchars'), r'\W') |
| 148 | + |
| 149 | + def test_fix_x11_paste(self): |
| 150 | + root = self.root |
| 151 | + classes = 'Text', 'Entry', 'Spinbox' |
| 152 | + before = {cls: root.bind_class(cls, '<<Paste>>') for cls in classes} |
| 153 | + util.fix_x11_paste(root) |
| 154 | + for cls in classes: |
| 155 | + with self.subTest(cls=cls): |
| 156 | + after = root.bind_class(cls, '<<Paste>>') |
| 157 | + if root._windowingsystem == 'x11': |
| 158 | + # Deleting the selection makes paste replace it. |
| 159 | + self.assertEqual( |
| 160 | + after, |
| 161 | + 'catch {%W delete sel.first sel.last}\n' + before[cls]) |
| 162 | + else: |
| 163 | + self.assertEqual(after, before[cls]) |
| 164 | + |
12 | 165 |
|
13 | 166 | if __name__ == '__main__': |
14 | 167 | unittest.main(verbosity=2) |
0 commit comments