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