on 4e77f91 help sign in
unity8/tests/plugins/Ubuntu/Gestures/tst_AxisVelocityCalculator.cpp
163 lines 4.1 kB history raw
1/*
2 * Copyright (C) 2013 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 * Authored by: Daniel d'Andrada <daniel.dandrada@canonical.com>
17 */
19#include <QtTest/QtTest>
21#include <AxisVelocityCalculator.h>
23class FakeTimeSource : public UbuntuGestures::TimeSource {
24public:
25 FakeTimeSource() : m_value(0) {}
27 virtual qint64 msecsSinceReference() {
28 return m_value;
29 }
31 void setMsecsSinceReference(qint64 time) {
32 m_value = time;
33 }
35 void increaseMsecsSinceReference(qint64 time) {
36 m_value += time;
37 }
39private:
40 qint64 m_value;
41};
43class tst_AxisVelocityCalculator : public QObject
45 Q_OBJECT
46private Q_SLOTS:
47 void init(); // called right before each and every test function is executed
48 void cleanup(); // called right after each and every test function is executed
50 void simpleSamples();
51 void noSamples();
52 void overflowSamples();
53 void average();
55private:
56 AxisVelocityCalculator *velCalc;
57 QSharedPointer<FakeTimeSource> fakeTimeSource;
58};
60void tst_AxisVelocityCalculator::init()
62 fakeTimeSource.reset(new FakeTimeSource);
64 velCalc = new AxisVelocityCalculator;
65 velCalc->setTimeSource(fakeTimeSource);
68void tst_AxisVelocityCalculator::cleanup()
70 delete velCalc;
71 fakeTimeSource.reset();
74void tst_AxisVelocityCalculator::simpleSamples()
76 qreal pos = 0;
78 velCalc->setTrackedPosition(pos);
79 velCalc->reset();
81 fakeTimeSource->setMsecsSinceReference(10);
82 pos += 20;
83 velCalc->setTrackedPosition(pos);
85 fakeTimeSource->setMsecsSinceReference(20);
86 pos += 20;
87 velCalc->setTrackedPosition(pos);
89 fakeTimeSource->setMsecsSinceReference(30);
90 pos += 20;
91 velCalc->setTrackedPosition(pos);
93 qreal velocity = velCalc->calculate();
95 QCOMPARE(velocity, 2.0f);
98void tst_AxisVelocityCalculator::noSamples()
100 float velocity = velCalc->calculate();
102 QCOMPARE(velocity, 0.0f);
105void tst_AxisVelocityCalculator::overflowSamples()
107 qreal pos = 0;
109 velCalc->setTrackedPosition(pos);
110 velCalc->reset();
112 for (int i = 0; i < 1000; ++i) {
113 fakeTimeSource->increaseMsecsSinceReference(10);
114 pos += 20;
115 velCalc->setTrackedPosition(pos);
116 }
118 /* overwrite all existing samples with faster ones */
119 for (int i = 0; i < AxisVelocityCalculator::MAX_SAMPLES; ++i) {
120 fakeTimeSource->increaseMsecsSinceReference(10);
121 pos += 40;
122 velCalc->setTrackedPosition(pos);
123 }
125 float velocity = velCalc->calculate();
127 /* check that the calculated velocity correspond to the latest, faster, samples */
128 QCOMPARE(velocity, 4.0f);
131void tst_AxisVelocityCalculator::average()
133 qreal pos = 0;
135 velCalc->setTrackedPosition(pos);
136 velCalc->reset();
138 fakeTimeSource->increaseMsecsSinceReference(10);
139 pos += 20;
140 velCalc->setTrackedPosition(pos);
142 fakeTimeSource->increaseMsecsSinceReference(10);
143 pos += 20;
144 velCalc->setTrackedPosition(pos);
146 /* the last sample is an erratic one and would yield a big velocity if
147 considered isolatedly */
148 fakeTimeSource->increaseMsecsSinceReference(10);
149 pos += 100;
150 velCalc->setTrackedPosition(pos);
152 float velocity = velCalc->calculate();
154 /* calculated velocity is lower than the one from the last sample */
155 QVERIFY(velocity < 9.0f);
157 /* but it's higher the the slow samples */
158 QVERIFY(velocity > 2.5f);
161QTEST_MAIN(tst_AxisVelocityCalculator)
163#include "tst_AxisVelocityCalculator.moc"