Skip to content

Commit

Permalink
Merge pull request #1088 from FillBk/plot
Browse files Browse the repository at this point in the history
Added tests for PlotWidget buttons
  • Loading branch information
danielhrisca authored Nov 7, 2024
2 parents 5cb7ce3 + 860fbe7 commit 9f456cb
Show file tree
Hide file tree
Showing 16 changed files with 552 additions and 169 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/coveralls.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.9"
python-version: "3.12"

- name: Install packages
shell: bash
Expand All @@ -39,7 +39,7 @@ jobs:
export DISPLAY=:0
- name: Run coverage
run: tox -e py39
run: tox -e py312

- name: Stop Xvfb
if: matrix.os == 'ubuntu-latest'
Expand Down
2 changes: 1 addition & 1 deletion src/asammdf/gui/widgets/mdi_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -4644,7 +4644,7 @@ def verify_bookmarks(self, bookmarks, plot):
"You have modified bookmarks.\n\n" "Do you want to save the changes in the measurement file?\n" "",
)

if result == MessageBox.No:
if result == MessageBox.StandardButton.No:
return

_password = self.mdf._password
Expand Down
1 change: 1 addition & 0 deletions test/asammdf/gui/dialogs/test_FunctionsManagerDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ def test_PushButton_StoreFunctionChanges_0(self):
"""
# Events:
QtTest.QTest.mouseClick(self.fm.widget.add_btn, QtCore.Qt.MouseButton.LeftButton)
self.mouseClick_WidgetItem(self.fm.widget.functions_list.item(0))

source = inspect.getsource(maximum)
self.fm.widget.function_definition.clear()
Expand Down
Binary file modified test/asammdf/gui/resources/ASAP2_Demo_V171.mf4
Binary file not shown.
68 changes: 44 additions & 24 deletions test/asammdf/gui/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class DragAndDrop

import pyqtgraph
from PySide6 import QtCore, QtGui, QtTest, QtWidgets
from asammdf import mdf

from asammdf.gui.utils import excepthook

Expand Down Expand Up @@ -53,10 +54,7 @@ class TestBase(unittest.TestCase):
resource = os.path.normpath(os.path.join(os.path.dirname(__file__), "resources"))
test_workspace = os.path.join(os.path.dirname(__file__), "test_workspace")
screenshots = os.path.join(os.path.dirname(__file__).split("test")[0], "screenshots")
# os.path.dirname(__file__).split("test")[0], f"screenshots_{sys.platform}_{platform.python_version()}"
# )
# save_ss_here = os.path.normpath(os.path.join(screenshots, sys.platform, platform.python_version()))
# save_ss_here = os.path.normpath(os.path.join(platform_path, platform.python_version().replace(".", "_")))

patchers = []
# MockClass ErrorDialog
mc_ErrorDialog = None
Expand Down Expand Up @@ -102,10 +100,6 @@ def setUp(self) -> None:
shutil.rmtree(self.test_workspace)
if not os.path.exists(self.screenshots):
os.makedirs(self.screenshots)
# if not os.path.exists(self.platform_path):
# os.makedirs(self.platform_path)
# if not os.path.exists(self.save_ss_here):
# os.makedirs(self.save_ss_here)

os.makedirs(self.test_workspace)
self.mc_ErrorDialog.reset_mock()
Expand All @@ -126,8 +120,29 @@ def tearDownClass(cls):

def tearDown(self):
self.processEvents()
path_ = os.path.join(self.screenshots, self.id().split("gui.")[-1].rsplit(".", 1)[0])
if not os.path.exists(path_):
os.makedirs(path_)

w = getattr(self, "widget", None)
if w:
w.grab().save(os.path.join(path_, f"{self.id().split('.')[-1]}.png"))
self.destroy(w)

self.mc_ErrorDialog.reset_mock()

if self.test_workspace and pathlib.Path(self.test_workspace).exists():
shutil.rmtree(self.test_workspace)
try:
shutil.rmtree(self.test_workspace)
except PermissionError as e:
self.destroy(w)
print(e)

@staticmethod
def destroy(w):
w.close()
w.destroy()
w.deleteLater()

def mouseClick_RadioButton(self, qitem):
QtTest.QTest.mouseClick(
Expand Down Expand Up @@ -218,29 +233,17 @@ class Pixmap:
COLOR_CURSOR = "#e69138"

@staticmethod
def is_black(pixmap):
def is_black(pixmap) -> bool:
"""
Excepting cursor
"""
cursor_x = None
cursor_y = None
cursor_color = None
image = pixmap.toImage()

for y in range(image.height()):
for x in range(image.width()):
color = QtGui.QColor(image.pixel(x, y))
if color.name() != Pixmap.COLOR_BACKGROUND:
if not cursor_x and not cursor_y and not cursor_color:
cursor_x = x
cursor_y = y + 1
cursor_color = color
continue
elif cursor_x == x and cursor_y == y and cursor_color == color:
cursor_y += 1
continue
else:
return False
if color.name() not in (Pixmap.COLOR_BACKGROUND, Pixmap.COLOR_CURSOR):
return False
return True

@staticmethod
Expand Down Expand Up @@ -454,3 +457,20 @@ def search_y_of_signal_in_column(pixmap_column, signal_color):
line = y
break
return line


class OpenMDF:
def __init__(self, file_path):
self.mdf = None
self._file_path = file_path
self._process_bus_logging = ("process_bus_logging", True)

def __enter__(self):
self.mdf = mdf.MDF(self._file_path, process_bus_logging=self._process_bus_logging)
return self.mdf

def __exit__(self, exc_type, exc_val, exc_tb):
for exc in (exc_type, exc_val, exc_tb):
if exc is not None:
raise exc
self.mdf.close()
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ def setUp(self):
# Get shortcuts
self.assertIsNotNone(self.load_shortcuts_from_json_file(self.widget))

def tearDown(self):
super().tearDown()
if self.widget:
self.widget.destroy()
self.widget.deleteLater()

def test_search_and_select_channels_shortcut(self):
"""
Test Scope:
Expand Down
12 changes: 0 additions & 12 deletions test/asammdf/gui/widgets/Shortcuts/test_NumericWidget_Shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ def setUp(self):
# get shortcuts
self.assertIsNotNone(self.load_shortcuts_from_json_file(self.table_view))

