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