Skip to content

Commit

Permalink
Merge pull request #1967 from Reminisque/mouse-set-cursor-unit-test
Browse files Browse the repository at this point in the history
Add mouse.set_cursor unit test
  • Loading branch information
Dan Lawrence committed Jul 10, 2020
2 parents 599a76c + b9a65a1 commit 6dbe050
Showing 1 changed file with 117 additions and 2 deletions.
119 changes: 117 additions & 2 deletions test/mouse_test.py
Expand Up @@ -95,9 +95,124 @@ def test_get_cursor(self):
with self.assertRaises(pygame.error):
pygame.mouse.get_cursor()

def todo_test_set_cursor(self):
@unittest.skipIf(not SDL1, "mouse.get_cursor only available in SDL1")
def test_set_cursor_sdl1(self):
"""Ensures set_cursor works correctly."""
self.fail()
size = (8, 8)
hotspot = (0, 0)
xormask = (0, 126, 64, 64, 32, 16, 0, 0)
andmask = (254, 255, 254, 112, 56, 28, 12, 0)
expected_cursor = (size, hotspot, xormask, andmask)

# Error should be raised when the display is uninitialized
with self.assertRaises(pygame.error):
pygame.display.quit()
pygame.mouse.set_cursor(size, hotspot, xormask, andmask)

pygame.display.init()

# 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)

# 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)

# 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))

# ValueError raised when width not divisible by 8
with self.assertRaises(ValueError):
pygame.mouse.set_cursor((3, 8), hotspot, xormask, andmask)

# 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))

# Working as intended
try:
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(), expected_cursor)
pygame.mouse.set_cursor(size, hotspot, list(xormask), list(andmask))
self.assertEqual(pygame.mouse.get_cursor(), expected_cursor)

# SDLError should be raised when mouse cursor is NULL
except pygame.error:
with self.assertRaises(pygame.error):
pygame.mouse.set_cursor(size, hotspot, xormask, andmask)


@unittest.skipIf(SDL1 or os.environ.get("SDL_VIDEODRIVER", "") == "dummy",
"Fails on SDL2 with dummy video driver, unable to use mouse.get_cursor")
def test_set_cursor_sdl2(self):
"""Ensures set_cursor works correctly."""
size = (8, 8)
hotspot = (0, 0)
xormask = (0, 126, 64, 64, 32, 16, 0, 0)
andmask = (254, 255, 254, 112, 56, 28, 12, 0)

# Error should be raised when the display is uninitialized
with self.assertRaises(pygame.error):
pygame.display.quit()
pygame.mouse.set_cursor(size, hotspot, xormask, andmask)

pygame.display.init()

# 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)

# 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)

# 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))

# ValueError raised when width not divisible by 8
with self.assertRaises(ValueError):
pygame.mouse.set_cursor((3, 8), hotspot, xormask, andmask)

# 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))

# Working as intended, no checks for same value as mouse.get_cursor in SDL2
try:
self.assertEqual(
pygame.mouse.set_cursor(size, hotspot, xormask, andmask), None)
self.assertEqual(
pygame.mouse.set_cursor(size, hotspot, list(xormask), list(andmask)), None)

# SDLError should be raised when mouse cursor is NULL
except pygame.error:
with self.assertRaises(pygame.error):
pygame.mouse.set_cursor(size, hotspot, xormask, andmask)

def test_get_focused(self):
"""Ensures get_focused returns the correct type."""
Expand Down

0 comments on commit 6dbe050

Please sign in to comment.