Skip to content

Commit 2ed7ffc

Browse files
Johnson-zsdeepin-bot[bot]
authored andcommitted
test: add comprehensive unit tests for dfm-base
1. Add test files for ConfigSynchronizer, CustomSettingItemRegister, DConfigManager, DefenderController, DeviceAliasManager, DeviceHelper, DeviceManager, DeviceProxyManager, DeviceUtils, DeviceWatcher, DiscDeviceScanner, SchemeFactory, SettingBackend, SettingJsonGenerator, Settings, SqliteConnectionPool, SqliteHandle, SqliteHelper, SqliteQueryable, StandardPaths, and UrlRoute 2. Move existing test_application.cpp and test_settings.cpp to proper base/ subdirectory 3. Remove unused synchronous device operation functions and related code from DeviceManager and DeviceUtils 4. Fix StandardPaths::getCachePath() use-after-free bug by storing QDir and building path properly 5. Clean up commented code and unused functions in MimesAppsManager Log: Added comprehensive unit test coverage for dfm-base library components Influence: 1. Test new ConfigSynchronizer singleton and SyncPair serialization 2. Test CustomSettingItemRegister registration functionality 3. Test DConfigManager configuration operations 4. Test DefenderController scanning detection and stopping 5. Test DeviceAliasManager protocol alias management 6. Test DeviceHelper device creation and info loading 7. Test DeviceManager device enumeration and async operations 8. Test DeviceProxyManager DBus proxy functionality 9. Test DeviceUtils utility functions and path handling 10. Test DeviceWatcher device monitoring 11. Test DiscDeviceScanner optical disc detection 12. Test SchemeFactory URL scheme registration 13. Test SettingBackend configuration management 14. Test SettingJsonGenerator JSON configuration generation 15. Test SqliteConnectionPool database connection pooling 16. Test SqliteHandle CRUD operations and transactions 17. Test SqliteHelper serialization and expressions 18. Test SqliteQueryable query building and filtering 19. Test StandardPaths location and URL conversion 20. Test UrlRoute scheme management and URL handling
1 parent 96cb830 commit 2ed7ffc

31 files changed

