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/AxisDecelerationAnimation.h>
26
TEST(AxisDecelerationAnimation, ComesToAHalt)
28
AxisDecelerationAnimation animation;
30
float max_pos = 100.0f;
31
float start_pos = 10.0f;
33
animation.SetMinimumPosition(min_pos);
34
animation.SetMaximumPosition(max_pos);
35
animation.SetStartPosition(start_pos);
36
animation.SetStartVelocity(100.0f);
37
animation.SetOvershootBoundsEnabled(false);
41
float last_pos = start_pos;
42
float last_movement = 0.0f;
43
for (int i = 0; i < 1000 && animation.IsActive(); ++i)
47
/* Check that it's moving along the velocity direction */
48
ASSERT_TRUE(animation.GetPosition() > last_pos);
50
float movement = animation.GetPosition() - last_pos;
54
/* Check that it's decelerating */
55
ASSERT_TRUE(movement - last_movement);
58
last_pos = animation.GetPosition();
59
last_movement = movement;
62
/* Check that it came to a halt */
63
ASSERT_FALSE(animation.IsActive());
66
TEST(AxisDecelerationAnimation, StopAtBounds)
68
AxisDecelerationAnimation animation;
70
float max_pos = 100.0f;
71
float start_pos = 50.0f;
73
animation.SetMinimumPosition(min_pos);
74
animation.SetMaximumPosition(max_pos);
76
/* test different velocities on both directions */
77
std::vector<int> velocities;
78
for (int i = 1; i <= 100; ++i)
80
velocities.push_back(i);
82
for (int i = -1; i >= -100; --i)
84
velocities.push_back(i);
87
for (size_t i = 0; i < velocities.size(); ++i)
89
animation.SetStartPosition(start_pos);
90
animation.SetStartVelocity(velocities[i]);
93
for (int j = 0; j < 1000 && animation.IsActive(); ++j)
97
ASSERT_FALSE(animation.IsActive());
99
ASSERT_LE(animation.GetPosition(), max_pos);
100
ASSERT_GE(animation.GetPosition(), min_pos);