File size: 12,053 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// SPDX-License-Identifier: LGPL-2.1-or-later
/****************************************************************************
 *                                                                          *
 *   Copyright (c) 2025 Kacper Donat <kacper@kadet.net>                     *
 *                                                                          *
 *   This file is part of FreeCAD.                                          *
 *                                                                          *
 *   FreeCAD is free software: you can redistribute it and/or modify it     *
 *   under the terms of the GNU Lesser General Public License as            *
 *   published by the Free Software Foundation, either version 2.1 of the   *
 *   License, or (at your option) any later version.                        *
 *                                                                          *
 *   FreeCAD is distributed in the hope that it will be useful, but         *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of             *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU       *
 *   Lesser General Public License for more details.                        *
 *                                                                          *
 *   You should have received a copy of the GNU Lesser General Public       *
 *   License along with FreeCAD. If not, see                                *
 *   <https://www.gnu.org/licenses/>.                                       *
 *                                                                          *
 ***************************************************************************/

#include <gtest/gtest.h>

#include <Gui/Application.h>
#include <Gui/Utilities.h>
#include <Gui/StyleParameters/ParameterManager.h>

using namespace Gui::StyleParameters;

class ParameterManagerTest: public ::testing::Test
{
protected:
    void SetUp() override
    {
        // Create test sources
        auto source1 = std::make_unique<InMemoryParameterSource>(
            std::list<Parameter> {
                {"BaseSize", "8px"},
                {"PrimaryColor", "#ff0000"},
                {"SecondaryColor", "#00ff00"},
            },
            ParameterSource::Metadata {"Source 1"}
        );

        auto source2 = std::make_unique<InMemoryParameterSource>(
            std::list<Parameter> {
                {"BaseSize", "16px"},  // Override from source1
                {"Margin", "@BaseSize * 2"},
                {"Padding", "@BaseSize / 2"},
            },
            ParameterSource::Metadata {"Source 2"}
        );

        manager.addSource(source1.get());
        manager.addSource(source2.get());
        sources.push_back(std::move(source1));
        sources.push_back(std::move(source2));
    }

    Gui::StyleParameters::ParameterManager manager;
    std::vector<std::unique_ptr<ParameterSource>> sources;
};

// Test basic parameter resolution
TEST_F(ParameterManagerTest, BasicParameterResolution)
{
    {
        auto result = manager.resolve("BaseSize");
        EXPECT_TRUE(result.has_value());
        EXPECT_TRUE(std::holds_alternative<Numeric>(*result));
        auto length = std::get<Numeric>(*result);
        EXPECT_DOUBLE_EQ(length.value, 16.0);  // Should get value from source2 (later source)
        EXPECT_EQ(length.unit, "px");
    }

    {
        auto result = manager.resolve("PrimaryColor");
        EXPECT_TRUE(result.has_value());
        EXPECT_TRUE(std::holds_alternative<Base::Color>(*result));
        auto color = std::get<Base::Color>(*result);
        EXPECT_EQ(color.r, 1);
        EXPECT_EQ(color.g, 0);
        EXPECT_EQ(color.b, 0);
    }

    {
        auto result = manager.resolve("SecondaryColor");
        EXPECT_TRUE(result.has_value());
        EXPECT_TRUE(std::holds_alternative<Base::Color>(*result));
        auto color = std::get<Base::Color>(*result);
        EXPECT_EQ(color.r, 0);
        EXPECT_EQ(color.g, 1);
        EXPECT_EQ(color.b, 0);
    }
}

// Test parameter references
TEST_F(ParameterManagerTest, ParameterReferences)
{
    {
        auto result = manager.resolve("Margin");
        EXPECT_TRUE(std::holds_alternative<Numeric>(*result));
        auto length = std::get<Numeric>(*result);
        EXPECT_DOUBLE_EQ(length.value, 32.0);  // @BaseSize * 2 = 16 * 2 = 32
        EXPECT_EQ(length.unit, "px");
    }

    {
        auto result = manager.resolve("Padding");
        EXPECT_TRUE(std::holds_alternative<Numeric>(*result));
        auto length = std::get<Numeric>(*result);
        EXPECT_DOUBLE_EQ(length.value, 8.0);  // @BaseSize / 2 = 16 / 2 = 8
        EXPECT_EQ(length.unit, "px");
    }
}

