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