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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
import unittest
import os
import platform
import warnings
import pygame
DARWIN = "Darwin" in platform.platform()
class MouseTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
# The display needs to be initialized for mouse functions.
pygame.display.init()
@classmethod
def tearDownClass(cls):
pygame.display.quit()
class MouseModuleInteractiveTest(MouseTests):
__tags__ = ["interactive"]
def test_set_pos(self):
"""Ensures set_pos works correctly.
Requires tester to move the mouse to be on the window.
"""
pygame.display.set_mode((500, 500))
pygame.event.get() # Pump event queue to make window get focus on macos.
if not pygame.mouse.get_focused():
# The window needs to be focused for the mouse.set_pos to work on macos.
return
clock = pygame.time.Clock()
expected_pos = ((10, 0), (0, 0), (499, 0), (499, 499), (341, 143), (94, 49))
for x, y in expected_pos:
pygame.mouse.set_pos(x, y)
pygame.event.get()
found_pos = pygame.mouse.get_pos()
clock.tick()
time_passed = 0.0
ready_to_test = False
while not ready_to_test and time_passed <= 1000.0: # Avoid endless loop
time_passed += clock.tick()
for event in pygame.event.get():
if event.type == pygame.MOUSEMOTION:
ready_to_test = True
self.assertEqual(found_pos, (x, y))
class MouseModuleTest(MouseTests):
@unittest.skipIf(
os.environ.get("SDL_VIDEODRIVER", "") == "dummy",
"Cursors not supported on headless test machines",
)
def test_get_cursor(self):
"""Ensures get_cursor works correctly."""
# error should be raised when the display is uninitialized
with self.assertRaises(pygame.error):
pygame.display.quit()
pygame.mouse.get_cursor()
pygame.display.init()
size = (8, 8)
hotspot = (0, 0)
xormask = (0, 96, 120, 126, 112, 96, 0, 0)
andmask = (224, 240, 254, 255, 254, 240, 96, 0)
expected_length = 4
expected_cursor = pygame.cursors.Cursor(size, hotspot, xormask, andmask)
pygame.mouse.set_cursor(expected_cursor)
try:
cursor = pygame.mouse.get_cursor()
self.assertIsInstance(cursor, pygame.cursors.Cursor)
self.assertEqual(len(cursor), expected_length)
for info in cursor:
self.assertIsInstance(info, tuple)
pygame.mouse.set_cursor(size, hotspot, xormask, andmask)
self.assertEqual(pygame.mouse.get_cursor(), expected_cursor)
# SDLError should be raised when the mouse cursor is NULL
except pygame.error:
with self.assertRaises(pygame.error):
pygame.mouse.get_cursor()
@unittest.skipIf(
os.environ.get("SDL_VIDEODRIVER", "") == "dummy",
"mouse.set_system_cursor only available in SDL2",
)
def test_set_system_cursor(self):
"""Ensures set_system_cursor works correctly."""
with warnings.catch_warnings(record=True) as w:
"""From Pygame 2.0.1, set_system_cursor() should raise a deprecation warning"""
# Cause all warnings to always be triggered.
warnings.simplefilter("always")
# Error should be raised when the display is uninitialized
with self.assertRaises(pygame.error):
pygame.display.quit()
pygame.mouse.set_system_cursor(pygame.SYSTEM_CURSOR_HAND)
pygame.display.init()
# TypeError raised when PyArg_ParseTuple fails to parse parameters
with self.assertRaises(TypeError):
pygame.mouse.set_system_cursor("b")
with self.assertRaises(TypeError):
pygame.mouse.set_system_cursor(None)
with self.assertRaises(TypeError):
pygame.mouse.set_system_cursor((8, 8), (0, 0))
# Right type, invalid value
with self.assertRaises(pygame.error):
pygame.mouse.set_system_cursor(2000)
# Working as intended
self.assertEqual(
pygame.mouse.set_system_cursor(pygame.SYSTEM_CURSOR_ARROW), None
)
# Making sure the warnings are working properly
self.assertEqual(len(w), 6)
self.assertTrue(
all(issubclass(warn.category, DeprecationWarning) for warn in w)
)
@unittest.skipIf(
os.environ.get("SDL_VIDEODRIVER", "") == "dummy",
"Cursors not supported on headless test machines",
)
def test_set_cursor(self):
"""Ensures set_cursor works correctly."""
# Bitmap cursor information
size = (8, 8)
hotspot = (0, 0)
xormask = (0, 126, 64, 64, 32, 16, 0, 0)
andmask = (254, 255, 254, 112, 56, 28, 12, 0)
bitmap_cursor = pygame.cursors.Cursor(size, hotspot, xormask, andmask)
# System cursor information
constant = pygame.SYSTEM_CURSOR_ARROW
system_cursor = pygame.cursors.Cursor(constant)
# Color cursor information (also uses hotspot variable from Bitmap cursor info)
surface = pygame.Surface((10, 10))
color_cursor = pygame.cursors.Cursor(hotspot, surface)
pygame.display.quit()
# Bitmap: Error should be raised when the display is uninitialized
with self.assertRaises(pygame.error):
pygame.mouse.set_cursor(bitmap_cursor)
# System: Error should be raised when the display is uninitialized
with self.assertRaises(pygame.error):
pygame.mouse.set_cursor(system_cursor)
# Color: Error should be raised when the display is uninitialized
with self.assertRaises(pygame.error):
pygame.mouse.set_cursor(color_cursor)
pygame.display.init()
# Bitmap: TypeError raised when PyArg_ParseTuple fails to parse parameters
with self.assertRaises(TypeError):
pygame.mouse.set_cursor(("w", "h"), hotspot, xormask, andmask)
with self.assertRaises(TypeError):
pygame.mouse.set_cursor(size, ("0", "0"), xormask, andmask)
with self.assertRaises(TypeError):
pygame.mouse.set_cursor(size, ("x", "y", "z"), xormask, andmask)
# Bitmap: TypeError raised when either mask is not a sequence
with self.assertRaises(TypeError):
pygame.mouse.set_cursor(size, hotspot, 12345678, andmask)
with self.assertRaises(TypeError):
pygame.mouse.set_cursor(size, hotspot, xormask, 12345678)
# Bitmap: TypeError raised when element of mask is not an integer
with self.assertRaises(TypeError):
pygame.mouse.set_cursor(size, hotspot, "00000000", andmask)
with self.assertRaises(TypeError):
pygame.mouse.set_cursor(size, hotspot, xormask, (2, [0], 4, 0, 0, 8, 0, 1))
# Bitmap: ValueError raised when width not divisible by 8
with self.assertRaises(ValueError):
pygame.mouse.set_cursor((3, 8), hotspot, xormask, andmask)
# Bitmap: ValueError raised when length of either mask != width * height / 8
with self.assertRaises(ValueError):
pygame.mouse.set_cursor((16, 2), hotspot, (128, 64, 32), andmask)
with self.assertRaises(ValueError):
pygame.mouse.set_cursor((16, 2), hotspot, xormask, (192, 96, 48, 0, 1))
# Bitmap: Working as intended
self.assertEqual(
pygame.mouse.set_cursor((16, 1), hotspot, (8, 0), (0, 192)), None
)
pygame.mouse.set_cursor(size, hotspot, xormask, andmask)
self.assertEqual(pygame.mouse.get_cursor(), bitmap_cursor)
# Bitmap: Working as intended + lists + masks with no references
pygame.mouse.set_cursor(size, hotspot, list(xormask), list(andmask))
self.assertEqual(pygame.mouse.get_cursor(), bitmap_cursor)
# System: TypeError raised when constant is invalid
with self.assertRaises(TypeError):
pygame.mouse.set_cursor(-50021232)
with self.assertRaises(TypeError):
pygame.mouse.set_cursor("yellow")
# System: Working as intended
self.assertEqual(pygame.mouse.set_cursor(constant), None)
pygame.mouse.set_cursor(constant)
self.assertEqual(pygame.mouse.get_cursor(), system_cursor)
pygame.mouse.set_cursor(system_cursor)
self.assertEqual(pygame.mouse.get_cursor(), system_cursor)
# Color: TypeError raised with invalid parameters
with self.assertRaises(TypeError):
pygame.mouse.set_cursor(("x", "y"), surface)
with self.assertRaises(TypeError):
pygame.mouse.set_cursor(hotspot, "not_a_surface")
# Color: Working as intended
self.assertEqual(pygame.mouse.set_cursor(hotspot, surface), None)
pygame.mouse.set_cursor(hotspot, surface)
self.assertEqual(pygame.mouse.get_cursor(), color_cursor)
pygame.mouse.set_cursor(color_cursor)
self.assertEqual(pygame.mouse.get_cursor(), color_cursor)
# Color: Working as intended + Surface with no references is returned okay
pygame.mouse.set_cursor((0, 0), pygame.Surface((20, 20)))
cursor = pygame.mouse.get_cursor()
self.assertEqual(cursor.type, "color")
self.assertEqual(cursor.data[0], (0, 0))
self.assertEqual(cursor.data[1].get_size(), (20, 20))
def test_get_focused(self):
"""Ensures get_focused returns the correct type."""
focused = pygame.mouse.get_focused()
self.assertIsInstance(focused, int)
def test_get_pressed(self):
"""Ensures get_pressed returns the correct types."""
expected_length = 3
buttons_pressed = pygame.mouse.get_pressed()
self.assertIsInstance(buttons_pressed, tuple)
self.assertEqual(len(buttons_pressed), expected_length)
for value in buttons_pressed:
self.assertIsInstance(value, bool)
expected_length = 5
buttons_pressed = pygame.mouse.get_pressed(num_buttons=5)
self.assertIsInstance(buttons_pressed, tuple)
self.assertEqual(len(buttons_pressed), expected_length)
for value in buttons_pressed:
self.assertIsInstance(value, bool)
expected_length = 3
buttons_pressed = pygame.mouse.get_pressed(3)
self.assertIsInstance(buttons_pressed, tuple)
self.assertEqual(len(buttons_pressed), expected_length)
for value in buttons_pressed:
self.assertIsInstance(value, bool)
expected_length = 5
buttons_pressed = pygame.mouse.get_pressed(5)
self.assertIsInstance(buttons_pressed, tuple)
self.assertEqual(len(buttons_pressed), expected_length)
for value in buttons_pressed:
self.assertIsInstance(value, bool)
with self.assertRaises(ValueError):
pygame.mouse.get_pressed(4)
def test_get_pos(self):
"""Ensures get_pos returns the correct types."""
expected_length = 2
pos = pygame.mouse.get_pos()
self.assertIsInstance(pos, tuple)
self.assertEqual(len(pos), expected_length)
for value in pos:
self.assertIsInstance(value, int)
def test_set_pos__invalid_pos(self):
"""Ensures set_pos handles invalid positions correctly."""
for invalid_pos in ((1,), [1, 2, 3], 1, "1", (1, "1"), []):
with self.assertRaises(TypeError):
pygame.mouse.set_pos(invalid_pos)
def test_get_rel(self):
"""Ensures get_rel returns the correct types."""
expected_length = 2
rel = pygame.mouse.get_rel()
self.assertIsInstance(rel, tuple)
self.assertEqual(len(rel), expected_length)
for value in rel:
self.assertIsInstance(value, int)
def test_get_visible(self):
"""Ensures get_visible works correctly."""
for expected_value in (False, True):
pygame.mouse.set_visible(expected_value)
visible = pygame.mouse.get_visible()
self.assertEqual(visible, expected_value)
def test_set_visible(self):
"""Ensures set_visible returns the correct values."""
# Set to a known state.
pygame.mouse.set_visible(True)
for expected_visible in (False, True):
prev_visible = pygame.mouse.set_visible(expected_visible)
self.assertEqual(prev_visible, not expected_visible)
def test_set_visible__invalid_value(self):
"""Ensures set_visible handles invalid positions correctly."""
for invalid_value in ((1,), [1, 2, 3], 1.1, "1", (1, "1"), []):
with self.assertRaises(TypeError):
prev_visible = pygame.mouse.set_visible(invalid_value)
################################################################################
if __name__ == "__main__":
unittest.main()
|