1import unittest 2from pygame.tests.test_utils import fixture_path 3import pygame 4 5 6class CursorsModuleTest(unittest.TestCase): 7 def test_compile(self): 8 9 # __doc__ (as of 2008-06-25) for pygame.cursors.compile: 10 11 # pygame.cursors.compile(strings, black, white,xor) -> data, mask 12 # compile cursor strings into cursor data 13 # 14 # This takes a set of strings with equal length and computes 15 # the binary data for that cursor. The string widths must be 16 # divisible by 8. 17 # 18 # The black and white arguments are single letter strings that 19 # tells which characters will represent black pixels, and which 20 # characters represent white pixels. All other characters are 21 # considered clear. 22 # 23 # This returns a tuple containing the cursor data and cursor mask 24 # data. Both these arguments are used when setting a cursor with 25 # pygame.mouse.set_cursor(). 26 27 #Various types of input strings 28 test_cursor1 = ( 29 "X.X.XXXX", 30 "XXXXXX..", 31 " XXXX " 32 ) 33 34 test_cursor2 = ( 35 "X.X.XXXX", 36 "XXXXXX..", 37 "XXXXXX ", 38 "XXXXXX..", 39 "XXXXXX..", 40 "XXXXXX", 41 "XXXXXX..", 42 "XXXXXX.." 43 ) 44 test_cursor3 = ( 45 ".XX.", 46 " ", 47 ".. ", 48 "X.. X" 49 ) 50 51 # Test such that total number of strings is not divisible by 8 52 with self.assertRaises(ValueError): 53 pygame.cursors.compile(test_cursor1) 54 55 # Test such that size of individual string is not divisible by 8 56 with self.assertRaises(ValueError): 57 pygame.cursors.compile(test_cursor2) 58 59 # Test such that neither size of individual string nor total number of strings is divisible by 8 60 with self.assertRaises(ValueError): 61 pygame.cursors.compile(test_cursor3) 62 63 #Test that checks whether the byte data from compile funtion is equal to actual byte data 64 actual_byte_data = (192, 0, 0, 224, 0, 0, 240, 0, 0, 216, 0, 0, 65 204, 0, 0, 198, 0, 0, 195, 0, 0, 193, 128, 0, 192, 192, 0, 192, 96, 0, 192, 48, 0, 66 192, 56, 0, 192, 248, 0, 220, 192, 0, 246, 96, 0, 198, 96, 0, 6, 96, 0, 3, 48, 0, 67 3, 48, 0, 1, 224, 0, 1, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), (192, 0, 0, 224, 0, 0, 68 240, 0, 0, 248, 0, 0, 252, 0, 0, 254, 0, 0, 255, 0, 0, 255, 128, 0, 255, 192, 0, 255, 69 224, 0, 255, 240, 0, 255, 248, 0, 255, 248, 0, 255, 192, 0, 247, 224, 0, 199, 224, 70 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) 71 72 cursor = pygame.cursors.compile(pygame.cursors.thickarrow_strings) 73 self.assertEqual(cursor,actual_byte_data) 74 75 #Test such that cursor byte data obtained from compile function is valid in pygame.mouse.set_cursor() 76 pygame.display.init() 77 try: 78 pygame.mouse.set_cursor((24, 24), (0, 0), *cursor) 79 except pygame.error as e: 80 if "not currently supported" in str(e): 81 unittest.skip("skipping test as set_cursor() is not supported") 82 finally: 83 pygame.display.quit() 84 85 86################################################################################ 87 88 def test_load_xbm(self): 89 # __doc__ (as of 2008-06-25) for pygame.cursors.load_xbm: 90 91 # pygame.cursors.load_xbm(cursorfile, maskfile) -> cursor_args 92 # reads a pair of XBM files into set_cursor arguments 93 # 94 # Arguments can either be filenames or filelike objects 95 # with the readlines method. Not largely tested, but 96 # should work with typical XBM files. 97 98 # Test that load_xbm will take filenames as arguments 99 cursorfile = fixture_path(r"xbm_cursors/white_sizing.xbm") 100 maskfile = fixture_path(r"xbm_cursors/white_sizing_mask.xbm") 101 cursor = pygame.cursors.load_xbm(cursorfile, maskfile) 102 103 # Test that load_xbm will take file objects as arguments 104 with open(cursorfile) as cursor_f, open(maskfile) as mask_f: 105 cursor = pygame.cursors.load_xbm(cursor_f, mask_f) 106 107 # Is it in a format that mouse.set_cursor won't blow up on? 108 pygame.display.init() 109 try: 110 pygame.mouse.set_cursor(*cursor) 111 except pygame.error as e: 112 if "not currently supported" in str(e): 113 unittest.skip("skipping test as set_cursor() is not supported") 114 finally: 115 pygame.display.quit() 116 117 118 def test_Cursor(self): 119 """Ensure that the cursor object parses information properly""" 120 121 c1 = pygame.cursors.Cursor(pygame.SYSTEM_CURSOR_CROSSHAIR) 122 123 self.assertEqual(c1.data, (pygame.SYSTEM_CURSOR_CROSSHAIR,)) 124 self.assertEqual(c1.type, "system") 125 126 c2 = pygame.cursors.Cursor(c1) 127 128 self.assertEqual(c1, c2) 129 130 with self.assertRaises(TypeError): 131 pygame.cursors.Cursor(-34002) 132 with self.assertRaises(TypeError): 133 pygame.cursors.Cursor("a", "b", "c", "d") 134 with self.assertRaises(TypeError): 135 pygame.cursors.Cursor((2,)) 136 137 c3 = pygame.cursors.Cursor((0,0), pygame.Surface((20,20))) 138 139 self.assertEqual(c3.data[0], (0,0)) 140 self.assertEqual(c3.data[1].get_size(), (20, 20)) 141 self.assertEqual(c3.type, "color") 142 143 xormask, andmask = pygame.cursors.compile(pygame.cursors.thickarrow_strings) 144 c4 = pygame.cursors.Cursor((24, 24), (0, 0), xormask, andmask) 145 146 self.assertEqual(c4.data, ((24,24), (0,0), xormask, andmask)) 147 self.assertEqual(c4.type, "bitmap") 148 149################################################################################ 150 151if __name__ == "__main__": 152 unittest.main() 153 154################################################################################ 155