KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_multi_top_level_sheets.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
24
25#include <boost/test/unit_test.hpp>
26
27#include <sch_sheet.h>
28#include <sch_screen.h>
29#include <schematic.h>
32#include <pgm_base.h>
33
34static SCH_SHEET* createTopLevelSheet( SCHEMATIC& aSchematic, const wxString& aName,
35 const wxString& aFileName )
36{
37 SCH_SHEET* sheet = new SCH_SHEET( &aSchematic );
38 SCH_SCREEN* screen = new SCH_SCREEN( &aSchematic );
39
40 const_cast<KIID&>( sheet->m_Uuid ) = screen->GetUuid();
41 sheet->SetScreen( screen );
42 sheet->SetName( aName );
43 sheet->SetFileName( aFileName );
44
45 return sheet;
46}
47
48BOOST_AUTO_TEST_SUITE( MultiTopLevelSheets )
49
50BOOST_AUTO_TEST_CASE( TestVirtualRootCreation )
51{
52 // Create a virtual root
53 SCH_SHEET virtualRoot;
54 const_cast<KIID&>( virtualRoot.m_Uuid ) = niluuid;
55 virtualRoot.SetScreen( nullptr );
56
57 // Verify it's recognized as virtual root
58 BOOST_CHECK( virtualRoot.m_Uuid == niluuid );
59 BOOST_CHECK( virtualRoot.GetScreen() == nullptr );
60}
61
62BOOST_AUTO_TEST_CASE( TestAddTopLevelSheet )
63{
64 SCHEMATIC schematic( nullptr );
65
66 SCH_SHEET* sheet1 = createTopLevelSheet( schematic, "Sheet1", "sheet1.kicad_sch" );
67
68 schematic.SetTopLevelSheets( { sheet1 } );
69
70 // Verify the sheet was added
71 const std::vector<SCH_SHEET*>& topSheets = schematic.GetTopLevelSheets();
72 BOOST_CHECK_EQUAL( topSheets.size(), 1 );
73 BOOST_CHECK_EQUAL( topSheets[0], sheet1 );
74 BOOST_CHECK_EQUAL( topSheets[0]->GetName(), "Sheet1" );
75 BOOST_CHECK_EQUAL( schematic.Hierarchy().size(), 1 );
76}
77
78BOOST_AUTO_TEST_CASE( TestAddMultipleTopLevelSheets )
79{
80 SCHEMATIC schematic( nullptr );
81
82 SCH_SHEET* sheet1 = createTopLevelSheet( schematic, "Sheet1", "sheet1.kicad_sch" );
83 schematic.SetTopLevelSheets( { sheet1 } );
84
85 // Create and add multiple top-level sheets
86 for( int i = 2; i <= 3; i++ )
87 {
88 SCH_SHEET* sheet = createTopLevelSheet( schematic, wxString::Format( "Sheet%d", i ),
89 wxString::Format( "sheet%d.kicad_sch", i ) );
90 schematic.AddTopLevelSheet( sheet );
91 }
92
93 // Verify all sheets were added
94 const std::vector<SCH_SHEET*>& topSheets = schematic.GetTopLevelSheets();
95 BOOST_CHECK_EQUAL( topSheets.size(), 3 );
96 BOOST_CHECK_EQUAL( topSheets[0]->GetName(), "Sheet1" );
97 BOOST_CHECK_EQUAL( topSheets[1]->GetName(), "Sheet2" );
98 BOOST_CHECK_EQUAL( topSheets[2]->GetName(), "Sheet3" );
99 BOOST_CHECK_EQUAL( schematic.Hierarchy().size(), 3 );
100}
101
102BOOST_AUTO_TEST_CASE( TestRemoveTopLevelSheet )
103{
104 SCHEMATIC schematic( nullptr );
105
106 SCH_SHEET* sheet1 = createTopLevelSheet( schematic, "Sheet1", "sheet1.kicad_sch" );
107 schematic.SetTopLevelSheets( { sheet1 } );
108
109 SCH_SHEET* sheet2 = createTopLevelSheet( schematic, "Sheet2", "sheet2.kicad_sch" );
110 schematic.AddTopLevelSheet( sheet2 );
111
112 // Remove first sheet
113 schematic.RemoveTopLevelSheet( sheet1 );
114
115 // Verify only sheet2 remains
116 const std::vector<SCH_SHEET*>& topSheets = schematic.GetTopLevelSheets();
117 BOOST_CHECK_EQUAL( topSheets.size(), 1 );
118 BOOST_CHECK_EQUAL( topSheets[0], sheet2 );
119 BOOST_CHECK_EQUAL( topSheets[0]->GetName(), "Sheet2" );
120 BOOST_CHECK_EQUAL( schematic.Hierarchy().size(), 1 );
121
122 // Cannot remove the final remaining sheet
123 BOOST_CHECK( !schematic.RemoveTopLevelSheet( topSheets[0] ) );
124}
125
126BOOST_AUTO_TEST_CASE( TestBuildSheetListWithMultipleRoots )
127{
128 SCHEMATIC schematic( nullptr );
129
130 SCH_SHEET* top1 = createTopLevelSheet( schematic, "TopSheet1", "top_sheet_1.kicad_sch" );
131 SCH_SHEET* top2 = createTopLevelSheet( schematic, "TopSheet2", "top_sheet_2.kicad_sch" );
132
133 SCH_SHEET* child1 = createTopLevelSheet( schematic, "ChildSheet1", "child_sheet_1.kicad_sch" );
134 child1->SetParent( top1 );
135 top1->GetScreen()->Append( child1 );
136
137 SCH_SHEET* child2 = createTopLevelSheet( schematic, "ChildSheet2", "child_sheet_2.kicad_sch" );
138 child2->SetParent( top2 );
139 top2->GetScreen()->Append( child2 );
140
141 schematic.SetTopLevelSheets( { top1, top2 } );
142
143 // Build sheet list
144 SCH_SHEET_LIST sheetList = schematic.Hierarchy();
145
146 // Should have 4 sheet paths: 2 top-level + 2 children
147 BOOST_CHECK_EQUAL( sheetList.size(), 4 );
148}
149
150
151BOOST_AUTO_TEST_CASE( TestHierarchyUpdatesOnSheetOperations )
152{
153 SCHEMATIC schematic( nullptr );
154
155 SCH_SHEET* baseSheet = createTopLevelSheet( schematic, "Base", "base.kicad_sch" );
156 schematic.SetTopLevelSheets( { baseSheet } );
157
158 BOOST_CHECK_EQUAL( schematic.Hierarchy().size(), 1 );
159
160 SCH_SHEET* copiedSheet = createTopLevelSheet( schematic, "Copy", "copy.kicad_sch" );
161 schematic.AddTopLevelSheet( copiedSheet );
162
163 BOOST_CHECK_EQUAL( schematic.Hierarchy().size(), 2 );
164
165 // Move the copied sheet ahead of the base sheet
166 schematic.SetTopLevelSheets( { copiedSheet, baseSheet } );
167 BOOST_CHECK_EQUAL( schematic.GetTopLevelSheet()->GetName(), "Copy" );
168 BOOST_CHECK_EQUAL( schematic.Hierarchy().size(), 2 );
169
170 // Removing one sheet should keep hierarchy valid
171 BOOST_CHECK( schematic.RemoveTopLevelSheet( baseSheet ) );
172 BOOST_CHECK_EQUAL( schematic.Hierarchy().size(), 1 );
173 BOOST_CHECK_EQUAL( schematic.GetTopLevelSheet()->GetName(), "Copy" );
174}
175
176BOOST_AUTO_TEST_CASE( TestTopLevelSheetInfoSerialization )
177{
178 TOP_LEVEL_SHEET_INFO info1( KIID(), "TestSheet", "test_sheet.kicad_sch" );
179
180 // Verify members
181 BOOST_CHECK_EQUAL( info1.name, "TestSheet" );
182 BOOST_CHECK_EQUAL( info1.filename, "test_sheet.kicad_sch" );
183
184 // Test equality
185 TOP_LEVEL_SHEET_INFO info2( info1.uuid, "TestSheet", "test_sheet.kicad_sch" );
186 BOOST_CHECK( info1 == info2 );
187
188 // Test inequality
189 TOP_LEVEL_SHEET_INFO info3( KIID(), "OtherSheet", "other_sheet.kicad_sch" );
190 BOOST_CHECK( info1 != info3 );
191}
192
const KIID m_Uuid
Definition eda_item.h:522
virtual void SetParent(EDA_ITEM *aParent)
Definition eda_item.h:113
Definition kiid.h:49
Holds all the data relating to one schematic.
Definition schematic.h:88
void AddTopLevelSheet(SCH_SHEET *aSheet)
Add a new top-level sheet to the schematic.
SCH_SHEET_LIST Hierarchy() const
Return the full schematic flattened hierarchical sheet list.
SCH_SHEET * GetTopLevelSheet(int aIndex=0) const
bool RemoveTopLevelSheet(SCH_SHEET *aSheet)
Remove a top-level sheet from the schematic.
void SetTopLevelSheets(const std::vector< SCH_SHEET * > &aSheets)
std::vector< SCH_SHEET * > GetTopLevelSheets() const
Get the list of top-level sheets.
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
const KIID & GetUuid() const
Definition sch_screen.h:531
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:48
void SetFileName(const wxString &aFilename)
Definition sch_sheet.h:382
wxString GetName() const
Definition sch_sheet.h:142
void SetName(const wxString &aName)
Definition sch_sheet.h:143
SCH_SCREEN * GetScreen() const
Definition sch_sheet.h:145
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
KIID niluuid(0)
see class PGM_BASE
Information about a top-level schematic sheet.
KIID uuid
Unique identifier for the sheet.
wxString name
Display name for the sheet.
wxString filename
Relative path to the sheet file.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
static SCH_SHEET * createTopLevelSheet(SCHEMATIC &aSchematic, const wxString &aName, const wxString &aFileName)
BOOST_AUTO_TEST_CASE(TestVirtualRootCreation)
BOOST_CHECK_EQUAL(result, "25.4")