KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_issue22651_sheet_annotation.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, 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
37
38#include <boost/test/unit_test.hpp>
39
40#include <sch_screen.h>
41#include <sch_sheet.h>
42#include <sch_sheet_path.h>
43#include <sch_symbol.h>
44#include <schematic.h>
45#include <sch_reference_list.h>
47
49
50#include <wx/filename.h>
51#include <wx/stdpaths.h>
52
53
55{
58 {
59 wxString tempDir = wxStandardPaths::Get().GetTempDir();
60 wxString projectPath = tempDir + wxFileName::GetPathSeparator()
61 + wxT( "test_sheet_annotation.kicad_pro" );
62 m_tempFiles.push_back( projectPath );
63
64 m_settingsManager.LoadProject( projectPath.ToStdString() );
65 m_schematic = std::make_unique<SCHEMATIC>( nullptr );
67 m_schematic->SetProject( m_project );
68 }
69
71 {
72 for( const wxString& file : m_tempFiles )
73 {
74 if( wxFileExists( file ) )
75 wxRemoveFile( file );
76 }
77
78 m_schematic.reset();
79 }
80
82 std::unique_ptr<SCHEMATIC> m_schematic;
84 std::vector<wxString> m_tempFiles;
85};
86
87
88BOOST_FIXTURE_TEST_SUITE( Issue22651SheetAnnotation, SHEET_ANNOTATION_FIXTURE )
89
90
91
95BOOST_AUTO_TEST_CASE( TestSheetFoundInHierarchyAfterRefresh )
96{
97 m_schematic->CreateDefaultScreens();
98
99 std::vector<SCH_SHEET*> topSheets = m_schematic->GetTopLevelSheets();
100 BOOST_REQUIRE( !topSheets.empty() );
101
102 SCH_SHEET* rootSheet = topSheets[0];
103 SCH_SCREEN* rootScreen = rootSheet->GetScreen();
104 BOOST_REQUIRE( rootScreen != nullptr );
105
106 // Create a new sub-sheet with its own screen
107 SCH_SHEET* newSheet = new SCH_SHEET( rootSheet, VECTOR2I( 0, 0 ) );
108 SCH_SCREEN* newScreen = new SCH_SCREEN( m_schematic.get() );
109 newSheet->SetScreen( newScreen );
110 newSheet->GetField( FIELD_T::SHEET_NAME )->SetText( wxT( "TestSubSheet" ) );
111 newSheet->GetField( FIELD_T::SHEET_FILENAME )->SetText( wxT( "test_subsheet.kicad_sch" ) );
112 newScreen->SetFileName( wxT( "/tmp/test_subsheet.kicad_sch" ) );
113
114 // Add a symbol to the sub-sheet's screen
115 SCH_SYMBOL* symbol = new SCH_SYMBOL();
116 symbol->SetPosition( VECTOR2I( 1000000, 0 ) );
117 newScreen->Append( symbol );
118
119 // Get hierarchy BEFORE adding the sheet (simulating stale cache)
120 SCH_SHEET_LIST hierarchyBefore = m_schematic->Hierarchy();
121 size_t countBefore = hierarchyBefore.size();
122
123 // Add the new sheet to the root screen
124 rootScreen->Append( newSheet );
125
126 // The cached hierarchy should still be stale at this point
127 SCH_SHEET_LIST hierarchyStale = m_schematic->Hierarchy();
128 BOOST_CHECK_EQUAL( hierarchyStale.size(), countBefore );
129
130 // Refresh the hierarchy
131 m_schematic->RefreshHierarchy();
132
133 // Now the hierarchy should include the new sheet
134 SCH_SHEET_LIST hierarchyAfter = m_schematic->Hierarchy();
135 BOOST_CHECK_EQUAL( hierarchyAfter.size(), countBefore + 1 );
136
137 BOOST_TEST_MESSAGE( "Test passed: Sheet found in hierarchy after refresh" );
138}
139
140
145BOOST_AUTO_TEST_CASE( TestSymbolsFoundInNewSheetAfterRefresh )
146{
147 m_schematic->CreateDefaultScreens();
148
149 std::vector<SCH_SHEET*> topSheets = m_schematic->GetTopLevelSheets();
150 BOOST_REQUIRE( !topSheets.empty() );
151
152 SCH_SHEET* rootSheet = topSheets[0];
153 SCH_SCREEN* rootScreen = rootSheet->GetScreen();
154 BOOST_REQUIRE( rootScreen != nullptr );
155
156 // Create a new sub-sheet with its own screen
157 SCH_SHEET* newSheet = new SCH_SHEET( rootSheet, VECTOR2I( 0, 0 ) );
158 SCH_SCREEN* newScreen = new SCH_SCREEN( m_schematic.get() );
159 newSheet->SetScreen( newScreen );
160 newSheet->GetField( FIELD_T::SHEET_NAME )->SetText( wxT( "DesignBlockSheet" ) );
161 newSheet->GetField( FIELD_T::SHEET_FILENAME )->SetText( wxT( "design_block.kicad_sch" ) );
162 newScreen->SetFileName( wxT( "/tmp/design_block.kicad_sch" ) );
163
164 // Add two symbols to the sub-sheet's screen (simulating design block content)
165 SCH_SYMBOL* symbol1 = new SCH_SYMBOL();
166 symbol1->SetPosition( VECTOR2I( 0, 0 ) );
167 newScreen->Append( symbol1 );
168
169 SCH_SYMBOL* symbol2 = new SCH_SYMBOL();
170 symbol2->SetPosition( VECTOR2I( 1000000, 0 ) );
171 newScreen->Append( symbol2 );
172
173 // Add the new sheet to the root screen
174 rootScreen->Append( newSheet );
175
176 // Refresh the hierarchy (this is the fix for issue 22651)
177 m_schematic->RefreshHierarchy();
178
179 // Build the sheet path for the new sheet
180 SCH_SHEET_PATH currentPath;
181 currentPath.push_back( rootSheet );
182
183 SCH_SHEET_PATH newSheetPath = currentPath;
184 newSheetPath.push_back( newSheet );
185
186 // Use GetSheetsWithinPath to find the new sheet (as annotation does)
187 SCH_SHEET_LIST hierarchy = m_schematic->Hierarchy();
188 std::vector<SCH_SHEET_PATH> foundSheets;
189 hierarchy.GetSheetsWithinPath( foundSheets, newSheetPath );
190
191 BOOST_CHECK_EQUAL( foundSheets.size(), 1 );
192
193 // Use GetSymbolsWithinPath to find symbols (as annotation does)
194 // Pass true for aForceIncludeOrphanSymbols since test symbols don't have library refs
195 SCH_REFERENCE_LIST references;
196 hierarchy.GetSymbolsWithinPath( references, newSheetPath, false, true );
197
198 BOOST_CHECK_EQUAL( references.GetCount(), 2 );
199
200 BOOST_TEST_MESSAGE( "Test passed: Symbols found in new sheet after hierarchy refresh" );
201}
202
203
208BOOST_AUTO_TEST_CASE( TestSymbolsNotFoundWithoutRefresh )
209{
210 m_schematic->CreateDefaultScreens();
211
212 std::vector<SCH_SHEET*> topSheets = m_schematic->GetTopLevelSheets();
213 BOOST_REQUIRE( !topSheets.empty() );
214
215 SCH_SHEET* rootSheet = topSheets[0];
216 SCH_SCREEN* rootScreen = rootSheet->GetScreen();
217 BOOST_REQUIRE( rootScreen != nullptr );
218
219 // Get hierarchy BEFORE adding any sheets
220 SCH_SHEET_LIST hierarchyBefore = m_schematic->Hierarchy();
221
222 // Create a new sub-sheet with its own screen
223 SCH_SHEET* newSheet = new SCH_SHEET( rootSheet, VECTOR2I( 0, 0 ) );
224 SCH_SCREEN* newScreen = new SCH_SCREEN( m_schematic.get() );
225 newSheet->SetScreen( newScreen );
226 newSheet->GetField( FIELD_T::SHEET_FILENAME )->SetText( wxT( "stale_test.kicad_sch" ) );
227 newScreen->SetFileName( wxT( "/tmp/stale_test.kicad_sch" ) );
228
229 // Add a symbol to the sub-sheet's screen
230 SCH_SYMBOL* symbol = new SCH_SYMBOL();
231 symbol->SetPosition( VECTOR2I( 0, 0 ) );
232 newScreen->Append( symbol );
233
234 // Add the new sheet to the root screen
235 rootScreen->Append( newSheet );
236
237 // Build the sheet path for the new sheet
238 SCH_SHEET_PATH currentPath;
239 currentPath.push_back( rootSheet );
240
241 SCH_SHEET_PATH newSheetPath = currentPath;
242 newSheetPath.push_back( newSheet );
243
244 // WITHOUT refreshing the hierarchy, try to find symbols
245 // This demonstrates the bug behavior before the fix
246 std::vector<SCH_SHEET_PATH> foundSheets;
247 hierarchyBefore.GetSheetsWithinPath( foundSheets, newSheetPath );
248
249 // The stale hierarchy should NOT find the new sheet
250 BOOST_CHECK_EQUAL( foundSheets.size(), 0 );
251
252 SCH_REFERENCE_LIST references;
253 hierarchyBefore.GetSymbolsWithinPath( references, newSheetPath, false, true );
254
255 // The stale hierarchy should NOT find the symbols
256 BOOST_CHECK_EQUAL( references.GetCount(), 0 );
257
258 BOOST_TEST_MESSAGE( "Test passed: Stale hierarchy does not find symbols (confirms root cause)" );
259}
260
261
Container for project specific data.
Definition project.h:65
void SetText(const wxString &aText) override
Container to create a flattened list of symbols because in a complex hierarchy, a symbol can be used ...
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
void GetSheetsWithinPath(std::vector< SCH_SHEET_PATH > &aSheets, const SCH_SHEET_PATH &aSheetPath) const
Add a SCH_SHEET_PATH object to aSheets for each sheet in the list that are contained within aSheetPat...
void GetSymbolsWithinPath(SCH_REFERENCE_LIST &aReferences, const SCH_SHEET_PATH &aSheetPath, bool aIncludePowerSymbols=true, bool aForceIncludeOrphanSymbols=false) const
Add a SCH_REFERENCE object to aReferences for each symbol in the list of sheets that are contained wi...
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
void push_back(SCH_SHEET *aSheet)
Forwarded method from std::vector.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:48
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this sheet.
SCH_SCREEN * GetScreen() const
Definition sch_sheet.h:145
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
Schematic symbol object.
Definition sch_symbol.h:76
void SetPosition(const VECTOR2I &aPosition) override
Definition sch_symbol.h:855
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
std::unique_ptr< SCHEMATIC > m_schematic
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(TestSheetFoundInHierarchyAfterRefresh)
Test that after adding a sheet and refreshing the hierarchy, the sheet path can be found via GetSheet...
BOOST_TEST_MESSAGE("Polyline has "<< chain.PointCount()<< " points")
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695