// Test caching
TEST_F(ParameterManagerTest, Caching)
{
    // First resolution should cache the result
    auto result1 = manager.resolve("BaseSize");
    EXPECT_TRUE(std::holds_alternative<Numeric>(*result1));

    // Second resolution should use cached value
    auto result2 = manager.resolve("BaseSize");
    EXPECT_TRUE(std::holds_alternative<Numeric>(*result2));

    // Results should be identical
    auto length1 = std::get<Numeric>(*result1);
    auto length2 = std::get<Numeric>(*result2);
    EXPECT_DOUBLE_EQ(length1.value, length2.value);
    EXPECT_EQ(length1.unit, length2.unit);
}

// Test cache invalidation
TEST_F(ParameterManagerTest, CacheInvalidation)
{
    // Initial resolution
    auto result1 = manager.resolve("BaseSize");
    EXPECT_TRUE(std::holds_alternative<Numeric>(*result1));
    auto length1 = std::get<Numeric>(*result1);
    EXPECT_DOUBLE_EQ(length1.value, 16.0);

    // Reload should clear cache
    manager.reload();

    // Resolution after reload should work the same
    auto result2 = manager.resolve("BaseSize");
    EXPECT_TRUE(std::holds_alternative<Numeric>(*result2));
    auto length2 = std::get<Numeric>(*result2);
    EXPECT_DOUBLE_EQ(length2.value, 16.0);
    EXPECT_EQ(length1.unit, length2.unit);
}

// Test source priority
TEST_F(ParameterManagerTest, SourcePriority)
{
    // Create a third source with higher priority
    auto source3 = std::make_unique<InMemoryParameterSource>(
        std::list<Parameter> {
            {"BaseSize", "24px"},  // Should override both previous sources
        },
        ParameterSource::Metadata {"Source 3"}
    );

    manager.addSource(source3.get());
    sources.push_back(std::move(source3));

    // Should get value from the latest source (highest priority)
    auto result = manager.resolve("BaseSize");
    EXPECT_TRUE(std::holds_alternative<Numeric>(*result));
    auto length = std::get<Numeric>(*result);
    EXPECT_DOUBLE_EQ(length.value, 24.0);
    EXPECT_EQ(length.unit, "px");
}

// Test parameter listing
TEST_F(ParameterManagerTest, ParameterListing)
{
    auto params = manager.parameters();

    // Should contain all parameters from all sources
    std::set<std::string> paramNames;
    for (const auto& param : params) {
        paramNames.insert(param.name);
    }

    EXPECT_TRUE(paramNames.contains("BaseSize"));
    EXPECT_TRUE(paramNames.contains("PrimaryColor"));
    EXPECT_TRUE(paramNames.contains("SecondaryColor"));
    EXPECT_TRUE(paramNames.contains("Margin"));
    EXPECT_TRUE(paramNames.contains("Padding"));

    // Should not contain duplicates (BaseSize should appear only once)
    EXPECT_EQ(paramNames.count("BaseSize"), 1);
}

// Test expression retrieval
TEST_F(ParameterManagerTest, ExpressionRetrieval)
{
    {
        auto expr = manager.expression("BaseSize");
        EXPECT_TRUE(expr.has_value());
        EXPECT_EQ(*expr, "16px");
    }

    {
        auto expr = manager.expression("Margin");
        EXPECT_TRUE(expr.has_value());
        EXPECT_EQ(*expr, "@BaseSize * 2");
    }

    {
        auto expr = manager.expression("NonExistent");
        EXPECT_FALSE(expr.has_value());
    }
}

// Test parameter retrieval
TEST_F(ParameterManagerTest, ParameterRetrieval)
{
    {
        auto param = manager.parameter("BaseSize");
        EXPECT_TRUE(param.has_value());
        EXPECT_EQ(param->name, "BaseSize");
        EXPECT_EQ(param->value, "16px");
    }

    {
        auto param = manager.parameter("NonExistent");
        EXPECT_FALSE(param.has_value());
    }
}

