KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_drawing_sheet_reactive.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 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
20#define BOOST_TEST_NO_MAIN
21#include <boost/test/unit_test.hpp>
22
23#include <algorithm>
24#include <base_units.h>
27#include <page_info.h>
28#include <text_var_dependency.h>
29#include <title_block.h>
30
31
32namespace
33{
34struct DefaultLayoutFixture
35{
36 DefaultLayoutFixture()
37 {
38 // The drawing-sheet system is singleton-based; tests depend on a
39 // populated default layout to find text items to scan.
41 }
42};
43}
44
45
46BOOST_FIXTURE_TEST_SUITE( DrawingSheetReactive, DefaultLayoutFixture )
47
48
49BOOST_AUTO_TEST_CASE( CollectKeysFindsTitleBlockSources )
50{
51 // Default drawing sheet template references PROJECTNAME, REVISION, TITLE,
52 // COMMENT[1..9], ISSUE_DATE, COMPANY, etc. — the set is defined by the
53 // drawing sheet parser. Collect them and confirm several well-known ones
54 // are present.
55 PAGE_INFO page;
56 TITLE_BLOCK tb;
57
58 DS_PROXY_VIEW_ITEM proxy( unityScale, &page, nullptr, &tb, nullptr );
59
60 const auto keys = proxy.CollectTextVarKeys();
61
62 auto hasKey = [&]( TEXT_VAR_REF_KEY::KIND k, const wxString& primary )
63 {
64 return std::any_of( keys.begin(), keys.end(),
65 [&]( const TEXT_VAR_REF_KEY& key )
66 { return key.kind == k && key.primary == primary; } );
67 };
68
69 // The default drawing sheet always surfaces at least these tokens.
70 BOOST_CHECK( hasKey( TEXT_VAR_REF_KEY::KIND::TITLE_BLOCK, wxT( "REVISION" ) ) );
71 BOOST_CHECK( hasKey( TEXT_VAR_REF_KEY::KIND::TITLE_BLOCK, wxT( "TITLE" ) ) );
72 BOOST_CHECK( hasKey( TEXT_VAR_REF_KEY::KIND::TITLE_BLOCK, wxT( "COMPANY" ) ) );
73}
74
75
76BOOST_AUTO_TEST_CASE( AttachRegistersProxyAsDependent )
77{
78 PAGE_INFO page;
79 TITLE_BLOCK tb;
80
81 DS_PROXY_VIEW_ITEM proxy( unityScale, &page, nullptr, &tb, nullptr );
82
83 TEXT_VAR_TRACKER tracker;
84 proxy.AttachToTracker( &tracker );
85
86 // The proxy should be registered as a dependent on every title-block key
87 // its template references.
88 BOOST_CHECK(
89 tracker.Index().DependentCount(
90 TEXT_VAR_REF_KEY::FromToken( wxT( "REVISION" ) ) ) > 0u );
91 BOOST_CHECK(
92 tracker.Index().DependentCount(
93 TEXT_VAR_REF_KEY::FromToken( wxT( "TITLE" ) ) ) > 0u );
94}
95
96
97BOOST_AUTO_TEST_CASE( InvalidationReachesProxyViaListener )
98{
99 PAGE_INFO page;
100 TITLE_BLOCK tb;
101
102 DS_PROXY_VIEW_ITEM proxy( unityScale, &page, nullptr, &tb, nullptr );
103 TEXT_VAR_TRACKER tracker;
104 proxy.AttachToTracker( &tracker );
105
106 // Frames install a long-lived listener that routes invalidations to the
107 // current drawing sheet proxy. Verify the dispatch semantics with a
108 // minimal listener.
109 EDA_ITEM* seenDep = nullptr;
110 (void) tracker.AddInvalidateListener(
111 [&]( EDA_ITEM* dep, const TEXT_VAR_REF_KEY& )
112 {
113 if( dep == &proxy )
114 seenDep = dep;
115 } );
116
117 tracker.InvalidateKey( TEXT_VAR_REF_KEY::FromToken( wxT( "REVISION" ) ) );
118
119 BOOST_CHECK_EQUAL( seenDep, &proxy );
120}
121
122
123BOOST_AUTO_TEST_CASE( DetachUnregistersProxy )
124{
125 PAGE_INFO page;
126 TITLE_BLOCK tb;
127
128 DS_PROXY_VIEW_ITEM proxy( unityScale, &page, nullptr, &tb, nullptr );
129 TEXT_VAR_TRACKER tracker;
130
131 proxy.AttachToTracker( &tracker );
132 BOOST_CHECK(
133 tracker.Index().DependentCount(
134 TEXT_VAR_REF_KEY::FromToken( wxT( "REVISION" ) ) ) > 0u );
135
136 proxy.AttachToTracker( nullptr );
138 tracker.Index().DependentCount(
139 TEXT_VAR_REF_KEY::FromToken( wxT( "REVISION" ) ) ),
140 0u );
141}
142
143
144BOOST_AUTO_TEST_CASE( DestructorAutoUnregisters )
145{
146 PAGE_INFO page;
147 TITLE_BLOCK tb;
148
149 TEXT_VAR_TRACKER tracker;
150
151 {
152 DS_PROXY_VIEW_ITEM proxy( unityScale, &page, nullptr, &tb, nullptr );
153 proxy.AttachToTracker( &tracker );
154 BOOST_REQUIRE( tracker.Index().ItemCount() > 0u );
155 }
156
157 // Proxy destructor must clean up the index so no dangling pointer remains
158 // for a future invalidation to match against.
159 BOOST_CHECK_EQUAL( tracker.Index().ItemCount(), 0u );
160}
161
162
constexpr EDA_IU_SCALE unityScale
Definition base_units.h:128
static DS_DATA_MODEL & GetTheInstance()
Return the instance of DS_DATA_MODEL used in the application.
void AttachToTracker(TEXT_VAR_TRACKER *aTracker)
Register this proxy with aTracker as a dependent on every title-block source variable its current tem...
std::vector< TEXT_VAR_REF_KEY > CollectTextVarKeys() const
Walk the current drawing-sheet definition and collect every ${...} reference encountered in its text ...
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:100
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition page_info.h:79
std::size_t DependentCount(const TEXT_VAR_REF_KEY &aKey) const
Coordinates the dependency index with change notifications.
ListenerHandle AddInvalidateListener(InvalidateCallback aCallback)
Register a listener that fires for every invalidation.
TEXT_VAR_DEPENDENCY_INDEX & Index()
void InvalidateKey(const TEXT_VAR_REF_KEY &aKey)
Fan out invalidation for a single explicit key — used when a non-item source changes (e....
Hold the information shown in the lower right corner of a plot, printout, or editing view.
Definition title_block.h:41
Identifies a single resolvable source that a text item's ${...} reference depends on.
static TEXT_VAR_REF_KEY FromToken(const wxString &aToken)
Parse a raw token (the text between ${ and }) into a key using lexical classification only — no looku...
KIND
Categorizes a reference by the source that will produce its value.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_CASE(CollectKeysFindsTitleBlockSources)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_EQUAL(result, "25.4")