Skip to content
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

[ENH] Widget testing utilities #1939

Merged
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ recursive-include Orange/canvas *.qss *ico *.png *.svg *.ico *.html
recursive-include Orange/canvas/application/workflows *.ows

recursive-include Orange/widgets *.png *.svg *.js *.css *.html
recursive-include Orange/widgets/tests *.tab
recursive-include Orange/widgets/utils/plot *.fs *.vs *.gs *.obj

recursive-include distribute *.svg *.desktop
Expand Down
58 changes: 57 additions & 1 deletion Orange/widgets/tests/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import unittest
import sip


import numpy as np
from AnyQt.QtWidgets import (
Expand Down Expand Up @@ -163,6 +164,9 @@ def send_signal(self, input_name, value, *args, widget=None):
if input_signal.name == input_name:
getattr(widget, input_signal.handler)(value, *args)
break
else:
raise ValueError("'{}' is not an input name for widget {}"
.format(input_name, type(widget).__name__))
widget.handleNewSignals()

def get_output(self, output_name, widget=None):
Expand Down Expand Up @@ -561,3 +565,55 @@ def _compare_selected_annotated_domains(self, selected, annotated):
selected_vars = selected.domain.variables + selected.domain.metas
annotated_vars = annotated.domain.variables + annotated.domain.metas
self.assertLess(set(selected_vars), set(annotated_vars))


class datasets:
@staticmethod
def path(filename):
dirname = os.path.join(os.path.dirname(__file__), "datasets")
return os.path.join(dirname, filename)

@classmethod
def missing_data_1(cls):
"""
Data set with 3 continuous features (X{1,2,3}) where all the columns
and rows contain at least one NaN value.

One discrete class D with NaN values
Mixed continuous/discrete/string metas ({X,D,S}M)

Returns
-------
data : Orange.data.Table
"""
return Table(cls.path("missing_data_1.tab"))

@classmethod
def missing_data_2(cls):
"""
Data set with 3 continuous features (X{1,2,3}) where all the columns
and rows contain at least one NaN value and X1, X2 are constant.

One discrete constant class D with NaN values.
Mixed continuous/discrete/string class metas ({X,D,S}M)

Returns
-------
data : Orange.data.Table
"""
return Table(cls.path("missing_data_2.tab"))

@classmethod
def missing_data_3(cls):
"""
Data set with 3 discrete features D{1,2,3} where all the columns and
rows contain at least one NaN value

One discrete class D with NaN values
Mixes continuous/discrete/string metas ({X,D,S}M)

Returns
-------
data : Orange.data.Table
"""
return Table(cls.path("missing_data_3.tab"))
8 changes: 8 additions & 0 deletions Orange/widgets/tests/datasets/missing_data_1.tab
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
X1 X2 X3 D XM DM SM
c c c d c d string
class meta meta meta
? 2 3 a 1 a a
2 ? 1 b 2 b
3 1 ? ? ? b b
2 ? 1 b ? ?
? 2 3 a 5 b c
8 changes: 8 additions & 0 deletions Orange/widgets/tests/datasets/missing_data_2.tab
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
X1 X2 X3 D XM DM SM
c c c d c d string
class meta meta meta
? 1 3 a 1 a a
1 ? 1 a 2 b
1 1 ? ? ? b b
1 ? 1 a ? ?
? 1 3 a 5 b c
8 changes: 8 additions & 0 deletions Orange/widgets/tests/datasets/missing_data_3.tab
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
D1 D2 D3 D XM DM SM
d d d d c d s
class meta meta meta
? b c a 1 a a
b ? a b 1 b a
c a ? ? 2 ?
b ? a b ? ? b
? b c a 3 b c
287 changes: 287 additions & 0 deletions Orange/widgets/tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,287 @@
import sys
import warnings
import contextlib

from AnyQt.QtCore import Qt, QObject, QEventLoop, QTimer
from AnyQt.QtTest import QTest


class EventSpy(QObject):
"""
A testing utility class (similar to QSignalSpy) to record events
delivered to a QObject instance.

Note
----
Only event types can be recorded (as QEvent instances are deleted
on delivery).

Note
----
Can only be used with a QCoreApplication running.

Parameters
----------
object : QObject
An object whose events need to be recorded.
etype : Union[QEvent.Type, Sequence[QEvent.Type]
A event type (or types) that should be recorded
"""
def __init__(self, object, etype, **kwargs):
super().__init__(**kwargs)
if not isinstance(object, QObject):
raise TypeError

self.__object = object
try:
len(etype)
except TypeError:
etypes = {etype}
else:
etypes = set(etype)

self.__etypes = etypes
self.__record = []
self.__loop = QEventLoop()
self.__timer = QTimer(self, singleShot=True)
self.__timer.timeout.connect(self.__loop.quit)
self.__object.installEventFilter(self)

def wait(self, timeout=5000):
"""
Start an event loop that runs until a spied event or a timeout occurred.

Parameters
----------
timeout : int
Timeout in milliseconds.

Returns
-------
res : bool
True if the event occurred and False otherwise.

Example
-------
>>> app = QCoreApplication.instance() or QCoreApplication([])
>>> obj = QObject()
>>> spy = EventSpy(obj, QEvent.User)
>>> app.postEvent(obj, QEvent(QEvent.User))
>>> spy.wait()
True
>>> print(spy.events())
[1000]
"""
count = len(self.__record)
self.__timer.stop()
self.__timer.setInterval(timeout)
self.__timer.start()
self.__loop.exec_()
self.__timer.stop()
return len(self.__record) != count

def eventFilter(self, reciever, event):
if reciever is self.__object and event.type() in self.__etypes:
self.__record.append(event.type())
if self.__loop.isRunning():
self.__loop.quit()
return super().eventFilter(reciever, event)

def events(self):
"""
Return a list of all (listened to) event types that occurred.

Returns
-------
events : List[QEvent.Type]
"""
return list(self.__record)


@contextlib.contextmanager
def excepthook_catch(raise_on_exit=True):
"""
Override `sys.excepthook` with a custom handler to record unhandled
exceptions.

Use this to capture or note exceptions that are raised and
unhandled within PyQt slots or virtual function overrides.

Note
----
The exceptions are still dispatched to the original `sys.excepthook`

Parameters
----------
raise_on_exit : bool
If True then the (first) exception that was captured will be
reraised on context exit

Returns
-------
ctx : ContextManager
A context manager

Example
-------
>>> class Obj(QObject):
... signal = pyqtSignal()
...
>>> o = Obj()
>>> o.signal.connect(lambda : 1/0)
>>> with excepthook_catch(raise_on_exit=False) as exc_list:
... o.signal.emit()
...
>>> print(exc_list) # doctest: +ELLIPSIS
[(<class 'ZeroDivisionError'>, ZeroDivisionError('division by zero',), ...
"""
excepthook = sys.excepthook
if excepthook != sys.__excepthook__:
warnings.warn(
"sys.excepthook was already patched (is {})"
"(just thought you should know this)".format(excepthook),
RuntimeWarning, stacklevel=2)
seen = []

def excepthook_handle(exctype, value, traceback):
seen.append((exctype, value, traceback))
excepthook(exctype, value, traceback)

sys.excepthook = excepthook_handle
shouldraise = raise_on_exit
try:
yield seen
except BaseException:
# propagate/preserve exceptions from within the ctx
shouldraise = False
raise
finally:
if sys.excepthook == excepthook_handle:
sys.excepthook = excepthook
else:
raise RuntimeError(
"The sys.excepthook that was installed by "
"'excepthook_catch' context at enter is not "
"the one present at exit.")
if shouldraise and seen:
raise seen[0][1]


class simulate:
"""
Utility functions for simulating user interactions with Qt widgets.
"""
@staticmethod
def combobox_run_through_all(cbox, delay=-1):
"""
Run through all items in a given combo box, simulating the user
focusing the combo box and pressing the Down arrow key activating
all the items on the way.

Unhandled exceptions from invoked PyQt slots/virtual function overrides
are captured and reraised.

Parameters
----------
cbox : QComboBox
delay : int
Run the event loop after the simulated key press (-1, the default,
means no delay)

See Also
--------
QTest.keyClick
"""
assert cbox.focusPolicy() & Qt.TabFocus
cbox.setFocus(Qt.TabFocusReason)
cbox.setCurrentIndex(-1)
for i in range(cbox.count()):
with excepthook_catch() as exlist:
QTest.keyClick(cbox, Qt.Key_Down, delay=delay)
if exlist:
raise exlist[0][1] from exlist[0][1]

@staticmethod
def combobox_activate_index(cbox, index, delay=-1):
"""
Activate an item at `index` in a given combo box.

The item at index **must** be enabled and selectable.

Parameters
----------
cbox : QComboBox
index : int
delay : int
Run the event loop after the signals are emitted for `delay`
milliseconds (-1, the default, means no delay).
"""
assert 0 <= index < cbox.count()
model = cbox.model()
column = cbox.modelColumn()
root = cbox.rootModelIndex()
mindex = model.index(index, column, root)
assert mindex.flags() & Qt.ItemIsEnabled
cbox.setCurrentIndex(index)
text = cbox.currentText()
# QComboBox does not have an interface which would allow selecting
# the current item as if a user would. Only setCurrentIndex which
# does not emit the activated signals.
cbox.activated[int].emit(index)
cbox.activated[str].emit(text)
if delay >= 0:
QTest.qWait(delay)

@staticmethod
def combobox_index_of(cbox, value, role=Qt.DisplayRole):
"""
Find the index of an **selectable** item in a combo box whose `role`
data contains the given `value`.

Parameters
----------
cbox : QComboBox
value : Any
role : Qt.ItemDataRole

Returns
-------
index : int
An index such that `cbox.itemData(index, role) == value` **and**
the item is enabled for selection or -1 if such an index could
not be found.
"""
model = cbox.model()
column = cbox.modelColumn()
root = cbox.rootModelIndex()
for i in range(model.rowCount(root)):
index = model.index(i, column, root)
if index.data(role) == value and \
index.flags() & Qt.ItemIsEnabled:
pos = i
break
else:
pos = -1
return pos

@staticmethod
def combobox_activate_item(cbox, value, role=Qt.DisplayRole, delay=-1):
"""
Find an **selectable** item in a combo box whose `role` data
contains the given value and activate it.

Raise an ValueError if the item could not be found.

Parameters
----------
cbox : QComboBox
value : Any
role : Qt.ItemDataRole
delay : int
Run the event loop after the signals are emitted for `delay`
milliseconds (-1, the default, means no delay).
"""
index = simulate.combobox_index_of(cbox, value, role)
if index < 0:
raise ValueError("{!r} not in {}".format(value, cbox))
simulate.combobox_activate_index(cbox, index, delay)
Loading