Skip to content

Commit

Permalink
Merge pull request #1056 from tov101/dnd
Browse files Browse the repository at this point in the history
Rework drag and drop in tests
  • Loading branch information
danielhrisca authored Jul 22, 2024
2 parents 6d28dbc + a2bd150 commit cd2557c
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 146 deletions.
37 changes: 21 additions & 16 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
name: tests

on: [push, pull_request]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

on: [ push, pull_request ]

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-22.04, macos-latest, windows-latest]
python-version: ["3.9", "3.10", "3.11", "3.12"]
os: [ ubuntu-22.04, macos-latest, windows-latest ]
python-version: [ "3.9", "3.10", "3.11", "3.12" ]
fail-fast: false

steps:
Expand All @@ -23,23 +27,24 @@ jobs:
- name: Install packages
shell: bash
run: |
pip install tox tox-gh-actions
if [ "$RUNNER_OS" == "Linux" ]; then
sudo apt update
sudo apt-get install libegl1 libopengl0
sudo apt-get install '^libxcb.*-dev' libx11-xcb-dev libglu1-mesa-dev libxrender-dev libxi-dev libxkbcommon-dev libxkbcommon-x11-dev
sudo apt-get install libxcb-xinerama0
sudo apt-get install libxkbcommon-x11-0
sudo apt-get install xorg
sudo apt-get install -y xvfb
fi
pip install tox tox-gh-actions
if [ "$RUNNER_OS" == "Linux" ]; then
sudo apt update
sudo apt-get install libegl1 libopengl0
sudo apt-get install '^libxcb.*-dev' libx11-xcb-dev libglu1-mesa-dev libxrender-dev libxi-dev libxkbcommon-dev libxkbcommon-x11-dev
sudo apt-get install libxcb-xinerama0
sudo apt-get install libxkbcommon-x11-0
sudo apt-get install xorg
sudo apt-get install -y xvfb
fi
- name: Start Xvfb
if: matrix.os == 'ubuntu-22.04'
run: |
Xvfb :0 -screen 0 1024x768x24 > /dev/null 2>&1 &
export DISPLAY=:0
Xvfb :0 -screen 0 1024x768x24 > /dev/null 2>&1 &
export DISPLAY=:0
touch ~/.Xauthority
# see: https://github.com/ymyzk/tox-gh-actions
- name: Run tests
run: tox
Expand Down
3 changes: 2 additions & 1 deletion run_black_and_ruff.bat
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pip install -U black ruff && ^
black --config pyproject.toml . && ^
black --config pyproject.toml ./src && ^
black --config pyproject.toml ./test && ^
black --config pyproject.toml asammdf.spec && ^
black --config pyproject.toml setup.py && ^
ruff check --fix ./src && ^
Expand Down
14 changes: 7 additions & 7 deletions src/asammdf/blocks/mdf_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3194,7 +3194,7 @@ def get_master(
return self._master

if raster is not None:
PendingDeprecationWarning(
raise PendingDeprecationWarning(
"the argument raster is deprecated since version 5.13.0 " "and will be removed in a future release"
)

Expand Down Expand Up @@ -3790,10 +3790,10 @@ def included_channels(

result = {}

for group_index, channels in gps.items():
for group_index, _channels in gps.items():
group = self.groups[group_index]

channel_dependencies = [group.channel_dependencies[ch_nr] for ch_nr in channels]
channel_dependencies = [group.channel_dependencies[ch_nr] for ch_nr in _channels]

if minimal:
for dep in channel_dependencies:
Expand All @@ -3802,15 +3802,15 @@ def included_channels(
for gp_nr, ch_nr in dep.referenced_channels:
if gp_nr == group_index:
try:
channels.remove(ch_nr)
_channels.remove(ch_nr)
except KeyError:
pass

gp_master = self.masters_db.get(group_index, None)
if skip_master and gp_master is not None and gp_master in channels and len(channels) > 1:
channels.remove(gp_master)
if skip_master and gp_master is not None and gp_master in _channels and len(_channels) > 1:
_channels.remove(gp_master)

result[group_index] = {group_index: sorted(channels)}
result[group_index] = {group_index: sorted(_channels)}

return result

Expand Down
2 changes: 1 addition & 1 deletion src/asammdf/blocks/mdf_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -8360,7 +8360,7 @@ def get_master(
"""

if raster is not None:
PendingDeprecationWarning(
raise PendingDeprecationWarning(
"the argument raster is deprecated since version 5.13.0 " "and will be removed in a future release"
)
if self._master is not None:
Expand Down
4 changes: 2 additions & 2 deletions src/asammdf/gui/widgets/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1161,8 +1161,8 @@ def _open_file(self, file_name):
else:
file_names = [file_name]

for file_name in file_names:
file_name = Path(file_name)
for _file_name in file_names:
file_name = Path(_file_name)
index = self.files.count()

try:
Expand Down
8 changes: 4 additions & 4 deletions src/asammdf/gui/widgets/mdi_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -3187,12 +3187,12 @@ def edit_channel(self, channel, item, widget):

widget.plot.update()

for channel in widget.plot.signals:
if channel.uuid == uuid:
for _channel in widget.plot.signals:
if _channel.uuid == uuid:
continue

if channel.flags & channel.Flags.computed:
required_channels = set(get_required_from_computed(channel.computation))
if _channel.flags & _channel.Flags.computed:
required_channels = set(get_required_from_computed(_channel.computation))
if old_name in required_channels:
item = widget.item_by_uuid

Expand Down
4 changes: 2 additions & 2 deletions src/asammdf/signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ def __init__(
encodings = ["utf-8", "latin-1"]
else:
encodings = [encoding, "utf-8", "latin-1"]
for encoding in encodings:
for _encoding in encodings:
try:
samples = encode(samples, encoding)
samples = encode(samples, _encoding)
break
except:
continue
Expand Down
138 changes: 71 additions & 67 deletions test/asammdf/gui/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,21 @@ class DragAndDrop
import pathlib
import shutil
import sys
import threading
import time
import unittest
from unittest import mock

import numpy as np
import pyqtgraph
from PySide6 import QtCore, QtGui, QtTest, QtWidgets

if sys.platform == "win32":
import win32api
import win32con
else:
import pyautogui

from asammdf.gui.utils import excepthook

if sys.platform == "win32":
Expand Down Expand Up @@ -69,18 +77,20 @@ def manual_use(widget, duration=None):
duration : float | None
duration in seconds
"""
widget.showMaximized()
app.exec()
if duration is None:
duration = 3600
else:
duration = abs(duration)

w.showNormal()
widget.showNormal()

loop = QtCore.QEventLoop()
QtCore.QTimer.singleShot(int(duration * 1000), loop.quit)
loop.exec_()

w.showNormal()
widget.showNormal()

@staticmethod
def processEvents(timeout=0.001):
Expand Down Expand Up @@ -159,70 +169,6 @@ def mouseDClick_WidgetItem(self, qitem):
self.processEvents(0.5)


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.MouseButton.LeftButton,
QtCore.Qt.KeyboardModifiers(),
self.position,
)
QtTest.QTest.qWait(10)

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

QtCore.QCoreApplication.processEvents()
if hasattr(src_widget, "viewport"):
source_viewport = src_widget.viewport()
else:
source_viewport = src_widget
# Move to Destination Widget
if hasattr(dst_widget, "viewport"):
destination_viewport = dst_widget.viewport()
else:
destination_viewport = dst_widget

# Press on Source Widget
QtTest.QTest.mousePress(
source_viewport, QtCore.Qt.MouseButton.LeftButton, QtCore.Qt.KeyboardModifiers(), src_pos
)

move_thread = DragAndDrop.MoveThread(widget=destination_viewport, position=dst_pos)
move_thread.start()

src_widget.startDrag(QtCore.Qt.DropAction.MoveAction)
QtTest.QTest.qWait(50)

move_thread.wait()
move_thread.quit()
QtCore.QCoreApplication.processEvents()


class Pixmap:
COLOR_BACKGROUND = "#000000"
COLOR_RANGE = "#000032"
Expand Down Expand Up @@ -322,7 +268,65 @@ def cursors_x(pixmap):
break
count += 1
else:
if count == image.height() - 3:
if count >= image.height() - 3:
cursors.append(x)

return cursors


class DragAndDrop:
def __init__(self, src_widget, dst_widget, src_pos, dst_pos):
src_offset = dst_offset = QtCore.QPoint(0, 1)
if hasattr(src_widget, "header"):
src_offset = QtCore.QPoint(0, src_widget.header().geometry().height() + 1)
if hasattr(dst_widget, "header"):
dst_offset = QtCore.QPoint(0, dst_widget.header().geometry().height() + 1)

t_move = threading.Thread(
target=dnd_worker,
args=(
src_widget.mapToGlobal(src_pos) + src_offset,
dst_widget.mapToGlobal(dst_pos) + dst_offset,
),
)
t_move.start()

while t_move.is_alive():
QtWidgets.QApplication.instance().processEvents()
time.sleep(0.001)

time.sleep(0.2)
for _ in range(10):
QtWidgets.QApplication.instance().processEvents()
time.sleep(0.001)


def dnd_worker(start, end):
x_vals = np.linspace(start.x(), end.x(), 10)
y_vals = np.linspace(start.y(), end.y(), len(x_vals))

if sys.platform == "win32":
win32api.SetCursorPos((start.x(), start.y()))
# Perform left mouse button down event
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, start.x(), start.y(), 0, 0)
# Move the mouse to the ending position
for x, y in zip(x_vals, y_vals):
win32api.SetCursorPos((int(x), int(y)))
time.sleep(0.02)
# Perform left mouse button up event
win32api.SetCursorPos((end.x(), end.y()))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, end.x(), end.y(), 0, 0)
time.sleep(0.2)

else:
# Move the mouse to the starting position
pyautogui.moveTo(start.x(), start.y())
# Perform left mouse button down event
pyautogui.mouseDown()
# Move the mouse to the ending position
for x, y in zip(x_vals, y_vals):
pyautogui.moveTo(int(x), int(y))
time.sleep(0.001)
# Perform left mouse button up event
pyautogui.moveTo(end.x(), end.y())
pyautogui.mouseUp()
3 changes: 2 additions & 1 deletion test/asammdf/gui/widgets/test_BaseFileWidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def setUpFileWidget(self, *args, measurement_file, default):
)
else:
self.widget = FileWidget(measurement_file, *args)
self.widget.showNormal()
self.widget.showMaximized()
self.widget.activateWindow()
self.processEvents()

def create_window(self, window_type, channels_names=(), channels_indexes=()):
Expand Down
Loading

0 comments on commit cd2557c

Please sign in to comment.