Skip to content

Commit

Permalink
test_schemeedit: Use drag/drop test helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
ales-erjavec committed Oct 13, 2020
1 parent f95b0cf commit b6a1368
Showing 1 changed file with 24 additions and 67 deletions.
91 changes: 24 additions & 67 deletions orangecanvas/document/tests/test_schemeedit.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

from ...registry.tests import small_testing_registry

from ...gui.test import QAppTestCase, mouseMove
from ...gui.test import QAppTestCase, mouseMove, dragDrop, dragEnterLeave
from ...utils import findf


Expand Down Expand Up @@ -431,78 +431,50 @@ def test_drop_event(self):
w.setRegistry(self.reg)
workflow = w.scheme()
desc = self.reg.widget("one")
view = w.view()
viewport = w.view().viewport()
mime = QMimeData()
mime.setData(
"application/vnd.orange-canvas.registry.qualified-name",
desc.qualified_name.encode("utf-8")
)
ev = QDragEnterEvent(
QPoint(10, 10), Qt.CopyAction, mime, Qt.LeftButton, Qt.NoModifier)
ev.setAccepted(False)
QApplication.sendEvent(view.viewport(), ev)
self.assertTrue(ev.isAccepted())

ev = QDragMoveEvent(
QPoint(11, 11), Qt.CopyAction, mime, Qt.LeftButton, Qt.NoModifier)
ev.setAccepted(False)
QApplication.sendEvent(view.viewport(), ev)
self.assertTrue(ev.isAccepted())

ev = QDropEvent(
QPoint(11, 11), Qt.CopyAction, mime, Qt.LeftButton, Qt.NoModifier)
ev.setAccepted(False)
QApplication.sendEvent(view.viewport(), ev)

self.assertTrue(dragDrop(viewport, mime, QPoint(10, 10)))

self.assertEqual(len(workflow.nodes), 1)
self.assertEqual(workflow.nodes[0].description, desc)

dragEnterLeave(viewport, mime)

self.assertEqual(len(workflow.nodes), 1)

def test_drag_drop(self):
w = self.w
w.setRegistry(self.reg)
handler = TestDropHandler()
w.setDropHandlers([handler])
view = w.view()
viewport = w.view().viewport()
mime = QMimeData()
mime.setData(handler.format_, b'abc')
ev = QDragEnterEvent(
QPoint(10, 10), Qt.CopyAction, mime, Qt.LeftButton, Qt.NoModifier)
QApplication.sendEvent(view.viewport(), ev)
self.assertTrue(ev.isAccepted())
self.assertEqual(handler.accepts_calls, 1)

ev = QDragMoveEvent(
QPoint(11, 11), Qt.CopyAction, mime, Qt.LeftButton, Qt.NoModifier)
QApplication.sendEvent(view.viewport(), ev)
self.assertTrue(ev.isAccepted())
self.assertEqual(handler.accepts_calls, 2)

ev = QDropEvent(
QPoint(11, 11), Qt.CopyAction, mime, Qt.LeftButton, Qt.NoModifier)
QApplication.sendEvent(view.viewport(), ev)

dragDrop(viewport, mime, QPoint(10, 10))

self.assertEqual(handler.doDrop_calls, 1)
self.assertGreaterEqual(handler.accepts_calls, 1)
self.assertIsNone(w._userInteractionHandler())

handler.accepts_calls = 0
handler.doDrop_calls = 0
mime = QMimeData()
mime.setData("application/prs.do-not-accept-this", b'abc')
ev = QDragEnterEvent(
QPoint(10, 10), Qt.CopyAction, mime, Qt.LeftButton, Qt.NoModifier)
QApplication.sendEvent(view.viewport(), ev)
self.assertEqual(handler.accepts_calls, 1)

ev = QDropEvent(
QPoint(11, 11), Qt.CopyAction, mime, Qt.LeftButton, Qt.NoModifier)
QApplication.sendEvent(view.viewport(), ev)

dragDrop(viewport, mime, QPoint(10, 10))

self.assertGreaterEqual(handler.accepts_calls, 1)
self.assertEqual(handler.doDrop_calls, 0)
self.assertIsNone(w._userInteractionHandler())

ev = QDragEnterEvent(
QPoint(10, 10), Qt.CopyAction, mime, Qt.LeftButton, Qt.NoModifier)
QApplication.sendEvent(view.viewport(), ev)
ev = QDragLeaveEvent()
QApplication.sendEvent(view.viewport(), ev)
dragEnterLeave(viewport, mime, QPoint(10, 10))

self.assertIsNone(w._userInteractionHandler())

@mock.patch(
Expand All @@ -523,30 +495,21 @@ def test_plugin_drag_drop(self):
w.setRegistry(self.reg)
w.setDropHandlers([handler])
workflow = w.scheme()
view = w.view()
viewport = w.view().viewport()
# Test empty handler
mime = QMimeData()
mime.setData(TestDropHandler.format_, b'abc')
ev = QDragEnterEvent(
QPoint(10, 10), Qt.CopyAction, mime, Qt.LeftButton, Qt.NoModifier)
QApplication.sendEvent(view.viewport(), ev)

ev = QDropEvent(
QPoint(10, 10), Qt.CopyAction, mime, Qt.LeftButton, Qt.NoModifier)
QApplication.sendEvent(view.viewport(), ev)
dragDrop(viewport, mime, QPoint(10, 10))

self.assertIsNone(w._userInteractionHandler())

# test create node handler
mime = QMimeData()
mime.setData(TestNodeFromMimeData.format_, b'abc')

ev = QDragEnterEvent(
QPoint(10, 10), Qt.CopyAction, mime, Qt.LeftButton, Qt.NoModifier)
QApplication.sendEvent(view.viewport(), ev)
dragDrop(viewport, mime, QPoint(10, 10))

ev = QDropEvent(
QPoint(10, 10), Qt.CopyAction, mime, Qt.LeftButton, Qt.NoModifier)
QApplication.sendEvent(view.viewport(), ev)
self.assertIsNone(w._userInteractionHandler())
self.assertEqual(len(workflow.nodes), 1)
self.assertEqual(workflow.nodes[0].description.name, "one")
Expand All @@ -559,18 +522,12 @@ def test_plugin_drag_drop(self):
mime.setData(TestDropHandler.format_, b'abc')
mime.setData(TestNodeFromMimeData.format_, b'abc')

ev = QDragEnterEvent(
QPoint(10, 10), Qt.CopyAction, mime, Qt.LeftButton, Qt.NoModifier)
QApplication.sendEvent(view.viewport(), ev)
ev = QDropEvent(
QPoint(10, 10), Qt.CopyAction, mime, Qt.LeftButton, Qt.NoModifier)

def exec(self, *args):
return action_by_name(self.actions(), "-pick-me")

# intercept QMenu.exec, force select the TestNodeFromMimeData handler
with mock.patch.object(QMenu, "exec", exec):
QApplication.sendEvent(view.viewport(), ev)
dragDrop(viewport, mime, QPoint(10, 10))

self.assertEqual(len(workflow.nodes), 1)
self.assertEqual(workflow.nodes[0].description.name, "one")
Expand Down

0 comments on commit b6a1368

Please sign in to comment.