Skip to content

Commit

Permalink
Add tests for gui. (#856)
Browse files Browse the repository at this point in the history
* Add tests. Rename inherited class variable: finished: QtCore.Signal
* Add tests and fix for ProgressDialog
* Fixes and Linux particularities.
  • Loading branch information
tov101 committed May 8, 2023
1 parent 25d6509 commit 864eda9
Show file tree
Hide file tree
Showing 11 changed files with 1,753 additions and 90 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/coveralls.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

jobs:
coveralls:
runs-on: ubuntu-latest
runs-on: windows-latest

steps:
- uses: actions/checkout@v3
Expand All @@ -33,6 +33,7 @@ jobs:
fi
- name: Start Xvfb
if: matrix.os == 'ubuntu-latest'
run: |
Xvfb :0 -screen 0 1024x768x24 > /dev/null 2>&1 &
export DISPLAY=:0
Expand All @@ -41,6 +42,7 @@ jobs:
run: tox -e py38

- name: Stop Xvfb
if: matrix.os == 'ubuntu-latest'
run: killall Xvfb

# see: https://coveralls-python.readthedocs.io/en/latest/usage/configuration.html#github-actions-support
Expand Down
2 changes: 1 addition & 1 deletion requirements_exe_build.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ keyring
pyinstaller; sys_platform=="win32"
pyinstaller; sys_platform=="darwin"
pyinstaller==4.10; sys_platform=="linux"
scipy
scipy
6 changes: 4 additions & 2 deletions src/asammdf/gui/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ def run(self):


class ProgressDialog(QtWidgets.QProgressDialog):
finished = QtCore.Signal()
qfinished = QtCore.Signal()

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand All @@ -267,6 +267,8 @@ def __init__(self, *args, **kwargs):
self.error = None
self.result = None
self.thread_finished = True
# Connect signal to "processEvents": Give the chance to "destroy" function to make his job
self.qfinished.connect(lambda: QtCore.QCoreApplication.processEvents())

def run_thread_with_progress(self, target, args, kwargs):
self.show()
Expand Down Expand Up @@ -300,9 +302,9 @@ def receive_error(self, error):
self.error = error

def thread_complete(self):
self.finished.emit()
self.thread_finished = True
super().close()
self.qfinished.emit()

def cancel(self):
super().cancel()
Expand Down
12 changes: 6 additions & 6 deletions src/asammdf/gui/widgets/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def scramble(self, event):
return

self._progress = setup_progress(parent=self, autoclose=False)
self._progress.finished.connect(self.scramble_finished)
self._progress.qfinished.connect(self.scramble_finished)

self._progress.run_thread_with_progress(
target=self.scramble_thread,
Expand Down Expand Up @@ -287,7 +287,7 @@ def extract_bus_logging(self, event):
source_files = [Path(self.files_list.item(row).text()) for row in range(count)]

self._progress = setup_progress(parent=self)
self._progress.finished.connect(self.extract_bus_logging_finished)
self._progress.qfinished.connect(self.extract_bus_logging_finished)

self._progress.run_thread_with_progress(
target=self.extract_bus_logging_thread,
Expand Down Expand Up @@ -469,7 +469,7 @@ def extract_bus_csv_logging(self, event):
source_files = [Path(self.files_list.item(row).text()) for row in range(count)]

self._progress = setup_progress(parent=self)
self._progress.finished.connect(self.extract_bus_csv_logging_finished)
self._progress.qfinished.connect(self.extract_bus_csv_logging_finished)

self._progress.run_thread_with_progress(
target=self.extract_bus_csv_logging_thread,
Expand Down Expand Up @@ -722,7 +722,7 @@ def concatenate(self, event):
source_files = [Path(self.files_list.item(row).text()) for row in range(count)]

self._progress = setup_progress(parent=self, autoclose=False)
self._progress.finished.connect(self.concatenate_finished)
self._progress.qfinished.connect(self.concatenate_finished)

self._progress.run_thread_with_progress(
target=self.concatenate_thread,
Expand Down Expand Up @@ -889,7 +889,7 @@ def stack(self, event):
source_files = [Path(self.files_list.item(row).text()) for row in range(count)]

self._progress = setup_progress(parent=self, autoclose=False)
self._progress.finished.connect(self.stack_finished)
self._progress.qfinished.connect(self.stack_finished)

self._progress.run_thread_with_progress(
target=self.stack_thread,
Expand Down Expand Up @@ -1391,7 +1391,7 @@ def apply_processing(self, event):
return

self._progress = setup_progress(parent=self, autoclose=False)
self._progress.finished.connect(self.apply_processing_finished)
self._progress.qfinished.connect(self.apply_processing_finished)

self._progress.run_thread_with_progress(
target=self.apply_processing_thread,
Expand Down
8 changes: 4 additions & 4 deletions src/asammdf/gui/widgets/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -1605,7 +1605,7 @@ def scramble_finished(self):

def scramble(self, event):
self._progress = setup_progress(parent=self)
self._progress.finished.connect(self.scramble_finished)
self._progress.qfinished.connect(self.scramble_finished)

self._progress.run_thread_with_progress(
target=self.scramble_thread,
Expand Down Expand Up @@ -1673,7 +1673,7 @@ def extract_bus_logging(self, event):
return

self._progress = setup_progress(parent=self)
self._progress.finished.connect(self.extract_bus_logging_finished)
self._progress.qfinished.connect(self.extract_bus_logging_finished)

self._progress.run_thread_with_progress(
target=self.extract_bus_logging_thread,
Expand Down Expand Up @@ -1840,7 +1840,7 @@ def extract_bus_csv_logging(self, event):
return

self._progress = setup_progress(parent=self)
self._progress.finished.connect(self.extract_bus_csv_logging_finished)
self._progress.qfinished.connect(self.extract_bus_csv_logging_finished)

self._progress.run_thread_with_progress(
target=self.extract_bus_csv_logging_thread,
Expand Down Expand Up @@ -2664,7 +2664,7 @@ def apply_processing(self, event):
return

self._progress = setup_progress(parent=self)
self._progress.finished.connect(self.apply_processing_finished)
self._progress.qfinished.connect(self.apply_processing_finished)

self._progress.run_thread_with_progress(
target=self.apply_processing_thread,
Expand Down
2 changes: 1 addition & 1 deletion src/asammdf/gui/widgets/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5675,7 +5675,7 @@ def range_modified_handler(self, region):
def scale_curve_to_pixmap(self, x, y, y_range, x_start, delta):
if self.py:
y_low, y_high = y_range
y_scale = (y_high - y_low) / self.py
y_scale = (np.float64(y_high) - np.float64(y_low)) / np.float64(self.py)
x_scale = self.px

if y_scale * x_scale:
Expand Down
2 changes: 1 addition & 1 deletion src/asammdf/gui/widgets/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ def startDrag(self, supportedActions):

drag = QtGui.QDrag(self)
drag.setMimeData(mimeData)
drag.exec_(QtCore.Qt.MoveAction)
drag.exec(QtCore.Qt.MoveAction)

def dragEnterEvent(self, e):
self.autoscroll_timer.start()
Expand Down
47 changes: 42 additions & 5 deletions test/asammdf/gui/resources/valid.dspf
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
-27.75,
1026.75
],
"origin_uuid": "95f656e7a338"
"origin_uuid": "117366e58983"
},
{
"type": "group",
Expand Down Expand Up @@ -84,7 +84,7 @@
-27.75,
1026.75
],
"origin_uuid": "95f656e7a338"
"origin_uuid": "117366e58983"
}
]
},
Expand All @@ -108,7 +108,7 @@
-135.08336345098002,
134.08336654901996
],
"origin_uuid": "95f656e7a338"
"origin_uuid": "117366e58983"
}
]
},
Expand All @@ -131,7 +131,42 @@
-128.0,
124.0
],
"origin_uuid": "95f656e7a338"
"origin_uuid": "117366e58983"
},
{
"type": "channel",
"name": "_C_PositiveValues",
"unit": "",
"flags": 32,
"enabled": true,
"individual_axis": false,
"common_axis": false,
"color": "#b8f11b",
"computed": true,
"ranges": [],
"precision": 3,
"fmt": "{}",
"format": "phys",
"mode": "phys",
"computation": {
"args": {
"a": []
},
"channel_comment": "",
"channel_name": "_C_PositiveValues",
"channel_unit": "",
"computation_mode": "sample_by_sample",
"definition": "",
"function": "PositiveValues",
"triggering": "triggering_on_channel",
"triggering_value": "ASAM.M.VIRTUAL.SCALAR.SWORD.PHYSICAL",
"type": "python_function"
},
"y_range": [
0.0,
0.0
],
"origin_uuid": "117366e58983"
}
],
"pattern": {},
Expand Down Expand Up @@ -245,5 +280,7 @@
"type": "Tabular"
}
],
"functions": {}
"functions": {
"PositiveValues": "def PositiveValues (a=0, t=0):\n\tif a > 0:\n\t\treturn a\n\telse:\n \treturn 0"
}
}
79 changes: 77 additions & 2 deletions test/asammdf/gui/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import shutil
import sys
from test.asammdf.gui import QtCore, QtTest
import time
import unittest
from unittest import mock
Expand All @@ -25,6 +26,9 @@
app.setApplicationName("py-asammdf")


