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