KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_rc_tree_model.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
20#include <boost/test/unit_test.hpp>
21
22#include <memory>
23
24#include <rc_item.h>
25#include <marker_base.h>
26
27
29{
30public:
32 RC_TREE_MODEL( nullptr, nullptr )
33 {
34 }
35
37 {
38 auto rcItem = std::make_shared<RC_ITEM>();
39 rcItem->SetItems( KIID(), niluuid, niluuid, niluuid );
40
41 auto marker = createNode( nullptr, rcItem, RC_TREE_NODE::MARKER );
42 auto child = createNode( marker, rcItem, RC_TREE_NODE::MAIN_ITEM );
43
44 marker->m_Children.push_back( child );
45 m_tree.push_back( marker );
46 return marker;
47 }
48
49 RC_TREE_NODE* AddItemlessMarker( const std::shared_ptr<RC_ITEM>& aRcItem )
50 {
51 auto marker = createNode( nullptr, aRcItem, RC_TREE_NODE::MARKER );
52 m_tree.push_back( marker );
53 return marker;
54 }
55
57 {
58 std::erase( m_tree, aNode );
59 retireNodeTree( aNode );
60 deleteNodeTree( aNode );
61 }
62};
63
64
65// Concrete marker so the rc-item has a parent.
67{
68public:
69 TEST_MARKER( std::shared_ptr<RC_ITEM> aItem ) :
70 MARKER_BASE( 0, std::move( aItem ) )
71 {
72 }
73
74 const KIID GetUUID() const override { return m_uuid; }
75 KIGFX::COLOR4D getColor() const override { return KIGFX::COLOR4D(); }
76
78};
79
80
81BOOST_AUTO_TEST_SUITE( RCTreeModel )
82
83
84BOOST_AUTO_TEST_CASE( DetachedHandlesBehaveAsInvalid )
85{
87 RC_TREE_NODE* marker = model.AddMarkerWithMainItem();
88 RC_TREE_NODE* child = marker->m_Children.front();
89
90 const wxDataViewItem staleMarker = RC_TREE_MODEL::ToItem( marker );
91 const wxDataViewItem staleChild = RC_TREE_MODEL::ToItem( child );
92
93 model.DetachTopLevel( marker );
94
95 wxDataViewItemArray children;
96
97 BOOST_CHECK( !model.IsContainer( staleMarker ) );
98 BOOST_CHECK( !model.GetParent( staleChild ).IsOk() );
99 BOOST_CHECK_EQUAL( model.GetChildren( staleMarker, children ), 0U );
100 BOOST_CHECK( children.empty() );
101 BOOST_CHECK( RC_TREE_MODEL::ToUUID( staleChild ) == niluuid );
102}
103
104
105BOOST_AUTO_TEST_CASE( ValueChangedItemlessMarkerDoesNotCrash )
106{
108
109 auto rcItem = std::make_shared<RC_ITEM>(); // no items -> marker node has no children
110 TEST_MARKER marker( rcItem );
111 rcItem->SetParent( &marker );
112
113 RC_TREE_NODE* node = model.AddItemlessMarker( rcItem );
114 BOOST_REQUIRE( node->m_Children.empty() );
115
116 // Used to call vector::back() on the empty child list here and crash.
117 model.ValueChanged( node );
118
119 // Excluding with a comment must append a comment node even though there were no children.
120 marker.SetExcluded( true, wxT( "excluded" ) );
121 model.ValueChanged( node );
122
123 BOOST_REQUIRE_EQUAL( node->m_Children.size(), 1u );
124 BOOST_CHECK( node->m_Children[0]->m_Type == RC_TREE_NODE::COMMENT );
125}
126
127
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
Definition kiid.h:44
void SetExcluded(bool aExcluded, const wxString &aComment=wxEmptyString)
Definition marker_base.h:90
MARKER_BASE(int aScalingFactor, std::shared_ptr< RC_ITEM > aItem, MARKER_T aType=MARKER_UNSPEC)
std::vector< RC_TREE_NODE * > m_tree
Definition rc_item.h:370
void retireNodeTree(RC_TREE_NODE *aNode)
Definition rc_item.cpp:276
static wxDataViewItem ToItem(RC_TREE_NODE const *aNode)
Definition rc_item.h:262
RC_TREE_MODEL(EDA_DRAW_FRAME *aParentFrame, wxDataViewCtrl *aView)
Definition rc_item.cpp:253
void deleteNodeTree(RC_TREE_NODE *aNode)
Definition rc_item.cpp:289
RC_TREE_NODE * createNode(RC_TREE_NODE *aParent, const std::shared_ptr< RC_ITEM > &aRcItem, RC_TREE_NODE::NODE_TYPE aType)
Definition rc_item.cpp:262
static KIID ToUUID(wxDataViewItem aItem)
Definition rc_item.cpp:223
std::vector< RC_TREE_NODE * > m_Children
Definition rc_item.h:255
const KIID GetUUID() const override
TEST_MARKER(std::shared_ptr< RC_ITEM > aItem)
KIGFX::COLOR4D getColor() const override
RC_TREE_NODE * AddItemlessMarker(const std::shared_ptr< RC_ITEM > &aRcItem)
RC_TREE_NODE * AddMarkerWithMainItem()
void DetachTopLevel(RC_TREE_NODE *aNode)
KIID niluuid(0)
STL namespace.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
KIBIS_MODEL * model
BOOST_AUTO_TEST_CASE(DetachedHandlesBehaveAsInvalid)
BOOST_CHECK_EQUAL(result, "25.4")