@unittest.skipIf(
sys.platform == "darwin", "Test Development on MacOS was not done yet."
)
class TestBase(unittest.TestCase):
longMessage = False

Expand All @@ -47,15 +51,15 @@ def manual_use(w):

@staticmethod
def processEvents(timeout=0.001):
app.processEvents()
QtCore.QCoreApplication.processEvents()
if timeout:
time.sleep(timeout)

def setUp(self) -> None:
if os.path.exists(self.test_workspace):
shutil.rmtree(self.test_workspace)
os.makedirs(self.test_workspace)
app.processEvents()
self.processEvents()

@classmethod
def setUpClass(cls):
Expand All @@ -73,3 +77,74 @@ def tearDownClass(cls):
def tearDown(self):
self.processEvents()
shutil.rmtree(self.test_workspace)


class DragAndDrop:
_previous_position = None

class MoveThread(QtCore.QThread):
def __init__(self, widget, position=None, step=None):
super().__init__()
self.widget = widget
self.position = position
self.step = step

def run(self):
time.sleep(0.1)
if not self.step:
QtTest.QTest.mouseMove(self.widget, self.position)
else:
for step in range(self.step):
QtTest.QTest.mouseMove(
self.widget, self.position + QtCore.QPoint(step, step)
)
QtTest.QTest.qWait(2)
QtTest.QTest.qWait(10)
# Release
QtTest.QTest.mouseRelease(
self.widget,
QtCore.Qt.LeftButton,
QtCore.Qt.NoModifier,
self.position,
)
QtTest.QTest.qWait(10)

