1 /****************************************************************************
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
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
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
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"
52 QML_DECLARE_TYPE(QuickTestResult)
53 QML_DECLARE_TYPE(QuickTestEvent)
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
60 static QScriptEngine *getScriptEngine(QDeclarativeEngine *engine);
61 static void setAnimationSlowDownFactor(qreal factor);
62 static void enableDebugging();
65 static QScriptContext *qtest_find_frame(QScriptContext *ctx)
68 if (ctx->argumentCount() > 0)
69 frame = ctx->argument(0).toInt32();
70 ++frame; // Exclude the native function; start at its caller.
74 ctx = ctx->parentContext();
79 static QScriptValue qtest_caller_file
80 (QScriptContext *ctx, QScriptEngine *engine)
82 ctx = qtest_find_frame(ctx);
84 QScriptContextInfo info(ctx);
85 return engine->newVariant(info.fileName());
87 return engine->newVariant(QLatin1String(""));
90 static QScriptValue qtest_caller_line
91 (QScriptContext *ctx, QScriptEngine *engine)
93 ctx = qtest_find_frame(ctx);
95 QScriptContextInfo info(ctx);
96 return engine->newVariant(info.lineNumber());
98 return engine->newVariant(qint32(0));
101 class QTestQmlModule : public QDeclarativeExtensionPlugin
105 virtual void registerTypes(const char *uri)
107 Q_ASSERT(QLatin1String(uri) == QLatin1String("QtTest"));
108 qmlRegisterType<QuickTestResult>(uri,1,0,"TestResult");
109 qmlRegisterType<QuickTestEvent>(uri,1,0,"TestEvent");
111 void initializeEngine(QDeclarativeEngine *engine, const char *)
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"));
121 (QLatin1String("qtest_caller_file"),
122 eng->newFunction(qtest_caller_file));
124 (QLatin1String("qtest_caller_line"),
125 eng->newFunction(qtest_caller_line));
133 Q_EXPORT_PLUGIN2(qmltestplugin, QT_PREPEND_NAMESPACE(QTestQmlModule));