File indexing completed on 2024-10-13 09:34:35

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 2007 David Faure <faure@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 #ifndef KDIRLISTERTEST_H
0008 #define KDIRLISTERTEST_H
0009 
0010 #include <QDate>
0011 #include <QEventLoop>
0012 #include <QObject>
0013 #include <QSignalSpy>
0014 #include <QTemporaryDir>
0015 #include <kdirlister.h>
0016 
0017 #include <memory>
0018 
0019 Q_DECLARE_METATYPE(KFileItemList)
0020 
0021 class GlobalInits
0022 {
0023 public:
0024     GlobalInits();
0025 };
0026 
0027 class MyDirLister : public KDirLister, GlobalInits
0028 {
0029 public:
0030     MyDirLister()
0031         : spyStarted(this, &KCoreDirLister::started)
0032         , spyItemsDeleted(this, &KCoreDirLister::itemsDeleted)
0033         , spyClear(this, qOverload<>(&KCoreDirLister::clear))
0034         , spyClearDir(this, &KCoreDirLister::clearDir)
0035         , spyCompleted(this, qOverload<>(&KCoreDirLister::completed))
0036         , spyCanceled(this, qOverload<>(&KCoreDirLister::canceled))
0037 #if KIOCORE_BUILD_DEPRECATED_SINCE(5, 79)
0038         , spyClearQUrl(this, qOverload<const QUrl &>(&KCoreDirLister::clear))
0039         , spyCompletedQUrl(this, qOverload<const QUrl &>(&KCoreDirLister::completed))
0040         , spyCanceledQUrl(this, qOverload<const QUrl &>(&KCoreDirLister::canceled))
0041 #else
0042         , spyCompletedQUrl(this, &KCoreDirLister::listingDirCompleted)
0043         , spyCanceledQUrl(this, &KCoreDirLister::listingDirCanceled)
0044 #endif
0045 #if KIOCORE_BUILD_DEPRECATED_SINCE(5, 80)
0046         , spyRedirection(this, qOverload<const QUrl &>(&KCoreDirLister::redirection))
0047 #else
0048         , spyRedirection(this, qOverload<const QUrl &, const QUrl &>(&KCoreDirLister::redirection))
0049 #endif
0050         , spyJobError(this, &KCoreDirLister::jobError)
0051     {
0052     }
0053 
0054     void clearSpies()
0055     {
0056         spyStarted.clear();
0057         spyClear.clear();
0058 #if KIOCORE_BUILD_DEPRECATED_SINCE(5, 79)
0059         spyClearQUrl.clear();
0060 #endif
0061         spyCompleted.clear();
0062         spyCompletedQUrl.clear();
0063         spyCanceled.clear();
0064         spyCanceledQUrl.clear();
0065         spyRedirection.clear();
0066         spyItemsDeleted.clear();
0067         spyJobError.clear();
0068     }
0069 
0070     QSignalSpy spyStarted;
0071     QSignalSpy spyItemsDeleted;
0072     QSignalSpy spyClear;
0073     QSignalSpy spyClearDir;
0074     QSignalSpy spyCompleted;
0075     QSignalSpy spyCanceled;
0076 #if KIOCORE_BUILD_DEPRECATED_SINCE(5, 79)
0077     QSignalSpy spyClearQUrl;
0078 #endif
0079     QSignalSpy spyCompletedQUrl;
0080     QSignalSpy spyCanceledQUrl;
0081     QSignalSpy spyRedirection;
0082     QSignalSpy spyJobError;
0083 };
0084 
0085 class KDirListerTest : public QObject
0086 {
0087     Q_OBJECT
0088 private Q_SLOTS:
0089     void initTestCase();
0090     void cleanup();
0091     void testInvalidUrl();
0092     void testNonListableUrl();
0093     void testOpenUrl();
0094     void testOpenUrlFromCache();
0095     void testNewItem();
0096     void testNewItems();
0097     void benchFindByUrl();
0098     void testNewItemByCopy();
0099     void testNewItemByCopyInSubDir();
0100     void testNewItemsInSymlink();
0101     void testRefreshItems();
0102     void testRefreshRootItem();
0103     void testDeleteItem();
0104     void testDeleteItems();
0105     void testRenameItem();
0106     void testRenameAndOverwrite();
0107     void testConcurrentListing();
0108     void testConcurrentHoldingListing();
0109     void testConcurrentListingAndStop();
0110     void testDeleteListerEarly();
0111     void testOpenUrlTwice();
0112     void testOpenUrlTwiceWithKeep();
0113     void testOpenAndStop();
0114     void testBug211472();
0115     void testRenameCurrentDir();
0116     void testRenameCurrentDirOpenUrl();
0117     void testRedirection();
0118     void testListEmptyDirFromCache();
0119     void testWatchingAfterCopyJob();
0120     void testRemoveWatchedDirectory();
0121     void testDirPermissionChange();
0122     void testCopyAfterListingAndMove(); // #353195
0123     void testRenameDirectory(); // #401552
0124     void testRequestMimeType();
0125     void testMimeFilter_data();
0126     void testMimeFilter();
0127     void testBug386763();
0128     void testCacheEviction();
0129     void testDeleteCurrentDir(); // must be just before last!
0130     void testForgetDir(); // must be last!
0131 
0132 protected Q_SLOTS: // 'more private than private slots' - i.e. not seen by qtestlib
0133     void slotNewItems(const KFileItemList &);
0134     void slotNewItems2(const KFileItemList &);
0135     void slotRefreshItems(const QList<QPair<KFileItem, KFileItem>> &);
0136     void slotRefreshItems2(const QList<QPair<KFileItem, KFileItem>> &);
0137     void slotOpenUrlOnRename(const QUrl &);
0138 
0139 Q_SIGNALS:
0140     void refreshItemsReceived();
0141 
0142 private:
0143     int fileCount() const;
0144     QString tempPath() const;
0145     void createSimpleFile(const QString &fileName);
0146     void fillDirLister2(MyDirLister &lister, const QString &path);
0147     void waitUntilMTimeChange(const QString &path);
0148     void waitUntilAfter(const QDateTime &ctime);
0149 
0150 private:
0151     int m_exitCount;
0152     QEventLoop m_eventLoop;
0153     std::unique_ptr<QTemporaryDir> m_tempDir;
0154     MyDirLister m_dirLister;
0155     KFileItemList m_items;
0156     KFileItemList m_items2;
0157     QList<QPair<KFileItem, KFileItem>> m_refreshedItems, m_refreshedItems2;
0158 };
0159 
0160 #endif