// Test source management
TEST_F(ParameterManagerTest, SourceManagement)
{
    auto sources = manager.sources();
    EXPECT_EQ(sources.size(), 2);  // We added 2 sources in SetUp

    // Test that we can access the sources
    for (auto source : sources) {
        EXPECT_NE(source, nullptr);
        auto params = source->all();
        EXPECT_FALSE(params.empty());
    }
}

// Test circular reference detection
TEST_F(ParameterManagerTest, CircularReferenceDetection)
{
    // Create a source with circular reference
    auto circularSource = std::make_unique<InMemoryParameterSource>(
        std::list<Parameter> {
            {"A", "@B"},
            {"B", "@A"},
        },
        ParameterSource::Metadata {"Circular Source"}
    );

    manager.addSource(circularSource.get());
    sources.push_back(std::move(circularSource));

    // Should handle circular reference gracefully
    auto result = manager.resolve("A");
    // Should return the expression string as fallback
    EXPECT_TRUE(std::holds_alternative<std::string>(*result));
}

// Test complex expressions
TEST_F(ParameterManagerTest, ComplexExpressions)
{
    // Create a source with complex expressions
    auto complexSource = std::make_unique<InMemoryParameterSource>(
        std::list<Parameter> {
            {"ComplexMargin", "(@BaseSize + 4px) * 2"},
            {"ComplexPadding", "(@BaseSize - 2px) / 2"},
            {"ColorWithFunction", "lighten(@PrimaryColor, 20)"},
        },
        ParameterSource::Metadata {"Complex Source"}
    );

    manager.addSource(complexSource.get());
    sources.push_back(std::move(complexSource));

    {
        auto result = manager.resolve("ComplexMargin");
        EXPECT_TRUE(std::holds_alternative<Numeric>(*result));
        auto length = std::get<Numeric>(*result);
        EXPECT_DOUBLE_EQ(length.value, 40.0);  // (16 + 4) * 2 = 20 * 2 = 40
        EXPECT_EQ(length.unit, "px");
    }

    {
        auto result = manager.resolve("ComplexPadding");
        EXPECT_TRUE(std::holds_alternative<Numeric>(*result));
        auto length = std::get<Numeric>(*result);
        EXPECT_DOUBLE_EQ(length.value, 7.0);  // (16 - 2) / 2 = 14 / 2 = 7
        EXPECT_EQ(length.unit, "px");
    }

    {
        auto result = manager.resolve("ColorWithFunction");
        EXPECT_TRUE(std::holds_alternative<Base::Color>(*result));
        auto color = std::get<Base::Color>(*result).asValue<QColor>();
        // Should be lighter than the original red
        EXPECT_GT(color.lightness(), QColor(0xff0000).lightness());
    }
}

// Test error handling
TEST_F(ParameterManagerTest, ErrorHandling)
{
    // Test non-existent parameter
    auto result = manager.resolve("NonExistent");
    EXPECT_FALSE(result.has_value());

    // Test invalid expression
    auto invalidSource = std::make_unique<InMemoryParameterSource>(
        std::list<Parameter> {
            {"Invalid", "invalid expression that will fail"},
        },
        ParameterSource::Metadata {"Invalid Source"}
    );

    manager.addSource(invalidSource.get());
    sources.push_back(std::move(invalidSource));

    // Should handle invalid expression gracefully
    auto invalidResult = manager.resolve("Invalid");
    // Should return the expression string as fallback
    EXPECT_TRUE(invalidResult.has_value());
    EXPECT_TRUE(std::holds_alternative<std::string>(*invalidResult));
}

DEFINE_STYLE_PARAMETER(BaseSize, Numeric(8, "px"));

TEST_F(ParameterManagerTest, ResolveParameterDefinition)
{
    auto result = manager.resolve(BaseSize);
    EXPECT_DOUBLE_EQ(result.value, 16);
    EXPECT_EQ(result.unit, "px");
}


DEFINE_STYLE_PARAMETER(MarginSize, Numeric(16, "px"));

TEST_F(ParameterManagerTest, ResolveParameterDefinitionDefault)
{
    auto result = manager.resolve(MarginSize);
    EXPECT_DOUBLE_EQ(result.value, 16);
    EXPECT_EQ(result.unit, "px");
}