~ubuntu-branches/ubuntu/precise/gwenview/precise-proposed

« back to all changes in this revision

Viewing changes to tests/auto/testutils.h

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-15 14:17:54 UTC
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: package-import@ubuntu.com-20111215141754-z043hyx69dulbggf
Tags: upstream-4.7.90
Import upstream version 4.7.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Gwenview: an image viewer
 
3
Copyright 2008 Aurélien Gâteau <agateau@kde.org>
 
4
 
 
5
This program is free software; you can redistribute it and/or
 
6
modify it under the terms of the GNU General Public License
 
7
as published by the Free Software Foundation; either version 2
 
8
of the License, or (at your option) any later version.
 
9
 
 
10
This program is distributed in the hope that it will be useful,
 
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
GNU General Public License for more details.
 
14
 
 
15
You should have received a copy of the GNU General Public License
 
16
along with this program; if not, write to the Free Software
 
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
18
 
 
19
*/
 
20
#ifndef TESTUTILS_H
 
21
#define TESTUTILS_H
 
22
 
 
23
#include <math.h>
 
24
 
 
25
// Qt
 
26
#include <QDir>
 
27
#include <QImage>
 
28
#include <QSignalSpy>
 
29
#include <QTest>
 
30
 
 
31
// KDE
 
32
#include <kdebug.h>
 
33
#include <ktempdir.h>
 
34
#include <kurl.h>
 
35
 
 
36
class SandBoxDir : public QDir
 
37
{
 
38
public:
 
39
    SandBoxDir()
 
40
        : mTempDir(QDir::currentPath() + "/sandbox-") {
 
41
        setPath(mTempDir.name());
 
42
    }
 
43
 
 
44
private:
 
45
    KTempDir mTempDir;
 
46
};
 
47
 
 
48
/*
 
49
 * This file contains simple helpers to access test files
 
50
 */
 
51
 
 
52
inline QString pathForTestFile(const QString& name)
 
53
{
 
54
    return QDir::cleanPath(QString("%1/../data/%2").arg(KDESRCDIR).arg(name));
 
55
}
 
56
 
 
57
inline KUrl urlForTestFile(const QString& name)
 
58
{
 
59
    KUrl url;
 
60
    url.setPath(pathForTestFile(name));
 
61
    return url;
 
62
}
 
63
 
 
64
inline QString pathForTestOutputFile(const QString& name)
 
65
{
 
66
    return QString("%1/%2").arg(QDir::currentPath()).arg(name);
 
67
}
 
68
 
 
69
inline KUrl urlForTestOutputFile(const QString& name)
 
70
{
 
71
    KUrl url;
 
72
    url.setPath(pathForTestOutputFile(name));
 
73
    return url;
 
74
}
 
75
 
 
76
inline bool waitForSignal(const QSignalSpy& spy, int timeout = 5)
 
77
{
 
78
    for (int x = 0; x < timeout; ++x) {
 
79
        if (spy.count() > 0) {
 
80
            return true;
 
81
        }
 
82
        QTest::qWait(1000);
 
83
    }
 
84
    return false;
 
85
}
 
86
 
 
87
inline bool fuzzyImageCompare(const QImage& img1, const QImage& img2)
 
88
{
 
89
    if (img1.size() != img2.size()) {
 
90
        kWarning() << "Different sizes" << img1.size() << img2.size();
 
91
        return false;
 
92
    }
 
93
    if (img1.format() != img2.format()) {
 
94
        kWarning() << "Different formats" << img1.format() << img2.format();
 
95
        return false;
 
96
    }
 
97
 
 
98
    for (int posY = 0; posY < img1.height(); ++posY) {
 
99
        for (int posX = 0; posX < img2.width(); ++posX) {
 
100
            if (!(int)abs((double)img1.pixel(posX, posY) - img2.pixel(posX, posY)) > 2) {
 
101
                kWarning() << "Different at" << QPoint(posX, posY);
 
102
                return false;
 
103
            }
 
104
        }
 
105
    }
 
106
    return true;
 
107
}
 
108
 
 
109
void createEmptyFile(const QString& path);
 
110
 
 
111
/**
 
112
 * Returns the url of the remote url dir if remote test dir was successfully
 
113
 * set up.
 
114
 * If testFile is valid, it is copied into the test dir.
 
115
 */
 
116
KUrl setUpRemoteTestDir(const QString& testFile = QString());
 
117
 
 
118
#endif /* TESTUTILS_H */