File: dropjobtest.cpp

package info (click to toggle)
kio 5.116.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 41,496 kB
  • sloc: cpp: 123,468; xml: 528; ansic: 466; ruby: 60; sh: 21; makefile: 13
file content (515 lines) | stat: -rw-r--r-- 22,077 bytes parent folder | download | duplicates (3)
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*
    This file is part of the KDE project
    SPDX-FileCopyrightText: 2014 David Faure <faure@kde.org>

    SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/

#include <QDir>
#include <QMenu>
#include <QMimeData>
#include <QSignalSpy>
#include <QStandardPaths>
#include <QTemporaryDir>
#include <QTest>

#include "jobuidelegatefactory.h"
#include "kiotesthelper.h"
#include "mockcoredelegateextensions.h"
#include <KConfigGroup>
#include <KDesktopFile>
#include <KFileItemListProperties>
#include <KIO/CopyJob>
#include <KIO/DeleteJob>
#include <KIO/DropJob>
#include <KIO/StatJob>
#include <KJobUiDelegate>

Q_DECLARE_METATYPE(Qt::KeyboardModifiers)
Q_DECLARE_METATYPE(Qt::DropAction)
Q_DECLARE_METATYPE(Qt::DropActions)
Q_DECLARE_METATYPE(KFileItemListProperties)

#ifndef Q_OS_WIN
void initLocale()
{
    setenv("LC_ALL", "en_US.utf-8", 1);
}
Q_CONSTRUCTOR_FUNCTION(initLocale)
#endif

class JobSpy : public QObject
{
    Q_OBJECT
public:
    explicit JobSpy(KIO::Job *job)
        : QObject(nullptr)
        , m_spy(job, &KJob::result)
        , m_error(0)
    {
        connect(job, &KJob::result, this, [this](KJob *job) {
            m_error = job->error();
        });
    }
    // like job->exec(), but with a timeout (to avoid being stuck with a popup grabbing mouse and keyboard...)
    bool waitForResult()
    {
        // implementation taken from QTRY_COMPARE, to move the QVERIFY to the caller
        if (m_spy.isEmpty()) {
            QTest::qWait(0);
        }
        for (int i = 0; i < 5000 && m_spy.isEmpty(); i += 50) {
            QTest::qWait(50);
        }
        return !m_spy.isEmpty();
    }
    int error() const
    {
        return m_error;
    }

private:
    QSignalSpy m_spy;
    int m_error;
};

class DropJobTest : public QObject
{
    Q_OBJECT

private Q_SLOTS:
    void initTestCase()
    {
        QStandardPaths::setTestModeEnabled(true);
        qputenv("KIOSLAVE_ENABLE_TESTMODE", "1"); // ensure the KIO workers call QStandardPaths::setTestModeEnabled too

        KIO::setDefaultJobUiDelegateFactoryV2(nullptr);
        KIO::setDefaultJobUiDelegateExtension(nullptr);

        m_trashDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/Trash");
        QDir(m_trashDir).removeRecursively();

        QVERIFY(m_tempDir.isValid());
        QVERIFY(m_nonWritableTempDir.isValid());
        QVERIFY(QFile(m_nonWritableTempDir.path()).setPermissions(QFile::ReadOwner | QFile::ReadUser | QFile::ExeOwner | QFile::ExeUser));
        m_srcDir = m_tempDir.path();

        m_srcFile = m_srcDir + "/srcfile";
        m_srcLink = m_srcDir + "/link";

        qRegisterMetaType<KIO::CopyJob *>();
    }

    void cleanupTestCase()
    {
        QVERIFY(QFile(m_nonWritableTempDir.path())
                    .setPermissions(QFile::ReadOwner | QFile::ReadUser | QFile::WriteOwner | QFile::WriteUser | QFile::ExeOwner | QFile::ExeUser));
    }

    // Before every test method, ensure the test file m_srcFile exists
    void init()
    {
        if (QFile::exists(m_srcFile)) {
            QVERIFY(QFileInfo(m_srcFile).isWritable());
        } else {
            QFile srcFile(m_srcFile);
            QVERIFY2(srcFile.open(QFile::WriteOnly), qPrintable(srcFile.errorString()));
            srcFile.write("Hello world\n");
        }
#ifndef Q_OS_WIN
        if (!QFile::exists(m_srcLink)) {
            QVERIFY(QFile(m_srcFile).link(m_srcLink));
            QVERIFY(QFileInfo(m_srcLink).isSymLink());
        }
#endif
        QVERIFY(QFileInfo(m_srcFile).isWritable());
        m_mimeData.setUrls(QList<QUrl>{QUrl::fromLocalFile(m_srcFile)});
    }

