def test_handle_relative(self, mock_get_relative): """Relative mouse movement produces events.""" pipe = mock.MagicMock() listener = inputs.QuartzMouseBaseListener(pipe) # We begin with no evdev events self.assertEqual(listener.events, []) # We have a Quartz event event = mock.MagicMock() # Let's call the method that we care about listener.handle_relative(event) # Now let's see what happened # get_relative was called mock_get_relative.assert_called_once() # Do we have events self.assertEqual(len(listener.events), 2) first_event = next(inputs.iter_unpack( listener.events[0])) self.assertEqual(first_event[2:], (2, 0, 600)) second_event = next(inputs.iter_unpack( listener.events[1])) self.assertEqual(second_event[2:], (2, 1, 400))
def test_handle_scrollwheel(self, mock_get_scroll): """Scroll wheel produces events.""" pipe = mock.MagicMock() listener = inputs.QuartzMouseBaseListener(pipe) # We begin with no evdev events self.assertEqual(listener.events, []) # We (pretend that we) have a Quartz event event = mock.MagicMock() # Let's call the method that we care about listener.handle_scrollwheel(event) # Now let's see what happened # get_scroll was called mock_get_scroll.assert_called_once() # Do we have events self.assertEqual(len(listener.events), 2) first_event = next(inputs.iter_unpack( listener.events[0])) self.assertEqual(first_event[2:], (2, 6, 2)) second_event = next(inputs.iter_unpack( listener.events[1])) self.assertEqual(second_event[2:], (2, 8, 2))
def test_get_key_value_other_type(self): """Unknown event type should return -1.""" pipe = mock.MagicMock() listener = inputs.AppKitKeyboardListener(pipe) event = mock.MagicMock() key_value = listener._get_key_value(event, 15) self.assertEqual(key_value, -1)
def test_handle_middle_button(self, mock_get_mouse_button_number, mock_get_click_state): """Convert quartz events to evdev events.""" pipe = mock.MagicMock() listener = inputs.QuartzMouseBaseListener(pipe) # We begin with no events self.assertEqual(listener.events, []) event = mock.MagicMock() listener.handle_button(event, 26) # _get_mouse_button_number was called mock_get_mouse_button_number.assert_called_once() # get_click_state was called mock_get_click_state.assert_called_once() # Now there are three events self.assertEqual(len(listener.events), 3) first_event = next(inputs.iter_unpack( listener.events[0])) self.assertEqual(first_event[2:], (4, 4, 589827)) second_event = next(inputs.iter_unpack( listener.events[1])) self.assertEqual(second_event[2:], (1, 274, 0)) third_event = next(inputs.iter_unpack( listener.events[2])) self.assertEqual(third_event[2:], (20, 2, 1))
def test_get_key_value_type_11(self): """Event type 11 should return 0.""" pipe = mock.MagicMock() listener = inputs.AppKitKeyboardListener(pipe) event = mock.MagicMock() key_value = listener._get_key_value(event, 11) self.assertEqual(key_value, 0)
def test_handle_relative(self, mock_get_deltas): """Relative position is processed.""" pipe = mock.MagicMock() listener = inputs.AppKitMouseBaseListener(pipe) # Events begin empty self.assertEqual(listener.events, []) event = mock.MagicMock() # Run the method under test listener.handle_relative(event) # Check that we have events self.assertEqual(len(listener.events), 3) first_event = next(inputs.iter_unpack( listener.events[0])) self.assertEqual(first_event[2:], (2, 0, 5)) second_event = next(inputs.iter_unpack( listener.events[1])) self.assertEqual(second_event[2:], (2, 1, 5)) third_event = next(inputs.iter_unpack( listener.events[2])) self.assertEqual(third_event[2:], (2, 2, 5)) mock_get_deltas.assert_called_once_with(event)
def test_get_key_value_type_12(self): """Event type 12 should check the flag value.""" pipe = mock.MagicMock() listener = inputs.AppKitKeyboardListener(pipe) event = mock.MagicMock() key_value = listener._get_key_value(event, 12) self.assertEqual(key_value, 1)
def test_detect_gamepads(self, mock_windll, mock_gamepad): """It appends the correct number of gamepads.""" self.device_manager.xinput = mock.MagicMock() xinputgetstate = mock.MagicMock(return_value=0) self.device_manager.xinput.attach_mock(xinputgetstate, 'XInputGetState') self.device_manager._detect_gamepads() self.assertEqual(len(self.device_manager.gamepads), 4)
def test_get_mouse_button_number(self): """Get mouse number calls buttonNumber method.""" pipe = mock.MagicMock() listener = inputs.AppKitMouseBaseListener(pipe) event = mock.MagicMock() button_number = listener._get_mouse_button_number(event) call = event.method_calls[0] self.assertEqual(call[0], 'buttonNumber') button_number.assert_not_called()
def test_get_flag_value(self): """Get event flags calls event.modifierFlags().""" pipe = mock.MagicMock() listener = inputs.AppKitKeyboardListener(pipe) event = mock.MagicMock() flag_value = listener._get_flag_value(event) call = event.method_calls[0] self.assertEqual(call[0], 'modifierFlags') self.assertEqual(flag_value, 1)
def test_detect_error_gamepads(self, mock_windll, mock_gamepad): """It raises an exception if a problem getting gamepad state.""" self.device_manager.xinput = mock.MagicMock() xinputgetstate = mock.MagicMock(return_value=1) self.device_manager.xinput.attach_mock(xinputgetstate, 'XInputGetState') with self.assertRaises(RuntimeError): self.device_manager._detect_gamepads() self.assertEqual(len(self.device_manager.gamepads), 0)
def test_get_event_type(self): """Get event type called type().""" pipe = mock.MagicMock() listener = inputs.AppKitKeyboardListener(pipe) event = mock.MagicMock() event_type = listener._get_event_type(event) call = event.method_calls[0] self.assertEqual(call[0], 'type') event_type.assert_not_called()
def test_get_absolute(self): """Get absolute calls locationInWindow method.""" pipe = mock.MagicMock() listener = inputs.AppKitMouseBaseListener(pipe) event = mock.MagicMock() button_number = listener._get_absolute(event) call = event.method_calls[0] self.assertEqual(call[0], 'locationInWindow') button_number.assert_not_called()
def test_get_gamepad(self, devices): """Get key reads from the first gamepad.""" gamepad = mock.MagicMock() reader = mock.MagicMock() gamepad.read = reader devices.gamepads = [gamepad] inputs.get_gamepad() reader.assert_called_once()
def test_get_mouse(self, devices): """Get event reads from the first mouse.""" mouse = mock.MagicMock() reader = mock.MagicMock() mouse.read = reader devices.mice = [mouse] inputs.get_mouse() reader.assert_called_once()
def test_get_key(self, devices): """Get key reads from the first keyboard.""" keyboard = mock.MagicMock() reader = mock.MagicMock() keyboard.read = reader devices.keyboards = [keyboard] inputs.get_key() reader.assert_called_once()
def test_get_data(self, mock_character_device, mock_set_name): """InputDevice._get_data reads data from the character device.""" mock_read = mock.MagicMock(return_value='Good Evening') mock_character_device.attach_mock(mock_read, 'read') manager = mock.MagicMock() inputdevice = inputs.InputDevice(manager, KBD_PATH) mock_set_name.assert_called() result = inputdevice._get_data(24) self.assertEqual(result, 'Good Evening') mock_read.assert_called_once_with(24)
def test_get_flag_value_something(self): """Get event flags calls event.modifierFlags().""" pipe = mock.MagicMock() listener = inputs.AppKitKeyboardListener(pipe) event = mock.MagicMock() modifier_flags = mock.MagicMock(return_value=256) event.attach_mock(modifier_flags, 'modifierFlags') flag_value = listener._get_flag_value(event) call = event.method_calls[0] self.assertEqual(call[0], 'modifierFlags') self.assertEqual(flag_value, 0)
def test_get_deltas(self): """Get deltas calls delta methods.""" pipe = mock.MagicMock() listener = inputs.AppKitMouseBaseListener(pipe) event = mock.MagicMock() button_number = listener._get_deltas(event) self.assertEqual(len(button_number), 3) # Check the three method names were called. cartesian = ('X', 'Y', 'Z') for index, call in enumerate(event.method_calls): method_name = 'delta' + cartesian[index] self.assertEqual(call[0], method_name)
def setup_mock_manager(): """Make a mock that works like a DeviceManager.""" manager = mock.MagicMock() manager.get_typecode.return_value = 17 manager.codes.__contains__.side_effect = CODES_DICT.__contains__ manager.codes.__getitem__.side_effect = CODES_DICT.__getitem__ mock_device = mock.MagicMock(name='mock_device') mock_get_char_device_path = mock.MagicMock(name='charpath') mock_device.attach(mock_get_char_device_path, 'get_char_device_path') mock_device.get_char_device_path.return_value = CHARPATH manager.all_devices.__iter__.return_value = [mock_device] return manager
def test_handle_button(self, mock_get_mouse_button_number): """Mouse click produces an event.""" pipe = mock.MagicMock() listener = inputs.AppKitMouseBaseListener(pipe) # Events begin empty self.assertEqual(listener.events, []) event = mock.MagicMock(return_value=1) listener.handle_button(event, 25) self.assertEqual(len(listener.events), 2) first_event = next(inputs.iter_unpack( listener.events[0])) self.assertEqual(first_event[2:], (4, 4, 589827)) second_event = next(inputs.iter_unpack( listener.events[1])) self.assertEqual(second_event[2:], (1, 274, 1))
def test_get_total_read_size(self, mock_set_name): """InputDevice.get_total_read_size returns how much data to process.""" manager = mock.MagicMock() inputdevice = inputs.InputDevice(manager, KBD_PATH) mock_set_name.assert_called() size = inputdevice._get_total_read_size() self.assertEqual(size, inputs.EVENT_SIZE)
def test_get_timeval(self): """Gives seconds and microseconds.""" pipe = mock.MagicMock() listener = inputs.BaseListener(pipe) seconds, microseconds = listener.get_timeval() self.assertTrue(seconds > 0) self.assertTrue(microseconds > 0)
def test_get_char_name(self, mock_realpath, mock_set_name): """It gives the short version of the char name.""" mock_realpath.side_effect = lambda path: EV_PATH manager = mock.MagicMock() inputdevice = inputs.InputDevice(manager, KBD_PATH) self.assertEqual(inputdevice.get_char_name(), 'event4') mock_set_name.assert_called()
def test_character_device(self, mock_io_open, mock_set_name): """InputDevice has a character device property.""" manager = mock.MagicMock() inputdevice = inputs.InputDevice(manager, KBD_PATH) self.assertEqual(inputdevice._character_device, CHARFILE) mock_io_open.assert_called() mock_set_name.assert_called()
def test_create_key_event_object(self): """It should create an evdev object.""" pipe = mock.MagicMock() listener = inputs.BaseListener(pipe) eventlist = listener.create_event_object("Key", 30, 1, (100, 0)) event_info = next(inputs.iter_unpack(eventlist)) self.assertEqual(event_info, (100, 0, 1, 30, 1))
def test_get_target_function(self, mock_set_name): """InputDevice._get_target_function returns false.""" manager = mock.MagicMock() inputdevice = inputs.InputDevice(manager, KBD_PATH) mock_set_name.assert_called() result = inputdevice._get_target_function() self.assertEqual(result, False)
def test_create_mouse_event_object(self): """It also should create an evdev object.""" pipe = mock.MagicMock() listener = inputs.BaseListener(pipe) eventlist = listener.create_event_object("Absolute", 0, 285, (100, 1)) event_info = next(inputs.iter_unpack(eventlist)) self.assertEqual(event_info, (100, 1, 3, 0, 285))
def test_init_device_path_is_none(self): """With a device path of None, it has a device path.""" manager = mock.MagicMock() inputs.InputDevice._device_path = None with self.assertRaises(inputs.NoDevicePath): inputs.InputDevice(manager) del inputs.InputDevice._device_path
def test_handle_input_unknown_code(self, mock_get_event_key_code, mock_get_event_type, mock_get_flags, mock_write_to_pipe): """Mac Keyboard events are processed.""" pipe = mock.MagicMock() listener = inputs.AppKitKeyboardListener(pipe) event = mock.MagicMock() listener.handle_input(event) self.assertEqual(len(listener.events), 3) event_info = inputs.iter_unpack(listener.events[1]) self.assertEqual(next(event_info)[2:], (1, 0, 1)) mock_get_event_key_code.assert_called_once_with(event) mock_get_event_type.assert_called_once_with(event) mock_write_to_pipe.assert_called_once_with(listener.events)