KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_sch_api_handler.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
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <memory>
21
22#include <boost/test/unit_test.hpp>
23
24#include <api/api_handler_sch.h>
25#include <api/api_sch_utils.h>
27#include <api/api_utils.h>
28#include <api/common/commands/editor_commands.pb.h>
29#include <api/common/envelope.pb.h>
30#include <api/common/tasks.pb.h>
32#include <api/schematic/schematic_tasks.pb.h>
33#include <api/schematic/schematic_types.pb.h>
36
37#include <schematic.h>
38#include <advanced_config.h>
39#include <connection_graph.h>
40#include <sch_label.h>
41#include <sch_line.h>
42#include <sch_screen.h>
43#include <sch_sheet.h>
44#include <set>
45#include <scoped_set_reset.h>
46
47
48namespace
49{
50
51struct API_HANDLER_SCH_FIXTURE
52{
53 SETTINGS_MANAGER m_settingsManager;
54 std::unique_ptr<SCHEMATIC> m_schematic;
55 SCHEMATIC* m_schematicSlot = nullptr;
56 std::shared_ptr<HEADLESS_SCH_CONTEXT> m_context;
57
58 SCHEMATIC* loadSchematic( const wxString& aRelPath )
59 {
60 KI_TEST::LoadSchematic( m_settingsManager, aRelPath, m_schematic );
61 m_schematicSlot = m_schematic.get();
62 m_context = std::make_shared<HEADLESS_SCH_CONTEXT>( &m_schematicSlot,
63 &m_settingsManager.Prj() );
64 return m_schematic.get();
65 }
66};
67
68
69kiapi::common::types::DocumentSpecifier makeDocument( const SCHEMATIC& aSchematic )
70{
71 kiapi::common::types::DocumentSpecifier document;
72 document.set_type( kiapi::common::types::DocumentType::DOCTYPE_SCHEMATIC );
73 kiapi::common::PackProject( *document.mutable_project(), aSchematic.Project() );
74
75 return document;
76}
77
78
79kiapi::common::ApiRequest makeBeginCommitRequest( const SCHEMATIC& aSchematic )
80{
81 kiapi::common::commands::BeginCommit command;
82 *command.mutable_header()->mutable_document() = makeDocument( aSchematic );
83
84 kiapi::common::ApiRequest request;
85 request.mutable_header()->set_client_name( "kicad.qa" );
86 BOOST_REQUIRE( request.mutable_message()->PackFrom( command ) );
87
88 return request;
89}
90
91
92kiapi::common::ApiRequest makeRevertRequest( const SCHEMATIC& aSchematic )
93{
94 kiapi::common::commands::RevertDocument command;
95 *command.mutable_document() = makeDocument( aSchematic );
96
97 kiapi::common::ApiRequest request;
98 request.mutable_header()->set_client_name( "kicad.qa" );
99 BOOST_REQUIRE( request.mutable_message()->PackFrom( command ) );
100
101 return request;
102}
103
104
105kiapi::common::ApiRequest makeStartErcTaskRequest( const SCHEMATIC& aSchematic )
106{
107 kiapi::schematic::tasks::StartErcTask command;
108 *command.mutable_document() = makeDocument( aSchematic );
109
110 kiapi::common::ApiRequest request;
111 request.mutable_header()->set_client_name( "kicad.qa" );
112 BOOST_REQUIRE( request.mutable_message()->PackFrom( command ) );
113
114 return request;
115}
116
117
118kiapi::common::ApiRequest makeGetErcTaskResultRequest( const std::string& aToken )
119{
120 kiapi::schematic::tasks::GetErcTaskResult command;
121 command.mutable_token()->set_value( aToken );
122
123 kiapi::common::ApiRequest request;
124 request.mutable_header()->set_client_name( "kicad.qa" );
125 BOOST_REQUIRE( request.mutable_message()->PackFrom( command ) );
126
127 return request;
128}
129
130
131}
132
133
134BOOST_FIXTURE_TEST_SUITE( ApiHandlerSch, API_HANDLER_SCH_FIXTURE )
135
136
137BOOST_AUTO_TEST_CASE( RevertDocumentRejectedWithOpenCommit )
138{
139 SCHEMATIC* schematic = loadSchematic( wxS( "api_kitchen_sink" ) );
140
141 API_HANDLER_SCH handler( m_context );
142
143 kiapi::common::ApiRequest beginRequest = makeBeginCommitRequest( *schematic );
144 BOOST_REQUIRE( handler.Handle( beginRequest ).has_value() );
145
146 kiapi::common::ApiRequest request = makeRevertRequest( *schematic );
147 API_RESULT result = handler.Handle( request );
148
149 BOOST_REQUIRE( !result.has_value() );
150 BOOST_CHECK_EQUAL( result.error().status(), kiapi::common::ApiStatusCode::AS_BUSY );
151 BOOST_CHECK( result.error().error_message().find( "commit" ) != std::string::npos );
152}
153
154
155BOOST_AUTO_TEST_CASE( RejectedSheetCreateLeavesNoChanges )
156{
157 SCHEMATIC* model = loadSchematic( wxS( "api_kitchen_sink" ) );
158 API_HANDLER_SCH handler( m_context );
159
160 schematic::types::SheetSymbol sheet;
161 const KIID id;
162 sheet.mutable_id()->set_value( id.AsStdString() );
163 sheet.mutable_name_field()->mutable_text()->set_text( "RejectedSheet" );
164 sheet.mutable_filename_field()->mutable_text()->set_text( "rejected.kicad_sch" );
165 sheet.set_page_number( "999" );
166 auto* field = sheet.add_user_fields();
167 field->set_name( "Reference Suffix" );
168 field->mutable_text()->set_text( "_M1" );
169 auto* variant = sheet.mutable_variants()->add_variants();
170 variant->set_name( "rejected-variant" );
171 ( *variant->mutable_fields() )["Reference Suffix"] = "_M2";
172 variant->set_dnp( true );
173
174 commands::CreateItems command;
175 *command.mutable_header()->mutable_document() = makeDocument( *model );
176 BOOST_REQUIRE( command.add_items()->PackFrom( sheet ) );
177 ApiRequest request;
178 request.mutable_header()->set_client_name( "kicad.qa" );
179 BOOST_REQUIRE( request.mutable_message()->PackFrom( command ) );
180 API_RESULT result = handler.Handle( request );
181 BOOST_REQUIRE( result.has_value() );
182
183 commands::CreateItemsResponse response;
184 BOOST_REQUIRE( result->message().UnpackTo( &response ) );
185 BOOST_REQUIRE_EQUAL( response.created_items_size(), 1 );
186 BOOST_CHECK_EQUAL( response.created_items( 0 ).status().code(), commands::ISC_INVALID_DATA );
187 BOOST_CHECK( !model->Hierarchy().ResolveItem( id, nullptr, true ) );
188 BOOST_CHECK( !model->HasVariant( wxS( "rejected-variant" ) ) );
189}
190
191
192BOOST_AUTO_TEST_CASE( RejectedSheetUpdateLeavesNoChanges )
193{
194 SCHEMATIC* model = loadSchematic( wxS( "issue13212" ) );
195 API_HANDLER_SCH handler( m_context );
196 BOOST_REQUIRE_GE( model->Hierarchy().size(), 2 );
197 SCH_SHEET_PATH child = model->Hierarchy()[1];
198 SCH_SHEET* original = child.Last();
199 SCH_SHEET_PATH parent = child;
200 parent.pop_back();
201 const wxString originalName = original->GetName();
202 const wxString originalPage = child.GetPageNumber();
203
204 schematic::types::SheetSymbol sheet;
205 BOOST_REQUIRE( PackSheet( &sheet, original, parent ) );
206 sheet.mutable_name_field()->mutable_text()->set_text( "RejectedName" );
207 sheet.set_page_number( "999" );
208 auto* field = sheet.add_user_fields();
209 field->set_name( "Reference Suffix" );
210 field->mutable_text()->set_text( "_M1" );
211 auto* variant = sheet.mutable_variants()->add_variants();
212 variant->set_name( "rejected-variant" );
213 ( *variant->mutable_fields() )["Reference Suffix"] = "_M2";
214
215 commands::UpdateItems command;
216 *command.mutable_header()->mutable_document() = makeDocument( *model );
217 BOOST_REQUIRE( command.add_items()->PackFrom( sheet ) );
218 ApiRequest request;
219 request.mutable_header()->set_client_name( "kicad.qa" );
220 BOOST_REQUIRE( request.mutable_message()->PackFrom( command ) );
221 API_RESULT result = handler.Handle( request );
222 BOOST_REQUIRE( result.has_value() );
223
224 commands::UpdateItemsResponse response;
225 BOOST_REQUIRE( result->message().UnpackTo( &response ) );
226 BOOST_REQUIRE_EQUAL( response.updated_items_size(), 1 );
227 BOOST_CHECK_EQUAL( response.updated_items( 0 ).status().code(), commands::ISC_INVALID_DATA );
228 BOOST_CHECK_EQUAL( original->GetName(), originalName );
229 BOOST_CHECK_EQUAL( child.GetPageNumber(), originalPage );
230 BOOST_CHECK( !model->HasVariant( wxS( "rejected-variant" ) ) );
231}
232
233
234BOOST_AUTO_TEST_CASE( SheetBatchKeepsValidItems )
235{
236 SCHEMATIC* model = loadSchematic( wxS( "api_kitchen_sink" ) );
237 API_HANDLER_SCH handler( m_context );
238 commands::CreateItems command;
239 *command.mutable_header()->mutable_document() = makeDocument( *model );
240 std::vector<KIID> ids( 3 );
241
242 for( int i = 0; i < 3; ++i )
243 {
244 schematic::types::SheetSymbol sheet;
245 sheet.mutable_id()->set_value( ids[i].AsStdString() );
246 sheet.mutable_name_field()->mutable_text()->set_text( "Sheet" + std::to_string( i ) );
247 sheet.mutable_filename_field()->mutable_text()->set_text( "batch" + std::to_string( i ) + ".kicad_sch" );
248 sheet.set_page_number( std::to_string( 20 + i ) );
249 auto* suffix = sheet.add_user_fields();
250 suffix->set_name( "Reference Suffix" );
251 suffix->mutable_text()->set_text( "_M1" );
252 auto* field = sheet.add_user_fields();
253 field->set_name( "Channel" );
254 field->mutable_text()->set_text( "base" );
255 auto* variant = sheet.mutable_variants()->add_variants();
256 variant->set_name( "batch-variant-" + std::to_string( i ) );
257 variant->set_dnp( true );
258 ( *variant->mutable_fields() )["Channel"] = "override";
259
260 if( i == 1 )
261 ( *variant->mutable_fields() )["Reference Suffix"] = "_M2";
262
263 BOOST_REQUIRE( command.add_items()->PackFrom( sheet ) );
264 }
265
266 ApiRequest request;
267 request.mutable_header()->set_client_name( "kicad.qa" );
268 BOOST_REQUIRE( request.mutable_message()->PackFrom( command ) );
269 API_RESULT result = handler.Handle( request );
270 BOOST_REQUIRE( result.has_value() );
271 commands::CreateItemsResponse response;
272 BOOST_REQUIRE( result->message().UnpackTo( &response ) );
273 BOOST_REQUIRE_EQUAL( response.created_items_size(), 3 );
274 BOOST_CHECK_EQUAL( response.status(), types::IRS_OK );
275
276 SCH_SHEET_PATH parent = model->Hierarchy()[0];
277
278 for( int i = 0; i < 3; ++i )
279 {
280 const wxString variantName = wxString::Format( wxS( "batch-variant-%d" ), i );
281 auto* sheet = dynamic_cast<SCH_SHEET*>( model->Hierarchy().ResolveItem( ids[i], nullptr, true ) );
282 BOOST_CHECK_EQUAL( response.created_items( i ).status().code(),
283 i == 1 ? commands::ISC_INVALID_DATA : commands::ISC_OK );
284 BOOST_CHECK_EQUAL( model->HasVariant( variantName ), i != 1 );
285
286 if( i == 1 )
287 {
288 BOOST_CHECK( !sheet );
289 continue;
290 }
291
292 BOOST_REQUIRE( sheet );
293 BOOST_CHECK( sheet->GetDNP( &parent, variantName ) );
294 BOOST_CHECK( sheet->GetFieldText( wxS( "Channel" ), &parent, variantName ) == wxS( "override" ) );
295 schematic::types::SheetSymbol stored;
296 BOOST_REQUIRE( PackSheet( &stored, sheet, parent ) );
297 BOOST_CHECK_EQUAL( stored.page_number(), std::to_string( 20 + i ) );
298 }
299}
300
301
302BOOST_AUTO_TEST_CASE( CustomPropertyCannotDuplicateSystemProperty )
303{
304 SCHEMATIC* schematic = loadSchematic( wxS( "api_kitchen_sink" ) );
305
306 API_HANDLER_SCH handler( m_context );
307 ApiRequest beginRequest = makeBeginCommitRequest( *schematic );
308 BOOST_REQUIRE( handler.Handle( beginRequest ).has_value() );
309
310 commands::CreateItems command;
311 command.mutable_header()->mutable_document()->CopyFrom( makeDocument( *schematic ) );
312
313 schematic::types::SchematicLine line;
314 line.mutable_id()->set_value( "83618809-06d6-4cbe-ba0e-ec60fe58921c" );
315 line.set_type( schematic::types::SchematicLineType::SLT_WIRE );
316 line.add_custom_properties()->set_key( "linE Width" );
317 line.add_custom_properties()->set_key( "start X" );
318
319 BOOST_REQUIRE( command.add_items()->PackFrom( line ) );
320
321 ApiRequest request;
322 request.mutable_header()->set_client_name( "kicad.qa" );
323 BOOST_REQUIRE( request.mutable_message()->PackFrom( command ) );
324
325 API_RESULT result = handler.Handle( request );
326 BOOST_REQUIRE( result.has_value() );
327
328 commands::CreateItemsResponse response;
329 BOOST_REQUIRE( result->message().UnpackTo( &response ) );
330 BOOST_REQUIRE_EQUAL( response.created_items_size(), 1 );
331
332 const commands::ItemStatus& status = response.created_items( 0 ).status();
333
334 // Should be rejected; duplicates built in property
335 BOOST_CHECK_EQUAL( status.code(), kiapi::common::commands::ItemStatusCode::ISC_INVALID_DATA );
336 BOOST_CHECK_NE( status.error_message().find( "Invalid custom properties" ), std::string::npos );
337}
338
339
340BOOST_AUTO_TEST_CASE( SymbolFieldTakesPrecedenceOverCustomProperty )
341{
342 SCHEMATIC* schematic = loadSchematic( wxS( "api_kitchen_sink" ) );
343
344 API_HANDLER_SCH handler( m_context );
345 ApiRequest beginRequest = makeBeginCommitRequest( *schematic );
346 BOOST_REQUIRE( handler.Handle( beginRequest ).has_value() );
347
348 commands::CreateItems command;
349 command.mutable_header()->mutable_document()->CopyFrom( makeDocument( *schematic ) );
350
351 schematic::types::SchematicSymbolInstance symbol;
352 symbol.mutable_id()->set_value( "f8688bac-2fcb-4184-ab0e-0cf46a139c43" );
353 symbol.mutable_transform()->set_orientation( schematic::types::SchematicSymbolOrientation::SSO_0 );
354
355 symbol.mutable_definition()->mutable_id()->set_entry_name( "R" );
356
357 schematic::types::SchematicField* field = symbol.add_user_fields();
358 field->set_name( "MPN" );
359 field->mutable_text()->set_text( "123" );
360
361 types::CustomProperty* prop = symbol.add_custom_properties();
362 prop->set_key( "MPN" );
363 prop->set_value( "456" );
364
365 BOOST_REQUIRE( command.add_items()->PackFrom( symbol ) );
366
367 ApiRequest request;
368 request.mutable_header()->set_client_name( "kicad.qa" );
369 BOOST_REQUIRE( request.mutable_message()->PackFrom( command ) );
370
371 API_RESULT result = handler.Handle( request );
372 BOOST_REQUIRE( result.has_value() );
373
374 commands::CreateItemsResponse response;
375 BOOST_REQUIRE( result->message().UnpackTo( &response ) );
376 BOOST_REQUIRE_EQUAL( response.created_items_size(), 1 );
377
378 const commands::ItemStatus& status = response.created_items( 0 ).status();
379
380 // The field takes precedence: a custom property duplicating a field name is rejected.
381 BOOST_CHECK_EQUAL( status.code(), kiapi::common::commands::ItemStatusCode::ISC_INVALID_DATA );
382 BOOST_CHECK_NE( status.error_message().find( "MPN" ), std::string::npos );
383}
384
385BOOST_AUTO_TEST_CASE( NetlistRebuildsSelectedConnectivityAndFiltersNativeItems )
386{
387 auto& enabled = const_cast<ADVANCED_CFG&>( ADVANCED_CFG::GetCfg() ).m_ConnectivityEngine;
388 SCOPED_SET_RESET restore( enabled, false );
389 SCHEMATIC* model = loadSchematic( wxS( "net_chains_four_nets_labeled" ) );
390 const auto path = model->Hierarchy().front();
391 SCH_LABEL* label = nullptr;
392
393 for( SCH_ITEM* item : path.LastScreen()->Items().OfType( SCH_LABEL_T ) )
394 {
395 if( static_cast<SCH_LABEL*>( item )->GetText() == wxS( "SIG" ) )
396 label = static_cast<SCH_LABEL*>( item );
397 }
398
399 BOOST_REQUIRE( label );
400 API_HANDLER_SCH handler( m_context );
401
402 for( bool published : { false, true } )
403 {
404 BOOST_TEST_CONTEXT( "published=" << published )
405 {
406 enabled = published;
407 const wxString name = published ? wxS( "PUBLISHED_API" ) : wxS( "LEGACY_API" );
408 label->SetText( name );
409
410 // Only the engine handler rebuilds; legacy answers from the graph the editor keeps current
411 if( published )
412 model->ConnectionGraph()->Reset();
413 else
414 model->RebuildConnectivity();
415
416 for( bool filtered : { false, true } )
417 {
418 kiapi::schematic::commands::GetSchematicNetlist command;
419 *command.mutable_document() = makeDocument( *model );
420
421 if( filtered )
422 command.add_types( kiapi::common::types::KOT_SCH_LABEL );
423
424 ApiRequest request;
425 request.mutable_header()->set_client_name( "kicad.qa" );
426 BOOST_REQUIRE( request.mutable_message()->PackFrom( command ) );
427 API_RESULT result = handler.Handle( request );
428 BOOST_REQUIRE( result.has_value() );
429 kiapi::schematic::commands::SchematicNetlistResponse response;
430 BOOST_REQUIRE( result->message().UnpackTo( &response ) );
431 std::set<std::string> items;
432
433 for( const auto& net : response.nets() )
434 {
435 if( net.name() != ( wxS( "/" ) + name ).ToStdString() )
436 continue;
437
438 for( const auto& sheet : net.sheets() )
439 {
440 for( const auto& item : sheet.items() )
441 items.insert( item.value() );
442 }
443 }
444
445 BOOST_CHECK( items.contains( label->m_Uuid.AsStdString() ) );
446
447 if( filtered )
448 BOOST_CHECK_EQUAL( items.size(), 1 );
449 else
450 BOOST_CHECK_GT( items.size(), 1 );
451 }
452 }
453 }
454}
455
456BOOST_AUTO_TEST_CASE( NetlistPreservesNativeHierarchyMembershipAndExcludesBuses )
457{
458 auto& enabled = const_cast<ADVANCED_CFG&>( ADVANCED_CFG::GetCfg() ).m_ConnectivityEngine;
459 SCOPED_SET_RESET restore( enabled, false );
460 SCHEMATIC* model = loadSchematic( wxS( "netlists/hierarchy_aliases/hierarchy_aliases" ) );
461 auto* root = model->Hierarchy().front().LastScreen();
462 auto labels = root->Items().OfType( SCH_LABEL_T );
463 BOOST_REQUIRE( labels.begin() != labels.end() );
464 auto* unusedBus = static_cast<SCH_LABEL*>( ( *labels.begin() )->Clone() );
465 const_cast<KIID&>( unusedBus->m_Uuid ) = KIID();
466 unusedBus->SetText( wxS( "API_UNUSED[0..1]" ) );
467 unusedBus->SetPosition( VECTOR2I( 0, 0 ) );
468 root->Append( unusedBus );
469 SCH_LINE* undriven = nullptr;
470
471 for( SCH_ITEM* item : root->Items().OfType( SCH_LINE_T ) )
472 {
473 auto* line = static_cast<SCH_LINE*>( item );
474
475 if( line->IsWire() )
476 {
477 undriven = static_cast<SCH_LINE*>( line->Clone() );
478 break;
479 }
480 }
481
482 BOOST_REQUIRE( undriven );
483 const_cast<KIID&>( undriven->m_Uuid ) = KIID();
484 undriven->Move( VECTOR2I( -1000000, -1000000 ) - undriven->GetStartPoint() );
485 root->Append( undriven );
486 API_HANDLER_SCH handler( m_context );
487 using CONTENTS = std::map<std::string, std::map<std::vector<std::string>, std::set<std::string>>>;
488
489 for( bool filtered : { false, true } )
490 {
491 CONTENTS expected;
492
493 for( bool published : { false, true } )
494 {
495 BOOST_TEST_CONTEXT( "published=" << published << " filtered=" << filtered )
496 {
497 enabled = published;
498
499 // Only the engine handler rebuilds; legacy answers from the graph the editor keeps current
500 if( published )
501 model->ConnectionGraph()->Reset();
502 else
503 model->RebuildConnectivity();
504
505 kiapi::schematic::commands::GetSchematicNetlist command;
506 *command.mutable_document() = makeDocument( *model );
507
508 if( filtered )
509 command.add_types( kiapi::common::types::KOT_SCH_PIN );
510
511 ApiRequest request;
512 request.mutable_header()->set_client_name( "kicad.qa" );
513 BOOST_REQUIRE( request.mutable_message()->PackFrom( command ) );
514 API_RESULT result = handler.Handle( request );
515 BOOST_REQUIRE( result.has_value() );
516 kiapi::schematic::commands::SchematicNetlistResponse response;
517 BOOST_REQUIRE( result->message().UnpackTo( &response ) );
518 CONTENTS actual;
519 size_t nestedItems = 0;
520
521 for( const auto& net : response.nets() )
522 {
523 BOOST_CHECK_GT( net.sheets_size(), 0 );
524 BOOST_CHECK( net.name().find( '{' ) == std::string::npos );
525 BOOST_CHECK( net.name().find( "API_UNUSED" ) == std::string::npos );
526
527 for( const auto& sheet : net.sheets() )
528 {
529 std::vector<std::string> path;
530
531 for( const auto& id : sheet.path().path() )
532 path.push_back( id.value() );
533
534 auto& items = actual[net.name()][path];
535
536 for( const auto& item : sheet.items() )
537 {
538 BOOST_CHECK_NE( item.value(), undriven->m_Uuid.AsStdString() );
539 items.insert( item.value() );
540 }
541
542 if( path.size() > 2 )
543 nestedItems += items.size();
544 }
545 }
546
547 BOOST_CHECK( actual.contains( "/S0.BOOT.SDA" ) );
548 BOOST_CHECK( actual.contains( "/S1.BOOT.SDA" ) );
549 BOOST_CHECK_GT( nestedItems, 0 );
550
551 if( published )
552 {
554 }
555 else
556 {
557 expected = std::move( actual );
558 }
559 }
560 }
561 }
562}
563
564BOOST_AUTO_TEST_CASE( NetlistReportsRebuildFailureAndAllowsRetry )
565{
566 struct FAILING_LABEL : SCH_LABEL
567 {
568 explicit FAILING_LABEL( const SCH_LABEL& source ) : SCH_LABEL( source ) {}
569
570 wxString GetShownText( const SCH_SHEET_PATH* path, RESOLUTION_CONTEXT context, int depth = 0 ) const override
571 {
572 if( fail )
573 throw std::runtime_error( "Native label extraction failed" );
574
575 return SCH_LABEL::GetShownText( path, context, depth );
576 }
577
578 bool fail = false;
579 };
580
581 auto& enabled = const_cast<ADVANCED_CFG&>( ADVANCED_CFG::GetCfg() ).m_ConnectivityEngine;
582 SCOPED_SET_RESET restore( enabled, false );
583 SCHEMATIC* model = loadSchematic( wxS( "net_chains_four_nets_labeled" ) );
584 auto* screen = model->Hierarchy().front().LastScreen();
585 auto labels = screen->Items().OfType( SCH_LABEL_T );
586 BOOST_REQUIRE( labels.begin() != labels.end() );
587 auto* original = static_cast<SCH_LABEL*>( *labels.begin() );
588 auto* replacement = new FAILING_LABEL( *original );
589 screen->Remove( original );
590 delete original;
591 screen->Append( replacement );
592 model->ConnectionGraph()->Reset();
593 replacement->fail = true;
594 enabled = true;
595 API_HANDLER_SCH handler( m_context );
596 kiapi::schematic::commands::GetSchematicNetlist command;
597 *command.mutable_document() = makeDocument( *model );
598 ApiRequest request;
599 request.mutable_header()->set_client_name( "kicad.qa" );
600 BOOST_REQUIRE( request.mutable_message()->PackFrom( command ) );
601 API_RESULT result = handler.Handle( request );
602 BOOST_REQUIRE( !result.has_value() );
603 BOOST_CHECK_EQUAL( result.error().status(), ApiStatusCode::AS_UNKNOWN );
604 BOOST_CHECK_EQUAL( result.error().error_message(), "Native label extraction failed" );
605 replacement->fail = false;
606 result = handler.Handle( request );
607 BOOST_REQUIRE( result.has_value() );
608 kiapi::schematic::commands::SchematicNetlistResponse response;
609 BOOST_REQUIRE( result->message().UnpackTo( &response ) );
610 BOOST_CHECK_GT( response.nets_size(), 0 );
611}
612
613
614BOOST_AUTO_TEST_CASE( StartErcTaskCompletesWithResults )
615{
616 SCHEMATIC* schematic = loadSchematic( wxS( "NoConnectOnPin" ) );
617
618 API_HANDLER_SCH handler( m_context );
619
620 kiapi::common::ApiRequest startRequest = makeStartErcTaskRequest( *schematic );
621 API_RESULT start = handler.Handle( startRequest );
622 BOOST_REQUIRE( start.has_value() );
623
624 kiapi::common::tasks::StartTaskResponse startResponse;
625 BOOST_REQUIRE( start->message().UnpackTo( &startResponse ) );
626 BOOST_REQUIRE( !startResponse.token().value().empty() );
627
629
630 auto status = API_TASK_SCHEDULER::Instance().GetStatus( startResponse.token().value() );
631 BOOST_REQUIRE( status.has_value() );
632 BOOST_CHECK_EQUAL( status->state(), kiapi::common::tasks::TS_COMPLETED );
633
634 kiapi::common::ApiRequest resultRequest = makeGetErcTaskResultRequest( startResponse.token().value() );
635 API_RESULT result = handler.Handle( resultRequest );
636 BOOST_REQUIRE( result.has_value() );
637
638 kiapi::schematic::tasks::ErcTaskResult ercResult;
639 BOOST_REQUIRE( result->message().UnpackTo( &ercResult ) );
640
641 BOOST_CHECK_GT( ercResult.violations_size(), 0 );
642 BOOST_CHECK_EQUAL( ercResult.excluded_count(), 0 );
643
644 BOOST_CHECK_GT( ercResult.ignored_count(), 0 );
645
646 int markerCount = 0;
647 SCH_SCREENS screens( schematic->Root() );
648
649 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
650 {
651 for( SCH_ITEM* item : screen->Items() )
652 {
653 if( item->Type() == SCH_MARKER_T )
654 ++markerCount;
655 }
656 }
657
658 BOOST_CHECK_EQUAL( markerCount, ercResult.violations_size() );
659
660 for( const kiapi::schematic::ErcMarker& violation : ercResult.violations() )
661 {
662 BOOST_CHECK( !violation.description().empty() );
663 BOOST_CHECK( violation.error_type() != kiapi::schematic::ErcErrorType::ERCET_UNKNOWN );
664 }
665}
666
667
668BOOST_AUTO_TEST_CASE( StartErcTaskWhileRunningIsRejected )
669{
670 SCHEMATIC* schematic = loadSchematic( wxS( "NoConnectOnPin" ) );
671
672 API_HANDLER_SCH handler( m_context );
673
674 kiapi::common::ApiRequest startRequest = makeStartErcTaskRequest( *schematic );
675 API_RESULT start = handler.Handle( startRequest );
676 BOOST_REQUIRE( start.has_value() );
677
678 kiapi::common::tasks::StartTaskResponse startResponse;
679 BOOST_REQUIRE( start->message().UnpackTo( &startResponse ) );
680 BOOST_REQUIRE( !startResponse.token().value().empty() );
681
682 // The ERC run event has not been pumped, so the task is still running
683 kiapi::common::ApiRequest secondRequest = makeStartErcTaskRequest( *schematic );
684 API_RESULT second = handler.Handle( secondRequest );
685 BOOST_REQUIRE( !second.has_value() );
686 BOOST_CHECK_EQUAL( second.error().status(), kiapi::common::ApiStatusCode::AS_BUSY );
687
688 kiapi::common::ApiRequest runningRequest = makeGetErcTaskResultRequest( startResponse.token().value() );
689 API_RESULT runningResult = handler.Handle( runningRequest );
690 BOOST_REQUIRE( !runningResult.has_value() );
691 BOOST_CHECK_EQUAL( runningResult.error().status(), kiapi::common::ApiStatusCode::AS_BUSY );
692
694
695 kiapi::common::ApiRequest finishedRequest =
696 makeGetErcTaskResultRequest( startResponse.token().value() );
697 API_RESULT finishedResult = handler.Handle( finishedRequest );
698 BOOST_REQUIRE( finishedResult.has_value() );
699}
700
701
702BOOST_AUTO_TEST_CASE( GetErcTaskResultRejectsUnknownToken )
703{
704 loadSchematic( wxS( "NoConnectOnPin" ) );
705
706 API_HANDLER_SCH handler( m_context );
707
708 kiapi::common::ApiRequest request = makeGetErcTaskResultRequest( "deadbeef-0000-0000-0000-000000000000" );
709 API_RESULT result = handler.Handle( request );
710
711 BOOST_REQUIRE( !result.has_value() );
712 BOOST_CHECK_EQUAL( result.error().status(), kiapi::common::ApiStatusCode::AS_BAD_REQUEST );
713}
714
const char * name
tl::expected< ApiResponse, ApiResponseStatus > API_RESULT
Definition api_handler.h:44
bool PackSheet(kiapi::schematic::types::SheetSymbol *aOutput, const SCH_SHEET *aInput, const SCH_SHEET_PATH &aPath)
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
API_RESULT Handle(ApiRequest &aMsg)
Attempt to handle the given API request, if a handler exists in this class for the message.
std::optional< kiapi::common::tasks::TaskStatusResponse > GetStatus(const std::string &aToken) const
Get a snapshot of the task's current status.
void Pump()
Run the current task to completion, synchronously.
static API_TASK_SCHEDULER & Instance()
const KIID m_Uuid
Definition eda_item.h:599
virtual void SetText(const wxString &aText)
Definition eda_text.cpp:237
Definition kiid.h:46
std::string AsStdString() const
Definition kiid.cpp:270
Holds all the data relating to one schematic.
Definition schematic.h:155
PROJECT & Project() const
Return a reference to the project this schematic is part of.
Definition schematic.h:177
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:170
wxString GetShownText(const SCH_SHEET_PATH *aPath, RESOLUTION_CONTEXT aContext, int aDepth=0) const override
SCH_LABEL(const VECTOR2I &aPos=VECTOR2I(0, 0), const wxString &aText=wxEmptyString)
Segment description base class to describe items which have 2 end points (track, wire,...
Definition sch_line.h:39
VECTOR2I GetStartPoint() const
Definition sch_line.h:136
void Move(const VECTOR2I &aMoveVector) override
Move the item by aMoveVector to a new position.
Definition sch_line.cpp:249
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition sch_screen.h:826
SCH_SCREEN * GetNext()
SCH_SCREEN * GetFirst()
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
wxString GetPageNumber() const
SCH_SHEET * Last() const
Return a pointer to the last SCH_SHEET of the list.
void pop_back()
Forwarded method from std::vector.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:50
wxString GetName() const
Definition sch_sheet.h:150
RAII class that sets an value at construction and resets it to the original value at destruction.
RESOLUTION_CONTEXT
Definition common.h:91
static std::string ToStdString(const wxString &aStr)
std::string source
void LoadSchematic(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< SCHEMATIC > &aSchematic)
KICOMMON_API void PackProject(types::ProjectSpecifier &aOutput, const PROJECT &aInput)
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
BOOST_REQUIRE_EQUAL(rows.size(), 1)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_REQUIRE(res.columns()==1)
BOOST_AUTO_TEST_SUITE_END()
std::string path
KIBIS_MODEL * model
VECTOR3I expected(15, 30, 45)
BOOST_AUTO_TEST_CASE(RevertDocumentRejectedWithOpenCommit)
BOOST_TEST_CONTEXT("Test Clearance")
int actual
wxString result
Test unit parsing edge cases and error handling.
@ SCH_LINE_T
Definition typeinfo.h:159
@ SCH_LABEL_T
Definition typeinfo.h:163
@ SCH_MARKER_T
Definition typeinfo.h:154
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708