    void shouldDropToDesktopFile()
    {
        // Given an executable application desktop file and a source file
        const QString desktopPath = m_srcDir + "/target.desktop";
        KDesktopFile desktopFile(desktopPath);
        KConfigGroup desktopGroup = desktopFile.desktopGroup();
        desktopGroup.writeEntry("Type", "Application");
        desktopGroup.writeEntry("StartupNotify", "false");
#ifdef Q_OS_WIN
        desktopGroup.writeEntry("Exec", "copy.exe %f %d/dest");
#else
        desktopGroup.writeEntry("Exec", "cp %f %d/dest");
#endif
        desktopFile.sync();
        QFile file(desktopPath);
        file.setPermissions(file.permissions() | QFile::ExeOwner | QFile::ExeUser);

        // When dropping the source file onto the desktop file
        QUrl destUrl = QUrl::fromLocalFile(desktopPath);
        QDropEvent dropEvent(QPoint(10, 10), Qt::CopyAction, &m_mimeData, Qt::LeftButton, Qt::NoModifier);
        KIO::DropJob *job = KIO::drop(&dropEvent, destUrl, KIO::HideProgressInfo);
        QSignalSpy spy(job, &KIO::DropJob::itemCreated);

        // Then the application is run with the source file as argument
        // (in this example, it copies the source file to "dest")
        QVERIFY2(job->exec(), qPrintable(job->errorString()));
        QCOMPARE(spy.count(), 0);
        const QString dest = m_srcDir + "/dest";
        QTRY_VERIFY(QFile::exists(dest));

        QVERIFY(QFile::remove(desktopPath));
        QVERIFY(QFile::remove(dest));
    }

    void shouldDropToDirectory_data()
    {
        QTest::addColumn<Qt::KeyboardModifiers>("modifiers");
        QTest::addColumn<Qt::DropAction>("dropAction"); // Qt's dnd support sets it from the modifiers, we fake it here
        QTest::addColumn<QString>("srcFile");
        QTest::addColumn<QString>("dest"); // empty for a temp dir
        QTest::addColumn<int>("expectedError");
        QTest::addColumn<bool>("shouldSourceStillExist");

        QTest::newRow("Ctrl") << Qt::KeyboardModifiers(Qt::ControlModifier) << Qt::CopyAction << m_srcFile << QString() << 0 << true;
        QTest::newRow("Shift") << Qt::KeyboardModifiers(Qt::ShiftModifier) << Qt::MoveAction << m_srcFile << QString() << 0 << false;
        QTest::newRow("Ctrl_Shift") << Qt::KeyboardModifiers(Qt::ControlModifier | Qt::ShiftModifier) << Qt::LinkAction << m_srcFile << QString() << 0 << true;
        QTest::newRow("DropOnItself") << Qt::KeyboardModifiers() << Qt::CopyAction << m_srcDir << m_srcDir << int(KIO::ERR_DROP_ON_ITSELF) << true;
        QTest::newRow("DropDirOnFile") << Qt::KeyboardModifiers(Qt::ControlModifier) << Qt::CopyAction << m_srcDir << m_srcFile << int(KIO::ERR_ACCESS_DENIED)
                                       << true;
        QTest::newRow("NonWritableDest") << Qt::KeyboardModifiers() << Qt::CopyAction << m_srcFile << m_nonWritableTempDir.path()
                                         << int(KIO::ERR_WRITE_ACCESS_DENIED) << true;
    }

