KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_settings_manager.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
24
26
29#include <settings/parameters.h>
31
32#include <filesystem>
33#include <system_error>
34
35namespace fs = std::filesystem;
36
37
38// Backed by a caller-controlled absolute path (SETTINGS_LOC::NONE) so the tests persist and
39// reload from a scratch directory without touching the shared config corpus.
41{
42public:
43 FLUSH_TEST_SETTINGS( const wxString& aFullPath ) :
44 JSON_SETTINGS( aFullPath, SETTINGS_LOC::NONE, 1 ),
45 m_value( 0 )
46 {
47 m_params.emplace_back( new PARAM<int>( "test.value", &m_value, 0 ) );
48 }
49
51};
52
53
55{
57 m_tempDir( fs::temp_directory_path() / "kicad_settings_manager_test" )
58 {
59 std::error_code ec;
60 fs::remove_all( m_tempDir, ec );
61
62 // Throwing overload so an unusable scratch directory fails setup loudly
63 fs::create_directories( m_tempDir );
64 }
65
67 {
68 std::error_code ec;
69 fs::remove_all( m_tempDir, ec );
70 }
71
72 wxString Path( const std::string& aName ) const
73 {
74 return wxString( ( m_tempDir / aName ).string() );
75 }
76
77 fs::path m_tempDir;
78};
79
80
81BOOST_FIXTURE_TEST_SUITE( SettingsManager, SETTINGS_MANAGER_FIXTURE )
82
83
84// Load() may run after a settings object was edited in memory but not yet written; the pending
85// edit must be flushed before reloading or the stale on-disk copy silently discards it.
86BOOST_AUTO_TEST_CASE( LoadFlushesDirtySettings )
87{
89
90 // Drop the auto-registered common settings so Load() only touches the scratch object
91 mgr.FlushAndRelease( mgr.GetCommonSettings(), false );
92
94 mgr.RegisterSettings( new FLUSH_TEST_SETTINGS( Path( "dirty" ) ), false );
95
96 cfg->SaveToFile();
97 cfg->m_value = 42;
98
99 mgr.Load();
100
101 BOOST_CHECK_EQUAL( cfg->m_value, 42 );
102
103 // The dirty value must have reached disk, not merely survived in memory
104 FLUSH_TEST_SETTINGS fresh( Path( "dirty" ) );
105 fresh.LoadFromFile();
106 BOOST_CHECK_EQUAL( fresh.m_value, 42 );
107}
108
109
110// A registered object that has never been synchronized with its file must not be flushed by
111// Load(); flushing would overwrite the file with construction state before it is ever read.
112BOOST_AUTO_TEST_CASE( LoadDoesNotFlushNeverSyncedSettings )
113{
114 {
115 FLUSH_TEST_SETTINGS seed( Path( "cold" ) );
116 seed.m_value = 7;
117 seed.SaveToFile();
118 }
119
121 mgr.FlushAndRelease( mgr.GetCommonSettings(), false );
122
124 mgr.RegisterSettings( new FLUSH_TEST_SETTINGS( Path( "cold" ) ), false );
125
126 mgr.Load();
127
128 BOOST_CHECK_EQUAL( cfg->m_value, 7 );
129
130 FLUSH_TEST_SETTINGS fresh( Path( "cold" ) );
131 fresh.LoadFromFile();
132 BOOST_CHECK_EQUAL( fresh.m_value, 7 );
133}
134
135
FLUSH_TEST_SETTINGS(const wxString &aFullPath)
virtual bool LoadFromFile(const wxString &aDirectory="")
Loads the backing file from disk and then calls Load()
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
JSON_SETTINGS(const wxString &aFilename, SETTINGS_LOC aLocation, int aSchemaVersion)
T * RegisterSettings(T *aSettings, bool aLoadNow=true)
Take ownership of the pointer passed in.
COMMON_SETTINGS * GetCommonSettings() const
Retrieve the common settings shared by all applications.
void FlushAndRelease(JSON_SETTINGS *aSettings, bool aSave=true)
If the given settings object is registered, save it to disk and unregister it.
@ NONE
Definition eda_shape.h:72
SETTINGS_LOC
wxString Path(const std::string &aName) const
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE_END()
const uint32_t seed
BOOST_AUTO_TEST_CASE(LoadFlushesDirtySettings)
BOOST_CHECK_EQUAL(result, "25.4")