2
* Copyright (C) 2012 - Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU Lesser General Public License, as
6
* published by the Free Software Foundation; either version 2.1 or 3.0
9
* This program is distributed in the hope that it will be useful, but
10
* WITHOUT ANY WARRANTY; without even the implied warranties of
11
* MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
12
* PURPOSE. See the applicable version of the GNU Lesser General Public
13
* License for more details.
15
* You should have received a copy of both the GNU Lesser General Public
16
* License along with this program. If not, see <http://www.gnu.org/licenses/>
18
* Authored by: Daniel d'Andrada <daniel.dandrada@canonical.com>
21
#include <gtest/gtest.h>
22
#include <Nux/KineticScrolling/VelocityCalculator.h>
23
#include "gtest-nux-globals.h"
27
TEST(VelocityCalculator, SimpleSamples)
29
VelocityCalculator vel_calc;
31
g_fake_monotonic_time = 10 * 1000;
32
vel_calc.ProcessMovement(20);
34
g_fake_monotonic_time = 20 * 1000;
35
vel_calc.ProcessMovement(20);
37
g_fake_monotonic_time = 30 * 1000;
38
vel_calc.ProcessMovement(20);
40
float velocity = vel_calc.CalculateVelocity();
42
ASSERT_FLOAT_EQ(2.0f, velocity);
45
TEST(VelocityCalculator, NoSamples)
47
VelocityCalculator vel_calc;
49
float velocity = vel_calc.CalculateVelocity();
51
ASSERT_FLOAT_EQ(0.0f, velocity);
54
TEST(VelocityCalculator, OverflowSamples)
56
VelocityCalculator vel_calc;
58
for (int i = 0; i < 1000; ++i)
60
g_fake_monotonic_time += 10 * 1000;
61
vel_calc.ProcessMovement(20);
64
/* overwrite all existing samples with faster ones */
65
for (int i = 0; i < VelocityCalculator::MAX_SAMPLES; ++i)
67
g_fake_monotonic_time += 10 * 1000;
68
vel_calc.ProcessMovement(40);
71
float velocity = vel_calc.CalculateVelocity();
73
/* check that the calculated velocity correspond to the latest, faster, samples */
74
ASSERT_FLOAT_EQ(4.0f, velocity);
77
TEST(VelocityCalculator, Average)
79
VelocityCalculator vel_calc;
81
g_fake_monotonic_time = 10 * 1000;
82
vel_calc.ProcessMovement(20);
84
g_fake_monotonic_time = 20 * 1000;
85
vel_calc.ProcessMovement(20);
87
/* the last sample is an erratic one and would yield a big velocity if
88
considered isolatedly */
89
g_fake_monotonic_time = 30 * 1000;
90
vel_calc.ProcessMovement(100);
92
float velocity = vel_calc.CalculateVelocity();
94
/* calculated velocity is lower than the one from the last sample */
95
ASSERT_TRUE(velocity < 9.0f);
97
/* but it's higher the the slow samples */
98
ASSERT_TRUE(velocity > 2.5f);