def tearDown(self):
super().tearDown()
if self.widget:
self.widget.destroy()
self.widget.deleteLater()

def test_delete_shortcut(self):
"""
Test Scope:
Expand Down Expand Up @@ -251,12 +245,6 @@ def setUp(self):
# get shortcuts
self.assertIsNotNone(self.load_shortcuts_from_json_file(self.numeric))

def tearDown(self):
super().tearDown()
if self.widget:
self.widget.destroy()
self.widget.deleteLater()

def test_ascii__bin__hex__physical_shortcuts(self):
"""
Test Scope:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,6 @@ def setUp(self):
self.assertIsNotNone(self.load_shortcuts_from_json_file(self.pg))
self.processEvents()

def tearDown(self):
"""
Destroy widget if it still exists
"""
super().tearDown()
if self.widget:
self.widget.destroy()
self.widget.deleteLater()

def test_lock_unlock_range_shortcut(self):
"""
Test Scope:
Expand Down Expand Up @@ -110,7 +101,7 @@ def test_lock_unlock_range_shortcut(self):

# Press Key 'Y' for range selection
QTest.keySequence(self.pg, QKeySequence(self.shortcuts["toggle_range"]))
self.processEvents(timeout=0.01)
self.processEvents(timeout=0.1)

# Save PixMap of Range plot
range_pixmap = self.pg.grab()
Expand Down Expand Up @@ -355,6 +346,21 @@ def test_fit__stack_shortcuts(self):
Additional Evaluation
- Evaluate that all signals are continuous on plot
"""

