KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_sch_item_swap_group.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, see <https://www.gnu.org/licenses/>.
18 */
19
33
34#include <boost/test/unit_test.hpp>
35
36#include <sch_group.h>
37#include <sch_line.h>
38#include <sch_screen.h>
39#include <schematic.h>
41
43
44#include <wx/filename.h>
45#include <wx/stdpaths.h>
46
47#include <memory>
48
49
51{
53 {
54 wxString tempDir = wxStandardPaths::Get().GetTempDir();
55 wxString projectPath = tempDir + wxFileName::GetPathSeparator() + wxT( "test_sch_item_swap_group.kicad_pro" );
56 m_tempFiles.push_back( projectPath );
57
58 m_settingsManager.LoadProject( projectPath.ToStdString() );
59 m_schematic = std::make_unique<SCHEMATIC>( nullptr );
60 m_schematic->SetProject( &m_settingsManager.Prj() );
61 m_schematic->CreateDefaultScreens();
62 }
63
65 {
66 m_schematic.reset();
67
68 for( const wxString& file : m_tempFiles )
69 {
70 if( wxFileExists( file ) )
71 wxRemoveFile( file );
72 }
73 }
74
76 std::unique_ptr<SCHEMATIC> m_schematic;
77 std::vector<wxString> m_tempFiles;
78};
79
80
81BOOST_FIXTURE_TEST_SUITE( SchItemSwapGroup, SCH_ITEM_SWAP_GROUP_FIXTURE )
82
83
84BOOST_AUTO_TEST_CASE( SwapItemDataKeepsGroupMembership )
85{
86 SCH_SCREEN* screen = m_schematic->GetTopLevelSheets()[0]->GetScreen();
87 BOOST_REQUIRE( screen );
88
89 SCH_LINE* wire = new SCH_LINE( VECTOR2I( 0, 0 ), LAYER_WIRE );
90 wire->SetEndPoint( VECTOR2I( 100000, 0 ) );
91 screen->Append( wire );
92
93 SCH_GROUP* group = new SCH_GROUP( screen );
94 group->AddItem( wire );
95 screen->Append( group );
96
98 BOOST_REQUIRE( group->GetItems().count( wire ) == 1 );
99
100 // The undo/cancel machinery holds a copy of the item that carries its data but no group
101 // membership. Give it different geometry so we can confirm the data swap actually happened.
102 std::unique_ptr<SCH_LINE> image( static_cast<SCH_LINE*>( wire->Clone() ) );
103 image->SetParentGroup( nullptr );
104 image->SetEndPoint( VECTOR2I( 200000, 0 ) );
105
106 wire->SwapItemData( image.get() );
107
108 // The item data must have been swapped in.
109 BOOST_CHECK( wire->GetEndPoint() == VECTOR2I( 200000, 0 ) );
110
111 // But the wire must still belong to its group. Swapping m_group here orphaned it from the
112 // group while the group still listed it, which is the corruption this test guards against.
113 BOOST_CHECK( wire->GetParentGroup() == group );
114 BOOST_CHECK( group->GetItems().count( wire ) == 1 );
115}
116
117
virtual EDA_GROUP * GetParentGroup() const
Definition eda_item.h:114
A set of SCH_ITEMs (i.e., without duplicates).
Definition sch_group.h:48
void SwapItemData(SCH_ITEM *aImage)
Swap data between aItem and aImage.
Definition sch_item.cpp:632
Segment description base class to describe items which have 2 end points (track, wire,...
Definition sch_line.h:38
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition sch_line.cpp:198
VECTOR2I GetEndPoint() const
Definition sch_line.h:144
void SetEndPoint(const VECTOR2I &aPosition)
Definition sch_line.h:145
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
@ LAYER_WIRE
Definition layer_ids.h:450
Class to handle a set of SCH_ITEMs.
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()
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683