    void shouldDropToDirectory()
    {
        QFETCH(Qt::KeyboardModifiers, modifiers);
        QFETCH(Qt::DropAction, dropAction);
        QFETCH(QString, srcFile);
        QFETCH(QString, dest);
        QFETCH(int, expectedError);
        QFETCH(bool, shouldSourceStillExist);

        // Given a directory and a source file
        QTemporaryDir tempDestDir;
        QVERIFY(tempDestDir.isValid());
        if (dest.isEmpty()) {
            dest = tempDestDir.path();
        }

        // When dropping the source file onto the directory
        const QUrl destUrl = QUrl::fromLocalFile(dest);
        m_mimeData.setUrls(QList<QUrl>{QUrl::fromLocalFile(srcFile)});
        QDropEvent dropEvent(QPoint(10, 10), dropAction, &m_mimeData, Qt::LeftButton, modifiers);
        KIO::DropJob *job = KIO::drop(&dropEvent, destUrl, KIO::HideProgressInfo | KIO::NoPrivilegeExecution);
        JobSpy jobSpy(job);
        QSignalSpy copyJobSpy(job, &KIO::DropJob::copyJobStarted);
        QSignalSpy itemCreatedSpy(job, &KIO::DropJob::itemCreated);

        // Then the file is copied
        QVERIFY(jobSpy.waitForResult());
        QCOMPARE(jobSpy.error(), expectedError);
        if (expectedError == 0) {
            QCOMPARE(copyJobSpy.count(), 1);
            const QString destFile = dest + "/srcfile";
            QCOMPARE(itemCreatedSpy.count(), 1);
            QCOMPARE(itemCreatedSpy.at(0).at(0).value<QUrl>(), QUrl::fromLocalFile(destFile));
            QVERIFY(QFile::exists(destFile));
            QCOMPARE(QFile::exists(m_srcFile), shouldSourceStillExist);
            if (dropAction == Qt::LinkAction) {
                QVERIFY(QFileInfo(destFile).isSymLink());
            }
        }
    }

    void shouldDropToTrash_data()
    {
        QTest::addColumn<Qt::KeyboardModifiers>("modifiers");
        QTest::addColumn<Qt::DropAction>("dropAction"); // Qt's dnd support sets it from the modifiers, we fake it here
        QTest::addColumn<QString>("srcFile");

        QTest::newRow("Ctrl") << Qt::KeyboardModifiers(Qt::ControlModifier) << Qt::CopyAction << m_srcFile;
        QTest::newRow("Shift") << Qt::KeyboardModifiers(Qt::ShiftModifier) << Qt::MoveAction << m_srcFile;
        QTest::newRow("Ctrl_Shift") << Qt::KeyboardModifiers(Qt::ControlModifier | Qt::ShiftModifier) << Qt::LinkAction << m_srcFile;
        QTest::newRow("NoModifiers") << Qt::KeyboardModifiers() << Qt::CopyAction << m_srcFile;
#ifndef Q_OS_WIN
        QTest::newRow("Link_Ctrl") << Qt::KeyboardModifiers(Qt::ControlModifier) << Qt::CopyAction << m_srcLink;
        QTest::newRow("Link_Shift") << Qt::KeyboardModifiers(Qt::ShiftModifier) << Qt::MoveAction << m_srcLink;
        QTest::newRow("Link_Ctrl_Shift") << Qt::KeyboardModifiers(Qt::ControlModifier | Qt::ShiftModifier) << Qt::LinkAction << m_srcLink;
        QTest::newRow("Link_NoModifiers") << Qt::KeyboardModifiers() << Qt::CopyAction << m_srcLink;
#endif
    }

    void shouldDropToTrash()
    {
        // Given a source file
        QFETCH(Qt::KeyboardModifiers, modifiers);
        QFETCH(Qt::DropAction, dropAction);
        QFETCH(QString, srcFile);
        const bool isLink = QFileInfo(srcFile).isSymLink();

        // When dropping it into the trash, with <modifiers> pressed
        m_mimeData.setUrls(QList<QUrl>{QUrl::fromLocalFile(srcFile)});
        QDropEvent dropEvent(QPoint(10, 10), dropAction, &m_mimeData, Qt::LeftButton, modifiers);
        KIO::DropJob *job = KIO::drop(&dropEvent, QUrl(QStringLiteral("trash:/")), KIO::HideProgressInfo);
        QSignalSpy copyJobSpy(job, &KIO::DropJob::copyJobStarted);
        QSignalSpy itemCreatedSpy(job, &KIO::DropJob::itemCreated);

        // Then a confirmation dialog should appear
        auto *uiDelegate = new KJobUiDelegate;
        job->setUiDelegate(uiDelegate);
        auto *askUserHandler = new MockAskUserInterface(uiDelegate);
        askUserHandler->m_deleteResult = true;

        // And the file should be moved to the trash, no matter what the modifiers are
        QVERIFY2(job->exec(), qPrintable(job->errorString()));
        QCOMPARE(askUserHandler->m_askUserDeleteCalled, 1);
        QCOMPARE(copyJobSpy.count(), 1);
        QCOMPARE(itemCreatedSpy.count(), 1);
        const QUrl trashUrl = itemCreatedSpy.at(0).at(0).value<QUrl>();
        QCOMPARE(trashUrl.scheme(), QString("trash"));
        KIO::StatJob *statJob = KIO::stat(trashUrl, KIO::HideProgressInfo);
        QVERIFY(statJob->exec());
        if (isLink) {
            QVERIFY(statJob->statResult().isLink());
        }

        // clean up
        KIO::DeleteJob *delJob = KIO::del(trashUrl, KIO::HideProgressInfo);
        QVERIFY2(delJob->exec(), qPrintable(delJob->errorString()));
    }

