File: test_drag_drop.py

package info (click to toggle)
python-enaml 0.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,284 kB
  • sloc: python: 31,443; cpp: 4,499; makefile: 140; javascript: 68; lisp: 53; sh: 20
file content (239 lines) | stat: -rw-r--r-- 6,891 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# ------------------------------------------------------------------------------
# Copyright (c) 2022-2024, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
# ------------------------------------------------------------------------------
"""Test the drag drop feature.

Due to the the limitations of QTest (cannot keep a button pressed during move),
we split the tests between:
- starting a drag using QTest.MoveTo and testing both success drop and failed drop
  through monkeypatching of the exec_ method on QDrag. This allows to avoid issues
  with a possible blocking behavior.
- handling drag enter, leave and drop event through direct call to the relevant
  widget method.

This is not perfect but should catch most regressions.

"""
import pytest

try:
    from enaml.qt.QtCore import QEvent, QPoint, QPointF, Qt
    from enaml.qt.QtGui import (
        QCursor,
        QDrag,
        QDragEnterEvent,
        QDragMoveEvent,
        QDragLeaveEvent,
        QDropEvent,
        QMouseEvent,
    )

    actions = [Qt.DropAction.CopyAction, Qt.DropAction.IgnoreAction]
except ImportError:
    # The enaml_qtbot will cause the tests to be skipped
    actions = []

from utils import compile_source, wait_for_window_displayed

SOURCE = """
from enaml.drag_drop import DragData, DropAction
from enaml.layout.api import hbox, vbox
from enaml.widgets.api import Container, Feature, Label, MultilineField, Window


def create_drag_data(dtype, data):
    drag = DragData()
    drag.supported_actions = DropAction.Copy
    drag.mime_data.set_data(dtype, data)
    return drag


enamldef DragLabel(Label):
    attr dtype: str
    attr data: bytes
    attr dragged: bool = False
    attr drag_ended: bool = False
    attr success = False
    style_class << 'success' if success else 'fail'
    features = Feature.DragEnabled
    drag_start => ():
        print("start")
        self.dragged = True
        return create_drag_data(dtype, data)
    drag_end => (drag_data, result):
        print("end", result)
        self.drag_ended = True
        self.success = result == DropAction.Copy


enamldef DropField(MultilineField):
    attr dtype: str
    attr awaiting_drop = None
    attr drag_moved = False
    features = Feature.DropEnabled
    drag_enter => (event):
        print("enter")
        if event.mime_data().has_format(dtype):
            self.awaiting_drop = True
            event.accept_proposed_action()
        else:
            self.awaiting_drop = False
    drag_move => (event):
        self.drag_moved = True
        print("move")
    drag_leave => ():
        print("leave")
        self.awaiting_drop = None
    drop => (event):
        print("dropped")
        self.awaiting_drop = None
        self.text = event.mime_data().data(dtype).decode('utf-8')
        event.accept()


enamldef Main(Window):

    alias s1: lbl1
    alias s2: lbl2
    alias dt: target
    always_on_top = True

    Container:
        constraints = [
            hbox(vbox(lbl1, lbl2), target),
        ]
        DragLabel: lbl1:
            text = 'Drag Me 1'
            dtype = "text"
            data = b"1"
        DragLabel: lbl2:
            text = 'Drag Me 2'
            dtype = "fake"
            data = b"2"
        DropField: target:
            dtype = "text"
            hug_width = 'strong'
            read_only = True

"""


@pytest.mark.parametrize("action", actions)
def test_drag_with_valid_drop(enaml_qtbot, monkeypatch, action):
    """Test performing a drag and drop operation."""
    win = compile_source(SOURCE, "Main")()
    win.show()
    wait_for_window_displayed(enaml_qtbot, win)

    monkeypatch.setattr(QDrag, "exec_", lambda *args: action)

    enaml_qtbot.mousePress(win.s1.proxy.widget, Qt.MouseButton.LeftButton)

    # This erroneously release the button causing both the drag to start and end
    # The monkeypatch avoid blocking and dealing with always fail drop since we
    # release before entering the target.
    enaml_qtbot.post_event(
        win.s1.proxy.widget,
        QMouseEvent(
            QEvent.MouseMove,
            QPointF(-1, -1),
            QPointF(QCursor.pos()),
            Qt.LeftButton,
            Qt.LeftButton,
            Qt.NoModifier,
        ),
    )

    enaml_qtbot.wait_until(lambda: win.s1.dragged)
    enaml_qtbot.wait_until(lambda: win.s1.drag_ended)

    assert win.s1.success == (action == Qt.DropAction.CopyAction)


@pytest.mark.parametrize("origin, expected_success", [("s1", True), ("s2", False)])
def test_drag_enter(enaml_qtbot, origin, expected_success):
    """Test handling drag enter.

    We cannot rely on event posting since we do not want to call QDrag.exec which
    can block.

    """
    win = compile_source(SOURCE, "Main")()
    win.show()
    wait_for_window_displayed(enaml_qtbot, win)

    drag_data = getattr(win, origin).drag_start()

    enter_event = QDragEnterEvent(
        QPoint(0, 0),
        Qt.DropAction(drag_data.supported_actions),
        drag_data.mime_data.q_data(),
        Qt.LeftButton,
        Qt.NoModifier,
    )

    win.dt.proxy.widget.dragEnterEvent(enter_event)
    if expected_success:
        assert enter_event.isAccepted()
        assert win.dt.awaiting_drop is True
    else:
        assert not enter_event.isAccepted()
        assert win.dt.awaiting_drop is False


def test_drag_move_and_leave(enaml_qtbot):
    """Test handling drag move and leave."""
    win = compile_source(SOURCE, "Main")()
    win.show()
    wait_for_window_displayed(enaml_qtbot, win)

    drag_data = win.s1.drag_start()

    enter_event = QDragEnterEvent(
        QPoint(0, 0),
        Qt.DropAction(drag_data.supported_actions),
        drag_data.mime_data.q_data(),
        Qt.LeftButton,
        Qt.NoModifier,
    )
    win.dt.proxy.widget.dragEnterEvent(enter_event)
    assert win.dt.awaiting_drop is True

    move_event = QDragMoveEvent(
        QPoint(0, 0),
        Qt.DropAction(drag_data.supported_actions),
        drag_data.mime_data.q_data(),
        Qt.LeftButton,
        Qt.NoModifier,
    )
    win.dt.proxy.widget.dragMoveEvent(move_event)
    assert win.dt.drag_moved

    leave_event = QDragLeaveEvent()
    win.dt.proxy.widget.dragLeaveEvent(leave_event)
    assert win.dt.awaiting_drop is None


def test_drop(enaml_qtbot):
    """Test handling drag move and leave."""
    win = compile_source(SOURCE, "Main")()
    win.show()
    wait_for_window_displayed(enaml_qtbot, win)

    drag_data = win.s1.drag_start()

    drop_event = QDropEvent(
        QPointF(0, 0),
        Qt.DropAction(drag_data.supported_actions),
        drag_data.mime_data.q_data(),
        Qt.LeftButton,
        Qt.NoModifier,
        QEvent.Type.Drop,
    )
    win.dt.proxy.widget.dropEvent(drop_event)
    assert drop_event.isAccepted()
    assert win.dt.text == "1"