File: fileundomanagertest.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 (833 lines) | stat: -rw-r--r-- 26,447 bytes parent folder | download | duplicates (2)
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
/*
    This file is part of KDE
    SPDX-FileCopyrightText: 2006, 2008 David Faure <faure@kde.org>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "fileundomanagertest.h"

#include "../src/utils_p.h"

#include "mockcoredelegateextensions.h"
#include <kio/batchrenamejob.h>
#include <kio/copyjob.h>
#include <kio/deletejob.h>
#include <kio/fileundomanager.h>
#include <kio/mkdirjob.h>
#include <kio/mkpathjob.h>
#include <kio/paste.h>
#include <kio/pastejob.h>
#include <kio/restorejob.h>
#include <kioglobal_p.h>
#include <kprotocolinfo.h>

#include <QApplication>
#include <QClipboard>
#include <QDateTime>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QMimeData>
#include <QSignalSpy>
#include <QTest>
#include <qplatformdefs.h>

#include <KConfig>
#include <KConfigGroup>
#include <KUrlMimeData>

#include <cerrno>
#include <time.h>
#ifdef Q_OS_WIN
#include <sys/utime.h>
#else
#include <sys/time.h>
#include <utime.h>
#endif

QTEST_MAIN(FileUndoManagerTest)

using namespace KIO;

static QString homeTmpDir()
{
    return QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QDir::separator();
}
static QString destDir()
{
    return homeTmpDir() + "destdir/";
}

static QString srcFile()
{
    return homeTmpDir() + "testfile";
}
static QString destFile()
{
    return destDir() + "testfile";
}

#ifndef Q_OS_WIN
static QString srcLink()
{
    return homeTmpDir() + "symlink";
}
static QString destLink()
{
    return destDir() + "symlink";
}
#endif

static QString srcSubDir()
{
    return homeTmpDir() + "subdir";
}
static QString destSubDir()
{
    return destDir() + "subdir";
}

static QList<QUrl> sourceList()
{
    QList<QUrl> lst;
    lst << QUrl::fromLocalFile(srcFile());
#ifndef Q_OS_WIN
    lst << QUrl::fromLocalFile(srcLink());
#endif
    return lst;
}

static void createTestFile(const QString &path, const char *contents)
{
    QFile f(path);
    if (!f.open(QIODevice::WriteOnly)) {
        qFatal("Couldn't create %s", qPrintable(path));
    }
    f.write(QByteArray(contents));
    f.close();
}

static void createTestSymlink(const QString &path)
{
    // Create symlink if it doesn't exist yet
    QT_STATBUF buf;
    if (QT_LSTAT(QFile::encodeName(path).constData(), &buf) != 0) {
        bool ok = KIOPrivate::createSymlink(QStringLiteral("/IDontExist"), path); // broken symlink
        if (!ok) {
            qFatal("couldn't create symlink: %s", strerror(errno));
        }
        QVERIFY(QT_LSTAT(QFile::encodeName(path).constData(), &buf) == 0);
        QVERIFY(Utils::isLinkMask(buf.st_mode));
    } else {
        QVERIFY(Utils::isLinkMask(buf.st_mode));
    }
    qDebug("symlink %s created", qPrintable(path));
    QVERIFY(QFileInfo(path).isSymLink());
}

static void checkTestDirectory(const QString &path)
{
    QVERIFY(QFileInfo(path).isDir());
    QVERIFY(QFileInfo(path + "/fileindir").isFile());
#ifndef Q_OS_WIN
    QVERIFY(QFileInfo(path + "/testlink").isSymLink());
#endif
    QVERIFY(QFileInfo(path + "/dirindir").isDir());
    QVERIFY(QFileInfo(path + "/dirindir/nested").isFile());
}

static void createTestDirectory(const QString &path)
{
    QDir dir;
    bool ok = dir.mkpath(path);
    if (!ok) {
        qFatal("couldn't create %s", qPrintable(path));
    }
    createTestFile(path + "/fileindir", "File in dir");
#ifndef Q_OS_WIN
    createTestSymlink(path + "/testlink");
#endif
    ok = dir.mkdir(path + "/dirindir");
    if (!ok) {
        qFatal("couldn't create %s", qPrintable(path));
    }
    createTestFile(path + "/dirindir/nested", "Nested");
    checkTestDirectory(path);
}

class TestUiInterface : public FileUndoManager::UiInterface
{
public:
    TestUiInterface()
        : FileUndoManager::UiInterface()
        , m_mockAskUserInterface(new MockAskUserInterface)
    {
        setShowProgressInfo(false);
    }
    void jobError(KIO::Job *job) override
    {
        m_errorCode = job->error();
        qWarning() << job->errorString();
    }
    bool copiedFileWasModified(const QUrl &src, const QUrl &dest, const QDateTime &srcTime, const QDateTime &destTime) override
    {
        Q_UNUSED(src);
        m_dest = dest;
        Q_UNUSED(srcTime);
        Q_UNUSED(destTime);
        return true;
    }
    bool confirmDeletion(const QList<QUrl> &) override
    {
        Q_ASSERT(false); // no longer called
        return false;
    }
    void setNextReplyToConfirmDeletion(bool b)
    {
        m_nextReplyToConfirmDeletion = b;
    }
    QUrl dest() const
    {
        return m_dest;
    }
    int errorCode() const
    {
        return m_errorCode;
    }
    void clear()
    {
        m_dest = QUrl();
        m_errorCode = 0;
        m_mockAskUserInterface->clear();
    }
    MockAskUserInterface *askUserMockInterface() const
    {
        return m_mockAskUserInterface.get();
    }
    void virtual_hook(int id, void *data) override
    {
        if (id == HookGetAskUserActionInterface) {
            AskUserActionInterface **p = static_cast<AskUserActionInterface **>(data);
            *p = m_mockAskUserInterface.get();
            m_mockAskUserInterface->m_deleteResult = m_nextReplyToConfirmDeletion;
        }
    }

private:
    QUrl m_dest;
    std::unique_ptr<MockAskUserInterface> m_mockAskUserInterface;
    int m_errorCode = 0;
    bool m_nextReplyToConfirmDeletion = true;
};

void FileUndoManagerTest::initTestCase()
{
    qDebug("initTestCase");

    QStandardPaths::setTestModeEnabled(true);

    // Get kio_trash to share our environment so that it writes trashrc to the right kdehome
    qputenv("KIOSLAVE_ENABLE_TESTMODE", "1");

    // Start with a clean base dir
    cleanupTestCase();

    if (!QFile::exists(homeTmpDir())) {
        bool ok = QDir().mkpath(homeTmpDir());
        if (!ok) {
            qFatal("Couldn't create %s", qPrintable(homeTmpDir()));
        }
    }

    createTestFile(srcFile(), "Hello world");
#ifndef Q_OS_WIN
    createTestSymlink(srcLink());
#endif
    createTestDirectory(srcSubDir());

    QDir().mkpath(destDir());
    QVERIFY(QFileInfo(destDir()).isDir());

    QVERIFY(!FileUndoManager::self()->isUndoAvailable());
    m_uiInterface = new TestUiInterface; // owned by FileUndoManager
    FileUndoManager::self()->setUiInterface(m_uiInterface);
}

void FileUndoManagerTest::cleanupTestCase()
{
    KIO::Job *job = KIO::del(QUrl::fromLocalFile(homeTmpDir()), KIO::HideProgressInfo);
    job->exec();
}

void FileUndoManagerTest::doUndo()
{
    QEventLoop eventLoop;
    connect(FileUndoManager::self(), &FileUndoManager::undoJobFinished, &eventLoop, &QEventLoop::quit);
    FileUndoManager::self()->undo();
    eventLoop.exec(QEventLoop::ExcludeUserInputEvents); // wait for undo job to finish
}

void FileUndoManagerTest::testCopyFiles()
{
    // Initially inspired from JobTest::copyFileToSamePartition()
    const QString destdir = destDir();
    QList<QUrl> lst = sourceList();
    const QUrl d = QUrl::fromLocalFile(destdir);
    KIO::CopyJob *job = KIO::copy(lst, d, KIO::HideProgressInfo);
    job->setUiDelegate(nullptr);
    FileUndoManager::self()->recordCopyJob(job);

    QSignalSpy spyUndoAvailable(FileUndoManager::self(), qOverload<bool>(&FileUndoManager::undoAvailable));
    QVERIFY(spyUndoAvailable.isValid());
    QSignalSpy spyTextChanged(FileUndoManager::self(), &FileUndoManager::undoTextChanged);
    QVERIFY(spyTextChanged.isValid());

    QVERIFY2(job->exec(), qPrintable(job->errorString()));

    QVERIFY(QFile::exists(destFile()));
#ifndef Q_OS_WIN
    // Don't use QFile::exists, it's a broken symlink...
    QVERIFY(QFileInfo(destLink()).isSymLink());
#endif

    // might have to wait for dbus signal here... but this is currently disabled.
    // QTest::qWait( 20 );
    QVERIFY(FileUndoManager::self()->isUndoAvailable());
    QCOMPARE(spyUndoAvailable.count(), 1);
    QCOMPARE(spyTextChanged.count(), 1);
    m_uiInterface->clear();

    m_uiInterface->setNextReplyToConfirmDeletion(false); // act like the user didn't confirm
    FileUndoManager::self()->undo();
    auto *lastMock = m_uiInterface->askUserMockInterface();
    QCOMPARE(lastMock->m_askUserDeleteCalled, 1);
    QVERIFY(QFile::exists(destFile())); // nothing happened yet

    // OK, now do it
    m_uiInterface->clear();
    m_uiInterface->setNextReplyToConfirmDeletion(true);
    doUndo();

    QVERIFY(!FileUndoManager::self()->isUndoAvailable());
    QVERIFY(spyUndoAvailable.count() >= 2); // it's in fact 3, due to lock/unlock emitting it as well
    QCOMPARE(spyTextChanged.count(), 2);
    QCOMPARE(m_uiInterface->askUserMockInterface()->m_askUserDeleteCalled, 1);

    // Check that undo worked
    QVERIFY(!QFile::exists(destFile()));
#ifndef Q_OS_WIN
    QVERIFY(!QFile::exists(destLink()));
    QVERIFY(!QFileInfo(destLink()).isSymLink());
#endif
}

void FileUndoManagerTest::testMoveFiles()
{
    const QString destdir = destDir();
    QList<QUrl> lst = sourceList();
    const QUrl d = QUrl::fromLocalFile(destdir);
    KIO::CopyJob *job = KIO::move(lst, d, KIO::HideProgressInfo);
    job->setUiDelegate(nullptr);
    FileUndoManager::self()->recordCopyJob(job);

    QVERIFY2(job->exec(), qPrintable(job->errorString()));

    QVERIFY(!QFile::exists(srcFile())); // the source moved
    QVERIFY(QFile::exists(destFile()));
#ifndef Q_OS_WIN
    QVERIFY(!QFileInfo(srcLink()).isSymLink());
    // Don't use QFile::exists, it's a broken symlink...
    QVERIFY(QFileInfo(destLink()).isSymLink());
#endif

    doUndo();

    QVERIFY(QFile::exists(srcFile())); // the source is back
    QVERIFY(!QFile::exists(destFile()));
#ifndef Q_OS_WIN
    QVERIFY(QFileInfo(srcLink()).isSymLink());
    QVERIFY(!QFileInfo(destLink()).isSymLink());
#endif
}

void FileUndoManagerTest::testCopyDirectory()
{
    const QString destdir = destDir();
    QList<QUrl> lst;
    lst << QUrl::fromLocalFile(srcSubDir());
    const QUrl d = QUrl::fromLocalFile(destdir);
    KIO::CopyJob *job = KIO::copy(lst, d, KIO::HideProgressInfo);
    job->setUiDelegate(nullptr);
    FileUndoManager::self()->recordCopyJob(job);

    QVERIFY2(job->exec(), qPrintable(job->errorString()));

    checkTestDirectory(srcSubDir()); // src untouched
    checkTestDirectory(destSubDir());

    doUndo();

    checkTestDirectory(srcSubDir());
    QVERIFY(!QFile::exists(destSubDir()));
}

void FileUndoManagerTest::testCopyEmptyDirectory()
{
    const QString src = srcSubDir() + "/.emptydir";
    const QString destEmptyDir = destDir() + "/.emptydir";
    QDir().mkpath(src);
    KIO::CopyJob *job = KIO::copy({QUrl::fromLocalFile(src)}, QUrl::fromLocalFile(destEmptyDir), KIO::HideProgressInfo);
    job->setUiDelegate(nullptr);
    FileUndoManager::self()->recordCopyJob(job);

    QVERIFY2(job->exec(), qPrintable(job->errorString()));

    QVERIFY(QFileInfo(src).isDir()); // untouched
    QVERIFY(QFileInfo(destEmptyDir).isDir());

    doUndo();

    QVERIFY(QFileInfo(src).isDir()); // untouched
    QVERIFY(!QFile::exists(destEmptyDir));
}

void FileUndoManagerTest::testMoveDirectory()
{
    const QString destdir = destDir();
    QList<QUrl> lst;
    lst << QUrl::fromLocalFile(srcSubDir());
    const QUrl d = QUrl::fromLocalFile(destdir);
    KIO::CopyJob *job = KIO::move(lst, d, KIO::HideProgressInfo);
    job->setUiDelegate(nullptr);
    FileUndoManager::self()->recordCopyJob(job);

    QVERIFY2(job->exec(), qPrintable(job->errorString()));

    QVERIFY(!QFile::exists(srcSubDir()));
    checkTestDirectory(destSubDir());

    doUndo();

    checkTestDirectory(srcSubDir());
    QVERIFY(!QFile::exists(destSubDir()));
}

void FileUndoManagerTest::testRenameFile()
{
    const QUrl oldUrl = QUrl::fromLocalFile(srcFile());
    const QUrl newUrl = QUrl::fromLocalFile(srcFile() + ".new");
    QList<QUrl> lst;
    lst.append(oldUrl);
    QSignalSpy spyUndoAvailable(FileUndoManager::self(), qOverload<bool>(&FileUndoManager::undoAvailable));
    QVERIFY(spyUndoAvailable.isValid());
    KIO::Job *job = KIO::moveAs(oldUrl, newUrl, KIO::HideProgressInfo);
    job->setUiDelegate(nullptr);
    FileUndoManager::self()->recordJob(FileUndoManager::Rename, lst, newUrl, job);

    QVERIFY2(job->exec(), qPrintable(job->errorString()));

    QVERIFY(!QFile::exists(srcFile()));
    QVERIFY(QFileInfo(newUrl.toLocalFile()).isFile());
    QCOMPARE(spyUndoAvailable.count(), 1);

    doUndo();

    QVERIFY(QFile::exists(srcFile()));
    QVERIFY(!QFileInfo(newUrl.toLocalFile()).isFile());
}

void FileUndoManagerTest::testRenameDir()
{
    const QUrl oldUrl = QUrl::fromLocalFile(srcSubDir());
    const QUrl newUrl = QUrl::fromLocalFile(srcSubDir() + ".new");
    QList<QUrl> lst;
    lst.append(oldUrl);
    KIO::Job *job = KIO::moveAs(oldUrl, newUrl, KIO::HideProgressInfo);
    job->setUiDelegate(nullptr);
    FileUndoManager::self()->recordJob(FileUndoManager::Rename, lst, newUrl, job);

    QVERIFY2(job->exec(), qPrintable(job->errorString()));

    QVERIFY(!QFile::exists(srcSubDir()));
    QVERIFY(QFileInfo(newUrl.toLocalFile()).isDir());

    doUndo();

    QVERIFY(QFile::exists(srcSubDir()));
    QVERIFY(!QFileInfo(newUrl.toLocalFile()).isDir());
}

void FileUndoManagerTest::testCreateSymlink()
{
#ifdef Q_OS_WIN
    QSKIP("Test skipped on Windows for lack of proper symlink support");
#endif
    const QUrl link = QUrl::fromLocalFile(homeTmpDir() + "newlink");
    const QString path = link.toLocalFile();
    QVERIFY(!QFile::exists(path));

    const QUrl target = QUrl::fromLocalFile(homeTmpDir() + "linktarget");
    const QString targetPath = target.toLocalFile();
    createTestFile(targetPath, "Link's Target");
    QVERIFY(QFile::exists(targetPath));

    KIO::CopyJob *job = KIO::link(target, link, KIO::HideProgressInfo);
    job->setUiDelegate(nullptr);
    FileUndoManager::self()->recordCopyJob(job);
    QVERIFY2(job->exec(), qPrintable(job->errorString()));
    QVERIFY(QFile::exists(path));
    QVERIFY(QFileInfo(path).isSymLink());

    // For undoing symlinks no confirmation is required. We delete it straight away.
    doUndo();

    QVERIFY(!QFile::exists(path));
}

void FileUndoManagerTest::testCreateDir()
{
    const QUrl url = QUrl::fromLocalFile(srcSubDir() + ".mkdir");
    const QString path = url.toLocalFile();
    QVERIFY(!QFile::exists(path));

    KIO::SimpleJob *job = KIO::mkdir(url);
    job->setUiDelegate(nullptr);
    FileUndoManager::self()->recordJob(FileUndoManager::Mkdir, QList<QUrl>(), url, job);
    QVERIFY2(job->exec(), qPrintable(job->errorString()));
    QVERIFY(QFile::exists(path));
    QVERIFY(QFileInfo(path).isDir());

    m_uiInterface->clear();
    m_uiInterface->setNextReplyToConfirmDeletion(false); // act like the user didn't confirm
    FileUndoManager::self()->undo();
    QCOMPARE(m_uiInterface->askUserMockInterface()->m_askUserDeleteCalled, 1);
    QVERIFY(QFile::exists(path)); // nothing happened yet

    // OK, now do it
    m_uiInterface->clear();
    m_uiInterface->setNextReplyToConfirmDeletion(true);
    doUndo();

    QVERIFY(!QFile::exists(path));
}

void FileUndoManagerTest::testMkpath()
{
    const QString parent = srcSubDir() + "mkpath";
    const QString path = parent + "/subdir";
    QVERIFY(!QFile::exists(path));
    const QUrl url = QUrl::fromLocalFile(path);

    KIO::Job *job = KIO::mkpath(url, QUrl(), KIO::HideProgressInfo);
    job->setUiDelegate(nullptr);
    FileUndoManager::self()->recordJob(FileUndoManager::Mkpath, QList<QUrl>(), url, job);
    QVERIFY(job->exec());
    QVERIFY(QFileInfo(path).isDir());

    m_uiInterface->clear();
    m_uiInterface->setNextReplyToConfirmDeletion(true);
    doUndo();

    QVERIFY(!FileUndoManager::self()->isUndoAvailable());
    QCOMPARE(m_uiInterface->askUserMockInterface()->m_askUserDeleteCalled, 1);

    QVERIFY(!QFile::exists(path));
}

void FileUndoManagerTest::testTrashFiles()
{
    if (!KProtocolInfo::isKnownProtocol(QStringLiteral("trash"))) {
        QSKIP("kio_trash not installed");
    }

    // Trash it all at once: the file, the symlink, the subdir.
    QList<QUrl> lst = sourceList();
    lst.append(QUrl::fromLocalFile(srcSubDir()));
    KIO::Job *job = KIO::trash(lst, KIO::HideProgressInfo);
    job->setUiDelegate(nullptr);
    FileUndoManager::self()->recordJob(FileUndoManager::Trash, lst, QUrl(QStringLiteral("trash:/")), job);

    QVERIFY2(job->exec(), qPrintable(job->errorString()));

    // Check that things got removed
    QVERIFY(!QFile::exists(srcFile()));
#ifndef Q_OS_WIN
    QVERIFY(!QFileInfo(srcLink()).isSymLink());
#endif
    QVERIFY(!QFile::exists(srcSubDir()));

    // check trash?
    // Let's just check that it's not empty. kio_trash has its own unit tests anyway.
    KConfig cfg(QStringLiteral("trashrc"), KConfig::SimpleConfig);
    QVERIFY(cfg.hasGroup("Status"));
    QCOMPARE(cfg.group("Status").readEntry("Empty", true), false);

    doUndo();

    QVERIFY(QFile::exists(srcFile()));
#ifndef Q_OS_WIN
    QVERIFY(QFileInfo(srcLink()).isSymLink());
#endif
    QVERIFY(QFile::exists(srcSubDir()));

    // We can't check that the trash is empty; other partitions might have their own trash
}

void FileUndoManagerTest::testRestoreTrashedFiles()
{
    if (!KProtocolInfo::isKnownProtocol(QStringLiteral("trash"))) {
        QSKIP("kio_trash not installed");
    }

    // Trash it all at once: the file, the symlink, the subdir.
    const QFile::Permissions origPerms = QFileInfo(srcFile()).permissions();
    QList<QUrl> lst = sourceList();
    lst.append(QUrl::fromLocalFile(srcSubDir()));
    KIO::Job *job = KIO::trash(lst, KIO::HideProgressInfo);
    job->setUiDelegate(nullptr);
    QVERIFY(job->exec());

    const QMap<QString, QString> metaData = job->metaData();
    QList<QUrl> trashUrls;
    for (const QUrl &src : std::as_const(lst)) {
        QMap<QString, QString>::ConstIterator it = metaData.find("trashURL-" + src.path());
        QVERIFY(it != metaData.constEnd());
        trashUrls.append(QUrl(it.value()));
    }

    // Restore from trash
    KIO::RestoreJob *restoreJob = KIO::restoreFromTrash(trashUrls, KIO::HideProgressInfo);
    restoreJob->setUiDelegate(nullptr);
    QVERIFY(restoreJob->exec());

    QVERIFY(QFile::exists(srcFile()));
    QCOMPARE(QFileInfo(srcFile()).permissions(), origPerms);
#ifndef Q_OS_WIN
    QVERIFY(QFileInfo(srcLink()).isSymLink());
#endif
    QVERIFY(QFile::exists(srcSubDir()));

    // TODO support for RestoreJob in FileUndoManager !!!
}

static void setTimeStamp(const QString &path)
{
#ifdef Q_OS_UNIX
    // Put timestamp in the past so that we can check that the
    // copy actually preserves it.
    struct timeval tp;
    gettimeofday(&tp, nullptr);
    struct utimbuf utbuf;
    utbuf.actime = tp.tv_sec + 30; // 30 seconds in the future
    utbuf.modtime = tp.tv_sec + 60; // 60 second in the future
    utime(QFile::encodeName(path).constData(), &utbuf);
    qDebug("Time changed for %s", qPrintable(path));
#endif
}

void FileUndoManagerTest::testModifyFileBeforeUndo()
{
    // based on testCopyDirectory (so that we check that it works for files in subdirs too)
    const QString destdir = destDir();
    const QList<QUrl> lst{QUrl::fromLocalFile(srcSubDir())};
    const QUrl dest = QUrl::fromLocalFile(destdir);
    KIO::CopyJob *job = KIO::copy(lst, dest, KIO::HideProgressInfo);
    job->setUiDelegate(nullptr);
    FileUndoManager::self()->recordCopyJob(job);

    QVERIFY2(job->exec(), qPrintable(job->errorString()));

    checkTestDirectory(srcSubDir()); // src untouched
    checkTestDirectory(destSubDir());
    const QString destFile = destSubDir() + "/fileindir";
    setTimeStamp(destFile); // simulate a modification of the file

    doUndo();

    // Check that TestUiInterface::copiedFileWasModified got called
    QCOMPARE(m_uiInterface->dest().toLocalFile(), destFile);

    checkTestDirectory(srcSubDir());
    QVERIFY(!QFile::exists(destSubDir()));
}

void FileUndoManagerTest::testPasteClipboardUndo()
{
    const QList<QUrl> urls(sourceList());
    QMimeData *mimeData = new QMimeData();
    mimeData->setUrls(urls);
    KIO::setClipboardDataCut(mimeData, true);
    QClipboard *clipboard = QApplication::clipboard();
    clipboard->setMimeData(mimeData);

    // Paste the contents of the clipboard and check its status
    QUrl destDirUrl = QUrl::fromLocalFile(destDir());
    KIO::Job *job = KIO::paste(mimeData, destDirUrl, KIO::HideProgressInfo);
    QVERIFY(job);
    QVERIFY(job->exec());

    // Check if the clipboard was updated after paste operation
    QList<QUrl> urls2;
    for (const QUrl &url : urls) {
        QUrl dUrl = destDirUrl.adjusted(QUrl::StripTrailingSlash);
        dUrl.setPath(dUrl.path() + '/' + url.fileName());
        urls2 << dUrl;
    }
    QList<QUrl> clipboardUrls = KUrlMimeData::urlsFromMimeData(clipboard->mimeData());
    QCOMPARE(clipboardUrls, urls2);

    // Check if the clipboard was updated after undo operation
    doUndo();
    clipboardUrls = KUrlMimeData::urlsFromMimeData(clipboard->mimeData());
    QCOMPARE(clipboardUrls, urls);
}

void FileUndoManagerTest::testBatchRename()
{
    auto createUrl = [](const QString &path) -> QUrl {
        return QUrl::fromLocalFile(homeTmpDir() + path);
    };

    QList<QUrl> srcList;
    srcList << createUrl("textfile.txt") << createUrl("mediafile.mkv") << createUrl("sourcefile.cpp");

    createTestFile(srcList.at(0).path(), "foo");
    createTestFile(srcList.at(1).path(), "foo");
    createTestFile(srcList.at(2).path(), "foo");

    KIO::Job *job = KIO::batchRename(srcList, QLatin1String("newfile###"), 1, QLatin1Char('#'), KIO::HideProgressInfo);
    job->setUiDelegate(nullptr);
    FileUndoManager::self()->recordJob(FileUndoManager::BatchRename, srcList, QUrl(), job);
    QVERIFY2(job->exec(), qPrintable(job->errorString()));

    QVERIFY(QFile::exists(createUrl("newfile001.txt").path()));
    QVERIFY(QFile::exists(createUrl("newfile002.mkv").path()));
    QVERIFY(QFile::exists(createUrl("newfile003.cpp").path()));
    QVERIFY(!QFile::exists(srcList.at(0).path()));
    QVERIFY(!QFile::exists(srcList.at(1).path()));
    QVERIFY(!QFile::exists(srcList.at(2).path()));

    doUndo();

    QVERIFY(!QFile::exists(createUrl("newfile###.txt").path()));
    QVERIFY(!QFile::exists(createUrl("newfile###.mkv").path()));
    QVERIFY(!QFile::exists(createUrl("newfile###.cpp").path()));
    QVERIFY(QFile::exists(srcList.at(0).path()));
    QVERIFY(QFile::exists(srcList.at(1).path()));
    QVERIFY(QFile::exists(srcList.at(2).path()));
}

void FileUndoManagerTest::testUndoCopyOfDeletedFile()
{
    const QUrl source = QUrl::fromLocalFile(homeTmpDir() + QLatin1String("source.txt"));
    const QUrl dest = QUrl::fromLocalFile(homeTmpDir() + QLatin1String("copy.txt"));

    createTestFile(source.toLocalFile(), "foo");
    QVERIFY(QFileInfo::exists(source.toLocalFile()));

    {
        auto copyJob = KIO::copy(source, dest, KIO::HideProgressInfo);
        copyJob->setUiDelegate(nullptr);
        FileUndoManager::self()->recordCopyJob(copyJob);
        QVERIFY2(copyJob->exec(), qPrintable(copyJob->errorString()));
        QVERIFY(QFileInfo::exists(dest.toLocalFile()));
    }

    {
        auto deleteJob = KIO::del(dest, KIO::HideProgressInfo);
        deleteJob->setUiDelegate(nullptr);
        QVERIFY2(deleteJob->exec(), qPrintable(deleteJob->errorString()));
        QVERIFY(!QFileInfo::exists(dest.toLocalFile()));
    }

    QVERIFY(FileUndoManager::self()->isUndoAvailable());
    QSignalSpy spyUndoAvailable(FileUndoManager::self(), qOverload<bool>(&FileUndoManager::undoAvailable));
    QVERIFY(spyUndoAvailable.isValid());
    doUndo();
    QVERIFY(spyUndoAvailable.count() >= 2); // it's in fact 3, due to lock/unlock emitting it as well
    QVERIFY(!spyUndoAvailable.at(0).at(0).toBool());
    QVERIFY(!FileUndoManager::self()->isUndoAvailable());
}

void FileUndoManagerTest::testErrorDuringMoveUndo()
{
    const QString destdir = destDir();
    QList<QUrl> lst{QUrl::fromLocalFile(srcFile())};
    KIO::CopyJob *job = KIO::move(lst, QUrl::fromLocalFile(destdir), KIO::HideProgressInfo);
    job->setUiDelegate(nullptr);
    FileUndoManager::self()->recordCopyJob(job);

    QVERIFY2(job->exec(), qPrintable(job->errorString()));

    QVERIFY(!QFile::exists(srcFile())); // the source moved
    QVERIFY(QFile::exists(destFile()));
    createTestFile(srcFile(), "I'm back");

    doUndo();

    QCOMPARE(m_uiInterface->errorCode(), KIO::ERR_FILE_ALREADY_EXIST);
    QVERIFY(QFile::exists(destFile())); // still there
}

void FileUndoManagerTest::testNoUndoForSkipAll()
{
    auto *undoManager = FileUndoManager::self();

    QTemporaryDir tempDir;
    const QString tempPath = tempDir.path();

    const QString destPath = tempPath + "/dest_dir";
    QVERIFY(QDir(tempPath).mkdir("dest_dir"));
    const QUrl destUrl = QUrl::fromLocalFile(destPath);

    const QList<QUrl> lst{QUrl::fromLocalFile(tempPath + "/file_a"), QUrl::fromLocalFile(tempPath + "/file_b")};
    for (const auto &url : lst) {
        createTestFile(url.toLocalFile(), "foo");
    }

    auto createJob = [&]() {
        return KIO::copy(lst, destUrl, KIO::HideProgressInfo);
    };

    KIO::CopyJob *job = createJob();
    job->setUiDelegate(nullptr);
    undoManager->recordCopyJob(job);

    QSignalSpy spyUndoAvailable(undoManager, qOverload<bool>(&FileUndoManager::undoAvailable));
    QVERIFY(spyUndoAvailable.isValid());
    QSignalSpy spyTextChanged(undoManager, &FileUndoManager::undoTextChanged);
    QVERIFY(spyTextChanged.isValid());

    QVERIFY2(job->exec(), qPrintable(job->errorString()));

    // Src files still exist
    for (const auto &url : lst) {
        QVERIFY(QFile::exists(url.toLocalFile()));
    }

    // Files copied to dest
    for (const auto &url : lst) {
        QVERIFY(QFile::exists(destPath + '/' + url.fileName()));
    }

    // An undo command was recorded
    QCOMPARE(spyUndoAvailable.count(), 1);
    QCOMPARE(spyTextChanged.count(), 1);

    KIO::CopyJob *repeatCopy = createJob();
    // Copying the same files again to the same dest, and setting skip all
    repeatCopy->setAutoSkip(true);
    undoManager->recordCopyJob(repeatCopy);

    QVERIFY2(repeatCopy->exec(), qPrintable(repeatCopy->errorString()));

    // No new undo command was added since the job didn't actually copy anything
    QCOMPARE(spyUndoAvailable.count(), 1);
    QCOMPARE(spyTextChanged.count(), 1);
}

// TODO: add test (and fix bug) for  DND of remote urls / "Link here" (creates .desktop files) // Undo (doesn't do anything)
// TODO: add test for interrupting a moving operation and then using Undo - bug:91579

#include "moc_fileundomanagertest.cpp"