Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 99b815e

Browse files
committed
WebAgg: Add tests for pan/zoom tool
1 parent 9bd2331 commit 99b815e

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

lib/matplotlib/tests/test_backend_webagg.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import re
23
import shutil
34
import subprocess
45
import sys
@@ -203,3 +204,118 @@ def test_webagg_toolbar_save(random_port, page):
203204

204205
new_page.wait_for_load_state()
205206
assert new_page.url.endswith('download.png')
207+
208+
209+
@pytest.mark.backend('webagg')
210+
def test_webagg_toolbar_pan(random_port, page):
211+
from playwright.sync_api import expect
212+
213+
# Listen for all console logs.
214+
page.on('console', lambda msg: print(f'CONSOLE: {msg.text}'))
215+
216+
fig, ax = plt.subplots(facecolor='w')
217+
ax.plot([3, 2, 1])
218+
orig_lim = ax.viewLim.frozen()
219+
# Make figure coords ~= axes coords, with ticks visible for inspection.
220+
ax.set_position([0, 0, 1, 1])
221+
ax.tick_params(axis='y', direction='in', pad=-22)
222+
ax.tick_params(axis='x', direction='in', pad=-15)
223+
224+
# Don't start the Tornado event loop, but use the existing event loop
225+
# started by the `page` fixture.
226+
WebAggApplication.initialize()
227+
WebAggApplication.started = True
228+
229+
page.goto(f'http://{WebAggApplication.address}:{WebAggApplication.port}/')
230+
231+
canvas = page.locator('canvas.mpl-canvas')
232+
expect(canvas).to_be_visible()
233+
home = page.locator('button.mpl-widget').nth(0)
234+
expect(home).to_be_visible()
235+
pan = page.locator('button.mpl-widget').nth(3)
236+
expect(pan).to_be_visible()
237+
zoom = page.locator('button.mpl-widget').nth(4)
238+
expect(zoom).to_be_visible()
239+
240+
active_re = re.compile(r'active')
241+
expect(pan).not_to_have_class(active_re)
242+
expect(zoom).not_to_have_class(active_re)
243+
assert ax.get_navigate_mode() is None
244+
pan.click()
245+
expect(pan).to_have_class(active_re)
246+
expect(zoom).not_to_have_class(active_re)
247+
assert ax.get_navigate_mode() == 'PAN'
248+
249+
# Pan 50% of the figure diagonally toward bottom-right.
250+
bbox = canvas.bounding_box()
251+
x, y = bbox['x'] + bbox['width'] / 4, bbox['y'] + bbox['height'] / 4
252+
page.mouse.move(x, y)
253+
page.mouse.down()
254+
page.mouse.move(x + bbox['width'] / 2, y + bbox['height'] / 2,
255+
steps=20)
256+
page.mouse.up()
257+
258+
assert ax.get_xlim() == (orig_lim.x0 - orig_lim.width / 2,
259+
orig_lim.x1 - orig_lim.width / 2)
260+
assert ax.get_ylim() == (orig_lim.y0 + orig_lim.height / 2,
261+
orig_lim.y1 + orig_lim.height / 2)
262+
263+
# Reset.
264+
home.click()
265+
assert ax.viewLim.bounds == orig_lim.bounds
266+
267+
# Pan 50% of the figure diagonally toward bottom-right, while holding 'x'
268+
# key, to constrain the pan horizontally.
269+
bbox = canvas.bounding_box()
270+
x, y = bbox['x'] + bbox['width'] / 4, bbox['y'] + bbox['height'] / 4
271+
page.mouse.move(x, y)
272+
page.mouse.down()
273+
page.keyboard.down('x')
274+
page.mouse.move(x + bbox['width'] / 2, y + bbox['height'] / 2,
275+
steps=20)
276+
page.mouse.up()
277+
page.keyboard.up('x')
278+
279+
assert ax.get_xlim() == (orig_lim.x0 - orig_lim.width / 2,
280+
orig_lim.x1 - orig_lim.width / 2)
281+
assert ax.get_ylim() == (orig_lim.y0, orig_lim.y1)
282+
283+
# Reset.
284+
home.click()
285+
assert ax.viewLim.bounds == orig_lim.bounds
286+
287+
# Pan 50% of the figure diagonally toward bottom-right, while holding 'y'
288+
# key, to constrain the pan vertically.
289+
bbox = canvas.bounding_box()
290+
x, y = bbox['x'] + bbox['width'] / 4, bbox['y'] + bbox['height'] / 4
291+
page.mouse.move(x, y)
292+
page.mouse.down()
293+
page.keyboard.down('y')
294+
page.mouse.move(x + bbox['width'] / 2, y + bbox['height'] / 2,
295+
steps=20)
296+
page.mouse.up()
297+
page.keyboard.up('y')
298+
299+
assert ax.get_xlim() == (orig_lim.x0, orig_lim.x1)
300+
assert ax.get_ylim() == (orig_lim.y0 + orig_lim.height / 2,
301+
orig_lim.y1 + orig_lim.height / 2)
302+
303+
# Reset.
304+
home.click()
305+
assert ax.viewLim.bounds == orig_lim.bounds
306+
307+
# Zoom 50% of the figure diagonally toward bottom-right.
308+
bbox = canvas.bounding_box()
309+
x, y = bbox['x'], bbox['y']
310+
page.mouse.move(x, y)
311+
page.mouse.down(button='right')
312+
page.mouse.move(x + bbox['width'] / 2, y + bbox['height'] / 2,
313+
steps=20)
314+
page.mouse.up(button='right')
315+
316+
# Expands in x-direction.
317+
assert ax.viewLim.x0 == orig_lim.x0
318+
assert ax.viewLim.x1 < orig_lim.x1 - orig_lim.width / 2
319+
# Contracts in y-direction.
320+
assert ax.viewLim.y1 == orig_lim.y1
321+
assert ax.viewLim.y0 < orig_lim.y0 - orig_lim.height / 2

0 commit comments

Comments
 (0)