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

Skip to content

Commit 5f8e34b

Browse files
committed
WebAgg: Add tests for pan/zoom tool
1 parent 35175e6 commit 5f8e34b

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

lib/matplotlib/tests/test_backend_webagg.py

Lines changed: 113 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
@@ -191,3 +192,115 @@ def test_webagg_toolbar_save(random_port, page):
191192

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

0 commit comments

Comments
 (0)