KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_reference_image_load.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 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25#include <boost/test/data/test_case.hpp>
26
27#include <board.h>
28#include <kiid.h>
29#include <layer_ids.h>
30#include <pcb_reference_image.h>
34
35namespace
36{
37
38struct REFERENCE_IMAGE_LOAD_TEST_FIXTURE
39{
40 REFERENCE_IMAGE_LOAD_TEST_FIXTURE() {}
41};
42
43
44struct REFERENCE_IMAGE_LOAD_TEST_CASE
45{
46 // Which one to look at in the file
47 KIID m_imageUuid;
48 // Expected values
49 bool m_expectedLocked;
50 VECTOR2I m_expectedPos;
51 double m_expectedScale;
52 // This should also check correct image decoding, as it won't work otherwise
53 VECTOR2I m_expectedPixelSize;
54};
55
56
57struct REFERENCE_IMAGE_LOAD_BOARD_TEST_CASE: public KI_TEST::BOARD_LOAD_TEST_CASE
58{
59 // List of images to check
60 std::vector<REFERENCE_IMAGE_LOAD_TEST_CASE> m_imageCases;
61};
62
63const std::vector<REFERENCE_IMAGE_LOAD_BOARD_TEST_CASE> ReferenceImageLoading_testCases{
64 {
65 "reference_images_load_save",
66 std::nullopt,
67 {
68 // From top to bottom in the board file
69 {
70 "7dde345e-020a-4fdd-af77-588b452be5e0",
71 false,
72 { 100, 46 },
73 1.0,
74 { 64, 64 },
75 },
76 {
77 "e4fd52dd-1d89-4c43-b621-aebfc9788d5c",
78 true,
79 { 100, 65 },
80 1.0,
81 { 64, 64 },
82 },
83 {
84 "d402397e-bce0-4cae-a398-b5aeef397e87",
85 false,
86 { 100, 90 },
87 2.0,
88 { 64, 64 }, // It's the same size, but scaled
89 },
90 },
91 },
92};
93
94} // namespace
95
96
97BOOST_DATA_TEST_CASE_F( REFERENCE_IMAGE_LOAD_TEST_FIXTURE, ReferenceImageLoading,
98 boost::unit_test::data::make( ReferenceImageLoading_testCases ), testCase )
99{
100 const auto doBoardTest = [&]( const BOARD& aBoard )
101 {
102 for( const REFERENCE_IMAGE_LOAD_TEST_CASE& imageTestCase : testCase.m_imageCases )
103 {
105 "Checking for image with UUID: " << imageTestCase.m_imageUuid.AsString() );
106
107 const auto& image =
109 aBoard, PCB_REFERENCE_IMAGE_T, imageTestCase.m_imageUuid ) );
110 const REFERENCE_IMAGE& refImage = image.GetReferenceImage();
111
112 BOOST_CHECK_EQUAL( image.IsLocked(), imageTestCase.m_expectedLocked );
113 BOOST_CHECK_EQUAL( image.GetPosition(), imageTestCase.m_expectedPos * 1000000 );
114 BOOST_CHECK_CLOSE( refImage.GetImageScale(), imageTestCase.m_expectedScale, 1e-6 );
115
116 const BITMAP_BASE& bitmap = refImage.GetImage();
117
118 BOOST_CHECK_EQUAL( bitmap.GetSizePixels(), imageTestCase.m_expectedPixelSize );
119 }
120 };
121
122 KI_TEST::LoadAndTestBoardFile( testCase.m_BoardFileRelativePath, true, doBoardTest,
123 testCase.m_ExpectedBoardVersion );
124}
125
126
131BOOST_FIXTURE_TEST_CASE( ReferenceImageFlipLayer, REFERENCE_IMAGE_LOAD_TEST_FIXTURE )
132{
133 const auto doBoardTest = [&]( const BOARD& aBoard )
134 {
135 KIID targetUuid( "7dde345e-020a-4fdd-af77-588b452be5e0" );
136
137 auto& image = static_cast<PCB_REFERENCE_IMAGE&>(
139 targetUuid ) );
140
141 BOOST_REQUIRE( image.GetReferenceImage().GetImage().GetSizePixels().x > 0 );
142
143 PCB_LAYER_ID origLayer = image.GetLayer();
144 VECTOR2I origPos = image.GetPosition();
145
146 // Flip left-right and verify layer changes
147 image.Flip( origPos, FLIP_DIRECTION::LEFT_RIGHT );
148
149 PCB_LAYER_ID flippedLayer = aBoard.FlipLayer( origLayer );
150 BOOST_CHECK_EQUAL( image.GetLayer(), flippedLayer );
151
152 // Position should stay the same since we flipped around it
153 BOOST_CHECK_EQUAL( image.GetPosition(), origPos );
154
155 // Flip again to return to original state
156 image.Flip( origPos, FLIP_DIRECTION::LEFT_RIGHT );
157
158 BOOST_CHECK_EQUAL( image.GetLayer(), origLayer );
159 BOOST_CHECK_EQUAL( image.GetPosition(), origPos );
160
161 // Same test for top-bottom flip
162 image.Flip( origPos, FLIP_DIRECTION::TOP_BOTTOM );
163
164 BOOST_CHECK_EQUAL( image.GetLayer(), flippedLayer );
165 BOOST_CHECK_EQUAL( image.GetPosition(), origPos );
166
167 image.Flip( origPos, FLIP_DIRECTION::TOP_BOTTOM );
168
169 BOOST_CHECK_EQUAL( image.GetLayer(), origLayer );
170 BOOST_CHECK_EQUAL( image.GetPosition(), origPos );
171 };
172
173 KI_TEST::LoadAndTestBoardFile( "reference_images_load_save", true, doBoardTest );
174}
General utilities for PCB file IO for QA programs.
This class handle bitmap images in KiCad.
Definition bitmap_base.h:49
VECTOR2I GetSizePixels() const
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:322
Definition kiid.h:49
wxString AsString() const
Definition kiid.cpp:244
Object to handle a bitmap image that can be inserted in a PCB.
A REFERENCE_IMAGE is a wrapper around a BITMAP_IMAGE that is displayed in an editor as a reference fo...
const BITMAP_BASE & GetImage() const
Get the underlying image.
double GetImageScale() const
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:60
@ LEFT_RIGHT
Flip left to right (around the Y axis)
Definition mirror.h:28
@ TOP_BOTTOM
Flip top to bottom (around the X axis)
Definition mirror.h:29
void LoadAndTestBoardFile(const wxString aRelativePath, bool aRoundtrip, std::function< void(BOARD &)> aBoardTestFunction, std::optional< int > aExpectedBoardVersion)
Perform "some test" on a board file loaded from the path, then optionally save and reload and run the...
BOARD_ITEM & RequireBoardItemWithTypeAndId(const BOARD &aBoard, KICAD_T aItemType, const KIID &aID)
Get an item from the given board with a certain type and UUID.
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_FIXTURE_TEST_CASE(ReferenceImageFlipLayer, REFERENCE_IMAGE_LOAD_TEST_FIXTURE)
Test that flipping a reference image changes its associated layer, matching the behavior of all other...
BOOST_DATA_TEST_CASE_F(REFERENCE_IMAGE_LOAD_TEST_FIXTURE, ReferenceImageLoading, boost::unit_test::data::make(ReferenceImageLoading_testCases), testCase)
BOOST_TEST_MESSAGE("Polyline has "<< chain.PointCount()<< " points")
BOOST_CHECK_EQUAL(result, "25.4")
@ PCB_REFERENCE_IMAGE_T
class PCB_REFERENCE_IMAGE, bitmap on a layer
Definition typeinfo.h:89
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695