def continuous(ch):
extremes = Pixmap.search_signal_extremes_by_ax(self.pg.grab(), signal_color=ch.color.name(), ax="x")
for x in range(self.pg.height() - 1):
column = self.pg.grab(QRect(x, 0, 1, self.pg.height()))
if x < extremes[0] - 1:
self.assertTrue(Pixmap.is_black(column), f"column {x} for channel {ch.name} is not black")
elif extremes[0] <= x <= extremes[1]:
self.assertTrue(
Pixmap.has_color(column, ch.color.name()),
f"column {x} doesn't have color of channel {ch.name}",
)
elif x > extremes[1] + 1:
self.assertTrue(Pixmap.is_black(column), f"column {x} for channel {ch.name} is not black")

self.pg.cursor1.color = "#000000"

self.add_channels([35, 36, 37])
Expand All @@ -368,7 +374,7 @@ def test_fit__stack_shortcuts(self):
# Evaluate
with self.subTest("test_stack_all_shortcut"):
# First 2 lines
self.assertTrue(Pixmap.is_black(self.pg.grab(QRect(0, 0, self.pg.width(), 2))))
self.assertTrue(Pixmap.is_black(self.pg.grab(QRect(0, 0, self.pg.width(), 1))))
# Top
pixmap = self.pg.grab(QRect(0, 0, self.pg.width(), int(self.pg.height() / 3)))
self.assertTrue(Pixmap.has_color(pixmap, channel_35.color.name()))
Expand Down Expand Up @@ -532,46 +538,15 @@ def test_fit__stack_shortcuts(self):

# search if all channels are fitted into extremes
self.mouseDClick_WidgetItem(channel_35)
extremes = Pixmap.search_signal_extremes_by_ax(self.pg.grab(), signal_color=channel_35.color.name(), ax="x")
for x in range(self.pg.height() - 1):
column = self.pg.grab(QRect(x, 0, 1, self.pg.height()))
if x < extremes[0] - 1:
self.assertTrue(Pixmap.is_black(column), f"column {x} is not black")
elif extremes[0] <= x <= extremes[1]:
self.assertTrue(
Pixmap.has_color(column, channel_35.color.name()),
f"column {x} doesn't have color of channel 35",
)
else:
self.assertTrue(Pixmap.is_black(column), f"column {x} is not black")
continuous(channel_35)

self.mouseDClick_WidgetItem(channel_35)
self.mouseDClick_WidgetItem(channel_36)
for x in range(self.pg.height() - 1):
column = self.pg.grab(QRect(x, 0, 1, self.pg.height()))
if x < extremes[0] - 1:
self.assertTrue(Pixmap.is_black(column), f"column {x} is not black")
elif extremes[0] <= x <= extremes[1]:
self.assertTrue(
Pixmap.has_color(column, channel_36.color.name()),
f"column {x} doesn't have color of channel 36",
)
else:
self.assertTrue(Pixmap.is_black(column), f"column {x} is not black")
continuous(channel_36)

self.mouseDClick_WidgetItem(channel_37)
self.mouseDClick_WidgetItem(channel_36)
for x in range(self.pg.height() - 1):
column = self.pg.grab(QRect(x, 0, 1, self.pg.height()))
if x < extremes[0] - 1:
self.assertTrue(Pixmap.is_black(column), f"column {x} is not black")
elif extremes[0] + 1 <= x <= extremes[1] - 1:
self.assertTrue(
Pixmap.has_color(column, channel_37.color.name()),
f"column {x} doesn't have color of channel 37",
)
else:
self.assertTrue(Pixmap.is_black(column), f"column {x} is not black")
self.mouseDClick_WidgetItem(channel_37)
continuous(channel_37)