def __init__(self, source_widget, destination_widget, source_pos, destination_pos):
# Ensure that previous drop was not in the same place because mouse needs to be moved.
if self._previous_position and self._previous_position == destination_pos:
move_thread = DragAndDrop.MoveThread(
widget=source_widget, position=QtCore.QPoint(101, 101)
)
move_thread.start()
move_thread.wait()
move_thread.quit()
DragAndDrop._previous_position = destination_pos

QtCore.QCoreApplication.processEvents()
if hasattr(source_widget, "viewport"):
source_viewport = source_widget.viewport()
else:
source_viewport = source_widget
# Press on Source Widget
QtTest.QTest.mousePress(
source_viewport, QtCore.Qt.LeftButton, QtCore.Qt.NoModifier, source_pos
)
# Drag few pixels in order to detect startDrag event
# drag_thread = DragAndDrop.MoveThread(widget=source_widget, position=source_pos, step=50)
# drag_thread.start()
# Move to Destination Widget
if hasattr(destination_widget, "viewport") and sys.platform == "win32":
destination_viewport = destination_widget.viewport()
else:
destination_viewport = destination_widget
move_thread = DragAndDrop.MoveThread(
widget=destination_viewport, position=destination_pos
)
move_thread.start()

source_widget.startDrag(QtCore.Qt.MoveAction)
QtTest.QTest.qWait(50)

# drag_thread.wait()
move_thread.wait()
move_thread.quit()
Loading

0 comments on commit 864eda9

Please sign in to comment.