ยปCore Development>Code coverage>Lib/tkinter/test/test_ttk/test_style.py

Python code coverage for Lib/tkinter/test/test_ttk/test_style.py

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