Skip to content
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
WebAgg: Add a test for resizing the figure
  • Loading branch information
QuLogic committed Sep 23, 2026
commit 9860201e5a837cda03e67f5655ee694e5dd8e1a9
39 changes: 39 additions & 0 deletions lib/matplotlib/tests/test_backend_webagg.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,45 @@ def test_webagg_general(page, request):
_raise_on_image_difference(expected, actual, tol=0)


@pytest.mark.backend('webagg')
def test_webagg_resize(page, request):
# Only macOS is well-supported by Playwright and other platforms are inconsistent.
# https://github.com/microsoft/playwright/issues/31017
if page.context.browser.browser_type.name == 'webkit' and sys.platform != 'darwin':
request.applymarker(
pytest.mark.xfail(reason="Playwright's WebKit does not support CSS resize"))

# Listen for all console logs.
page.on('console', lambda msg: print(f'CONSOLE: {msg.text}'))
# Increase the viewport to at least twice the size of the default figure.
# Playwright seems to have a bug with mouse movement in Firefox that places the
# cursor somewhere near the origin when outside the viewport size.
page.set_viewport_size({'width': 1600, 'height': 1200})

fig, ax = plt.subplots(facecolor='w')
orig_bbox = fig.bbox.frozen()

# Don't start the Tornado event loop, but use the existing event loop
# started by the `page` fixture.
WebAggApplication.initialize()
WebAggApplication.started = True

page.goto(f'http://{WebAggApplication.address}:{WebAggApplication.port}/')

canvas = page.locator('canvas.mpl-canvas')

# Resize the canvas to be twice as big.
bbox = canvas.bounding_box()
x, y = bbox['x'] + bbox['width'] - 1, bbox['y'] + bbox['height'] - 1
page.mouse.move(x, y)
page.mouse.down()
page.mouse.move(x + bbox['width'], y + bbox['height'])
page.mouse.up()

assert fig.bbox.height == orig_bbox.height * 2
assert fig.bbox.width == orig_bbox.width * 2


@pytest.mark.filterwarnings('ignore:Treat the new Tool classes:UserWarning')
@pytest.mark.backend('webagg')
@pytest.mark.parametrize('toolbar', ['toolbar2', 'toolmanager'])
Expand Down