ยปCore Development>Code coverage>Lib/tkinter/test/test_ttk/test_extensions.py

Python code coverage for Lib/tkinter/test/test_ttk/test_extensions.py

#countcontent
1n/aimport sys
2n/aimport unittest
3n/aimport tkinter
4n/afrom tkinter import ttk
5n/afrom test.support import requires, run_unittest, swap_attr
6n/afrom tkinter.test.support import AbstractTkTest, destroy_default_root
7n/a
8n/arequires('gui')
9n/a
10n/aclass LabeledScaleTest(AbstractTkTest, unittest.TestCase):
11n/a
12n/a def tearDown(self):
13n/a self.root.update_idletasks()
14n/a super().tearDown()
15n/a
16n/a def test_widget_destroy(self):
17n/a # automatically created variable
18n/a x = ttk.LabeledScale(self.root)
19n/a var = x._variable._name
20n/a x.destroy()
21n/a self.assertRaises(tkinter.TclError, x.tk.globalgetvar, var)
22n/a
23n/a # manually created variable
24n/a myvar = tkinter.DoubleVar(self.root)
25n/a name = myvar._name
26n/a x = ttk.LabeledScale(self.root, variable=myvar)
27n/a x.destroy()
28n/a if self.wantobjects:
29n/a self.assertEqual(x.tk.globalgetvar(name), myvar.get())
30n/a else:
31n/a self.assertEqual(float(x.tk.globalgetvar(name)), myvar.get())
32n/a del myvar
33n/a self.assertRaises(tkinter.TclError, x.tk.globalgetvar, name)
34n/a
35n/a # checking that the tracing callback is properly removed
36n/a myvar = tkinter.IntVar(self.root)
37n/a # LabeledScale will start tracing myvar
38n/a x = ttk.LabeledScale(self.root, variable=myvar)
39n/a x.destroy()
40n/a # Unless the tracing callback was removed, creating a new
41n/a # LabeledScale with the same var will cause an error now. This
42n/a # happens because the variable will be set to (possibly) a new
43n/a # value which causes the tracing callback to be called and then
44n/a # it tries calling instance attributes not yet defined.
45n/a ttk.LabeledScale(self.root, variable=myvar)
46n/a if hasattr(sys, 'last_type'):
47n/a self.assertNotEqual(sys.last_type, tkinter.TclError)
48n/a
49n/a
50n/a def test_initialization_no_master(self):
51n/a # no master passing
52n/a with swap_attr(tkinter, '_default_root', None), \
53n/a swap_attr(tkinter, '_support_default_root', True):
54n/a try:
55n/a x = ttk.LabeledScale()
56n/a self.assertIsNotNone(tkinter._default_root)
57n/a self.assertEqual(x.master, tkinter._default_root)
58n/a self.assertEqual(x.tk, tkinter._default_root.tk)
59n/a x.destroy()
60n/a finally:
61n/a destroy_default_root()
62n/a
63n/a def test_initialization(self):
64n/a # master passing
65n/a master = tkinter.Frame(self.root)
66n/a x = ttk.LabeledScale(master)
67n/a self.assertEqual(x.master, master)
68n/a x.destroy()
69n/a
70n/a # variable initialization/passing
71n/a passed_expected = (('0', 0), (0, 0), (10, 10),
72n/a (-1, -1), (sys.maxsize + 1, sys.maxsize + 1),
73n/a (2.5, 2), ('2.5', 2))
74n/a for pair in passed_expected:
75n/a x = ttk.LabeledScale(self.root, from_=pair[0])
76n/a self.assertEqual(x.value, pair[1])
77n/a x.destroy()
78n/a x = ttk.LabeledScale(self.root, from_=None)
79n/a self.assertRaises((ValueError, tkinter.TclError), x._variable.get)
80n/a x.destroy()
81n/a # variable should have its default value set to the from_ value
82n/a myvar = tkinter.DoubleVar(self.root, value=20)
83n/a x = ttk.LabeledScale(self.root, variable=myvar)
84n/a self.assertEqual(x.value, 0)
85n/a x.destroy()
86n/a # check that it is really using a DoubleVar
87n/a x = ttk.LabeledScale(self.root, variable=myvar, from_=0.5)
88n/a self.assertEqual(x.value, 0.5)
89n/a self.assertEqual(x._variable._name, myvar._name)
90n/a x.destroy()
91n/a
92n/a # widget positionment
93n/a def check_positions(scale, scale_pos, label, label_pos):
94n/a self.assertEqual(scale.pack_info()['side'], scale_pos)
95n/a self.assertEqual(label.place_info()['anchor'], label_pos)
96n/a x = ttk.LabeledScale(self.root, compound='top')
97n/a check_positions(x.scale, 'bottom', x.label, 'n')
98n/a x.destroy()
99n/a x = ttk.LabeledScale(self.root, compound='bottom')
100n/a check_positions(x.scale, 'top', x.label, 's')
101n/a x.destroy()
102n/a # invert default positions
103n/a x = ttk.LabeledScale(self.root, compound='unknown')
104n/a check_positions(x.scale, 'top', x.label, 's')
105n/a x.destroy()
106n/a x = ttk.LabeledScale(self.root) # take default positions
107n/a check_positions(x.scale, 'bottom', x.label, 'n')
108n/a x.destroy()
109n/a
110n/a # extra, and invalid, kwargs
111n/a self.assertRaises(tkinter.TclError, ttk.LabeledScale, master, a='b')
112n/a
113n/a
114n/a def test_horizontal_range(self):
115n/a lscale = ttk.LabeledScale(self.root, from_=0, to=10)
116n/a lscale.pack()
117n/a lscale.wait_visibility()
118n/a lscale.update()
119n/a
120n/a linfo_1 = lscale.label.place_info()
121n/a prev_xcoord = lscale.scale.coords()[0]
122n/a self.assertEqual(prev_xcoord, int(linfo_1['x']))
123n/a # change range to: from -5 to 5. This should change the x coord of
124n/a # the scale widget, since 0 is at the middle of the new
125n/a # range.
126n/a lscale.scale.configure(from_=-5, to=5)
127n/a # The following update is needed since the test doesn't use mainloop,
128n/a # at the same time this shouldn't affect test outcome
129n/a lscale.update()
130n/a curr_xcoord = lscale.scale.coords()[0]
131n/a self.assertNotEqual(prev_xcoord, curr_xcoord)
132n/a # the label widget should have been repositioned too
133n/a linfo_2 = lscale.label.place_info()
134n/a self.assertEqual(lscale.label['text'], 0 if self.wantobjects else '0')
135n/a self.assertEqual(curr_xcoord, int(linfo_2['x']))
136n/a # change the range back
137n/a lscale.scale.configure(from_=0, to=10)
138n/a self.assertNotEqual(prev_xcoord, curr_xcoord)
139n/a self.assertEqual(prev_xcoord, int(linfo_1['x']))
140n/a
141n/a lscale.destroy()
142n/a
143n/a
144n/a def test_variable_change(self):
145n/a x = ttk.LabeledScale(self.root)
146n/a x.pack()
147n/a x.wait_visibility()
148n/a x.update()
149n/a
150n/a curr_xcoord = x.scale.coords()[0]
151n/a newval = x.value + 1
152n/a x.value = newval
153n/a # The following update is needed since the test doesn't use mainloop,
154n/a # at the same time this shouldn't affect test outcome
155n/a x.update()
156n/a self.assertEqual(x.value, newval)
157n/a self.assertEqual(x.label['text'],
158n/a newval if self.wantobjects else str(newval))
159n/a self.assertEqual(float(x.scale.get()), newval)
160n/a self.assertGreater(x.scale.coords()[0], curr_xcoord)
161n/a self.assertEqual(x.scale.coords()[0],
162n/a int(x.label.place_info()['x']))
163n/a
164n/a # value outside range
165n/a if self.wantobjects:
166n/a conv = lambda x: x
167n/a else:
168n/a conv = int
169n/a x.value = conv(x.scale['to']) + 1 # no changes shouldn't happen
170n/a x.update()
171n/a self.assertEqual(x.value, newval)
172n/a self.assertEqual(conv(x.label['text']), newval)
173n/a self.assertEqual(float(x.scale.get()), newval)
174n/a self.assertEqual(x.scale.coords()[0],
175n/a int(x.label.place_info()['x']))
176n/a
177n/a # non-integer value
178n/a x.value = newval = newval + 1.5
179n/a x.update()
180n/a self.assertEqual(x.value, int(newval))
181n/a self.assertEqual(conv(x.label['text']), int(newval))
182n/a self.assertEqual(float(x.scale.get()), newval)
183n/a
184n/a x.destroy()
185n/a
186n/a
187n/a def test_resize(self):
188n/a x = ttk.LabeledScale(self.root)
189n/a x.pack(expand=True, fill='both')
190n/a x.wait_visibility()
191n/a x.update()
192n/a
193n/a width, height = x.master.winfo_width(), x.master.winfo_height()
194n/a width_new, height_new = width * 2, height * 2
195n/a
196n/a x.value = 3
197n/a x.update()
198n/a x.master.wm_geometry("%dx%d" % (width_new, height_new))
199n/a self.assertEqual(int(x.label.place_info()['x']),
200n/a x.scale.coords()[0])
201n/a
202n/a # Reset geometry
203n/a x.master.wm_geometry("%dx%d" % (width, height))
204n/a x.destroy()
205n/a
206n/a
207n/aclass OptionMenuTest(AbstractTkTest, unittest.TestCase):
208n/a
209n/a def setUp(self):
210n/a super().setUp()
211n/a self.textvar = tkinter.StringVar(self.root)
212n/a
213n/a def tearDown(self):
214n/a del self.textvar
215n/a super().tearDown()
216n/a
217n/a
218n/a def test_widget_destroy(self):
219n/a var = tkinter.StringVar(self.root)
220n/a optmenu = ttk.OptionMenu(self.root, var)
221n/a name = var._name
222n/a optmenu.update_idletasks()
223n/a optmenu.destroy()
224n/a self.assertEqual(optmenu.tk.globalgetvar(name), var.get())
225n/a del var
226n/a self.assertRaises(tkinter.TclError, optmenu.tk.globalgetvar, name)
227n/a
228n/a
229n/a def test_initialization(self):
230n/a self.assertRaises(tkinter.TclError,
231n/a ttk.OptionMenu, self.root, self.textvar, invalid='thing')
232n/a
233n/a optmenu = ttk.OptionMenu(self.root, self.textvar, 'b', 'a', 'b')
234n/a self.assertEqual(optmenu._variable.get(), 'b')
235n/a
236n/a self.assertTrue(optmenu['menu'])
237n/a self.assertTrue(optmenu['textvariable'])
238n/a
239n/a optmenu.destroy()
240n/a
241n/a
242n/a def test_menu(self):
243n/a items = ('a', 'b', 'c')
244n/a default = 'a'
245n/a optmenu = ttk.OptionMenu(self.root, self.textvar, default, *items)
246n/a found_default = False
247n/a for i in range(len(items)):
248n/a value = optmenu['menu'].entrycget(i, 'value')
249n/a self.assertEqual(value, items[i])
250n/a if value == default:
251n/a found_default = True
252n/a self.assertTrue(found_default)
253n/a optmenu.destroy()
254n/a
255n/a # default shouldn't be in menu if it is not part of values
256n/a default = 'd'
257n/a optmenu = ttk.OptionMenu(self.root, self.textvar, default, *items)
258n/a curr = None
259n/a i = 0
260n/a while True:
261n/a last, curr = curr, optmenu['menu'].entryconfigure(i, 'value')
262n/a if last == curr:
263n/a # no more menu entries
264n/a break
265n/a self.assertNotEqual(curr, default)
266n/a i += 1
267n/a self.assertEqual(i, len(items))
268n/a
269n/a # check that variable is updated correctly
270n/a optmenu.pack()
271n/a optmenu.wait_visibility()
272n/a optmenu['menu'].invoke(0)
273n/a self.assertEqual(optmenu._variable.get(), items[0])
274n/a
275n/a # changing to an invalid index shouldn't change the variable
276n/a self.assertRaises(tkinter.TclError, optmenu['menu'].invoke, -1)
277n/a self.assertEqual(optmenu._variable.get(), items[0])
278n/a
279n/a optmenu.destroy()
280n/a
281n/a # specifying a callback
282n/a success = []
283n/a def cb_test(item):
284n/a self.assertEqual(item, items[1])
285n/a success.append(True)
286n/a optmenu = ttk.OptionMenu(self.root, self.textvar, 'a', command=cb_test,
287n/a *items)
288n/a optmenu['menu'].invoke(1)
289n/a if not success:
290n/a self.fail("Menu callback not invoked")
291n/a
292n/a optmenu.destroy()
293n/a
294n/a
295n/atests_gui = (LabeledScaleTest, OptionMenuTest)
296n/a
297n/aif __name__ == "__main__":
298n/a run_unittest(*tests_gui)