summaryrefslogtreecommitdiffstats
path: root/tests/auto/qdrag/tst_qdrag.cpp
blob: 9b0911b27def75e181405576992aacfde0cc1f1a (plain)
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/


#include <QtTest/QtTest>

#include <qcoreapplication.h>
#include <qdebug.h>
#include <qdrag.h>

#ifdef Q_WS_X11
#include <QBitmap>
#include <QPainter>
#include <QX11Info>

#include <X11/extensions/shape.h>
#endif

//TESTED_CLASS=
//TESTED_FILES=

class DragEnterCounterWidget : public QWidget
{
public:
    DragEnterCounterWidget();

    void dragEnterEvent(QDragEnterEvent * event);

    int enterEvents;
};

class DragCounterAndCreatorWidget : public DragEnterCounterWidget
{
public:
    DragCounterAndCreatorWidget();

    void mousePressEvent(QMouseEvent * event);

    int startedDrags;
};

class BiggerDragCounterWidget : public DragEnterCounterWidget
{
public:
    BiggerDragCounterWidget();
};

DragEnterCounterWidget::DragEnterCounterWidget() : QWidget(0), enterEvents(0)
{
    setAcceptDrops(true);
    setWindowFlags(Qt::FramelessWindowHint);
    show();
}

void DragEnterCounterWidget::dragEnterEvent(QDragEnterEvent * event)
{
    event->acceptProposedAction();
    ++enterEvents;
}

DragCounterAndCreatorWidget::DragCounterAndCreatorWidget() : startedDrags(0)
{
    resize(80, 80);
    move(300, 300);
}

void DragCounterAndCreatorWidget::mousePressEvent(QMouseEvent * /*event*/)
{
    ++startedDrags;
    QDrag *drag = new QDrag(this);
    drag->setMimeData(new QMimeData);
    QPixmap p("dummy.png");
    drag->setHotSpot(QPoint( p.width() / 2, p.height() / 2 ));
    drag->setPixmap(p);
    drag->exec();
}

BiggerDragCounterWidget::BiggerDragCounterWidget()
{
    resize(180, 180);
    move(250, 250);
}

class DoMouseReleaseHelper : public QTimer
{
Q_OBJECT

public:
    DoMouseReleaseHelper(QWidget *w, int timeout = 0);

private slots:
    void doMouseRelease();

private:
    QWidget *m_w;
};

DoMouseReleaseHelper::DoMouseReleaseHelper(QWidget *w, int timeout) : m_w(w)
{
    setSingleShot(true);
    start(timeout);
    connect(this, SIGNAL(timeout()), this, SLOT(doMouseRelease()));
}

void DoMouseReleaseHelper::doMouseRelease()
{
    QTest::mouseRelease(m_w, Qt::LeftButton);
}

class DoMouseMoveHelper : public QTimer
{
Q_OBJECT

public:
    DoMouseMoveHelper(QWidget *w, const QPoint &p, int timeout = 0);

private slots:
    void doMouseMove();

private:
    QWidget *m_w;
    QPoint m_p;
};

DoMouseMoveHelper::DoMouseMoveHelper(QWidget *w, const QPoint &p, int timeout) : m_w(w), m_p(p)
{
    setSingleShot(true);
    start(timeout);
    connect(this, SIGNAL(timeout()), this, SLOT(doMouseMove()));
}

void DoMouseMoveHelper::doMouseMove()
{
    QTest::mouseMove(m_w, m_p);
}

class tst_QDrag : public QObject
{
Q_OBJECT

public:
    tst_QDrag();
    virtual ~tst_QDrag();

private slots:
    void getSetCheck();
    void testDragEnterSelf();
    void testDragEnterNoShaping();
    void testDragEnterSomeShaping();
    void testDragEnterAllShaping();
};

tst_QDrag::tst_QDrag()
{
}

tst_QDrag::~tst_QDrag()
{
}

// Testing get/set functions
void tst_QDrag::getSetCheck()
{
    QDrag obj1(0);
    // QMimeData * QDrag::mimeData()
    // void QDrag::setMimeData(QMimeData *)
    QMimeData *var1 = new QMimeData;
    obj1.setMimeData(var1);
    QCOMPARE(var1, obj1.mimeData());
    obj1.setMimeData(var1);
    QCOMPARE(var1, obj1.mimeData());
    obj1.setMimeData((QMimeData *)0);
    QCOMPARE((QMimeData *)0, obj1.mimeData());
    // delete var1; // No delete, since QDrag takes ownership

    Qt::DropAction result = obj1.start();
    QCOMPARE(result, Qt::IgnoreAction);
    result = obj1.start(Qt::MoveAction | Qt::LinkAction);
    QCOMPARE(result, Qt::IgnoreAction);
}

void tst_QDrag::testDragEnterSelf()
{
#ifdef Q_WS_X11
    // Widget of 80x80 at 300, 300
    DragCounterAndCreatorWidget dw;
    QTest::qWaitForWindowShown(&dw);

    // Press mouse to create a drag in dw
    QTest::qWait(100);
    QTest::mouseMove(&dw);
    DoMouseReleaseHelper aux(&dw);
    QTest::mousePress(&dw, Qt::LeftButton);

    // Verify that without a window in the middle the drag goes to dw itself
    QCOMPARE(dw.startedDrags, 1);
    QCOMPARE(dw.enterEvents, 1);
#endif
}

