Skip to content

A simple test of NapariNavigationToolbar._update_buttons_checked. #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/napari_matplotlib/tests/test_ui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import pytest
from qtpy.QtCore import QSize
from qtpy.QtGui import QImage

from napari_matplotlib import HistogramWidget, ScatterWidget, SliceWidget


def _are_different(a: QImage, b: QImage) -> bool:
"""
Check that a and b are identical, pixel by pixel. Via a stupid nested for loop.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Check that a and b are identical, pixel by pixel. Via a stupid nested for loop.
Check that a and b are not identical, pixel by pixel. Via a stupid nested for loop.

"""
assert not a.isNull()
assert not b.isNull()
assert a.size() == b.size()
for x in range(a.width()):
for y in range(a.height()):
if a.pixel(x, y) != b.pixel(x, y):
return True # exit quickly
return False


@pytest.mark.parametrize(
"Widget", [HistogramWidget, ScatterWidget, SliceWidget]
)
def test_mpl_toolbar_buttons_checked(make_napari_viewer, Widget):
"""Test that the icons for checkable actions change when when a tool is selected.

A simple test of NapariNavigationToolbar._update_buttons_checked. Make sure the
checked and unchecked icons are not the same.
"""
checkable_actions = ["Zoom", "Pan"]

viewer = make_napari_viewer()
widget = Widget(viewer)

# search through all of the icons for the ones whose icons are expected to
# change when checked
for action in widget.toolbar.actions():
if action.text() in checkable_actions:
assert action.isChecked() is False
assert action.isCheckable() is True
unchecked = action.icon().pixmap(QSize(48, 48)).toImage()

# simulate the user click (QTest.mouseClick can't take a QAction)
action.trigger()

assert action.isChecked() is True
checked = action.icon().pixmap(QSize(48, 48)).toImage()
assert _are_different(unchecked, checked)