diff --git a/lib/matplotlib/testing/widgets.py b/lib/matplotlib/testing/widgets.py new file mode 100644 index 000000000000..006141741365 --- /dev/null +++ b/lib/matplotlib/testing/widgets.py @@ -0,0 +1,66 @@ +""" +======================== +Widget testing utilities +======================== + +Functions that are useful for testing widgets. +See also matplotlib.tests.test_widgets +""" + +import matplotlib.pyplot as plt + +try: + # mock in python 3.3+ + from unittest import mock +except ImportError: + import mock + + +def get_ax(): + fig, ax = plt.subplots(1, 1) + ax.plot([0, 200], [0, 200]) + ax.set_aspect(1.0) + ax.figure.canvas.draw() + return ax + + +def do_event(tool, etype, button=1, xdata=0, ydata=0, key=None, step=1): + """ + *tool* + a matplotlib.widgets.RectangleSelector instance + + *etype* + the event to trigger + + *xdata* + x coord of mouse in data coords + + *ydata* + y coord of mouse in data coords + + *button* + button pressed None, 1, 2, 3, 'up', 'down' (up and down are used + for scroll events) + + *key* + the key depressed when the mouse event triggered (see + :class:`KeyEvent`) + + *step* + number of scroll steps (positive for 'up', negative for 'down') + """ + event = mock.Mock() + event.button = button + ax = tool.ax + event.x, event.y = ax.transData.transform([(xdata, ydata), + (xdata, ydata)])[00] + event.xdata, event.ydata = xdata, ydata + event.inaxes = ax + event.canvas = ax.figure.canvas + event.key = key + event.step = step + event.guiEvent = None + event.name = 'Custom' + + func = getattr(tool, etype) + func(event) diff --git a/lib/matplotlib/tests/test_widgets.py b/lib/matplotlib/tests/test_widgets.py index 345140dbdab1..cdd2cf0df715 100644 --- a/lib/matplotlib/tests/test_widgets.py +++ b/lib/matplotlib/tests/test_widgets.py @@ -1,81 +1,15 @@ from __future__ import (absolute_import, division, print_function, unicode_literals) -try: - # mock in python 3.3+ - from unittest import mock -except ImportError: - import mock - import matplotlib.widgets as widgets import matplotlib.pyplot as plt -from matplotlib.testing.decorators import image_comparison - -from numpy.testing import assert_allclose import pytest +from matplotlib.testing.decorators import image_comparison +from matplotlib.testing.widgets import do_event, get_ax -def get_ax(): - fig, ax = plt.subplots(1, 1) - ax.plot([0, 200], [0, 200]) - ax.set_aspect(1.0) - ax.figure.canvas.draw() - return ax - - -def do_event(tool, etype, button=1, xdata=0, ydata=0, key=None, step=1): - """ - *name* - the event name - - *canvas* - the FigureCanvas instance generating the event - - *guiEvent* - the GUI event that triggered the matplotlib event - - *x* - x position - pixels from left of canvas - - *y* - y position - pixels from bottom of canvas - - *inaxes* - the :class:`~matplotlib.axes.Axes` instance if mouse is over axes - - *xdata* - x coord of mouse in data coords - - *ydata* - y coord of mouse in data coords - - *button* - button pressed None, 1, 2, 3, 'up', 'down' (up and down are used - for scroll events) - - *key* - the key depressed when the mouse event triggered (see - :class:`KeyEvent`) - - *step* - number of scroll steps (positive for 'up', negative for 'down') - """ - event = mock.Mock() - event.button = button - ax = tool.ax - event.x, event.y = ax.transData.transform([(xdata, ydata), - (xdata, ydata)])[00] - event.xdata, event.ydata = xdata, ydata - event.inaxes = ax - event.canvas = ax.figure.canvas - event.key = key - event.step = step - event.guiEvent = None - event.name = 'Custom' - - func = getattr(tool, etype) - func(event) +from numpy.testing import assert_allclose def check_rectangle(**kwargs):