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
25
#include <
qa_utils/wx_utils/unit_test_utils.h
>
26
27
#include <
settings/common_settings.h
>
28
#include <
settings/json_settings.h
>
29
#include <
settings/parameters.h
>
30
#include <
settings/settings_manager.h
>
31
32
#include <filesystem>
33
#include <system_error>
34
35
namespace
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.
40
class
FLUSH_TEST_SETTINGS
:
public
JSON_SETTINGS
41
{
42
public
:
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
50
int
m_value
;
51
};
52
53
54
struct
SETTINGS_MANAGER_FIXTURE
55
{
56
SETTINGS_MANAGER_FIXTURE
() :
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
66
~SETTINGS_MANAGER_FIXTURE
()
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
81
BOOST_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.
86
BOOST_AUTO_TEST_CASE
( LoadFlushesDirtySettings )
87
{
88
SETTINGS_MANAGER
mgr;
89
90
// Drop the auto-registered common settings so Load() only touches the scratch object
91
mgr.
FlushAndRelease
( mgr.
GetCommonSettings
(),
false
);
92
93
FLUSH_TEST_SETTINGS
* cfg =
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.
112
BOOST_AUTO_TEST_CASE
( LoadDoesNotFlushNeverSyncedSettings )
113
{
114
{
115
FLUSH_TEST_SETTINGS
seed
( Path(
"cold"
) );
116
seed
.m_value = 7;
117
seed
.SaveToFile();
118
}
119
120
SETTINGS_MANAGER
mgr;
121
mgr.
FlushAndRelease
( mgr.
GetCommonSettings
(),
false
);
122
123
FLUSH_TEST_SETTINGS
* cfg =
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
136
BOOST_AUTO_TEST_SUITE_END
()
FLUSH_TEST_SETTINGS
Definition
test_settings_manager.cpp:41
FLUSH_TEST_SETTINGS::m_value
int m_value
Definition
test_settings_manager.cpp:50
FLUSH_TEST_SETTINGS::FLUSH_TEST_SETTINGS
FLUSH_TEST_SETTINGS(const wxString &aFullPath)
Definition
test_settings_manager.cpp:43
JSON_SETTINGS::LoadFromFile
virtual bool LoadFromFile(const wxString &aDirectory="")
Loads the backing file from disk and then calls Load()
Definition
json_settings.cpp:156
JSON_SETTINGS::m_params
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
Definition
json_settings.h:335
JSON_SETTINGS::JSON_SETTINGS
JSON_SETTINGS(const wxString &aFilename, SETTINGS_LOC aLocation, int aSchemaVersion)
Definition
json_settings.h:74
PARAM
Definition
parameters.h:94
SETTINGS_MANAGER
Definition
settings_manager.h:49
SETTINGS_MANAGER::RegisterSettings
T * RegisterSettings(T *aSettings, bool aLoadNow=true)
Take ownership of the pointer passed in.
Definition
settings_manager.h:81
SETTINGS_MANAGER::Load
void Load()
Definition
settings_manager.cpp:181
SETTINGS_MANAGER::GetCommonSettings
COMMON_SETTINGS * GetCommonSettings() const
Retrieve the common settings shared by all applications.
Definition
settings_manager.h:258
SETTINGS_MANAGER::FlushAndRelease
void FlushAndRelease(JSON_SETTINGS *aSettings, bool aSave=true)
If the given settings object is registered, save it to disk and unregister it.
Definition
settings_manager.cpp:238
common_settings.h
NONE
@ NONE
Definition
eda_shape.h:72
json_settings.h
SETTINGS_LOC
SETTINGS_LOC
Definition
json_settings.h:56
parameters.h
settings_manager.h
SETTINGS_MANAGER_FIXTURE
Definition
test_settings_manager.cpp:55
SETTINGS_MANAGER_FIXTURE::m_tempDir
fs::path m_tempDir
Definition
test_settings_manager.cpp:77
SETTINGS_MANAGER_FIXTURE::Path
wxString Path(const std::string &aName) const
Definition
test_settings_manager.cpp:72
SETTINGS_MANAGER_FIXTURE::~SETTINGS_MANAGER_FIXTURE
~SETTINGS_MANAGER_FIXTURE()
Definition
test_settings_manager.cpp:66
SETTINGS_MANAGER_FIXTURE::SETTINGS_MANAGER_FIXTURE
SETTINGS_MANAGER_FIXTURE()
Definition
test_settings_manager.cpp:56
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
Definition
test_api_enums.cpp:71
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
seed
const uint32_t seed
Definition
test_pip_benchmark.cpp:765
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(LoadFlushesDirtySettings)
Definition
test_settings_manager.cpp:86
BOOST_CHECK_EQUAL
BOOST_CHECK_EQUAL(result, "25.4")
unit_test_utils.h
src
qa
tests
common
test_settings_manager.cpp
Generated on Tue Jul 7 2026 00:07:26 for KiCad PCB EDA Suite by
1.13.2