File: core-utils.cpp

package info (click to toggle)
maliit-framework 2.3.0-3.2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,488 kB
  • sloc: cpp: 13,098; ansic: 2,506; xml: 299; sh: 34; makefile: 23; sed: 4
file content (128 lines) | stat: -rw-r--r-- 4,173 bytes parent folder | download | duplicates (2)
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
/* * This file is part of Maliit framework *
 *
 * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 *
 *
 * This library is free software; you can redistribute it and/or
 * modify it 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.
 */

#include "core-utils.h"

#include <QtDebug>
#include <QtCore>
#include <QtTest>

namespace {
    const QString TestingInSandboxEnvVariable("TESTING_IN_SANDBOX");

    const QString GlobalTestPluginPath(MALIIT_TEST_PLUGINS_DIR);
    const QString TestPluginPathEnvVariable("TESTPLUGIN_PATH");

    const QString GlobalTestDataPath(MALIIT_TEST_DATA_PATH);
    const QString TestDataPathEnvVariable("TESTDATA_PATH");

    // If the environment variable envVar has a value, set *path to this value
    // Returns true on success, false on error
    bool setPathFromEnvironmentVariable(QString *path, QString envVar) {
        const QStringList env(QProcess::systemEnvironment());
        int index = env.indexOf(QRegExp('^' + envVar + "=.*", Qt::CaseInsensitive));

        if (index != -1) {
            QString pathCandidate = env.at(index);
            pathCandidate = pathCandidate.remove(
                                QRegExp('^' + envVar + '=', Qt::CaseInsensitive));
            if (!pathCandidate.isEmpty()) {
                *path = pathCandidate;
                return true;
            } else {
                qCritical() << "Invalid " << envVar << " environment variable.\n";
                return false;
            }
        }
        return true;

    }
}

namespace MaliitTestUtils {

/* Return true if we are testing against the repository tree,
or false if testing against installed software. */
bool isTestingInSandbox()
{
    bool testingInSandbox = false;
    const QStringList env(QProcess::systemEnvironment());
    int index = env.indexOf(QRegExp('^' + TestingInSandboxEnvVariable + "=.*", Qt::CaseInsensitive));
    if (index != -1) {
        QString statusCandidate = env.at(index);
        statusCandidate = statusCandidate.remove(
                              QRegExp('^' + TestingInSandboxEnvVariable + '=', Qt::CaseInsensitive));
        bool statusOk = false;
        int status = statusCandidate.toInt(&statusOk);
        if (statusOk && (status == 0 || status == 1)) {
            testingInSandbox = (status == 1);
        } else {
            qCritical() << "Invalid " << TestingInSandboxEnvVariable << " environment variable.\n";
        }
    }
    return testingInSandbox;
}

// Use either global test plugin directory or TESTPLUGIN_PATH, if it is
// set (to local sandbox's plugin directory by makefile, at least).
//
// Returns a null QString on failure
QString getTestPluginPath()
{
    QString pluginPath = GlobalTestPluginPath;
    bool success = setPathFromEnvironmentVariable(&pluginPath, TestPluginPathEnvVariable);

    if (!success) {
        return QString();
    }

    if (!QDir("./").exists(pluginPath)) {
        qCritical("Test plugin directory does not exist.");
        return QString();
    }
    return pluginPath;
}

// Use either global test plugin directory or TESTDATA_PATH, if it is
// set (to local sandbox's plugin directory by makefile, at least).
//
// The test data path is the base directory where the tests directories (like ut_mtoolbardata) reside.
//
// Returns a null QString on failure
QString getTestDataPath()
{
    QString dataPath = GlobalTestDataPath;
    bool success = setPathFromEnvironmentVariable(&dataPath, TestDataPathEnvVariable);

    if (!success) {
        return QString();
    }

    if (!QDir("./").exists(dataPath)) {
        qCritical("Test data directory does not exist.");
        return QString();
    }
    return dataPath;
}


// Wait for signal or timeout; use SIGNAL macro for signal
void waitForSignal(const QObject* object, const char* signal, int timeout)
{
    QEventLoop eventLoop;
    QObject::connect(object, signal, &eventLoop, SLOT(quit()));
    QTimer::singleShot(timeout, &eventLoop, SLOT(quit()));
    eventLoop.exec();
}

}