Home | History | Annotate | Download | only in test_tkinter
      1 import unittest
      2 import tkinter
      3 from tkinter import font
      4 from test.support import requires, run_unittest, gc_collect
      5 from tkinter.test.support import AbstractTkTest
      6 
      7 requires('gui')
      8 
      9 fontname = "TkDefaultFont"
     10 
     11 class 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_eq(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         self.assertNotEqual(font1, 0)
     73 
     74     def test_measure(self):
     75         self.assertIsInstance(self.font.measure('abc'), int)
     76 
     77     def test_metrics(self):
     78         metrics = self.font.metrics()
     79         self.assertGreaterEqual(set(metrics),
     80             {'ascent', 'descent', 'linespace', 'fixed'})
     81         for key in metrics:
     82             self.assertEqual(self.font.metrics(key), metrics[key])
     83             self.assertIsInstance(metrics[key], int)
     84             self.assertIsInstance(self.font.metrics(key), int)
     85 
     86     def test_families(self):
     87         families = font.families(self.root)
     88         self.assertIsInstance(families, tuple)
     89         self.assertTrue(families)
     90         for family in families:
     91             self.assertIsInstance(family, str)
     92             self.assertTrue(family)
     93 
     94     def test_names(self):
     95         names = font.names(self.root)
     96         self.assertIsInstance(names, tuple)
     97         self.assertTrue(names)
     98         for name in names:
     99             self.assertIsInstance(name, str)
    100             self.assertTrue(name)
    101         self.assertIn(fontname, names)
    102 
    103 tests_gui = (FontTest, )
    104 
    105 if __name__ == "__main__":
    106     run_unittest(*tests_gui)
    107