KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_footprint_editor_tabs.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
21
22#include <memory>
23
24#include <functional>
25#include <vector>
26
27#include <board.h>
28#include <footprint.h>
29#include <lib_id.h>
30
34
35
37static std::unique_ptr<BOARD> makeFpHolder( const wxString& aLib, const wxString& aName )
38{
39 auto board = std::make_unique<BOARD>();
40 board->SetBoardUse( BOARD_USE::FPHOLDER );
41
42 FOOTPRINT* fp = new FOOTPRINT( board.get() );
43 fp->SetFPID( LIB_ID( aLib, aName ) );
44 board->Add( fp );
45
46 return board;
47}
48
49
52{
53public:
54 explicit INSTRUMENTED_BOARD( int* aDtorCounter ) : m_dtorCounter( aDtorCounter ) {}
55
57 {
58 if( m_dtorCounter )
59 ( *m_dtorCounter )++;
60 }
61
62private:
64};
65
66
67BOOST_AUTO_TEST_SUITE( FootprintEditorTabs )
68
69
70
72BOOST_AUTO_TEST_CASE( ReusedPreviewTabBoardOutlivesInstall )
73{
74 int dtorCount = 0;
75
76 auto oldBoard = std::make_unique<INSTRUMENTED_BOARD>( &dtorCount );
77 oldBoard->SetBoardUse( BOARD_USE::FPHOLDER );
78
79 std::vector<std::unique_ptr<FOOTPRINT_EDITOR_TAB_CONTEXT>> contexts;
80 contexts.push_back( std::make_unique<FOOTPRINT_EDITOR_TAB_CONTEXT>( wxS( "Lib" ), wxS( "A" ),
81 std::move( oldBoard ) ) );
82
83 auto newCtx = std::make_unique<FOOTPRINT_EDITOR_TAB_CONTEXT>(
84 wxS( "Lib" ), wxS( "B" ), makeFpHolder( wxS( "Lib" ), wxS( "B" ) ) );
85 FOOTPRINT_EDITOR_TAB_CONTEXT* newRaw = newCtx.get();
86
87 int dtorCountDuringInstall = -1;
88
90 contexts, 0, std::move( newCtx ),
91 [&]() { dtorCountDuringInstall = dtorCount; } );
92
93 // The displaced board is still alive while the successor is installed, and freed only afterwards.
94 BOOST_CHECK_EQUAL( dtorCountDuringInstall, 0 );
95 BOOST_CHECK_EQUAL( dtorCount, 1 );
96
97 // The slot holds the successor, stays index-aligned, and is returned raw.
98 BOOST_REQUIRE_EQUAL( contexts.size(), 1u );
99 BOOST_CHECK_EQUAL( contexts[0].get(), newRaw );
100 BOOST_CHECK_EQUAL( installed, newRaw );
101 BOOST_CHECK_EQUAL( contexts[0]->GetName(), wxS( "B" ) );
102}
103
104
105BOOST_AUTO_TEST_CASE( ContextIdentityAndDirty )
106{
107 std::unique_ptr<BOARD> board = makeFpHolder( wxS( "Resistors" ), wxS( "R_0402" ) );
108 BOARD* raw = board.get();
109
110 FOOTPRINT_EDITOR_TAB_CONTEXT ctx( wxS( "Resistors" ), wxS( "R_0402" ), std::move( board ) );
111
112 BOOST_CHECK_EQUAL( ctx.GetTabKey(), wxS( "Resistors:R_0402" ) );
113 BOOST_CHECK_EQUAL( ctx.GetDisplayName(), wxS( "R_0402" ) );
114
115 BOOST_CHECK_EQUAL( ctx.GetBoard(), raw );
116
117 BOOST_CHECK( !ctx.IsModified() );
118
119 ctx.SetModified( true );
120 BOOST_CHECK( ctx.IsModified() );
121
122 ctx.SetModified( false );
123 BOOST_CHECK( !ctx.IsModified() );
124}
125
126
127BOOST_AUTO_TEST_CASE( ContextEmptyBoardIsNeverDirty )
128{
129 auto board = std::make_unique<BOARD>();
130 board->SetBoardUse( BOARD_USE::FPHOLDER );
131
132 FOOTPRINT_EDITOR_TAB_CONTEXT ctx( wxS( "Lib" ), wxS( "Empty" ), std::move( board ) );
133
134 ctx.SetModified( true );
135 BOOST_CHECK( !ctx.IsModified() );
136}
137
138
141BOOST_AUTO_TEST_CASE( InstanceTabContextIsTransientAndKeyedByUuid )
142{
143 KIID sourceUuid;
144 std::unique_ptr<BOARD> board = makeFpHolder( wxS( "Resistors" ), wxS( "R_0402" ) );
145 BOARD* raw = board.get();
146
147 FOOTPRINT_EDITOR_TAB_CONTEXT ctx( sourceUuid, wxS( "R5" ), std::move( board ) );
148
149 BOOST_CHECK( ctx.IsTransient() );
150 BOOST_CHECK( ctx.IsFromBoard() );
151 BOOST_CHECK_EQUAL( ctx.GetReference(), wxS( "R5" ) );
152 BOOST_CHECK_EQUAL( ctx.GetSourceUuid().AsString(), sourceUuid.AsString() );
153 BOOST_CHECK_EQUAL( ctx.GetBoard(), raw );
154 BOOST_CHECK_EQUAL( ctx.GetDisplayName(), wxS( "R5" ) );
155
158 BOOST_CHECK( ctx.GetTabKey() != wxS( "Resistors:R_0402" ) );
159 BOOST_CHECK( ctx.GetTabKey().StartsWith( wxString( wxT( "\x01" ) ) ) );
160
161 // The remap is mutable and survives on the context for save-back.
162 KIID editorId, boardId;
163 ctx.BoardFootprintUuids()[editorId] = boardId;
164 BOOST_REQUIRE_EQUAL( ctx.BoardFootprintUuids().count( editorId ), 1u );
165 BOOST_CHECK_EQUAL( ctx.BoardFootprintUuids()[editorId].AsString(), boardId.AsString() );
166}
167
168
169BOOST_AUTO_TEST_CASE( SettingsOpenTabsRoundTrip )
170{
172
173 settings.m_OpenTabs.clear();
174 settings.m_OpenTabs.push_back( { wxS( "Resistors" ), wxS( "R_0402" ) } );
175 settings.m_OpenTabs.push_back( { wxS( "Caps" ), wxS( "C_0603" ) } );
176 settings.m_ActiveTab = wxS( "Caps:C_0603" );
177
178 settings.Store();
179
180 settings.m_OpenTabs.clear();
181 settings.m_ActiveTab = wxEmptyString;
182
183 settings.Load();
184
185 BOOST_REQUIRE_EQUAL( settings.m_OpenTabs.size(), 2u );
186 BOOST_CHECK_EQUAL( settings.m_OpenTabs[0].m_lib, wxS( "Resistors" ) );
187 BOOST_CHECK_EQUAL( settings.m_OpenTabs[0].m_fpName, wxS( "R_0402" ) );
188 BOOST_CHECK_EQUAL( settings.m_OpenTabs[1].m_lib, wxS( "Caps" ) );
189 BOOST_CHECK_EQUAL( settings.m_OpenTabs[1].m_fpName, wxS( "C_0603" ) );
190 BOOST_CHECK_EQUAL( settings.m_ActiveTab, wxS( "Caps:C_0603" ) );
191}
192
193
@ FPHOLDER
Definition board.h:365
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:373
BOARD()
Definition board.cpp:90
std::vector< OPEN_TAB > m_OpenTabs
wxString m_ActiveTab
Tab key ("lib:fpName") of the tab that was active when the editor last closed.
One open footprint tab owning its fp-holder board, lent to the frame by raw pointer while active.
std::map< KIID, KIID > & BoardFootprintUuids()
Editor-to-board UUID remap captured at load, used by SaveFootprintToBoard to restore the original boa...
bool IsTransient() const
True for an instance (board) tab, which is session-only and never persisted.
BOARD * GetBoard() const
The fp-holder board owned by this context.
wxString GetDisplayName() const override
Short label shown on the tab.
static wxString MakeInstanceTabKey(const KIID &aSourceUuid)
De-duplication key for a placed board footprint, in a namespace disjoint from library keys.
bool IsModified() const override
True only when dirty and the board actually holds a footprint to edit.
wxString GetTabKey() const override
Stable identity for persistence and de-duplication.
static FOOTPRINT_EDITOR_TAB_CONTEXT * placeReusedTabContext(std::vector< std::unique_ptr< FOOTPRINT_EDITOR_TAB_CONTEXT > > &aContexts, int aSlot, std::unique_ptr< FOOTPRINT_EDITOR_TAB_CONTEXT > aNew, const std::function< void()> &aInstallSuccessor)
Replace the tab context at aSlot with aNew, running aInstallSuccessor before the displaced context is...
void SetFPID(const LIB_ID &aFPID)
Definition footprint.h:442
INSTRUMENTED_BOARD(int *aDtorCounter)
virtual void Load()
Updates the parameters of this object based on the current JSON document contents.
virtual bool Store()
Stores the current parameters into the JSON document represented by this object Note: this doesn't do...
Definition kiid.h:44
wxString AsString() const
Definition kiid.cpp:242
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_CASE(ReusedPreviewTabBoardOutlivesInstall)
Reusing a preview tab must install the successor board before the displaced board is freed,...
static std::unique_ptr< BOARD > makeFpHolder(const wxString &aLib, const wxString &aName)
Build an fp-holder BOARD carrying a single footprint with the given identity.
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_EQUAL(result, "25.4")