summaryrefslogtreecommitdiffstats
path: root/tests/auto/uiloader/uiloader/uiloader.cpp
blob: 9246c30d805cc8257dea9753aca8af4218a71872 (plain)
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
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/


#include "uiloader.h"

#include <QtCore/QDir>
#include <QtCore/QProcess>
#include <QtCore/QSettings>
#include <QtCore/QCoreApplication>

#include <QtTest/QSignalSpy>
#include <QTest>

#include <QString>
#include <QHash>
#include <QFile>
#include <QFtp>
#include <QObject>
#include <QHostInfo>
#include <QWidget>
#include <QImage>

#include <QLibraryInfo>


/*
 * Our own QVERIFY since the one from QTest can't be used in non-void functions.
 * Just pass the desired return value as third argument.
 */

#define QVERIFY3(statement, description, returnValue) \
do {\
    if (statement) {\
        if (!QTest::qVerify(true, #statement, (description), __FILE__, __LINE__))\
            return returnValue;\
    } else {\
        if (!QTest::qVerify(false, #statement, (description), __FILE__, __LINE__))\
            return returnValue;\
    }\
} while (0)



uiLoader::uiLoader(const QString &_pathToProgram)
    : pathToProgram(_pathToProgram)
{
  //   initTests();
}




/*
 * Load the configuration file for your machine.
 * Return true if everything was loaded, else false.
 *
 * If the hostname is 'kayak', the config file should be 'kayak.ini':
 *
 *  [General]
 *  ftpBaseDir=/arthurtest
 *  ftpHost=wartburg
 *  ftpPass=anonymouspass
 *  ftpUser=anonymous
 *  output=testresults
 *
 *  [engines]
 *  1\engine=uic
 *  size=1
 */

bool uiLoader::loadConfig(const QString &filePath, QString *errorMessage)
{
    qDebug() << " ========== Loading config file " << filePath;
    configPath = filePath;

    // If there is no config file, dont proceed;
    QSettings settings( filePath, QSettings::IniFormat, this );

    // all keys available?
    QStringList keyList;
    keyList << QLatin1String("output") << QLatin1String("ftpUser") << QLatin1String("ftpPass") << QLatin1String("ftpHost") << QLatin1String("ftpBaseDir");
    for (int i = 0; i < keyList.size(); ++i) {
        const QString currentKey = keyList.at(i);
        if (!settings.contains(currentKey)) {
            *errorMessage = QString::fromLatin1("Config file '%1' does not contain the required key '%2'.").arg(filePath, currentKey);
            return false;
        }

        qDebug() << "\t\t(I)" << currentKey << "\t" << settings.value(currentKey).toString();
    }

    const int size = settings.beginReadArray(QLatin1String("engines"));
    if (!size) {
        *errorMessage = QString::fromLatin1("Config file '%1' does not contain the necessary section engines.").arg(filePath);
        return false;
    }

    // get the values
    for ( int i = 0; i < size; ++i ) {
        settings.setArrayIndex(i);
        qDebug() << "\t\t(I)" << "engine" << "\t" << settings.value( "engine" ).toString();
        enginesToTest.insert(settings.value(QLatin1String("engine")).toString(), QLatin1String("Info here please :p"));
    }
    settings.endArray();

    output = settings.value(QLatin1String("output")).toString();
    output += QDir::separator() + QLibraryInfo::buildKey() + QDir::separator() + QString( qVersion() );
    ftpUser = settings.value( QLatin1String("ftpUser") ).toString();
    ftpPass = settings.value( QLatin1String("ftpPass") ).toString();
    ftpHost = settings.value( QLatin1String("ftpHost") ).toString();
    ftpBaseDir = settings.value( QLatin1String("ftpBaseDir") ).toString() + QDir::separator() + QHostInfo::localHostName().split( QLatin1Char('.')).first();
    threshold = settings.value( QLatin1String("threshold") ).toString();

    qDebug() << "\t(I) Values adapted:";
    qDebug() << "\t\t(I)" << "ftpBaseDir" << "\t" << ftpBaseDir;
    qDebug() << "\t\t(I)" << "output" << "\t" << output;

    return true;
}

/*
 * Upload testresults to the server in order to create the new baseline.
 */

void uiLoader::createBaseline()
{
    // can't use ftpUploadFile() here
    qDebug() << " ========== Uploading baseline of only the latest test values ";

    QFtp ftp;
    ftp.connectToHost( ftpHost );
    ftp.login( ftpUser, ftpPass );
    ftp.cd( ftpBaseDir );

    QDir dir( output );

    // Upload all the latest test results to the FTP server's baseline directory.
    QHashIterator<QString, QString> i(enginesToTest);
    while ( i.hasNext() ) {
        i.next();

        dir.cd( i.key() );
        ftp.cd( i.key() + ".baseline" );

        dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
        dir.setNameFilters( QStringList() << "*.png" );
        QFileInfoList list = dir.entryInfoList();

        dir.cd( ".." );

        for (int n = 0; n < list.size(); n++) {
            QFileInfo fileInfo = list.at( n );
            QFile file( QString( output ) + "/" + i.key() + "/" + fileInfo.fileName() );

            errorMsg = "could not open file " + fileInfo.fileName();
            QVERIFY2( file.open(QIODevice::ReadOnly), qPrintable(errorMsg));

            QByteArray fileData = file.readAll();
            file.close();

            ftp.put( fileData, fileInfo.fileName(), QFtp::Binary );
            qDebug() << "\t(I) Uploading:" << fileInfo.fileName() << "with file size" << fileData.size();
        }

        ftp.cd( ".." );
    }

    ftp.close();

    while ( ftp.hasPendingCommands() )
        QCoreApplication::instance()->processEvents();
}



/*
 * Download baseline from server in order to compare results.
 */

void uiLoader::downloadBaseline()
{
    qDebug() << " ========== Downloading baseline...";

    QHashIterator<QString, QString> i(enginesToTest);
    while ( i.hasNext() ) {
        i.next();
        QString engineName = i.key();

        QString dirWithFiles = ftpBaseDir + '/' + engineName + ".baseline";
        QString ftpDir = ftpBaseDir + '/' + engineName + ".baseline";
        QString saveToDir = QDir::currentPath() + '/' + output + '/' + engineName + ".baseline";

        ftpList(dirWithFiles);

        QList<QString> filesToDownload(lsDirList);
        ftpGetFiles(filesToDownload, ftpDir, saveToDir);
    }
}



/*
 * Enter the dir pathDir local and remove all files (not recursive!)
 */

void uiLoader::clearDirectory(const QString& pathDir)
{
    qDebug() << "\t(I) Clearing directory local: " << pathDir;

    QDir dir(pathDir);
    dir.setFilter(QDir::Files);
    QStringList list = dir.entryList();

    for (int n = 0; n < list.size(); n++) {
        QString filePath = pathDir + "/" + list.at(n);
        QFile file(filePath);

        errorMsg = "could not remove file " + filePath;
        QVERIFY2( file.remove(), qPrintable(errorMsg));
    }

}



/*
 * Setup the local environment.
 */

void uiLoader::setupLocal()
{
    qDebug( " ========== Setting up local environment" );

    QDir dir;

    errorMsg = "could not create path " + output;
    QVERIFY2( dir.mkpath(output), qPrintable(errorMsg) );

    QHashIterator<QString, QString> j(enginesToTest);
    while ( j.hasNext() ) {
        j.next();

        QString engineName = j.key();
        QString engineDir = output + '/' + engineName;

        // create <engine> or clean it
        QString tmpPath = output + '/' + engineName;
        if ( dir.exists(tmpPath) ) {
            clearDirectory(tmpPath);
        } else {
            dir.mkdir(tmpPath);
        }

        // create *.baseline or clean it
        tmpPath = output + '/' + engineName + ".baseline";
        if ( dir.exists(tmpPath) ) {
            clearDirectory(tmpPath);
        } else {
            dir.mkdir(tmpPath);
        }

        // create *.diff or clean it
        tmpPath = output + '/' + engineName + ".diff";
        if ( dir.exists(tmpPath) ) {
            clearDirectory(tmpPath);
        } else {
            dir.mkdir(tmpPath);
        }

        // create *.failed or clean it
        tmpPath = output + '/' + engineName + ".failed";
        if ( dir.exists(tmpPath) ) {
            clearDirectory(tmpPath);
        } else {
            dir.mkdir(tmpPath);
        }
    }

    qDebug() << "\t(I) Created on local machine:" << output;
}



/*
 * Setup the remote environment.
 */

void uiLoader::setupFTP()
{
    qDebug( " ========== Setting up FTP environment" );

    // create dirs on ftp server
    ftpMkDir( ftpBaseDir );
    ftpBaseDir += "/" + QLibraryInfo::buildKey();
    ftpMkDir( ftpBaseDir );
    ftpBaseDir += "/" + QString( qVersion() );
    ftpMkDir( ftpBaseDir );

    QString dir = "";
    ftpList(ftpBaseDir + '/' + dir);
    QList<QString> dirListing(lsDirList);

    // create *.failed, *.diff if necessary, else remove the files in it
    // if *.baseline does not exist, memorize it
    QHashIterator<QString, QString> j(enginesToTest);
    while ( j.hasNext() ) {
        j.next();

        QString curDir = QString( j.key() ) + ".failed";
        if ( dirListing.contains( curDir ) ) {
            ftpClearDirectory(ftpBaseDir + "/" + curDir + "/");
        } else {
            ftpMkDir(ftpBaseDir + "/" + curDir + "/");
        }

        curDir = QString( j.key() ) + ".diff";
        if ( dirListing.contains( curDir ) ) {
            ftpClearDirectory(ftpBaseDir + "/" + curDir + "/");
        } else {
            ftpMkDir(ftpBaseDir + "/" + curDir + "/");
        }

        curDir = QString( j.key() ) + ".baseline";
        lsNeedBaseline.clear();
        if ( !dirListing.contains( curDir ) ) {
            ftpMkDir(ftpBaseDir + "/" + curDir + "/");
            lsNeedBaseline << j.key();
        } else {
            qDebug() << "\t(I)" << curDir << "exists on server.";
        }
    }
}



/*
 * Download files listed in fileLisiting from dir pathRemoteDir on sever and save
 * them in pathSaveDir.
 */

void uiLoader::ftpGetFiles(QList<QString>& fileListing, const QString& pathRemoteDir, const QString& pathSaveDir)
{
    QFtp ftp;
    ftp.connectToHost( ftpHost );
    ftp.login( ftpUser, ftpPass );

    if ( !fileListing.empty() ) {
        for ( int i = 0; i < fileListing.size(); ++i ) {
            QFile file( pathSaveDir + "/" +  fileListing.at(i) );

            errorMsg = "could not open file for writing: " + file.fileName();
            QVERIFY2( file.open(QIODevice::WriteOnly), qPrintable(errorMsg) );

            QString ftpFileName = pathRemoteDir + '/' + fileListing.at(i);
            ftp.get( ftpFileName, &file );
            //qDebug() << "\t(I) Got" << file.fileName();
            ftp.list(); //Only there to fill up a slot in the pendingCommands queue.

            while ( ftp.hasPendingCommands() )
                QCoreApplication::instance()->processEvents();

            file.close();
        }
    }

    ftp.close();

    while ( ftp.hasPendingCommands() )
        QCoreApplication::instance()->processEvents();

    fileListing.clear();
}



/*
 * Upload the file filePath to the server and save it there at filePathRemote.
 *
 * HINT: It seems you can't use this function in a loop, to many connections
 *       are established?!
 */

bool uiLoader::ftpUploadFile(const QString& filePathRemote, const QString& filePath)
{
    QFile file(filePath);

    errorMsg = "could not open file: " + filePath;
    QVERIFY3( file.open(QIODevice::ReadOnly), qPrintable(errorMsg), false );

    QByteArray contents = file.readAll();
    file.close();

    qDebug() << "\t(I) Uploading file to" << filePathRemote;

    QFtp ftp;
    ftp.connectToHost( ftpHost );
    ftp.login( ftpUser, ftpPass );

    ftp.put( contents, filePathRemote, QFtp::Binary );

    ftp.close();

    while ( ftp.hasPendingCommands() )
        QCoreApplication::instance()->processEvents();

    return true;
}



/*
 * Enter the dir dir on the server and remove all files (not recursive!)
 */

void uiLoader::ftpClearDirectory(const QString& pathDir)
{
    qDebug() << "\t(I) Clearing directory remote: " << pathDir;

    ftpList(pathDir);
    QList<QString> dirListing(lsDirList);

    QFtp ftp;
    ftp.connectToHost( ftpHost );
    ftp.login( ftpUser, ftpPass );

    for (int i = 0; i < dirListing.size(); ++i) {
        QString file = dirListing.at(i);
        qDebug() << "\t(I) Removing" << pathDir + file;
        ftp.remove(pathDir + file);
    }

    ftp.close();

    while ( ftp.hasPendingCommands() )
        QCoreApplication::instance()->processEvents();
}



/*
 * Get a directory listing from the server in the dir dir.
 * You can access it via lsDirList.
 */

void uiLoader::ftpList(const QString & dir) {
    qDebug() << "\t(I) Getting list of files in dir" << dir;

    lsDirList.clear();

    QFtp ftp;
    QObject::connect( &ftp, SIGNAL( listInfo( const QUrlInfo & ) ), this, SLOT( ftpAddLsEntry(const QUrlInfo & ) ) );
    //QObject::connect( &ftp, SIGNAL( done( bool ) ), this, SLOT( ftpAddLsDone( bool ) ) );

    ftp.connectToHost( ftpHost );
    ftp.login( ftpUser, ftpPass );

    ftp.list( dir );
    ftp.close();

    while ( ftp.hasPendingCommands() )
        QCoreApplication::instance()->processEvents();
}



/*
 * Creates a dir on the ftp server.
 *
 * Hint: If the ftp.mkdir() fails we just assume the dir already exist.
 */

void uiLoader::ftpMkDir( QString pathDir )
{
    QFtp ftp;

    QSignalSpy commandSpy(&ftp, SIGNAL(commandFinished(int, bool)));

    ftp.connectToHost( ftpHost );
    ftp.login( ftpUser, ftpPass );
    const int command = ftp.mkdir( pathDir );
    ftp.close();

    while ( ftp.hasPendingCommands() )
        QCoreApplication::instance()->processEvents();

    // check wheter there was an error or not
    for (int i = 0; i < commandSpy.count(); ++i) {
        if (commandSpy.at(i).at(0) == command) {
            if ( !commandSpy.at(i).at(1).toBool() ) {
                qDebug() << "\t(I) Created at remote machine:" << pathDir;
            } else {
                qDebug() << "\t(I) Could not create on remote machine - probably the dir exists";
            }
        }
    }
}



/*
 * Just a slot, needed for ftpList().
 */

void uiLoader::ftpAddLsEntry( const QUrlInfo &urlInfo )
{
    //Just adding the file to the list
    lsDirList << urlInfo.name();
}

/*
 * Return a list of the test case ui files
 */

QStringList uiLoader::uiFiles() const
{
    QString baselinePath = QDir::currentPath();
    baselinePath += QLatin1String("/baseline");
    QDir dir(baselinePath);
    dir.setFilter(QDir::Files);
    dir.setNameFilters(QStringList(QLatin1String("*.ui")));
    const QFileInfoList list = dir.entryInfoList();
    QStringList rc;
    const QChar slash = QLatin1Char('/');
    foreach (const QFileInfo &fi, list) {
        QString fileAbsolutePath = baselinePath;
        fileAbsolutePath += slash;
        fileAbsolutePath += fi.fileName();
        rc.push_back(fileAbsolutePath);
    }
    return rc;
}
/*
 * The actual method for generating local files that will be compared
 * to the baseline.
 *
 * The external program uiscreenshot/uiscreenshot is called to generate
 * *.png files of *.ui files.
 */

void uiLoader::executeTests()
{
    qDebug(" ========== Executing the tests...[generating pngs from uis]");

    qDebug() << "Current Dir" << QDir::currentPath();

    qDebug() << "\t(I) Using" << pathToProgram;

    QProcess myProcess;
    foreach(const QString &fileAbsolutePath, uiFiles()) {
        qDebug() << "\t(I) Current file:" << fileAbsolutePath;

        QHashIterator<QString, QString> j(enginesToTest);
        while ( j.hasNext() ) {
            j.next();

            QString outputDirectory = output + '/' + j.key();

            QStringList arguments;
            arguments << fileAbsolutePath;
            arguments << outputDirectory;

            myProcess.start(pathToProgram, arguments);

            // took too long?
            errorMsg = "process does not exited normally (QProcess timeout) -  " + pathToProgram;
            QVERIFY2( myProcess.waitForFinished(), qPrintable(errorMsg) );

            qDebug() << "\n" << myProcess.readAllStandardError();

            // check exit code/status
            errorMsg = "process does not exited normally - " + pathToProgram;
            QVERIFY2( myProcess.exitStatus() == QProcess::NormalExit, qPrintable(errorMsg) );
            QVERIFY2( myProcess.exitCode() == EXIT_SUCCESS, qPrintable(errorMsg) );
        }
    }
}

/*
 * Comparing generated files to the baseline.
 */

bool uiLoader::compare()
{
    qDebug( " ========== Now comparing the results to the baseline" );

    QDir dir(output);

    QHashIterator<QString, QString> i(enginesToTest);
    while ( i.hasNext() ) {
        i.next();

        QString engineName = i.key();

        // Perform comparisons between the two directories.
        dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
        dir.setNameFilters( QStringList() << "*.png" );
        dir.cd( engineName + ".baseline" );

        QFileInfoList list = dir.entryInfoList();

        for (int i = 0; i < list.size(); ++i) {
            QFileInfo fileInfo = list.at(i);
            diff(output, engineName, fileInfo.fileName());
        }
    }

    return true;
}




void uiLoader::diff(const QString& basedir, const QString& engine, const QString& fileName)
{
    QString filePathBaseline = basedir + "/" + engine + ".baseline/" + fileName;
    QString filePathGenerated = basedir + "/" + engine + '/' + fileName;

    qDebug() << "\t(I) Comparing" << filePathBaseline;
    qDebug() << "\t(I) Comparing" << filePathGenerated;

    QString filePathDiffImage = basedir + "/" + engine + ".diff/" + fileName;

    if ( QFile::exists(filePathGenerated) ) {
        QString filePathDiffImage = basedir + "/" + engine + ".diff/" + fileName;
        int pixelDiff = imgDiff(filePathBaseline, filePathGenerated, filePathDiffImage);

        if ( pixelDiff <= threshold.toInt() ) {
            qDebug() << "\t(I) TEST OK";
            QVERIFY(true);
        } else {
            qDebug() << "\t(I) TEST FAILED";
            qDebug() << "\t(I)\t...saving baseline in *.failed";

            // local: save in *.failed
            QString filePathFailed = basedir + "/" + engine + ".failed/" + fileName;
            errorMsg = "Could not save " + filePathGenerated + " to " + filePathFailed;
            QVERIFY2( QFile::copy(filePathGenerated, filePathFailed), qPrintable(errorMsg) );

            // remote: save in *.failed
            QString filePathFailedRemote = ftpBaseDir + "/" + engine + ".failed" + "/" + fileName;
            ftpUploadFile(filePathFailedRemote, filePathGenerated);

            errorMsg = "Something broke in the image comparison with  " + filePathDiffImage;
            QVERIFY2( (pixelDiff != -1), qPrintable(errorMsg) );

            // remote: save in *.diff
            QString filePathDiffRemote = ftpBaseDir + "/" + engine + ".diff" + "/" + fileName;
            ftpUploadFile(filePathDiffRemote, filePathDiffImage);
            QFAIL(qPrintable(fileName));
        }

    } else {
        qWarning() << "\t(W) Expected generated file" << filePathGenerated << "does not exist.";
        qWarning() << "\t(W)   ...saving baseline in *.failed";

        // save local
        QString filePathMissing = basedir + '/' + engine + ".failed/" + fileName + "_missing";
        errorMsg = "Could not save " + filePathMissing;
        QVERIFY2( QFile::copy(filePathBaseline, filePathMissing), qPrintable(errorMsg) );

        // save remote
        QString filePathDiffRemote = ftpBaseDir + "/" + engine + ".diff" + "/" + fileName;
        ftpUploadFile(filePathDiffRemote, filePathBaseline);

        errorMsg = filePathGenerated + " was not generated, but baseline for this file exists";
        QVERIFY2(false, qPrintable(errorMsg));
    }

}

/*
 * Execution starts here.
 */

uiLoader::TestResult uiLoader::runAutoTests(QString *errorMessage)
{
    // SVG needs this widget...
    QWidget dummy;

    qDebug() << "Running test on buildkey:" << QLibraryInfo::buildKey() << "  qt version:" << qVersion();
    qDebug() << "Initializing tests...";

    // load config
    const QString configFileName = QHostInfo::localHostName().split(QLatin1Char('.')).first() + QLatin1String(".ini");
    const QFileInfo fi(configFileName);
    if (!fi.isFile() || !fi.isReadable()) {
        *errorMessage = QString::fromLatin1("Config file '%1' does not exist or is not readable.").arg(configFileName);
        return TestNoConfig;
    }

    if (!loadConfig(configFileName, errorMessage))
        return TestConfigError;

    // reset the local environment where the results are stored
    setupLocal();

    // reset the FTP environment where the results are stored
    setupFTP();

    // retrieve the latest test result baseline from the FTP server.
    downloadBaseline();

    // execute tests
    executeTests();

    // upload testresults as new baseline or compare results
    if ( lsNeedBaseline.size() )
        createBaseline();
    else
        compare();

    return TestRunDone;
}

int uiLoader::imgDiff(const QString fileA, const QString fileB, const QString output)
{
//  qDebug() << "Comparing " << fileA << " and " << fileB << " outputting to " << output;
  QImage imageA(fileA);
  QImage imageB(fileB);

  // Invalid images
  if (imageA.isNull() || imageB.isNull())
  {
    qDebug() << "Fatal error: unable to open one or more input images.";
    return false;
  }

  //Choose the largest image size, so that the output can capture the entire diff.
  QSize largestSize = imageA.size();
  QSize otherSize = imageB.size();

  if (largestSize.width() < otherSize.width())
    largestSize.setWidth(otherSize.width());

  if (largestSize.height() < otherSize.height())
    largestSize.setHeight(otherSize.height());

  QImage imageDiff(largestSize, QImage::Format_ARGB32);

  imageA = imageA.convertToFormat(QImage::Format_ARGB32);
  imageB = imageB.convertToFormat(QImage::Format_ARGB32);

  int pixelDiff = 0;

  for (int y = 0; y < imageDiff.height(); ++y)
  {
    for (int x = 0; x < imageDiff.width(); ++x)
    {
      //Are the pixels within range? Else, draw a black pixel in diff.
      if (imageA.valid(x,y) && imageB.valid(x,y))
      {
        //Both images have a pixel at x,y - are they the same? If not, black pixel in diff.
        if (imageA.pixel(x,y) != imageB.pixel(x,y))
        {
          imageDiff.setPixel(x,y,0xff000000);
          pixelDiff++;
        }
        else
          imageDiff.setPixel(x,y,0xffffffff);
      }
      else
      {
        imageDiff.setPixel(x,y,0xff000000);
        pixelDiff++;
      }
    }
  }

  imageDiff.setText("comment", QString::number(pixelDiff));

  if (!imageDiff.save(output, "PNG"))
      pixelDiff = -1;

  return pixelDiff;
}