summaryrefslogtreecommitdiffstats
path: root/tests/auto/declarative/qmlvisual/shared/qmlvisualaids/testmodel.cpp
blob: 4aec14bff711193941c789c18e3c9aa31a6298f4 (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
#include "testmodel.h"
#include <QProcess>
#include <QDebug>
#include <QFile>

TestModel::TestModel(QObject *parent) :
    QObject(parent), _testName("Invalid")
{

}

//testPath is the path to the test script, and assumes the file under test has the same name and is in the dir above
bool TestModel::setTest(const QString &testPath)
{
    _good.clear();
    _bad.clear();
    _diff.clear();

    _testScriptPath = testPath;
    if(!_testScriptPath.endsWith(".qml"))
        _testScriptPath += ".qml";
    _testName = _testScriptPath.split('/').last();

    //Assumed that the test case is in the directory above and has the same name as the script
    _testPath = _testScriptPath.left(_testScriptPath.lastIndexOf('/', _testScriptPath.lastIndexOf('/') - 1))
            + '/' + _testName;

    bool ret = QFile::exists(_testPath) && QFile::exists(_testScriptPath);
    if(!ret)
        return ret;

    QFile test(_testPath);
    test.open(QFile::ReadOnly | QFile::Text);
    _testCase = test.readAll();

    QFile script(_testScriptPath);
    script.open(QFile::ReadOnly | QFile::Text);
    _testScript = script.readAll();

    QString base = _testScriptPath;
    base.chop(4);//remove .qml, replace with .%1.png
    base += ".%1.png";
    int c = 0;
    while (QFile::exists(base.arg(c))) {
        _good << "file://" + base.arg(c);
        if(QFile::exists(base.arg(c) + ".reject.png"))
            _bad << "file://" + base.arg(c) + ".reject.png";
        else
            _bad << "";

        if(QFile::exists(base.arg(c) + ".diff.png"))
            _diff << "file://" + base.arg(c) + ".diff.png";
        else
            _diff << "";

        c++;
    }

    return ret;
}

//returns true iff running the test changed the failure images.
bool TestModel::runTest()
{
    launchTester("-play");
    return false;//TODO: Actually check that
}

void TestModel::updateVisuals()
{
    launchTester("-updatevisuals");
}

void TestModel::updatePlatformVisuals()
{
    launchTester("-updateplatformvisuals");
}

void TestModel::launchTester(const QString &args)
{
    QStringList arguments;
    arguments << args << _testPath;

    QString visualTest;
#if defined(Q_WS_WIN) || defined(Q_WS_S60)
    visualTest = "tst_qmlvisual.exe";
#else
    visualTest = "./tst_qmlvisual";
#endif

    QProcess p;
    p.setProcessChannelMode(QProcess::ForwardedChannels);
    p.start(visualTest, arguments);
    p.waitForFinished();
}