Skip to content

Added unit tests for handling key events in processing:core #966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 21, 2025

Conversation

Rishab87
Copy link
Contributor

@Rishab87 Rishab87 commented Mar 8, 2025

I've added unit tests to test key events. Here are the tests that I added:

testSingleKeyPressAndRelease
testShiftAndLetterSequence
testControlAndLetterSequence
testAltAndLetterSequence
testKeyRepeat
testKeyRepeatEnabled
testKeyFocusLost

As expected testControlAndLetterSequence and testShiftAndLetterSequence fail (see #779)

Fixes #965

Update this PR also adds a fix to #779 , now all tests pass

Update, this also fixes #824

@Rishab87
Copy link
Contributor Author

Just a follow up anything else I need to change here?

@SableRaf SableRaf requested a review from Stefterv March 10, 2025 13:09
@SableRaf
Copy link
Collaborator

Hey @Rishab87, thanks for your contribution! We're busy right now preparing the release of Processing 4.4 but we'll review your PR as soon as we can.

@Rishab87
Copy link
Contributor Author

Rishab87 commented Mar 10, 2025

Thanks for letting me know @SableRaf, please take your time.

@Rishab87
Copy link
Contributor Author

Also apart from this should I start with addition of more tests by then?

@SableRaf
Copy link
Collaborator

Also apart from this should I start with addition of more tests by then?

If you'd like, but I'd suggest rather focusing on your GSoC application in the meantime :)

@Rishab87
Copy link
Contributor Author

Sure @SableRaf , I'll focus on my application then in the meantime.

@Rishab87
Copy link
Contributor Author

Rishab87 commented Mar 13, 2025

@SableRaf I've created first draft of my proposal, so should I start with writing other unit tests, like for math related functionalities their is no unit test currently present for it so should I continue with it?

@Rishab87
Copy link
Contributor Author

I've pushed some changes which fixes #779 but I don't think it's the most optimised approach. It unregisters all the hashes with that key code

The issue seems to be when we press Ctrl and then A, Ctrl gets registered and ctrl+A gets registered, when we release ctrl we release 2 keys, ctrl and ctrl+A at the same time

I'm open to hearing your thoughts on this, will make changes accordingly.

Copy link
Collaborator

@Stefterv Stefterv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Rishab87 Looks good, just a small readability improvement and some cleanup

case KeyEvent.RELEASE -> {
pressedKeys.remove(((long) keyCode << Character.SIZE) | key);
case KeyEvent.RELEASE -> {
List<Long> hashesToRemove = new ArrayList<>();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead we can do

    pressedKeys.removeIf(hash -> {
      int pressedKeyCode = (int)(hash >> Character.SIZE);
      return pressedKeyCode == keyCode;
    });

applet.handleKeyEvent(releaseShift);

Assert.assertFalse("keyPressed should be false after all keys released", applet.keyPressed);
Assert.assertEquals("pressedKeys should be empty", true, applet.pressedKeys.isEmpty());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use assertTrue

applet.handleKeyEvent(releaseAlt);

Assert.assertFalse("keyPressed should be false after all keys released", applet.keyPressed);
Assert.assertEquals("pressedKeys should be empty", true, applet.pressedKeys.isEmpty());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

@Rishab87
Copy link
Contributor Author

Thanks for the review @Stefterv , I've made those changes

@Rishab87 Rishab87 requested a review from Stefterv March 14, 2025 09:06
@SableRaf SableRaf merged commit 0ace796 into processing:main Mar 21, 2025
11 checks passed
@SableRaf
Copy link
Collaborator

@all-contributors please add @Rishab87 for code

Copy link
Contributor

@SableRaf

I've put up a pull request to add @Rishab87! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants