8000 WebAgg: Add tests for pan/zoom tool · matplotlib/matplotlib@6987988 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6987988

Browse files
committed
WebAgg: Add tests for pan/zoom tool
1 parent ff022df commit 6987988

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
@@ -184,3 +185,115 @@ def test_webagg_toolbar_save(random_port, page):
184185

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

0 commit comments

Comments
 (0)
0