| 1 |
/* |
| 2 |
* Copyright (C) 2014-2015 Canonical Ltd. |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; version 3. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU General Public License |
| 14 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <QtCore/QSet> |
| 18 |
#include <QtGui/QTouchEvent> |
| 19 |
#include <QtQuick/QQuickItem> |
| 20 |
#include <QtTest/QtTest> |
| 21 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 22 |
#include <private/qeventpoint_p.h> |
| 23 |
#endif |
| 24 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 25 |
#include <LomiriGestures-Qt6/private/timer_p.h> |
| 26 |
#else |
| 27 |
#include <LomiriGestures/private/timer_p.h> |
| 28 |
#endif |
| 29 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 30 |
#include <LomiriGestures-Qt6/private/touchownershipevent_p.h> |
| 31 |
#else |
| 32 |
#include <LomiriGestures/private/touchownershipevent_p.h> |
| 33 |
#endif |
| 34 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 35 |
#include <LomiriGestures-Qt6/private/touchregistry_p.h> |
| 36 |
#else |
| 37 |
#include <LomiriGestures/private/touchregistry_p.h> |
| 38 |
#endif |
| 39 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 40 |
#include <LomiriGestures-Qt6/private/unownedtouchevent_p.h> |
| 41 |
#else |
| 42 |
#include <LomiriGestures/private/unownedtouchevent_p.h> |
| 43 |
#endif |
| 44 |
|
| 45 |
namespace { |
| 46 |
// Helper class to create touch events, compatible with Qt5+Qt6 |
| 47 |
class TouchEventBuilder { |
| 48 |
public: |
| 49 |
TouchEventBuilder(QEvent::Type eventType = QEvent::TouchBegin) |
| 50 |
: m_eventType(eventType) {} |
| 51 |
|
| 52 |
TouchEventBuilder& addPoint(int id, Qt::TouchPointState state, QPointF pos = QPointF()) { |
| 53 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 54 |
m_touchPoints.append(QEventPoint(id, TPStateToEPState(state), pos, pos)); |
| 55 |
QMutableEventPoint::setPosition(m_touchPoints.last(), pos); |
| 56 |
#else |
| 57 |
int idx = m_touchPoints.count(); |
| 58 |
m_touchPoints.append(QTouchEvent::TouchPoint(id)); |
| 59 |
m_touchPoints[idx].setState(state); |
| 60 |
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) |
| 61 |
m_touchPoints[idx].setRect(QRect(pos.x(), pos.y(), 0, 0)); |
| 62 |
#else |
| 63 |
m_touchPoints[idx].setPos(pos); |
| 64 |
#endif |
| 65 |
#endif |
| 66 |
return *this; |
| 67 |
} |
| 68 |
|
| 69 |
void updateTouchRegistry(UG_PREPEND_NAMESPACE(TouchRegistry) *touchRegistry) { |
| 70 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 71 |
QTouchEvent touchEvent(m_eventType, nullptr, Qt::NoModifier, m_touchPoints); |
| 72 |
#else |
| 73 |
Qt::TouchPointStates states; |
| 74 |
for (const auto &tp : this->m_touchPoints) { |
| 75 |
states.setFlag(tp.state()); |
| 76 |
} |
| 77 |
QTouchEvent touchEvent(m_eventType, 0, Qt::NoModifier, states, m_touchPoints); |
| 78 |
#endif |
| 79 |
touchRegistry->update(&touchEvent); |
| 80 |
} |
| 81 |
|
| 82 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 83 |
static QEventPoint::State TPStateToEPState(Qt::TouchPointState state) { |
| 84 |
switch(state) { |
| 85 |
case Qt::TouchPointStationary: return QEventPoint::State::Stationary; |
| 86 |
case Qt::TouchPointPressed: return QEventPoint::State::Pressed; |
| 87 |
case Qt::TouchPointMoved: return QEventPoint::State::Updated; |
| 88 |
case Qt::TouchPointReleased: return QEventPoint::State::Released; |
| 89 |
default: return QEventPoint::State::Unknown; |
| 90 |
} |
| 91 |
} |
| 92 |
#endif |
| 93 |
|
| 94 |
private: |
| 95 |
QEvent::Type m_eventType; |
| 96 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 97 |
QList<QEventPoint> m_touchPoints; |
| 98 |
#else |
| 99 |
QList<QTouchEvent::TouchPoint> m_touchPoints; |
| 100 |
#endif |
| 101 |
}; |
| 102 |
} |
| 103 |
|
| 104 |
UG_NAMESPACE_BEGIN |
| 105 |
|
| 106 |
class TouchMemento { |
| 107 |
public: |
| 108 |
TouchMemento(const QTouchEvent *touchEvent); |
| 109 |
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) |
| 110 |
Qt::TouchPointStates touchPointStates; |
| 111 |
QList<QTouchEvent::TouchPoint> touchPoints; |
| 112 |
#else |
| 113 |
QEventPoint::States touchPointStates; |
| 114 |
QList<QEventPoint> touchPoints; |
| 115 |
#endif |
| 116 |
|
| 117 |
bool containsTouchWithId(int touchId) const; |
| 118 |
}; |
| 119 |
|
| 120 |
class DummyCandidate : public QQuickItem |
| 121 |
{ |
| 122 |
Q_OBJECT |
| 123 |
public: |
| 124 |
bool event(QEvent *e) override; |
| 125 |
QSet<int> ownedTouches; |
| 126 |
QSet<int> lostTouches; |
| 127 |
QList<TouchMemento> unownedTouchEvents; |
| 128 |
|
| 129 |
Q_SIGNALS: |
| 130 |
void gainedOwnership(); |
| 131 |
void lostOwnership(); |
| 132 |
}; |
| 133 |
|
| 134 |
class tst_TouchRegistry : public QObject |
| 135 |
{ |
| 136 |
Q_OBJECT |
| 137 |
public: |
| 138 |
tst_TouchRegistry() : QObject(0) { } |
| 139 |
private Q_SLOTS: |
| 140 |
void initTestCase() {} // will be called before the first test function is executed |
| 141 |
void cleanupTestCase() {} // will be called after the last test function was executed. |
| 142 |
|
| 143 |
void init(); // called right before each and every test function is executed |
| 144 |
void cleanup(); // called right after each and every test function is executed |
| 145 |
|
| 146 |
void requestWithNoCandidates(); |
| 147 |
void lateCandidateRequestGetsNothing(); |
| 148 |
void lateCandidateGestOwnershipOnceEarlyCandidateQuits(); |
| 149 |
void dispatchesTouchEventsToCandidates(); |
| 150 |
void dispatchesTouchEventsToWatchers(); |
| 151 |
void keepDispatchingToWatchersAfterLastCandidateGivesUp(); |
| 152 |
void candidatesAndWatchers_1(); |
| 153 |
void candidatesAndWatchers_2(); |
| 154 |
void rejectingTouchfterItsEnd(); |
| 155 |
void removeOldUndecidedCandidates(); |
| 156 |
void interimOwnerWontGetUnownedTouchEvents(); |
| 157 |
void candidateVanishes(); |
| 158 |
void candicateOwnershipReentrace(); |
| 159 |
void touchReleaseWithoutPressDoesNotCrash(); |
| 160 |
|
| 161 |
private: |
| 162 |
TouchRegistry *touchRegistry; |
| 163 |
}; |
| 164 |
|
| 165 |
void tst_TouchRegistry::init() |
| 166 |
{ |
| 167 |
touchRegistry = TouchRegistry::instance(); |
| 168 |
} |
| 169 |
|
| 170 |
void tst_TouchRegistry::cleanup() |
| 171 |
{ |
| 172 |
delete touchRegistry; |
| 173 |
} |
| 174 |
|
| 175 |
void tst_TouchRegistry::requestWithNoCandidates() |
| 176 |
{ |
| 177 |
DummyCandidate candidate; |
| 178 |
|
| 179 |
TouchEventBuilder() |
| 180 |
.addPoint(0, Qt::TouchPointPressed) |
| 181 |
.updateTouchRegistry(touchRegistry); |
| 182 |
|
| 183 |
touchRegistry->requestTouchOwnership(0, &candidate); |
| 184 |
|
| 185 |
QVERIFY(candidate.ownedTouches.contains(0)); |
| 186 |
} |
| 187 |
|
| 188 |
void tst_TouchRegistry::lateCandidateRequestGetsNothing() |
| 189 |
{ |
| 190 |
DummyCandidate earlyCandidate; |
| 191 |
earlyCandidate.setObjectName("early"); |
| 192 |
DummyCandidate lateCandidate; |
| 193 |
lateCandidate.setObjectName("late"); |
| 194 |
|
| 195 |
TouchEventBuilder() |
| 196 |
.addPoint(0, Qt::TouchPointPressed) |
| 197 |
.updateTouchRegistry(touchRegistry); |
| 198 |
|
| 199 |
touchRegistry->addCandidateOwnerForTouch(0, &earlyCandidate); |
| 200 |
touchRegistry->addCandidateOwnerForTouch(0, &lateCandidate); |
| 201 |
|
| 202 |
QVERIFY(earlyCandidate.ownedTouches.isEmpty()); |
| 203 |
QVERIFY(lateCandidate.ownedTouches.isEmpty()); |
| 204 |
|
| 205 |
touchRegistry->requestTouchOwnership(0, &lateCandidate); |
| 206 |
|
| 207 |
QVERIFY(earlyCandidate.ownedTouches.isEmpty()); |
| 208 |
QVERIFY(lateCandidate.ownedTouches.isEmpty()); |
| 209 |
|
| 210 |
touchRegistry->requestTouchOwnership(0, &earlyCandidate); |
| 211 |
|
| 212 |
QVERIFY(earlyCandidate.ownedTouches.contains(0)); |
| 213 |
QVERIFY(lateCandidate.ownedTouches.isEmpty()); |
| 214 |
} |
| 215 |
|
| 216 |
void tst_TouchRegistry::lateCandidateGestOwnershipOnceEarlyCandidateQuits() |
| 217 |
{ |
| 218 |
DummyCandidate earlyCandidate; |
| 219 |
DummyCandidate lateCandidate; |
| 220 |
|
| 221 |
TouchEventBuilder() |
| 222 |
.addPoint(0, Qt::TouchPointPressed) |
| 223 |
.updateTouchRegistry(touchRegistry); |
| 224 |
|
| 225 |
touchRegistry->addCandidateOwnerForTouch(0, &earlyCandidate); |
| 226 |
touchRegistry->addCandidateOwnerForTouch(0, &lateCandidate); |
| 227 |
|
| 228 |
QVERIFY(earlyCandidate.ownedTouches.isEmpty()); |
| 229 |
QVERIFY(lateCandidate.ownedTouches.isEmpty()); |
| 230 |
|
| 231 |
touchRegistry->requestTouchOwnership(0, &lateCandidate); |
| 232 |
|
| 233 |
QVERIFY(earlyCandidate.ownedTouches.isEmpty()); |
| 234 |
QVERIFY(lateCandidate.ownedTouches.isEmpty()); |
| 235 |
|
| 236 |
touchRegistry->removeCandidateOwnerForTouch(0, &earlyCandidate); |
| 237 |
|
| 238 |
QVERIFY(earlyCandidate.ownedTouches.isEmpty()); |
| 239 |
QVERIFY(lateCandidate.ownedTouches.contains(0)); |
| 240 |
} |
| 241 |
|
| 242 |
void tst_TouchRegistry::dispatchesTouchEventsToCandidates() |
| 243 |
{ |
| 244 |
QQuickItem rootItem; |
| 245 |
|
| 246 |
DummyCandidate candidate0; |
| 247 |
candidate0.setObjectName("0"); |
| 248 |
candidate0.setParentItem(&rootItem); |
| 249 |
candidate0.setX(1); |
| 250 |
candidate0.setY(2); |
| 251 |
|
| 252 |
DummyCandidate candidate1; |
| 253 |
candidate1.setObjectName("1"); |
| 254 |
candidate0.setParentItem(&rootItem); |
| 255 |
candidate1.setX(3); |
| 256 |
candidate1.setY(4); |
| 257 |
|
| 258 |
TouchEventBuilder() |
| 259 |
.addPoint(0, Qt::TouchPointPressed, QPointF(10, 10)) |
| 260 |
.updateTouchRegistry(touchRegistry); |
| 261 |
|
| 262 |
touchRegistry->addCandidateOwnerForTouch(0, &candidate0); |
| 263 |
|
| 264 |
TouchEventBuilder(QEvent::TouchUpdate) |
| 265 |
.addPoint(0, Qt::TouchPointMoved, QPointF(11, 11)) |
| 266 |
.addPoint(1, Qt::TouchPointPressed, QPointF(20, 20)) |
| 267 |
.updateTouchRegistry(touchRegistry); |
| 268 |
|
| 269 |
// candidate0 should have received an update on the touch he's interested on. |
| 270 |
QCOMPARE(candidate0.unownedTouchEvents.count(), 1); |
| 271 |
// make sure only touch 0 is there (i.e. no mention of touch 1) |
| 272 |
QCOMPARE(candidate0.unownedTouchEvents[0].touchPoints.count(), 1); |
| 273 |
QCOMPARE(candidate0.unownedTouchEvents[0].touchPoints[0].id(), 0); |
| 274 |
// Check that the points local coordinates have been mapped to candidate0's coordinate system. |
| 275 |
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) |
| 276 |
QCOMPARE(candidate0.unownedTouchEvents[0].touchPoints[0].rect().x(), qreal(11) - candidate0.x()); |
| 277 |
QCOMPARE(candidate0.unownedTouchEvents[0].touchPoints[0].rect().y(), qreal(11) - candidate0.y()); |
| 278 |
#elif QT_VERSION < QT_VERSION_CHECK(6, 0, 0) |
| 279 |
QCOMPARE(candidate0.unownedTouchEvents[0].touchPoints[0].pos().x(), qreal(11) - candidate0.x()); |
| 280 |
QCOMPARE(candidate0.unownedTouchEvents[0].touchPoints[0].pos().y(), qreal(11) - candidate0.y()); |
| 281 |
#else |
| 282 |
QCOMPARE(candidate0.unownedTouchEvents[0].touchPoints[0].position().x(), qreal(11) - candidate0.x()); |
| 283 |
QCOMPARE(candidate0.unownedTouchEvents[0].touchPoints[0].position().y(), qreal(11) - candidate0.y()); |
| 284 |
#endif |
| 285 |
|
| 286 |
touchRegistry->addCandidateOwnerForTouch(1, &candidate1); |
| 287 |
|
| 288 |
TouchEventBuilder(QEvent::TouchUpdate) |
| 289 |
.addPoint(0, Qt::TouchPointMoved, QPointF(12, 12)) |
| 290 |
.addPoint(1, Qt::TouchPointMoved, QPointF(21, 21)) |
| 291 |
.updateTouchRegistry(touchRegistry); |
| 292 |
|
| 293 |
// candidate0 gets updates only for touch 0 and |
| 294 |
// candidate1 gets updates only for touch 1 |
| 295 |
|
| 296 |
QCOMPARE(candidate0.unownedTouchEvents.count(), 2); |
| 297 |
QCOMPARE(candidate0.unownedTouchEvents[1].touchPoints.count(), 1); |
| 298 |
QCOMPARE(candidate0.unownedTouchEvents[1].touchPoints[0].id(), 0); |
| 299 |
|
| 300 |
QCOMPARE(candidate1.unownedTouchEvents.count(), 1); |
| 301 |
QCOMPARE(candidate1.unownedTouchEvents[0].touchPoints.count(), 1); |
| 302 |
QCOMPARE(candidate1.unownedTouchEvents[0].touchPoints[0].id(), 1); |
| 303 |
} |
| 304 |
|
| 305 |
void tst_TouchRegistry::dispatchesTouchEventsToWatchers() |
| 306 |
{ |
| 307 |
QQuickItem rootItem; |
| 308 |
|
| 309 |
DummyCandidate watcher; |
| 310 |
watcher.setObjectName("watcher"); |
| 311 |
watcher.setParentItem(&rootItem); |
| 312 |
watcher.setX(1); |
| 313 |
watcher.setY(2); |
| 314 |
|
| 315 |
TouchEventBuilder() |
| 316 |
.addPoint(0, Qt::TouchPointPressed, QPointF(10, 10)) |
| 317 |
.updateTouchRegistry(touchRegistry); |
| 318 |
|
| 319 |
touchRegistry->addTouchWatcher(0, &watcher); |
| 320 |
|
| 321 |
TouchEventBuilder(QEvent::TouchUpdate) |
| 322 |
.addPoint(0, Qt::TouchPointMoved, QPointF(11, 11)) |
| 323 |
.addPoint(1, Qt::TouchPointPressed, QPointF(20, 20)) |
| 324 |
.updateTouchRegistry(touchRegistry); |
| 325 |
|
| 326 |
// watcher should have received an update on the touch he's interested on. |
| 327 |
QCOMPARE(watcher.unownedTouchEvents.count(), 1); |
| 328 |
// make sure only touch 0 is there (i.e. no mention of touch 1) |
| 329 |
QCOMPARE(watcher.unownedTouchEvents[0].touchPoints.count(), 1); |
| 330 |
QCOMPARE(watcher.unownedTouchEvents[0].touchPoints[0].id(), 0); |
| 331 |
// Check that the points local coordinates have been mapped to watcher's coordinate system. |
| 332 |
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) |
| 333 |
QCOMPARE(watcher.unownedTouchEvents[0].touchPoints[0].rect().x(), qreal(11) - watcher.x()); |
| 334 |
QCOMPARE(watcher.unownedTouchEvents[0].touchPoints[0].rect().y(), qreal(11) - watcher.y()); |
| 335 |
#elif QT_VERSION < QT_VERSION_CHECK(6, 0, 0) |
| 336 |
QCOMPARE(watcher.unownedTouchEvents[0].touchPoints[0].pos().x(), qreal(11) - watcher.x()); |
| 337 |
QCOMPARE(watcher.unownedTouchEvents[0].touchPoints[0].pos().y(), qreal(11) - watcher.y()); |
| 338 |
#else |
| 339 |
QCOMPARE(watcher.unownedTouchEvents[0].touchPoints[0].position().x(), qreal(11) - watcher.x()); |
| 340 |
QCOMPARE(watcher.unownedTouchEvents[0].touchPoints[0].position().y(), qreal(11) - watcher.y()); |
| 341 |
#endif |
| 342 |
|
| 343 |
TouchEventBuilder(QEvent::TouchUpdate) |
| 344 |
.addPoint(0, Qt::TouchPointMoved, QPointF(12, 12)) |
| 345 |
.addPoint(1, Qt::TouchPointMoved, QPointF(21, 21)) |
| 346 |
.updateTouchRegistry(touchRegistry); |
| 347 |
|
| 348 |
// watcher gets updates only for touch 0 |
| 349 |
|
| 350 |
QCOMPARE(watcher.unownedTouchEvents.count(), 2); |
| 351 |
QCOMPARE(watcher.unownedTouchEvents[1].touchPoints.count(), 1); |
| 352 |
QCOMPARE(watcher.unownedTouchEvents[1].touchPoints[0].id(), 0); |
| 353 |
} |
| 354 |
|
| 355 |
void tst_TouchRegistry::keepDispatchingToWatchersAfterLastCandidateGivesUp() |
| 356 |
{ |
| 357 |
DummyCandidate item; |
| 358 |
|
| 359 |
TouchEventBuilder() |
| 360 |
.addPoint(0, Qt::TouchPointPressed) |
| 361 |
.updateTouchRegistry(touchRegistry); |
| 362 |
|
| 363 |
touchRegistry->addCandidateOwnerForTouch(0, &item); |
| 364 |
|
| 365 |
TouchEventBuilder(QEvent::TouchUpdate) |
| 366 |
.addPoint(0, Qt::TouchPointMoved) |
| 367 |
.addPoint(1, Qt::TouchPointPressed) |
| 368 |
.updateTouchRegistry(touchRegistry); |
| 369 |
|
| 370 |
QCOMPARE(item.unownedTouchEvents.count(), 1); |
| 371 |
QCOMPARE(item.unownedTouchEvents[0].touchPoints.count(), 1); |
| 372 |
QCOMPARE(item.unownedTouchEvents[0].touchPoints[0].id(), 0); |
| 373 |
item.unownedTouchEvents.clear(); |
| 374 |
|
| 375 |
touchRegistry->addTouchWatcher(1, &item); |
| 376 |
|
| 377 |
TouchEventBuilder(QEvent::TouchUpdate) |
| 378 |
.addPoint(0, Qt::TouchPointMoved) |
| 379 |
.addPoint(1, Qt::TouchPointMoved) |
| 380 |
.updateTouchRegistry(touchRegistry); |
| 381 |
|
| 382 |
QCOMPARE(item.unownedTouchEvents.count(), 1); |
| 383 |
QCOMPARE(item.unownedTouchEvents[0].touchPoints.count(), 2); |
| 384 |
QVERIFY(item.unownedTouchEvents[0].containsTouchWithId(0)); |
| 385 |
QVERIFY(item.unownedTouchEvents[0].containsTouchWithId(1)); |
| 386 |
item.unownedTouchEvents.clear(); |
| 387 |
|
| 388 |
TouchEventBuilder(QEvent::TouchUpdate) |
| 389 |
.addPoint(0, Qt::TouchPointReleased) |
| 390 |
.addPoint(1, Qt::TouchPointMoved) |
| 391 |
.updateTouchRegistry(touchRegistry); |
| 392 |
|
| 393 |
QCOMPARE(item.unownedTouchEvents.count(), 1); |
| 394 |
QCOMPARE(item.unownedTouchEvents[0].touchPoints.count(), 2); |
| 395 |
QVERIFY(item.unownedTouchEvents[0].containsTouchWithId(0)); |
| 396 |
QVERIFY(item.unownedTouchEvents[0].containsTouchWithId(1)); |
| 397 |
item.unownedTouchEvents.clear(); |
| 398 |
|
| 399 |
touchRegistry->removeCandidateOwnerForTouch(0, &item); |
| 400 |
|
| 401 |
TouchEventBuilder(QEvent::TouchEnd) |
| 402 |
.addPoint(1, Qt::TouchPointReleased) |
| 403 |
.updateTouchRegistry(touchRegistry); |
| 404 |
|
| 405 |
QCOMPARE(item.unownedTouchEvents.count(), 1); |
| 406 |
QCOMPARE(item.unownedTouchEvents[0].touchPoints.count(), 1); |
| 407 |
QVERIFY(item.unownedTouchEvents[0].containsTouchWithId(1)); |
| 408 |
item.unownedTouchEvents.clear(); |
| 409 |
|
| 410 |
QVERIFY(touchRegistry->m_touchInfoPool.isEmpty()); |
| 411 |
} |
| 412 |
|
| 413 |
/* |
| 414 |
Regression test that reproduces a problematic scenario that came up during manual testing. |
| 415 |
It reproduces the interaction between TouchRegistry and a DirectionalDragArea |
| 416 |
*/ |
| 417 |
void tst_TouchRegistry::candidatesAndWatchers_1() |
| 418 |
{ |
| 419 |
DummyCandidate item; |
| 420 |
|
| 421 |
TouchEventBuilder() |
| 422 |
.addPoint(0, Qt::TouchPointPressed) |
| 423 |
.updateTouchRegistry(touchRegistry); |
| 424 |
|
| 425 |
touchRegistry->addCandidateOwnerForTouch(0, &item); |
| 426 |
|
| 427 |
TouchEventBuilder(QEvent::TouchUpdate) |
| 428 |
.addPoint(0, Qt::TouchPointMoved) |
| 429 |
.addPoint(1, Qt::TouchPointPressed) |
| 430 |
.updateTouchRegistry(touchRegistry); |
| 431 |
|
| 432 |
touchRegistry->addTouchWatcher(1, &item); |
| 433 |
|
| 434 |
touchRegistry->removeCandidateOwnerForTouch(0, &item); |
| 435 |
|
| 436 |
touchRegistry->addTouchWatcher(0, &item); |
| 437 |
|
| 438 |
TouchEventBuilder(QEvent::TouchUpdate) |
| 439 |
.addPoint(0, Qt::TouchPointReleased) |
| 440 |
.addPoint(1, Qt::TouchPointMoved) |
| 441 |
.updateTouchRegistry(touchRegistry); |
| 442 |
|
| 443 |
TouchEventBuilder(QEvent::TouchEnd) |
| 444 |
.addPoint(1, Qt::TouchPointReleased) |
| 445 |
.updateTouchRegistry(touchRegistry); |
| 446 |
|
| 447 |
QVERIFY(touchRegistry->m_touchInfoPool.isEmpty()); |
| 448 |
|
| 449 |
item.unownedTouchEvents.clear(); |
| 450 |
|
| 451 |
TouchEventBuilder() |
| 452 |
.addPoint(0, Qt::TouchPointPressed) |
| 453 |
.updateTouchRegistry(touchRegistry); |
| 454 |
|
| 455 |
// Haven't made any subscription for that new touch 0. |
| 456 |
QCOMPARE(item.unownedTouchEvents.count(), 0); |
| 457 |
} |
| 458 |
|
| 459 |
/* |
| 460 |
Regression test that reproduces a problematic scenario that came up during manual testing. |
| 461 |
It reproduces the interaction between TouchRegistry, DirectionalDragArea and a TouchGate. |
| 462 |
*/ |
| 463 |
void tst_TouchRegistry::candidatesAndWatchers_2() |
| 464 |
{ |
| 465 |
DummyCandidate directionalDragArea; |
| 466 |
directionalDragArea.setObjectName("DirectionalDragArea"); |
| 467 |
|
| 468 |
DummyCandidate touchGate; |
| 469 |
touchGate.setObjectName("TouchGate"); |
| 470 |
|
| 471 |
// [DDA] 1298 TouchBegin (id:0, state:pressed, scenePos:(147,1023)) |
| 472 |
TouchEventBuilder() |
| 473 |
.addPoint(0, Qt::TouchPointPressed) |
| 474 |
.updateTouchRegistry(touchRegistry); |
| 475 |
|
| 476 |
// [TouchRegistry] addCandidateOwnerForTouch id 0 candidate DirectionalDragArea |
| 477 |
touchRegistry->addCandidateOwnerForTouch(0, &directionalDragArea); |
| 478 |
|
| 479 |
// [TouchRegistry] requestTouchOwnership id 0 candidate TouchGate |
| 480 |
touchRegistry->requestTouchOwnership(0, &touchGate); |
| 481 |
|
| 482 |
//[TouchRegistry] got TouchUpdate (id:0, state:moved, scenePos:(147,1023)) (id:1, state:pressed, scenePos:(327,792)) |
| 483 |
TouchEventBuilder(QEvent::TouchUpdate) |
| 484 |
.addPoint(0, Qt::TouchPointMoved) |
| 485 |
.addPoint(1, Qt::TouchPointPressed) |
| 486 |
.updateTouchRegistry(touchRegistry); |
| 487 |
|
| 488 |
// [TouchRegistry] addTouchWatcher id 1 watcher DirectionalDragArea |
| 489 |
touchRegistry->addTouchWatcher(1, &directionalDragArea); |
| 490 |
|
| 491 |
// [TouchRegistry] removeCandidateOwnerForTouch id 0 candidate DirectionalDragArea |
| 492 |
touchRegistry->removeCandidateOwnerForTouch(0, &directionalDragArea); |
| 493 |
|
| 494 |
//[TouchRegistry] sending TouchWonershipEvent(id = 0 gained) to candidate TouchGate |
| 495 |
QCOMPARE(touchGate.ownedTouches.count(), 1); |
| 496 |
QVERIFY(touchGate.ownedTouches.contains(0)); |
| 497 |
|
| 498 |
// [TouchRegistry] addTouchWatcher id 0 watcher DirectionalDragArea |
| 499 |
touchRegistry->addTouchWatcher(0, &directionalDragArea); |
| 500 |
|
| 501 |
// [TouchRegistry] requestTouchOwnership id 1 candidate TouchGate |
| 502 |
touchRegistry->requestTouchOwnership(1, &touchGate); |
| 503 |
|
| 504 |
//[TouchRegistry] sending TouchWonershipEvent(id = 1 gained) to candidate TouchGate |
| 505 |
QCOMPARE(touchGate.ownedTouches.count(), 2); |
| 506 |
QVERIFY(touchGate.ownedTouches.contains(1)); |
| 507 |
|
| 508 |
directionalDragArea.unownedTouchEvents.clear(); |
| 509 |
touchGate.unownedTouchEvents.clear(); |
| 510 |
|
| 511 |
//[TouchRegistry] got TouchUpdate (id:0, state:moved, scenePos:(148,1025)) (id:1, state:moved, scenePos:(329,795)) |
| 512 |
TouchEventBuilder(QEvent::TouchUpdate) |
| 513 |
.addPoint(0, Qt::TouchPointMoved) |
| 514 |
.addPoint(1, Qt::TouchPointMoved) |
| 515 |
.updateTouchRegistry(touchRegistry); |
| 516 |
|
| 517 |
//vvvvv DDA Watchers are being ignored from now on vvvvvvv |
| 518 |
|
| 519 |
QCOMPARE(directionalDragArea.unownedTouchEvents.count(), 1); |
| 520 |
QCOMPARE(directionalDragArea.unownedTouchEvents.first().touchPoints.count(), 2); |
| 521 |
QVERIFY(directionalDragArea.unownedTouchEvents.first().containsTouchWithId(0)); |
| 522 |
QVERIFY(directionalDragArea.unownedTouchEvents.first().containsTouchWithId(1)); |
| 523 |
|
| 524 |
QVERIFY(touchGate.unownedTouchEvents.isEmpty()); |
| 525 |
|
| 526 |
directionalDragArea.unownedTouchEvents.clear(); |
| 527 |
|
| 528 |
//[TouchRegistry] got TouchUpdate (id:0, state:moved, scenePos:(151,1029)) (id:1, state:released, scenePos:(329,795)) |
| 529 |
TouchEventBuilder(QEvent::TouchUpdate) |
| 530 |
.addPoint(0, Qt::TouchPointMoved) |
| 531 |
.addPoint(1, Qt::TouchPointReleased) |
| 532 |
.updateTouchRegistry(touchRegistry); |
| 533 |
|
| 534 |
QCOMPARE(directionalDragArea.unownedTouchEvents.count(), 1); |
| 535 |
QCOMPARE(directionalDragArea.unownedTouchEvents.first().touchPoints.count(), 2); |
| 536 |
QVERIFY(directionalDragArea.unownedTouchEvents.first().containsTouchWithId(0)); |
| 537 |
QVERIFY(directionalDragArea.unownedTouchEvents.first().containsTouchWithId(1)); |
| 538 |
|
| 539 |
QVERIFY(touchGate.unownedTouchEvents.isEmpty()); |
| 540 |
|
| 541 |
directionalDragArea.unownedTouchEvents.clear(); |
| 542 |
|
| 543 |
//[TouchRegistry] got TouchEnd (id:0, state:released, scenePos:(157,1034)) |
| 544 |
TouchEventBuilder(QEvent::TouchEnd) |
| 545 |
.addPoint(0, Qt::TouchPointReleased) |
| 546 |
.updateTouchRegistry(touchRegistry); |
| 547 |
|
| 548 |
QCOMPARE(directionalDragArea.unownedTouchEvents.count(), 1); |
| 549 |
QCOMPARE(directionalDragArea.unownedTouchEvents.first().touchPoints.count(), 1); |
| 550 |
QVERIFY(directionalDragArea.unownedTouchEvents.first().containsTouchWithId(0)); |
| 551 |
|
| 552 |
QVERIFY(touchGate.unownedTouchEvents.isEmpty()); |
| 553 |
} |
| 554 |
|
| 555 |
void tst_TouchRegistry::rejectingTouchfterItsEnd() |
| 556 |
{ |
| 557 |
DummyCandidate earlyCandidate; |
| 558 |
DummyCandidate lateCandidate; |
| 559 |
|
| 560 |
TouchEventBuilder() |
| 561 |
.addPoint(0, Qt::TouchPointPressed) |
| 562 |
.updateTouchRegistry(touchRegistry); |
| 563 |
|
| 564 |
touchRegistry->addCandidateOwnerForTouch(0, &earlyCandidate); |
| 565 |
touchRegistry->addCandidateOwnerForTouch(0, &lateCandidate); |
| 566 |
|
| 567 |
QVERIFY(earlyCandidate.ownedTouches.isEmpty()); |
| 568 |
QVERIFY(lateCandidate.ownedTouches.isEmpty()); |
| 569 |
|
| 570 |
touchRegistry->requestTouchOwnership(0, &lateCandidate); |
| 571 |
|
| 572 |
QVERIFY(earlyCandidate.ownedTouches.isEmpty()); |
| 573 |
QVERIFY(lateCandidate.ownedTouches.isEmpty()); |
| 574 |
|
| 575 |
TouchEventBuilder(QEvent::TouchEnd) |
| 576 |
.addPoint(0, Qt::TouchPointReleased) |
| 577 |
.updateTouchRegistry(touchRegistry); |
| 578 |
|
| 579 |
QVERIFY(earlyCandidate.ownedTouches.isEmpty()); |
| 580 |
QVERIFY(lateCandidate.ownedTouches.isEmpty()); |
| 581 |
|
| 582 |
touchRegistry->removeCandidateOwnerForTouch(0, &earlyCandidate); |
| 583 |
|
| 584 |
QCOMPARE(lateCandidate.ownedTouches.count(), 1); |
| 585 |
QCOMPARE(lateCandidate.ownedTouches.contains(0), true); |
| 586 |
|
| 587 |
// Check that there's no trace left of touch 0 as we no longer need to keep tabs on it. |
| 588 |
QVERIFY(!touchRegistry->findTouchInfo(0)); |
| 589 |
} |
| 590 |
|
| 591 |
void tst_TouchRegistry::removeOldUndecidedCandidates() |
| 592 |
{ |
| 593 |
FakeTimerFactory *fakeTimerFactory = new FakeTimerFactory; |
| 594 |
touchRegistry->setTimerFactory(fakeTimerFactory); |
| 595 |
|
| 596 |
DummyCandidate undecidedCandidate; |
| 597 |
undecidedCandidate.setObjectName("undecided"); |
| 598 |
|
| 599 |
DummyCandidate candidateThatWantsTouch; |
| 600 |
candidateThatWantsTouch.setObjectName("wantsTouch"); |
| 601 |
|
| 602 |
TouchEventBuilder() |
| 603 |
.addPoint(0, Qt::TouchPointPressed) |
| 604 |
.updateTouchRegistry(touchRegistry); |
| 605 |
|
| 606 |
touchRegistry->addCandidateOwnerForTouch(0, &undecidedCandidate); |
| 607 |
|
| 608 |
touchRegistry->requestTouchOwnership(0, &candidateThatWantsTouch); |
| 609 |
|
| 610 |
QVERIFY(undecidedCandidate.ownedTouches.isEmpty()); |
| 611 |
QVERIFY(undecidedCandidate.lostTouches.isEmpty()); |
| 612 |
QVERIFY(candidateThatWantsTouch.ownedTouches.isEmpty()); |
| 613 |
QVERIFY(candidateThatWantsTouch.lostTouches.isEmpty()); |
| 614 |
|
| 615 |
// Simulate that enough time has passed to cause the CandidateInactivityTimer to timeout, |
| 616 |
// making TouchRegistry consider that undecidedCantidate defaulted. |
| 617 |
fakeTimerFactory->updateTime(10000); |
| 618 |
|
| 619 |
QVERIFY(undecidedCandidate.ownedTouches.isEmpty()); |
| 620 |
QVERIFY(undecidedCandidate.lostTouches.contains(0)); |
| 621 |
QVERIFY(candidateThatWantsTouch.ownedTouches.contains(0)); |
| 622 |
QVERIFY(candidateThatWantsTouch.lostTouches.isEmpty()); |
| 623 |
} |
| 624 |
|
| 625 |
/* |
| 626 |
An item that calls requestTouchOwnership() without first having called addCandidateOwnerForTouch() |
| 627 |
is assumed to be the interim owner of the touch point, thus there's no point in sending |
| 628 |
UnownedTouchEvents to him as he already gets proper QTouchEvents from QQuickWindow because |
| 629 |
he didn't ignore the first QTouchEvent with that touch point. |
| 630 |
*/ |
| 631 |
void tst_TouchRegistry::interimOwnerWontGetUnownedTouchEvents() |
| 632 |
{ |
| 633 |
FakeTimerFactory *fakeTimerFactory = new FakeTimerFactory; |
| 634 |
touchRegistry->setTimerFactory(fakeTimerFactory); |
| 635 |
|
| 636 |
DummyCandidate undecidedCandidate; |
| 637 |
undecidedCandidate.setObjectName("undecided"); |
| 638 |
|
| 639 |
DummyCandidate interimOwner; |
| 640 |
interimOwner.setObjectName("interimOwner"); |
| 641 |
|
| 642 |
TouchEventBuilder() |
| 643 |
.addPoint(0, Qt::TouchPointPressed) |
| 644 |
.updateTouchRegistry(touchRegistry); |
| 645 |
|
| 646 |
touchRegistry->addCandidateOwnerForTouch(0, &undecidedCandidate); |
| 647 |
touchRegistry->requestTouchOwnership(0, &interimOwner); |
| 648 |
|
| 649 |
TouchEventBuilder(QEvent::TouchUpdate) |
| 650 |
.addPoint(0, Qt::TouchPointMoved) |
| 651 |
.updateTouchRegistry(touchRegistry); |
| 652 |
|
| 653 |
QCOMPARE(undecidedCandidate.unownedTouchEvents.count(), 1); |
| 654 |
QCOMPARE(interimOwner.unownedTouchEvents.count(), 0); |
| 655 |
|
| 656 |
TouchEventBuilder(QEvent::TouchUpdate) |
| 657 |
.addPoint(0, Qt::TouchPointMoved) |
| 658 |
.updateTouchRegistry(touchRegistry); |
| 659 |
|
| 660 |
QCOMPARE(undecidedCandidate.unownedTouchEvents.count(), 2); |
| 661 |
QCOMPARE(interimOwner.unownedTouchEvents.count(), 0); |
| 662 |
} |
| 663 |
|
| 664 |
/* |
| 665 |
Regression test for https://bugs.launchpad.net/lomiri/+source/unity8/+bug/1473492 |
| 666 |
|
| 667 |
Bug caused by candidates getting deleted before ownership resolution |
| 668 |
*/ |
| 669 |
void tst_TouchRegistry::candidateVanishes() |
| 670 |
{ |
| 671 |
DummyCandidate mainCandidate; |
| 672 |
DummyCandidate *missingCandidate = new DummyCandidate; |
| 673 |
|
| 674 |
TouchEventBuilder() |
| 675 |
.addPoint(0, Qt::TouchPointPressed) |
| 676 |
.updateTouchRegistry(touchRegistry); |
| 677 |
|
| 678 |
touchRegistry->addCandidateOwnerForTouch(0, &mainCandidate); |
| 679 |
touchRegistry->addCandidateOwnerForTouch(0, missingCandidate); |
| 680 |
|
| 681 |
TouchEventBuilder(QEvent::TouchUpdate) |
| 682 |
.addPoint(0, Qt::TouchPointMoved) |
| 683 |
.updateTouchRegistry(touchRegistry); |
| 684 |
|
| 685 |
// This candidate suddenly disappears without having removed its candidacy. |
| 686 |
delete missingCandidate; |
| 687 |
missingCandidate = nullptr; |
| 688 |
|
| 689 |
touchRegistry->requestTouchOwnership(0, &mainCandidate); |
| 690 |
|
| 691 |
QVERIFY(mainCandidate.ownedTouches.contains(0)); |
| 692 |
} |
| 693 |
|
| 694 |
/* |
| 695 |
Regression test for canidate reentrance |
| 696 |
|
| 697 |
Bug caused by candidates getting removed during ownership resolution |
| 698 |
*/ |
| 699 |
void tst_TouchRegistry::candicateOwnershipReentrace() |
| 700 |
{ |
| 701 |
DummyCandidate mainCandidate; |
| 702 |
DummyCandidate candicate2; |
| 703 |
DummyCandidate candicate3; |
| 704 |
|
| 705 |
TouchEventBuilder() |
| 706 |
.addPoint(0, Qt::TouchPointPressed) |
| 707 |
.updateTouchRegistry(touchRegistry); |
| 708 |
|
| 709 |
// Re-entrance! |
| 710 |
connect(&candicate2, &DummyCandidate::lostOwnership, this, [&]() { |
| 711 |
touchRegistry->removeCandidateOwnerForTouch(0, &candicate2); |
| 712 |
}); |
| 713 |
|
| 714 |
touchRegistry->addCandidateOwnerForTouch(0, &mainCandidate); |
| 715 |
touchRegistry->addCandidateOwnerForTouch(0, &candicate2); |
| 716 |
touchRegistry->addCandidateOwnerForTouch(0, &candicate3); |
| 717 |
|
| 718 |
TouchEventBuilder(QEvent::TouchUpdate) |
| 719 |
.addPoint(0, Qt::TouchPointMoved) |
| 720 |
.updateTouchRegistry(touchRegistry); |
| 721 |
|
| 722 |
touchRegistry->requestTouchOwnership(0, &mainCandidate); |
| 723 |
|
| 724 |
QCOMPARE(mainCandidate.ownedTouches.count(), 1); |
| 725 |
QCOMPARE(candicate2.lostTouches.count(), 1); |
| 726 |
QCOMPARE(candicate3.lostTouches.count(), 1); |
| 727 |
} |
| 728 |
|
| 729 |
void tst_TouchRegistry::touchReleaseWithoutPressDoesNotCrash() { |
| 730 |
TouchEventBuilder(QEvent::TouchEnd) |
| 731 |
.addPoint(0, Qt::TouchPointReleased) |
| 732 |
.updateTouchRegistry(touchRegistry); |
| 733 |
} |
| 734 |
|
| 735 |
////////////// TouchMemento ////////// |
| 736 |
|
| 737 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 738 |
TouchMemento::TouchMemento(const QTouchEvent *touchEvent) |
| 739 |
: touchPointStates(touchEvent->touchPointStates()), touchPoints(touchEvent->points()) |
| 740 |
{ |
| 741 |
} |
| 742 |
#else |
| 743 |
TouchMemento::TouchMemento(const QTouchEvent *touchEvent) |
| 744 |
: touchPointStates(touchEvent->touchPointStates()), touchPoints(touchEvent->touchPoints()) |
| 745 |
{ |
| 746 |
} |
| 747 |
#endif |
| 748 |
|
| 749 |
bool TouchMemento::containsTouchWithId(int touchId) const |
| 750 |
{ |
| 751 |
for (int i = 0; i < touchPoints.count(); ++i) { |
| 752 |
if (touchPoints.at(i).id() == touchId) { |
| 753 |
return true; |
| 754 |
} |
| 755 |
} |
| 756 |
return false; |
| 757 |
} |
| 758 |
|
| 759 |
////////////// DummyCandidate ////////// |
| 760 |
|
| 761 |
bool DummyCandidate::event(QEvent *e) |
| 762 |
{ |
| 763 |
if (e->type() == TouchOwnershipEvent::touchOwnershipEventType()) { |
| 764 |
TouchOwnershipEvent *touchOwnershipEvent = static_cast<TouchOwnershipEvent *>(e); |
| 765 |
|
| 766 |
// NB: Cannot use QVERIFY here because the macro doesn't return a boolean and is |
| 767 |
// meant for use only directly in the body of a test function |
| 768 |
if (ownedTouches.contains(touchOwnershipEvent->touchId())) |
| 769 |
qFatal("Sent ownership event for a touch that is already owned."); |
| 770 |
if (lostTouches.contains(touchOwnershipEvent->touchId())) |
| 771 |
qFatal("Sent ownership event for a touch that has already been lost."); |
| 772 |
|
| 773 |
if (touchOwnershipEvent->gained()) { |
| 774 |
ownedTouches.insert(touchOwnershipEvent->touchId()); |
| 775 |
Q_EMIT gainedOwnership(); |
| 776 |
} else { |
| 777 |
lostTouches.insert(touchOwnershipEvent->touchId()); |
| 778 |
Q_EMIT lostOwnership(); |
| 779 |
} |
| 780 |
return true; |
| 781 |
} else if (e->type() == UnownedTouchEvent::unownedTouchEventType()) { |
| 782 |
UnownedTouchEvent *unownedTouchEvent = static_cast<UnownedTouchEvent *>(e); |
| 783 |
unownedTouchEvents.append(TouchMemento(unownedTouchEvent->touchEvent())); |
| 784 |
return true; |
| 785 |
} else { |
| 786 |
return QObject::event(e); |
| 787 |
} |
| 788 |
} |
| 789 |
|
| 790 |
UG_NAMESPACE_END |
| 791 |
|
| 792 |
QTEST_GUILESS_MAIN(UG_PREPEND_NAMESPACE(tst_TouchRegistry)) |
| 793 |
|
| 794 |
#include "tst_TouchRegistry.moc" |