KiCad PCB EDA Suite
test_sch_sheet_list.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 (C) 2020 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 
21 #include "eeschema_test_utils.h"
22 
23 #include <sch_io_mgr.h>
24 #include <sch_screen.h>
25 #include <sch_sheet_path.h>
26 #include <schematic.h>
29 
30 
32 {
33 public:
35  m_schematic( nullptr ),
36  m_manager( true )
37  {
38  m_pi = SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD );
39  }
40 
42  {
44  delete m_pi;
45  }
46 
47  void loadSchematic( const wxString& aRelativePath );
48 
49  wxFileName buildFullPath( const wxString& aRelativePath );
50 
53 
55 
57 };
58 
59 
60 void TEST_SCH_SHEET_LIST_FIXTURE::loadSchematic( const wxString& aRelativePath )
61 {
62  wxFileName fn = buildFullPath( aRelativePath );
63 
64  BOOST_TEST_MESSAGE( fn.GetFullPath() );
65 
66  wxFileName pro( fn );
67  pro.SetExt( ProjectFileExtension );
68 
71 
72  m_manager.LoadProject( pro.GetFullPath() );
74 
76 
77  m_schematic.SetRoot( m_pi->Load( fn.GetFullPath(), &m_schematic ) );
78 
79  BOOST_REQUIRE_EQUAL( m_pi->GetError().IsEmpty(), true );
80 
82 
83  SCH_SCREENS screens( m_schematic.Root() );
84 
85  for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
86  screen->UpdateLocalLibSymbolLinks();
87 
89 
90  // Restore all of the loaded symbol instances from the root sheet screen.
93 
94  sheets.AnnotatePowerSymbols();
95 
96  // NOTE: This is required for multi-unit symbols to be correct
97  // Normally called from SCH_EDIT_FRAME::FixupJunctions() but could be refactored
98  for( SCH_SHEET_PATH& sheet : sheets )
99  sheet.UpdateAllScreenReferences();
100 }
101 
102 
103 wxFileName TEST_SCH_SHEET_LIST_FIXTURE::buildFullPath( const wxString& aRelativePath )
104 {
105  wxFileName fn = KI_TEST::GetEeschemaTestDataDir();
106  fn.AppendDir( "netlists" );
107 
108  wxString path = fn.GetFullPath();
109  path += aRelativePath + wxT( "." ) + KiCadSchematicFileExtension;
110 
111  return wxFileName( path );
112 }
113 
114 
115 BOOST_FIXTURE_TEST_SUITE( SchSheetList, TEST_SCH_SHEET_LIST_FIXTURE )
116 
117 
118 BOOST_AUTO_TEST_CASE( TestSheetListPageProperties )
119 {
120  loadSchematic( "complex_hierarchy/complex_hierarchy" );
121 
122  SCH_SHEET_LIST sheets = m_schematic.GetSheets();
123 
125 
126  sheets.SetInitialPageNumbers();
127 
128  // The root sheet should now be page 1.
129  BOOST_CHECK_EQUAL( sheets.at( 0 ).GetPageNumber(), "1" );
130  BOOST_CHECK_EQUAL( sheets.at( 1 ).GetPageNumber(), "2" );
131  BOOST_CHECK_EQUAL( sheets.at( 2 ).GetPageNumber(), "3" );
132 }
133 
134 
135 BOOST_AUTO_TEST_CASE( TestEditPageNumbersInSharedDesign )
136 {
137  BOOST_TEST_CONTEXT( "Read Sub-Sheet, prior to modification" )
138  {
139  // Check the Sub Sheet has the expected page numbers
140  loadSchematic( "complex_hierarchy_shared/ampli_ht/ampli_ht" );
141 
142  SCH_SHEET_LIST sheets = m_schematic.GetSheets();
143 
144  BOOST_CHECK_EQUAL( sheets.size(), 2 );
145  BOOST_CHECK_EQUAL( sheets.at( 0 ).GetPageNumber(), "i" );
146  BOOST_CHECK_EQUAL( sheets.at( 1 ).GetPageNumber(), "ii" );
147  }
148 
149  BOOST_TEST_CONTEXT( "Read Root Sheet, prior to modification" )
150  {
151  // Check the parent sheet has the expected page numbers
152  loadSchematic( "complex_hierarchy_shared/complex_hierarchy" );
153 
154  SCH_SHEET_LIST sheets = m_schematic.GetSheets();
155 
156  BOOST_CHECK_EQUAL( sheets.size(), 5 );
157  BOOST_CHECK_EQUAL( sheets.at( 0 ).GetPageNumber(), "1" );
158  BOOST_CHECK_EQUAL( sheets.at( 1 ).GetPageNumber(), "2" );
159  BOOST_CHECK_EQUAL( sheets.at( 2 ).GetPageNumber(), "3" );
160  BOOST_CHECK_EQUAL( sheets.at( 3 ).GetPageNumber(), "4" );
161  BOOST_CHECK_EQUAL( sheets.at( 4 ).GetPageNumber(), "5" );
162  }
163 
164  BOOST_TEST_CONTEXT( "Modify page numbers in root sheet" )
165  {
166  SCH_SHEET_LIST sheets = m_schematic.GetSheets();
167 
168  // Amend Page numbers
169  sheets.at( 0 ).SetPageNumber( "A" );
170  sheets.at( 1 ).SetPageNumber( "B" );
171  sheets.at( 2 ).SetPageNumber( "C" );
172  sheets.at( 3 ).SetPageNumber( "D" );
173  sheets.at( 4 ).SetPageNumber( "E" );
174 
175  // Save and reload
176  wxString tempName = "complex_hierarchy_shared/complex_hierarchy_modified";
177  wxFileName tempFn = buildFullPath( tempName );
178  m_pi->Save( tempFn.GetFullPath(), &m_schematic.Root(), &m_schematic );
179  loadSchematic( tempName );
180 
181  sheets = m_schematic.GetSheets();
182 
183  BOOST_CHECK_EQUAL( sheets.size(), 5 );
184  BOOST_CHECK_EQUAL( sheets.at( 0 ).GetPageNumber(), "A" );
185  BOOST_CHECK_EQUAL( sheets.at( 1 ).GetPageNumber(), "B" );
186  BOOST_CHECK_EQUAL( sheets.at( 2 ).GetPageNumber(), "C" );
187  BOOST_CHECK_EQUAL( sheets.at( 3 ).GetPageNumber(), "D" );
188  BOOST_CHECK_EQUAL( sheets.at( 4 ).GetPageNumber(), "E" );
189 
190  // Cleanup
191  wxRemoveFile( tempFn.GetFullPath() );
192  }
193 
194  BOOST_TEST_CONTEXT( "Read Sub-Sheet, after modification" )
195  {
196  // Check the Sub Sheet has the expected page numbers
197  // (This should not have been modified after editing the root sheet)
198  loadSchematic( "complex_hierarchy_shared/ampli_ht/ampli_ht" );
199 
200  SCH_SHEET_LIST sheets = m_schematic.GetSheets();
201 
202  BOOST_CHECK_EQUAL( sheets.size(), 2 );
203  BOOST_CHECK_EQUAL( sheets.at( 0 ).GetPageNumber(), "i" );
204  BOOST_CHECK_EQUAL( sheets.at( 1 ).GetPageNumber(), "ii" );
205  }
206 }
207 
208 BOOST_AUTO_TEST_SUITE_END()
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
Holds all the data relating to one schematic.
Definition: schematic.h:59
const std::string ProjectFileExtension
void push_back(SCH_SHEET *aSheet)
Forwarded method from std::vector.
virtual SCH_SHEET * Load(const wxString &aFileName, SCHEMATIC *aSchematic, SCH_SHEET *aAppendToMe=nullptr, const PROPERTIES *aProperties=nullptr)
Load information from some input file format that this SCH_PLUGIN implementation knows about,...
Definition: sch_plugin.cpp:51
const std::vector< SYMBOL_INSTANCE_REFERENCE > & GetSymbolInstances() const
Definition: sch_screen.h:453
void UpdateSheetInstances(const std::vector< SCH_SHEET_INSTANCE > &aSheetInstances)
Update all of the sheet instance information using aSheetInstances.
BOOST_CHECK(v2.Cross(v1)==1)
virtual void SetElem(ELEM_T aIndex, _ELEM *aElem)
Definition: project.cpp:259
void SetRoot(SCH_SHEET *aRootSheet)
Initialize the schematic with a new root sheet.
Definition: schematic.cpp:104
Base class that schematic file and library loading and saving plugins should derive from.
Definition: sch_io_mgr.h:151
SCHEMATIC m_schematic
Schematic to load
const std::vector< SCH_SHEET_INSTANCE > & GetSheetInstances() const
Definition: sch_screen.h:458
wxFileName buildFullPath(const wxString &aRelativePath)
Definition of file extensions used in Kicad.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
#define BOOST_TEST_CONTEXT(A)
void SetProject(PROJECT *aPrj)
Definition: schematic.cpp:76
void clear()
Forwarded method from std::vector.
void UpdateSymbolInstances(const std::vector< SYMBOL_INSTANCE_REFERENCE > &aSymbolInstances)
Update all of the symbol instance information using aSymbolInstances.
wxFileName GetEeschemaTestDataDir()
Get the configured location of Eeschema test data.
bool LoadProject(const wxString &aFullPath, bool aSetActive=true)
Loads a project or sets up a new project with a specified path.
void Reset()
Initialize this schematic to a blank one, unloading anything existing.
Definition: schematic.cpp:51
SCH_SHEET_LIST GetSheets() const override
Builds and returns an updated schematic hierarchy TODO: can this be cached?
Definition: schematic.h:87
bool AllSheetPageNumbersEmpty() const
Check all of the sheet instance for empty page numbers.
SCH_SHEET & Root() const
Definition: schematic.h:92
virtual const wxString & GetError() const
Return an error string to the caller.
Definition: sch_plugin.cpp:177
const std::string KiCadSchematicFileExtension
BOOST_AUTO_TEST_CASE(TestSheetListPageProperties)
void AnnotatePowerSymbols()
Silently annotate the not yet annotated power symbols of the entire hierarchy of the sheet path list.
SCH_SCREEN * RootScreen() const
Helper to retrieve the screen of the root sheet.
Definition: schematic.cpp:117
SCH_SHEET_PATH & CurrentSheet() const override
Definition: schematic.h:119
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
void SetInitialPageNumbers()
Set initial sheet page numbers.
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition: sch_screen.h:551
void loadSchematic(const wxString &aRelativePath)