|
| 1 | +import time |
| 2 | +import unittest |
| 3 | +import tkinter |
| 4 | +from tkinter import font |
| 5 | +from tkinter.fontchooser import FontChooser |
| 6 | +from test import support |
| 7 | +from test.support import requires |
| 8 | +from test.test_tkinter.support import (AbstractTkTest, |
| 9 | + AbstractDefaultRootTest, |
| 10 | + setUpModule) # noqa: F401 |
| 11 | + |
| 12 | +requires('gui') |
| 13 | + |
| 14 | + |
| 15 | +class FontChooserTest(AbstractTkTest, unittest.TestCase): |
| 16 | + |
| 17 | + def setUp(self): |
| 18 | + super().setUp() |
| 19 | + self.fc = FontChooser(self.root) |
| 20 | + self.addCleanup(self._reset) |
| 21 | + |
| 22 | + def _reset(self): |
| 23 | + # The font dialog is global for the interpreter, so restore |
| 24 | + # a clean state for the next test. |
| 25 | + try: |
| 26 | + self.fc.configure(parent=self.root, title='', command=None) |
| 27 | + self.fc.hide() |
| 28 | + except tkinter.TclError: |
| 29 | + pass |
| 30 | + |
| 31 | + def _visible(self): |
| 32 | + return self.root.tk.getboolean(self.fc.cget('visible')) |
| 33 | + |
| 34 | + def _wait_visibility(self, expected, timeout=None): |
| 35 | + # Bounded wait until the dialog visibility matches expected. |
| 36 | + if timeout is None: |
| 37 | + timeout = support.LOOPBACK_TIMEOUT |
| 38 | + deadline = time.monotonic() + timeout |
| 39 | + while time.monotonic() < deadline: |
| 40 | + self.root.update() |
| 41 | + if self._visible() == expected: |
| 42 | + return True |
| 43 | + time.sleep(0.01) |
| 44 | + return False |
| 45 | + |
| 46 | + def test_configure_query(self): |
| 47 | + options = self.fc.configure() |
| 48 | + self.assertIsInstance(options, dict) |
| 49 | + self.assertLessEqual({'parent', 'title', 'font', 'command', 'visible'}, |
| 50 | + options.keys()) |
| 51 | + |
| 52 | + def test_configure(self): |
| 53 | + self.fc.configure(title='Pick a font') |
| 54 | + if self.root._windowingsystem == 'x11': |
| 55 | + self.assertEqual(self.fc.cget('title'), 'Pick a font') |
| 56 | + self.fc.configure(font='Courier 10') |
| 57 | + self.assertTrue(self.fc.cget('font')) |
| 58 | + if self.root._windowingsystem == 'x11': |
| 59 | + self.assertEqual(str(self.fc.cget('font')), 'Courier 10') |
| 60 | + |
| 61 | + def test_parent(self): |
| 62 | + top = tkinter.Toplevel(self.root) |
| 63 | + self.addCleanup(top.destroy) |
| 64 | + # The constructor does not force -parent to the master: it stays |
| 65 | + # the main window even when the master is another widget. |
| 66 | + FontChooser(top) |
| 67 | + self.assertEqual(self.fc.cget('parent'), str(self.root)) |
| 68 | + # -parent can be set explicitly. |
| 69 | + self.fc.configure(parent=top) |
| 70 | + self.assertEqual(self.fc.cget('parent'), str(top)) |
| 71 | + |
| 72 | + def test_configure_font_instance(self): |
| 73 | + # A Font instance can be passed as the font, both to the |
| 74 | + # constructor and to configure(). |
| 75 | + f = font.Font(self.root, family='Courier', size=14, weight='bold') |
| 76 | + fc = FontChooser(self.root, font=f) |
| 77 | + self.assertEqual(str(fc.cget('font')), str(f)) |
| 78 | + f2 = font.Font(self.root, family='Times', size=11) |
| 79 | + fc.configure(font=f2) |
| 80 | + self.assertEqual(str(fc.cget('font')), str(f2)) |
| 81 | + |
| 82 | + def test_configure_visible_readonly(self): |
| 83 | + with self.assertRaises(tkinter.TclError): |
| 84 | + self.fc.configure(visible=True) |
| 85 | + |
| 86 | + def test_cget_visible(self): |
| 87 | + self.assertFalse(self._visible()) |
| 88 | + |
| 89 | + def test_command(self): |
| 90 | + result = [] |
| 91 | + self.fc.configure(command=result.append) |
| 92 | + name = self.fc._command_name |
| 93 | + self.assertTrue(name) |
| 94 | + # The Tcl command is named after the wrapped callback. |
| 95 | + self.assertTrue(name.endswith('append'), name) |
| 96 | + # The callback receives a Font wrapping the selected font. |
| 97 | + self.root.tk.call(name, 'Courier 10') |
| 98 | + self.assertEqual(len(result), 1) |
| 99 | + selected = result[0] |
| 100 | + self.assertIsInstance(selected, font.Font) |
| 101 | + # The description is wrapped without creating a named font. |
| 102 | + self.assertEqual(str(selected), 'Courier 10') |
| 103 | + self.assertFalse(selected.delete_font) |
| 104 | + self.assertEqual(selected.actual('size'), 10) |
| 105 | + # Replacing the callback deletes the old Tcl command. |
| 106 | + self.fc.configure(command=lambda font: None) |
| 107 | + self.assertNotEqual(self.fc._command_name, name) |
| 108 | + self.assertFalse(self.root.tk.call('info', 'commands', name)) |
| 109 | + # Removing the callback deletes the Tcl command. |
| 110 | + name = self.fc._command_name |
| 111 | + self.fc.configure(command=None) |
| 112 | + self.assertIsNone(self.fc._command_name) |
| 113 | + self.assertFalse(self.root.tk.call('info', 'commands', name)) |
| 114 | + self.assertEqual(self.fc.cget('command'), '') |
| 115 | + |
| 116 | + def test_show_hide(self): |
| 117 | + if self.root._windowingsystem != 'x11': |
| 118 | + self.skipTest('cannot safely drive the native font dialog') |
| 119 | + events = [] |
| 120 | + self.root.bind('<<TkFontchooserVisibility>>', events.append) |
| 121 | + self.fc.show() |
| 122 | + if not self._wait_visibility(True): |
| 123 | + self.skipTest('the font dialog was not mapped') |
| 124 | + self.fc.hide() |
| 125 | + self.assertTrue(self._wait_visibility(False)) |
| 126 | + self.assertTrue(events) |
| 127 | + |
| 128 | + |
| 129 | +class DefaultRootTest(AbstractDefaultRootTest, unittest.TestCase): |
| 130 | + |
| 131 | + def test_fontchooser(self): |
| 132 | + root = tkinter.Tk() |
| 133 | + fc = FontChooser() |
| 134 | + self.assertIs(fc.master, root) |
| 135 | + root.destroy() |
| 136 | + tkinter.NoDefaultRoot() |
| 137 | + self.assertRaises(RuntimeError, FontChooser) |
| 138 | + |
| 139 | + |
| 140 | +if __name__ == "__main__": |
| 141 | + unittest.main() |
0 commit comments