Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qdeclarativescriptdebugging / tst_qdeclarativescriptdebugging.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 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 #include <qtest.h>
42 #include <QtDeclarative/qdeclarativecomponent.h>
43 #include <QtDeclarative/qdeclarativeengine.h>
44 #include <QtDeclarative/qdeclarativeitem.h>
45 #include <QtDeclarative/qdeclarativecontext.h>
46 #include <QtCore/QDir>
47 #include <QtScript/QScriptEngineAgent>
48 #include <private/qdeclarativeengine_p.h>
49
50 class MyTestObject : public QObject {
51     Q_OBJECT
52     Q_PROPERTY(QString someProperty READ someProperty WRITE setSomeProperty NOTIFY somePropertyChanged)
53
54 public:
55     QString someProperty() { return _someProperty; }
56     void setSomeProperty(const QString &p) { _someProperty = p; }
57     QString _someProperty;
58
59     void emitSignal(const QString &value) { emit signaled(value); }
60
61 signals:
62     void signaled(const QString &value);
63     void somePropertyChanged();
64 };
65
66
67 class BtAgent : public QScriptEngineAgent {
68 public:
69     BtAgent(QScriptEngine *engine) : QScriptEngineAgent(engine)
70     { count = 0; engine->setAgent(this); }
71
72     QStringList bt;
73     int count;
74     int breakpoint;
75     void positionChange(qint64 , int lineNumber, int )
76     {
77         if(lineNumber == breakpoint) {
78             count++;
79             bt = engine()->currentContext()->backtrace();
80         }
81     }
82 };
83
84
85
86 /*
87 This test covers evaluation of ECMAScript expressions and bindings from within
88 QML.  This does not include static QML language issues.
89
90 Static QML language issues are covered in qmllanguage
91 */
92 inline QUrl TEST_FILE(const QString &filename)
93 {
94     QFileInfo fileInfo(__FILE__);
95     return QUrl::fromLocalFile(fileInfo.absoluteDir().filePath("data/" + filename));
96 }
97
98 inline QUrl TEST_FILE(const char *filename)
99 {
100     return TEST_FILE(QLatin1String(filename));
101 }
102
103 class tst_qdeclarativescriptdebugging : public QObject
104 {
105     Q_OBJECT
106 public:
107     tst_qdeclarativescriptdebugging() {}
108
109 private slots:
110     void initTestCase();
111     void backtrace1();
112 };
113
114 void tst_qdeclarativescriptdebugging::initTestCase()
115 {
116         qmlRegisterType<MyTestObject>("Qt.test", 1,0, "MyTestObject");
117 }
118
119 void tst_qdeclarativescriptdebugging::backtrace1()
120 {
121     {
122     QDeclarativeEngine engine;
123     QUrl file = TEST_FILE("backtrace1.qml");
124     QDeclarativeComponent component(&engine, file);
125     QDeclarativeItem *item = qobject_cast<QDeclarativeItem *>(component.create());
126     QVERIFY(item);
127     MyTestObject *obj = item->findChild<MyTestObject *>();
128     QVERIFY(obj);
129     QCOMPARE(obj->someProperty(), QString("Hello Default"));
130
131     QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(&engine);
132     BtAgent agent(&ep->scriptEngine);
133     agent.breakpoint = 16;
134
135     obj->emitSignal("blah");
136     QCOMPARE(obj->someProperty(), QString("Hello blahb"));
137     QCOMPARE(agent.count, 1);
138
139     QStringList expected;
140     expected << "append(a = 'Hello', b = 'blahb') at @PREFIX@/backtrace1.qml:16"
141              << "$someProperty() at @PREFIX@/backtrace1.qml:21"
142              << "function2InScript(a = 'blahb') at @PREFIX@/backtrace1.js:4"
143              << "functionInScript(a = 'blah', b = 'b') at @PREFIX@/backtrace1.js:10"
144              << "onSignaled() at @PREFIX@/backtrace1.qml:24"
145              << "<global>() at -1";
146     expected.replaceInStrings("@PREFIX@", file.toString().section('/', 0, -2));
147     QCOMPARE(agent.bt, expected);
148     }
149 }
150
151
152 QTEST_MAIN(tst_qdeclarativescriptdebugging)
153
154 #include "tst_qdeclarativescriptdebugging.moc"