• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import unittest
2import tkinter
3from tkinter import font
4from test.support import requires, 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    def test_nametofont(self):
112        testfont = font.nametofont(fontname, root=self.root)
113        self.assertIsInstance(testfont, font.Font)
114        self.assertEqual(testfont.name, fontname)
115
116    def test_repr(self):
117        self.assertEqual(
118            repr(self.font), f'<tkinter.font.Font object {fontname!r}>'
119        )
120
121
122class DefaultRootTest(AbstractDefaultRootTest, unittest.TestCase):
123
124    def test_families(self):
125        self.assertRaises(RuntimeError, font.families)
126        root = tkinter.Tk()
127        families = font.families()
128        self.assertIsInstance(families, tuple)
129        self.assertTrue(families)
130        for family in families:
131            self.assertIsInstance(family, str)
132            self.assertTrue(family)
133        root.destroy()
134        tkinter.NoDefaultRoot()
135        self.assertRaises(RuntimeError, font.families)
136
137    def test_names(self):
138        self.assertRaises(RuntimeError, font.names)
139        root = tkinter.Tk()
140        names = font.names()
141        self.assertIsInstance(names, tuple)
142        self.assertTrue(names)
143        for name in names:
144            self.assertIsInstance(name, str)
145            self.assertTrue(name)
146        self.assertIn(fontname, names)
147        root.destroy()
148        tkinter.NoDefaultRoot()
149        self.assertRaises(RuntimeError, font.names)
150
151    def test_nametofont(self):
152        self.assertRaises(RuntimeError, font.nametofont, fontname)
153        root = tkinter.Tk()
154        testfont = font.nametofont(fontname)
155        self.assertIsInstance(testfont, font.Font)
156        self.assertEqual(testfont.name, fontname)
157        root.destroy()
158        tkinter.NoDefaultRoot()
159        self.assertRaises(RuntimeError, font.nametofont, fontname)
160
161
162if __name__ == "__main__":
163    unittest.main()
164