-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathlifecyclecallback_unittest.cc
More file actions
292 lines (241 loc) · 7.41 KB
/
lifecyclecallback_unittest.cc
File metadata and controls
292 lines (241 loc) · 7.41 KB
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
#include "lifecyclecallback.hpp"
#include <QObject>
#include <QtTest>
#include <functional>
#include <memory>
// 测试用的辅助类
class TestQObject : public QObject
{
Q_OBJECT
public:
int callCount = 0;
QString lastMessage;
QJsonObject lastJson;
public slots:
void handleMessage(const QString &message)
{
callCount++;
lastMessage = message;
}
void handleJson(const QJsonObject &json)
{
callCount++;
lastJson = json;
}
void handleProgress(qint64 downloaded, qint64 total)
{
callCount++;
lastMessage = QString("%1/%2").arg(downloaded).arg(total);
}
};
class TestSharedObject
{
public:
int callCount = 0;
QString lastMessage;
QJsonObject lastJson;
void handleMessage(const QString &message)
{
callCount++;
lastMessage = message;
}
void handleJson(const QJsonObject &json)
{
callCount++;
lastJson = json;
}
void handleProgress(qint64 downloaded, qint64 total)
{
callCount++;
lastMessage = QString("%1/%2").arg(downloaded).arg(total);
}
};
// 自由函数
void freeFunctionMessage(const QString &message)
{
static int callCount = 0;
callCount++;
Q_UNUSED(message)
Q_UNUSED(callCount)
}
void freeFunctionJson(const QJsonObject &json)
{
static int callCount = 0;
callCount++;
Q_UNUSED(json)
Q_UNUSED(callCount)
}
class TestLifecycleCallback : public QObject
{
Q_OBJECT
private slots:
// 测试默认构造
void testDefaultConstruction()
{
LifecycleCallback<> callback;
QVERIFY(!callback);
callback(); // 应该不会崩溃
}
// 测试自由函数回调
void testFreeFunction()
{
LifecycleCallback<const QString &> callback(freeFunctionMessage);
QVERIFY(callback);
callback("test message");
LifecycleCallback<const QJsonObject &> jsonCallback(freeFunctionJson);
QVERIFY(jsonCallback);
QJsonObject testJson;
testJson["key"] = "value";
jsonCallback(testJson);
}
// 测试 Lambda 回调
void testLambda()
{
int callCount = 0;
QString lastMessage;
LifecycleCallback<const QString &> callback([&](const QString &msg) {
callCount++;
lastMessage = msg;
});
QVERIFY(callback);
callback("lambda test");
QCOMPARE(callCount, 1);
QCOMPARE(lastMessage, "lambda test");
callback("another test");
QCOMPARE(callCount, 2);
QCOMPARE(lastMessage, "another test");
}
// 测试 QObject 成员函数回调
void testQObjectMember_alive()
{
auto *testObj = new TestQObject;
LifecycleCallback<const QString &> callback
= makeLifecycleCallback(testObj, &TestQObject::handleMessage);
QVERIFY(callback);
callback("qobject test");
QCOMPARE(testObj->callCount, 1);
QCOMPARE(testObj->lastMessage, "qobject test");
delete testObj;
}
// 测试 QObject 成员函数回调(对象销毁)
void testQObjectMember_destroyed()
{
auto *testObj = new TestQObject;
LifecycleCallback<const QString &> callback
= makeLifecycleCallback(testObj, &TestQObject::handleMessage);
QVERIFY(callback);
delete testObj;
QVERIFY(!callback);
callback("should not be handled");
}
// 测试 std::shared_ptr 成员函数回调
void testSharedPtrMember_alive()
{
auto testObj = std::make_shared<TestSharedObject>();
LifecycleCallback<const QString &> callback
= makeLifecycleCallback(testObj, &TestSharedObject::handleMessage);
QVERIFY(callback);
callback("shared_ptr test");
QCOMPARE(testObj->callCount, 1);
QCOMPARE(testObj->lastMessage, "shared_ptr test");
}
// 测试 std::shared_ptr 成员函数回调(对象销毁)
void testSharedPtrMember_destroyed()
{
auto testObj = std::make_shared<TestSharedObject>();
LifecycleCallback<const QString &> callback
= makeLifecycleCallback(testObj, &TestSharedObject::handleMessage);
QVERIFY(callback);
testObj.reset();
QVERIFY(!callback);
callback("should not be handled");
}
// 测试多种参数类型
void testMultipleParameters()
{
// 测试无参数
int callCountNoArgs = 0;
LifecycleCallback<> noArgsCallback([&]() { callCountNoArgs++; });
noArgsCallback();
QCOMPARE(callCountNoArgs, 1);
// 测试单个参数
int callCountOneArg = 0;
QString lastArg;
LifecycleCallback<const QString &> oneArgCallback([&](const QString &arg) {
callCountOneArg++;
lastArg = arg;
});
oneArgCallback("test");
QCOMPARE(callCountOneArg, 1);
QCOMPARE(lastArg, "test");
// 测试多个参数
int callCountMultiArgs = 0;
qint64 lastDownloaded = 0, lastTotal = 0;
LifecycleCallback<qint64, qint64> multiArgsCallback([&](qint64 downloaded, qint64 total) {
callCountMultiArgs++;
lastDownloaded = downloaded;
lastTotal = total;
});
multiArgsCallback(100, 1000);
QCOMPARE(callCountMultiArgs, 1);
QCOMPARE(lastDownloaded, 100);
QCOMPARE(lastTotal, 1000);
}
// 测试移动语义
void testMoveSemantics()
{
int callCount1 = 0;
LifecycleCallback<> callback1([&]() { callCount1++; });
LifecycleCallback<> callback2(std::move(callback1));
QVERIFY(!callback1);
QVERIFY(callback2);
callback2();
QCOMPARE(callCount1, 1);
}
// 测试工厂函数
void testFactoryFunctions()
{
auto *qobject = new TestQObject;
auto qobjectCallback = makeLifecycleCallback(qobject, &TestQObject::handleMessage);
qobjectCallback("factory test");
QCOMPARE(qobject->callCount, 1);
delete qobject;
auto sharedObj = std::make_shared<TestSharedObject>();
auto sharedCallback = makeLifecycleCallback(sharedObj, &TestSharedObject::handleMessage);
sharedCallback("shared factory test");
QCOMPARE(sharedObj->callCount, 1);
}
// 测试空 std::function
void testEmptyStdFunction()
{
std::function<void()> emptyFunc;
LifecycleCallback<> callback(emptyFunc);
QVERIFY(!callback);
std::function<void()> emptyFunc2 = nullptr;
LifecycleCallback<> callback2(emptyFunc2);
QVERIFY(!callback2);
}
// 测试生命周期安全
void testLifecycleSafety()
{
// QObject 生命周期安全
auto *qobject = new TestQObject;
LifecycleCallback<const QString &> callback1
= makeLifecycleCallback(qobject, &TestQObject::handleMessage);
delete qobject;
callback1("after deletion");
QVERIFY(!callback1);
// shared_ptr 生命周期安全
auto sharedObj = std::make_shared<TestSharedObject>();
LifecycleCallback<const QString &> callback2
= makeLifecycleCallback(sharedObj, &TestSharedObject::handleMessage);
sharedObj.reset();
for (int i = 0; i < 10; ++i) {
callback2("after reset");
}
QVERIFY(!callback2);
}
};
// 注册测试
QTEST_APPLESS_MAIN(TestLifecycleCallback)
#include "lifecyclecallback_unittest.moc"