forked from pygame-community/pygame-ce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtouch_test.py
110 lines (87 loc) · 3.65 KB
/
touch_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
import unittest
import os
import pygame
from pygame._sdl2 import touch
from pygame.tests.test_utils import question
class TouchTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
os.environ["SDL_MOUSE_TOUCH_EVENTS"] = "0"
pygame.init()
@classmethod
def tearDownClass(cls):
pygame.quit()
def test_num_devices(self):
touch.get_num_devices()
def test_get_device(self):
if touch.get_num_devices() > 0:
touch.get_device(0)
def test_get_device__invalid(self):
self.assertRaises(pygame.error, touch.get_device, -1234)
self.assertRaises(TypeError, touch.get_device, "test")
def test_num_fingers(self):
if touch.get_num_devices() > 0:
if touch.get_device(0) >= 0:
# mice acting as touch devices have negative device IDs
# they also do not work with get_num_fingers()
touch.get_num_fingers(touch.get_device(0))
def test_num_fingers__invalid(self):
self.assertRaises(TypeError, touch.get_num_fingers, "test")
self.assertRaises(pygame.error, touch.get_num_fingers, -1234)
class TouchInteractiveTest(unittest.TestCase):
__tags__ = ["interactive"]
@classmethod
def setUpClass(cls):
# if we don't have a touch device, enable the mouse to act as a
# touch device for the interactive tests.
os.environ["SDL_MOUSE_TOUCH_EVENTS"] = "0"
pygame.init()
if touch.get_num_devices() == 0:
pygame.quit()
os.environ["SDL_MOUSE_TOUCH_EVENTS"] = "1"
pygame.init()
@classmethod
def tearDownClass(cls):
pygame.quit()
def test_get_finger(self):
"""ask for touch input and check the dict"""
os.environ["SDL_VIDEO_WINDOW_POS"] = "50,50"
screen = pygame.display.set_mode((1000, 600))
screen.fill((255, 255, 255))
font = pygame.font.Font(None, 32)
instructions_str_1 = "Please place some fingers on your touch device"
instructions_str_2 = (
"Close the window when finished, " "and answer the question"
)
inst_1_render = font.render(instructions_str_1, True, pygame.Color("#000000"))
inst_2_render = font.render(instructions_str_2, True, pygame.Color("#000000"))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
finger_data_renders = []
num_devices = pygame._sdl2.touch.get_num_devices()
if num_devices > 0:
first_device = pygame._sdl2.touch.get_device(0)
if first_device >= 0:
num_fingers = pygame._sdl2.touch.get_num_fingers(first_device)
else:
num_fingers = 1
if num_fingers > 0:
for finger_index in range(0, num_fingers):
data = pygame._sdl2.touch.get_finger(first_device, finger_index)
render = font.render(
f"finger - {data}", True, pygame.Color("#000000")
)
finger_data_renders.append(render)
screen.fill((255, 255, 255))
screen.blit(inst_1_render, (5, 5))
screen.blit(inst_2_render, (5, 40))
for index, finger in enumerate(finger_data_renders):
screen.blit(finger, (5, 80 + (index * 40)))
pygame.display.update()
response = question("Does the finger data seem correct?")
self.assertTrue(response)
if __name__ == "__main__":
unittest.main()