KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_issue24870_top_level_sheet_delete.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 3
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
31
33
34#include <schematic.h>
35#include <schematic_file_util.h>
36#include <sch_line.h>
37#include <sch_screen.h>
38#include <sch_sheet.h>
39#include <sch_sheet_path.h>
40#include <sch_text.h>
42#include <project.h>
43#include <locale_io.h>
44
45#include <wx/filename.h>
46
47#include <fstream>
48
49
51{
53 {
54 m_settingsManager.LoadProject( "" );
55 m_schematic = std::make_unique<SCHEMATIC>( &m_settingsManager.Prj() );
56 m_schematic->Reset();
57
58 // Reset() leaves one empty top-level sheet behind; that is the reporter's first page
59 m_page1 = m_schematic->GetTopLevelSheet( 0 );
61 BOOST_REQUIRE( m_page1->GetScreen() );
62
63 m_page1->GetScreen()->SetFileName( wxT( "page1.kicad_sch" ) );
64
65 SCH_LINE* wire = new SCH_LINE( VECTOR2I( 0, 0 ), LAYER_WIRE );
66 wire->SetEndPoint( VECTOR2I( 10000, 0 ) );
67 m_page1->GetScreen()->Append( wire );
68
69 SCH_TEXT* text = new SCH_TEXT( VECTOR2I( 0, 5000 ), wxT( "PAGE1 CONTENT" ) );
70 m_page1->GetScreen()->Append( text );
71 }
72
75 {
76 SCH_SHEET* page2 = new SCH_SHEET( m_schematic.get() );
77 SCH_SCREEN* screen = new SCH_SCREEN( m_schematic.get() );
78
79 page2->SetScreen( screen );
80 page2->SetName( wxT( "Untitled" ) );
81 screen->SetFileName( wxT( "untitled.kicad_sch" ) );
82
83 m_schematic->AddTopLevelSheet( page2 );
84
85 SCH_SHEET_PATH page2Path;
86 page2Path.push_back( page2 );
87 page2Path.SetPageNumber( wxT( "2" ) );
88
89 return page2;
90 }
91
93 std::unique_ptr<SCHEMATIC> m_schematic;
94 SCH_SHEET* m_page1 = nullptr;
95};
96
97
98BOOST_FIXTURE_TEST_SUITE( Issue24870TopLevelSheetDelete, ISSUE24870_FIXTURE )
99
100
101
105BOOST_AUTO_TEST_CASE( DeleteDisplayedPageRetargetsCurrentSheet )
106{
107 SCH_SHEET* page2 = addSecondPage();
108 SCH_SCREEN* page1Screen = m_page1->GetScreen();
109 size_t itemCount = page1Screen->Items().size();
110
111 // The reporter deletes the second page while looking at it
112 SCH_SHEET_PATH page2Path;
113 page2Path.push_back( page2 );
114 m_schematic->SetCurrentSheet( page2Path );
115
116 BOOST_REQUIRE( m_schematic->RemoveTopLevelSheet( page2 ) );
117
118 BOOST_REQUIRE( !m_schematic->CurrentSheet().empty() );
119 BOOST_CHECK( m_schematic->IsTopLevelSheet( m_schematic->CurrentSheet().at( 0 ) ) );
120 BOOST_CHECK( m_schematic->CurrentSheet().LastScreen() == page1Screen );
121
122 // The path the editor is about to draw has to be one the hierarchy still knows about
123 BOOST_CHECK( m_schematic->Hierarchy().HasPath( m_schematic->CurrentSheet().Path() ) );
124
125 BOOST_CHECK_EQUAL( page1Screen->Items().size(), itemCount );
126}
127
128
132BOOST_AUTO_TEST_CASE( DeletePageKeepsOtherPageContentOnDisk )
133{
135 SCH_SHEET* page2 = addSecondPage();
136 SCH_SCREEN* page1Screen = m_page1->GetScreen();
137
138 SCH_SHEET_PATH page2Path;
139 page2Path.push_back( page2 );
140 m_schematic->SetCurrentSheet( page2Path );
141
142 BOOST_REQUIRE( m_schematic->RemoveTopLevelSheet( page2 ) );
143
144 BOOST_CHECK_EQUAL( page1Screen->Items().size(), size_t( 2 ) );
145
146 wxString tmpName = wxFileName::CreateTempFileName( wxT( "issue24870" ) );
147 std::string path = ( tmpName + wxT( ".kicad_sch" ) ).ToStdString();
148
149 KI_TEST::DumpSchematicToFile( *m_schematic, *m_page1, path );
150
151 std::ifstream stream( path );
152 BOOST_REQUIRE( stream.is_open() );
153
154 std::unique_ptr<SCHEMATIC> reloaded =
155 KI_TEST::ReadSchematicFromStream( stream, &m_settingsManager.Prj() );
156
157 BOOST_REQUIRE( reloaded );
158 BOOST_REQUIRE( reloaded->RootScreen() );
159
160 int wires = 0;
161 int texts = 0;
162
163 for( SCH_ITEM* item : reloaded->RootScreen()->Items() )
164 {
165 if( item->Type() == SCH_LINE_T && static_cast<SCH_LINE*>( item )->IsWire() )
166 wires++;
167 else if( item->Type() == SCH_TEXT_T )
168 texts++;
169 }
170
171 BOOST_CHECK_EQUAL( wires, 1 );
172 BOOST_CHECK_EQUAL( texts, 1 );
173
174 stream.close();
175 wxRemoveFile( tmpName );
176 wxRemoveFile( path );
177}
178
179
size_t size() const
Return the number of items in the tree.
Definition sch_rtree.h:177
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:37
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:165
Segment description base class to describe items which have 2 end points (track, wire,...
Definition sch_line.h:39
bool IsWire() const
Return true if the line is a wire.
void SetEndPoint(const VECTOR2I &aPosition)
Definition sch_line.h:146
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:118
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
void SetPageNumber(const wxString &aPageNumber)
Set the sheet instance user definable page number.
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
void SetName(const wxString &aName)
Definition sch_sheet.h:143
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
static std::string ToStdString(const wxString &aStr)
@ LAYER_WIRE
Definition layer_ids.h:474
std::unique_ptr< SCHEMATIC > ReadSchematicFromStream(std::istream &aStream, PROJECT *aProject)
void DumpSchematicToFile(SCHEMATIC &aSchematic, SCH_SHEET &aSheet, const std::string &aFilename)
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
std::vector< FAB_LAYER_COLOR > dummy
SCH_SHEET * addSecondPage()
Add a second top-level sheet the way HIERARCHY_PANE's "New Top-Level Sheet" does.
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(DeleteDisplayedPageRetargetsCurrentSheet)
Deleting the top-level sheet that is being displayed must retarget the current sheet onto a page that...
std::string path
BOOST_CHECK_EQUAL(result, "25.4")
@ SCH_LINE_T
Definition typeinfo.h:159
@ SCH_TEXT_T
Definition typeinfo.h:147
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683