summaryrefslogtreecommitdiff
path: root/tools/qtestlib/wince/cetest/main.cpp
blob: e0e475ccc8cb803d1a1cb224a0f6d189e1efedde (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
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the tools applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, 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.0, 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://www.qtsoftware.com/contact.
** $QT_END_LICENSE$
**
****************************************************************************/

#ifdef QT_CETEST_NO_ACTIVESYNC
#   include "cetcpsyncconnection.h"
#else
#   include "activesyncconnection.h"
#endif

#include "deployment.h"
#include <option.h>
#include <project.h>
#include <property.h>
#include <qstringlist.h>
#include <qfileinfo.h>
#include <qdir.h>
#include <iostream>
using namespace std;

const int debugLevel = 0;
void debugOutput(const QString& text, int level)
{
    if (level <= debugLevel)
        cout << qPrintable(text) << endl;
}

// needed for QMake sources to compile
QString project_builtin_regx() { return QString();}
static QString pwd;
QString qmake_getpwd()
{
    if(pwd.isNull())
        pwd = QDir::currentPath();
    return pwd;
}
bool qmake_setpwd(const QString &p)
{
    if(QDir::setCurrent(p)) {
        pwd = QDir::currentPath();
        return true;
    }
    return false;
}

namespace TestConfiguration {
    QString localExecutable;
    QString localQtConf;
    QString remoteTestPath;
    QString remoteLibraryPath;
    QString remoteExecutable;
    QString remoteResultFile;

    bool testDebug;
    void init()
    {
        testDebug = true;
        localQtConf = QLatin1String("no");
        remoteTestPath = QLatin1String("\\Program Files\\qt_test");
        remoteLibraryPath = remoteTestPath;
        remoteResultFile = QLatin1String("\\qt_test_results.txt");
    }
}

void usage()
{
    cout <<
        "QTestLib options\n"
        " -functions : Returns a list of current testfunctions\n"
        " -xml       : Outputs results as XML document\n"
        " -lightxml  : Outputs results as stream of XML tags\n"
        " -o filename: Writes all output into a file\n"
        " -silent    : Only outputs warnings and failures\n"
        " -v1        : Print enter messages for each testfunction\n"
        " -v2        : Also print out each QVERIFY/QCOMPARE/QTEST\n"
        " -vs        : Print every signal emitted\n"
        " -eventdelay ms    : Set default delay for mouse and keyboard simulation to ms milliseconds\n"
        " -keydelay ms      : Set default delay for keyboard simulation to ms milliseconds\n"
        " -mousedelay ms    : Set default delay for mouse simulation to ms milliseconds\n"
        " -keyevent-verbose : Turn on verbose messages for keyboard simulation\n"
        " -maxwarnings n    : Sets the maximum amount of messages to output.\n"
        "                     0 means unlimited, default: 2000\n"
        " -help             : This help\n";
    cout <<
        "cetest specific options\n"
        " -debug            : Test debug version[default]\n"
        " -release          : Test release version\n"
        " -libpath <path>   : Remote path to deploy Qt libraries to\n"
        " -qt-delete        : Delete the Qt libraries after execution\n"
        " -project-delete   : Delete the project file(s) after execution\n"
        " -delete           : Delete everything deployed after execution\n"
        " -conf             : Specify location of qt.conf file\n"
        " -f <file>         : Specify project file\n"
        " -cache <file>     : Specify .qmake.cache file to use\n"
        " -timeout <value>  : Specify a timeout value after which the test will be terminated\n"
        "                     -1 specifies waiting forever (default)\n"
        "                      0 specifies starting the process detached\n"
        "                     >0 wait <value> seconds\n"
        "\n";
}

int main(int argc, char **argv)
{
    QStringList arguments;
    for (int i=0; i<argc; ++i)
        arguments.append(QString::fromLatin1(argv[i]));

    TestConfiguration::init();

    QStringList launchArguments;
    QString resultFile;
    QString proFile;
    QString cacheFile;
    int timeout = -1;
    bool cleanupQt = false;
    bool cleanupProject = false;

    for (int i=1; i<arguments.size(); ++i) {
        if (arguments.at(i).toLower() == QLatin1String("-help")
                    || arguments.at(i).toLower() == QLatin1String("--help")
                    || arguments.at(i).toLower() == QLatin1String("/?")) {
            usage();
            return 0;
        } else if (arguments.at(i).toLower() == QLatin1String("-o")) {
            if (++i == arguments.size()) {
                cout << "Error: No output file specified!" << endl;
                return -1;
            }
            resultFile = arguments.at(i);
        } else if (arguments.at(i).toLower() == QLatin1String("-eventdelay")
                    || arguments.at(i).toLower() == QLatin1String("-keydelay")
                    || arguments.at(i).toLower() == QLatin1String("-mousedelay")
                    || arguments.at(i).toLower() == QLatin1String("-maxwarnings")) {
            launchArguments.append(arguments.at(i++));
            if (i == arguments.size()) {
                cout << "Please specify value for:" << qPrintable(arguments.at(i-1).mid(1)) << endl;
                return -1;
            }
            launchArguments.append(arguments.at(i));
        } else if (arguments.at(i).toLower() == QLatin1String("-debug")) {
            TestConfiguration::testDebug = true;
            Option::before_user_vars.append("CONFIG-=release");
            Option::before_user_vars.append("CONFIG+=debug");
        } else if (arguments.at(i).toLower() == QLatin1String("-release")) {
            TestConfiguration::testDebug = false;
            Option::before_user_vars.append("CONFIG-=debug");
            Option::before_user_vars.append("CONFIG+=release");
        } else if (arguments.at(i).toLower() == QLatin1String("-libpath")) {
            if (++i == arguments.size()) {
                cout << "Error: No library path specified!" << endl;
                return -1;
            }
            TestConfiguration::remoteLibraryPath = arguments.at(i);
        } else if (arguments.at(i).toLower() == QLatin1String("-qt-delete")) {
            cleanupQt = true;
        } else if (arguments.at(i).toLower() == QLatin1String("-project-delete")) {
            cleanupProject = true;
        } else if (arguments.at(i).toLower() == QLatin1String("-delete")) {
            cleanupQt = true;
            cleanupProject = true;
        } else if (arguments.at(i).toLower() == QLatin1String("-conf")) {
            if (++i == arguments.size()) {
                cout << "Error: No qt.conf file specified!" << endl;
                return -1;
            }
            if (!QFileInfo(arguments.at(i)).exists())
                cout << "Warning: could not find qt.conf file at:" << qPrintable(arguments.at(i)) << endl;
            else
                TestConfiguration::localQtConf = arguments.at(i);
        } else if (arguments.at(i).toLower() == QLatin1String("-f")) {
            if (++i == arguments.size()) {
                cout << "Error: No output file specified!" << endl;
                return -1;
            }
            proFile = arguments.at(i);
        } else if (arguments.at(i).toLower() == QLatin1String("-cache")) {
            if (++i == arguments.size()) {
                cout << "Error: No cache file specified!" << endl;
                return -1;
            }
            cacheFile = arguments.at(i);
        } else if (arguments.at(i).toLower() == QLatin1String("-timeout")) {
            if (++i == arguments.size()) {
                cout << "Error: No timeout value specified!" << endl;
                return -1;
            }
            timeout = QString(arguments.at(i)).toInt();
        } else {
            launchArguments.append(arguments.at(i));
        }
    }

    // check for .pro file
    if (proFile.isEmpty()) {
        proFile = QDir::current().dirName() + QLatin1String(".pro");
        if (!QFileInfo(proFile).exists()) {
            cout << "Error: Could not find project file in current directory." << endl;
            return -1;
        }
        debugOutput(QString::fromLatin1("Using Project File:").append(proFile),1);
    }

    // read target and deployment rules
    int qmakeArgc = 1;
    char* qmakeArgv[] = { "qmake.exe" };
    Option::qmake_mode = Option::QMAKE_GENERATE_NOTHING;
    Option::output_dir = qmake_getpwd();
    if (!cacheFile.isEmpty())
        Option::mkfile::cachefile = cacheFile;
    int ret = Option::init(qmakeArgc, qmakeArgv);
    if(ret != Option::QMAKE_CMDLINE_SUCCESS) {
        cout << "Error: could not parse " << qPrintable(proFile) << endl;
        return -1;
    }

    QMakeProperty prop;
    QMakeProject project(&prop);

    project.read(proFile);
    if (project.values("TEMPLATE").join(" ").toLower() != QString("app")) {
        cout << "Error: Can only test executables!" << endl;
        return -1;
    }
    // Check wether the project is still in debug/release mode after reading
    // If .pro specifies to be one mode only, we need to accept this
    if (project.isActiveConfig("debug"))
        TestConfiguration::testDebug = true;
    else
        TestConfiguration::testDebug = false;

    QString destDir = project.values("DESTDIR").join(" ");
    if (!destDir.isEmpty()) {
        if (QDir::isRelativePath(destDir)) {
            QFileInfo fi(proFile);
            if (destDir == QLatin1String("."))
                destDir = fi.absolutePath() + "/" + destDir + "/" + (TestConfiguration::testDebug ? "debug" : "release");
            else
                destDir = fi.absolutePath() + QDir::separator() + destDir;
        }
    } else {
        QFileInfo fi(proFile);
        destDir = fi.absolutePath();
        destDir += QDir::separator() + QLatin1String(TestConfiguration::testDebug ? "debug" : "release");
    }

    DeploymentList qtDeploymentList;
    DeploymentList projectDeploymentList;

    TestConfiguration::localExecutable = Option::fixPathToLocalOS(destDir + QDir::separator() + project.values("TARGET").join(" ") + QLatin1String(".exe"));
    TestConfiguration::remoteTestPath = QLatin1String("\\Program Files\\") + Option::fixPathToLocalOS(project.values("TARGET").join(QLatin1String(" ")));
    if (!arguments.contains(QLatin1String("-libpath"), Qt::CaseInsensitive))
        TestConfiguration::remoteLibraryPath = TestConfiguration::remoteTestPath;

    QString targetExecutable = Option::fixPathToLocalOS(project.values("TARGET").join(QLatin1String(" ")));
    int last = targetExecutable.lastIndexOf(QLatin1Char('\\'));
    targetExecutable = targetExecutable.mid( last == -1 ? 0 : last+1 );
    TestConfiguration::remoteExecutable = TestConfiguration::remoteTestPath + QDir::separator() + targetExecutable + QLatin1String(".exe");
    projectDeploymentList.append(CopyItem(TestConfiguration::localExecutable , TestConfiguration::remoteExecutable));

    // deploy
#ifdef QT_CETEST_NO_ACTIVESYNC
    CeTcpSyncConnection connection;
#else
    ActiveSyncConnection connection;
#endif
    if (!connection.connect()) {
        cout << "Error: Could not connect to device!" << endl;
        return -1;
    }
    DeploymentHandler deployment;
    deployment.setConnection(&connection);

    deployment.initQtDeploy(&project, qtDeploymentList, TestConfiguration::remoteLibraryPath);
    deployment.initProjectDeploy(&project , projectDeploymentList, TestConfiguration::remoteTestPath);

    // add qt.conf
    if (TestConfiguration::localQtConf != QLatin1String("no")) {
        QString qtConfOrigin = QFileInfo(TestConfiguration::localQtConf).absoluteFilePath();
        QString qtConfTarget = Option::fixPathToLocalOS(TestConfiguration::remoteTestPath + QDir::separator() + QLatin1String("qt.conf"));
        projectDeploymentList.append(CopyItem(qtConfOrigin, qtConfTarget));
    }

    if (!deployment.deviceDeploy(qtDeploymentList) || !deployment.deviceDeploy(projectDeploymentList)) {
        cout << "Error: Could not copy file(s) to device" << endl;
        return -1;
    }

    // launch
    launchArguments.append("-o");
    launchArguments.append(TestConfiguration::remoteResultFile);

    cout << endl << "Remote Launch:" << qPrintable(TestConfiguration::remoteExecutable) << " " << qPrintable(launchArguments.join(" ")) << endl;
    if (!connection.execute(TestConfiguration::remoteExecutable, launchArguments.join(" "), timeout)) {
        cout << "Error: Could not execute target file" << endl;
        return -1;
    }


    // copy result file
    // show results
    if (resultFile.isEmpty()) {
        QString tempResultFile = Option::fixPathToLocalOS(QDir::tempPath() + "/qt_ce_temp_result_file.txt");
        if (connection.copyFileFromDevice(TestConfiguration::remoteResultFile, tempResultFile)) {
            QFile file(tempResultFile);
            QByteArray arr;
            if (file.open(QIODevice::ReadOnly)) {
                arr = file.readAll();
                cout << arr.constData() << endl;
            }
            file.close();
            file.remove();
        }
    } else {
        connection.copyFileFromDevice(TestConfiguration::remoteResultFile, resultFile);
    }

    // delete
    connection.deleteFile(TestConfiguration::remoteResultFile);
    if (cleanupQt)
        deployment.cleanup(qtDeploymentList);
    if (cleanupProject)
        deployment.cleanup(projectDeploymentList);
    return 0;
}