KiCad PCB EDA Suite
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 (C) 2018 KiCad Developers, see CHANGELOG.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, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
25 
26 #include <view/zoom_controller.h>
27 
28 
29 // All these tests are of a class in KIGFX
30 using namespace KIGFX;
31 
32 
33 BOOST_AUTO_TEST_SUITE( ZoomController )
34 
35 
37 {
38  double scale_factor;
40  double exp_zoom_in;
41  double exp_zoom_out;
42 };
43 
44 
45 /*
46  * Some "sane" examples for steps, scale factors and results.
47  * These should be actual examples that could be encountered
48  *
49  * TODO: Add more cases for, eg, Mac
50  */
51 static const std::vector<CONST_ZOOM_CASE> const_zoom_cases = {
52  // A single scroll step on a GTK3 Linux system
53  // 120 is the standard wheel delta, so it's what you might expect
54  // from a single scroll wheel detent.
55  { CONSTANT_ZOOM_CONTROLLER::GTK3_SCALE, 120, 1.1, 1 / 1.1 },
56 };
57 
61 BOOST_AUTO_TEST_CASE( ConstController )
62 {
63  // How close we need to be (not very, this is a subjective thing anyway)
64  const double tol_percent = 10;
65 
66  double scale_for_step;
67 
68  for( const auto& c : const_zoom_cases )
69  {
70  CONSTANT_ZOOM_CONTROLLER zoom_ctrl( c.scale_factor );
71 
72  scale_for_step = zoom_ctrl.GetScaleForRotation( c.scroll_amount );
73  BOOST_CHECK_CLOSE( scale_for_step, c.exp_zoom_in, tol_percent );
74 
75  scale_for_step = zoom_ctrl.GetScaleForRotation( -c.scroll_amount );
76  BOOST_CHECK_CLOSE( scale_for_step, c.exp_zoom_out, tol_percent );
77  }
78 }
79 
84 {
85 public:
86  using STAMP_LIST = std::vector<int>;
87 
88  PREDEF_TIMESTAMPER( const STAMP_LIST& aStamps )
89  : m_stamps( aStamps ), m_iter( m_stamps.begin() )
90  {
91  }
92 
97  {
98  // Don't ask for more samples than given
99  BOOST_REQUIRE( m_iter != m_stamps.end() );
100 
101  return ACCELERATING_ZOOM_CONTROLLER::TIME_PT( std::chrono::milliseconds( *m_iter++ ) );
102  }
103 
105  STAMP_LIST::const_iterator m_iter;
106 };
107 
108 
110 {
111  int timeout;
112  std::vector<int> stamps; // NB includes the initial stamp!
113  std::vector<int> scrolls;
114  std::vector<double> zooms;
115 };
116 
117 static const std::vector<ACCEL_ZOOM_CASE> accel_cases = {
118  // Scrolls widely spaced, just go up and down by a constant factor
119  { 500, { 0, 1000, 2000, 3000 }, { 120, 120, -120 }, { 1.2, 1.2, 1 / 1.2 } },
120  // Close scrolls - acceleration on the latter
121  { 500, { 0, 1000, 1100 }, { 120, 120 }, { 1.2, 2.05 } },
122 };
123 
124 
128 BOOST_AUTO_TEST_CASE( AccelController )
129 {
130  const double tol_percent = 10.0;
131 
132  for( const auto& c : accel_cases )
133  {
134  PREDEF_TIMESTAMPER timestamper( c.stamps );
135 
137  std::chrono::milliseconds( c.timeout ), &timestamper );
138 
139  for( unsigned i = 0; i < c.scrolls.size(); i++ )
140  {
141  const auto zoom_scale = zoom_ctrl.GetScaleForRotation( c.scrolls[i] );
142 
143  BOOST_CHECK_CLOSE( zoom_scale, c.zooms[i], tol_percent );
144  }
145  }
146 }
147 
148 
149 BOOST_AUTO_TEST_SUITE_END()
std::vector< int > scrolls
double GetScaleForRotation(int aRotation) override
Gets the scale factor produced by a given mousewheel rotation.
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:131
std::chrono::time_point< CLOCK > TIME_PT
The type of the time stamps.
static constexpr double GTK3_SCALE
A suitable (magic) scale factor for GTK3 systems.
STAMP_LIST::const_iterator m_iter
BOOST_AUTO_TEST_CASE(ConstController)
Check basic setting and getting of values.
std::vector< double > zooms
Class that zooms faster if scroll events happen very close together.
PREDEF_TIMESTAMPER(const STAMP_LIST &aStamps)
std::vector< int > STAMP_LIST
static const std::vector< CONST_ZOOM_CASE > const_zoom_cases
double GetScaleForRotation(int aRotation) override
Gets the scale factor produced by a given mousewheel rotation.
Timestamper that returns predefined values from a vector.
ACCELERATING_ZOOM_CONTROLLER::TIME_PT GetTimestamp() override
A ZOOM_CONTROLLER that zooms by a fixed factor based only on the magnitude of the scroll wheel rotati...
std::vector< int > stamps
static const std::vector< ACCEL_ZOOM_CASE > accel_cases