Home | History | Annotate | Download | only in test_ttk
      1 import unittest
      2 import Tkinter
      3 import ttk
      4 from test.test_support import requires, run_unittest
      5 
      6 import support
      7 
      8 requires('gui')
      9 
     10 class StyleTest(unittest.TestCase):
     11 
     12     def setUp(self):
     13         self.style = ttk.Style()
     14 
     15 
     16     def test_configure(self):
     17         style = self.style
     18         style.configure('TButton', background='yellow')
     19         self.assertEqual(style.configure('TButton', 'background'),
     20             'yellow')
     21         self.assertTrue(isinstance(style.configure('TButton'), dict))
     22 
     23 
     24     def test_map(self):
     25         style = self.style
     26         style.map('TButton', background=[('active', 'background', 'blue')])
     27         self.assertEqual(style.map('TButton', 'background'),
     28             [('active', 'background', 'blue')])
     29         self.assertTrue(isinstance(style.map('TButton'), dict))
     30 
     31 
     32     def test_lookup(self):
     33         style = self.style
     34         style.configure('TButton', background='yellow')
     35         style.map('TButton', background=[('active', 'background', 'blue')])
     36 
     37         self.assertEqual(style.lookup('TButton', 'background'), 'yellow')
     38         self.assertEqual(style.lookup('TButton', 'background',
     39             ['active', 'background']), 'blue')
     40         self.assertEqual(style.lookup('TButton', 'optionnotdefined',
     41             default='iknewit'), 'iknewit')
     42 
     43 
     44     def test_layout(self):
     45         style = self.style
     46         self.assertRaises(Tkinter.TclError, style.layout, 'NotALayout')
     47         tv_style = style.layout('Treeview')
     48 
     49         # "erase" Treeview layout
     50         style.layout('Treeview', '')
     51         self.assertEqual(style.layout('Treeview'),
     52             [('null', {'sticky': 'nswe'})]
     53         )
     54 
     55         # restore layout
     56         style.layout('Treeview', tv_style)
     57         self.assertEqual(style.layout('Treeview'), tv_style)
     58 
     59         # should return a list
     60         self.assertTrue(isinstance(style.layout('TButton'), list))
     61 
     62         # correct layout, but "option" doesn't exist as option
     63         self.assertRaises(Tkinter.TclError, style.layout, 'Treeview',
     64             [('name', {'option': 'inexistent'})])
     65 
     66 
     67     def test_theme_use(self):
     68         self.assertRaises(Tkinter.TclError, self.style.theme_use,
     69             'nonexistingname')
     70 
     71         curr_theme = self.style.theme_use()
     72         new_theme = None
     73         for theme in self.style.theme_names():
     74             if theme != curr_theme:
     75                 new_theme = theme
     76                 self.style.theme_use(theme)
     77                 break
     78         else:
     79             # just one theme available, can't go on with tests
     80             return
     81 
     82         self.assertFalse(curr_theme == new_theme)
     83         self.assertFalse(new_theme != self.style.theme_use())
     84 
     85         self.style.theme_use(curr_theme)
     86 
     87 
     88 tests_gui = (StyleTest, )
     89 
     90 if __name__ == "__main__":
     91     run_unittest(*tests_gui)
     92