1import os
2import sys
3import unittest
4import test.support as test_support
5from tkinter import Tcl, TclError
6
7test_support.requires('gui')
8
9class TkLoadTest(unittest.TestCase):
10
11    @unittest.skipIf('DISPLAY' not in os.environ, 'No $DISPLAY set.')
12    def testLoadTk(self):
13        tcl = Tcl()
14        self.assertRaises(TclError,tcl.winfo_geometry)
15        tcl.loadtk()
16        self.assertEqual('1x1+0+0', tcl.winfo_geometry())
17        tcl.destroy()
18
19    def testLoadTkFailure(self):
20        old_display = None
21        if sys.platform.startswith(('win', 'darwin', 'cygwin')):
22            # no failure possible on windows?
23
24            # XXX Maybe on tk older than 8.4.13 it would be possible,
25            # see tkinter.h.
26            return
27        with test_support.EnvironmentVarGuard() as env:
28            if 'DISPLAY' in os.environ:
29                del env['DISPLAY']
30                # on some platforms, deleting environment variables
31                # doesn't actually carry through to the process level
32                # because they don't support unsetenv
33                # If that's the case, abort.
34                with os.popen('echo $DISPLAY') as pipe:
35                    display = pipe.read().strip()
36                if display:
37                    return
38
39            tcl = Tcl()
40            self.assertRaises(TclError, tcl.winfo_geometry)
41            self.assertRaises(TclError, tcl.loadtk)
42
43tests_gui = (TkLoadTest, )
44
45if __name__ == "__main__":
46    test_support.run_unittest(*tests_gui)
47