File indexing completed on 2024-05-12 04:19:59

0001 /*
0002 Gwenview: an image viewer
0003 Copyright 2008 Aurélien Gâteau <agateau@kde.org>
0004 
0005 This program is free software; you can redistribute it and/or
0006 modify it under the terms of the GNU General Public License
0007 as published by the Free Software Foundation; either version 2
0008 of the License, or (at your option) any later version.
0009 
0010 This program is distributed in the hope that it will be useful,
0011 but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 GNU General Public License for more details.
0014 
0015 You should have received a copy of the GNU General Public License
0016 along with this program; if not, write to the Free Software
0017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
0018 
0019 */
0020 #ifndef TESTUTILS_H
0021 #define TESTUTILS_H
0022 
0023 // STL
0024 #include <cmath>
0025 
0026 // Qt
0027 #include <QDir>
0028 #include <QImage>
0029 #include <QSignalSpy>
0030 #include <QTemporaryDir>
0031 #include <QTest>
0032 #include <QUrl>
0033 
0034 // KF
0035 
0036 #include "config-gwenview.h"
0037 
0038 /*
0039  * This file contains simple helpers to access test files
0040  */
0041 
0042 inline QString pathForTestFile(const QString &name)
0043 {
0044     return QDir::cleanPath(QStringLiteral("%1/%2").arg(GV_TEST_DATA_DIR, name));
0045 }
0046 
0047 inline QUrl urlForTestFile(const QString &name)
0048 {
0049     return QUrl::fromLocalFile(pathForTestFile(name));
0050 }
0051 
0052 inline QString pathForTestOutputFile(const QString &name)
0053 {
0054     return QStringLiteral("%1/%2").arg(QDir::currentPath(), name);
0055 }
0056 
0057 inline QUrl urlForTestOutputFile(const QString &name)
0058 {
0059     return QUrl::fromLocalFile(pathForTestOutputFile(name));
0060 }
0061 
0062 inline bool waitForSignal(const QSignalSpy &spy, int timeout = 5)
0063 {
0064     for (int x = 0; x < timeout; ++x) {
0065         if (spy.count() > 0) {
0066             return true;
0067         }
0068         QTest::qWait(1000);
0069     }
0070     return false;
0071 }
0072 
0073 void createEmptyFile(const QString &path);
0074 
0075 /**
0076  * Returns the url of the remote url dir if remote test dir was successfully
0077  * set up.
0078  * If testFile is valid, it is copied into the test dir.
0079  */
0080 QUrl setUpRemoteTestDir(const QString &testFile = QString());
0081 
0082 /**
0083  * Make sure all objects on which deleteLater() have been called have been
0084  * destroyed.
0085  */
0086 void waitForDeferredDeletes();
0087 
0088 // FIXME: Top-level functions should move to the TestUtils namespace
0089 namespace TestUtils
0090 {
0091 bool fuzzyImageCompare(const QImage &img1, const QImage &img2, int delta = 2);
0092 
0093 bool imageCompare(const QImage &img1, const QImage &img2);
0094 
0095 void purgeUserConfiguration();
0096 
0097 class SandBoxDir : public QDir
0098 {
0099 public:
0100     SandBoxDir();
0101     void fill(const QStringList &files);
0102 
0103 private:
0104     QTemporaryDir mTempDir;
0105 };
0106 
0107 /**
0108  * An event loop which stops itself after a predefined duration
0109  */
0110 class TimedEventLoop : public QEventLoop
0111 {
0112     Q_OBJECT
0113 public:
0114     TimedEventLoop(int maxDurationInSeconds = 60);
0115 
0116     int exec(ProcessEventsFlags flags = AllEvents);
0117 
0118 private Q_SLOTS:
0119     void fail();
0120 
0121 private:
0122     QTimer *mTimer;
0123 };
0124 
0125 } // namespace
0126 
0127 #endif /* TESTUTILS_H */