Merge branch 'master' into refactor
[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 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtDeclarative/qdeclarativeextensionplugin.h>
43 #include <QtDeclarative/qdeclarative.h>
44 #include <QtDeclarative/qjsvalue.h>
45 #include <QtDeclarative/qjsengine.h>
46 #include "QtQuickTest/private/quicktestresult_p.h"
47 #include "QtQuickTest/private/quicktestevent_p.h"
48 #include "private/qtestoptions_p.h"
49 #include "QtDeclarative/qsgitem.h"
50 #include <QtDeclarative/private/qdeclarativeengine_p.h>
51 QT_BEGIN_NAMESPACE
52
53 QML_DECLARE_TYPE(QuickTestResult)
54 QML_DECLARE_TYPE(QuickTestEvent)
55
56 #include <QtDebug>
57
58 class QuickTestUtil : public QObject
59 {
60     Q_OBJECT
61     Q_PROPERTY(bool printAvailableFunctions READ printAvailableFunctions)
62     Q_PROPERTY(bool wrapper READ wrapper)
63 public:
64     QuickTestUtil(QObject *parent = 0)
65         :QObject(parent)
66     {}
67
68     ~QuickTestUtil()
69     {}
70     bool printAvailableFunctions() const
71     {
72         return QTest::printAvailableFunctions;
73     }
74     bool wrapper() const
75     {
76         return true;
77     }
78
79 public Q_SLOTS:
80
81     QDeclarativeV8Handle typeName(const QVariant& v) const
82     {
83         QString name(v.typeName());
84         //qDebug() << "type:" << name  << " string value:" << v.toString() << " value:" << v;
85         if (v.canConvert<QObject*>()) {
86             QDeclarativeType *type = 0;
87             const QMetaObject *mo = v.value<QObject*>()->metaObject();
88             while (!type && mo) {
89                 type = QDeclarativeMetaType::qmlType(mo);
90                 mo = mo->superClass();
91             }
92             if (type) {
93                 name = type->qmlTypeName();
94             }
95         }
96
97         return QDeclarativeV8Handle::fromHandle(v8::String::New(name.toUtf8()));
98     }
99
100     bool compare(const QVariant& act, const QVariant& exp) const {
101         return act == exp;
102     }
103 //    QDeclarativeV8Handle toString(const QVariant& v) const
104 //    {
105 //        QString name(v.typeName());
106
107 //        if (v.canConvert<QObject*>()) {
108 //            QDeclarativeType *type = 0;
109 //            const QMetaObject *mo = v.value<QObject*>()->metaObject();
110 //            while (!type && mo) {
111 //                type = QDeclarativeMetaType::qmlType(mo);
112 //                mo = mo->superClass();
113 //            }
114 //            if (type) {
115 //                name = type->qmlTypeName();
116 //            }
117 //        }
118
119 //        return QDeclarativeV8Handle::fromHandle(v8::String::New(name.toUtf8()));
120 //    }
121
122     QDeclarativeV8Handle callerFile(int frameIndex = 0) const
123     {
124         v8::HandleScope scope;
125
126         v8::Local<v8::StackTrace> stacks = v8::StackTrace::CurrentStackTrace(10, v8::StackTrace::kDetailed);
127         int count = stacks->GetFrameCount();
128         if (count >= frameIndex + 2) {
129             v8::Local<v8::StackFrame> frame = stacks->GetFrame(frameIndex + 2);
130             return QDeclarativeV8Handle::fromHandle(frame->GetScriptNameOrSourceURL());
131         }
132         return QDeclarativeV8Handle();
133     }
134     int callerLine(int frameIndex = 0) const
135     {
136         v8::HandleScope scope;
137         v8::Local<v8::StackTrace> stacks = v8::StackTrace::CurrentStackTrace(10, v8::StackTrace::kDetailed);
138         int count = stacks->GetFrameCount();
139         if (count >= frameIndex + 2) {
140             v8::Local<v8::StackFrame> frame = stacks->GetFrame(frameIndex + 2);
141             return frame->GetLineNumber();
142         }
143         return -1;
144     }
145 };
146 QML_DECLARE_TYPE(QuickTestUtil)
147
148 class QTestQmlModule : public QDeclarativeExtensionPlugin
149 {
150     Q_OBJECT
151 public:
152     virtual void registerTypes(const char *uri)
153     {
154         Q_ASSERT(QLatin1String(uri) == QLatin1String("QtTest"));
155         qmlRegisterType<QuickTestResult>(uri,1,0,"TestResult");
156         qmlRegisterType<QuickTestEvent>(uri,1,0,"TestEvent");
157         qmlRegisterType<QuickTestUtil>(uri,1,0,"TestUtil");
158     }
159
160     void initializeEngine(QDeclarativeEngine *engine, const char *)
161     {
162     }
163 };
164
165 QT_END_NAMESPACE
166
167 #include "main.moc"
168
169 Q_EXPORT_PLUGIN2(qmltestplugin, QT_PREPEND_NAMESPACE(QTestQmlModule));