on bb09f86 help sign in
unity8/tests/qmltests/Components/tst_EdgeDragEvaluator.cpp
166 lines 5.0 kB history raw
1/*
2 * Copyright (C) 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 */
17#include <functional>
18#include <QtTest>
19#include <QObject>
20#include <QQuickView>
21#include <QQmlEngine>
22#include <QQuickItem>
24// Ubuntu.Gestures plugin
25#include <AxisVelocityCalculator.h>
26#include <Direction.h>
27#include <UbuntuGestures/private/timer_p.h>
29#include <paths.h>
31UG_USE_NAMESPACE
33class tst_EdgeDragEvaluator: public QObject
35 Q_OBJECT
36public:
37 tst_EdgeDragEvaluator() {}
38private Q_SLOTS:
39 void initTestCase(); // will be called before the first test function is executed
40 void cleanupTestCase(); // will be called after the last test function was executed.
42 void init(); // called right before each and every test function is executed
43 void cleanup(); // called right after each and every test function is executed
45 void horizontalDirection();
46 void horizontalDirection_data();
48private:
49 void drag(qreal &pos, qreal distance, qreal speed);
51 QQuickView *createView();
52 QQuickView *m_view;
53 FakeTimer *m_fakeTimer;
54 QSharedPointer<FakeTimeSource> m_fakeTimeSource;
55 AxisVelocityCalculator *m_dragEvaluator;
56};
59void tst_EdgeDragEvaluator::initTestCase()
61 m_view = 0;
64void tst_EdgeDragEvaluator::cleanupTestCase()
68void tst_EdgeDragEvaluator::init()
71 m_view = createView();
72 m_view->setSource(QUrl::fromLocalFile(testDataDir() + "/" TEST_DIR "/tst_EdgeDragEvaluator.qml"));
73 m_view->show();
74 QVERIFY(QTest::qWaitForWindowExposed(m_view));
75 QVERIFY(m_view->rootObject() != 0);
76 qApp->processEvents();
78 m_fakeTimeSource.reset(new FakeTimeSource);
79 m_fakeTimer = new FakeTimer(m_fakeTimeSource);
81 m_dragEvaluator =
82 m_view->rootObject()->findChild<AxisVelocityCalculator*>("edgeDragEvaluator");
83 QVERIFY(m_dragEvaluator);
84 m_dragEvaluator->setTimeSource(m_fakeTimeSource);
87void tst_EdgeDragEvaluator::cleanup()
89 m_dragEvaluator = nullptr;
91 delete m_view;
92 m_view = 0;
94 delete m_fakeTimer;
95 m_fakeTimer = 0;
97 m_fakeTimeSource.reset();
100QQuickView *tst_EdgeDragEvaluator::createView()
102 QQuickView *window = new QQuickView(0);
103 window->setResizeMode(QQuickView::SizeRootObjectToView);
104 window->engine()->addImportPath(testDataDir() + "/" TEST_DIR);
106 return window;
109void tst_EdgeDragEvaluator::drag(qreal &pos, qreal distance, qreal speed)
111 int numSteps = 10;
112 qreal distanceStep = distance / ((qreal)numSteps);
113 int timeStep = qAbs(distanceStep / speed);
115 for (int i = 0; i < numSteps; ++i) {
116 m_fakeTimeSource->m_msecsSinceReference += timeStep;
117 pos += distanceStep;
118 m_dragEvaluator->setTrackedPosition(pos);
119 }
122void tst_EdgeDragEvaluator::horizontalDirection()
124 qreal maxDragDistance = 100.;
125 qreal speedThreshold = 100.;
126 qreal speedThresholdMs = speedThreshold / 1000.;
127 QFETCH(qreal, dragDirection);
129 m_dragEvaluator->setProperty("maxDragDistance", QVariant::fromValue(maxDragDistance));
130 m_dragEvaluator->setProperty("dragThreshold", QVariant::fromValue(maxDragDistance * 0.5));
131 m_dragEvaluator->setProperty("speedThreshold", QVariant::fromValue(speedThreshold));
133 // If direction is "Horizontal" it means it can drag leftwards or rightwards
134 m_dragEvaluator->setProperty("direction", QVariant::fromValue((int)Direction::Horizontal));
136 qreal pos = 0.;
137 m_dragEvaluator->setTrackedPosition(pos);
139 // Go far beyond dragThreshold
140 drag(pos, maxDragDistance * 0.75 * dragDirection /*distance*/, speedThresholdMs * 0.5 /*speed*/);
142 // Then come back a bit, getting close to dragThreshold
143 drag(pos, -maxDragDistance * 0.20 * dragDirection /*distance*/, speedThresholdMs * 0.5 /*speed*/);
145 QVariant shouldAutoComplete;
146 bool ok = QMetaObject::invokeMethod(m_dragEvaluator, "shouldAutoComplete", Qt::DirectConnection,
147 Q_RETURN_ARG(QVariant, shouldAutoComplete));
148 QVERIFY(ok);
150 // It should not autocomplete because the current negative velocity (speedThresholdMs * 0.5)
151 // is bigger (in absolute terms) than the minimum negative velocity at this position needed to
152 // cancel autocompletion (speedThreshold * 0.1)
153 QCOMPARE(shouldAutoComplete.toBool(), false);
156void tst_EdgeDragEvaluator::horizontalDirection_data()
158 QTest::addColumn<qreal>("dragDirection");
160 QTest::newRow("right") << 1.;
161 QTest::newRow("left") << -1.;
164QTEST_MAIN(tst_EdgeDragEvaluator)
166#include "tst_EdgeDragEvaluator.moc"