Skip to content

Commit 20ad6b6

Browse files
[3.14] gh-154357: Fix tkinter, ttk and IDLE tests depending on the window manager (GH-154370)
The window manager can take the focus from the application, ignore lift() and resize a toplevel on its own. * Hide the root window in the dialog tests, so that it does not compete for the focus. * Take the focus right before generating a key event. * Tolerate additional focus events. * Do not check focus_get() and focus_displayof() without the focus. * Resize the toplevel to fit its content in wait_until_mapped(). (cherry picked from commit 3b56438)
1 parent a9152fb commit 20ad6b6

6 files changed

Lines changed: 43 additions & 16 deletions

File tree

Lib/idlelib/idle_test/test_configdialog.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ def test_fontlist_key(self):
150150
font = d.fontlist.get('active')
151151

152152
# Test Down key.
153-
fontlist.focus_force()
154153
fontlist.update()
154+
fontlist.focus_force()
155155
fontlist.event_generate('<Key-Down>')
156156
fontlist.event_generate('<KeyRelease-Down>')
157157

@@ -160,8 +160,8 @@ def test_fontlist_key(self):
160160
self.assertIn(d.font_name.get(), down_font.lower())
161161

162162
# Test Up key.
163-
fontlist.focus_force()
164163
fontlist.update()
164+
fontlist.focus_force()
165165
fontlist.event_generate('<Key-Up>')
166166
fontlist.event_generate('<KeyRelease-Up>')
167167

Lib/test/test_tkinter/support.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ def require_mapped(self, widget, timeout=None):
6161
f'(timed out after {timeout:g}s)')
6262

6363

64+
class AbstractDialogTest(AbstractTkTest):
65+
# Tk delivers generated keyboard events to the focused window. Hide the
66+
# root window, otherwise the window manager can take the focus back from
67+
# the dialog (gh-154357).
68+
69+
def setUp(self):
70+
super().setUp()
71+
self.root.withdraw()
72+
73+
6474
class AbstractDefaultRootTest:
6575

6676
def setUp(self):
@@ -112,6 +122,7 @@ def wait_until_mapped(widget, timeout=None, *, full_size=False):
112122
timeout = support.LOOPBACK_TIMEOUT
113123
deadline = time.monotonic() + timeout
114124
widget.update_idletasks()
125+
reset = False
115126
while True:
116127
widget.update() # drain pending Map/Configure events
117128
if widget.winfo_ismapped():
@@ -123,6 +134,11 @@ def wait_until_mapped(widget, timeout=None, *, full_size=False):
123134
h_ok = widget.winfo_height() > 1
124135
if w_ok and h_ok:
125136
return True
137+
if full_size and not reset:
138+
# Tk no longer resizes the toplevel to fit its content if
139+
# the window manager has resized it. Undo this.
140+
widget.winfo_toplevel().wm_geometry('')
141+
reset = True
126142
if time.monotonic() >= deadline:
127143
return False
128144
time.sleep(0.01)

Lib/test/test_tkinter/test_filedialog.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from tkinter.commondialog import Dialog
55
from test.support import requires, swap_attr
66
from test.test_tkinter.support import setUpModule # noqa: F401
7-
from test.test_tkinter.support import AbstractTkTest
7+
from test.test_tkinter.support import AbstractDialogTest, AbstractTkTest
88

99
requires('gui')
1010

@@ -37,7 +37,7 @@ def test_directory(self):
3737
self.check(filedialog.Directory, 'tk_chooseDirectory')
3838

3939

40-
class FileDialogTest(AbstractTkTest, unittest.TestCase):
40+
class FileDialogTest(AbstractDialogTest, unittest.TestCase):
4141
# The pure-Python FileDialog runs its own modal loop in go(); its logic is
4242
# exercised here without entering the loop.
4343

Lib/test/test_tkinter/test_misc.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -451,15 +451,20 @@ def test_focus_methods(self):
451451
self.root.update_idletasks()
452452
f.focus_force()
453453
self.root.update()
454-
self.assertIs(self.root.focus_get(), f)
455-
self.assertIs(self.root.focus_displayof(), f)
454+
# The window manager can take the focus away, and then focus_get()
455+
# and focus_displayof() return None.
456+
if self.root.focus_displayof() is not None:
457+
self.assertIs(self.root.focus_get(), f)
458+
self.assertIs(self.root.focus_displayof(), f)
456459
self.assertIs(f.focus_lastfor(), f)
457460
b = tkinter.Button(f)
458461
b.pack()
459462
self.root.update()
460463
b.focus_set()
461464
self.root.update()
462-
self.assertIs(self.root.focus_get(), b)
465+
if self.root.focus_displayof() is not None:
466+
self.assertIs(self.root.focus_get(), b)
467+
self.assertIs(f.focus_lastfor(), b)
463468

464469
def test_focus_methods_unresolvable(self):
465470
# The focus may be on a widget that tkinter did not create and so
@@ -1164,7 +1169,9 @@ def test_focus(self):
11641169

11651170
f.focus_force()
11661171
self.root.update()
1167-
self.assertEqual(len(events), 1, events)
1172+
# The window manager can take the focus away and give it back,
1173+
# which makes Tk generate additional focus events.
1174+
self.assertGreaterEqual(len(events), 1, events)
11681175
e = events[0]
11691176
self.assertIs(e.type, tkinter.EventType.FocusIn)
11701177
self.assertIs(e.widget, f)

Lib/test/test_tkinter/test_simpledialog.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
from tkinter import messagebox
44
from test.support import requires, swap_attr
55
from test.test_tkinter.support import setUpModule # noqa: F401
6-
from test.test_tkinter.support import AbstractDefaultRootTest, AbstractTkTest
6+
from test.test_tkinter.support import (AbstractDefaultRootTest,
7+
AbstractDialogTest)
78
from tkinter.simpledialog import (Dialog, SimpleDialog,
89
askinteger, askfloat, askstring,
910
_QueryInteger, _QueryFloat, _QueryString)
1011

1112
requires('gui')
1213

1314

14-
class SimpleDialogTest(AbstractTkTest, unittest.TestCase):
15+
class SimpleDialogTest(AbstractDialogTest, unittest.TestCase):
1516
# SimpleDialog's modal loop is in go(); its bindings are exercised here by
1617
# generating events on the constructed dialog, without entering the loop.
1718

@@ -50,8 +51,8 @@ def test_return_activates_default(self):
5051
# <Return> invokes the default button.
5152
d = self.create() # default 0
5253
self.require_mapped(d.root)
53-
d.root.focus_force()
5454
d.root.update()
55+
d.root.focus_force()
5556
d.root.event_generate('<Return>')
5657
d.root.update()
5758
self.assertEqual(d.num, 0)
@@ -61,10 +62,9 @@ def test_return_no_default(self):
6162
# open instead of activating a button.
6263
d = self.create(default=None)
6364
self.require_mapped(d.root)
64-
d.root.focus_force()
65-
d.root.update()
6665
bells = []
6766
with swap_attr(d.root, 'bell', lambda *a, **k: bells.append(True)):
67+
d.root.focus_force()
6868
d.root.event_generate('<Return>')
6969
d.root.update()
7070
self.assertTrue(bells) # rang the bell
@@ -98,7 +98,7 @@ def test_go(self):
9898
self.assertEqual(d.go(), 0)
9999

100100

101-
class DialogTest(AbstractTkTest, unittest.TestCase):
101+
class DialogTest(AbstractDialogTest, unittest.TestCase):
102102
# Dialog is a base class for custom dialogs; exercise it via _QueryInteger.
103103

104104
def open(self, **kw):
@@ -167,7 +167,7 @@ def mock_wait_window(w):
167167
self.assertRaises(RuntimeError, askinteger, "Go To Line", "Line number")
168168

169169

170-
class QueryDialogTest(AbstractTkTest, unittest.TestCase):
170+
class QueryDialogTest(AbstractDialogTest, unittest.TestCase):
171171
# The query dialogs are modal: their __init__ blocks in wait_window().
172172
# Mock that out so the dialog stays alive and can be driven with generated
173173
# events, exercising the <Return>/<Escape> bindings and the validation.

Lib/test/test_ttk/test_widgets.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2030,19 +2030,23 @@ def test_virtual_events(self):
20302030
lambda e: selects.append(self.tv.selection()))
20312031
self.tv.bind('<<TreeviewOpen>>', lambda e: opens.append(self.tv.focus()))
20322032
self.tv.bind('<<TreeviewClose>>', lambda e: closes.append(self.tv.focus()))
2033-
self.tv.focus_force()
20342033
self.tv.focus(parent)
20352034
self.tv.selection_set(parent)
20362035
self.tv.update()
20372036

2037+
# Force the focus right before generating the event: the window
2038+
# manager can take it back.
2039+
self.tv.focus_force()
20382040
self.tv.event_generate('<Right>') # Open the focused parent.
20392041
self.tv.update()
20402042
self.assertEqual(opens, [parent])
20412043

2044+
self.tv.focus_force()
20422045
self.tv.event_generate('<Left>') # Close it again.
20432046
self.tv.update()
20442047
self.assertEqual(closes, [parent])
20452048

2049+
self.tv.focus_force()
20462050
self.tv.event_generate('<Down>') # Move the selection.
20472051
self.tv.update()
20482052
self.assertEqual(self.tv.selection(), (item2,))

0 commit comments

Comments
 (0)