def test_grid_shortcut(self):
"""
Expand Down Expand Up @@ -729,6 +704,9 @@ def get_expected_result(step, is_x_axis: bool):
return top - delta * step, bottom + delta * step

# Setup
if self.plot.lock_btn.isFlat():
QTest.mouseClick(self.plot.lock_btn, Qt.MouseButton.LeftButton)

y_step = 0.165
x_step = 0.25

Expand Down Expand Up @@ -966,6 +944,8 @@ def test_move_cursor_left__right_shortcuts(self):
- Evaluate values from selected channel value and cursor info, it must be equal to the expected values.
"""
# Setup
if self.plot.selected_channel_value_btn.isFlat():
QTest.mouseClick(self.plot.selected_channel_value_btn, Qt.MouseButton.LeftButton)
self.add_channels([37])
ch = self.channels[0]
# Number of times that specific key will be pressed
Expand Down Expand Up @@ -1063,6 +1043,8 @@ def test_shift_channels_shortcut(self):
- Evaluate that first signal is shifted down & left after pressing combination "Shift+Down" & "Shift+Left"
- Evaluate that second signal is shifted up & right after pressing combination "Shift+Up" & "Shift+Right"
"""
if self.plot.lock_btn.isFlat():
QTest.mouseClick(self.plot.lock_btn, Qt.MouseButton.LeftButton)
self.add_channels([36, 37])
channel_36 = self.channels[0]
channel_37 = self.channels[1]
Expand Down
37 changes: 32 additions & 5 deletions test/asammdf/gui/widgets/Shortcuts/test_PlotWidget_Shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from PySide6.QtTest import QTest
from PySide6.QtWidgets import QMessageBox

from asammdf.gui.utils import COLORS
from test.asammdf.gui.test_base import Pixmap
from test.asammdf.gui.widgets.test_BasePlotWidget import TestPlotWidget

Expand Down Expand Up @@ -61,11 +62,8 @@ def setUp(self):
def tearDown(self):
# Ensure that at the end, button "No" is pressed for MessageBox question window
with mock.patch("asammdf.gui.widgets.mdi_area.MessageBox.question") as mo_question:
mo_question.return_value = QMessageBox.No
mo_question.return_value = QMessageBox.StandardButton.No
super().tearDown()
if self.widget:
self.widget.destroy()
self.widget.deleteLater()

def test_statistics_shortcut(self):
"""
Expand Down Expand Up @@ -315,15 +313,44 @@ def test_insert_bookmark_shortcut(self):
- Evaluate that bookmarks are displayed after pressing "Ctrl+I"
and the message of the last bookmark is the first element of the returned list of mock object
"""
QTest.mouseClick(
self.plot.plot.viewport(), Qt.MouseButton.LeftButton, pos=self.plot.plot.viewport().geometry().center()
)
self.processEvents()
timestamp = self.plot.plot.cursor1.value()
# mock for bookmark
with mock.patch("asammdf.gui.widgets.plot.QtWidgets.QInputDialog.getMultiLineText") as mo_getMultiLineText:
mo_getMultiLineText.return_value = [self.id(), True]
# Press "Ctrl+I"
QTest.keySequence(self.plot, QKeySequence(self.shortcuts["insert_bookmark"]))
# Evaluate

mo_getMultiLineText.assert_called()

# Destroy current widget
with mock.patch("asammdf.gui.widgets.mdi_area.MessageBox.question") as mo_question:
mo_question.return_value = QMessageBox.StandardButton.Yes
self.destroy(self.widget)

# Open file with new bookmark in new File Widget
self.setUpFileWidget(measurement_file=self.measurement_file, default=True)
self.processEvents(0.1)
self.widget.channel_view.setCurrentText("Natural sort")
# Select channels -> Press PushButton "Create Window" -> "Plot"
self.create_window(window_type="Plot")
self.assertEqual(len(self.widget.mdi_area.subWindowList()), 1)
self.plot = self.widget.mdi_area.subWindowList()[0].widget()
self.processEvents(0.1)

pg_colors = Pixmap.color_names_exclude_defaults(self.plot.plot.viewport().grab())
bookmarks_colors = COLORS[: len(COLORS) - len(self.plot.plot.bookmarks) - 1 : -1]

# Evaluate
self.assertTrue(self.plot.show_bookmarks)
self.assertEqual(self.plot.plot.bookmarks[len(self.plot.plot.bookmarks) - 1].message, self.id())
self.assertEqual(self.plot.plot.bookmarks[len(self.plot.plot.bookmarks) - 1].value(), timestamp)
for bookmark, color in zip(self.plot.plot.bookmarks, bookmarks_colors):
self.assertEqual(bookmark.color, color)
self.assertIn(color, pg_colors)

def test_toggle_bookmarks_shortcut(self):
"""
Expand Down
Loading

0 comments on commit 9f456cb

Please sign in to comment.