Integrate QtQuickTest into Qt
[profile/ivi/qtdeclarative.git] / src / imports / testlib / main.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtDeclarative/qdeclarativeextensionplugin.h>
43 #include <QtDeclarative/qdeclarative.h>
44 #include <QtScript/qscriptvalue.h>
45 #include <QtScript/qscriptcontext.h>
46 #include <QtScript/qscriptcontextinfo.h>
47 #include <QtScript/qscriptengine.h>
48 #include "QtQuickTest/private/quicktestresult_p.h"
49 #include "QtQuickTest/private/quicktestevent_p.h"
50 QT_BEGIN_NAMESPACE
51
52 QML_DECLARE_TYPE(QuickTestResult)
53 QML_DECLARE_TYPE(QuickTestEvent)
54
55 // Copied from qdeclarativedebughelper_p.h in Qt, to avoid a dependency
56 // on a private header from Qt.
57 class Q_DECLARATIVE_EXPORT QDeclarativeDebugHelper
58 {
59 public:
60     static QScriptEngine *getScriptEngine(QDeclarativeEngine *engine);
61     static void setAnimationSlowDownFactor(qreal factor);
62     static void enableDebugging();
63 };
64
65 static QScriptContext *qtest_find_frame(QScriptContext *ctx)
66 {
67     qint32 frame = 1;
68     if (ctx->argumentCount() > 0)
69         frame = ctx->argument(0).toInt32();
70     ++frame;    // Exclude the native function; start at its caller.
71     while (ctx) {
72         if (frame-- <= 0)
73             break;
74         ctx = ctx->parentContext();
75     }
76     return ctx;
77 }
78
79 static QScriptValue qtest_caller_file
80     (QScriptContext *ctx, QScriptEngine *engine)
81 {
82     ctx = qtest_find_frame(ctx);
83     if (ctx) {
84         QScriptContextInfo info(ctx);
85         return engine->newVariant(info.fileName());
86     }
87     return engine->newVariant(QLatin1String(""));
88 }
89
90 static QScriptValue qtest_caller_line
91     (QScriptContext *ctx, QScriptEngine *engine)
92 {
93     ctx = qtest_find_frame(ctx);
94     if (ctx) {
95         QScriptContextInfo info(ctx);
96         return engine->newVariant(info.lineNumber());
97     }
98     return engine->newVariant(qint32(0));
99 }
100
101 class QTestQmlModule : public QDeclarativeExtensionPlugin
102 {
103     Q_OBJECT
104 public:
105     virtual void registerTypes(const char *uri)
106     {
107         Q_ASSERT(QLatin1String(uri) == QLatin1String("QtTest"));
108         qmlRegisterType<QuickTestResult>(uri,1,0,"TestResult");
109         qmlRegisterType<QuickTestEvent>(uri,1,0,"TestEvent");
110     }
111     void initializeEngine(QDeclarativeEngine *engine, const char *)
112     {
113         // Install some helper functions in the global "Qt" object
114         // for walking the stack and finding a caller's location.
115         // Normally we would use an exception's backtrace, but JSC
116         // only provides the top-most frame in the backtrace.
117         QScriptEngine *eng = QDeclarativeDebugHelper::getScriptEngine(engine);
118         QScriptValue qtObject
119             = eng->globalObject().property(QLatin1String("Qt"));
120         qtObject.setProperty
121             (QLatin1String("qtest_caller_file"),
122              eng->newFunction(qtest_caller_file));
123         qtObject.setProperty
124             (QLatin1String("qtest_caller_line"),
125              eng->newFunction(qtest_caller_line));
126     }
127 };
128
129 QT_END_NAMESPACE
130
131 #include "main.moc"
132
133 Q_EXPORT_PLUGIN2(qmltestplugin, QT_PREPEND_NAMESPACE(QTestQmlModule));