summaryrefslogtreecommitdiffstats
path: root/libqsystemtest/desktoptestcontrol.h
blob: 9b1a301630f72dbe98bbcc33623abc1e6aeaf3a8 (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
#ifndef DESKTOPTESTCONTROL_H
#define DESKTOPTESTCONTROL_H

#include "testcontrol.h"
#include "qsystemtest.h"

#include <QProcess>

namespace Qt4Test {

class TestProcess : public QProcess
{
    Q_OBJECT
    public:
        TestProcess(QObject *parent = 0)
            : QProcess(parent)
            , env()
            , test(0)
        {
            connect(this,SIGNAL(readyReadStandardError()),this,SLOT(error()));
            connect(this,SIGNAL(readyReadStandardOutput()),this,SLOT(output()));
            connect(this,SIGNAL(finished(int,QProcess::ExitStatus)),
                    this,SLOT(deleteLater()));
        }

        QStringList env;
        QSystemTest* test;

    private slots:
        void output()
        {
            QByteArray text = readAllStandardOutput();
            if (!test) return;

            while (text.endsWith("\n")) text.chop(1);
            QList<QByteArray> lines = text.split('\n');
            test->applicationStandardOutput(lines);
        }

        void error()
        {
            QByteArray text = readAllStandardError();
            if (!test) return;

            while (text.endsWith("\n")) text.chop(1);
            QList<QByteArray> lines = text.split('\n');
            test->applicationStandardError(lines);
        }

    protected:
        virtual void setupChildProcess()
        {
            foreach (QString const& e, env) {
                int equals = e.indexOf('=');
                if (equals == -1) continue;
                QString key = e.left(equals);
                QString value = e.mid(equals+1);
                prependToEnv(key.toLocal8Bit(), value.toLocal8Bit());
            }
        }

    private:
        void prependToEnv(QByteArray const& key, QByteArray const& value)
        {
            // Environment variables which are a colon-separated list (like PATH)
            static const QList<QByteArray> pathlike = QList<QByteArray>()
                << "LD_LIBRARY_PATH"
            ;

            // Environment variables which can be silently clobbered
            static const QList<QByteArray> clobber = QList<QByteArray>()
                << "DISPLAY"
            ;

            QByteArray current = qgetenv(key.constData());
            QByteArray set = value;
            if (!current.isEmpty()) {
                if (pathlike.contains(key)) {
                    set = set + ":" + current;
                } else if (clobber.contains(key)) {
                } else {
                    // Cannot use qWarning because we are in a child process and we will pass
                    // through message handlers twice
                    fprintf(stderr, "Environment variable %s is already set (to \"%s\"); "
                            "qtuitestrunner will clobber it (with \"%s\") when starting test "
                            "process!"
                        ,key.constData()
                        ,current.constData()
                        ,set.constData()
                    );
                }
            }
#ifndef Q_OS_WIN
            setenv(key.constData(), set.constData(), 1);
#endif
        }
};


class DesktopTestControl : public TestControl
{
    Q_OBJECT
public:
    static DesktopTestControl* instance();
    DesktopTestControl();
    virtual ~DesktopTestControl();

    bool deviceConfiguration( QString &reply );

    bool startApplication( const QString &application, const QStringList &arguments,
                            bool styleQtUITest, const QStringList &environment, QString &reply );
    bool killApplication( const QString &application, QString &reply );

private:
    TestProcess* proc;
};

}

#endif // DESKTOPTESTCONTROL_H