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