+11201
-242
lines changed
File renamed without changes.
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
// SPDX-FileCopyrightText: 2025 UnionTech Software Technology Co., Ltd.
2+
//
3+
// SPDX-License-Identifier: GPL-3.0-or-later
4+
5+
#include <gtest/gtest.h>
6+
#include <QCoreApplication>
7+
#include <QVariant>
8+
9+
#include "stubext.h"
10+
11+
#include <dfm-base/base/configs/configsynchronizer.h>
12+
#include <dfm-base/base/configs/configsyncdefs.h>
13+
14+
DFMBASE_USE_NAMESPACE
15+
16+
class TestConfigSynchronizer : public testing::Test
17+
{
18+
public:
19+
void SetUp() override
20+
{
21+
// Initialize ConfigSynchronizer instance
22+
synchronizer = ConfigSynchronizer::instance();
23+
ASSERT_TRUE(synchronizer);
24+
}
25+
26+
void TearDown() override
27+
{
28+
stub.clear();
29+
}
30+
31+
protected:
32+
ConfigSynchronizer *synchronizer;
33+
stub_ext::StubExt stub;
34+
};
35+
36+
TEST_F(TestConfigSynchronizer, SingletonCreation_Basic)
37+
{
38+
// Test that instance returns the same pointer
39+
ConfigSynchronizer *instance1 = ConfigSynchronizer::instance();
40+
ConfigSynchronizer *instance2 = ConfigSynchronizer::instance();
41+
EXPECT_EQ(instance1, instance2);
42+
}
43+
44+
TEST_F(TestConfigSynchronizer, WatchChange)
45+
{
46+
// Test watchChange with valid SyncPair
47+
SyncPair validPair;
48+
validPair.set.type = SettingType::kGenAttr;
49+
validPair.set.val = 1; // Represents some application attribute
50+
validPair.cfg.path = "dfm.settings";
51+
validPair.cfg.key = "general.view.mode";
52+
53+
// Since the real watchChange might have dependencies, let's stub it
54+
bool watchCalled = false;
55+
stub.set_lamda(ADDR(ConfigSynchronizer, watchChange), [&](ConfigSynchronizer *obj, const SyncPair &pair) {
56+
Q_UNUSED(obj);
57+
watchCalled = true;
58+
EXPECT_TRUE(pair.isValid());
59+
EXPECT_EQ(pair.cfg.path, "dfm.settings");
60+
EXPECT_EQ(pair.cfg.key, "general.view.mode");
61+
return true; // Return success
62+
});
63+
64+
bool result = synchronizer->watchChange(validPair);
65+
EXPECT_TRUE(result);
66+
EXPECT_TRUE(watchCalled);
67+
}
68+
69+
TEST_F(TestConfigSynchronizer, WatchChange_InvalidPair)
70+
{
71+
// Test watchChange with invalid SyncPair
72+
SyncPair invalidPair;
73+
// pair is invalid because set.type is kNone and cfg fields are empty
74+
75+
// Test that watchChange rejects invalid pairs even without stub
76+
bool result = synchronizer->watchChange(invalidPair);
77+
EXPECT_FALSE(result);
78+
}
79+
80+
// Test SyncPair struct separately since it's a standalone struct with utility functions
81+
TEST(TestSyncPair, IsValid)
82+
{
83+
// Test valid SyncPair
84+
SyncPair validPair;
85+
validPair.set.type = SettingType::kAppAttr;
86+
validPair.set.val = 2;
87+
validPair.cfg.path = "test.config";
88+
validPair.cfg.key = "test.key";
89+
EXPECT_TRUE(validPair.isValid());
90+
91+
// Test invalid SyncPair - missing type
92+
SyncPair invalidPair1;
93+
invalidPair1.set.type = SettingType::kNone;
94+
invalidPair1.cfg.path = "test.config";
95+
invalidPair1.cfg.key = "test.key";
96+
EXPECT_FALSE(invalidPair1.isValid());
97+
98+
// Test invalid SyncPair - missing cfg.path
99+
SyncPair invalidPair2;
100+
invalidPair2.set.type = SettingType::kGenAttr;
101+
invalidPair2.set.val = 3;
102+
invalidPair2.cfg.key = "test.key";
103+
EXPECT_FALSE(invalidPair2.isValid());
104+
105+
// Test invalid SyncPair - missing cfg.key
106+
SyncPair invalidPair3;
107+
invalidPair3.set.type = SettingType::kGenAttr;
108+
invalidPair3.set.val = 4;
109+
invalidPair3.cfg.path = "test.config";
110+
EXPECT_FALSE(invalidPair3.isValid());
111+
}
112+
113+
TEST(TestSyncPair, Serialize)
114+
{
115+
// Test serialization of SyncPair
116+
SyncPair pair;
117+
pair.set.type = SettingType::kAppAttr;
118+
pair.set.val = 5;
119+
pair.cfg.path = "app/config/path";
120+
pair.cfg.key = "app_setting_key";
121+
122+
QString serialized = pair.serialize();
123+
// Expected format: "type/val:path/key" - kAppAttr is 0
124+
EXPECT_EQ(serialized, "0/5:app/config/path/app_setting_key");
125+
126+
// Test serialization with static method
127+
QString staticSerialized = SyncPair::serialize(pair.set, pair.cfg);
128+
EXPECT_EQ(staticSerialized, serialized);
129+
130+
// Test serialization with kNone type (special case)
131+
SyncPair nonePair;
132+
nonePair.set.type = SettingType::kNone;
133+
nonePair.cfg.path = "only/config/path";
134+
nonePair.cfg.key = "only_key";
135+
QString noneSerialized = nonePair.serialize();
136+
EXPECT_EQ(noneSerialized, ":only/config/path/only_key");
137+
138+
// Test serialization with empty cfg (another special case)
139+
SyncPair cfgEmptyPair;
140+
cfgEmptyPair.set.type = SettingType::kGenAttr;
141+
cfgEmptyPair.set.val = 6;
142+
QString cfgEmptySerialized = cfgEmptyPair.serialize();
143+
EXPECT_EQ(cfgEmptySerialized, "1/6:");
144+
}
145+
146+
TEST(TestSyncPair, FunctionTypes)
147+
{
148+
// Test that the function type aliases work correctly
149+
int testValue = 0;
150+
151+
// Test ConfigSaver
152+
ConfigSaver saver = [&](const QVariant &val) {
153+
testValue = val.toInt();
154+
};
155+
156+
saver(42);
157+
EXPECT_EQ(testValue, 42);
158+
159+
// Test SyncToAppSet
160+
QString savedConfig;
161+
QString savedKey;
162+
QVariant savedValue;
163+
164+
SyncToAppSet syncToApp = [&](const QString &config, const QString &key, const QVariant &value) {
165+
savedConfig = config;
166+
savedKey = key;
167+
savedValue = value;
168+
};
169+
170+
syncToApp("test.cfg", "test.key", "test.val");
171+
EXPECT_EQ(savedConfig, "test.cfg");
172+
EXPECT_EQ(savedKey, "test.key");
173+
EXPECT_EQ(savedValue.toString(), "test.val");
174+
175+
// Test IsConfEqual
176+
IsConfEqual isEqual = [](const QVariant &dconfVal, const QVariant &dsetVal) {
177+
return dconfVal.toString().toLower() == dsetVal.toString().toLower();
178+
};
179+
180+
EXPECT_TRUE(isEqual("Value1", "value1"));
181+
EXPECT_FALSE(isEqual("Value1", "Value2"));
182+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// SPDX-FileCopyrightText: 2025 UnionTech Software Technology Co., Ltd.
2+
//
3+
// SPDX-License-Identifier: GPL-3.0-or-later
4+
5+
#include <gtest/gtest.h>
6+
7+
#include "stubext.h"
8+
9+
#include <dfm-base/settingdialog/customsettingitemregister.h>
10+
11+
TEST(TestCustomSettingItemRegister, SingletonCreation_Basic)
12+
{
13+
using namespace dfmbase;
14+
15+
CustomSettingItemRegister *instance1 = CustomSettingItemRegister::instance();
16+
CustomSettingItemRegister *instance2 = CustomSettingItemRegister::instance();
17+
18+
EXPECT_EQ(instance1, instance2);
19+
EXPECT_NE(instance1, nullptr);
20+
}
21+
22+
TEST(TestCustomSettingItemRegister, RegistCustomSettingItemType)
23+
{
24+
using namespace dfmbase;
25+
26+
CustomSettingItemRegister *registerer = CustomSettingItemRegister::instance();
27+
ASSERT_NE(registerer, nullptr);
28+
29+
// Create a simple creator function - returns a pair of QWidget pointers
30+
CustomSettingItemCreator creator = [](QObject *parent) -> std::pair<QWidget *, QWidget *> {
31+
Q_UNUSED(parent);
32+
return { nullptr, nullptr };
33+
};
34+
35+
// Test registering a new type
36+
const QString typeName = "test-custom-item";
37+
bool result = registerer->registCustomSettingItemType(typeName, creator);
38+
EXPECT_TRUE(result);
39+
40+
// Test registering the same type again should fail
41+
bool resultAgain = registerer->registCustomSettingItemType(typeName, creator);
42+
EXPECT_FALSE(resultAgain);
43+
}
44+
45+
TEST(TestCustomSettingItemRegister, GetCreators)
46+
{
47+
using namespace dfmbase;
48+
49+
CustomSettingItemRegister *registerer = CustomSettingItemRegister::instance();
50+
ASSERT_NE(registerer, nullptr);
51+
52+
// Get creators before any registration
53+
const QMap<QString, CustomSettingItemCreator> &creatorsBefore = registerer->getCreators();
54+
55+
// Create a simple creator function
56+
CustomSettingItemCreator creator = [](QObject *parent) -> std::pair<QWidget *, QWidget *> {
57+
Q_UNUSED(parent);
58+
return { nullptr, nullptr };
59+
};
60+
61+
// Register a new type
62+
const QString typeName = "test-custom-widget";
63+
registerer->registCustomSettingItemType(typeName, creator);
64+
65+
// Get creators after registration
66+
const QMap<QString, CustomSettingItemCreator> &creatorsAfter = registerer->getCreators();
67+
68+
// Test that we have more creators after registration
69+
EXPECT_TRUE(creatorsAfter.size() >= creatorsBefore.size() + 1);
70+
}

0 commit comments

Comments
 (0)