Repository: matplotlib/matplotlib
File: lib/matplotlib/tests/test_widgets.py (Python)

Code before:
                         check_props=check_props)

    cb = widgets.CheckButtons(fig_test.subplots(), ['tea', 'coffee'],
                              [True, True])
    cb.set_label_props(label_props)
    # Setting the label size automatically increases default marker size, so we
    # need to do that here as well.
    cb.set_frame_props({**frame_props, 's': (24 / 2)**2})
    # FIXME: Axes.scatter promotes facecolor to edgecolor on unfilled markers,
    # but Collection.update doesn't do that (it forgot the marker already).
    # This means we cannot pass facecolor to both setters directly.
    check_props['edgecolor'] = check_props.pop('facecolor')
    cb.set_check_props({**check_props, 's': (24 / 2)**2})


@pytest.mark.parametrize("widget", [widgets.RadioButtons, widgets.CheckButtons])
def test__buttons_callbacks(widget):
    """Tests what https://github.com/matplotlib/matplotlib/pull/31031 fixed"""
    ax = get_ax()
    button = widget(ax, ["Test Button"])
    ax.figure.canvas.callbacks.process(
        "button_press_event",
        MouseEvent(
            name="test", canvas=ax.figure.canvas, x=200, y=200, button=1, key=None
        ),
    )


def test_slider_slidermin_slidermax_invalid():
    fig, ax = plt.subplots()
    # test min/max with floats
    with pytest.raises(ValueError):
        widgets.Slider(ax=ax, label='', valmin=0.0, valmax=24.0,
                       slidermin=10.0)
    with pytest.raises(ValueError):
        widgets.Slider(ax=ax, label='', valmin=0.0, valmax=24.0,
                       slidermax=10.0)


def test_slider_slidermin_slidermax():
    fig, ax = plt.subplots()
    slider_ = widgets.Slider(ax=ax, label='', valmin=0.0, valmax=24.0,
                             valinit=5.0)

    slider = widgets.Slider(ax=ax, label='', valmin=0.0, valmax=24.0,
                            valinit=1.0, slidermin=slider_)
    assert slider.val == slider_.val

    slider = widgets.Slider(ax=ax, label='', valmin=0.0, valmax=24.0,
                            valinit=10.0, slidermax=slider_)
    assert slider.val == slider_.val

Review comment (suggestion):
This should use the same form as e.g., `test_rectangle_selector` for events.

Code after:
                         check_props=check_props)

    cb = widgets.CheckButtons(fig_test.subplots(), ['tea', 'coffee'],
                              [True, True])
    cb.set_label_props(label_props)
    # Setting the label size automatically increases default marker size, so we
    # need to do that here as well.
    cb.set_frame_props({**frame_props, 's': (24 / 2)**2})
    # FIXME: Axes.scatter promotes facecolor to edgecolor on unfilled markers,
    # but Collection.update doesn't do that (it forgot the marker already).
    # This means we cannot pass facecolor to both setters directly.
    check_props['edgecolor'] = check_props.pop('facecolor')
    cb.set_check_props({**check_props, 's': (24 / 2)**2})


@pytest.mark.parametrize("widget", [widgets.RadioButtons, widgets.CheckButtons])
def test__buttons_callbacks(ax, widget):
    """Tests what https://github.com/matplotlib/matplotlib/pull/31031 fixed"""
    on_clicked = mock.Mock(spec=noop, return_value=None)
    button = widget(ax, ["Test Button"])
    button.on_clicked(on_clicked)
    MouseEvent._from_ax_coords(
        "button_press_event",
        ax,
        ax.transData.inverted().transform(ax.transAxes.transform(
            # (x, y) of the 0th button defined at
            # `{Check,Radio}Buttons._init_props`
            (0.15, 0.5),
        )),
        1,
    )._process()
    on_clicked.assert_called_once()


def test_slider_slidermin_slidermax_invalid():
    fig, ax = plt.subplots()
    # test min/max with floats
    with pytest.raises(ValueError):
        widgets.Slider(ax=ax, label='', valmin=0.0, valmax=24.0,
                       slidermin=10.0)
    with pytest.raises(ValueError):
        widgets.Slider(ax=ax, label='', valmin=0.0, valmax=24.0,
                       slidermax=10.0)


def test_slider_slidermin_slidermax():
    fig, ax = plt.subplots()
    slider_ = widgets.Slider(ax=ax, label='', valmin=0.0, valmax=24.0,
                             valinit=5.0)

    slider = widgets.Slider(ax=ax, label='', valmin=0.0, valmax=24.0,