KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_tool_dispatcher.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 modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <boost/test/unit_test.hpp>
22
24#include <tool/tool_manager.h>
26#include <tool/action_menu.h>
27
28#include <wx/display.h>
29#include <wx/evtloop.h>
30
35
36BOOST_AUTO_TEST_SUITE( ToolDispatcherAutoRepeat )
37
38// A single key press whose key has already been released (a quick tap) must always run.
39BOOST_AUTO_TEST_CASE( LeadingEdgeAlwaysRuns )
40{
41 int lastKey = 0;
42 wxLongLong lastTime = 0;
43
44 bool drop = TOOL_DISPATCHER::ShouldDropAutoRepeat( 'R', /*now*/ 1000, /*keyIsDown*/ false,
45 lastKey, lastTime );
46
47 BOOST_CHECK( !drop );
48 BOOST_CHECK_EQUAL( lastKey, 'R' );
49 BOOST_CHECK( lastTime == 1000 );
50}
51
52// Repeats that arrive within the burst window while the key is still held keep running.
53BOOST_AUTO_TEST_CASE( HeldKeyRepeatsRun )
54{
55 int lastKey = 0;
56 wxLongLong lastTime = 0;
57
58 BOOST_CHECK( !TOOL_DISPATCHER::ShouldDropAutoRepeat( 'R', 1000, true, lastKey, lastTime ) );
59 BOOST_CHECK( !TOOL_DISPATCHER::ShouldDropAutoRepeat( 'R', 1030, true, lastKey, lastTime ) );
60 BOOST_CHECK( !TOOL_DISPATCHER::ShouldDropAutoRepeat( 'R', 1060, true, lastKey, lastTime ) );
61}
62
63// The defect: repeats from the same burst that arrive after release must be dropped.
64BOOST_AUTO_TEST_CASE( StaleRepeatAfterReleaseDropped )
65{
66 int lastKey = 0;
67 wxLongLong lastTime = 0;
68
69 BOOST_CHECK( !TOOL_DISPATCHER::ShouldDropAutoRepeat( 'R', 1000, true, lastKey, lastTime ) );
70
71 // Backlogged events still in the queue after the key came up.
72 BOOST_CHECK( TOOL_DISPATCHER::ShouldDropAutoRepeat( 'R', 1030, false, lastKey, lastTime ) );
73 BOOST_CHECK( TOOL_DISPATCHER::ShouldDropAutoRepeat( 'R', 1060, false, lastKey, lastTime ) );
74}
75
76// A second deliberate tap after a human-scale gap is a new burst and must run again.
77BOOST_AUTO_TEST_CASE( SecondDeliberateTapRuns )
78{
79 int lastKey = 0;
80 wxLongLong lastTime = 0;
81
82 BOOST_CHECK( !TOOL_DISPATCHER::ShouldDropAutoRepeat( 'R', 1000, false, lastKey, lastTime ) );
83
84 // Well beyond the auto-repeat window, so this is treated as a fresh press, not stale backlog.
85 wxLongLong later = 1000 + TOOL_DISPATCHER::AutoRepeatWindowMs + 1;
86 BOOST_CHECK( !TOOL_DISPATCHER::ShouldDropAutoRepeat( 'R', later, false, lastKey, lastTime ) );
87}
88
89// Switching to a different key starts a new burst whose leading edge runs.
90BOOST_AUTO_TEST_CASE( DifferentKeyIsNewBurst )
91{
92 int lastKey = 0;
93 wxLongLong lastTime = 0;
94
95 BOOST_CHECK( !TOOL_DISPATCHER::ShouldDropAutoRepeat( 'R', 1000, true, lastKey, lastTime ) );
96
97 // A different hotkey arriving immediately is not part of the R burst.
98 BOOST_CHECK( !TOOL_DISPATCHER::ShouldDropAutoRepeat( 'F', 1010, false, lastKey, lastTime ) );
99}
100
102
103
104namespace
105{
106// TOOL_EVENT's constructor reads ACTIONS::cancelInteractive, a static in another translation
107// unit, so the event has to be initialized on first use rather than at file scope
108const TOOL_EVENT& armMenuEvent()
109{
110 static const TOOL_EVENT evt( TC_COMMAND, TA_ACTION, std::string( "test.armContextMenu" ) );
111
112 return evt;
113}
114
115
116class YIELDING_EVENT_LOOP : public wxEventLoop
117{
118public:
119 bool IsYielding() const override { return true; }
120};
121
122
123class CONTEXT_MENU_TEST_TOOL : public TOOL_INTERACTIVE
124{
125public:
126 CONTEXT_MENU_TEST_TOOL() :
127 TOOL_INTERACTIVE( "test.contextMenuTool" ),
128 m_menuRan( false ),
129 m_menu( true )
130 {
131 }
132
133 void Reset( RESET_REASON aReason ) override {}
134
135 int ArmMenu( const TOOL_EVENT& aEvent )
136 {
137 Activate();
138 SetContextMenu( &m_menu, CMENU_NOW );
139 Wait( TOOL_EVENT( TC_COMMAND, TA_CHOICE_MENU_CLOSED ) );
140 m_menuRan = true;
141
142 return 0;
143 }
144
145 bool m_menuRan;
146
147private:
148 void setTransitions() override { Go( &CONTEXT_MENU_TEST_TOOL::ArmMenu, armMenuEvent() ); }
149
150 ACTION_MENU m_menu;
151};
152} // namespace
153
154
155BOOST_AUTO_TEST_SUITE( ToolManagerContextMenu )
156
157BOOST_AUTO_TEST_CASE( RightClickOpensMenuWhileEventLoopYields )
158{
160 {
161 BOOST_TEST_MESSAGE( "Skipping test - no display available" );
162 return;
163 }
164
165 TOOL_MANAGER mgr;
166 CONTEXT_MENU_TEST_TOOL* tool = new CONTEXT_MENU_TEST_TOOL();
167
168 mgr.SetEnvironment( nullptr, nullptr, nullptr, nullptr, nullptr );
169 mgr.RegisterTool( tool );
171
172 YIELDING_EVENT_LOOP loop;
173 wxEventLoopActivator activate( &loop );
174
175 mgr.ProcessEvent( armMenuEvent() );
176 BOOST_CHECK( !tool->m_menuRan );
177
179 BOOST_CHECK( tool->m_menuRan );
180}
181
@ RUN
Tool is invoked after being inactive.
Definition tool_base.h:75
static const int AutoRepeatWindowMs
The maximum gap (ms) between two events of the same key for them to count as one OS auto-repeat burst...
static bool ShouldDropAutoRepeat(int aKeyCode, wxLongLong aNowMs, bool aKeyIsDown, int &aLastKey, wxLongLong &aLastTimeMs)
Pure decision used to discard backlogged OS key auto-repeat events.
Generic, UI-independent tool event.
Definition tool_event.h:167
Master controller class:
bool ProcessEvent(const TOOL_EVENT &aEvent)
Propagate an event to tools that requested events of matching type(s).
void ResetTools(TOOL_BASE::RESET_REASON aReason)
Reset all tools (i.e.
void RegisterTool(TOOL_BASE *aTool)
Add a tool to the manager set and sets it up.
void SetEnvironment(EDA_ITEM *aModel, KIGFX::VIEW *aView, KIGFX::VIEW_CONTROLS *aViewControls, APP_SETTINGS_BASE *aSettings, TOOLS_HOLDER *aFrame)
Set the work environment (model, view, view controls and the parent window).
void Reset() override
bool CanDoDisplayTests()
Some tests on some platforms require a display connection to run.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
BOOST_TEST_MESSAGE("Polyline has "<< chain.PointCount()<< " points")
BOOST_CHECK_EQUAL(result, "25.4")
BOOST_AUTO_TEST_CASE(LeadingEdgeAlwaysRuns)
Tests for TOOL_DISPATCHER::ShouldDropAutoRepeat, the pure decision that discards backlogged OS key au...
@ TA_MOUSE_CLICK
Definition tool_event.h:63
@ TA_CHOICE_MENU_CLOSED
Context menu is closed, no matter whether anything has been chosen or not.
Definition tool_event.h:97
@ TA_ACTION
Tool action (allows one to control tools).
Definition tool_event.h:108
@ CMENU_NOW
Right now (after TOOL_INTERACTIVE::SetContextMenu).
Definition tool_event.h:152
@ TC_COMMAND
Definition tool_event.h:53
@ TC_MOUSE
Definition tool_event.h:51
@ BUT_RIGHT
Definition tool_event.h:129