summaryrefslogtreecommitdiffstats
path: root/tests/auto/gestures/tst_gestures.cpp
blob: eb692e904890e19599e2e24f1101ae1acd52a6ad (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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** 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 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/


#include <qapplication.h>
#include <qlayout.h>

#include <QtTest/QtTest>

#include "customgesturerecognizer.h"

//TESTED_CLASS=
//TESTED_FILES=

// color generator for syntax highlighting from QQ-26 by Helder Correia
QVector<QColor> highlight(const QColor &bg, const
                          QColor &fg, int noColors)
{
    QVector<QColor> colors;
    const int HUE_BASE = (bg.hue() == -1) ? 90 : bg.hue();
    int h, s, v;
    for (int i = 0; i < noColors; i++) {
        h = int(HUE_BASE + (360.0 / noColors * i)) % 360;
        s = 240;
        v = int(qMax(bg.value(), fg.value()) * 0.85);
        
        const int M = 35;
        if ((h < bg.hue() + M && h > bg.hue() - M)
            || (h < fg.hue() + M && h > fg.hue() - M))
        {
            h = ((bg.hue() + fg.hue()) / (i+1)) % 360;
            s = ((bg.saturation() + fg.saturation() + 2*i)
                 / 2) % 256;
            v = ((bg.value() + fg.value() + 2*i) / 2)
                % 256;
        }
        colors.append(QColor::fromHsv(h, s, v));
    }
    return colors;
}


// a hack to mark an event as spontaneous.
class QETWidget
{
public:
    static void setSpont(QEvent *event, bool spont) { event->spont = spont; }
};

class GestureWidget : public QWidget
{
    Q_OBJECT
    static QVector<QColor> colors;
    static int numberOfWidgets;

public:
    enum Type { DoNotGrabGestures, GrabAllGestures, GrabSingleshot, GrabPinch, GrabSecondFinger };

    static const int LeftMargin = 10;
    static const int TopMargin  = 20;

    GestureWidget(Type type = GrabAllGestures)
    {
        if (colors.isEmpty()) {
            colors = highlight(palette().color(QPalette::Window), palette().color(QPalette::Text), 5);
        }
        QPalette p = palette();
        p.setColor(QPalette::Window, colors[numberOfWidgets % colors.size()]);
        setPalette(p);
        setAutoFillBackground(true);
        ++numberOfWidgets;

        QVBoxLayout *l = new QVBoxLayout(this);
        l->setSpacing(0);
        l->setContentsMargins(LeftMargin, TopMargin, LeftMargin, TopMargin);

        singleshotGestureId = -1;
        pinchGestureId = -1;
        secondFingerGestureId = -1;
        if (type == GrabAllGestures || type == GrabSingleshot) {
            singleshotGestureId = grabGesture(SingleshotGestureRecognizer::Name);
        }
        if (type == GrabAllGestures || type == GrabPinch) {
            pinchGestureId = grabGesture(PinchGestureRecognizer::Name);
        }
        if (type == GrabAllGestures || type == GrabSecondFinger) {
            secondFingerGestureId = grabGesture(SecondFingerGestureRecognizer::Name);
        }

        reset();
    }
    ~GestureWidget()
    {
        --numberOfWidgets;
        ungrabGestures();
    }

    void grabSingleshotGesture()
    {
        singleshotGestureId = grabGesture(SingleshotGestureRecognizer::Name);
    }
    void grabPinchGesture()
    {
        pinchGestureId = grabGesture(PinchGestureRecognizer::Name);
    }
    void grabSecondFingerGesture()
    {
        secondFingerGestureId = grabGesture(SecondFingerGestureRecognizer::Name);
    }
    void ungrabGestures()
    {
        releaseGesture(singleshotGestureId);
        singleshotGestureId = -1;
        releaseGesture(pinchGestureId);
        pinchGestureId = -1;
        releaseGesture(secondFingerGestureId);
        secondFingerGestureId = -1;
    }

    int singleshotGestureId;
    int pinchGestureId;
    int secondFingerGestureId;

    bool shouldAcceptSingleshotGesture;
    bool shouldAcceptPinchGesture;
    bool shouldAcceptSecondFingerGesture;

    int seenGestureEvent;
    struct LastGestureEvent
    {
        struct SingleshotGesture
        {
            bool delivered;
            QPoint offset;
        } singleshot;
        struct PinchGesture
        {
            bool delivered;
            TouchPoint startPoints[2];
            TouchPoint lastPoints[2];
            TouchPoint points[2];
            QPoint offset;
        } pinch;
        struct SecondFingerGesture
        {
            bool delivered;
            TouchPoint startPoint;
            TouchPoint lastPoint;
            TouchPoint point;
            QPoint offset;
        } secondfinger;
        QSet<QString> cancelled;
    } last;
    void reset()
    {
        seenGestureEvent = 0;
        last.singleshot.delivered = false;
        last.singleshot.offset = QPoint();
        last.pinch.delivered = false;
        last.pinch.startPoints[0] = TouchPoint();
        last.pinch.startPoints[1] = TouchPoint();
        last.pinch.lastPoints[0] = TouchPoint();
        last.pinch.lastPoints[1] = TouchPoint();
        last.pinch.points[0] = TouchPoint();
        last.pinch.points[1] = TouchPoint();
        last.pinch.offset = QPoint();
        last.secondfinger.delivered = false;
        last.secondfinger.startPoint = TouchPoint();
        last.secondfinger.lastPoint = TouchPoint();
        last.secondfinger.point = TouchPoint();
        last.secondfinger.offset = QPoint();
        last.cancelled.clear();

        shouldAcceptSingleshotGesture = true;
        shouldAcceptPinchGesture = true;
        shouldAcceptSecondFingerGesture = true;
    }

protected:
    bool event(QEvent *event)
    {
        if (event->type() == QEvent::Gesture) {
            QGestureEvent *e = static_cast<QGestureEvent*>(event);
            ++seenGestureEvent;
            if (SingleshotGesture *g = (SingleshotGesture*)e->gesture(SingleshotGestureRecognizer::Name)) {
                last.singleshot.delivered = true;
                last.singleshot.offset = g->offset;
                if (shouldAcceptSingleshotGesture)
                    g->accept();
            }
            if (PinchGesture *g = (PinchGesture*)e->gesture(PinchGestureRecognizer::Name)) {
                last.pinch.delivered = true;
                last.pinch.startPoints[0] = g->startPoints[0];
                last.pinch.startPoints[1] = g->startPoints[1];
                last.pinch.lastPoints[0] = g->lastPoints[0];
                last.pinch.lastPoints[1] = g->lastPoints[1];
                last.pinch.points[0] = g->points[0];
                last.pinch.points[1] = g->points[1];
                last.pinch.offset = g->offset;
                if (shouldAcceptPinchGesture)
                    g->accept();
            }
            if (SecondFingerGesture *g = (SecondFingerGesture*)e->gesture(SecondFingerGestureRecognizer::Name)) {
                last.secondfinger.delivered = true;
                last.secondfinger.startPoint = g->startPoint;
                last.secondfinger.lastPoint = g->lastPoint;
                last.secondfinger.point = g->point;
                last.secondfinger.offset = g->offset;
                if (shouldAcceptSecondFingerGesture)
                    g->accept();
            }
            last.cancelled = e->cancelledGestures();
            return true;
        }
        return QWidget::event(event);
    }
};
QVector<QColor> GestureWidget::colors;
int GestureWidget::numberOfWidgets = 0;

class tst_Gestures : public QObject
{
    Q_OBJECT

public:
    tst_Gestures();
    virtual ~tst_Gestures();

public slots:
    void initTestCase();
    void cleanupTestCase();
    void init();
    void cleanup();

private slots:
    void singleshotGesture();
    void pinchGesture();

    void simplePropagation();
    void simplePropagation2();
    void acceptedGesturePropagation();

private:
    SingleshotGestureRecognizer *singleshotRecognizer;
    PinchGestureRecognizer *pinchRecognizer;
    SecondFingerGestureRecognizer *secondFingerRecognizer;
    GestureWidget *mainWidget;

    bool sendSpontaneousEvent(QWidget *receiver, QEvent *event);
    void sendPinchEvents(QWidget *receiver, const QPoint &fromFinger1, const QPoint &fromFinger2);
};

tst_Gestures::tst_Gestures()
{
    singleshotRecognizer = new SingleshotGestureRecognizer;
    pinchRecognizer = new PinchGestureRecognizer;
    secondFingerRecognizer = new SecondFingerGestureRecognizer;
    qApp->addGestureRecognizer(singleshotRecognizer);
    qApp->addGestureRecognizer(pinchRecognizer);
    qApp->addGestureRecognizer(secondFingerRecognizer);
}

tst_Gestures::~tst_Gestures()
{
}


void tst_Gestures::initTestCase()
{
    mainWidget = new GestureWidget(GestureWidget::DoNotGrabGestures);
    mainWidget->setObjectName("MainGestureWidget");
    mainWidget->resize(500, 600);
    mainWidget->show();
}

void tst_Gestures::cleanupTestCase()
{
    delete mainWidget; mainWidget = 0;
}

void tst_Gestures::init()
{
    // TODO: Add initialization code here.
    // This will be executed immediately before each test is run.
    mainWidget->reset();
}

void tst_Gestures::cleanup()
{
}

bool tst_Gestures::sendSpontaneousEvent(QWidget *receiver, QEvent *event)
{
    QETWidget::setSpont(event, true);
    return qApp->notify(receiver, event);
}

void tst_Gestures::singleshotGesture()
{
    mainWidget->grabSingleshotGesture();
    SingleshotEvent event;
    sendSpontaneousEvent(mainWidget, &event);
    QVERIFY(mainWidget->seenGestureEvent);
    QVERIFY(mainWidget->last.singleshot.delivered);
    QVERIFY(mainWidget->last.cancelled.isEmpty());
}

void tst_Gestures::sendPinchEvents(QWidget *receiver, const QPoint &fromFinger1, const QPoint &fromFinger2)
{
    int x1 = fromFinger1.x();
    int y1 = fromFinger1.x();
    int x2 = fromFinger2.x();
    int y2 = fromFinger2.x();

    TouchEvent event;
    event.points[0] = TouchPoint(0,x1,y1, TouchPoint::Begin);
    event.points[1] = TouchPoint();
    sendSpontaneousEvent(receiver, &event);
    event.points[0] = TouchPoint(0, x1+=2,y1+=2, TouchPoint::Update);
    event.points[1] = TouchPoint();
    sendSpontaneousEvent(receiver, &event);
    event.points[0] = TouchPoint(0, x1,y1, TouchPoint::Update);
    event.points[1] = TouchPoint(1, x2,y2, TouchPoint::Begin);
    sendSpontaneousEvent(receiver, &event);
    event.points[0] = TouchPoint(0, x1+=5,y1+=10, TouchPoint::End);
    event.points[1] = TouchPoint(1, x2+=3,y2+=6, TouchPoint::Update);
    sendSpontaneousEvent(receiver, &event);
    event.points[0] = TouchPoint();
    event.points[1] = TouchPoint(1, x2+=10,y2+=15, TouchPoint::Update);
    sendSpontaneousEvent(receiver, &event);
    event.points[0] = TouchPoint();
    event.points[1] = TouchPoint(1, x2,y2, TouchPoint::End);
    sendSpontaneousEvent(receiver, &event);
}

void tst_Gestures::pinchGesture()
{
    mainWidget->grabPinchGesture();
    sendPinchEvents(mainWidget, QPoint(10,10), QPoint(20,20));

    QVERIFY(mainWidget->seenGestureEvent);
    QVERIFY(!mainWidget->last.singleshot.delivered);
    QVERIFY(mainWidget->last.cancelled.isEmpty());
    QVERIFY(mainWidget->last.pinch.delivered);
    QCOMPARE(mainWidget->last.pinch.startPoints[0].pt, QPoint(10,10));
    QCOMPARE(mainWidget->last.pinch.startPoints[1].pt, QPoint(20,20));
    QCOMPARE(mainWidget->last.pinch.offset, QPoint(0,0));
}

void tst_Gestures::simplePropagation()
{
    mainWidget->grabSingleshotGesture();
    GestureWidget offsetWidget(GestureWidget::DoNotGrabGestures);
    offsetWidget.setFixedSize(30, 30);
    mainWidget->layout()->addWidget(&offsetWidget);
    GestureWidget nonGestureWidget(GestureWidget::DoNotGrabGestures);
    mainWidget->layout()->addWidget(&nonGestureWidget);
    QApplication::processEvents();

    SingleshotEvent event;
    sendSpontaneousEvent(&nonGestureWidget, &event);
    QVERIFY(!offsetWidget.seenGestureEvent);
    QVERIFY(!nonGestureWidget.seenGestureEvent);
    QVERIFY(mainWidget->seenGestureEvent);
    QVERIFY(mainWidget->last.cancelled.isEmpty());
    QVERIFY(mainWidget->last.singleshot.delivered);
    QCOMPARE(mainWidget->last.singleshot.offset, QPoint(GestureWidget::LeftMargin, 30 + GestureWidget::TopMargin));
}

void tst_Gestures::simplePropagation2()
{
    mainWidget->grabSingleshotGesture();
    mainWidget->grabPinchGesture();
    GestureWidget nonGestureMiddleWidget(GestureWidget::DoNotGrabGestures);
    GestureWidget secondGestureWidget(GestureWidget::GrabPinch);
    nonGestureMiddleWidget.layout()->addWidget(&secondGestureWidget);
    mainWidget->layout()->addWidget(&nonGestureMiddleWidget);
    QApplication::processEvents();

    SingleshotEvent event;
    sendSpontaneousEvent(&secondGestureWidget, &event);
    QVERIFY(!secondGestureWidget.seenGestureEvent);
    QVERIFY(!nonGestureMiddleWidget.seenGestureEvent);
    QVERIFY(mainWidget->seenGestureEvent);
    QVERIFY(mainWidget->last.singleshot.delivered);
    QVERIFY(mainWidget->last.cancelled.isEmpty());
    QCOMPARE(mainWidget->last.singleshot.offset, QPoint(GestureWidget::LeftMargin*2,GestureWidget::TopMargin*2));

    mainWidget->reset();
    nonGestureMiddleWidget.reset();
    secondGestureWidget.reset();

    sendPinchEvents(&secondGestureWidget, QPoint(10,10), QPoint(20,20));
    QVERIFY(secondGestureWidget.seenGestureEvent);
    QVERIFY(!secondGestureWidget.last.singleshot.delivered);
    QVERIFY(secondGestureWidget.last.pinch.delivered);
    QCOMPARE(secondGestureWidget.last.pinch.startPoints[0].pt, QPoint(10,10));
    QCOMPARE(secondGestureWidget.last.pinch.startPoints[1].pt, QPoint(20,20));
    QCOMPARE(secondGestureWidget.last.pinch.offset, QPoint(0,0));
    QVERIFY(!nonGestureMiddleWidget.seenGestureEvent);
    QVERIFY(!mainWidget->seenGestureEvent);
}

void tst_Gestures::acceptedGesturePropagation()
{
    mainWidget->grabSingleshotGesture();
    mainWidget->grabPinchGesture();
    mainWidget->grabSecondFingerGesture();
    GestureWidget nonGestureMiddleWidget(GestureWidget::DoNotGrabGestures);
    nonGestureMiddleWidget.setObjectName("nonGestureMiddleWidget");
    GestureWidget secondGestureWidget(GestureWidget::GrabSecondFinger);
    secondGestureWidget.setObjectName("secondGestureWidget");
    nonGestureMiddleWidget.layout()->addWidget(&secondGestureWidget);
    mainWidget->layout()->addWidget(&nonGestureMiddleWidget);
    QApplication::processEvents();

    sendPinchEvents(&secondGestureWidget, QPoint(10, 10), QPoint(40, 40));
    QVERIFY(secondGestureWidget.seenGestureEvent);
    QVERIFY(!secondGestureWidget.last.singleshot.delivered);
    QVERIFY(!secondGestureWidget.last.pinch.delivered);
    QVERIFY(secondGestureWidget.last.secondfinger.delivered);
    QCOMPARE(secondGestureWidget.last.secondfinger.startPoint.pt, QPoint(40,40));
    QVERIFY(!nonGestureMiddleWidget.seenGestureEvent);
    QVERIFY(mainWidget->seenGestureEvent);
    QVERIFY(!mainWidget->last.singleshot.delivered);
    QVERIFY(!mainWidget->last.secondfinger.delivered);
    QVERIFY(mainWidget->last.pinch.delivered);
    QCOMPARE(mainWidget->last.pinch.startPoints[0].pt, QPoint(10,10));
    QCOMPARE(mainWidget->last.pinch.startPoints[1].pt, QPoint(40,40));

    mainWidget->reset();
    nonGestureMiddleWidget.reset();
    secondGestureWidget.reset();

    // don't accept it and make sure it propagates to parent
    secondGestureWidget.shouldAcceptSecondFingerGesture = false;
    sendPinchEvents(&secondGestureWidget, QPoint(10, 10), QPoint(40, 40));
    QVERIFY(secondGestureWidget.seenGestureEvent);
    QVERIFY(secondGestureWidget.last.secondfinger.delivered);
    QVERIFY(!nonGestureMiddleWidget.seenGestureEvent);
    QVERIFY(mainWidget->seenGestureEvent);
    QVERIFY(!mainWidget->last.singleshot.delivered);
    QVERIFY(mainWidget->last.secondfinger.delivered);
    QVERIFY(mainWidget->last.pinch.delivered);
}


QTEST_MAIN(tst_Gestures)
#include "tst_gestures.moc"