Skip to content

Commit c45e0a3

Browse files
committed
test_oweditdomain: Add test for context menu
1 parent 0f046bc commit c45e0a3

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

Orange/widgets/data/tests/test_oweditdomain.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from AnyQt.QtCore import QItemSelectionModel, Qt, QItemSelection
1414
from AnyQt.QtWidgets import QAction, QComboBox, QLineEdit, \
15-
QStyleOptionViewItem, QDialog
15+
QStyleOptionViewItem, QDialog, QMenu
1616
from AnyQt.QtTest import QTest, QSignalSpy
1717

1818
from Orange.widgets.utils import colorpalettes
@@ -38,6 +38,7 @@
3838
GroupItemsDialog)
3939
from Orange.widgets.data.owcolor import OWColor, ColorRole
4040
from Orange.widgets.tests.base import WidgetTest, GuiTest
41+
from Orange.widgets.tests.utils import contextMenu
4142
from Orange.tests import test_filename, assert_array_nanequal
4243
from Orange.widgets.utils.state_summary import format_summary_details
4344

@@ -544,6 +545,23 @@ def test_discrete_editor_rename_selected_items_action(self):
544545
list(spy), [[]], 'variable_changed should emit exactly once'
545546
)
546547

548+
def test_discrete_editor_context_menu(self):
549+
w = DiscreteVariableEditor()
550+
v = Categorical("C", ("a", "b", "c"),
551+
(("A", "1"), ("B", "b")), False)
552+
w.set_data_categorical(v, [])
553+
view = w.values_edit
554+
model = view.model()
555+
556+
pos = view.visualRect(model.index(0, 0)).center()
557+
with patch.object(QMenu, "setVisible", return_value=None) as m:
558+
contextMenu(view.viewport(), pos)
559+
m.assert_called()
560+
561+
menu = view.findChild(QMenu)
562+
self.assertIsNotNone(menu)
563+
menu.close()
564+
547565
def test_time_editor(self):
548566
w = TimeVariableEditor()
549567
self.assertEqual(w.get_data(), (None, []))

Orange/widgets/tests/utils.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
from AnyQt.QtCore import Qt, QObject, QEventLoop, QTimer, QLocale, QPoint
88
from AnyQt.QtTest import QTest
9-
from AnyQt.QtGui import QMouseEvent
10-
from AnyQt.QtWidgets import QApplication
9+
from AnyQt.QtGui import QMouseEvent, QContextMenuEvent
10+
from AnyQt.QtWidgets import QApplication, QWidget
1111

1212
from Orange.data import Table, Domain, ContinuousVariable
1313

@@ -323,6 +323,25 @@ def mouseMove(widget, pos=QPoint(), delay=-1): # pragma: no-cover
323323
QApplication.sendEvent(widget, me)
324324

325325

326+
def contextMenu(
327+
widget: QWidget, pos=QPoint(), reason=QContextMenuEvent.Mouse,
328+
modifiers=Qt.NoModifier, delay=-1
329+
) -> None:
330+
"""
331+
Simulate a context menu event on `widget`.
332+
333+
`pos` is the event origin specified in widget's local coordinates. If not
334+
specified. Then widget.rect().center() is used instead.
335+
"""
336+
if pos.isNull():
337+
pos = widget.rect().center()
338+
globalPos = widget.mapToGlobal(pos)
339+
ev = QContextMenuEvent(reason, pos, globalPos, modifiers)
340+
if delay >= 0:
341+
QTest.qWait(delay)
342+
QApplication.sendEvent(widget, ev)
343+
344+
326345
def table_dense_sparse(test_case):
327346
# type: (Callable) -> Callable
328347
"""Run a single test case on both dense and sparse Orange tables.

0 commit comments

Comments
 (0)