    void shouldDropFromTrash()
    {
        // Given a file in the trash
        const QFileInfo srcInfo(m_srcFile);
        const QFile::Permissions origPerms = srcInfo.permissions();
        QVERIFY(QFileInfo(m_srcFile).isWritable());
        KIO::CopyJob *copyJob = KIO::move(QUrl::fromLocalFile(m_srcFile), QUrl(QStringLiteral("trash:/")));

        QSignalSpy copyingDoneSpy(copyJob, &KIO::CopyJob::copyingDone);
        QVERIFY(copyJob->exec());
        const QUrl trashUrl = copyingDoneSpy.at(0).at(2).value<QUrl>();
        QVERIFY(trashUrl.isValid());
        QVERIFY(!QFile::exists(m_srcFile));

        // trashinfo file was created
        const QString infoFile(m_trashDir + QStringLiteral("/info/") + srcInfo.fileName() + QStringLiteral(".trashinfo"));
        QVERIFY(QFileInfo::exists(infoFile));

        // When dropping the trashed file into a local dir, without modifiers
        m_mimeData.setUrls(QList<QUrl>{trashUrl});
        QDropEvent dropEvent(QPoint(10, 10), Qt::CopyAction, &m_mimeData, Qt::LeftButton, Qt::NoModifier);
        KIO::DropJob *job = KIO::drop(&dropEvent, QUrl::fromLocalFile(m_srcDir), KIO::HideProgressInfo);
        QSignalSpy copyJobSpy(job, &KIO::DropJob::copyJobStarted);
        QSignalSpy spy(job, &KIO::DropJob::itemCreated);

        // Then the file should be moved, without a popup. No point in copying out of the trash, or linking to it.
        QVERIFY2(job->exec(), qPrintable(job->errorString()));
        QCOMPARE(copyJobSpy.count(), 1);
        QCOMPARE(spy.count(), 1);
        QCOMPARE(spy.at(0).at(0).value<QUrl>(), QUrl::fromLocalFile(m_srcFile));
        QVERIFY(QFile::exists(m_srcFile));
        QCOMPARE(int(QFileInfo(m_srcFile).permissions()), int(origPerms));
        QVERIFY(QFileInfo(m_srcFile).isWritable());
        KIO::StatJob *statJob = KIO::stat(trashUrl, KIO::HideProgressInfo);
        QVERIFY(!statJob->exec());
        QVERIFY(QFileInfo(m_srcFile).isWritable());

        // trashinfo file was removed
        QVERIFY(!QFileInfo::exists(infoFile));

        QVERIFY(QFileInfo(m_srcFile).isWritable());
    }

    void shouldDropTrashRootWithoutMovingAllTrashedFiles() // #319660
    {
        // Given some stuff in the trash
        const QUrl trashUrl(QStringLiteral("trash:/"));
        KIO::CopyJob *copyJob = KIO::move(QUrl::fromLocalFile(m_srcFile), trashUrl);
        QVERIFY(copyJob->exec());
        // and an empty destination directory
        QTemporaryDir tempDestDir;
        QVERIFY(tempDestDir.isValid());
        const QUrl destUrl = QUrl::fromLocalFile(tempDestDir.path());

        // When dropping a link / icon of the trash...
        m_mimeData.setUrls(QList<QUrl>{trashUrl});
        QDropEvent dropEvent(QPoint(10, 10), Qt::CopyAction, &m_mimeData, Qt::LeftButton, Qt::NoModifier);
        KIO::DropJob *job = KIO::drop(&dropEvent, destUrl, KIO::HideProgressInfo);
        QSignalSpy copyJobSpy(job, &KIO::DropJob::copyJobStarted);
        QVERIFY2(job->exec(), qPrintable(job->errorString()));

        // Then a full move shouldn't happen, just a link
        QCOMPARE(copyJobSpy.count(), 1);
        const QStringList items = QDir(tempDestDir.path()).entryList();
        QVERIFY2(!items.contains("srcfile"), qPrintable(items.join(',')));
        QVERIFY2(items.contains("trash:" + QChar(0x2044) + ".desktop"), qPrintable(items.join(',')));
    }

