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

Skip to content

Commit 361f9d6

Browse files
committed
WebAgg: Add test for the toolbar
1 parent 8dd068e commit 361f9d6

File tree

1 file changed

+63
-5
lines changed

1 file changed

+63
-5
lines changed

lib/matplotlib/tests/test_backend_webagg.py

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import shutil
33
import subprocess
44
import sys
5+
import warnings
56

67
import pytest
78

@@ -71,11 +72,6 @@ def test_webagg_general(page):
7172
# Check title.
7273
expect(page.locator('div.ui-dialog-title')).to_have_text('Figure 1')
7374

74-
# Check toolbar buttons.
75-
assert page.locator('button.mpl-widget').count() == len([
76-
name for name, *_ in fig.canvas.manager.ToolbarCls.toolitems
77-
if name is not None])
78-
7975
# Check canvas actually contains something.
8076
baseline_dir, result_dir = _image_directories(test_webagg_general)
8177
browser = page.context.browser.browser_type.name
@@ -89,3 +85,65 @@ def test_webagg_general(page):
8985
err = compare_images(expected, actual, tol=0)
9086
if err:
9187
raise ImageComparisonFailure(err)
88+
89+
90+
@pytest.mark.backend('webagg')
91+
@pytest.mark.parametrize('toolbar', ['toolbar2', 'toolmanager'])
92+
def test_webagg_toolbar(page, toolbar):
93+
from playwright.sync_api import expect
94+
95+
# Listen for all console logs.
96+
page.on('console', lambda msg: print(f'CONSOLE: {msg.text}'))
97+
98+
with warnings.catch_warnings():
99+
warnings.filterwarnings('ignore', message='Treat the new Tool classes',
100+
category=UserWarning)
101+
plt.rcParams['toolbar'] = toolbar
102+
103+
fig, ax = plt.subplots(facecolor='w')
104+
105+
# Don't start the Tornado event loop, but use the existing event loop
106+
# started by the `page` fixture.
107+
WebAggApplication.initialize()
108+
WebAggApplication.started = True
109+
110+
page.goto(f'http://{WebAggApplication.address}:{WebAggApplication.port}/')
111+
112+
expect(page.locator('button.mpl-widget')).to_have_count(
113+
len([
114+
name for name, *_ in fig.canvas.manager.ToolbarCls.toolitems
115+
if name is not None]))
116+
117+
home = page.locator('button.mpl-widget').nth(0)
118+
expect(home).to_be_visible()
119+
120+
back = page.locator('button.mpl-widget').nth(1)
121+
expect(back).to_be_visible()
122+
forward = page.locator('button.mpl-widget').nth(2)
123+
expect(forward).to_be_visible()
124+
if toolbar == 'toolbar2':
125+
# ToolManager doesn't implement history button disabling.
126+
# https://github.com/matplotlib/matplotlib/issues/17979
127+
expect(back).to_be_disabled()
128+
expect(forward).to_be_disabled()
129+
130+
pan = page.locator('button.mpl-widget').nth(3)
131+
expect(pan).to_be_visible()
132+
zoom = page.locator('button.mpl-widget').nth(4)
133+
expect(zoom).to_be_visible()
134+
135+
save = page.locator('button.mpl-widget').nth(5)
136+
expect(save).to_be_visible()
137+
format_dropdown = page.locator('select.mpl-widget')
138+
expect(format_dropdown).to_be_visible()
139+
140+
if toolbar == 'toolmanager':
141+
# Location in status bar is not supported by ToolManager.
142+
return
143+
144+
ax.set_position([0, 0, 1, 1])
145+
bbox = page.locator('canvas.mpl-canvas').bounding_box()
146+
x, y = bbox['x'] + bbox['width'] / 2, bbox['y'] + bbox['height'] / 2
147+
page.mouse.move(x, y, steps=2)
148+
message = page.locator('span.mpl-message')
149+
expect(message).to_have_text('x=0.500 y=0.500')

0 commit comments

Comments
 (0)