KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_zoom_controller.cpp
Go to the documentation of this file.
1
/*
2
* This program source code file is part of KiCad, a free EDA CAD application.
3
*
4
* Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
5
*
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License
17
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18
*/
19
20
#include <
qa_utils/wx_utils/unit_test_utils.h
>
21
22
#include <
view/zoom_controller.h
>
23
24
25
// All these tests are of a class in KIGFX
26
using namespace
KIGFX
;
27
28
29
BOOST_AUTO_TEST_SUITE
( ZoomController )
30
31
32
struct
CONST_ZOOM_CASE
33
{
34
double
scale_factor
;
35
int
scroll_amount
;
36
double
exp_zoom_in
;
37
double
exp_zoom_out
;
38
};
39
40
41
/*
42
* Some "sane" examples for steps, scale factors and results.
43
* These should be actual examples that could be encountered
44
*
45
* TODO: Add more cases for, eg, Mac
46
*/
47
static
const
std::vector<CONST_ZOOM_CASE>
const_zoom_cases
= {
48
// A single scroll step on a GTK3 Linux system
49
// 120 is the standard wheel delta, so it's what you might expect
50
// from a single scroll wheel detent.
51
{
CONSTANT_ZOOM_CONTROLLER::GTK3_SCALE
, 120, 1.1, 1 / 1.1 },
52
};
53
57
BOOST_AUTO_TEST_CASE
( ConstController )
58
{
59
// How close we need to be (not very, this is a subjective thing anyway)
60
const
double
tol_percent = 10;
61
62
double
scale_for_step;
63
64
for
(
const
auto
& c :
const_zoom_cases
)
65
{
66
CONSTANT_ZOOM_CONTROLLER
zoom_ctrl( c.scale_factor );
67
68
scale_for_step = zoom_ctrl.
GetScaleForRotation
( c.scroll_amount );
69
BOOST_CHECK_CLOSE( scale_for_step, c.exp_zoom_in, tol_percent );
70
71
scale_for_step = zoom_ctrl.
GetScaleForRotation
( -c.scroll_amount );
72
BOOST_CHECK_CLOSE( scale_for_step, c.exp_zoom_out, tol_percent );
73
}
74
}
75
79
class
PREDEF_TIMESTAMPER
:
public
ACCELERATING_ZOOM_CONTROLLER::TIMESTAMP_PROVIDER
80
{
81
public
:
82
using
STAMP_LIST
= std::vector<int>;
83
84
PREDEF_TIMESTAMPER
(
const
STAMP_LIST
& aStamps )
85
:
m_stamps
( aStamps ),
m_iter
(
m_stamps
.begin() )
86
{
87
}
88
92
ACCELERATING_ZOOM_CONTROLLER::TIME_PT
GetTimestamp
()
override
93
{
94
// Don't ask for more samples than given
95
BOOST_REQUIRE
(
m_iter
!=
m_stamps
.end() );
96
97
return
ACCELERATING_ZOOM_CONTROLLER::TIME_PT
( std::chrono::milliseconds( *
m_iter
++ ) );
98
}
99
100
const
STAMP_LIST
m_stamps
;
101
STAMP_LIST::const_iterator
m_iter
;
102
};
103
104
105
struct
ACCEL_ZOOM_CASE
106
{
107
int
timeout
;
108
std::vector<int>
stamps
;
// NB includes the initial stamp!
109
std::vector<int>
scrolls
;
110
std::vector<double>
zooms
;
111
};
112
113
static
const
std::vector<ACCEL_ZOOM_CASE>
accel_cases
= {
114
// Scrolls widely spaced, just go up and down by a constant factor
115
{ 500, { 0, 1000, 2000, 3000, 4000 }, { 120, 120, -120, -120 }, { 1.05, 1.05, 1 / 1.05, 1 / 1.05 } },
116
// Close scrolls - acceleration, apart from when changing direction
117
{ 500, { 0, 1000, 1100, 1200, 1300, 1400 }, { 120, 120, -120, -120, 120 }, { 1.05, 2.05, 1 / 1.05, 1 / 2.05, 1.05 } },
118
};
119
120
124
BOOST_AUTO_TEST_CASE
( AccelController )
125
{
126
const
double
tol_percent = 10.0;
127
128
for
(
const
auto
& c :
accel_cases
)
129
{
130
PREDEF_TIMESTAMPER
timestamper( c.stamps );
131
132
ACCELERATING_ZOOM_CONTROLLER
zoom_ctrl(
133
ACCELERATING_ZOOM_CONTROLLER::DEFAULT_ACCELERATION_SCALE
,
134
std::chrono::milliseconds( c.timeout ), ×tamper );
135
136
for
(
unsigned
i = 0; i < c.scrolls.size(); i++ )
137
{
138
const
auto
zoom_scale = zoom_ctrl.
GetScaleForRotation
( c.scrolls[i] );
139
140
BOOST_CHECK_CLOSE( zoom_scale, c.zooms[i], tol_percent );
141
}
142
}
143
}
144
145
146
BOOST_AUTO_TEST_SUITE_END
()
KIGFX::ACCELERATING_ZOOM_CONTROLLER::TIMESTAMP_PROVIDER
Definition
zoom_controller.h:80
KIGFX::ACCELERATING_ZOOM_CONTROLLER
Class that zooms faster if scroll events happen very close together.
Definition
zoom_controller.h:59
KIGFX::ACCELERATING_ZOOM_CONTROLLER::TIME_PT
std::chrono::time_point< CLOCK > TIME_PT
The type of the time stamps.
Definition
zoom_controller.h:68
KIGFX::ACCELERATING_ZOOM_CONTROLLER::GetScaleForRotation
double GetScaleForRotation(int aRotation) override
Get the scale factor produced by a given mousewheel rotation.
Definition
zoom_controller.cpp:69
KIGFX::ACCELERATING_ZOOM_CONTROLLER::DEFAULT_ACCELERATION_SCALE
static constexpr double DEFAULT_ACCELERATION_SCALE
The default minimum step factor for accelerating controller.
Definition
zoom_controller.h:74
KIGFX::CONSTANT_ZOOM_CONTROLLER
A CONSTANT_ZOOM_CONTROLLER that zooms by a fixed factor based only on the magnitude of the scroll whe...
Definition
zoom_controller.h:139
KIGFX::CONSTANT_ZOOM_CONTROLLER::GetScaleForRotation
double GetScaleForRotation(int aRotation) override
Get the scale factor produced by a given mousewheel rotation.
Definition
zoom_controller.cpp:121
KIGFX::CONSTANT_ZOOM_CONTROLLER::GTK3_SCALE
static constexpr double GTK3_SCALE
A suitable (magic) scale factor for GTK3 systems.
Definition
zoom_controller.h:150
PREDEF_TIMESTAMPER
Timestamper that returns predefined values from a vector.
Definition
test_zoom_controller.cpp:80
PREDEF_TIMESTAMPER::STAMP_LIST
std::vector< int > STAMP_LIST
Definition
test_zoom_controller.cpp:82
PREDEF_TIMESTAMPER::PREDEF_TIMESTAMPER
PREDEF_TIMESTAMPER(const STAMP_LIST &aStamps)
Definition
test_zoom_controller.cpp:84
PREDEF_TIMESTAMPER::GetTimestamp
ACCELERATING_ZOOM_CONTROLLER::TIME_PT GetTimestamp() override
Definition
test_zoom_controller.cpp:92
PREDEF_TIMESTAMPER::m_stamps
const STAMP_LIST m_stamps
Definition
test_zoom_controller.cpp:100
PREDEF_TIMESTAMPER::m_iter
STAMP_LIST::const_iterator m_iter
Definition
test_zoom_controller.cpp:101
KIGFX
The Cairo implementation of the graphics abstraction layer.
Definition
eda_group.h:29
ACCEL_ZOOM_CASE
Definition
test_zoom_controller.cpp:106
ACCEL_ZOOM_CASE::scrolls
std::vector< int > scrolls
Definition
test_zoom_controller.cpp:109
ACCEL_ZOOM_CASE::stamps
std::vector< int > stamps
Definition
test_zoom_controller.cpp:108
ACCEL_ZOOM_CASE::zooms
std::vector< double > zooms
Definition
test_zoom_controller.cpp:110
ACCEL_ZOOM_CASE::timeout
int timeout
Definition
test_zoom_controller.cpp:107
CONST_ZOOM_CASE
Definition
test_zoom_controller.cpp:33
CONST_ZOOM_CASE::scale_factor
double scale_factor
Definition
test_zoom_controller.cpp:34
CONST_ZOOM_CASE::scroll_amount
int scroll_amount
Definition
test_zoom_controller.cpp:35
CONST_ZOOM_CASE::exp_zoom_out
double exp_zoom_out
Definition
test_zoom_controller.cpp:37
CONST_ZOOM_CASE::exp_zoom_in
double exp_zoom_in
Definition
test_zoom_controller.cpp:36
BOOST_AUTO_TEST_SUITE
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
accel_cases
static const std::vector< ACCEL_ZOOM_CASE > accel_cases
Definition
test_zoom_controller.cpp:113
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(ConstController)
Check basic setting and getting of values.
Definition
test_zoom_controller.cpp:57
const_zoom_cases
static const std::vector< CONST_ZOOM_CASE > const_zoom_cases
Definition
test_zoom_controller.cpp:47
unit_test_utils.h
zoom_controller.h
ZOOM_CONTROLLER class definition.
src
qa
tests
common
view
test_zoom_controller.cpp
Generated on Fri Jun 26 2026 00:05:43 for KiCad PCB EDA Suite by
1.13.2