def test_none_hook_handler(self): """Test running a hook without a handler The next hook in chain still should be called Simulate an odd situation when we got a hook ID (a hook is inserted) but a handler for the hook processing wasn't supplied by a user """ self.hook.keyboard_id = self.fake_kbhook_id self.hook.mouse_id = self.fake_mousehook_id self.hook.handler = None # replace the system API with a mock object windll.user32.CallNextHookEx = mock.Mock(return_value=0) # prepare arguments for _keyboard_ll_hdl call kbd = win32structures.KBDLLHOOKSTRUCT(0, 0, 0, 0, 0) res = self.hook._keyboard_ll_hdl(-1, 3, id(kbd)) windll.user32.CallNextHookEx.assert_called_with( self.fake_kbhook_id, -1, 3, id(kbd)) self.assertEqual(res, 0) # Setup a fresh mock object and arguments for _mouse_ll_hdl call windll.user32.CallNextHookEx = mock.Mock(return_value=0) mouse = win32structures.MSLLHOOKSTRUCT((11, 12), 0, 0, 0, 0) res = self.hook._mouse_ll_hdl(-1, 3, id(mouse)) self.assertEqual(res, 0) windll.user32.CallNextHookEx.assert_called_with( self.fake_mousehook_id, -1, 3, id(mouse))
def test_mouse_hook_exception(self): """Test handling an exception in a mouse hook""" self.hook.handler = _on_hook_event_with_exception windll.user32.CallNextHookEx = mock.Mock(return_value=0) mouse = win32structures.MSLLHOOKSTRUCT((11, 12), 0, 0, 0, 0) self.hook.mouse_id = self.fake_mousehook_id # Verify CallNextHookEx is called even if there is an exception is raised self.assertRaises(ValueError, self.hook._mouse_ll_hdl, -1, 3, id(mouse)) windll.user32.CallNextHookEx.assert_called() windll.user32.CallNextHookEx.assert_called_with( self.fake_mousehook_id, -1, 3, id(mouse)) self.assertRaises(ValueError, self.hook._mouse_ll_hdl, 0, 3, id(mouse)) windll.user32.CallNextHookEx.assert_called() windll.user32.CallNextHookEx.assert_called_with( self.fake_mousehook_id, 0, 3, id(mouse))