author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Thu, 08 Apr 2010 14:19:33 +0300 | |
branch | RCL_3 |
changeset 7 | 3f74d0d4af4c |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 |
||
43 |
#include <QtTest/QtTest> |
|
44 |
||
45 |
#include <QtScript/qscriptengine.h> |
|
46 |
#include <QtScript/qscriptclass.h> |
|
47 |
#include <QtScript/qscriptclasspropertyiterator.h> |
|
48 |
#include <QtScript/qscriptstring.h> |
|
49 |
#include <QtScript/qscriptvalueiterator.h> |
|
50 |
||
51 |
Q_DECLARE_METATYPE(QScriptContext*) |
|
52 |
Q_DECLARE_METATYPE(QScriptValueList) |
|
53 |
Q_DECLARE_METATYPE(QScriptValue) |
|
54 |
||
55 |
//TESTED_CLASS= |
|
56 |
//TESTED_FILES=script/qscriptclass.h script/qscriptclass.cpp script/qscriptclasspropertyiterator.h script/qscriptclasspropertyiterator.cpp |
|
57 |
||
58 |
class tst_QScriptClass : public QObject |
|
59 |
{ |
|
60 |
Q_OBJECT |
|
61 |
||
62 |
public: |
|
63 |
tst_QScriptClass(); |
|
64 |
virtual ~tst_QScriptClass(); |
|
65 |
||
66 |
private slots: |
|
67 |
void newInstance(); |
|
68 |
void getAndSetProperty(); |
|
69 |
void enumerate(); |
|
70 |
void extension(); |
|
71 |
}; |
|
72 |
||
73 |
tst_QScriptClass::tst_QScriptClass() |
|
74 |
{ |
|
75 |
} |
|
76 |
||
77 |
tst_QScriptClass::~tst_QScriptClass() |
|
78 |
{ |
|
79 |
} |
|
80 |
||
81 |
||
82 |
||
83 |
class TestClass : public QScriptClass |
|
84 |
{ |
|
85 |
public: |
|
86 |
struct CustomProperty { |
|
87 |
QueryFlags qflags; |
|
88 |
uint id; |
|
89 |
QScriptValue::PropertyFlags pflags; |
|
90 |
QScriptValue value; |
|
91 |
||
92 |
CustomProperty(QueryFlags qf, uint i, QScriptValue::PropertyFlags pf, |
|
93 |
const QScriptValue &val) |
|
94 |
: qflags(qf), id(i), pflags(pf), value(val) { } |
|
95 |
}; |
|
96 |
||
97 |
enum CallableMode { |
|
98 |
NotCallable, |
|
99 |
CallableReturnsSum, |
|
100 |
CallableReturnsArgument, |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
101 |
CallableReturnsInvalidVariant, |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
102 |
CallableReturnsGlobalObject, |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
103 |
CallableReturnsThisObject, |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
104 |
CallableReturnsCallee, |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
105 |
CallableReturnsArgumentsObject, |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
106 |
CallableInitializesThisObject |
0 | 107 |
}; |
108 |
||
109 |
TestClass(QScriptEngine *engine); |
|
110 |
~TestClass(); |
|
111 |
||
112 |
void addCustomProperty(const QScriptString &name, QueryFlags qflags, |
|
113 |
uint id, QScriptValue::PropertyFlags pflags, |
|
114 |
const QScriptValue &value); |
|
115 |
void removeCustomProperty(const QScriptString &name); |
|
116 |
||
117 |
QueryFlags queryProperty(const QScriptValue &object, |
|
118 |
const QScriptString &name, |
|
119 |
QueryFlags flags, uint *id); |
|
120 |
||
121 |
QScriptValue property(const QScriptValue &object, |
|
122 |
const QScriptString &name, uint id); |
|
123 |
||
124 |
void setProperty(QScriptValue &object, const QScriptString &name, |
|
125 |
uint id, const QScriptValue &value); |
|
126 |
||
127 |
QScriptValue::PropertyFlags propertyFlags( |
|
128 |
const QScriptValue &object, const QScriptString &name, uint id); |
|
129 |
||
130 |
QScriptClassPropertyIterator *newIterator(const QScriptValue &object); |
|
131 |
||
132 |
QScriptValue prototype() const; |
|
133 |
||
134 |
QString name() const; |
|
135 |
||
136 |
bool supportsExtension(Extension extension) const; |
|
137 |
QVariant extension(Extension extension, |
|
138 |
const QVariant &argument = QVariant()); |
|
139 |
||
140 |
QScriptValue lastQueryPropertyObject() const; |
|
141 |
QScriptString lastQueryPropertyName() const; |
|
142 |
QueryFlags lastQueryPropertyFlags() const; |
|
143 |
||
144 |
QScriptValue lastPropertyObject() const; |
|
145 |
QScriptString lastPropertyName() const; |
|
146 |
uint lastPropertyId() const; |
|
147 |
||
148 |
QScriptValue lastSetPropertyObject() const; |
|
149 |
QScriptString lastSetPropertyName() const; |
|
150 |
uint lastSetPropertyId() const; |
|
151 |
QScriptValue lastSetPropertyValue() const; |
|
152 |
||
153 |
QScriptValue lastPropertyFlagsObject() const; |
|
154 |
QScriptString lastPropertyFlagsName() const; |
|
155 |
uint lastPropertyFlagsId() const; |
|
156 |
||
157 |
QScriptClass::Extension lastExtensionType() const; |
|
158 |
QVariant lastExtensionArgument() const; |
|
159 |
||
160 |
void clearReceivedArgs(); |
|
161 |
||
162 |
void setIterationEnabled(bool enable); |
|
163 |
bool isIterationEnabled() const; |
|
164 |
||
165 |
void setCallableMode(CallableMode mode); |
|
166 |
CallableMode callableMode() const; |
|
167 |
||
168 |
void setHasInstance(bool hasInstance); |
|
169 |
bool hasInstance() const; |
|
170 |
||
171 |
private: |
|
172 |
CustomProperty *findCustomProperty(const QScriptString &name); |
|
173 |
||
174 |
QHash<QScriptString, CustomProperty*> customProperties; |
|
175 |
||
176 |
QScriptValue m_lastQueryPropertyObject; |
|
177 |
QScriptString m_lastQueryPropertyName; |
|
178 |
QScriptClass::QueryFlags m_lastQueryPropertyFlags; |
|
179 |
||
180 |
QScriptValue m_lastPropertyObject; |
|
181 |
QScriptString m_lastPropertyName; |
|
182 |
uint m_lastPropertyId; |
|
183 |
||
184 |
QScriptValue m_lastSetPropertyObject; |
|
185 |
QScriptString m_lastSetPropertyName; |
|
186 |
uint m_lastSetPropertyId; |
|
187 |
QScriptValue m_lastSetPropertyValue; |
|
188 |
||
189 |
QScriptValue m_lastPropertyFlagsObject; |
|
190 |
QScriptString m_lastPropertyFlagsName; |
|
191 |
uint m_lastPropertyFlagsId; |
|
192 |
||
193 |
QScriptClass::Extension m_lastExtensionType; |
|
194 |
QVariant m_lastExtensionArgument; |
|
195 |
||
196 |
QScriptValue m_prototype; |
|
197 |
bool m_iterationEnabled; |
|
198 |
CallableMode m_callableMode; |
|
199 |
bool m_hasInstance; |
|
200 |
}; |
|
201 |
||
202 |
class TestClassPropertyIterator : public QScriptClassPropertyIterator |
|
203 |
{ |
|
204 |
public: |
|
205 |
TestClassPropertyIterator(const QHash<QScriptString, TestClass::CustomProperty*> &props, |
|
206 |
const QScriptValue &object); |
|
207 |
~TestClassPropertyIterator(); |
|
208 |
||
209 |
bool hasNext() const; |
|
210 |
void next(); |
|
211 |
||
212 |
bool hasPrevious() const; |
|
213 |
void previous(); |
|
214 |
||
215 |
void toFront(); |
|
216 |
void toBack(); |
|
217 |
||
218 |
QScriptString name() const; |
|
219 |
uint id() const; |
|
220 |
QScriptValue::PropertyFlags flags() const; |
|
221 |
||
222 |
private: |
|
223 |
int m_index; |
|
224 |
int m_last; |
|
225 |
QHash<QScriptString, TestClass::CustomProperty*> m_props; |
|
226 |
}; |
|
227 |
||
228 |
||
229 |
||
230 |
TestClass::TestClass(QScriptEngine *engine) |
|
231 |
: QScriptClass(engine), m_iterationEnabled(true), |
|
232 |
m_callableMode(NotCallable), m_hasInstance(false) |
|
233 |
{ |
|
234 |
m_prototype = engine->newObject(); |
|
235 |
clearReceivedArgs(); |
|
236 |
} |
|
237 |
||
238 |
TestClass::~TestClass() |
|
239 |
{ |
|
240 |
qDeleteAll(customProperties); |
|
241 |
} |
|
242 |
||
243 |
TestClass::CustomProperty* TestClass::findCustomProperty(const QScriptString &name) |
|
244 |
{ |
|
245 |
QHash<QScriptString, CustomProperty*>::const_iterator it; |
|
246 |
it = customProperties.constFind(name); |
|
247 |
if (it == customProperties.constEnd()) |
|
248 |
return 0; |
|
249 |
return it.value(); |
|
250 |
||
251 |
} |
|
252 |
||
253 |
void TestClass::addCustomProperty(const QScriptString &name, QueryFlags qflags, |
|
254 |
uint id, QScriptValue::PropertyFlags pflags, |
|
255 |
const QScriptValue &value) |
|
256 |
{ |
|
257 |
customProperties.insert(name, new CustomProperty(qflags, id, pflags, value)); |
|
258 |
} |
|
259 |
||
260 |
void TestClass::removeCustomProperty(const QScriptString &name) |
|
261 |
{ |
|
262 |
CustomProperty *prop = customProperties.take(name); |
|
263 |
if (prop) |
|
264 |
delete prop; |
|
265 |
} |
|
266 |
||
267 |
QScriptClass::QueryFlags TestClass::queryProperty(const QScriptValue &object, |
|
268 |
const QScriptString &name, |
|
269 |
QueryFlags flags, uint *id) |
|
270 |
{ |
|
271 |
m_lastQueryPropertyObject = object; |
|
272 |
m_lastQueryPropertyName = name; |
|
273 |
m_lastQueryPropertyFlags = flags; |
|
274 |
CustomProperty *prop = findCustomProperty(name); |
|
275 |
if (!prop) |
|
276 |
return 0; |
|
277 |
*id = prop->id; |
|
278 |
return prop->qflags & flags; |
|
279 |
} |
|
280 |
||
281 |
QScriptValue TestClass::property(const QScriptValue &object, |
|
282 |
const QScriptString &name, uint id) |
|
283 |
{ |
|
284 |
m_lastPropertyObject = object; |
|
285 |
m_lastPropertyName = name; |
|
286 |
m_lastPropertyId = id; |
|
287 |
CustomProperty *prop = findCustomProperty(name); |
|
288 |
if (!prop) |
|
289 |
return QScriptValue(); |
|
290 |
return prop->value; |
|
291 |
} |
|
292 |
||
293 |
void TestClass::setProperty(QScriptValue &object, const QScriptString &name, |
|
294 |
uint id, const QScriptValue &value) |
|
295 |
{ |
|
296 |
m_lastSetPropertyObject = object; |
|
297 |
m_lastSetPropertyName = name; |
|
298 |
m_lastSetPropertyId = id; |
|
299 |
m_lastSetPropertyValue = value; |
|
300 |
CustomProperty *prop = findCustomProperty(name); |
|
301 |
if (!prop) |
|
302 |
return; |
|
303 |
prop->value = value; |
|
304 |
} |
|
305 |
||
306 |
QScriptValue::PropertyFlags TestClass::propertyFlags( |
|
307 |
const QScriptValue &object, const QScriptString &name, uint id) |
|
308 |
{ |
|
309 |
m_lastPropertyFlagsObject = object; |
|
310 |
m_lastPropertyFlagsName = name; |
|
311 |
m_lastPropertyFlagsId = id; |
|
312 |
CustomProperty *prop = findCustomProperty(name); |
|
313 |
if (!prop) |
|
314 |
return 0; |
|
315 |
return prop->pflags; |
|
316 |
} |
|
317 |
||
318 |
QScriptClassPropertyIterator *TestClass::newIterator(const QScriptValue &object) |
|
319 |
{ |
|
320 |
if (!m_iterationEnabled) |
|
321 |
return 0; |
|
322 |
return new TestClassPropertyIterator(customProperties, object); |
|
323 |
} |
|
324 |
||
325 |
QScriptValue TestClass::prototype() const |
|
326 |
{ |
|
327 |
return m_prototype; |
|
328 |
} |
|
329 |
||
330 |
QString TestClass::name() const |
|
331 |
{ |
|
332 |
return QLatin1String("TestClass"); |
|
333 |
} |
|
334 |
||
335 |
bool TestClass::supportsExtension(Extension extension) const |
|
336 |
{ |
|
337 |
if (extension == Callable) |
|
338 |
return (m_callableMode != NotCallable); |
|
339 |
if (extension == HasInstance) |
|
340 |
return m_hasInstance; |
|
341 |
return false; |
|
342 |
} |
|
343 |
||
344 |
QVariant TestClass::extension(Extension extension, |
|
345 |
const QVariant &argument) |
|
346 |
{ |
|
347 |
m_lastExtensionType = extension; |
|
348 |
m_lastExtensionArgument = argument; |
|
349 |
if (extension == Callable) { |
|
350 |
Q_ASSERT(m_callableMode != NotCallable); |
|
351 |
QScriptContext *ctx = qvariant_cast<QScriptContext*>(argument); |
|
352 |
if (m_callableMode == CallableReturnsSum) { |
|
353 |
qsreal sum = 0; |
|
354 |
for (int i = 0; i < ctx->argumentCount(); ++i) |
|
355 |
sum += ctx->argument(i).toNumber(); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
356 |
QScriptValueIterator it(ctx->callee()); |
0 | 357 |
while (it.hasNext()) { |
358 |
it.next(); |
|
359 |
sum += it.value().toNumber(); |
|
360 |
} |
|
361 |
return sum; |
|
362 |
} else if (m_callableMode == CallableReturnsArgument) { |
|
363 |
return qVariantFromValue(ctx->argument(0)); |
|
364 |
} else if (m_callableMode == CallableReturnsInvalidVariant) { |
|
365 |
return QVariant(); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
366 |
} else if (m_callableMode == CallableReturnsGlobalObject) { |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
367 |
return qVariantFromValue(engine()->globalObject()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
368 |
} else if (m_callableMode == CallableReturnsThisObject) { |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
369 |
return qVariantFromValue(ctx->thisObject()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
370 |
} else if (m_callableMode == CallableReturnsCallee) { |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
371 |
return qVariantFromValue(ctx->callee()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
372 |
} else if (m_callableMode == CallableReturnsArgumentsObject) { |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
373 |
return qVariantFromValue(ctx->argumentsObject()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
374 |
} else if (m_callableMode == CallableInitializesThisObject) { |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
375 |
engine()->newQObject(ctx->thisObject(), engine()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
376 |
return QVariant(); |
0 | 377 |
} |
378 |
} else if (extension == HasInstance) { |
|
379 |
Q_ASSERT(m_hasInstance); |
|
380 |
QScriptValueList args = qvariant_cast<QScriptValueList>(argument); |
|
381 |
QScriptValue obj = args.at(0); |
|
382 |
QScriptValue value = args.at(1); |
|
383 |
return value.property("foo").equals(obj.property("foo")); |
|
384 |
} |
|
385 |
return QVariant(); |
|
386 |
} |
|
387 |
||
388 |
QScriptValue TestClass::lastQueryPropertyObject() const |
|
389 |
{ |
|
390 |
return m_lastQueryPropertyObject; |
|
391 |
} |
|
392 |
||
393 |
QScriptString TestClass::lastQueryPropertyName() const |
|
394 |
{ |
|
395 |
return m_lastQueryPropertyName; |
|
396 |
} |
|
397 |
||
398 |
QScriptClass::QueryFlags TestClass::lastQueryPropertyFlags() const |
|
399 |
{ |
|
400 |
return m_lastQueryPropertyFlags; |
|
401 |
} |
|
402 |
||
403 |
QScriptValue TestClass::lastPropertyObject() const |
|
404 |
{ |
|
405 |
return m_lastPropertyObject; |
|
406 |
} |
|
407 |
||
408 |
QScriptString TestClass::lastPropertyName() const |
|
409 |
{ |
|
410 |
return m_lastPropertyName; |
|
411 |
} |
|
412 |
||
413 |
uint TestClass::lastPropertyId() const |
|
414 |
{ |
|
415 |
return m_lastPropertyId; |
|
416 |
} |
|
417 |
||
418 |
QScriptValue TestClass::lastSetPropertyObject() const |
|
419 |
{ |
|
420 |
return m_lastSetPropertyObject; |
|
421 |
} |
|
422 |
||
423 |
QScriptString TestClass::lastSetPropertyName() const |
|
424 |
{ |
|
425 |
return m_lastSetPropertyName; |
|
426 |
} |
|
427 |
||
428 |
uint TestClass::lastSetPropertyId() const |
|
429 |
{ |
|
430 |
return m_lastSetPropertyId; |
|
431 |
} |
|
432 |
||
433 |
QScriptValue TestClass::lastSetPropertyValue() const |
|
434 |
{ |
|
435 |
return m_lastSetPropertyValue; |
|
436 |
} |
|
437 |
||
438 |
QScriptValue TestClass::lastPropertyFlagsObject() const |
|
439 |
{ |
|
440 |
return m_lastPropertyFlagsObject; |
|
441 |
} |
|
442 |
||
443 |
QScriptString TestClass::lastPropertyFlagsName() const |
|
444 |
{ |
|
445 |
return m_lastPropertyFlagsName; |
|
446 |
} |
|
447 |
||
448 |
uint TestClass::lastPropertyFlagsId() const |
|
449 |
{ |
|
450 |
return m_lastPropertyFlagsId; |
|
451 |
} |
|
452 |
||
453 |
QScriptClass::Extension TestClass::lastExtensionType() const |
|
454 |
{ |
|
455 |
return m_lastExtensionType; |
|
456 |
} |
|
457 |
||
458 |
QVariant TestClass::lastExtensionArgument() const |
|
459 |
{ |
|
460 |
return m_lastExtensionArgument; |
|
461 |
} |
|
462 |
||
463 |
void TestClass::clearReceivedArgs() |
|
464 |
{ |
|
465 |
m_lastQueryPropertyObject = QScriptValue(); |
|
466 |
m_lastQueryPropertyName = QScriptString(); |
|
467 |
m_lastQueryPropertyFlags = 0; |
|
468 |
||
469 |
m_lastPropertyObject = QScriptValue(); |
|
470 |
m_lastPropertyName = QScriptString(); |
|
471 |
m_lastPropertyId = uint(-1); |
|
472 |
||
473 |
m_lastSetPropertyObject = QScriptValue(); |
|
474 |
m_lastSetPropertyName = QScriptString(); |
|
475 |
m_lastSetPropertyId = uint(-1); |
|
476 |
m_lastSetPropertyValue = QScriptValue(); |
|
477 |
||
478 |
m_lastPropertyFlagsObject = QScriptValue(); |
|
479 |
m_lastPropertyFlagsName = QScriptString(); |
|
480 |
m_lastPropertyFlagsId = uint(-1); |
|
481 |
||
482 |
m_lastExtensionType = static_cast<QScriptClass::Extension>(-1); |
|
483 |
m_lastExtensionArgument = QVariant(); |
|
484 |
} |
|
485 |
||
486 |
void TestClass::setIterationEnabled(bool enable) |
|
487 |
{ |
|
488 |
m_iterationEnabled = enable; |
|
489 |
} |
|
490 |
||
491 |
bool TestClass::isIterationEnabled() const |
|
492 |
{ |
|
493 |
return m_iterationEnabled; |
|
494 |
} |
|
495 |
||
496 |
void TestClass::setCallableMode(CallableMode mode) |
|
497 |
{ |
|
498 |
m_callableMode = mode; |
|
499 |
} |
|
500 |
||
501 |
TestClass::CallableMode TestClass::callableMode() const |
|
502 |
{ |
|
503 |
return m_callableMode; |
|
504 |
} |
|
505 |
||
506 |
void TestClass::setHasInstance(bool hasInstance) |
|
507 |
{ |
|
508 |
m_hasInstance = hasInstance; |
|
509 |
} |
|
510 |
||
511 |
bool TestClass::hasInstance() const |
|
512 |
{ |
|
513 |
return m_hasInstance; |
|
514 |
} |
|
515 |
||
516 |
||
517 |
TestClassPropertyIterator::TestClassPropertyIterator(const QHash<QScriptString, TestClass::CustomProperty*> &props, |
|
518 |
const QScriptValue &object) |
|
519 |
: QScriptClassPropertyIterator(object) |
|
520 |
{ |
|
521 |
m_props = props; |
|
522 |
toFront(); |
|
523 |
} |
|
524 |
||
525 |
TestClassPropertyIterator::~TestClassPropertyIterator() |
|
526 |
{ |
|
527 |
} |
|
528 |
||
529 |
bool TestClassPropertyIterator::hasNext() const |
|
530 |
{ |
|
531 |
return m_index < m_props.size(); |
|
532 |
} |
|
533 |
||
534 |
void TestClassPropertyIterator::next() |
|
535 |
{ |
|
536 |
m_last = m_index; |
|
537 |
++m_index; |
|
538 |
} |
|
539 |
||
540 |
bool TestClassPropertyIterator::hasPrevious() const |
|
541 |
{ |
|
542 |
return m_index > 0; |
|
543 |
} |
|
544 |
||
545 |
void TestClassPropertyIterator::previous() |
|
546 |
{ |
|
547 |
--m_index; |
|
548 |
m_last = m_index; |
|
549 |
} |
|
550 |
||
551 |
void TestClassPropertyIterator::toFront() |
|
552 |
{ |
|
553 |
m_index = 0; |
|
554 |
m_last = -1; |
|
555 |
} |
|
556 |
||
557 |
void TestClassPropertyIterator::toBack() |
|
558 |
{ |
|
559 |
m_index = m_props.size(); |
|
560 |
m_last = -1; |
|
561 |
} |
|
562 |
||
563 |
QScriptString TestClassPropertyIterator::name() const |
|
564 |
{ |
|
565 |
return m_props.keys().value(m_last); |
|
566 |
} |
|
567 |
||
568 |
uint TestClassPropertyIterator::id() const |
|
569 |
{ |
|
570 |
QScriptString key = m_props.keys().value(m_last); |
|
571 |
if (!key.isValid()) |
|
572 |
return 0; |
|
573 |
TestClass::CustomProperty *prop = m_props.value(key); |
|
574 |
return prop->id; |
|
575 |
} |
|
576 |
||
577 |
QScriptValue::PropertyFlags TestClassPropertyIterator::flags() const |
|
578 |
{ |
|
579 |
QScriptString key = m_props.keys().value(m_last); |
|
580 |
if (!key.isValid()) |
|
581 |
return 0; |
|
582 |
TestClass::CustomProperty *prop = m_props.value(key); |
|
583 |
return prop->pflags; |
|
584 |
} |
|
585 |
||
586 |
||
587 |
||
588 |
void tst_QScriptClass::newInstance() |
|
589 |
{ |
|
590 |
QScriptEngine eng; |
|
591 |
||
592 |
TestClass cls(&eng); |
|
593 |
||
594 |
QScriptValue obj1 = eng.newObject(&cls); |
|
595 |
QVERIFY(!obj1.data().isValid()); |
|
596 |
QVERIFY(obj1.prototype().strictlyEquals(cls.prototype())); |
|
597 |
QEXPECT_FAIL("", "classname is not implemented", Continue); |
|
598 |
QCOMPARE(obj1.toString(), QString::fromLatin1("[object TestClass]")); |
|
599 |
QCOMPARE(obj1.scriptClass(), (QScriptClass*)&cls); |
|
600 |
||
601 |
QScriptValue num(&eng, 456); |
|
602 |
QScriptValue obj2 = eng.newObject(&cls, num); |
|
603 |
QVERIFY(obj2.data().strictlyEquals(num)); |
|
604 |
QVERIFY(obj2.prototype().strictlyEquals(cls.prototype())); |
|
605 |
QCOMPARE(obj2.scriptClass(), (QScriptClass*)&cls); |
|
606 |
||
607 |
QScriptValue obj3 = eng.newObject(); |
|
608 |
QCOMPARE(obj3.scriptClass(), (QScriptClass*)0); |
|
609 |
obj3.setScriptClass(&cls); |
|
610 |
QCOMPARE(obj3.scriptClass(), (QScriptClass*)&cls); |
|
611 |
||
612 |
obj3.setScriptClass(0); |
|
613 |
QCOMPARE(obj3.scriptClass(), (QScriptClass*)0); |
|
614 |
obj3.setScriptClass(&cls); |
|
615 |
QCOMPARE(obj3.scriptClass(), (QScriptClass*)&cls); |
|
616 |
||
617 |
TestClass cls2(&eng); |
|
618 |
obj3.setScriptClass(&cls2); |
|
619 |
QCOMPARE(obj3.scriptClass(), (QScriptClass*)&cls2); |
|
620 |
||
621 |
// undefined behavior really, but shouldn't crash |
|
622 |
QScriptValue arr = eng.newArray(); |
|
623 |
QVERIFY(arr.isArray()); |
|
624 |
QCOMPARE(arr.scriptClass(), (QScriptClass*)0); |
|
625 |
QTest::ignoreMessage(QtWarningMsg, "QScriptValue::setScriptClass() failed: cannot change class of non-QScriptObject"); |
|
626 |
arr.setScriptClass(&cls); |
|
627 |
QEXPECT_FAIL("", "Changing class of arbitrary script object is not allowed (it's OK)", Continue); |
|
628 |
QCOMPARE(arr.scriptClass(), (QScriptClass*)&cls); |
|
629 |
QEXPECT_FAIL("", "Changing class of arbitrary script object is not allowed (it's OK)", Continue); |
|
630 |
QVERIFY(!arr.isArray()); |
|
631 |
QVERIFY(arr.isObject()); |
|
632 |
} |
|
633 |
||
634 |
void tst_QScriptClass::getAndSetProperty() |
|
635 |
{ |
|
636 |
QScriptEngine eng; |
|
637 |
||
638 |
TestClass cls(&eng); |
|
639 |
||
640 |
QScriptValue obj1 = eng.newObject(&cls); |
|
641 |
QScriptValue obj2 = eng.newObject(&cls); |
|
642 |
QScriptString foo = eng.toStringHandle("foo"); |
|
643 |
QScriptString bar = eng.toStringHandle("bar"); |
|
644 |
QScriptValue num(&eng, 123); |
|
645 |
||
646 |
// should behave just like normal |
|
647 |
for (int x = 0; x < 2; ++x) { |
|
648 |
QScriptValue &o = (x == 0) ? obj1 : obj2; |
|
649 |
for (int y = 0; y < 2; ++y) { |
|
650 |
QScriptString &s = (y == 0) ? foo : bar; |
|
651 |
||
652 |
// read property |
|
653 |
cls.clearReceivedArgs(); |
|
654 |
QScriptValue ret = o.property(s); |
|
655 |
QVERIFY(!ret.isValid()); |
|
656 |
QVERIFY(cls.lastQueryPropertyObject().strictlyEquals(o)); |
|
657 |
QVERIFY(cls.lastQueryPropertyName() == s); |
|
658 |
QVERIFY(!cls.lastPropertyObject().isValid()); |
|
659 |
QVERIFY(!cls.lastSetPropertyObject().isValid()); |
|
660 |
QVERIFY(cls.lastQueryPropertyFlags() == QScriptClass::HandlesReadAccess); |
|
661 |
||
662 |
// write property |
|
663 |
cls.clearReceivedArgs(); |
|
664 |
o.setProperty(s, num); |
|
665 |
QVERIFY(cls.lastQueryPropertyObject().strictlyEquals(o)); |
|
666 |
QVERIFY(cls.lastQueryPropertyName() == s); |
|
667 |
QVERIFY(!cls.lastPropertyObject().isValid()); |
|
668 |
QVERIFY(!cls.lastSetPropertyObject().isValid()); |
|
669 |
QVERIFY(cls.lastQueryPropertyFlags() == QScriptClass::HandlesWriteAccess); |
|
670 |
||
671 |
// re-read property |
|
672 |
// When a QScriptClass doesn't want to handle a property write, |
|
673 |
// that property becomes a normal property and the QScriptClass |
|
674 |
// shall not be queried about it again. |
|
675 |
cls.clearReceivedArgs(); |
|
676 |
QVERIFY(o.property(s).strictlyEquals(num)); |
|
677 |
QVERIFY(!cls.lastQueryPropertyObject().isValid()); |
|
678 |
} |
|
679 |
} |
|
680 |
||
681 |
// add a custom property |
|
682 |
QScriptString foo2 = eng.toStringHandle("foo2"); |
|
683 |
const uint foo2Id = 123; |
|
684 |
const QScriptValue::PropertyFlags foo2Pflags = QScriptValue::Undeletable; |
|
685 |
QScriptValue foo2Value(&eng, 456); |
|
686 |
cls.addCustomProperty(foo2, QScriptClass::HandlesReadAccess | QScriptClass::HandlesWriteAccess, |
|
687 |
foo2Id, foo2Pflags, foo2Value); |
|
688 |
||
689 |
{ |
|
690 |
// read property |
|
691 |
cls.clearReceivedArgs(); |
|
692 |
{ |
|
693 |
QScriptValue ret = obj1.property(foo2); |
|
694 |
QVERIFY(ret.strictlyEquals(foo2Value)); |
|
695 |
} |
|
696 |
QVERIFY(cls.lastQueryPropertyObject().strictlyEquals(obj1)); |
|
697 |
QVERIFY(cls.lastQueryPropertyName() == foo2); |
|
698 |
QVERIFY(cls.lastPropertyObject().strictlyEquals(obj1)); |
|
699 |
QVERIFY(cls.lastPropertyName() == foo2); |
|
700 |
QCOMPARE(cls.lastPropertyId(), foo2Id); |
|
701 |
||
702 |
// read flags |
|
703 |
cls.clearReceivedArgs(); |
|
704 |
QCOMPARE(obj1.propertyFlags(foo2), foo2Pflags); |
|
705 |
QVERIFY(cls.lastQueryPropertyObject().strictlyEquals(obj1)); |
|
706 |
QVERIFY(cls.lastQueryPropertyName() == foo2); |
|
707 |
QVERIFY(!cls.lastPropertyObject().isValid()); |
|
708 |
QVERIFY(cls.lastPropertyFlagsObject().strictlyEquals(obj1)); |
|
709 |
QVERIFY(cls.lastPropertyFlagsName() == foo2); |
|
710 |
QCOMPARE(cls.lastPropertyFlagsId(), foo2Id); |
|
711 |
||
712 |
// write property |
|
713 |
cls.clearReceivedArgs(); |
|
714 |
QScriptValue newFoo2Value(&eng, 789); |
|
715 |
obj1.setProperty(foo2, newFoo2Value); |
|
716 |
QVERIFY(cls.lastQueryPropertyObject().strictlyEquals(obj1)); |
|
717 |
QVERIFY(cls.lastQueryPropertyName() == foo2); |
|
718 |
||
719 |
// read property again |
|
720 |
cls.clearReceivedArgs(); |
|
721 |
{ |
|
722 |
QScriptValue ret = obj1.property(foo2); |
|
723 |
QVERIFY(ret.strictlyEquals(newFoo2Value)); |
|
724 |
} |
|
725 |
QVERIFY(cls.lastQueryPropertyObject().strictlyEquals(obj1)); |
|
726 |
QVERIFY(cls.lastQueryPropertyName() == foo2); |
|
727 |
QVERIFY(cls.lastPropertyObject().strictlyEquals(obj1)); |
|
728 |
QVERIFY(cls.lastPropertyName() == foo2); |
|
729 |
QCOMPARE(cls.lastPropertyId(), foo2Id); |
|
730 |
} |
|
731 |
||
732 |
// remove script class; normal properties should remain |
|
733 |
obj1.setScriptClass(0); |
|
734 |
QCOMPARE(obj1.scriptClass(), (QScriptClass*)0); |
|
735 |
QVERIFY(obj1.property(foo).equals(num)); |
|
736 |
QVERIFY(obj1.property(bar).equals(num)); |
|
737 |
obj1.setProperty(foo, QScriptValue()); |
|
738 |
QVERIFY(!obj1.property(foo).isValid()); |
|
739 |
obj1.setProperty(bar, QScriptValue()); |
|
740 |
QVERIFY(!obj1.property(bar).isValid()); |
|
741 |
} |
|
742 |
||
743 |
void tst_QScriptClass::enumerate() |
|
744 |
{ |
|
745 |
QScriptEngine eng; |
|
746 |
||
747 |
TestClass cls(&eng); |
|
748 |
||
749 |
QScriptValue obj = eng.newObject(&cls); |
|
750 |
QScriptString foo = eng.toStringHandle("foo"); |
|
751 |
obj.setProperty(foo, QScriptValue(&eng, 123)); |
|
752 |
||
753 |
cls.setIterationEnabled(false); |
|
754 |
{ |
|
755 |
QScriptValueIterator it(obj); |
|
756 |
QVERIFY(it.hasNext()); |
|
757 |
it.next(); |
|
758 |
QVERIFY(it.scriptName() == foo); |
|
759 |
QVERIFY(!it.hasNext()); |
|
760 |
} |
|
761 |
||
762 |
// add a custom property |
|
763 |
QScriptString foo2 = eng.toStringHandle("foo2"); |
|
764 |
const uint foo2Id = 123; |
|
765 |
const QScriptValue::PropertyFlags foo2Pflags = QScriptValue::Undeletable; |
|
766 |
QScriptValue foo2Value(&eng, 456); |
|
767 |
cls.addCustomProperty(foo2, QScriptClass::HandlesReadAccess | QScriptClass::HandlesWriteAccess, |
|
768 |
foo2Id, foo2Pflags, QScriptValue()); |
|
769 |
||
770 |
cls.setIterationEnabled(true); |
|
771 |
QScriptValueIterator it(obj); |
|
772 |
for (int x = 0; x < 2; ++x) { |
|
773 |
QVERIFY(it.hasNext()); |
|
774 |
it.next(); |
|
775 |
QEXPECT_FAIL("", "", Abort); |
|
776 |
QVERIFY(it.scriptName() == foo); |
|
777 |
QVERIFY(it.hasNext()); |
|
778 |
it.next(); |
|
779 |
QVERIFY(it.scriptName() == foo2); |
|
780 |
QCOMPARE(it.flags(), foo2Pflags); |
|
781 |
QVERIFY(!it.hasNext()); |
|
782 |
||
783 |
QVERIFY(it.hasPrevious()); |
|
784 |
it.previous(); |
|
785 |
QVERIFY(it.scriptName() == foo2); |
|
786 |
QCOMPARE(it.flags(), foo2Pflags); |
|
787 |
QVERIFY(it.hasPrevious()); |
|
788 |
it.previous(); |
|
789 |
QVERIFY(it.scriptName() == foo); |
|
790 |
QVERIFY(!it.hasPrevious()); |
|
791 |
} |
|
792 |
} |
|
793 |
||
794 |
void tst_QScriptClass::extension() |
|
795 |
{ |
|
796 |
QScriptEngine eng; |
|
797 |
{ |
|
798 |
TestClass cls(&eng); |
|
799 |
cls.setCallableMode(TestClass::NotCallable); |
|
800 |
QVERIFY(!cls.supportsExtension(QScriptClass::Callable)); |
|
801 |
QVERIFY(!cls.supportsExtension(QScriptClass::HasInstance)); |
|
802 |
QScriptValue obj = eng.newObject(&cls); |
|
803 |
QVERIFY(!obj.call().isValid()); |
|
804 |
QCOMPARE((int)cls.lastExtensionType(), -1); |
|
805 |
QVERIFY(!obj.instanceOf(obj)); |
|
806 |
QCOMPARE((int)cls.lastExtensionType(), -1); |
|
807 |
} |
|
808 |
// Callable |
|
809 |
{ |
|
810 |
TestClass cls(&eng); |
|
811 |
cls.setCallableMode(TestClass::CallableReturnsSum); |
|
812 |
QVERIFY(cls.supportsExtension(QScriptClass::Callable)); |
|
813 |
||
814 |
QScriptValue obj = eng.newObject(&cls); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
815 |
eng.globalObject().setProperty("obj", obj); |
0 | 816 |
obj.setProperty("one", QScriptValue(&eng, 1)); |
817 |
obj.setProperty("two", QScriptValue(&eng, 2)); |
|
818 |
obj.setProperty("three", QScriptValue(&eng, 3)); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
819 |
// From C++ |
0 | 820 |
cls.clearReceivedArgs(); |
821 |
{ |
|
822 |
QScriptValueList args; |
|
823 |
args << QScriptValue(&eng, 4) << QScriptValue(&eng, 5); |
|
824 |
QScriptValue ret = obj.call(obj, args); |
|
825 |
QCOMPARE(cls.lastExtensionType(), QScriptClass::Callable); |
|
826 |
QCOMPARE(cls.lastExtensionArgument().userType(), qMetaTypeId<QScriptContext*>()); |
|
827 |
QVERIFY(ret.isNumber()); |
|
828 |
QCOMPARE(ret.toNumber(), qsreal(15)); |
|
829 |
} |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
830 |
// From JS |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
831 |
cls.clearReceivedArgs(); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
832 |
{ |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
833 |
QScriptValue ret = eng.evaluate("obj(4, 5)"); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
834 |
QCOMPARE(cls.lastExtensionType(), QScriptClass::Callable); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
835 |
QCOMPARE(cls.lastExtensionArgument().userType(), qMetaTypeId<QScriptContext*>()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
836 |
QVERIFY(ret.isNumber()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
837 |
QCOMPARE(ret.toNumber(), qsreal(15)); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
838 |
} |
0 | 839 |
|
840 |
cls.setCallableMode(TestClass::CallableReturnsArgument); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
841 |
// From C++ |
0 | 842 |
cls.clearReceivedArgs(); |
843 |
{ |
|
844 |
QScriptValue ret = obj.call(obj, QScriptValueList() << 123); |
|
845 |
QCOMPARE(cls.lastExtensionType(), QScriptClass::Callable); |
|
846 |
QCOMPARE(cls.lastExtensionArgument().userType(), qMetaTypeId<QScriptContext*>()); |
|
847 |
QVERIFY(ret.isNumber()); |
|
848 |
QCOMPARE(ret.toInt32(), 123); |
|
849 |
} |
|
850 |
cls.clearReceivedArgs(); |
|
851 |
{ |
|
852 |
QScriptValue ret = obj.call(obj, QScriptValueList() << true); |
|
853 |
QCOMPARE(cls.lastExtensionType(), QScriptClass::Callable); |
|
854 |
QCOMPARE(cls.lastExtensionArgument().userType(), qMetaTypeId<QScriptContext*>()); |
|
855 |
QVERIFY(ret.isBoolean()); |
|
856 |
QCOMPARE(ret.toBoolean(), true); |
|
857 |
} |
|
858 |
{ |
|
859 |
QScriptValue ret = obj.call(obj, QScriptValueList() << QString::fromLatin1("ciao")); |
|
860 |
QVERIFY(ret.isString()); |
|
861 |
QCOMPARE(ret.toString(), QString::fromLatin1("ciao")); |
|
862 |
} |
|
863 |
{ |
|
864 |
QScriptValue objobj = eng.newObject(); |
|
865 |
QScriptValue ret = obj.call(obj, QScriptValueList() << objobj); |
|
866 |
QVERIFY(ret.isObject()); |
|
867 |
QVERIFY(ret.strictlyEquals(objobj)); |
|
868 |
} |
|
869 |
{ |
|
870 |
QScriptValue ret = obj.call(obj, QScriptValueList() << QScriptValue()); |
|
871 |
QVERIFY(ret.isUndefined()); |
|
872 |
} |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
873 |
// From JS |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
874 |
cls.clearReceivedArgs(); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
875 |
{ |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
876 |
QScriptValue ret = eng.evaluate("obj(123)"); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
877 |
QVERIFY(ret.isNumber()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
878 |
QCOMPARE(ret.toInt32(), 123); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
879 |
} |
0 | 880 |
|
881 |
cls.setCallableMode(TestClass::CallableReturnsInvalidVariant); |
|
882 |
{ |
|
883 |
QScriptValue ret = obj.call(obj); |
|
884 |
QVERIFY(ret.isUndefined()); |
|
885 |
} |
|
886 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
887 |
cls.setCallableMode(TestClass::CallableReturnsThisObject); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
888 |
// From C++ |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
889 |
{ |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
890 |
QScriptValue ret = obj.call(obj); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
891 |
QVERIFY(ret.isObject()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
892 |
QVERIFY(ret.strictlyEquals(obj)); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
893 |
} |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
894 |
// From JS |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
895 |
{ |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
896 |
QScriptValue ret = eng.evaluate("obj()"); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
897 |
QVERIFY(ret.isObject()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
898 |
QVERIFY(ret.strictlyEquals(eng.globalObject())); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
899 |
} |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
900 |
|
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
901 |
cls.setCallableMode(TestClass::CallableReturnsCallee); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
902 |
// From C++ |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
903 |
{ |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
904 |
QScriptValue ret = obj.call(); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
905 |
QVERIFY(ret.isObject()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
906 |
QVERIFY(ret.strictlyEquals(obj)); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
907 |
} |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
908 |
// From JS |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
909 |
{ |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
910 |
QScriptValue ret = eng.evaluate("obj()"); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
911 |
QVERIFY(ret.isObject()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
912 |
QVERIFY(ret.strictlyEquals(obj)); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
913 |
} |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
914 |
|
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
915 |
cls.setCallableMode(TestClass::CallableReturnsArgumentsObject); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
916 |
// From C++ |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
917 |
{ |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
918 |
QScriptValue ret = obj.call(obj, QScriptValueList() << 123); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
919 |
QVERIFY(ret.isObject()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
920 |
QVERIFY(ret.property("length").isNumber()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
921 |
QCOMPARE(ret.property("length").toInt32(), 1); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
922 |
QVERIFY(ret.property(0).isNumber()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
923 |
QCOMPARE(ret.property(0).toInt32(), 123); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
924 |
} |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
925 |
// From JS |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
926 |
{ |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
927 |
QScriptValue ret = eng.evaluate("obj(123)"); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
928 |
QVERIFY(ret.isObject()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
929 |
QVERIFY(ret.property("length").isNumber()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
930 |
QCOMPARE(ret.property("length").toInt32(), 1); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
931 |
QVERIFY(ret.property(0).isNumber()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
932 |
QCOMPARE(ret.property(0).toInt32(), 123); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
933 |
} |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
934 |
|
0 | 935 |
// construct() |
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
936 |
// From C++ |
0 | 937 |
cls.clearReceivedArgs(); |
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
938 |
cls.setCallableMode(TestClass::CallableReturnsGlobalObject); |
0 | 939 |
{ |
940 |
QScriptValue ret = obj.construct(); |
|
941 |
QCOMPARE(cls.lastExtensionType(), QScriptClass::Callable); |
|
942 |
QCOMPARE(cls.lastExtensionArgument().userType(), qMetaTypeId<QScriptContext*>()); |
|
943 |
QVERIFY(ret.isObject()); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
944 |
QVERIFY(ret.strictlyEquals(eng.globalObject())); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
945 |
} |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
946 |
// From JS |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
947 |
cls.clearReceivedArgs(); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
948 |
{ |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
949 |
QScriptValue ret = eng.evaluate("new obj()"); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
950 |
QCOMPARE(cls.lastExtensionType(), QScriptClass::Callable); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
951 |
QCOMPARE(cls.lastExtensionArgument().userType(), qMetaTypeId<QScriptContext*>()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
952 |
QVERIFY(ret.isObject()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
953 |
QVERIFY(ret.strictlyEquals(eng.globalObject())); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
954 |
} |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
955 |
// From C++ |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
956 |
cls.clearReceivedArgs(); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
957 |
cls.setCallableMode(TestClass::CallableInitializesThisObject); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
958 |
{ |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
959 |
QScriptValue ret = obj.construct(); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
960 |
QCOMPARE(cls.lastExtensionType(), QScriptClass::Callable); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
961 |
QCOMPARE(cls.lastExtensionArgument().userType(), qMetaTypeId<QScriptContext*>()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
962 |
QVERIFY(ret.isQObject()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
963 |
QCOMPARE(ret.toQObject(), (QObject*)&eng); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
964 |
} |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
965 |
// From JS |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
966 |
cls.clearReceivedArgs(); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
967 |
{ |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
968 |
QScriptValue ret = eng.evaluate("new obj()"); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
969 |
QCOMPARE(cls.lastExtensionType(), QScriptClass::Callable); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
970 |
QCOMPARE(cls.lastExtensionArgument().userType(), qMetaTypeId<QScriptContext*>()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
971 |
QVERIFY(ret.isQObject()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
972 |
QCOMPARE(ret.toQObject(), (QObject*)&eng); |
0 | 973 |
} |
974 |
} |
|
975 |
// HasInstance |
|
976 |
{ |
|
977 |
TestClass cls(&eng); |
|
978 |
cls.setHasInstance(true); |
|
979 |
QVERIFY(cls.supportsExtension(QScriptClass::HasInstance)); |
|
980 |
||
981 |
QScriptValue obj = eng.newObject(&cls); |
|
982 |
obj.setProperty("foo", QScriptValue(&eng, 123)); |
|
983 |
QScriptValue plain = eng.newObject(); |
|
984 |
QVERIFY(!plain.instanceOf(obj)); |
|
985 |
||
986 |
eng.globalObject().setProperty("HasInstanceTester", obj); |
|
987 |
eng.globalObject().setProperty("hasInstanceValue", plain); |
|
988 |
cls.clearReceivedArgs(); |
|
989 |
{ |
|
990 |
QScriptValue ret = eng.evaluate("hasInstanceValue instanceof HasInstanceTester"); |
|
991 |
QCOMPARE(cls.lastExtensionType(), QScriptClass::HasInstance); |
|
992 |
QCOMPARE(cls.lastExtensionArgument().userType(), qMetaTypeId<QScriptValueList>()); |
|
993 |
QScriptValueList lst = qvariant_cast<QScriptValueList>(cls.lastExtensionArgument()); |
|
994 |
QCOMPARE(lst.size(), 2); |
|
995 |
QVERIFY(lst.at(0).strictlyEquals(obj)); |
|
996 |
QVERIFY(lst.at(1).strictlyEquals(plain)); |
|
997 |
QVERIFY(ret.isBoolean()); |
|
998 |
QVERIFY(!ret.toBoolean()); |
|
999 |
} |
|
1000 |
||
1001 |
plain.setProperty("foo", QScriptValue(&eng, 456)); |
|
1002 |
QVERIFY(!plain.instanceOf(obj)); |
|
1003 |
{ |
|
1004 |
QScriptValue ret = eng.evaluate("hasInstanceValue instanceof HasInstanceTester"); |
|
1005 |
QVERIFY(ret.isBoolean()); |
|
1006 |
QVERIFY(!ret.toBoolean()); |
|
1007 |
} |
|
1008 |
||
1009 |
plain.setProperty("foo", obj.property("foo")); |
|
1010 |
QVERIFY(plain.instanceOf(obj)); |
|
1011 |
{ |
|
1012 |
QScriptValue ret = eng.evaluate("hasInstanceValue instanceof HasInstanceTester"); |
|
1013 |
QVERIFY(ret.isBoolean()); |
|
1014 |
QVERIFY(ret.toBoolean()); |
|
1015 |
} |
|
1016 |
} |
|
1017 |
} |
|
1018 |
||
1019 |
QTEST_MAIN(tst_QScriptClass) |
|
1020 |
#include "tst_qscriptclass.moc" |