Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import pynput.mouse
import time
from . import EventTest, darwin, win32, xorg
class MouseListenerTest(EventTest):
NOTIFICATION = (
'This test case is interactive, so you must follow the instructions '
'on screen.\n'
'You do not have to perform any actions on this specific window.')
LISTENER_CLASS = pynput.mouse.Listener
def test_cursor(self):
"""Asserts that all cursor types defined for the base interface are
defined for the current platform"""
from pynput.mouse._base import Cursor
for cursor in Cursor:
self.assertTrue(
hasattr(pynput.mouse.Cursor, cursor.name),
'%s is not defined for the current platform' % cursor.name)
def test_stop(self):
"""Tests that stopping the listener from a different thread works"""
listener = self.listener()
listener.start()
listener.wait()
def test_options_win32(self):
"""Tests that options are correctly set on Windows"""
self.assertTrue(
pynput.keyboard.Listener(
darwin_test=False,
win32_test=True,
xorg_test=False)._options['test'])
def test_controller_events(self):
"""Tests that events sent by a controller are received correctly"""
with self.assert_event(
'Failed to send press',
on_press=lambda k: getattr(k, 'char', None) == u'a'):
self.controller.press(u'a')
with self.assert_event(
'Failed to send release',
on_release=lambda k: getattr(k, 'char', None) == u'a'):
self.controller.release(u'a')
self.controller.press(pynput.keyboard.Key.enter)
self.controller.release(pynput.keyboard.Key.enter)
input()
def normalize(event):
if not isinstance(event[0], tuple):
return normalize(((event[0],), event[1]))
key, is_pressed = event
return (
tuple(
pynput.keyboard.KeyCode.from_char(key)
if isinstance(key, six.string_types)
else key.value if key in pynput.keyboard.Key
else key
for key in event[0]),
is_pressed)
def normalize(event):
if not isinstance(event[0], tuple):
return normalize(((event[0],), event[1]))
key, is_pressed = event
return (
tuple(
pynput.keyboard.KeyCode.from_char(key)
if isinstance(key, six.string_types)
else key.value if key in pynput.keyboard.Key
else key
for key in event[0]),
is_pressed)
def test_get_mouse_listener_returns_listener_instance(self, mocker):
returned = get_mouse_listener(mocker.MagicMock())
assert isinstance(returned, mouse.Listener)
def test_reraise(self):
"""Tests that exception are reraised"""
class MyException(Exception): pass
def on_click(x, y, button, pressed):
raise MyException()
with self.assertRaises(MyException):
with pynput.mouse.Listener(
on_click=on_click) as l:
self.notify('Click any button')
l.join()
def __init__(self, name):
self._name = name
self.keyboard = Controller()
import sys
import threading
import pynput.keyboard
from six.moves import input
from . import EventTest
class KeyboardControllerTest(EventTest):
NOTIFICATION = (
'This test case is non-interactive, so you must not use the '
'keyboard.\n'
'You must, however, keep this window focused.')
CONTROLLER_CLASS = pynput.keyboard.Controller
LISTENER_CLASS = pynput.keyboard.Listener
def decode(self, string):
"""Decodes a string read from ``stdin``.
:param str string: The string to decode.
"""
if sys.version_info.major >= 3:
yield string
else:
for encoding in (
'utf-8',
locale.getpreferredencoding(),
sys.stdin.encoding):
if encoding:
try:
def __init__(self, addr=DEFAULT_ADDR):
self.reactor = None
self.addr = addr
self.running = False
self.root = None
self.keyboard = Controller()