|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 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 |
|
42 #include <qtest.h> |
|
43 #include <QtScript> |
|
44 |
|
45 Q_DECLARE_METATYPE(QScriptContext*) |
|
46 Q_DECLARE_METATYPE(QScriptValue) |
|
47 Q_DECLARE_METATYPE(QScriptValueList) |
|
48 |
|
49 //TESTED_FILES= |
|
50 |
|
51 class TestClass : public QScriptClass |
|
52 { |
|
53 public: |
|
54 struct CustomProperty { |
|
55 QueryFlags qflags; |
|
56 uint id; |
|
57 QScriptValue::PropertyFlags pflags; |
|
58 QScriptValue value; |
|
59 |
|
60 CustomProperty(QueryFlags qf, uint i, QScriptValue::PropertyFlags pf, |
|
61 const QScriptValue &val) |
|
62 : qflags(qf), id(i), pflags(pf), value(val) { } |
|
63 }; |
|
64 |
|
65 enum CallableMode { |
|
66 NotCallable, |
|
67 CallableReturnsSum, |
|
68 CallableReturnsArgument, |
|
69 CallableReturnsInvalidVariant |
|
70 }; |
|
71 |
|
72 TestClass(QScriptEngine *engine); |
|
73 ~TestClass(); |
|
74 |
|
75 void addCustomProperty(const QScriptString &name, QueryFlags qflags, |
|
76 uint id, QScriptValue::PropertyFlags pflags, |
|
77 const QScriptValue &value); |
|
78 void removeCustomProperty(const QScriptString &name); |
|
79 |
|
80 QueryFlags queryProperty(const QScriptValue &object, |
|
81 const QScriptString &name, |
|
82 QueryFlags flags, uint *id); |
|
83 |
|
84 QScriptValue property(const QScriptValue &object, |
|
85 const QScriptString &name, uint id); |
|
86 |
|
87 void setProperty(QScriptValue &object, const QScriptString &name, |
|
88 uint id, const QScriptValue &value); |
|
89 |
|
90 QScriptValue::PropertyFlags propertyFlags( |
|
91 const QScriptValue &object, const QScriptString &name, uint id); |
|
92 |
|
93 QScriptClassPropertyIterator *newIterator(const QScriptValue &object); |
|
94 |
|
95 QScriptValue prototype() const; |
|
96 |
|
97 QString name() const; |
|
98 |
|
99 bool supportsExtension(Extension extension) const; |
|
100 QVariant extension(Extension extension, |
|
101 const QVariant &argument = QVariant()); |
|
102 |
|
103 void setIterationEnabled(bool enable); |
|
104 bool isIterationEnabled() const; |
|
105 |
|
106 void setCallableMode(CallableMode mode); |
|
107 CallableMode callableMode() const; |
|
108 |
|
109 void setHasInstance(bool hasInstance); |
|
110 bool hasInstance() const; |
|
111 |
|
112 private: |
|
113 inline CustomProperty *findCustomProperty(const QScriptString &name); |
|
114 |
|
115 QHash<QScriptString, CustomProperty*> customProperties; |
|
116 |
|
117 QScriptValue m_prototype; |
|
118 bool m_iterationEnabled; |
|
119 CallableMode m_callableMode; |
|
120 bool m_hasInstance; |
|
121 }; |
|
122 |
|
123 class TestClassPropertyIterator : public QScriptClassPropertyIterator |
|
124 { |
|
125 public: |
|
126 TestClassPropertyIterator(const QHash<QScriptString, TestClass::CustomProperty*> &props, |
|
127 const QScriptValue &object); |
|
128 ~TestClassPropertyIterator(); |
|
129 |
|
130 bool hasNext() const; |
|
131 void next(); |
|
132 |
|
133 bool hasPrevious() const; |
|
134 void previous(); |
|
135 |
|
136 void toFront(); |
|
137 void toBack(); |
|
138 |
|
139 QScriptString name() const; |
|
140 uint id() const; |
|
141 QScriptValue::PropertyFlags flags() const; |
|
142 |
|
143 private: |
|
144 int m_index; |
|
145 int m_last; |
|
146 QHash<QScriptString, TestClass::CustomProperty*> m_props; |
|
147 }; |
|
148 |
|
149 TestClass::TestClass(QScriptEngine *engine) |
|
150 : QScriptClass(engine), m_iterationEnabled(true), |
|
151 m_callableMode(NotCallable), m_hasInstance(false) |
|
152 { |
|
153 m_prototype = engine->newObject(); |
|
154 } |
|
155 |
|
156 TestClass::~TestClass() |
|
157 { |
|
158 qDeleteAll(customProperties); |
|
159 } |
|
160 |
|
161 TestClass::CustomProperty* TestClass::findCustomProperty(const QScriptString &name) |
|
162 { |
|
163 QHash<QScriptString, CustomProperty*>::const_iterator it; |
|
164 it = customProperties.constFind(name); |
|
165 if (it == customProperties.constEnd()) |
|
166 return 0; |
|
167 return it.value(); |
|
168 |
|
169 } |
|
170 |
|
171 void TestClass::addCustomProperty(const QScriptString &name, QueryFlags qflags, |
|
172 uint id, QScriptValue::PropertyFlags pflags, |
|
173 const QScriptValue &value) |
|
174 { |
|
175 customProperties.insert(name, new CustomProperty(qflags, id, pflags, value)); |
|
176 } |
|
177 |
|
178 void TestClass::removeCustomProperty(const QScriptString &name) |
|
179 { |
|
180 CustomProperty *prop = customProperties.take(name); |
|
181 if (prop) |
|
182 delete prop; |
|
183 } |
|
184 |
|
185 QScriptClass::QueryFlags TestClass::queryProperty(const QScriptValue &/*object*/, |
|
186 const QScriptString &name, |
|
187 QueryFlags flags, uint *id) |
|
188 { |
|
189 CustomProperty *prop = findCustomProperty(name); |
|
190 if (!prop) |
|
191 return 0; |
|
192 *id = prop->id; |
|
193 return prop->qflags & flags; |
|
194 } |
|
195 |
|
196 QScriptValue TestClass::property(const QScriptValue &/*object*/, |
|
197 const QScriptString &name, uint /*id*/) |
|
198 { |
|
199 CustomProperty *prop = findCustomProperty(name); |
|
200 if (!prop) |
|
201 return QScriptValue(); |
|
202 return prop->value; |
|
203 } |
|
204 |
|
205 void TestClass::setProperty(QScriptValue &/*object*/, const QScriptString &name, |
|
206 uint /*id*/, const QScriptValue &value) |
|
207 { |
|
208 CustomProperty *prop = findCustomProperty(name); |
|
209 if (!prop) |
|
210 return; |
|
211 prop->value = value; |
|
212 } |
|
213 |
|
214 QScriptValue::PropertyFlags TestClass::propertyFlags( |
|
215 const QScriptValue &/*object*/, const QScriptString &name, uint /*id*/) |
|
216 { |
|
217 CustomProperty *prop = findCustomProperty(name); |
|
218 if (!prop) |
|
219 return 0; |
|
220 return prop->pflags; |
|
221 } |
|
222 |
|
223 QScriptClassPropertyIterator *TestClass::newIterator(const QScriptValue &object) |
|
224 { |
|
225 if (!m_iterationEnabled) |
|
226 return 0; |
|
227 return new TestClassPropertyIterator(customProperties, object); |
|
228 } |
|
229 |
|
230 QScriptValue TestClass::prototype() const |
|
231 { |
|
232 return m_prototype; |
|
233 } |
|
234 |
|
235 QString TestClass::name() const |
|
236 { |
|
237 return QLatin1String("TestClass"); |
|
238 } |
|
239 |
|
240 bool TestClass::supportsExtension(Extension extension) const |
|
241 { |
|
242 if (extension == Callable) |
|
243 return (m_callableMode != NotCallable); |
|
244 if (extension == HasInstance) |
|
245 return m_hasInstance; |
|
246 return false; |
|
247 } |
|
248 |
|
249 QVariant TestClass::extension(Extension extension, |
|
250 const QVariant &argument) |
|
251 { |
|
252 if (extension == Callable) { |
|
253 Q_ASSERT(m_callableMode != NotCallable); |
|
254 QScriptContext *ctx = qvariant_cast<QScriptContext*>(argument); |
|
255 if (m_callableMode == CallableReturnsSum) { |
|
256 qsreal sum = 0; |
|
257 for (int i = 0; i < ctx->argumentCount(); ++i) |
|
258 sum += ctx->argument(i).toNumber(); |
|
259 QScriptValueIterator it(ctx->thisObject()); |
|
260 while (it.hasNext()) { |
|
261 it.next(); |
|
262 sum += it.value().toNumber(); |
|
263 } |
|
264 return sum; |
|
265 } else if (m_callableMode == CallableReturnsArgument) { |
|
266 return qVariantFromValue(ctx->argument(0)); |
|
267 } else if (m_callableMode == CallableReturnsInvalidVariant) { |
|
268 return QVariant(); |
|
269 } |
|
270 } else if (extension == HasInstance) { |
|
271 Q_ASSERT(m_hasInstance); |
|
272 QScriptValueList args = qvariant_cast<QScriptValueList>(argument); |
|
273 QScriptValue obj = args.at(0); |
|
274 QScriptValue value = args.at(1); |
|
275 return value.property("foo").equals(obj.property("foo")); |
|
276 } |
|
277 return QVariant(); |
|
278 } |
|
279 |
|
280 void TestClass::setIterationEnabled(bool enable) |
|
281 { |
|
282 m_iterationEnabled = enable; |
|
283 } |
|
284 |
|
285 bool TestClass::isIterationEnabled() const |
|
286 { |
|
287 return m_iterationEnabled; |
|
288 } |
|
289 |
|
290 void TestClass::setCallableMode(CallableMode mode) |
|
291 { |
|
292 m_callableMode = mode; |
|
293 } |
|
294 |
|
295 TestClass::CallableMode TestClass::callableMode() const |
|
296 { |
|
297 return m_callableMode; |
|
298 } |
|
299 |
|
300 void TestClass::setHasInstance(bool hasInstance) |
|
301 { |
|
302 m_hasInstance = hasInstance; |
|
303 } |
|
304 |
|
305 bool TestClass::hasInstance() const |
|
306 { |
|
307 return m_hasInstance; |
|
308 } |
|
309 |
|
310 TestClassPropertyIterator::TestClassPropertyIterator(const QHash<QScriptString, TestClass::CustomProperty*> &props, |
|
311 const QScriptValue &object) |
|
312 : QScriptClassPropertyIterator(object) |
|
313 { |
|
314 m_props = props; |
|
315 toFront(); |
|
316 } |
|
317 |
|
318 TestClassPropertyIterator::~TestClassPropertyIterator() |
|
319 { |
|
320 } |
|
321 |
|
322 bool TestClassPropertyIterator::hasNext() const |
|
323 { |
|
324 return m_index < m_props.size(); |
|
325 } |
|
326 |
|
327 void TestClassPropertyIterator::next() |
|
328 { |
|
329 m_last = m_index; |
|
330 ++m_index; |
|
331 } |
|
332 |
|
333 bool TestClassPropertyIterator::hasPrevious() const |
|
334 { |
|
335 return m_index > 0; |
|
336 } |
|
337 |
|
338 void TestClassPropertyIterator::previous() |
|
339 { |
|
340 --m_index; |
|
341 m_last = m_index; |
|
342 } |
|
343 |
|
344 void TestClassPropertyIterator::toFront() |
|
345 { |
|
346 m_index = 0; |
|
347 m_last = -1; |
|
348 } |
|
349 |
|
350 void TestClassPropertyIterator::toBack() |
|
351 { |
|
352 m_index = m_props.size(); |
|
353 m_last = -1; |
|
354 } |
|
355 |
|
356 QScriptString TestClassPropertyIterator::name() const |
|
357 { |
|
358 return m_props.keys().value(m_last); |
|
359 } |
|
360 |
|
361 uint TestClassPropertyIterator::id() const |
|
362 { |
|
363 QScriptString key = m_props.keys().value(m_last); |
|
364 if (!key.isValid()) |
|
365 return 0; |
|
366 TestClass::CustomProperty *prop = m_props.value(key); |
|
367 return prop->id; |
|
368 } |
|
369 |
|
370 QScriptValue::PropertyFlags TestClassPropertyIterator::flags() const |
|
371 { |
|
372 QScriptString key = m_props.keys().value(m_last); |
|
373 if (!key.isValid()) |
|
374 return 0; |
|
375 TestClass::CustomProperty *prop = m_props.value(key); |
|
376 return prop->pflags; |
|
377 } |
|
378 |
|
379 class tst_QScriptClass : public QObject |
|
380 { |
|
381 Q_OBJECT |
|
382 |
|
383 public: |
|
384 tst_QScriptClass(); |
|
385 virtual ~tst_QScriptClass(); |
|
386 |
|
387 public slots: |
|
388 void init(); |
|
389 void cleanup(); |
|
390 |
|
391 private slots: |
|
392 void noSuchProperty(); |
|
393 void property(); |
|
394 void setProperty(); |
|
395 void propertyFlags(); |
|
396 void call(); |
|
397 void hasInstance(); |
|
398 void iterate(); |
|
399 }; |
|
400 |
|
401 tst_QScriptClass::tst_QScriptClass() |
|
402 { |
|
403 } |
|
404 |
|
405 tst_QScriptClass::~tst_QScriptClass() |
|
406 { |
|
407 } |
|
408 |
|
409 void tst_QScriptClass::init() |
|
410 { |
|
411 } |
|
412 |
|
413 void tst_QScriptClass::cleanup() |
|
414 { |
|
415 } |
|
416 |
|
417 void tst_QScriptClass::noSuchProperty() |
|
418 { |
|
419 QScriptEngine eng; |
|
420 TestClass cls(&eng); |
|
421 QScriptValue obj = eng.newObject(&cls); |
|
422 QString propertyName = QString::fromLatin1("foo"); |
|
423 QBENCHMARK { |
|
424 (void)obj.property(propertyName); |
|
425 } |
|
426 } |
|
427 |
|
428 void tst_QScriptClass::property() |
|
429 { |
|
430 QScriptEngine eng; |
|
431 TestClass cls(&eng); |
|
432 QScriptString foo = eng.toStringHandle("foo"); |
|
433 cls.addCustomProperty(foo, QScriptClass::HandlesReadAccess, /*id=*/1, /*attributes=*/0, /*value=*/123); |
|
434 QScriptValue obj = eng.newObject(&cls); |
|
435 QBENCHMARK { |
|
436 (void)obj.property(foo); |
|
437 } |
|
438 } |
|
439 |
|
440 void tst_QScriptClass::setProperty() |
|
441 { |
|
442 QScriptEngine eng; |
|
443 TestClass cls(&eng); |
|
444 QScriptString foo = eng.toStringHandle("foo"); |
|
445 cls.addCustomProperty(foo, QScriptClass::HandlesWriteAccess, /*id=*/1, /*attributes=*/0, /*value=*/123); |
|
446 QScriptValue obj = eng.newObject(&cls); |
|
447 QScriptValue value(456); |
|
448 QBENCHMARK { |
|
449 obj.setProperty(foo, value); |
|
450 } |
|
451 } |
|
452 |
|
453 void tst_QScriptClass::propertyFlags() |
|
454 { |
|
455 QScriptEngine eng; |
|
456 TestClass cls(&eng); |
|
457 QScriptString foo = eng.toStringHandle("foo"); |
|
458 cls.addCustomProperty(foo, QScriptClass::HandlesReadAccess, /*id=*/1, QScriptValue::ReadOnly, /*value=*/123); |
|
459 QScriptValue obj = eng.newObject(&cls); |
|
460 QBENCHMARK { |
|
461 (void)obj.propertyFlags(foo); |
|
462 } |
|
463 } |
|
464 |
|
465 void tst_QScriptClass::call() |
|
466 { |
|
467 QScriptEngine eng; |
|
468 TestClass cls(&eng); |
|
469 cls.setCallableMode(TestClass::CallableReturnsArgument); |
|
470 QScriptValue obj = eng.newObject(&cls); |
|
471 QScriptValue thisObject; |
|
472 QScriptValueList args; |
|
473 args.append(123); |
|
474 QBENCHMARK { |
|
475 (void)obj.call(thisObject, args); |
|
476 } |
|
477 } |
|
478 |
|
479 void tst_QScriptClass::hasInstance() |
|
480 { |
|
481 QScriptEngine eng; |
|
482 TestClass cls(&eng); |
|
483 cls.setHasInstance(true); |
|
484 QScriptValue obj = eng.newObject(&cls); |
|
485 obj.setProperty("foo", 123); |
|
486 QScriptValue plain = eng.newObject(); |
|
487 plain.setProperty("foo", obj.property("foo")); |
|
488 QBENCHMARK { |
|
489 (void)plain.instanceOf(obj); |
|
490 } |
|
491 } |
|
492 |
|
493 void tst_QScriptClass::iterate() |
|
494 { |
|
495 QScriptEngine eng; |
|
496 TestClass cls(&eng); |
|
497 cls.setIterationEnabled(true); |
|
498 cls.addCustomProperty(eng.toStringHandle("foo"), QScriptClass::HandlesReadAccess, /*id=*/1, /*attributes=*/0, /*value=*/123); |
|
499 cls.addCustomProperty(eng.toStringHandle("bar"), QScriptClass::HandlesReadAccess, /*id=*/2, /*attributes=*/0, /*value=*/456); |
|
500 QScriptValue obj = eng.newObject(&cls); |
|
501 QBENCHMARK { |
|
502 QScriptValueIterator it(obj); |
|
503 while (it.hasNext()) { |
|
504 it.next(); |
|
505 (void)it.scriptName(); |
|
506 } |
|
507 } |
|
508 } |
|
509 |
|
510 QTEST_MAIN(tst_QScriptClass) |
|
511 #include "tst_qscriptclass.moc" |