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
34BOOST_AUTO_TEST_SUITE( MultiTopLevelSheets )
35
36BOOST_AUTO_TEST_CASE( TestVirtualRootCreation )
37{
38 // Create a virtual root
39 SCH_SHEET virtualRoot;
40 const_cast<KIID&>( virtualRoot.m_Uuid ) = niluuid;
41 virtualRoot.SetScreen( nullptr );
42
43 // Verify it's recognized as virtual root
44 BOOST_CHECK( virtualRoot.m_Uuid == niluuid );
45 BOOST_CHECK( virtualRoot.GetScreen() == nullptr );
46}
47
48BOOST_AUTO_TEST_CASE( TestAddTopLevelSheet )
49{
50 SCHEMATIC schematic( nullptr );
51
52 // Create a virtual root
53 SCH_SHEET* virtualRoot = new SCH_SHEET();
54 const_cast<KIID&>( virtualRoot->m_Uuid ) = niluuid;
55 virtualRoot->SetScreen( nullptr );
56 virtualRoot->SetParent( &schematic );
57 schematic.SetRoot( virtualRoot );
58
59 // Create and add a top-level sheet
60 SCH_SHEET* sheet1 = new SCH_SHEET();
61 sheet1->SetName( "Sheet1" );
62 SCH_SCREEN* screen1 = new SCH_SCREEN();
63 screen1->SetParent( &schematic );
64 sheet1->SetScreen( screen1 );
65 sheet1->SetParent( &schematic );
66
67 schematic.AddTopLevelSheet( sheet1 );
68
69 // Verify the sheet was added
70 const std::vector<SCH_SHEET*>& topSheets = schematic.GetTopLevelSheets();
71 BOOST_CHECK_EQUAL( topSheets.size(), 1 );
72 BOOST_CHECK_EQUAL( topSheets[0], sheet1 );
73 BOOST_CHECK_EQUAL( topSheets[0]->GetName(), "Sheet1" );
74}
75
76BOOST_AUTO_TEST_CASE( TestAddMultipleTopLevelSheets )
77{
78 SCHEMATIC schematic( nullptr );
79
80 // Create a virtual root
81 SCH_SHEET* virtualRoot = new SCH_SHEET();
82 const_cast<KIID&>( virtualRoot->m_Uuid ) = niluuid;
83 virtualRoot->SetScreen( nullptr );
84 virtualRoot->SetParent( &schematic );
85 schematic.SetRoot( virtualRoot );
86
87 // Create and add multiple top-level sheets
88 for( int i = 1; i <= 3; i++ )
89 {
90 SCH_SHEET* sheet = new SCH_SHEET();
91 sheet->SetName( wxString::Format( "Sheet%d", i ) );
92 SCH_SCREEN* screen = new SCH_SCREEN();
93 screen->SetParent( &schematic );
94 sheet->SetScreen( screen );
95 sheet->SetParent( &schematic );
96 schematic.AddTopLevelSheet( sheet );
97 }
98
99 // Verify all sheets were added
100 const std::vector<SCH_SHEET*>& topSheets = schematic.GetTopLevelSheets();
101 BOOST_CHECK_EQUAL( topSheets.size(), 3 );
102 BOOST_CHECK_EQUAL( topSheets[0]->GetName(), "Sheet1" );
103 BOOST_CHECK_EQUAL( topSheets[1]->GetName(), "Sheet2" );
104 BOOST_CHECK_EQUAL( topSheets[2]->GetName(), "Sheet3" );
105}
106
107BOOST_AUTO_TEST_CASE( TestRemoveTopLevelSheet )
108{
109 SCHEMATIC schematic( nullptr );
110
111 // Create a virtual root
112 SCH_SHEET* virtualRoot = new SCH_SHEET();
113 const_cast<KIID&>( virtualRoot->m_Uuid ) = niluuid;
114 virtualRoot->SetScreen( nullptr );
115 virtualRoot->SetParent( &schematic );
116 schematic.SetRoot( virtualRoot );
117
118 // Create and add sheets
119 SCH_SHEET* sheet1 = new SCH_SHEET();
120 sheet1->SetName( "Sheet1" );
121 SCH_SCREEN* screen1 = new SCH_SCREEN();
122 screen1->SetParent( &schematic );
123 sheet1->SetScreen( screen1 );
124 sheet1->SetParent( &schematic );
125 schematic.AddTopLevelSheet( sheet1 );
126
127 SCH_SHEET* sheet2 = new SCH_SHEET();
128 sheet2->SetName( "Sheet2" );
129 SCH_SCREEN* screen2 = new SCH_SCREEN();
130 screen2->SetParent( &schematic );
131 sheet2->SetScreen( screen2 );
132 sheet2->SetParent( &schematic );
133 schematic.AddTopLevelSheet( sheet2 );
134
135 // Remove first sheet
136 schematic.RemoveTopLevelSheet( sheet1 );
137
138 // Verify only sheet2 remains
139 const std::vector<SCH_SHEET*>& topSheets = schematic.GetTopLevelSheets();
140 BOOST_CHECK_EQUAL( topSheets.size(), 1 );
141 BOOST_CHECK_EQUAL( topSheets[0], sheet2 );
142 BOOST_CHECK_EQUAL( topSheets[0]->GetName(), "Sheet2" );
143}
144
145BOOST_AUTO_TEST_CASE( TestBuildSheetListWithMultipleRoots )
146{
147 SCHEMATIC schematic( nullptr );
148
149 // Create a virtual root
150 SCH_SHEET* virtualRoot = new SCH_SHEET();
151 const_cast<KIID&>( virtualRoot->m_Uuid ) = niluuid;
152 virtualRoot->SetScreen( nullptr );
153 virtualRoot->SetParent( &schematic );
154 schematic.SetRoot( virtualRoot );
155
156 // Create two top-level sheets, each with one child
157 for( int i = 1; i <= 2; i++ )
158 {
159 SCH_SHEET* topSheet = new SCH_SHEET();
160 topSheet->SetName( wxString::Format( "TopSheet%d", i ) );
161 topSheet->SetFileName( wxString::Format( "top_sheet_%d.kicad_sch", i ) );
162 SCH_SCREEN* topScreen = new SCH_SCREEN();
163 topScreen->SetParent( &schematic );
164 topSheet->SetScreen( topScreen );
165 topSheet->SetParent( &schematic );
166
167 // Add a child sheet
168 SCH_SHEET* childSheet = new SCH_SHEET();
169 childSheet->SetName( wxString::Format( "ChildSheet%d", i ) );
170 childSheet->SetFileName( wxString::Format( "child_sheet_%d.kicad_sch", i ) );
171 SCH_SCREEN* childScreen = new SCH_SCREEN();
172 childScreen->SetParent( &schematic );
173 childSheet->SetScreen( childScreen );
174 childSheet->SetParent( &schematic );
175 topScreen->Append( childSheet );
176
177 schematic.AddTopLevelSheet( topSheet );
178 }
179
180 // Build sheet list
181 schematic.RefreshHierarchy();
182 SCH_SHEET_LIST sheetList = schematic.Hierarchy();
183
184 // Should have 4 sheet paths: 2 top-level + 2 children
185 BOOST_CHECK_EQUAL( sheetList.size(), 4 );
186}
187
188BOOST_AUTO_TEST_CASE( TestTopLevelSheetInfoSerialization )
189{
190 TOP_LEVEL_SHEET_INFO info1( KIID(), "TestSheet", "test_sheet.kicad_sch" );
191
192 // Verify members
193 BOOST_CHECK_EQUAL( info1.name, "TestSheet" );
194 BOOST_CHECK_EQUAL( info1.filename, "test_sheet.kicad_sch" );
195
196 // Test equality
197 TOP_LEVEL_SHEET_INFO info2( info1.uuid, "TestSheet", "test_sheet.kicad_sch" );
198 BOOST_CHECK( info1 == info2 );
199
200 // Test inequality
201 TOP_LEVEL_SHEET_INFO info3( KIID(), "OtherSheet", "other_sheet.kicad_sch" );
202 BOOST_CHECK( info1 != info3 );
203}
204
const KIID m_Uuid
Definition eda_item.h:516
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.
bool RemoveTopLevelSheet(SCH_SHEET *aSheet)
Remove a top-level sheet from the schematic.
void SetRoot(SCH_SHEET *aRootSheet)
Initialize the schematic with a new root sheet.
std::vector< SCH_SHEET * > GetTopLevelSheets() const
Get the list of top-level sheets.
void RefreshHierarchy()
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
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:47
void SetFileName(const wxString &aFilename)
Definition sch_sheet.h:338
void SetName(const wxString &aName)
Definition sch_sheet.h:114
SCH_SCREEN * GetScreen() const
Definition sch_sheet.h:116
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()
BOOST_AUTO_TEST_CASE(TestVirtualRootCreation)
BOOST_CHECK_EQUAL(result, "25.4")