summaryrefslogtreecommitdiffstats
path: root/Lib/tkinter/test/test_ttk
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2020-11-22 20:48:52 (GMT)
committerGitHub <noreply@github.com>2020-11-22 20:48:52 (GMT)
commitdd844a2916fb3a8f481ec7c732802c13c3375691 (patch)
treed276bed95a59baa88d2ac047972112349f2fd2c8 /Lib/tkinter/test/test_ttk
parent313467efdc23a1ec8662b77d2001642be598f80a (diff)
downloadcpython-dd844a2916fb3a8f481ec7c732802c13c3375691.zip
cpython-dd844a2916fb3a8f481ec7c732802c13c3375691.tar.gz
cpython-dd844a2916fb3a8f481ec7c732802c13c3375691.tar.bz2
bpo-42328: Fix tkinter.ttk.Style.map(). (GH-23300)
The function accepts now the representation of the default state as empty sequence (as returned by Style.map()). The structure of the result is now the same on all platform and does not depend on the value of wantobjects.
Diffstat (limited to 'Lib/tkinter/test/test_ttk')
-rw-r--r--Lib/tkinter/test/test_ttk/test_functions.py7
-rw-r--r--Lib/tkinter/test/test_ttk/test_style.py90
2 files changed, 88 insertions, 9 deletions
diff --git a/Lib/tkinter/test/test_ttk/test_functions.py b/Lib/tkinter/test/test_ttk/test_functions.py
index f8e69a9..5c23d6f 100644
--- a/Lib/tkinter/test/test_ttk/test_functions.py
+++ b/Lib/tkinter/test/test_ttk/test_functions.py
@@ -137,6 +137,9 @@ class InternalFunctionsTest(unittest.TestCase):
result = ttk._format_mapdict(opts)
self.assertEqual(result, ('-üñíćódè', 'á vãl'))
+ self.assertEqual(ttk._format_mapdict({'opt': [('value',)]}),
+ ('-opt', '{} value'))
+
# empty states
valid = {'opt': [('', '', 'hi')]}
self.assertEqual(ttk._format_mapdict(valid), ('-opt', '{ } hi'))
@@ -159,10 +162,6 @@ class InternalFunctionsTest(unittest.TestCase):
opts = {'a': None}
self.assertRaises(TypeError, ttk._format_mapdict, opts)
- # items in the value must have size >= 2
- self.assertRaises(IndexError, ttk._format_mapdict,
- {'a': [('invalid', )]})
-
def test_format_elemcreate(self):
self.assertTrue(ttk._format_elemcreate(None), (None, ()))
diff --git a/Lib/tkinter/test/test_ttk/test_style.py b/Lib/tkinter/test/test_ttk/test_style.py
index 3537536..54e9133 100644
--- a/Lib/tkinter/test/test_ttk/test_style.py
+++ b/Lib/tkinter/test/test_ttk/test_style.py
@@ -1,11 +1,22 @@
import unittest
import tkinter
from tkinter import ttk
+from test import support
from test.support import requires, run_unittest
from tkinter.test.support import AbstractTkTest
requires('gui')
+CLASS_NAMES = [
+ '.', 'ComboboxPopdownFrame', 'Heading',
+ 'Horizontal.TProgressbar', 'Horizontal.TScale', 'Item', 'Sash',
+ 'TButton', 'TCheckbutton', 'TCombobox', 'TEntry',
+ 'TLabelframe', 'TLabelframe.Label', 'TMenubutton',
+ 'TNotebook', 'TNotebook.Tab', 'Toolbutton', 'TProgressbar',
+ 'TRadiobutton', 'Treeview', 'TScale', 'TScrollbar', 'TSpinbox',
+ 'Vertical.TProgressbar', 'Vertical.TScale'
+]
+
class StyleTest(AbstractTkTest, unittest.TestCase):
def setUp(self):
@@ -23,11 +34,36 @@ class StyleTest(AbstractTkTest, unittest.TestCase):
def test_map(self):
style = self.style
- style.map('TButton', background=[('active', 'background', 'blue')])
- self.assertEqual(style.map('TButton', 'background'),
- [('active', 'background', 'blue')] if self.wantobjects else
- [('active background', 'blue')])
- self.assertIsInstance(style.map('TButton'), dict)
+
+ # Single state
+ for states in ['active'], [('active',)]:
+ with self.subTest(states=states):
+ style.map('TButton', background=[(*states, 'white')])
+ expected = [('active', 'white')]
+ self.assertEqual(style.map('TButton', 'background'), expected)
+ m = style.map('TButton')
+ self.assertIsInstance(m, dict)
+ self.assertEqual(m['background'], expected)
+
+ # Multiple states
+ for states in ['pressed', '!disabled'], ['pressed !disabled'], [('pressed', '!disabled')]:
+ with self.subTest(states=states):
+ style.map('TButton', background=[(*states, 'black')])
+ expected = [('pressed', '!disabled', 'black')]
+ self.assertEqual(style.map('TButton', 'background'), expected)
+ m = style.map('TButton')
+ self.assertIsInstance(m, dict)
+ self.assertEqual(m['background'], expected)
+
+ # Default state
+ for states in [], [''], [()]:
+ with self.subTest(states=states):
+ style.map('TButton', background=[(*states, 'grey')])
+ expected = [('grey',)]
+ self.assertEqual(style.map('TButton', 'background'), expected)
+ m = style.map('TButton')
+ self.assertIsInstance(m, dict)
+ self.assertEqual(m['background'], expected)
def test_lookup(self):
@@ -86,6 +122,50 @@ class StyleTest(AbstractTkTest, unittest.TestCase):
self.style.theme_use(curr_theme)
+ def test_configure_custom_copy(self):
+ style = self.style
+
+ curr_theme = self.style.theme_use()
+ self.addCleanup(self.style.theme_use, curr_theme)
+ for theme in self.style.theme_names():
+ self.style.theme_use(theme)
+ for name in CLASS_NAMES:
+ default = style.configure(name)
+ if not default:
+ continue
+ with self.subTest(theme=theme, name=name):
+ if support.verbose >= 2:
+ print('configure', theme, name, default)
+ newname = f'C.{name}'
+ self.assertEqual(style.configure(newname), None)
+ style.configure(newname, **default)
+ self.assertEqual(style.configure(newname), default)
+ for key, value in default.items():
+ self.assertEqual(style.configure(newname, key), value)
+
+
+ def test_map_custom_copy(self):
+ style = self.style
+
+ curr_theme = self.style.theme_use()
+ self.addCleanup(self.style.theme_use, curr_theme)
+ for theme in self.style.theme_names():
+ self.style.theme_use(theme)
+ for name in CLASS_NAMES:
+ default = style.map(name)
+ if not default:
+ continue
+ with self.subTest(theme=theme, name=name):
+ if support.verbose >= 2:
+ print('map', theme, name, default)
+ newname = f'C.{name}'
+ self.assertEqual(style.map(newname), {})
+ style.map(newname, **default)
+ self.assertEqual(style.map(newname), default)
+ for key, value in default.items():
+ self.assertEqual(style.map(newname, key), value)
+
+
tests_gui = (StyleTest, )
if __name__ == "__main__":