KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_dialog_shim.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#define BOOST_TEST_NO_MAIN
21#include <boost/test/unit_test.hpp>
23
24#include <dialog_shim.h>
25
26#include <wx/display.h>
27#include <wx/textctrl.h>
28
29
30#define SKIP_IF_HEADLESS() \
31 do \
32 { \
33 if( !KI_TEST::CanDoDisplayTests() ) \
34 { \
35 BOOST_TEST_MESSAGE( "Skipping test - no display available" ); \
36 return; \
37 } \
38 } while( 0 )
39
40namespace
41{
42class TEST_PERSIST_DIALOG : public DIALOG_SHIM
43{
44public:
45 TEST_PERSIST_DIALOG( const wxString& aTitle, bool aOptOutFileName ) :
46 DIALOG_SHIM( nullptr, wxID_ANY, aTitle )
47 {
48 // Two controls so we can watch one restore while the other opts out.
49 // Keys are generated from class name plus sibling index, so identically
50 // shaped dialogs share persisted state.
51 m_plainCtrl = new wxTextCtrl( this, wxID_ANY );
52 m_fileNameCtrl = new wxTextCtrl( this, wxID_ANY );
53
54 if( aOptOutFileName )
55 OptOut( m_fileNameCtrl );
56 }
57
58 wxTextCtrl* m_plainCtrl;
59 wxTextCtrl* m_fileNameCtrl;
60};
61} // namespace
62
63
64BOOST_AUTO_TEST_SUITE( DialogShimControlPersistence )
65
66
67// Regression test for https://gitlab.com/kicad/code/kicad/-/work_items/24860
68// LoadControlState() restored the last-typed value from kicad_common over a
69// value the dialog had already loaded from the project file. Controls backed
70// by project settings opt out, and that opt-out must hold on both the load
71// and the save side.
72//
73// Dialogs are heap-allocated and released with Destroy() because destroying a
74// top-level window directly crashes in this harness (no running event loop).
75BOOST_AUTO_TEST_CASE( OptOutControlNotRestored )
76{
78
79 wxString title = wxS( "PersistTestOptOutLoad" );
80
81 TEST_PERSIST_DIALOG* seed = new TEST_PERSIST_DIALOG( title, false );
82 seed->m_plainCtrl->SetValue( wxS( "stale_plain" ) );
83 seed->m_fileNameCtrl->SetValue( wxS( "stale_from_common.csv" ) );
84 seed->SaveControlState();
85 seed->Destroy();
86
87 TEST_PERSIST_DIALOG* dlg = new TEST_PERSIST_DIALOG( title, true );
88 dlg->m_plainCtrl->SetValue( wxS( "project_plain" ) );
89 dlg->m_fileNameCtrl->SetValue( wxS( "value_from_project.csv" ) );
90 dlg->LoadControlState();
91
92 // The non-opted control restores, proving LoadControlState actually ran
93 BOOST_CHECK_EQUAL( dlg->m_plainCtrl->GetValue(), wxString( "stale_plain" ) );
94
95 // The opted-out control keeps the value set by the dialog
96 BOOST_CHECK_EQUAL( dlg->m_fileNameCtrl->GetValue(), wxString( "value_from_project.csv" ) );
97
98 dlg->Destroy();
99}
100
101
102BOOST_AUTO_TEST_CASE( OptOutControlNotSaved )
103{
105
106 wxString title = wxS( "PersistTestOptOutSave" );
107
108 TEST_PERSIST_DIALOG* seed = new TEST_PERSIST_DIALOG( title, true );
109 seed->m_plainCtrl->SetValue( wxS( "saved_plain" ) );
110 seed->m_fileNameCtrl->SetValue( wxS( "must_not_be_saved.csv" ) );
111 seed->SaveControlState();
112 seed->Destroy();
113
114 TEST_PERSIST_DIALOG* dlg = new TEST_PERSIST_DIALOG( title, false );
115 dlg->m_plainCtrl->SetValue( wxS( "overwrite_me" ) );
116 dlg->m_fileNameCtrl->SetValue( wxS( "value_from_project.csv" ) );
117 dlg->LoadControlState();
118
119 // Sanity check that this dialog's persisted state exists
120 BOOST_CHECK_EQUAL( dlg->m_plainCtrl->GetValue(), wxString( "saved_plain" ) );
121
122 // Nothing was persisted for the opted-out control, so nothing restores
123 // over it even without an opt-out on the loading side
124 BOOST_CHECK_EQUAL( dlg->m_fileNameCtrl->GetValue(), wxString( "value_from_project.csv" ) );
125
126 dlg->Destroy();
127}
128
129
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition dialog_shim.h:80
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_CASE(OptOutControlNotRestored)
#define SKIP_IF_HEADLESS()
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_EQUAL(result, "25.4")