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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the test suite 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://qt.nokia.com/contact.
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtTest/QtTest>
#include <qdebug.h>
#include <QtScript/qscriptengine.h>
#include <QtScript/qscriptable.h>
//TESTED_CLASS=
//TESTED_FILES=
class MyScriptable : public QObject, public QScriptable
{
Q_OBJECT
Q_PROPERTY(int baz READ baz WRITE setBaz)
Q_PROPERTY(QObject* zab READ zab WRITE setZab)
Q_PROPERTY(int 0 READ baz)
Q_PROPERTY(QObject* 1 READ zab)
Q_PROPERTY(int oof WRITE setOof)
public:
MyScriptable(QObject *parent = 0)
: QObject(parent), m_lastEngine(0)
{ }
~MyScriptable() { }
QScriptEngine *lastEngine() const;
void setOof(int)
{ m_oofThisObject = context()->thisObject(); }
QScriptValue oofThisObject() const
{ return m_oofThisObject; }
void emitSig(int value)
{ emit sig(value); }
public slots:
void foo();
void setX(int x);
void setX(const QString &x);
void setX2(int x);
bool isBar();
int baz();
void setBaz(int x);
void evalIsBar();
bool useInAnotherEngine();
void setOtherEngine();
QObject *zab();
QObject *setZab(QObject *);
QScriptValue getArguments();
int getArgumentCount();
signals:
void sig(int);
private:
QScriptEngine *m_lastEngine;
QScriptEngine *m_otherEngine;
QScriptValue m_oofThisObject;
};
QScriptEngine *MyScriptable::lastEngine() const
{
return m_lastEngine;
}
int MyScriptable::baz()
{
m_lastEngine = engine();
return 123;
}
void MyScriptable::setBaz(int)
{
m_lastEngine = engine();
}
QObject *MyScriptable::zab()
{
return thisObject().toQObject();
}
QObject *MyScriptable::setZab(QObject *)
{
return thisObject().toQObject();
}
QScriptValue MyScriptable::getArguments()
{
return context()->argumentsObject();
}
int MyScriptable::getArgumentCount()
{
return context()->argumentCount();
}
void MyScriptable::foo()
{
m_lastEngine = engine();
QVERIFY(engine() != 0);
context()->throwError("MyScriptable.foo");
}
void MyScriptable::evalIsBar()
{
engine()->evaluate("this.isBar()");
m_lastEngine = engine();
}
bool MyScriptable::useInAnotherEngine()
{
QScriptEngine eng;
eng.globalObject().setProperty("foo", eng.newQObject(this));
eng.evaluate("foo.baz()");
m_lastEngine = engine();
return (m_otherEngine == &eng);
}
void MyScriptable::setOtherEngine()
{
m_otherEngine = engine();
}
void MyScriptable::setX(int x)
{
m_lastEngine = engine();
Q_ASSERT(engine());
thisObject().setProperty("x", QScriptValue(engine(), x));
}
void MyScriptable::setX(const QString &x)
{
m_lastEngine = engine();
Q_ASSERT(engine());
thisObject().setProperty("x", QScriptValue(engine(), x));
}
void MyScriptable::setX2(int)
{
m_lastEngine = engine();
thisObject().setProperty("x", argument(0));
}
bool MyScriptable::isBar()
{
m_lastEngine = engine();
QString str = thisObject().toString();
return str.contains(QLatin1Char('@'));
}
class tst_QScriptable : public QObject
{
Q_OBJECT
public:
tst_QScriptable();
virtual ~tst_QScriptable();
private slots:
void initTestCase();
void cleanupTestCase();
void engine();
void thisObject();
void arguments();
void throwError();
private:
QScriptEngine m_engine;
MyScriptable m_scriptable;
};
tst_QScriptable::tst_QScriptable()
{
}
tst_QScriptable::~tst_QScriptable()
{
}
void tst_QScriptable::initTestCase()
{
QScriptValue obj = m_engine.newQObject(&m_scriptable);
m_engine.globalObject().setProperty("scriptable", obj);
}
void tst_QScriptable::cleanupTestCase()
{
}
void tst_QScriptable::engine()
{
QCOMPARE(m_scriptable.engine(), (QScriptEngine*)0);
QCOMPARE(m_scriptable.context(), (QScriptContext*)0);
QCOMPARE(m_scriptable.lastEngine(), (QScriptEngine *)0);
// reading property
{
QScriptValue ret = m_engine.evaluate("scriptable.baz");
QCOMPARE(ret.strictlyEquals(QScriptValue(&m_engine, 123)), true);
}
QCOMPARE(m_scriptable.lastEngine(), &m_engine);
{
QScriptValue ret = m_engine.evaluate("scriptable[0]");
QCOMPARE(ret.strictlyEquals(QScriptValue(&m_engine, 123)), true);
}
QCOMPARE(m_scriptable.lastEngine(), &m_engine);
// when reading from C++, engine() should be 0
(void)m_scriptable.property("baz");
QCOMPARE(m_scriptable.lastEngine(), (QScriptEngine *)0);
// writing property
m_engine.evaluate("scriptable.baz = 123");
QCOMPARE(m_scriptable.lastEngine(), &m_engine);
(void)m_scriptable.setProperty("baz", 123);
QCOMPARE(m_scriptable.lastEngine(), (QScriptEngine *)0);
// calling slot
m_engine.evaluate("scriptable.setX(123)");
QCOMPARE(m_scriptable.lastEngine(), &m_engine);
QCOMPARE(m_engine.evaluate("scriptable.x")
.strictlyEquals(QScriptValue(&m_engine, 123)), true);
(void)m_scriptable.setProperty("baz", 123);
QCOMPARE(m_scriptable.lastEngine(), (QScriptEngine *)0);
// calling overloaded slot
m_engine.evaluate("scriptable.setX('123')");
QCOMPARE(m_scriptable.lastEngine(), &m_engine);
QCOMPARE(m_engine.evaluate("scriptable.x")
.strictlyEquals(QScriptValue(&m_engine, QLatin1String("123"))), true);
// calling a slot from another slot
m_engine.evaluate("scriptable.evalIsBar()");
QCOMPARE(m_scriptable.lastEngine(), &m_engine);
// calling a slot that registers m_scriptable in a different engine
// and calls evaluate()
{
QScriptValue ret = m_engine.evaluate("scriptable.useInAnotherEngine()");
QCOMPARE(m_scriptable.lastEngine(), &m_engine);
}
}
void tst_QScriptable::thisObject()
{
m_engine.evaluate("o = { }");
{
QScriptValue ret = m_engine.evaluate("o.__proto__ = scriptable;"
"o.setX(123);"
"o.__proto__ = Object.prototype;"
"o.x");
QCOMPARE(ret.strictlyEquals(QScriptValue(&m_engine, 123)), true);
}
{
QScriptValue ret = m_engine.evaluate("o.__proto__ = scriptable;"
"o.setX2(456);"
"o.__proto__ = Object.prototype;"
"o.x");
QCOMPARE(ret.strictlyEquals(QScriptValue(&m_engine, 456)), true);
}
m_engine.evaluate("o.__proto__ = scriptable");
{
QScriptValue ret = m_engine.evaluate("o.isBar()");
QCOMPARE(ret.strictlyEquals(QScriptValue(&m_engine, false)), true);
}
{
QScriptValue ret = m_engine.evaluate("o.toString = function() { return 'foo@bar'; }; o.isBar()");
QCOMPARE(ret.strictlyEquals(QScriptValue(&m_engine, true)), true);
}
// property getter
{
QScriptValue ret = m_engine.evaluate("scriptable.zab");
QCOMPARE(ret.isQObject(), true);
QCOMPARE(ret.toQObject(), (QObject *)&m_scriptable);
}
{
QScriptValue ret = m_engine.evaluate("scriptable[1]");
QCOMPARE(ret.isQObject(), true);
QCOMPARE(ret.toQObject(), (QObject *)&m_scriptable);
}
{
QScriptValue ret = m_engine.evaluate("o.zab");
QCOMPARE(ret.toQObject(), (QObject *)0);
}
// property setter
{
QScriptValue ret = m_engine.evaluate("scriptable.setZab(null)");
QCOMPARE(ret.isQObject(), true);
QCOMPARE(ret.toQObject(), (QObject *)&m_scriptable);
}
{
QVERIFY(!m_scriptable.oofThisObject().isValid());
m_engine.evaluate("o.oof = 123");
QEXPECT_FAIL("", "Setter doesn't get called when it's in the prototype", Continue);
QVERIFY(m_scriptable.oofThisObject().strictlyEquals(m_engine.evaluate("o")));
}
{
m_engine.evaluate("scriptable.oof = 123");
QVERIFY(m_scriptable.oofThisObject().strictlyEquals(m_engine.evaluate("scriptable")));
}
// target of signal
{
{
QScriptValue ret = m_engine.evaluate("scriptable.sig.connect(o, scriptable.setX)");
QVERIFY(ret.isUndefined());
}
QVERIFY(m_engine.evaluate("o.x").strictlyEquals(QScriptValue(&m_engine, 456)));
m_scriptable.emitSig(654321);
QVERIFY(m_engine.evaluate("o.x").strictlyEquals(QScriptValue(&m_engine, 654321)));
{
QScriptValue ret = m_engine.evaluate("scriptable.sig.disconnect(o, scriptable.setX)");
QVERIFY(ret.isUndefined());
}
}
m_engine.evaluate("delete o");
}
void tst_QScriptable::arguments()
{
// even though the C++ slot accepts zero arguments, it should
// still be invoked; the arguments should be accessible through
// the QScriptable API
QScriptValue args = m_engine.evaluate("scriptable.getArguments(10, 20, 30, 'hi')");
QVERIFY(args.property("length").strictlyEquals(QScriptValue(&m_engine, 4)));
QVERIFY(args.property("0").strictlyEquals(QScriptValue(&m_engine, 10)));
QVERIFY(args.property("1").strictlyEquals(QScriptValue(&m_engine, 20)));
QVERIFY(args.property("2").strictlyEquals(QScriptValue(&m_engine, 30)));
QVERIFY(args.property("3").strictlyEquals(QScriptValue(&m_engine, "hi")));
QScriptValue argc = m_engine.evaluate("scriptable.getArgumentCount(1, 2, 3)");
QVERIFY(argc.isNumber());
QCOMPARE(argc.toInt32(), 3);
QCOMPARE(m_scriptable.argumentCount(), -1);
QVERIFY(!m_scriptable.argument(-1).isValid());
QVERIFY(!m_scriptable.argument(0).isValid());
}
void tst_QScriptable::throwError()
{
QScriptValue ret = m_engine.evaluate("scriptable.foo()");
QCOMPARE(ret.isError(), true);
QCOMPARE(ret.toString(), QString("Error: MyScriptable.foo"));
}
QTEST_MAIN(tst_QScriptable)
#include "tst_qscriptable.moc"
|