summaryrefslogtreecommitdiffstats
path: root/Lib/tkinter/test
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/tkinter/test')
-rw-r--r--Lib/tkinter/test/support.py46
-rw-r--r--Lib/tkinter/test/test_tkinter/test_font.py33
-rw-r--r--Lib/tkinter/test/test_tkinter/test_loadtk.py3
-rw-r--r--Lib/tkinter/test/test_tkinter/test_misc.py45
-rw-r--r--Lib/tkinter/test/test_ttk/test_functions.py40
-rw-r--r--Lib/tkinter/test/test_ttk/test_widgets.py32
6 files changed, 188 insertions, 11 deletions
diff --git a/Lib/tkinter/test/support.py b/Lib/tkinter/test/support.py
index 97212fb..6dd6d4a 100644
--- a/Lib/tkinter/test/support.py
+++ b/Lib/tkinter/test/support.py
@@ -1,6 +1,52 @@
+import sys
import tkinter
+import unittest
+
+_tk_unavailable = None
+
+def check_tk_availability():
+ """Check that Tk is installed and available."""
+ global _tk_unavailable
+
+ if _tk_unavailable is None:
+ _tk_unavailable = False
+ if sys.platform == 'darwin':
+ # The Aqua Tk implementations on OS X can abort the process if
+ # being called in an environment where a window server connection
+ # cannot be made, for instance when invoked by a buildbot or ssh
+ # process not running under the same user id as the current console
+ # user. To avoid that, raise an exception if the window manager
+ # connection is not available.
+ from ctypes import cdll, c_int, pointer, Structure
+ from ctypes.util import find_library
+
+ app_services = cdll.LoadLibrary(find_library("ApplicationServices"))
+
+ if app_services.CGMainDisplayID() == 0:
+ _tk_unavailable = "cannot run without OS X window manager"
+ else:
+ class ProcessSerialNumber(Structure):
+ _fields_ = [("highLongOfPSN", c_int),
+ ("lowLongOfPSN", c_int)]
+ psn = ProcessSerialNumber()
+ psn_p = pointer(psn)
+ if ( (app_services.GetCurrentProcess(psn_p) < 0) or
+ (app_services.SetFrontProcess(psn_p) < 0) ):
+ _tk_unavailable = "cannot run without OS X gui process"
+ else: # not OS X
+ import tkinter
+ try:
+ tkinter.Button()
+ except tkinter.TclError as msg:
+ # assuming tk is not available
+ _tk_unavailable = "tk not available: %s" % msg
+
+ if _tk_unavailable:
+ raise unittest.SkipTest(_tk_unavailable)
+ return
def get_tk_root():
+ check_tk_availability() # raise exception if tk unavailable
try:
root = tkinter._default_root
except AttributeError:
diff --git a/Lib/tkinter/test/test_tkinter/test_font.py b/Lib/tkinter/test/test_tkinter/test_font.py
new file mode 100644
index 0000000..dfd630b
--- /dev/null
+++ b/Lib/tkinter/test/test_tkinter/test_font.py
@@ -0,0 +1,33 @@
+import unittest
+import tkinter
+from tkinter import font
+from test.support import requires, run_unittest
+import tkinter.test.support as support
+
+requires('gui')
+
+class FontTest(unittest.TestCase):
+
+ def setUp(self):
+ support.root_deiconify()
+
+ def tearDown(self):
+ support.root_withdraw()
+
+ def test_font_eq(self):
+ fontname = "TkDefaultFont"
+ try:
+ f = font.Font(name=fontname, exists=True)
+ except tkinter._tkinter.TclError:
+ f = font.Font(name=fontname, exists=False)
+ font1 = font.nametofont(fontname)
+ font2 = font.nametofont(fontname)
+ self.assertIsNot(font1, font2)
+ self.assertEqual(font1, font2)
+ self.assertNotEqual(font1, font1.copy())
+ self.assertNotEqual(font1, 0)
+
+tests_gui = (FontTest, )
+
+if __name__ == "__main__":
+ run_unittest(*tests_gui)
diff --git a/Lib/tkinter/test/test_tkinter/test_loadtk.py b/Lib/tkinter/test/test_tkinter/test_loadtk.py
index 8f1a085..bab7bcd 100644
--- a/Lib/tkinter/test/test_tkinter/test_loadtk.py
+++ b/Lib/tkinter/test/test_tkinter/test_loadtk.py
@@ -31,7 +31,8 @@ class TkLoadTest(unittest.TestCase):
# doesn't actually carry through to the process level
# because they don't support unsetenv
# If that's the case, abort.
- display = os.popen('echo $DISPLAY').read().strip()
+ with os.popen('echo $DISPLAY') as pipe:
+ display = pipe.read().strip()
if display:
return
diff --git a/Lib/tkinter/test/test_tkinter/test_misc.py b/Lib/tkinter/test/test_tkinter/test_misc.py
new file mode 100644
index 0000000..d325b31
--- /dev/null
+++ b/Lib/tkinter/test/test_tkinter/test_misc.py
@@ -0,0 +1,45 @@
+import unittest
+import tkinter
+from tkinter import ttk
+from test import support
+
+support.requires('gui')
+
+class MiscTest(unittest.TestCase):
+
+ def setUp(self):
+ self.root = ttk.setup_master()
+
+ def test_tk_setPalette(self):
+ root = self.root
+ root.tk_setPalette('black')
+ self.assertEqual(root['background'], 'black')
+ root.tk_setPalette('white')
+ self.assertEqual(root['background'], 'white')
+ self.assertRaisesRegex(tkinter.TclError,
+ '^unknown color name "spam"$',
+ root.tk_setPalette, 'spam')
+
+ root.tk_setPalette(background='black')
+ self.assertEqual(root['background'], 'black')
+ root.tk_setPalette(background='blue', highlightColor='yellow')
+ self.assertEqual(root['background'], 'blue')
+ self.assertEqual(root['highlightcolor'], 'yellow')
+ root.tk_setPalette(background='yellow', highlightColor='blue')
+ self.assertEqual(root['background'], 'yellow')
+ self.assertEqual(root['highlightcolor'], 'blue')
+ self.assertRaisesRegex(tkinter.TclError,
+ '^unknown color name "spam"$',
+ root.tk_setPalette, background='spam')
+ self.assertRaisesRegex(tkinter.TclError,
+ '^must specify a background color$',
+ root.tk_setPalette, spam='white')
+ self.assertRaisesRegex(tkinter.TclError,
+ '^must specify a background color$',
+ root.tk_setPalette, highlightColor='blue')
+
+
+tests_gui = (MiscTest, )
+
+if __name__ == "__main__":
+ support.run_unittest(*tests_gui)
diff --git a/Lib/tkinter/test/test_ttk/test_functions.py b/Lib/tkinter/test/test_ttk/test_functions.py
index 2303e4c..0d8df16 100644
--- a/Lib/tkinter/test/test_ttk/test_functions.py
+++ b/Lib/tkinter/test/test_ttk/test_functions.py
@@ -49,13 +49,17 @@ class InternalFunctionsTest(unittest.TestCase):
ttk._format_optdict({'test': {'left': 'as is'}}),
{'-test': {'left': 'as is'}})
- # check script formatting and untouched value(s)
+ # check script formatting
check_against(
ttk._format_optdict(
- {'test': [1, -1, '', '2m', 0], 'nochange1': 3,
- 'nochange2': 'abc def'}, script=True),
- {'-test': '{1 -1 {} 2m 0}', '-nochange1': 3,
- '-nochange2': 'abc def' })
+ {'test': [1, -1, '', '2m', 0], 'test2': 3,
+ 'test3': '', 'test4': 'abc def',
+ 'test5': '"abc"', 'test6': '{}',
+ 'test7': '} -spam {'}, script=True),
+ {'-test': '{1 -1 {} 2m 0}', '-test2': '3',
+ '-test3': '{}', '-test4': '{abc def}',
+ '-test5': '{"abc"}', '-test6': r'\{\}',
+ '-test7': r'\}\ -spam\ \{'})
opts = {'αβγ': True, 'á': False}
orig_opts = opts.copy()
@@ -69,6 +73,32 @@ class InternalFunctionsTest(unittest.TestCase):
ttk._format_optdict(
{'option': ('one two', 'three')}),
{'-option': '{one two} three'})
+ check_against(
+ ttk._format_optdict(
+ {'option': ('one\ttwo', 'three')}),
+ {'-option': '{one\ttwo} three'})
+
+ # passing empty strings inside a tuple/list
+ check_against(
+ ttk._format_optdict(
+ {'option': ('', 'one')}),
+ {'-option': '{} one'})
+
+ # passing values with braces inside a tuple/list
+ check_against(
+ ttk._format_optdict(
+ {'option': ('one} {two', 'three')}),
+ {'-option': r'one\}\ \{two three'})
+
+ # passing quoted strings inside a tuple/list
+ check_against(
+ ttk._format_optdict(
+ {'option': ('"one"', 'two')}),
+ {'-option': '{"one"} two'})
+ check_against(
+ ttk._format_optdict(
+ {'option': ('{one}', 'two')}),
+ {'-option': r'\{one\} two'})
# ignore an option
amount_opts = len(ttk._format_optdict(opts, ignore=('á'))) / 2
diff --git a/Lib/tkinter/test/test_ttk/test_widgets.py b/Lib/tkinter/test/test_ttk/test_widgets.py
index 35824ea..45a686a 100644
--- a/Lib/tkinter/test/test_ttk/test_widgets.py
+++ b/Lib/tkinter/test/test_ttk/test_widgets.py
@@ -1,7 +1,9 @@
import unittest
import tkinter
+import os
from tkinter import ttk
from test.support import requires, run_unittest
+import sys
import tkinter.test.support as support
from tkinter.test.test_ttk.test_functions import MockTclObj, MockStateSpec
@@ -187,6 +189,14 @@ class ComboboxTest(unittest.TestCase):
self.combo.configure(values=[1, '', 2])
self.assertEqual(self.combo['values'], ('1', '', '2'))
+ # testing values with spaces
+ self.combo['values'] = ['a b', 'a\tb', 'a\nb']
+ self.assertEqual(self.combo['values'], ('a b', 'a\tb', 'a\nb'))
+
+ # testing values with special characters
+ self.combo['values'] = [r'a\tb', '"a"', '} {']
+ self.assertEqual(self.combo['values'], (r'a\tb', '"a"', '} {'))
+
# out of range
self.assertRaises(tkinter.TclError, self.combo.current,
len(self.combo['values']))
@@ -560,11 +570,19 @@ class NotebookTest(unittest.TestCase):
self.nb.pack()
self.nb.wait_visibility()
- self.assertEqual(self.nb.tab('@5,5'), self.nb.tab('current'))
+ if sys.platform == 'darwin':
+ tb_idx = "@20,5"
+ else:
+ tb_idx = "@5,5"
+ self.assertEqual(self.nb.tab(tb_idx), self.nb.tab('current'))
for i in range(5, 100, 5):
- if self.nb.tab('@%d, 5' % i, text=None) == 'a':
- break
+ try:
+ if self.nb.tab('@%d, 5' % i, text=None) == 'a':
+ break
+ except tkinter.TclError:
+ pass
+
else:
self.fail("Tab with text 'a' not found")
@@ -721,7 +739,10 @@ class NotebookTest(unittest.TestCase):
self.nb.enable_traversal()
self.nb.focus_force()
support.simulate_mouse_click(self.nb, 5, 5)
- self.nb.event_generate('<Alt-a>')
+ if sys.platform == 'darwin':
+ self.nb.event_generate('<Option-a>')
+ else:
+ self.nb.event_generate('<Alt-a>')
self.assertEqual(self.nb.select(), str(self.child1))
@@ -925,7 +946,8 @@ class TreeviewTest(unittest.TestCase):
self.assertRaises(tkinter.TclError, self.tv.heading, '#0',
anchor=1)
-
+ # XXX skipping for now; should be fixed to work with newer ttk
+ @unittest.skip("skipping pending resolution of Issue #10734")
def test_heading_callback(self):
def simulate_heading_click(x, y):
support.simulate_mouse_click(self.tv, x, y)