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