    void shouldDropFromTrashToTrash() // #378051
    {
        // Given a file in the trash
        QVERIFY(QFileInfo(m_srcFile).isWritable());
        KIO::CopyJob *copyJob = KIO::move(QUrl::fromLocalFile(m_srcFile), QUrl(QStringLiteral("trash:/")));
        QSignalSpy copyingDoneSpy(copyJob, &KIO::CopyJob::copyingDone);
        QVERIFY(copyJob->exec());
        const QUrl trashUrl = copyingDoneSpy.at(0).at(2).value<QUrl>();
        QVERIFY(trashUrl.isValid());
        QVERIFY(!QFile::exists(m_srcFile));

        // When dropping the trashed file in the trash
        m_mimeData.setUrls(QList<QUrl>{trashUrl});
        QDropEvent dropEvent(QPoint(10, 10), Qt::CopyAction, &m_mimeData, Qt::LeftButton, Qt::NoModifier);
        KIO::DropJob *job = KIO::drop(&dropEvent, QUrl(QStringLiteral("trash:/")), KIO::HideProgressInfo);
        QSignalSpy copyJobSpy(job, &KIO::DropJob::copyJobStarted);
        QSignalSpy spy(job, &KIO::DropJob::itemCreated);

        // Then an error should be reported and no files action should occur
        QVERIFY(!job->exec());
        QCOMPARE(job->error(), KIO::ERR_DROP_ON_ITSELF);
    }

    void shouldDropToDirectoryWithPopup_data()
    {
        QTest::addColumn<QString>("dest"); // empty for a temp dir
        QTest::addColumn<Qt::DropActions>("offeredActions");
        QTest::addColumn<int>("triggerActionNumber");
        QTest::addColumn<int>("expectedError");
        QTest::addColumn<Qt::DropAction>("expectedDropAction");
        QTest::addColumn<bool>("shouldSourceStillExist");

        const Qt::DropActions threeActions = Qt::MoveAction | Qt::CopyAction | Qt::LinkAction;
        const Qt::DropActions copyAndLink = Qt::CopyAction | Qt::LinkAction;
        QTest::newRow("Move") << QString() << threeActions << 0 << 0 << Qt::MoveAction << false;
        QTest::newRow("Copy") << QString() << threeActions << 1 << 0 << Qt::CopyAction << true;
        QTest::newRow("Link") << QString() << threeActions << 2 << 0 << Qt::LinkAction << true;
        QTest::newRow("SameDestCopy") << m_srcDir << copyAndLink << 0 << int(KIO::ERR_IDENTICAL_FILES) << Qt::CopyAction << true;
        QTest::newRow("SameDestLink") << m_srcDir << copyAndLink << 1 << int(KIO::ERR_FILE_ALREADY_EXIST) << Qt::LinkAction << true;
    }

