Repository navigation
Expand file tree
/
Copy pathtest_embedded_window_controller.cpp
More file actions
356 lines (322 loc) · 11.8 KB
/
Copy pathtest_embedded_window_controller.cpp
File metadata and controls
356 lines (322 loc) · 11.8 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
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
/*
* This file is part of NotepadAI.
* Copyright 2026 NotepadAI contributors
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <QtTest>
#include "EmbeddedWindowController.h"
#include "PendingCloseState.h"
class FakeEmbeddedController final : public EmbeddedWindowController
{
public:
bool captureOk = true;
bool createOk = true;
bool markOk = true;
bool prepareOk = true;
bool tokenOwned = true;
bool expectedAttached = false;
bool hostForeignChild = false;
bool restoreOk = false;
bool failSafeOk = false;
int captureCalls = 0;
int createCalls = 0;
int markCalls = 0;
int prepareCalls = 0;
int tokenOwnershipCalls = 0;
int expectedAttachedCalls = 0;
int hierarchyCalls = 0;
int restoreCalls = 0;
int failSafeCalls = 0;
int detachCalls = 0;
int clearCallbackCalls = 0;
int forceCloseCalls = 0;
int scratchCloseCalls = 0;
int embedRefusedNotices = 0;
int closeVetoNotices = 0;
quintptr lastToken = 0;
bool requestClose(bool failSafe = false)
{
return closeByToken(lastToken, failSafe ? ReleaseMode::FailSafeDetach
: ReleaseMode::RestoreExact);
}
protected:
bool platCaptureState(quintptr handle, EmbeddedWindowWin32::NativeWindowState *out) override
{
++captureCalls;
if (!captureOk)
return false;
out->handle = handle;
out->processId = 11;
out->threadId = 22;
return true;
}
quintptr platCreateTab(quintptr token, const QString &) override
{
++createCalls;
lastToken = token;
return createOk ? 0xCAFE : 0;
}
bool platMarkWindow(quintptr, quintptr) override { ++markCalls; return markOk; }
bool platPrepareAndAttach(quintptr, const EmbeddedWindowWin32::NativeWindowState &,
quintptr) override
{
++prepareCalls;
return prepareOk;
}
bool platTargetOwnsToken(
const EmbeddedWindowWin32::NativeWindowState &, quintptr) override
{
++tokenOwnershipCalls;
return tokenOwned;
}
bool platExpectedTargetAttached(
const EmbeddedWindowWin32::NativeWindowState &, quintptr) override
{
++expectedAttachedCalls;
return expectedAttached;
}
bool platHostRetainsForeignChild(quintptr) override
{
++hierarchyCalls;
return hostForeignChild;
}
bool platRestoreExact(const EmbeddedWindowWin32::NativeWindowState &, quintptr) override
{
++restoreCalls;
return restoreOk;
}
bool platFailSafeDetach(const EmbeddedWindowWin32::NativeWindowState &, quintptr) override
{
++failSafeCalls;
return failSafeOk;
}
void platDetachHost(quintptr) override { ++detachCalls; }
void platClearHostCallback(quintptr) override { ++clearCallbackCalls; }
void platForceCloseTab(quintptr) override { ++forceCloseCalls; }
void platCloseScratchTab() override { ++scratchCloseCalls; }
void platNotifyEmbedRefused() override { ++embedRefusedNotices; }
void platNotifyCloseVetoed() override { ++closeVetoNotices; }
};
class TestEmbeddedWindowController : public QObject
{
Q_OBJECT
private slots:
void releaseSuccess_isSoleForceClosePredecessor();
void releaseFailure_retainsEverything();
void failSafeClose_fallsBackImmediately();
void watchdogRetry_closesExactlyOnce();
void shutdown_isIdempotent();
void recycledHwnd_notAttached_reapsWithoutMutation();
void tokenLost_targetStillChild_retainsHierarchy();
void targetGone_reapsWithoutNativeMutation();
void differentForeignChild_retainsHostAfterTargetRelease();
void failuresBeforeAndAfterRegistration_unwindSafely();
void scratchClosesOnlyAfterAttachSuccess();
void pendingExitAndRestart_completeAfterRegistryDrain();
};
void TestEmbeddedWindowController::releaseSuccess_isSoleForceClosePredecessor()
{
FakeEmbeddedController controller;
QSignalSpy drained(&controller, &EmbeddedWindowController::allEmbedsReleased);
controller.embedWindow(100, QStringLiteral("target"));
QCOMPARE(controller.embedCount(), 1);
controller.restoreOk = true;
QVERIFY(controller.requestClose());
QCOMPARE(controller.detachCalls, 1);
QCOMPARE(controller.clearCallbackCalls, 1);
QCOMPARE(controller.forceCloseCalls, 1);
QCOMPARE(controller.embedCount(), 0);
QCOMPARE(drained.count(), 1);
}
void TestEmbeddedWindowController::releaseFailure_retainsEverything()
{
FakeEmbeddedController controller;
controller.embedWindow(100, QStringLiteral("target"));
QVERIFY(!controller.requestClose());
QCOMPARE(controller.forceCloseCalls, 0);
QCOMPARE(controller.clearCallbackCalls, 0);
QCOMPARE(controller.detachCalls, 0);
QCOMPARE(controller.embedCount(), 1);
QCOMPARE(controller.closeVetoNotices, 1);
controller.cancelPendingClose();
controller.failSafeOk = true;
controller.poll();
QCOMPARE(controller.forceCloseCalls, 0); // canceled retry leaves hierarchy untouched
controller.restoreOk = true;
controller.shutdown();
}
void TestEmbeddedWindowController::failSafeClose_fallsBackImmediately()
{
FakeEmbeddedController controller;
controller.embedWindow(100, QStringLiteral("target"));
controller.failSafeOk = true;
QVERIFY(controller.requestClose(true));
QCOMPARE(controller.restoreCalls, 1);
QCOMPARE(controller.failSafeCalls, 1);
QCOMPARE(controller.detachCalls, 1);
QCOMPARE(controller.forceCloseCalls, 1);
QCOMPARE(controller.embedCount(), 0);
}
void TestEmbeddedWindowController::watchdogRetry_closesExactlyOnce()
{
FakeEmbeddedController controller;
QSignalSpy drained(&controller, &EmbeddedWindowController::allEmbedsReleased);
controller.embedWindow(100, QStringLiteral("target"));
QVERIFY(!controller.requestClose());
controller.failSafeOk = true;
controller.poll();
QCOMPARE(controller.restoreCalls, 2); // interactive attempt + retry
QCOMPARE(controller.failSafeCalls, 1);
QCOMPARE(controller.forceCloseCalls, 1);
QCOMPARE(controller.embedCount(), 0);
QCOMPARE(drained.count(), 1);
controller.poll();
QCOMPARE(controller.forceCloseCalls, 1);
QCOMPARE(drained.count(), 1);
}
void TestEmbeddedWindowController::shutdown_isIdempotent()
{
FakeEmbeddedController controller;
controller.embedWindow(100, QStringLiteral("target"));
controller.restoreOk = true;
controller.shutdown();
controller.shutdown();
QCOMPARE(controller.forceCloseCalls, 1);
QCOMPARE(controller.detachCalls, 1);
QCOMPARE(controller.embedCount(), 0);
}
void TestEmbeddedWindowController::recycledHwnd_notAttached_reapsWithoutMutation()
{
FakeEmbeddedController controller;
controller.embedWindow(100, QStringLiteral("target"));
controller.tokenOwned = false; // token mismatch despite same PID/TID-shaped state
controller.hostForeignChild = false; // recycled HWND is not under our host
controller.shutdown();
QCOMPARE(controller.restoreCalls, 0);
QCOMPARE(controller.failSafeCalls, 0);
QCOMPARE(controller.detachCalls, 1);
QCOMPARE(controller.forceCloseCalls, 1);
QCOMPARE(controller.embedCount(), 0);
controller.poll();
QCOMPARE(controller.forceCloseCalls, 1); // reaped exactly once
}
void TestEmbeddedWindowController::tokenLost_targetStillChild_retainsHierarchy()
{
FakeEmbeddedController controller;
controller.embedWindow(100, QStringLiteral("target"));
controller.tokenOwned = false; // token was externally removed: never mutate target
controller.expectedAttached = true; // expected HWND is still a direct host child
controller.hostForeignChild = true; // production aggregate agrees
controller.shutdown();
QCOMPARE(controller.restoreCalls, 0);
QCOMPARE(controller.failSafeCalls, 0);
QCOMPARE(controller.detachCalls, 0);
QCOMPARE(controller.forceCloseCalls, 0);
QCOMPARE(controller.embedCount(), 1);
}
void TestEmbeddedWindowController::targetGone_reapsWithoutNativeMutation()
{
FakeEmbeddedController controller;
controller.embedWindow(100, QStringLiteral("target"));
controller.tokenOwned = false;
controller.hostForeignChild = false;
controller.poll();
QCOMPARE(controller.restoreCalls, 0);
QCOMPARE(controller.failSafeCalls, 0);
QCOMPARE(controller.detachCalls, 1);
QCOMPARE(controller.forceCloseCalls, 1);
QCOMPARE(controller.embedCount(), 0);
}
void TestEmbeddedWindowController::differentForeignChild_retainsHostAfterTargetRelease()
{
FakeEmbeddedController controller;
controller.embedWindow(100, QStringLiteral("target"));
controller.restoreOk = true; // expected target was safely restored
controller.hostForeignChild = true; // unrelated foreign child remains
controller.shutdown();
QCOMPARE(controller.restoreCalls, 1);
QCOMPARE(controller.failSafeCalls, 0);
QCOMPARE(controller.detachCalls, 1); // tracking target stopped after restore
QCOMPARE(controller.forceCloseCalls, 0); // host cannot be destroyed
QCOMPARE(controller.embedCount(), 1);
}
void TestEmbeddedWindowController::failuresBeforeAndAfterRegistration_unwindSafely()
{
{
FakeEmbeddedController c;
c.captureOk = false;
c.embedWindow(1, QStringLiteral("x"));
QCOMPARE(c.createCalls, 0);
QCOMPARE(c.forceCloseCalls, 0);
QCOMPARE(c.embedCount(), 0);
}
{
FakeEmbeddedController c;
c.createOk = false;
c.embedWindow(1, QStringLiteral("x"));
QCOMPARE(c.markCalls, 0);
QCOMPARE(c.forceCloseCalls, 0); // platCreateTab owns its failed resources
QCOMPARE(c.embedCount(), 0);
}
{
FakeEmbeddedController c;
c.markOk = false;
c.embedWindow(1, QStringLiteral("x"));
QCOMPARE(c.prepareCalls, 0);
QCOMPARE(c.forceCloseCalls, 1);
QCOMPARE(c.embedCount(), 0);
}
{
FakeEmbeddedController c;
c.prepareOk = false;
c.restoreOk = true;
c.embedWindow(1, QStringLiteral("x"));
QCOMPARE(c.forceCloseCalls, 1);
QCOMPARE(c.scratchCloseCalls, 0);
QCOMPARE(c.embedCount(), 0);
}
{
FakeEmbeddedController c;
c.prepareOk = false;
c.embedWindow(1, QStringLiteral("x"));
QCOMPARE(c.forceCloseCalls, 0);
QCOMPARE(c.scratchCloseCalls, 0);
QCOMPARE(c.embedCount(), 1); // partial attach retained until retry
}
}
void TestEmbeddedWindowController::scratchClosesOnlyAfterAttachSuccess()
{
FakeEmbeddedController controller;
controller.embedWindow(100, QStringLiteral("target"));
QCOMPARE(controller.scratchCloseCalls, 1);
QCOMPARE(controller.embedCount(), 1);
controller.restoreOk = true;
controller.shutdown();
}
void TestEmbeddedWindowController::pendingExitAndRestart_completeAfterRegistryDrain()
{
FakeEmbeddedController controller;
PendingCloseState closeState;
int queuedRetries = 0;
connect(&controller, &EmbeddedWindowController::allEmbedsReleased, this, [&]() {
if (closeState.requestRetry())
++queuedRetries;
});
controller.embedWindow(100, QStringLiteral("target"));
closeState.begin(PendingCloseState::Intent::Restart);
QVERIFY(!controller.requestClose());
QCOMPARE(queuedRetries, 0);
controller.failSafeOk = true;
controller.poll();
QCOMPARE(queuedRetries, 1);
QVERIFY(!closeState.requestRetry()); // anti-reentrancy / no duplicate queue
closeState.retryDequeued();
QCOMPARE(closeState.accept(), PendingCloseState::Intent::Restart);
QCOMPARE(closeState.accept(), PendingCloseState::Intent::None); // launch exactly once
closeState.begin(PendingCloseState::Intent::Exit);
closeState.cancel();
QVERIFY(!closeState.requestRetry());
}
QTEST_GUILESS_MAIN(TestEmbeddedWindowController)
#include "test_embedded_window_controller.moc"