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