Blender V5.3
recents_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include <map>
6#include <string>
7#include <vector>
8
10#include "BKE_global.hh"
11#include "BKE_gtest_base.hh"
12#include "BKE_recents.hh"
13
14#include "testing/testing.h"
15#include "gmock/gmock.h"
16
17namespace blender::bke::tests {
18
19using namespace blender::recents;
20using testing::ElementsAre;
21using testing::FloatEq;
22
23using TableArray = std::vector<std::map<std::string, std::string>>;
24
25/* Runs with `G.background = true` so the module never touches the real
26 * on-disk user config while the test suite is running. */
28 protected:
29 bool background_backup_ = false;
30
31 void SetUp() override
32 {
33 background_backup_ = G.background;
34 G.background = true;
36 }
37
38 void TearDown() override
39 {
40 G.background = background_backup_;
41 }
42};
43
44TEST_F(RecentsTest, GetUnsetValueReturnsTypeDefault)
45{
46 Section section("unit_test.get_unset_value");
47 EXPECT_EQ(section.get<std::string>("missing"), "");
48 EXPECT_EQ(section.get<bool>("missing"), false);
49 EXPECT_EQ(section.get<int32_t>("missing"), 0);
50 EXPECT_EQ(section.get<float>("missing"), 0.0f);
51}
52
53TEST_F(RecentsTest, SetThenGetRoundTrip)
54{
55 Section section("unit_test.set_then_get");
56
57 section.set<std::string>("str", "hello");
58 EXPECT_EQ(section.get<std::string>("str"), "hello");
59
60 section.set<bool>("flag", true);
61 EXPECT_EQ(section.get<bool>("flag"), true);
62
63 section.set<int32_t>("count", -42);
64 EXPECT_EQ(section.get<int32_t>("count"), -42);
65
66 section.set<float>("scale", 1.5f);
67 EXPECT_FLOAT_EQ(section.get<float>("scale"), 1.5f);
68
69 section.set<std::vector<int>>("indices", {1, 2, 3});
70 EXPECT_THAT(section.get<std::vector<int>>("indices"), ElementsAre(1, 2, 3));
71
72 section.remove_section();
73}
74
75TEST_F(RecentsTest, RootSectionStoresAtTopLevel)
76{
77 /* An empty section name reads/writes keys at the document root. */
78 Section root("");
79 root.set<int32_t>("unit_test_root_key", 7);
80 EXPECT_EQ(root.get<int32_t>("unit_test_root_key"), 7);
81
82 root.remove("unit_test_root_key");
83 EXPECT_EQ(root.get<int32_t>("unit_test_root_key"), 0);
84}
85
86TEST_F(RecentsTest, SetInSectionThatIsNotATable)
87{
88 /* A section name used as a plain value, for example in an edited file, is replaced. */
89 Section root("");
90 root.set<int32_t>("unit_test.not_a_table", 1);
91
92 Section section("unit_test.not_a_table");
93 section.set<int32_t>("value", 2);
94 EXPECT_EQ(section.get<int32_t>("value"), 2);
95
96 section.remove_section();
97}
98
99TEST_F(RecentsTest, GetWrongTypeReturnsTypeDefault)
100{
101 Section section("unit_test.wrong_type");
102 section.set<std::string>("value", "text");
103 EXPECT_EQ(section.get<int32_t>("value"), 0);
104 EXPECT_TRUE(section.get<TableArray>("value").empty());
105
106 section.remove_section();
107}
108
109TEST_F(RecentsTest, ArrayOfTablesRoundTrip)
110{
111 Section section("unit_test.array_of_tables");
112
113 const TableArray items = {{{"a", "1"}, {"b", "2"}}, {{"a", "3"}}};
114 section.set<TableArray>("items", items);
115 EXPECT_EQ(section.get<TableArray>("items"), items);
116
117 section.remove_section();
118}
119
120TEST_F(RecentsTest, BuiltinRootDefaultsAreReadable)
121{
122 Section root("");
123 EXPECT_EQ(root.get<std::string>("title"), "Saved UI State Settings");
124 EXPECT_EQ(root.get<std::string>("name"), "Blender");
125}
126
127TEST_F(RecentsTest, SectionsAreIsolated)
128{
129 Section a("unit_test.section_a");
130 Section b("unit_test.section_b");
131
132 a.set<int32_t>("value", 1);
133 b.set<int32_t>("value", 2);
134
135 EXPECT_EQ(a.get<int32_t>("value"), 1);
136 EXPECT_EQ(b.get<int32_t>("value"), 2);
137
138 a.remove_section();
139 b.remove_section();
140}
141
142TEST_F(RecentsTest, RemoveItemFallsBackToDefault)
143{
144 Section section("unit_test.remove_item");
145 section.set<int32_t>("value", 99);
146 ASSERT_EQ(section.get<int32_t>("value"), 99);
147
148 section.remove("value");
149 EXPECT_EQ(section.get<int32_t>("value"), 0);
150}
151
152TEST_F(RecentsTest, RemoveSectionClearsAllItems)
153{
154 Section section("unit_test.remove_section");
155 section.set<int32_t>("a", 1);
156 section.set<int32_t>("b", 2);
157 ASSERT_EQ(section.get<int32_t>("a"), 1);
158 ASSERT_EQ(section.get<int32_t>("b"), 2);
159
160 section.remove_section();
161
162 EXPECT_EQ(section.get<int32_t>("a"), 0);
163 EXPECT_EQ(section.get<int32_t>("b"), 0);
164}
165
166TEST_F(RecentsTest, BuiltinSectionLevelDefault)
167{
168 /* `panel.sortorder` and `panel.open` only define a section-level
169 * `_default`, so any unknown key should fall back to it. */
170 EXPECT_EQ(Section("panel.sortorder").get<int32_t>("unit_test_unknown_key"), -1);
171 EXPECT_EQ(Section("panel.open").get<bool>("unit_test_unknown_key"), true);
172}
173
174TEST_F(RecentsTest, PerKeyDefaultOverridesSectionLevelDefault)
175{
176 Section dims("temp.window.dimensions");
177
178 /* `PREFERENCES` has its own per-key default distinct from `_default`. */
179 EXPECT_THAT(dims.get<std::vector<float>>("PREFERENCES"),
180 ElementsAre(FloatEq(100.0f), FloatEq(940.0f), FloatEq(350.0f), FloatEq(900.0f)));
181
182 /* An editor type with no specific default falls back to the section's `_default`. */
183 EXPECT_THAT(dims.get<std::vector<float>>("UNIT_TEST_UNKNOWN_EDITOR"),
184 ElementsAre(FloatEq(100.0f), FloatEq(900.0f), FloatEq(200.0f), FloatEq(800.0f)));
185}
186
187TEST_F(RecentsTest, UserValueOverridesBuiltinDefault)
188{
189 Section dims("temp.window.dimensions");
190 const std::vector<float> builtin_default = dims.get<std::vector<float>>("PREFERENCES");
191
192 const std::vector<float> user_value = {10.0f, 20.0f, 30.0f, 40.0f};
193 dims.set<std::vector<float>>("PREFERENCES", user_value);
194 EXPECT_THAT(dims.get<std::vector<float>>("PREFERENCES"),
195 ElementsAre(FloatEq(10.0f), FloatEq(20.0f), FloatEq(30.0f), FloatEq(40.0f)));
196
197 /* Removing the user override should reveal the builtin default again. */
198 dims.remove("PREFERENCES");
199 EXPECT_THAT(dims.get<std::vector<float>>("PREFERENCES"),
200 ElementsAre(FloatEq(builtin_default[0]),
201 FloatEq(builtin_default[1]),
202 FloatEq(builtin_default[2]),
203 FloatEq(builtin_default[3])));
204}
205
206TEST_F(RecentsTest, UserSectionDefaultOverridesBuiltinSectionDefault)
207{
208 Section sortorder("panel.sortorder");
209 ASSERT_EQ(sortorder.get<int32_t>("unit_test_unknown_key"), -1);
210
211 sortorder.set<int32_t>("_default", 5);
212 EXPECT_EQ(sortorder.get<int32_t>("unit_test_unknown_key"), 5);
213
214 /* Restore shared builtin-section state for any other test/run in this process. */
215 sortorder.remove("_default");
216 EXPECT_EQ(sortorder.get<int32_t>("unit_test_unknown_key"), -1);
217}
218
219TEST_F(RecentsTest, SaveIsNoOpInBackgroundMode)
220{
221 /* The fixture forces background mode, so `save()` must never touch disk. */
223}
224
225TEST_F(RecentsTest, SaveStampsBlenderVersion)
226{
227 /* `save()` still stamps the current Blender version onto the in-memory data
228 * even in background mode, though the no-op path skips writing it to disk. */
229 ASSERT_FALSE(save());
230 EXPECT_EQ(Section("").get<int32_t>("blender_version"), BLENDER_VERSION);
231}
232
233TEST_F(RecentsTest, ExplicitInitIsSafeToCallAgain)
234{
235 /* In background mode `init()` only re-parses the builtin defaults and must
236 * not disturb values already set by user code. */
237 Section section("unit_test.explicit_init");
238 section.set<int32_t>("value", 3);
239
240 init();
241
242 EXPECT_EQ(section.get<int32_t>("value"), 3);
243 section.remove_section();
244}
245
246} // namespace blender::bke::tests
#define BLENDER_VERSION
T get(const std::string &item_key) const
Definition recents.cc:224
Section(std::string section)
void set(const std::string &item_key, const T &value)
Definition recents.cc:285
void remove(const std::string &item_key)
Definition recents.cc:311
#define EXPECT_FALSE(result)
#define EXPECT_TRUE(result)
TEST_F(AnimDataTest, BKE_fcurves_id_cb_test)
std::vector< std::map< std::string, std::string > > TableArray
void ensure_init()
Definition recents.cc:129
Global G
Definition blender.cc:57
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
signed int int32_t
Definition BKE_text.h:78