~unity-team/unity8/ota9.5

« back to all changes in this revision

Viewing changes to tests/plugins/Ubuntu/Gestures/tst_AxisVelocityCalculator.cpp

  • Committer: Michał Sawicz
  • Date: 2013-06-05 22:03:08 UTC
  • Revision ID: michal.sawicz@canonical.com-20130605220308-yny8fv3futtr04fg
Inital unity8 commit.

Previous history can be found at https://code.launchpad.net/~unity-team/unity/phablet

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 */
 
18
 
 
19
#include <QtTest/QtTest>
 
20
 
 
21
#include <AxisVelocityCalculator.h>
 
22
 
 
23
class FakeTimeSource : public AxisVelocityCalculator::TimeSource {
 
24
public:
 
25
    FakeTimeSource() : m_value(0) {}
 
26
 
 
27
    virtual qint64 msecsSinceReference() {
 
28
        return m_value;
 
29
    }
 
30
 
 
31
    void setMsecsSinceReference(qint64 time) {
 
32
        m_value = time;
 
33
    }
 
34
 
 
35
    void increaseMsecsSinceReference(qint64 time) {
 
36
        m_value += time;
 
37
    }
 
38
 
 
39
private:
 
40
    qint64 m_value;
 
41
};
 
42
 
 
43
class tst_AxisVelocityCalculator : public QObject
 
44
{
 
45
    Q_OBJECT
 
46
private Q_SLOTS:
 
47
    void simpleSamples();
 
48
    void noSamples();
 
49
    void overflowSamples();
 
50
    void average();
 
51
};
 
52
 
 
53
void tst_AxisVelocityCalculator::simpleSamples()
 
54
{
 
55
    FakeTimeSource *fakeTimeSource = new FakeTimeSource;
 
56
    AxisVelocityCalculator velCalc(fakeTimeSource);
 
57
    qreal pos = 0;
 
58
 
 
59
    velCalc.setTrackedPosition(pos);
 
60
    velCalc.reset();
 
61
 
 
62
    fakeTimeSource->setMsecsSinceReference(10);
 
63
    pos += 20;
 
64
    velCalc.setTrackedPosition(pos);
 
65
 
 
66
    fakeTimeSource->setMsecsSinceReference(20);
 
67
    pos += 20;
 
68
    velCalc.setTrackedPosition(pos);
 
69
 
 
70
    fakeTimeSource->setMsecsSinceReference(30);
 
71
    pos += 20;
 
72
    velCalc.setTrackedPosition(pos);
 
73
 
 
74
    qreal velocity = velCalc.calculate();
 
75
 
 
76
    QCOMPARE(velocity, 2.0f);
 
77
}
 
78
 
 
79
void tst_AxisVelocityCalculator::noSamples()
 
80
{
 
81
    FakeTimeSource *fakeTimeSource = new FakeTimeSource;
 
82
    AxisVelocityCalculator velCalc(fakeTimeSource);
 
83
 
 
84
    float velocity = velCalc.calculate();
 
85
 
 
86
    QCOMPARE(velocity, 0.0f);
 
87
}
 
88
 
 
89
void tst_AxisVelocityCalculator::overflowSamples()
 
90
{
 
91
    FakeTimeSource *fakeTimeSource = new FakeTimeSource;
 
92
    AxisVelocityCalculator velCalc(fakeTimeSource);
 
93
    qreal pos = 0;
 
94
 
 
95
    velCalc.setTrackedPosition(pos);
 
96
    velCalc.reset();
 
97
 
 
98
    for (int i = 0; i < 1000; ++i) {
 
99
        fakeTimeSource->increaseMsecsSinceReference(10);
 
100
        pos += 20;
 
101
        velCalc.setTrackedPosition(pos);
 
102
    }
 
103
 
 
104
    /* overwrite all existing samples with faster ones */
 
105
    for (int i = 0; i < AxisVelocityCalculator::MAX_SAMPLES; ++i) {
 
106
        fakeTimeSource->increaseMsecsSinceReference(10);
 
107
        pos += 40;
 
108
        velCalc.setTrackedPosition(pos);
 
109
    }
 
110
 
 
111
    float velocity = velCalc.calculate();
 
112
 
 
113
    /* check that the calculated velocity correspond to the latest, faster, samples */
 
114
    QCOMPARE(velocity, 4.0f);
 
115
}
 
116
 
 
117
void tst_AxisVelocityCalculator::average()
 
118
{
 
119
    FakeTimeSource *fakeTimeSource = new FakeTimeSource;
 
120
    AxisVelocityCalculator velCalc(fakeTimeSource);
 
121
    qreal pos = 0;
 
122
 
 
123
    velCalc.setTrackedPosition(pos);
 
124
    velCalc.reset();
 
125
 
 
126
    fakeTimeSource->increaseMsecsSinceReference(10);
 
127
    pos += 20;
 
128
    velCalc.setTrackedPosition(pos);
 
129
 
 
130
    fakeTimeSource->increaseMsecsSinceReference(10);
 
131
    pos += 20;
 
132
    velCalc.setTrackedPosition(pos);
 
133
 
 
134
    /* the last sample is an erratic one and would yield a big velocity if
 
135
       considered isolatedly */
 
136
    fakeTimeSource->increaseMsecsSinceReference(10);
 
137
    pos += 100;
 
138
    velCalc.setTrackedPosition(pos);
 
139
 
 
140
    float velocity = velCalc.calculate();
 
141
 
 
142
    /* calculated velocity is lower than the one from the last sample */
 
143
    QVERIFY(velocity < 9.0f);
 
144
 
 
145
    /* but it's higher the the slow samples */
 
146
    QVERIFY(velocity > 2.5f);
 
147
}
 
148
 
 
149
QTEST_MAIN(tst_AxisVelocityCalculator)
 
150
 
 
151
#include "tst_AxisVelocityCalculator.moc"