void tst_QDrag::testDragEnterNoShaping()
{
#ifdef Q_WS_X11
    // Widget of 80x80 at 300, 300
    DragCounterAndCreatorWidget dw;
    QTest::qWaitForWindowShown(&dw);

    // Widget of 180x180 at 250, 250
    BiggerDragCounterWidget widgetOnTop;
    QTest::qWaitForWindowShown(&widgetOnTop);

    // Press mouse to create a drag in dw
    QTest::qWait(100);
    QTest::mouseMove(&dw);
    DoMouseReleaseHelper aux(&dw);
    QTest::mousePress(&dw, Qt::LeftButton);

    // Verify that with widgetOnTop in the middle the drag, the drag event does not go to dw
    // and goes to widgetOnTop
    QCOMPARE(dw.startedDrags, 1);
    QCOMPARE(dw.enterEvents, 0);
    QCOMPARE(widgetOnTop.enterEvents, 1);
#endif
}

void tst_QDrag::testDragEnterSomeShaping()
{
#if defined(Q_WS_X11) && defined(ShapeInput)
    // Widget of 80x80 at 300, 300
    DragCounterAndCreatorWidget dw;
    QTest::qWaitForWindowShown(&dw);

    // Widget of 180x180 at 250, 250
    BiggerDragCounterWidget widgetOnTop;
    QTest::qWaitForWindowShown(&widgetOnTop);

    // Punch a hole in widgetOnTop to let the mouse go through the widget
    // in the center of dw
    QBitmap inputShape(180, 180);
    inputShape.fill(Qt::color1);
    QPainter painter(&inputShape);
    painter.fillRect(80, 80, 50, 50, Qt::color0);
    painter.end();
    XShapeCombineRegion(QX11Info::display(), widgetOnTop.effectiveWinId(), ShapeInput, 0, 0, QRegion(inputShape).handle(), ShapeSet);

    // Press mouse to create a drag in dw
    QTest::qWait(100);
    QTest::mouseMove(&dw);
    DoMouseReleaseHelper aux(&dw);
    QTest::mousePress(&dw, Qt::LeftButton);

    // Verify that with a input shaped widgetOnTop in the middle the drag, the drag event goes to dw
    // and does not go to widgetOnTop
    QCOMPARE(dw.startedDrags, 1);
    QCOMPARE(dw.enterEvents, 1);
    QCOMPARE(widgetOnTop.enterEvents, 0);

    // Press mouse to create a drag in dw and move it to the corner of the dw where widgetOnTop is not shaped anymore
    DoMouseMoveHelper aux2(&dw, QPoint(1, 1), 100);
    DoMouseReleaseHelper aux3(&dw, 200);
    QTest::mousePress(&dw, Qt::LeftButton);

    // Verify once we get out of the shaped area the drag event also goes to widgetOnTop
    QCOMPARE(dw.startedDrags, 2);
    QCOMPARE(dw.enterEvents, 2);
    QCOMPARE(widgetOnTop.enterEvents, 1);
#endif
}

void tst_QDrag::testDragEnterAllShaping()
{
#if defined(Q_WS_X11) && defined(ShapeInput)
    // Widget of 80x80 at 300, 300
    DragCounterAndCreatorWidget dw;
    QTest::qWaitForWindowShown(&dw);

    // Widget of 180x180 at 250, 250
    BiggerDragCounterWidget widgetOnTop;
    QTest::qWaitForWindowShown(&widgetOnTop);

    // Make widgetOnTop totally a hole regarding input
    QBitmap inputShape(180, 180);
    inputShape.fill(Qt::color0);
    XShapeCombineRegion(QX11Info::display(), widgetOnTop.effectiveWinId(), ShapeInput, 0, 0, QRegion(inputShape).handle(), ShapeSet);

    // Press mouse to create a drag in dw
    QTest::qWait(100);
    QTest::mouseMove(&dw);
    DoMouseReleaseHelper aux(&dw);
    QTest::mousePress(&dw, Qt::LeftButton);

    // Verify that with a input shaped widgetOnTop in the middle the drag, the drag event goes to dw
    // and does not go to widgetOnTop
    QCOMPARE(dw.startedDrags, 1);
    QCOMPARE(dw.enterEvents, 1);
    QCOMPARE(widgetOnTop.enterEvents, 0);

    // Press mouse to create a drag in dw and move it to the corner of the dw
    DoMouseMoveHelper aux2(&widgetOnTop, QPoint(1, 1));
    DoMouseReleaseHelper aux3(&dw, 200);
    QTest::mousePress(&dw, Qt::LeftButton);

    // Verify the event also went to dw
    QCOMPARE(dw.startedDrags, 2);
    QCOMPARE(dw.enterEvents, 2);
    QCOMPARE(widgetOnTop.enterEvents, 0);
#endif
}

QTEST_MAIN(tst_QDrag)
#include "tst_qdrag.moc"