ยปCore Development>Code coverage>Lib/tkinter/test/test_tkinter/test_images.py

Python code coverage for Lib/tkinter/test/test_tkinter/test_images.py

#countcontent
1n/aimport unittest
2n/aimport tkinter
3n/afrom test import support
4n/afrom tkinter.test.support import AbstractTkTest, requires_tcl
5n/a
6n/asupport.requires('gui')
7n/a
8n/a
9n/aclass MiscTest(AbstractTkTest, unittest.TestCase):
10n/a
11n/a def test_image_types(self):
12n/a image_types = self.root.image_types()
13n/a self.assertIsInstance(image_types, tuple)
14n/a self.assertIn('photo', image_types)
15n/a self.assertIn('bitmap', image_types)
16n/a
17n/a def test_image_names(self):
18n/a image_names = self.root.image_names()
19n/a self.assertIsInstance(image_names, tuple)
20n/a
21n/a
22n/aclass BitmapImageTest(AbstractTkTest, unittest.TestCase):
23n/a
24n/a @classmethod
25n/a def setUpClass(cls):
26n/a AbstractTkTest.setUpClass.__func__(cls)
27n/a cls.testfile = support.findfile('python.xbm', subdir='imghdrdata')
28n/a
29n/a def test_create_from_file(self):
30n/a image = tkinter.BitmapImage('::img::test', master=self.root,
31n/a foreground='yellow', background='blue',
32n/a file=self.testfile)
33n/a self.assertEqual(str(image), '::img::test')
34n/a self.assertEqual(image.type(), 'bitmap')
35n/a self.assertEqual(image.width(), 16)
36n/a self.assertEqual(image.height(), 16)
37n/a self.assertIn('::img::test', self.root.image_names())
38n/a del image
39n/a self.assertNotIn('::img::test', self.root.image_names())
40n/a
41n/a def test_create_from_data(self):
42n/a with open(self.testfile, 'rb') as f:
43n/a data = f.read()
44n/a image = tkinter.BitmapImage('::img::test', master=self.root,
45n/a foreground='yellow', background='blue',
46n/a data=data)
47n/a self.assertEqual(str(image), '::img::test')
48n/a self.assertEqual(image.type(), 'bitmap')
49n/a self.assertEqual(image.width(), 16)
50n/a self.assertEqual(image.height(), 16)
51n/a self.assertIn('::img::test', self.root.image_names())
52n/a del image
53n/a self.assertNotIn('::img::test', self.root.image_names())
54n/a
55n/a def assertEqualStrList(self, actual, expected):
56n/a self.assertIsInstance(actual, str)
57n/a self.assertEqual(self.root.splitlist(actual), expected)
58n/a
59n/a def test_configure_data(self):
60n/a image = tkinter.BitmapImage('::img::test', master=self.root)
61n/a self.assertEqual(image['data'], '-data {} {} {} {}')
62n/a with open(self.testfile, 'rb') as f:
63n/a data = f.read()
64n/a image.configure(data=data)
65n/a self.assertEqualStrList(image['data'],
66n/a ('-data', '', '', '', data.decode('ascii')))
67n/a self.assertEqual(image.width(), 16)
68n/a self.assertEqual(image.height(), 16)
69n/a
70n/a self.assertEqual(image['maskdata'], '-maskdata {} {} {} {}')
71n/a image.configure(maskdata=data)
72n/a self.assertEqualStrList(image['maskdata'],
73n/a ('-maskdata', '', '', '', data.decode('ascii')))
74n/a
75n/a def test_configure_file(self):
76n/a image = tkinter.BitmapImage('::img::test', master=self.root)
77n/a self.assertEqual(image['file'], '-file {} {} {} {}')
78n/a image.configure(file=self.testfile)
79n/a self.assertEqualStrList(image['file'],
80n/a ('-file', '', '', '',self.testfile))
81n/a self.assertEqual(image.width(), 16)
82n/a self.assertEqual(image.height(), 16)
83n/a
84n/a self.assertEqual(image['maskfile'], '-maskfile {} {} {} {}')
85n/a image.configure(maskfile=self.testfile)
86n/a self.assertEqualStrList(image['maskfile'],
87n/a ('-maskfile', '', '', '', self.testfile))
88n/a
89n/a def test_configure_background(self):
90n/a image = tkinter.BitmapImage('::img::test', master=self.root)
91n/a self.assertEqual(image['background'], '-background {} {} {} {}')
92n/a image.configure(background='blue')
93n/a self.assertEqual(image['background'], '-background {} {} {} blue')
94n/a
95n/a def test_configure_foreground(self):
96n/a image = tkinter.BitmapImage('::img::test', master=self.root)
97n/a self.assertEqual(image['foreground'],
98n/a '-foreground {} {} #000000 #000000')
99n/a image.configure(foreground='yellow')
100n/a self.assertEqual(image['foreground'],
101n/a '-foreground {} {} #000000 yellow')
102n/a
103n/a
104n/aclass PhotoImageTest(AbstractTkTest, unittest.TestCase):
105n/a
106n/a @classmethod
107n/a def setUpClass(cls):
108n/a AbstractTkTest.setUpClass.__func__(cls)
109n/a cls.testfile = support.findfile('python.gif', subdir='imghdrdata')
110n/a
111n/a def create(self):
112n/a return tkinter.PhotoImage('::img::test', master=self.root,
113n/a file=self.testfile)
114n/a
115n/a def colorlist(self, *args):
116n/a if tkinter.TkVersion >= 8.6 and self.wantobjects:
117n/a return args
118n/a else:
119n/a return tkinter._join(args)
120n/a
121n/a def check_create_from_file(self, ext):
122n/a testfile = support.findfile('python.' + ext, subdir='imghdrdata')
123n/a image = tkinter.PhotoImage('::img::test', master=self.root,
124n/a file=testfile)
125n/a self.assertEqual(str(image), '::img::test')
126n/a self.assertEqual(image.type(), 'photo')
127n/a self.assertEqual(image.width(), 16)
128n/a self.assertEqual(image.height(), 16)
129n/a self.assertEqual(image['data'], '')
130n/a self.assertEqual(image['file'], testfile)
131n/a self.assertIn('::img::test', self.root.image_names())
132n/a del image
133n/a self.assertNotIn('::img::test', self.root.image_names())
134n/a
135n/a def check_create_from_data(self, ext):
136n/a testfile = support.findfile('python.' + ext, subdir='imghdrdata')
137n/a with open(testfile, 'rb') as f:
138n/a data = f.read()
139n/a image = tkinter.PhotoImage('::img::test', master=self.root,
140n/a data=data)
141n/a self.assertEqual(str(image), '::img::test')
142n/a self.assertEqual(image.type(), 'photo')
143n/a self.assertEqual(image.width(), 16)
144n/a self.assertEqual(image.height(), 16)
145n/a self.assertEqual(image['data'], data if self.wantobjects
146n/a else data.decode('latin1'))
147n/a self.assertEqual(image['file'], '')
148n/a self.assertIn('::img::test', self.root.image_names())
149n/a del image
150n/a self.assertNotIn('::img::test', self.root.image_names())
151n/a
152n/a def test_create_from_ppm_file(self):
153n/a self.check_create_from_file('ppm')
154n/a
155n/a def test_create_from_ppm_data(self):
156n/a self.check_create_from_data('ppm')
157n/a
158n/a def test_create_from_pgm_file(self):
159n/a self.check_create_from_file('pgm')
160n/a
161n/a def test_create_from_pgm_data(self):
162n/a self.check_create_from_data('pgm')
163n/a
164n/a def test_create_from_gif_file(self):
165n/a self.check_create_from_file('gif')
166n/a
167n/a def test_create_from_gif_data(self):
168n/a self.check_create_from_data('gif')
169n/a
170n/a @requires_tcl(8, 6)
171n/a def test_create_from_png_file(self):
172n/a self.check_create_from_file('png')
173n/a
174n/a @requires_tcl(8, 6)
175n/a def test_create_from_png_data(self):
176n/a self.check_create_from_data('png')
177n/a
178n/a def test_configure_data(self):
179n/a image = tkinter.PhotoImage('::img::test', master=self.root)
180n/a self.assertEqual(image['data'], '')
181n/a with open(self.testfile, 'rb') as f:
182n/a data = f.read()
183n/a image.configure(data=data)
184n/a self.assertEqual(image['data'], data if self.wantobjects
185n/a else data.decode('latin1'))
186n/a self.assertEqual(image.width(), 16)
187n/a self.assertEqual(image.height(), 16)
188n/a
189n/a def test_configure_format(self):
190n/a image = tkinter.PhotoImage('::img::test', master=self.root)
191n/a self.assertEqual(image['format'], '')
192n/a image.configure(file=self.testfile, format='gif')
193n/a self.assertEqual(image['format'], ('gif',) if self.wantobjects
194n/a else 'gif')
195n/a self.assertEqual(image.width(), 16)
196n/a self.assertEqual(image.height(), 16)
197n/a
198n/a def test_configure_file(self):
199n/a image = tkinter.PhotoImage('::img::test', master=self.root)
200n/a self.assertEqual(image['file'], '')
201n/a image.configure(file=self.testfile)
202n/a self.assertEqual(image['file'], self.testfile)
203n/a self.assertEqual(image.width(), 16)
204n/a self.assertEqual(image.height(), 16)
205n/a
206n/a def test_configure_gamma(self):
207n/a image = tkinter.PhotoImage('::img::test', master=self.root)
208n/a self.assertEqual(image['gamma'], '1.0')
209n/a image.configure(gamma=2.0)
210n/a self.assertEqual(image['gamma'], '2.0')
211n/a
212n/a def test_configure_width_height(self):
213n/a image = tkinter.PhotoImage('::img::test', master=self.root)
214n/a self.assertEqual(image['width'], '0')
215n/a self.assertEqual(image['height'], '0')
216n/a image.configure(width=20)
217n/a image.configure(height=10)
218n/a self.assertEqual(image['width'], '20')
219n/a self.assertEqual(image['height'], '10')
220n/a self.assertEqual(image.width(), 20)
221n/a self.assertEqual(image.height(), 10)
222n/a
223n/a def test_configure_palette(self):
224n/a image = tkinter.PhotoImage('::img::test', master=self.root)
225n/a self.assertEqual(image['palette'], '')
226n/a image.configure(palette=256)
227n/a self.assertEqual(image['palette'], '256')
228n/a image.configure(palette='3/4/2')
229n/a self.assertEqual(image['palette'], '3/4/2')
230n/a
231n/a def test_blank(self):
232n/a image = self.create()
233n/a image.blank()
234n/a self.assertEqual(image.width(), 16)
235n/a self.assertEqual(image.height(), 16)
236n/a self.assertEqual(image.get(4, 6), self.colorlist(0, 0, 0))
237n/a
238n/a def test_copy(self):
239n/a image = self.create()
240n/a image2 = image.copy()
241n/a self.assertEqual(image2.width(), 16)
242n/a self.assertEqual(image2.height(), 16)
243n/a self.assertEqual(image.get(4, 6), image.get(4, 6))
244n/a
245n/a def test_subsample(self):
246n/a image = self.create()
247n/a image2 = image.subsample(2, 3)
248n/a self.assertEqual(image2.width(), 8)
249n/a self.assertEqual(image2.height(), 6)
250n/a self.assertEqual(image2.get(2, 2), image.get(4, 6))
251n/a
252n/a image2 = image.subsample(2)
253n/a self.assertEqual(image2.width(), 8)
254n/a self.assertEqual(image2.height(), 8)
255n/a self.assertEqual(image2.get(2, 3), image.get(4, 6))
256n/a
257n/a def test_zoom(self):
258n/a image = self.create()
259n/a image2 = image.zoom(2, 3)
260n/a self.assertEqual(image2.width(), 32)
261n/a self.assertEqual(image2.height(), 48)
262n/a self.assertEqual(image2.get(8, 18), image.get(4, 6))
263n/a self.assertEqual(image2.get(9, 20), image.get(4, 6))
264n/a
265n/a image2 = image.zoom(2)
266n/a self.assertEqual(image2.width(), 32)
267n/a self.assertEqual(image2.height(), 32)
268n/a self.assertEqual(image2.get(8, 12), image.get(4, 6))
269n/a self.assertEqual(image2.get(9, 13), image.get(4, 6))
270n/a
271n/a def test_put(self):
272n/a image = self.create()
273n/a image.put('{red green} {blue yellow}', to=(4, 6))
274n/a self.assertEqual(image.get(4, 6), self.colorlist(255, 0, 0))
275n/a self.assertEqual(image.get(5, 6),
276n/a self.colorlist(0, 128 if tkinter.TkVersion >= 8.6
277n/a else 255, 0))
278n/a self.assertEqual(image.get(4, 7), self.colorlist(0, 0, 255))
279n/a self.assertEqual(image.get(5, 7), self.colorlist(255, 255, 0))
280n/a
281n/a image.put((('#f00', '#00ff00'), ('#000000fff', '#ffffffff0000')))
282n/a self.assertEqual(image.get(0, 0), self.colorlist(255, 0, 0))
283n/a self.assertEqual(image.get(1, 0), self.colorlist(0, 255, 0))
284n/a self.assertEqual(image.get(0, 1), self.colorlist(0, 0, 255))
285n/a self.assertEqual(image.get(1, 1), self.colorlist(255, 255, 0))
286n/a
287n/a def test_get(self):
288n/a image = self.create()
289n/a self.assertEqual(image.get(4, 6), self.colorlist(62, 116, 162))
290n/a self.assertEqual(image.get(0, 0), self.colorlist(0, 0, 0))
291n/a self.assertEqual(image.get(15, 15), self.colorlist(0, 0, 0))
292n/a self.assertRaises(tkinter.TclError, image.get, -1, 0)
293n/a self.assertRaises(tkinter.TclError, image.get, 0, -1)
294n/a self.assertRaises(tkinter.TclError, image.get, 16, 15)
295n/a self.assertRaises(tkinter.TclError, image.get, 15, 16)
296n/a
297n/a def test_write(self):
298n/a image = self.create()
299n/a self.addCleanup(support.unlink, support.TESTFN)
300n/a
301n/a image.write(support.TESTFN)
302n/a image2 = tkinter.PhotoImage('::img::test2', master=self.root,
303n/a format='ppm',
304n/a file=support.TESTFN)
305n/a self.assertEqual(str(image2), '::img::test2')
306n/a self.assertEqual(image2.type(), 'photo')
307n/a self.assertEqual(image2.width(), 16)
308n/a self.assertEqual(image2.height(), 16)
309n/a self.assertEqual(image2.get(0, 0), image.get(0, 0))
310n/a self.assertEqual(image2.get(15, 8), image.get(15, 8))
311n/a
312n/a image.write(support.TESTFN, format='gif', from_coords=(4, 6, 6, 9))
313n/a image3 = tkinter.PhotoImage('::img::test3', master=self.root,
314n/a format='gif',
315n/a file=support.TESTFN)
316n/a self.assertEqual(str(image3), '::img::test3')
317n/a self.assertEqual(image3.type(), 'photo')
318n/a self.assertEqual(image3.width(), 2)
319n/a self.assertEqual(image3.height(), 3)
320n/a self.assertEqual(image3.get(0, 0), image.get(4, 6))
321n/a self.assertEqual(image3.get(1, 2), image.get(5, 8))
322n/a
323n/a
324n/atests_gui = (MiscTest, BitmapImageTest, PhotoImageTest,)
325n/a
326n/aif __name__ == "__main__":
327n/a support.run_unittest(*tests_gui)