forked from pygame/pygame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcursors_test.py
290 lines (257 loc) · 7.52 KB
/
cursors_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import unittest
from pygame.tests.test_utils import fixture_path
import pygame
class CursorsModuleTest(unittest.TestCase):
def test_compile(self):
# __doc__ (as of 2008-06-25) for pygame.cursors.compile:
# pygame.cursors.compile(strings, black, white,xor) -> data, mask
# compile cursor strings into cursor data
#
# This takes a set of strings with equal length and computes
# the binary data for that cursor. The string widths must be
# divisible by 8.
#
# The black and white arguments are single letter strings that
# tells which characters will represent black pixels, and which
# characters represent white pixels. All other characters are
# considered clear.
#
# This returns a tuple containing the cursor data and cursor mask
# data. Both these arguments are used when setting a cursor with
# pygame.mouse.set_cursor().
# Various types of input strings
test_cursor1 = ("X.X.XXXX", "XXXXXX..", " XXXX ")
test_cursor2 = (
"X.X.XXXX",
"XXXXXX..",
"XXXXXX ",
"XXXXXX..",
"XXXXXX..",
"XXXXXX",
"XXXXXX..",
"XXXXXX..",
)
test_cursor3 = (".XX.", " ", ".. ", "X.. X")
# Test such that total number of strings is not divisible by 8
with self.assertRaises(ValueError):
pygame.cursors.compile(test_cursor1)
# Test such that size of individual string is not divisible by 8
with self.assertRaises(ValueError):
pygame.cursors.compile(test_cursor2)
# Test such that neither size of individual string nor total number of strings is divisible by 8
with self.assertRaises(ValueError):
pygame.cursors.compile(test_cursor3)
# Test that checks whether the byte data from compile function is equal to actual byte data
actual_byte_data = (
192,
0,
0,
224,
0,
0,
240,
0,
0,
216,
0,
0,
204,
0,
0,
198,
0,
0,
195,
0,
0,
193,
128,
0,
192,
192,
0,
192,
96,
0,
192,
48,
0,
192,
56,
0,
192,
248,
0,
220,
192,
0,
246,
96,
0,
198,
96,
0,
6,
96,
0,
3,
48,
0,
3,
48,
0,
1,
224,
0,
1,
128,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
), (
192,
0,
0,
224,
0,
0,
240,
0,
0,
248,
0,
0,
252,
0,
0,
254,
0,
0,
255,
0,
0,
255,
128,
0,
255,
192,
0,
255,
224,
0,
255,
240,
0,
255,
248,
0,
255,
248,
0,
255,
192,
0,
247,
224,
0,
199,
224,
0,
7,
224,
0,
3,
240,
0,
3,
240,
0,
1,
224,
0,
1,
128,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
)
cursor = pygame.cursors.compile(pygame.cursors.thickarrow_strings)
self.assertEqual(cursor, actual_byte_data)
# Test such that cursor byte data obtained from compile function is valid in pygame.mouse.set_cursor()
pygame.display.init()
try:
pygame.mouse.set_cursor((24, 24), (0, 0), *cursor)
except pygame.error as e:
if "not currently supported" in str(e):
unittest.skip("skipping test as set_cursor() is not supported")
finally:
pygame.display.quit()
################################################################################
def test_load_xbm(self):
# __doc__ (as of 2008-06-25) for pygame.cursors.load_xbm:
# pygame.cursors.load_xbm(cursorfile, maskfile) -> cursor_args
# reads a pair of XBM files into set_cursor arguments
#
# Arguments can either be filenames or filelike objects
# with the readlines method. Not largely tested, but
# should work with typical XBM files.
# Test that load_xbm will take filenames as arguments
cursorfile = fixture_path(r"xbm_cursors/white_sizing.xbm")
maskfile = fixture_path(r"xbm_cursors/white_sizing_mask.xbm")
cursor = pygame.cursors.load_xbm(cursorfile, maskfile)
# Test that load_xbm will take file objects as arguments
with open(cursorfile) as cursor_f, open(maskfile) as mask_f:
cursor = pygame.cursors.load_xbm(cursor_f, mask_f)
# Can it load using pathlib.Path?
import pathlib
cursor = pygame.cursors.load_xbm(
pathlib.Path(cursorfile), pathlib.Path(maskfile)
)
# Is it in a format that mouse.set_cursor won't blow up on?
pygame.display.init()
try:
pygame.mouse.set_cursor(*cursor)
except pygame.error as e:
if "not currently supported" in str(e):
unittest.skip("skipping test as set_cursor() is not supported")
finally:
pygame.display.quit()
def test_Cursor(self):
"""Ensure that the cursor object parses information properly"""
c1 = pygame.cursors.Cursor(pygame.SYSTEM_CURSOR_CROSSHAIR)
self.assertEqual(c1.data, (pygame.SYSTEM_CURSOR_CROSSHAIR,))
self.assertEqual(c1.type, "system")
c2 = pygame.cursors.Cursor(c1)
self.assertEqual(c1, c2)
with self.assertRaises(TypeError):
pygame.cursors.Cursor(-34002)
with self.assertRaises(TypeError):
pygame.cursors.Cursor("a", "b", "c", "d")
with self.assertRaises(TypeError):
pygame.cursors.Cursor((2,))
c3 = pygame.cursors.Cursor((0, 0), pygame.Surface((20, 20)))
self.assertEqual(c3.data[0], (0, 0))
self.assertEqual(c3.data[1].get_size(), (20, 20))
self.assertEqual(c3.type, "color")
xormask, andmask = pygame.cursors.compile(pygame.cursors.thickarrow_strings)
c4 = pygame.cursors.Cursor((24, 24), (0, 0), xormask, andmask)
self.assertEqual(c4.data, ((24, 24), (0, 0), xormask, andmask))
self.assertEqual(c4.type, "bitmap")
################################################################################
if __name__ == "__main__":
unittest.main()
################################################################################