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_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_abstract_methods(self):
        """Test that they raise an exception."""
        pipe = mock.MagicMock()
        listener = inputs.QuartzMouseBaseListener(pipe)

        event = mock.MagicMock()
        with self.assertRaises(NotImplementedError):
            listener._get_mouse_button_number(event)
        event.assert_not_called()

        event = mock.MagicMock()
        with self.assertRaises(NotImplementedError):
            listener._get_click_state(event)
        event.assert_not_called()

        event = mock.MagicMock()
        with self.assertRaises(NotImplementedError):
            listener._get_scroll(event)
        event.assert_not_called()

        event = mock.MagicMock()
        with self.assertRaises(NotImplementedError):
            listener._get_absolute(event)
        event.assert_not_called()

        event = mock.MagicMock()
        with self.assertRaises(NotImplementedError):
            listener._get_relative(event)
        event.assert_not_called()
 def test_init(self):
     """The created object has properties."""
     pipe = mock.MagicMock()
     listener = inputs.QuartzMouseBaseListener(pipe)
     self.assertTrue(listener.active)
     self.assertEqual(
         listener.codes[1],
         ('Key', 272, 1, 589825))
    def test_handle_input(self,
                          mock_handle_button,
                          mock_handle_absolute,
                          mock_handle_relative):
        """Input events from Quartz are handled with the correct method."""
        pipe = mock.MagicMock()
        listener = inputs.QuartzMouseBaseListener(pipe)
        event = mock.MagicMock()
        listener.handle_input(None, 1, event, None)

        # So what happened?

        # The functions were called
        mock_handle_button.assert_called_once_with(event, 1)
        mock_handle_absolute.assert_called_once_with(event)
        mock_handle_relative.assert_called_once_with(event)

        # The sync marker was added
        self.assertEqual(len(listener.events), 1)
        first_event = next(inputs.iter_unpack(
            listener.events[0]))
        self.assertEqual(first_event[2:], (0, 0, 0))