Skip to content

Trigger events via standard callbacks in widget testing. #29993

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented May 1, 2025

Sending actual events through the whole event processing pipeline is a more complete test, reveals a few minor issues (see changes below), and avoids being linked to the rather nonstandard widget method names ("press" or "_click"?).

The coordinates in the "move first vertex after completing the polygon" subtest of test_polygon_selector(draw_bounding_box=True) were altered because the original coordinates would actually not work in a real case, as the mouse-drag would actually also trigger the polygon-rescaling behavior.

The coordinates in test_rectangle_{drag,resize} were altered because for the original coordinates, the click_and_drag would actually be ignore()d due to starting (just) outside of the axes.

Closes #22720.

PR summary

PR checklist

@@ -137,7 +135,7 @@ def test_rectangle_drag(ax, drag_from_anywhere, new_center):
tool = widgets.RectangleSelector(ax, interactive=True,
drag_from_anywhere=drag_from_anywhere)
# Create rectangle
click_and_drag(tool, start=(0, 10), end=(100, 120))
click_and_drag(tool, start=(10, 10), end=(90, 120))
Copy link
Member

Choose a reason for hiding this comment

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

The positions in the last part of this test are also outside both before and after rectangles, so just checking if those are still valid, or are they actually outside the Axes just as these were (and thus not checking what was intended)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The axes default to ranging from 0 to 200, so the last part's rectangles are within the axes.

@dstansby
Copy link
Member

dstansby commented May 5, 2025

👍 to removing do_event() from the public API, but it is a useful helper for testing. Why not keep it around (rename to _do_event()), and change the function definition to MouseEvent._from_ax_coords(...)._process()? That way it would avoid having to remember to have to call _from_ax_coords() and _process() across the tests.

@anntzer
Copy link
Contributor Author

anntzer commented May 5, 2025

The problem is that right now do_event takes a (not-standardized) widget method name as parameter. I guess, sure, we could completely change the signature and have a private do_event("button_press_event", ax, xycoords, button) helper method, but if we really think that MouseEvent._from_ax_coords(...)._process() is not ergonomic enough, perhaps that rather points to the fact that that API should be reworked anyways (which is not something I really want to go into right now)?

@timhoffm
Copy link
Member

I guess, sure, we could completely change the signature and have a private do_event("button_press_event", ax, xycoords, button) helper method, but if we really think that MouseEvent._from_ax_coords(...)._process() is not ergonomic enough

I'm +/-0 here. MouseEvent._from_ax_coords(...)._process() is not really elegant:

  • We now have an asymmetry in that some events can be created via the constructor, and some via the class method
    KeyEvent._from_ax_coords("key_release_event", ax, end, key)._process()
    KeyEvent("key_press_event", ax.figure.canvas, "escape")._process()
    Note also the difference in passing ax vs. ax.figure.canvas
  • explicitly calling _process feels a bit low-level. That was more elegant in do_event.

OTOH I don't have a good API either.

@tacaswell tacaswell added this to the v3.11.0 milestone Jul 24, 2025
Sending actual events through the whole event processing pipeline is a
more complete test, reveals a few minor issues (see changes below), and
avoids being linked to the rather nonstandard widget method names
("press" or "_click"?).

The coordinates in the "move first vertex after completing the polygon"
subtest of test_polygon_selector(draw_bounding_box=True) were altered
because the original coordinates would actually not work in a real case,
as the mouse-drag would actually also trigger the polygon-rescaling
behavior.

The coordinates in test_rectangle_{drag,resize} were altered because for
the original coordinates, the click_and_drag would actually be ignore()d
due to starting (just) outside of the axes.
@anntzer
Copy link
Contributor Author

anntzer commented Jul 25, 2025

We now have an asymmetry in that some events can be created via the constructor, and some via the class method

KeyEvent._from_ax_coords("key_release_event", ax, end, key)._process()
KeyEvent("key_press_event", ax.figure.canvas, "escape")._process()

Note also the difference in passing ax vs. ax.figure.canvas

Sure, we could require the caller to also pass ax.figure.canvas when using data coordinates (in which case xy could just be given as KeyEvent("key_release_event, ax.figure.canvas, key, *ax.transData.transform((x, y)))) but that feels even more redundant to me. I guess I could inline that at all relevant places if you really prefer it.

explicitly calling _process feels a bit low-level. That was more elegant in do_event.

That point I do feel quite strongly about: directly calling the event handlers, like do_event did, actually abstracted too much, as shown by the changes I had to make to the tests; going via manual event emission is the proper way to simulate what really happens, and does catch additional relevant issues.

Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

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

Let's go with this. It's an internal pattern. We can always change later if we find something better.

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

Successfully merging this pull request may close these issues.

[MNT]: Generalize widget mouse testing
5 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy