Commit d00aff2f authored by Serhiy Storchaka's avatar Serhiy Storchaka

Issue #22236: Tkinter tests now don't reuse default root window. New root

window is created for every test class.

Fixed Tkinter images copying operations in NoDefaultRoot mode.

Tcl command names generated for "after" callbacks now contains a name of
original function.
parent ee558260
......@@ -6,7 +6,7 @@ from test import support
support.import_module('_tkinter')
# Make sure tkinter._fix runs to set up the environment
support.import_fresh_module('tkinter')
tkinter = support.import_fresh_module('tkinter')
# Skip test if tk cannot be initialized.
support.requires('gui')
......@@ -14,20 +14,24 @@ support.requires('gui')
from _tkinter import TclError
from tkinter import ttk
from tkinter.test import runtktests
from tkinter.test.support import get_tk_root
root = None
try:
ttk.Button()
root = tkinter.Tk()
button = ttk.Button(root)
button.destroy()
del button
except TclError as msg:
# assuming ttk is not available
raise unittest.SkipTest("ttk not available: %s" % msg)
finally:
if root is not None:
root.destroy()
del root
def test_main():
try:
support.run_unittest(
*runtktests.get_tests(text=False, packages=['test_ttk']))
finally:
get_tk_root().destroy()
support.run_unittest(
*runtktests.get_tests(text=False, packages=['test_ttk']))
if __name__ == '__main__':
test_main()
......@@ -562,6 +562,7 @@ class Misc:
self.deletecommand(name)
except TclError:
pass
callit.__name__ = func.__name__
name = self._register(callit)
return self.tk.call('after', ms, name)
def after_idle(self, func, *args):
......@@ -3351,7 +3352,7 @@ class Image:
master = _default_root
if not master:
raise RuntimeError('Too early to create image')
self.tk = master.tk
self.tk = getattr(master, 'tk', master)
if not name:
Image._last_id += 1
name = "pyimage%r" % (Image._last_id,) # tk itself would use image<x>
......@@ -3422,20 +3423,20 @@ class PhotoImage(Image):
# XXX copy -from, -to, ...?
def copy(self):
"""Return a new PhotoImage with the same image as this widget."""
destImage = PhotoImage()
destImage = PhotoImage(master=self.tk)
self.tk.call(destImage, 'copy', self.name)
return destImage
def zoom(self,x,y=''):
"""Return a new PhotoImage with the same image as this widget
but zoom it with X and Y."""
destImage = PhotoImage()
destImage = PhotoImage(master=self.tk)
if y=='': y=x
self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
return destImage
def subsample(self,x,y=''):
"""Return a new PhotoImage based on the same image as this widget
but use only every Xth or Yth pixel."""
destImage = PhotoImage()
destImage = PhotoImage(master=self.tk)
if y=='': y=x
self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
return destImage
......
......@@ -3,30 +3,43 @@ import tkinter
import unittest
from test.support import requires
def get_tk_root():
requires('gui') # raise exception if tk unavailable
try:
root = tkinter._default_root
except AttributeError:
# it is possible to disable default root in Tkinter, although
# I haven't seen people doing it (but apparently someone did it
# here).
root = None
class AbstractTkTest:
if root is None:
# create a new master only if there isn't one already
root = tkinter.Tk()
@classmethod
def setUpClass(cls):
cls._old_support_default_root = tkinter._support_default_root
destroy_default_root()
tkinter.NoDefaultRoot()
cls.root = tkinter.Tk()
cls.wantobjects = cls.root.wantobjects()
# De-maximize main window.
# Some window managers can maximize new windows.
cls.root.wm_state('normal')
try:
cls.root.wm_attributes('-zoomed', False)
except tkinter.TclError:
pass
return root
@classmethod
def tearDownClass(cls):
cls.root.destroy()
cls.root = None
tkinter._default_root = None
tkinter._support_default_root = cls._old_support_default_root
def root_deiconify():
root = get_tk_root()
root.deiconify()
def setUp(self):
self.root.deiconify()
def root_withdraw():
root = get_tk_root()
root.withdraw()
def tearDown(self):
for w in self.root.winfo_children():
w.destroy()
self.root.withdraw()
def destroy_default_root():
if getattr(tkinter, '_default_root', None):
tkinter._default_root.update_idletasks()
tkinter._default_root.destroy()
tkinter._default_root = None
def simulate_mouse_click(widget, x, y):
"""Generate proper events to click at the x, y position (tries to act
......
......@@ -2,26 +2,20 @@ import unittest
import tkinter
from tkinter import font
from test.support import requires, run_unittest
import tkinter.test.support as support
from tkinter.test.support import AbstractTkTest
requires('gui')
class FontTest(unittest.TestCase):
def setUp(self):
support.root_deiconify()
def tearDown(self):
support.root_withdraw()
class FontTest(AbstractTkTest, unittest.TestCase):
def test_font_eq(self):
fontname = "TkDefaultFont"
try:
f = font.Font(name=fontname, exists=True)
f = font.Font(root=self.root, name=fontname, exists=True)
except tkinter._tkinter.TclError:
f = font.Font(name=fontname, exists=False)
font1 = font.nametofont(fontname)
font2 = font.nametofont(fontname)
f = font.Font(root=self.root, name=fontname, exists=False)
font1 = font.Font(root=self.root, name=fontname, exists=True)
font2 = font.Font(root=self.root, name=fontname, exists=True)
self.assertIsNot(font1, font2)
self.assertEqual(font1, font2)
self.assertNotEqual(font1, font1.copy())
......
......@@ -278,7 +278,7 @@ class PlaceTest(AbstractWidgetTest, unittest.TestCase):
def create2(self):
t = tkinter.Toplevel(self.root, width=300, height=200, bd=0)
t.wm_geometry('+0+0')
t.wm_geometry('300x200+0+0')
f = tkinter.Frame(t, width=154, height=84, bd=2, relief='raised')
f.place_configure(x=48, y=38)
f2 = tkinter.Frame(t, width=30, height=60, bd=2, relief='raised')
......@@ -479,7 +479,6 @@ class PlaceTest(AbstractWidgetTest, unittest.TestCase):
class GridTest(AbstractWidgetTest, unittest.TestCase):
def tearDown(self):
super().tearDown()
cols, rows = self.root.grid_size()
for i in range(cols + 1):
self.root.grid_columnconfigure(i, weight=0, minsize=0, pad=0, uniform='')
......@@ -488,10 +487,10 @@ class GridTest(AbstractWidgetTest, unittest.TestCase):
self.root.grid_propagate(1)
if tcl_version >= (8, 5):
self.root.grid_anchor('nw')
super().tearDown()
def test_grid_configure(self):
b = tkinter.Button(self.root)
self.addCleanup(b.destroy)
self.assertEqual(b.grid_info(), {})
b.grid_configure()
self.assertEqual(b.grid_info()['in'], self.root)
......@@ -578,7 +577,6 @@ class GridTest(AbstractWidgetTest, unittest.TestCase):
def test_grid_configure_row(self):
b = tkinter.Button(self.root)
self.addCleanup(b.destroy)
with self.assertRaisesRegex(TclError, 'bad (row|grid) value "-1": '
'must be a non-negative integer'):
b.grid_configure(row=-1)
......@@ -795,7 +793,7 @@ class GridTest(AbstractWidgetTest, unittest.TestCase):
self.root.grid_bbox(0, 0, 0, 'x')
with self.assertRaises(TypeError):
self.root.grid_bbox(0, 0, 0, 0, 0)
t = tkinter.Toplevel(self.root)
t = self.root
# de-maximize
t.wm_geometry('1x1+0+0')
t.wm_geometry('')
......@@ -823,7 +821,7 @@ class GridTest(AbstractWidgetTest, unittest.TestCase):
self.root.grid_location('x', 'y')
with self.assertRaisesRegex(TclError, 'bad screen distance "y"'):
self.root.grid_location('1c', 'y')
t = tkinter.Toplevel(self.root)
t = self.root
# de-maximize
t.wm_geometry('1x1+0+0')
t.wm_geometry('')
......
import unittest
import tkinter
from tkinter import ttk
from test import support
from tkinter.test.support import requires_tcl
from tkinter.test.support import AbstractTkTest, requires_tcl
support.requires('gui')
class MiscTest(unittest.TestCase):
def setUp(self):
self.root = ttk.setup_master()
class MiscTest(AbstractTkTest, unittest.TestCase):
def test_image_types(self):
image_types = self.root.image_types()
......@@ -23,15 +19,13 @@ class MiscTest(unittest.TestCase):
self.assertIsInstance(image_names, tuple)
class BitmapImageTest(unittest.TestCase):
class BitmapImageTest(AbstractTkTest, unittest.TestCase):
@classmethod
def setUpClass(cls):
AbstractTkTest.setUpClass.__func__(cls)
cls.testfile = support.findfile('python.xbm', subdir='imghdrdata')
def setUp(self):
self.root = ttk.setup_master()
def test_create_from_file(self):
image = tkinter.BitmapImage('::img::test', master=self.root,
foreground='yellow', background='blue',
......@@ -107,16 +101,13 @@ class BitmapImageTest(unittest.TestCase):
'-foreground {} {} #000000 yellow')
class PhotoImageTest(unittest.TestCase):
class PhotoImageTest(AbstractTkTest, unittest.TestCase):
@classmethod
def setUpClass(cls):
AbstractTkTest.setUpClass.__func__(cls)
cls.testfile = support.findfile('python.gif', subdir='imghdrdata')
def setUp(self):
self.root = ttk.setup_master()
self.wantobjects = self.root.wantobjects()
def create(self):
return tkinter.PhotoImage('::img::test', master=self.root,
file=self.testfile)
......
import unittest
import tkinter
from tkinter import ttk
from test import support
from tkinter.test.support import AbstractTkTest
support.requires('gui')
class MiscTest(unittest.TestCase):
def setUp(self):
self.root = ttk.setup_master()
class MiscTest(AbstractTkTest, unittest.TestCase):
def test_tk_setPalette(self):
root = self.root
......
import unittest
import tkinter
from test.support import requires, run_unittest
from tkinter.ttk import setup_master
from tkinter.test.support import AbstractTkTest
requires('gui')
class TextTest(unittest.TestCase):
class TextTest(AbstractTkTest, unittest.TestCase):
def setUp(self):
self.root = setup_master()
super().setUp()
self.text = tkinter.Text(self.root)
def tearDown(self):
self.text.destroy()
def test_debug(self):
text = self.text
olddebug = text.debug()
......
import unittest
from tkinter import Variable, StringVar, IntVar, DoubleVar, BooleanVar, Tk
from tkinter import Variable, StringVar, IntVar, DoubleVar, BooleanVar, Tcl
class Var(Variable):
......@@ -16,10 +16,10 @@ class Var(Variable):
class TestBase(unittest.TestCase):
def setUp(self):
self.root = Tk()
self.root = Tcl()
def tearDown(self):
self.root.destroy()
del self.root
class TestVariable(TestBase):
......@@ -81,7 +81,7 @@ class TestVariable(TestBase):
self.root.setvar(b'var\x00name', "value")
def test_initialize(self):
v = Var()
v = Var(self.root)
self.assertFalse(v.side_effect)
v.set("value")
self.assertTrue(v.side_effect)
......
......@@ -66,7 +66,7 @@ class ToplevelTest(AbstractToplevelTest, unittest.TestCase):
'takefocus', 'use', 'visual', 'width',
)
def _create(self, **kwargs):
def create(self, **kwargs):
return tkinter.Toplevel(self.root, **kwargs)
def test_menu(self):
......@@ -105,7 +105,7 @@ class FrameTest(AbstractToplevelTest, unittest.TestCase):
'relief', 'takefocus', 'visual', 'width',
)
def _create(self, **kwargs):
def create(self, **kwargs):
return tkinter.Frame(self.root, **kwargs)
......@@ -120,7 +120,7 @@ class LabelFrameTest(AbstractToplevelTest, unittest.TestCase):
'takefocus', 'text', 'visual', 'width',
)
def _create(self, **kwargs):
def create(self, **kwargs):
return tkinter.LabelFrame(self.root, **kwargs)
def test_labelanchor(self):
......@@ -158,7 +158,7 @@ class LabelTest(AbstractLabelTest, unittest.TestCase):
'underline', 'width', 'wraplength',
)
def _create(self, **kwargs):
def create(self, **kwargs):
return tkinter.Label(self.root, **kwargs)
......@@ -175,7 +175,7 @@ class ButtonTest(AbstractLabelTest, unittest.TestCase):
'state', 'takefocus', 'text', 'textvariable',
'underline', 'width', 'wraplength')
def _create(self, **kwargs):
def create(self, **kwargs):
return tkinter.Button(self.root, **kwargs)
def test_default(self):
......@@ -199,7 +199,7 @@ class CheckbuttonTest(AbstractLabelTest, unittest.TestCase):
'underline', 'variable', 'width', 'wraplength',
)
def _create(self, **kwargs):
def create(self, **kwargs):
return tkinter.Checkbutton(self.root, **kwargs)
......@@ -227,7 +227,7 @@ class RadiobuttonTest(AbstractLabelTest, unittest.TestCase):
'underline', 'value', 'variable', 'width', 'wraplength',
)
def _create(self, **kwargs):
def create(self, **kwargs):
return tkinter.Radiobutton(self.root, **kwargs)
def test_value(self):
......@@ -250,7 +250,7 @@ class MenubuttonTest(AbstractLabelTest, unittest.TestCase):
)
_conv_pixels = staticmethod(pixels_round)
def _create(self, **kwargs):
def create(self, **kwargs):
return tkinter.Menubutton(self.root, **kwargs)
def test_direction(self):
......@@ -268,7 +268,7 @@ class MenubuttonTest(AbstractLabelTest, unittest.TestCase):
'crashes with Cocoa Tk (issue19733)')
def test_image(self):
widget = self.create()
image = tkinter.PhotoImage('image1')
image = tkinter.PhotoImage(master=self.root, name='image1')
self.checkParam(widget, 'image', image, conv=str)
errmsg = 'image "spam" doesn\'t exist'
with self.assertRaises(tkinter.TclError) as cm:
......@@ -303,7 +303,7 @@ class MenubuttonTest(AbstractLabelTest, unittest.TestCase):
class OptionMenuTest(MenubuttonTest, unittest.TestCase):
def _create(self, default='b', values=('a', 'b', 'c'), **kwargs):
def create(self, default='b', values=('a', 'b', 'c'), **kwargs):
return tkinter.OptionMenu(self.root, None, default, *values, **kwargs)
......@@ -322,7 +322,7 @@ class EntryTest(AbstractWidgetTest, unittest.TestCase):
'validate', 'validatecommand', 'width', 'xscrollcommand',
)
def _create(self, **kwargs):
def create(self, **kwargs):
return tkinter.Entry(self.root, **kwargs)
def test_disabledbackground(self):
......@@ -396,7 +396,7 @@ class SpinboxTest(EntryTest, unittest.TestCase):
'width', 'wrap', 'xscrollcommand',
)
def _create(self, **kwargs):
def create(self, **kwargs):
return tkinter.Spinbox(self.root, **kwargs)
test_show = None
......@@ -490,9 +490,9 @@ class TextTest(AbstractWidgetTest, unittest.TestCase):
'xscrollcommand', 'yscrollcommand',
)
if tcl_version < (8, 5):
wantobjects = False
_stringify = True
def _create(self, **kwargs):
def create(self, **kwargs):
return tkinter.Text(self.root, **kwargs)
def test_autoseparators(self):
......@@ -644,9 +644,9 @@ class CanvasTest(AbstractWidgetTest, unittest.TestCase):
)
_conv_pixels = round
wantobjects = False
_stringify = True
def _create(self, **kwargs):
def create(self, **kwargs):
return tkinter.Canvas(self.root, **kwargs)
def test_closeenough(self):
......@@ -699,7 +699,7 @@ class ListboxTest(AbstractWidgetTest, unittest.TestCase):
'takefocus', 'width', 'xscrollcommand', 'yscrollcommand',
)
def _create(self, **kwargs):
def create(self, **kwargs):
return tkinter.Listbox(self.root, **kwargs)
def test_activestyle(self):
......@@ -709,7 +709,7 @@ class ListboxTest(AbstractWidgetTest, unittest.TestCase):
def test_listvariable(self):
widget = self.create()
var = tkinter.DoubleVar()
var = tkinter.DoubleVar(self.root)
self.checkVariableParam(widget, 'listvariable', var)
def test_selectmode(self):
......@@ -831,7 +831,7 @@ class ScaleTest(AbstractWidgetTest, unittest.TestCase):
)
default_orient = 'vertical'
def _create(self, **kwargs):
def create(self, **kwargs):
return tkinter.Scale(self.root, **kwargs)
def test_bigincrement(self):
......@@ -897,10 +897,10 @@ class ScrollbarTest(AbstractWidgetTest, unittest.TestCase):
'takefocus', 'troughcolor', 'width',
)
_conv_pixels = round
wantobjects = False
_stringify = True
default_orient = 'vertical'
def _create(self, **kwargs):
def create(self, **kwargs):
return tkinter.Scrollbar(self.root, **kwargs)
def test_activerelief(self):
......@@ -946,7 +946,7 @@ class PanedWindowTest(AbstractWidgetTest, unittest.TestCase):
)
default_orient = 'horizontal'
def _create(self, **kwargs):
def create(self, **kwargs):
return tkinter.PanedWindow(self.root, **kwargs)
def test_handlepad(self):
......@@ -1104,7 +1104,7 @@ class MenuTest(AbstractWidgetTest, unittest.TestCase):
)
_conv_pixels = noconv
def _create(self, **kwargs):
def create(self, **kwargs):
return tkinter.Menu(self.root, **kwargs)
def test_postcommand(self):
......@@ -1173,7 +1173,7 @@ class MessageTest(AbstractWidgetTest, unittest.TestCase):
)
_conv_pad_pixels = noconv
def _create(self, **kwargs):
def create(self, **kwargs):
return tkinter.Message(self.root, **kwargs)
def test_aspect(self):
......
......@@ -2,15 +2,15 @@ import unittest
import tkinter
from tkinter import ttk
from test.support import requires, run_unittest
import tkinter.test.support as support
from tkinter.test.support import AbstractTkTest
requires('gui')
class StyleTest(unittest.TestCase):
class StyleTest(AbstractTkTest, unittest.TestCase):
def setUp(self):
self.style = ttk.Style()
super().setUp()
self.style = ttk.Style(self.root)
def test_configure(self):
......@@ -25,7 +25,7 @@ class StyleTest(unittest.TestCase):
style = self.style
style.map('TButton', background=[('active', 'background', 'blue')])
self.assertEqual(style.map('TButton', 'background'),
[('active', 'background', 'blue')] if style.tk.wantobjects() else
[('active', 'background', 'blue')] if self.wantobjects else
[('active background', 'blue')])
self.assertIsInstance(style.map('TButton'), dict)
......
This diff is collapsed.
......@@ -3,9 +3,9 @@
import unittest
import sys
import tkinter
from tkinter.ttk import setup_master, Scale
from tkinter.test.support import (tcl_version, requires_tcl, get_tk_patchlevel,
pixels_conv, tcl_obj_eq)
from tkinter.ttk import Scale
from tkinter.test.support import (AbstractTkTest, tcl_version, requires_tcl,
get_tk_patchlevel, pixels_conv, tcl_obj_eq)
import test.support
......@@ -22,33 +22,26 @@ if get_tk_patchlevel()[:3] == (8, 5, 11):
_sentinel = object()
class AbstractWidgetTest:
class AbstractWidgetTest(AbstractTkTest):
_conv_pixels = staticmethod(pixels_round)
_conv_pad_pixels = None
wantobjects = True
_stringify = False
def setUp(self):
self.root = setup_master()
self.scaling = float(self.root.call('tk', 'scaling'))
if not self.root.wantobjects():
self.wantobjects = False
def tearDown(self):
for w in self.root.winfo_children():
w.destroy()
@property
def scaling(self):
try:
return self._scaling
except AttributeError:
self._scaling = float(self.root.call('tk', 'scaling'))
return self._scaling
def _str(self, value):
if self.wantobjects and tcl_version >= (8, 6):
if not self._stringify and self.wantobjects and tcl_version >= (8, 6):
return value
if isinstance(value, tuple):
return ' '.join(map(self._str, value))
return str(value)
def create(self, **kwargs):
widget = self._create(**kwargs)
self.addCleanup(widget.destroy)
return widget
def assertEqual2(self, actual, expected, msg=None, eq=object.__eq__):
if eq(actual, expected):
return
......@@ -61,7 +54,7 @@ class AbstractWidgetTest:
expected = value
if conv:
expected = conv(expected)
if not self.wantobjects:
if self._stringify or not self.wantobjects:
if isinstance(expected, tuple):
expected = tkinter._join(expected)
else:
......@@ -193,7 +186,7 @@ class AbstractWidgetTest:
errmsg=errmsg)
def checkImageParam(self, widget, name):
image = tkinter.PhotoImage('image1')
image = tkinter.PhotoImage(master=self.root, name='image1')
self.checkParam(widget, name, image, conv=str)
self.checkInvalidParam(widget, name, 'spam',
errmsg='image "spam" doesn\'t exist')
......@@ -414,7 +407,7 @@ class StandardOptionsTests:
def test_textvariable(self):
widget = self.create()
var = tkinter.StringVar()
var = tkinter.StringVar(self.root)
self.checkVariableParam(widget, 'textvariable', var)
def test_troughcolor(self):
......@@ -475,7 +468,7 @@ class StandardOptionsTests:
def test_variable(self):
widget = self.create()
var = tkinter.DoubleVar()
var = tkinter.DoubleVar(self.root)
self.checkVariableParam(widget, 'variable', var)
......
......@@ -27,7 +27,9 @@ Core and Builtins
Library
-------
-- Issue #22191: Fix warnings.__all__.
- Issue #22236: Fixed Tkinter images copying operations in NoDefaultRoot mode.
- Issue #22191: Fix warnings.__all__.
- Issue #15696: Add a __sizeof__ implementation for mmap objects on Windows.
......@@ -279,6 +281,9 @@ IDLE
Tests
-----
- Issue #22236: Tkinter tests now don't reuse default root window. New root
window is created for every test class.
- Issue #20746: Fix test_pdb to run in refleak mode (-R). Patch by Xavier
de Gaye.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment