1 | n/a | import re |
---|
2 | n/a | import tkinter |
---|
3 | n/a | import unittest |
---|
4 | n/a | |
---|
5 | n/a | class AbstractTkTest: |
---|
6 | n/a | |
---|
7 | n/a | @classmethod |
---|
8 | n/a | def setUpClass(cls): |
---|
9 | n/a | cls._old_support_default_root = tkinter._support_default_root |
---|
10 | n/a | destroy_default_root() |
---|
11 | n/a | tkinter.NoDefaultRoot() |
---|
12 | n/a | cls.root = tkinter.Tk() |
---|
13 | n/a | cls.wantobjects = cls.root.wantobjects() |
---|
14 | n/a | # De-maximize main window. |
---|
15 | n/a | # Some window managers can maximize new windows. |
---|
16 | n/a | cls.root.wm_state('normal') |
---|
17 | n/a | try: |
---|
18 | n/a | cls.root.wm_attributes('-zoomed', False) |
---|
19 | n/a | except tkinter.TclError: |
---|
20 | n/a | pass |
---|
21 | n/a | |
---|
22 | n/a | @classmethod |
---|
23 | n/a | def tearDownClass(cls): |
---|
24 | n/a | cls.root.update_idletasks() |
---|
25 | n/a | cls.root.destroy() |
---|
26 | n/a | del cls.root |
---|
27 | n/a | tkinter._default_root = None |
---|
28 | n/a | tkinter._support_default_root = cls._old_support_default_root |
---|
29 | n/a | |
---|
30 | n/a | def setUp(self): |
---|
31 | n/a | self.root.deiconify() |
---|
32 | n/a | |
---|
33 | n/a | def tearDown(self): |
---|
34 | n/a | for w in self.root.winfo_children(): |
---|
35 | n/a | w.destroy() |
---|
36 | n/a | self.root.withdraw() |
---|
37 | n/a | |
---|
38 | n/a | def destroy_default_root(): |
---|
39 | n/a | if getattr(tkinter, '_default_root', None): |
---|
40 | n/a | tkinter._default_root.update_idletasks() |
---|
41 | n/a | tkinter._default_root.destroy() |
---|
42 | n/a | tkinter._default_root = None |
---|
43 | n/a | |
---|
44 | n/a | def simulate_mouse_click(widget, x, y): |
---|
45 | n/a | """Generate proper events to click at the x, y position (tries to act |
---|
46 | n/a | like an X server).""" |
---|
47 | n/a | widget.event_generate('<Enter>', x=0, y=0) |
---|
48 | n/a | widget.event_generate('<Motion>', x=x, y=y) |
---|
49 | n/a | widget.event_generate('<ButtonPress-1>', x=x, y=y) |
---|
50 | n/a | widget.event_generate('<ButtonRelease-1>', x=x, y=y) |
---|
51 | n/a | |
---|
52 | n/a | |
---|
53 | n/a | import _tkinter |
---|
54 | n/a | tcl_version = tuple(map(int, _tkinter.TCL_VERSION.split('.'))) |
---|
55 | n/a | |
---|
56 | n/a | def requires_tcl(*version): |
---|
57 | n/a | return unittest.skipUnless(tcl_version >= version, |
---|
58 | n/a | 'requires Tcl version >= ' + '.'.join(map(str, version))) |
---|
59 | n/a | |
---|
60 | n/a | _tk_patchlevel = None |
---|
61 | n/a | def get_tk_patchlevel(): |
---|
62 | n/a | global _tk_patchlevel |
---|
63 | n/a | if _tk_patchlevel is None: |
---|
64 | n/a | tcl = tkinter.Tcl() |
---|
65 | n/a | patchlevel = tcl.call('info', 'patchlevel') |
---|
66 | n/a | m = re.fullmatch(r'(\d+)\.(\d+)([ab.])(\d+)', patchlevel) |
---|
67 | n/a | major, minor, releaselevel, serial = m.groups() |
---|
68 | n/a | major, minor, serial = int(major), int(minor), int(serial) |
---|
69 | n/a | releaselevel = {'a': 'alpha', 'b': 'beta', '.': 'final'}[releaselevel] |
---|
70 | n/a | if releaselevel == 'final': |
---|
71 | n/a | _tk_patchlevel = major, minor, serial, releaselevel, 0 |
---|
72 | n/a | else: |
---|
73 | n/a | _tk_patchlevel = major, minor, 0, releaselevel, serial |
---|
74 | n/a | return _tk_patchlevel |
---|
75 | n/a | |
---|
76 | n/a | units = { |
---|
77 | n/a | 'c': 72 / 2.54, # centimeters |
---|
78 | n/a | 'i': 72, # inches |
---|
79 | n/a | 'm': 72 / 25.4, # millimeters |
---|
80 | n/a | 'p': 1, # points |
---|
81 | n/a | } |
---|
82 | n/a | |
---|
83 | n/a | def pixels_conv(value): |
---|
84 | n/a | return float(value[:-1]) * units[value[-1:]] |
---|
85 | n/a | |
---|
86 | n/a | def tcl_obj_eq(actual, expected): |
---|
87 | n/a | if actual == expected: |
---|
88 | n/a | return True |
---|
89 | n/a | if isinstance(actual, _tkinter.Tcl_Obj): |
---|
90 | n/a | if isinstance(expected, str): |
---|
91 | n/a | return str(actual) == expected |
---|
92 | n/a | if isinstance(actual, tuple): |
---|
93 | n/a | if isinstance(expected, tuple): |
---|
94 | n/a | return (len(actual) == len(expected) and |
---|
95 | n/a | all(tcl_obj_eq(act, exp) |
---|
96 | n/a | for act, exp in zip(actual, expected))) |
---|
97 | n/a | return False |
---|
98 | n/a | |
---|
99 | n/a | def widget_eq(actual, expected): |
---|
100 | n/a | if actual == expected: |
---|
101 | n/a | return True |
---|
102 | n/a | if isinstance(actual, (str, tkinter.Widget)): |
---|
103 | n/a | if isinstance(expected, (str, tkinter.Widget)): |
---|
104 | n/a | return str(actual) == str(expected) |
---|
105 | n/a | return False |
---|