diff --git a/lib/matplotlib/tests/baseline_images/test_offsetbox/paddedbox.png b/lib/matplotlib/tests/baseline_images/test_offsetbox/paddedbox.png new file mode 100644 index 000000000000..dfb68e2c35a1 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_offsetbox/paddedbox.png differ diff --git a/lib/matplotlib/tests/test_offsetbox.py b/lib/matplotlib/tests/test_offsetbox.py index a0116d5dfcd9..cd57122272df 100644 --- a/lib/matplotlib/tests/test_offsetbox.py +++ b/lib/matplotlib/tests/test_offsetbox.py @@ -5,15 +5,15 @@ from numpy.testing import assert_allclose import pytest -from matplotlib.testing.decorators import image_comparison +from matplotlib.testing.decorators import check_figures_equal, image_comparison import matplotlib.pyplot as plt import matplotlib.patches as mpatches import matplotlib.lines as mlines from matplotlib.backend_bases import MouseButton, MouseEvent from matplotlib.offsetbox import ( - AnchoredOffsetbox, AnnotationBbox, AnchoredText, DrawingArea, OffsetBox, - OffsetImage, PaddedBox, TextArea, _get_packed_offsets, HPacker, VPacker) + AnchoredOffsetbox, AnnotationBbox, AnchoredText, DrawingArea, HPacker, + OffsetBox, OffsetImage, PaddedBox, TextArea, VPacker, _get_packed_offsets) @image_comparison(['offsetbox_clipping'], remove_text=True) @@ -28,6 +28,7 @@ def test_offsetbox_clipping(): fig, ax = plt.subplots() size = 100 da = DrawingArea(size, size, clip=True) + assert da.clip_children bg = mpatches.Rectangle((0, 0), size, size, facecolor='#CCCCCC', edgecolor='None', @@ -386,10 +387,66 @@ def test_packers(align): [(px + x_height, py), (px, py - y2)]) -def test_paddedbox(): +def test_paddedbox_default_values(): # smoke test paddedbox for correct default value fig, ax = plt.subplots() at = AnchoredText("foo", 'upper left') pb = PaddedBox(at, patch_attrs={'facecolor': 'r'}, draw_frame=True) ax.add_artist(pb) fig.draw_without_rendering() + + +def test_annotationbbox_properties(): + ab = AnnotationBbox(DrawingArea(20, 20, 0, 0, clip=True), (0.5, 0.5), + xycoords='data') + assert ab.xyann == (0.5, 0.5) # xy if xybox not given + assert ab.anncoords == 'data' # xycoords if boxcoords not given + + ab = AnnotationBbox(DrawingArea(20, 20, 0, 0, clip=True), (0.5, 0.5), + xybox=(-0.2, 0.4), xycoords='data', + boxcoords='axes fraction') + assert ab.xyann == (-0.2, 0.4) # xybox if given + assert ab.anncoords == 'axes fraction' # boxcoords if given + + +def test_textarea_properties(): + ta = TextArea('Foo') + assert ta.get_text() == 'Foo' + assert not ta.get_multilinebaseline() + + ta.set_text('Bar') + ta.set_multilinebaseline(True) + assert ta.get_text() == 'Bar' + assert ta.get_multilinebaseline() + + +@check_figures_equal() +def test_textarea_set_text(fig_test, fig_ref): + ax_ref = fig_ref.add_subplot() + text0 = AnchoredText("Foo", "upper left") + ax_ref.add_artist(text0) + + ax_test = fig_test.add_subplot() + text1 = AnchoredText("Bar", "upper left") + ax_test.add_artist(text1) + text1.txt.set_text("Foo") + + +@image_comparison(['paddedbox.png'], remove_text=True, style='mpl20') +def test_paddedbox(): + fig, ax = plt.subplots() + + ta = TextArea("foo") + pb = PaddedBox(ta, pad=5, patch_attrs={'facecolor': 'r'}, draw_frame=True) + ab = AnchoredOffsetbox('upper left', child=pb) + ax.add_artist(ab) + + ta = TextArea("bar") + pb = PaddedBox(ta, pad=10, patch_attrs={'facecolor': 'b'}) + ab = AnchoredOffsetbox('upper right', child=pb) + ax.add_artist(ab) + + ta = TextArea("foobar") + pb = PaddedBox(ta, pad=15, draw_frame=True) + ab = AnchoredOffsetbox('lower right', child=pb) + ax.add_artist(ab)