on bbbdccd help sign in
unity8/tests/qmltests/Components/tst_EdgeDragEvaluator.cpp
164 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 <Timer.h>
29using namespace UbuntuGestures;
31class tst_EdgeDragEvaluator: public QObject
33 Q_OBJECT
34public:
35 tst_EdgeDragEvaluator() {}
36private Q_SLOTS:
37 void initTestCase(); // will be called before the first test function is executed
38 void cleanupTestCase(); // will be called after the last test function was executed.
40 void init(); // called right before each and every test function is executed
41 void cleanup(); // called right after each and every test function is executed
43 void horizontalDirection();
44 void horizontalDirection_data();
46private:
47 void drag(qreal &pos, qreal distance, qreal speed);
49 QQuickView *createView();
50 QQuickView *m_view;
51 FakeTimer *m_fakeTimer;
52 QSharedPointer<FakeTimeSource> m_fakeTimeSource;
53 AxisVelocityCalculator *m_dragEvaluator;
54};
57void tst_EdgeDragEvaluator::initTestCase()
59 m_view = 0;
62void tst_EdgeDragEvaluator::cleanupTestCase()
66void tst_EdgeDragEvaluator::init()
69 m_view = createView();
70 m_view->setSource(QUrl::fromLocalFile(TEST_DIR"/tst_EdgeDragEvaluator.qml"));
71 m_view->show();
72 QVERIFY(QTest::qWaitForWindowExposed(m_view));
73 QVERIFY(m_view->rootObject() != 0);
74 qApp->processEvents();
76 m_fakeTimeSource.reset(new FakeTimeSource);
77 m_fakeTimer = new FakeTimer(m_fakeTimeSource);
79 m_dragEvaluator =
80 m_view->rootObject()->findChild<AxisVelocityCalculator*>("edgeDragEvaluator");
81 QVERIFY(m_dragEvaluator);
82 m_dragEvaluator->setTimeSource(m_fakeTimeSource);
85void tst_EdgeDragEvaluator::cleanup()
87 m_dragEvaluator = nullptr;
89 delete m_view;
90 m_view = 0;
92 delete m_fakeTimer;
93 m_fakeTimer = 0;
95 m_fakeTimeSource.reset();
98QQuickView *tst_EdgeDragEvaluator::createView()
100 QQuickView *window = new QQuickView(0);
101 window->setResizeMode(QQuickView::SizeRootObjectToView);
102 window->engine()->addImportPath(QLatin1String(TEST_DIR));
104 return window;
107void tst_EdgeDragEvaluator::drag(qreal &pos, qreal distance, qreal speed)
109 int numSteps = 10;
110 qreal distanceStep = distance / ((qreal)numSteps);
111 int timeStep = qAbs(distanceStep / speed);
113 for (int i = 0; i < numSteps; ++i) {
114 m_fakeTimeSource->m_msecsSinceReference += timeStep;
115 pos += distanceStep;
116 m_dragEvaluator->setTrackedPosition(pos);
117 }
120void tst_EdgeDragEvaluator::horizontalDirection()
122 qreal maxDragDistance = 100.;
123 qreal speedThreshold = 100.;
124 qreal speedThresholdMs = speedThreshold / 1000.;
125 QFETCH(qreal, dragDirection);
127 m_dragEvaluator->setProperty("maxDragDistance", QVariant::fromValue(maxDragDistance));
128 m_dragEvaluator->setProperty("dragThreshold", QVariant::fromValue(maxDragDistance * 0.5));
129 m_dragEvaluator->setProperty("speedThreshold", QVariant::fromValue(speedThreshold));
131 // If direction is "Horizontal" it means it can drag leftwards or rightwards
132 m_dragEvaluator->setProperty("direction", QVariant::fromValue((int)Direction::Horizontal));
134 qreal pos = 0.;
135 m_dragEvaluator->setTrackedPosition(pos);
137 // Go far beyond dragThreshold
138 drag(pos, maxDragDistance * 0.75 * dragDirection /*distance*/, speedThresholdMs * 0.5 /*speed*/);
140 // Then come back a bit, getting close to dragThreshold
141 drag(pos, -maxDragDistance * 0.20 * dragDirection /*distance*/, speedThresholdMs * 0.5 /*speed*/);
143 QVariant shouldAutoComplete;
144 bool ok = QMetaObject::invokeMethod(m_dragEvaluator, "shouldAutoComplete", Qt::DirectConnection,
145 Q_RETURN_ARG(QVariant, shouldAutoComplete));
146 QVERIFY(ok);
148 // It should not autocomplete because the current negative velocity (speedThresholdMs * 0.5)
149 // is bigger (in absolute terms) than the minimum negative velocity at this position needed to
150 // cancel autocompletion (speedThreshold * 0.1)
151 QCOMPARE(shouldAutoComplete.toBool(), false);
154void tst_EdgeDragEvaluator::horizontalDirection_data()
156 QTest::addColumn<qreal>("dragDirection");
158 QTest::newRow("right") << 1.;
159 QTest::newRow("left") << -1.;
162QTEST_MAIN(tst_EdgeDragEvaluator)
164#include "tst_EdgeDragEvaluator.moc"