    void shouldDropToDirectoryWithPopup()
    {
        QFETCH(QString, dest);
        QFETCH(Qt::DropActions, offeredActions);
        QFETCH(int, triggerActionNumber);
        QFETCH(int, expectedError);
        QFETCH(Qt::DropAction, expectedDropAction);
        QFETCH(bool, shouldSourceStillExist);

        // Given a directory and a source file
        QTemporaryDir tempDestDir;
        QVERIFY(tempDestDir.isValid());
        if (dest.isEmpty()) {
            dest = tempDestDir.path();
        }
        QVERIFY(!findPopup());

        // When dropping the source file onto the directory
        QUrl destUrl = QUrl::fromLocalFile(dest);
        QDropEvent dropEvent(QPoint(10, 10), Qt::CopyAction /*unused*/, &m_mimeData, Qt::LeftButton, Qt::NoModifier);
        KIO::DropJob *job = KIO::drop(&dropEvent, destUrl, KIO::HideProgressInfo);
        JobSpy jobSpy(job);
        qRegisterMetaType<KFileItemListProperties>();
        QSignalSpy spyShow(job, &KIO::DropJob::popupMenuAboutToShow);
        QSignalSpy copyJobSpy(job, &KIO::DropJob::copyJobStarted);
        QVERIFY(spyShow.isValid());

        // Then a popup should appear, with the expected available actions
        QVERIFY(spyShow.wait());
        QTRY_VERIFY(findPopup());
        QMenu *popup = findPopup();
        QCOMPARE(int(popupDropActions(popup)), int(offeredActions));

        // And when selecting action number <triggerActionNumber>
        QAction *action = popup->actions().at(triggerActionNumber);
        QVERIFY(action);
        QCOMPARE(int(action->data().value<Qt::DropAction>()), int(expectedDropAction));
        const QRect actionGeom = popup->actionGeometry(action);
        QTest::mouseClick(popup, Qt::LeftButton, Qt::NoModifier, actionGeom.center());

        // Then the job should finish, and the chosen action should happen.
        QVERIFY(jobSpy.waitForResult());
        QCOMPARE(jobSpy.error(), expectedError);
        if (expectedError == 0) {
            QCOMPARE(copyJobSpy.count(), 1);
            const QString destFile = dest + "/srcfile";
            QVERIFY(QFile::exists(destFile));
            QCOMPARE(QFile::exists(m_srcFile), shouldSourceStillExist);
            if (expectedDropAction == Qt::LinkAction) {
                QVERIFY(QFileInfo(destFile).isSymLink());
            }
        }
        QTRY_VERIFY(!findPopup()); // flush deferred delete, so we don't get this popup again in findPopup
    }

    void shouldAddApplicationActionsToPopup()
    {
        // Given a directory and a source file
        QTemporaryDir tempDestDir;
        QVERIFY(tempDestDir.isValid());
        const QUrl destUrl = QUrl::fromLocalFile(tempDestDir.path());

        // When dropping the source file onto the directory
        QDropEvent dropEvent(QPoint(10, 10), Qt::CopyAction /*unused*/, &m_mimeData, Qt::LeftButton, Qt::NoModifier);
        KIO::DropJob *job = KIO::drop(&dropEvent, destUrl, KIO::HideProgressInfo);
        QAction appAction1(QStringLiteral("action1"), this);
        QAction appAction2(QStringLiteral("action2"), this);
        QList<QAction *> appActions;
        appActions << &appAction1 << &appAction2;
        job->setApplicationActions(appActions);
        JobSpy jobSpy(job);

        // Then a popup should appear, with the expected available actions
        QTRY_VERIFY(findPopup());
        QMenu *popup = findPopup();
        const QList<QAction *> actions = popup->actions();
        QVERIFY(actions.contains(&appAction1));
        QVERIFY(actions.contains(&appAction2));
        QVERIFY(actions.at(actions.indexOf(&appAction1) - 1)->isSeparator());
        QVERIFY(actions.at(actions.indexOf(&appAction2) + 1)->isSeparator());

        // And when selecting action appAction1
        const QRect actionGeom = popup->actionGeometry(&appAction1);
        QTest::mouseClick(popup, Qt::LeftButton, Qt::NoModifier, actionGeom.center());

        // Then the menu should hide and the job terminate (without doing any copying)
        QVERIFY(jobSpy.waitForResult());
        QCOMPARE(jobSpy.error(), 0);
        const QString destFile = tempDestDir.path() + "/srcfile";
        QVERIFY(!QFile::exists(destFile));
    }

private:
    static QMenu *findPopup()
    {
        const QList<QWidget *> widgetsList = qApp->topLevelWidgets();
        for (QWidget *widget : widgetsList) {
            if (QMenu *menu = qobject_cast<QMenu *>(widget)) {
                return menu;
            }
        }
        return nullptr;
    }
    static Qt::DropActions popupDropActions(QMenu *menu)
    {
        Qt::DropActions actions;
        const QList<QAction *> actionsList = menu->actions();
        for (const QAction *action : actionsList) {
            const QVariant userData = action->data();
            if (userData.isValid()) {
                actions |= userData.value<Qt::DropAction>();
            }
        }
        return actions;
    }
    QMimeData m_mimeData; // contains m_srcFile
    QTemporaryDir m_tempDir;
    QString m_srcDir;
    QString m_srcFile;
    QString m_srcLink;
    QTemporaryDir m_nonWritableTempDir;
    QString m_trashDir;
};

QTEST_MAIN(DropJobTest)

#include "dropjobtest.moc"