1import unittest 2import tkinter 3from tkinter import font 4from test.support import requires, run_unittest, gc_collect, ALWAYS_EQ 5from tkinter.test.support import AbstractTkTest, AbstractDefaultRootTest 6 7requires('gui') 8 9fontname = "TkDefaultFont" 10 11class FontTest(AbstractTkTest, unittest.TestCase): 12 13 @classmethod 14 def setUpClass(cls): 15 AbstractTkTest.setUpClass.__func__(cls) 16 try: 17 cls.font = font.Font(root=cls.root, name=fontname, exists=True) 18 except tkinter.TclError: 19 cls.font = font.Font(root=cls.root, name=fontname, exists=False) 20 21 def test_configure(self): 22 options = self.font.configure() 23 self.assertGreaterEqual(set(options), 24 {'family', 'size', 'weight', 'slant', 'underline', 'overstrike'}) 25 for key in options: 26 self.assertEqual(self.font.cget(key), options[key]) 27 self.assertEqual(self.font[key], options[key]) 28 for key in 'family', 'weight', 'slant': 29 self.assertIsInstance(options[key], str) 30 self.assertIsInstance(self.font.cget(key), str) 31 self.assertIsInstance(self.font[key], str) 32 sizetype = int if self.wantobjects else str 33 for key in 'size', 'underline', 'overstrike': 34 self.assertIsInstance(options[key], sizetype) 35 self.assertIsInstance(self.font.cget(key), sizetype) 36 self.assertIsInstance(self.font[key], sizetype) 37 38 def test_unicode_family(self): 39 family = 'MS \u30b4\u30b7\u30c3\u30af' 40 try: 41 f = font.Font(root=self.root, family=family, exists=True) 42 except tkinter.TclError: 43 f = font.Font(root=self.root, family=family, exists=False) 44 self.assertEqual(f.cget('family'), family) 45 del f 46 gc_collect() 47 48 def test_actual(self): 49 options = self.font.actual() 50 self.assertGreaterEqual(set(options), 51 {'family', 'size', 'weight', 'slant', 'underline', 'overstrike'}) 52 for key in options: 53 self.assertEqual(self.font.actual(key), options[key]) 54 for key in 'family', 'weight', 'slant': 55 self.assertIsInstance(options[key], str) 56 self.assertIsInstance(self.font.actual(key), str) 57 sizetype = int if self.wantobjects else str 58 for key in 'size', 'underline', 'overstrike': 59 self.assertIsInstance(options[key], sizetype) 60 self.assertIsInstance(self.font.actual(key), sizetype) 61 62 def test_name(self): 63 self.assertEqual(self.font.name, fontname) 64 self.assertEqual(str(self.font), fontname) 65 66 def test_equality(self): 67 font1 = font.Font(root=self.root, name=fontname, exists=True) 68 font2 = font.Font(root=self.root, name=fontname, exists=True) 69 self.assertIsNot(font1, font2) 70 self.assertEqual(font1, font2) 71 self.assertNotEqual(font1, font1.copy()) 72 73 self.assertNotEqual(font1, 0) 74 self.assertEqual(font1, ALWAYS_EQ) 75 76 root2 = tkinter.Tk() 77 self.addCleanup(root2.destroy) 78 font3 = font.Font(root=root2, name=fontname, exists=True) 79 self.assertEqual(str(font1), str(font3)) 80 self.assertNotEqual(font1, font3) 81 82 def test_measure(self): 83 self.assertIsInstance(self.font.measure('abc'), int) 84 85 def test_metrics(self): 86 metrics = self.font.metrics() 87 self.assertGreaterEqual(set(metrics), 88 {'ascent', 'descent', 'linespace', 'fixed'}) 89 for key in metrics: 90 self.assertEqual(self.font.metrics(key), metrics[key]) 91 self.assertIsInstance(metrics[key], int) 92 self.assertIsInstance(self.font.metrics(key), int) 93 94 def test_families(self): 95 families = font.families(self.root) 96 self.assertIsInstance(families, tuple) 97 self.assertTrue(families) 98 for family in families: 99 self.assertIsInstance(family, str) 100 self.assertTrue(family) 101 102 def test_names(self): 103 names = font.names(self.root) 104 self.assertIsInstance(names, tuple) 105 self.assertTrue(names) 106 for name in names: 107 self.assertIsInstance(name, str) 108 self.assertTrue(name) 109 self.assertIn(fontname, names) 110 111 112class DefaultRootTest(AbstractDefaultRootTest, unittest.TestCase): 113 114 def test_families(self): 115 self.assertRaises(RuntimeError, font.families) 116 root = tkinter.Tk() 117 families = font.families() 118 self.assertIsInstance(families, tuple) 119 self.assertTrue(families) 120 for family in families: 121 self.assertIsInstance(family, str) 122 self.assertTrue(family) 123 root.destroy() 124 tkinter.NoDefaultRoot() 125 self.assertRaises(RuntimeError, font.families) 126 127 def test_names(self): 128 self.assertRaises(RuntimeError, font.names) 129 root = tkinter.Tk() 130 names = font.names() 131 self.assertIsInstance(names, tuple) 132 self.assertTrue(names) 133 for name in names: 134 self.assertIsInstance(name, str) 135 self.assertTrue(name) 136 self.assertIn(fontname, names) 137 root.destroy() 138 tkinter.NoDefaultRoot() 139 self.assertRaises(RuntimeError, font.names) 140 141 142tests_gui = (FontTest, DefaultRootTest) 143 144if __name__ == "__main__": 145 run_unittest(*tests_gui) 146