Debian Codegraph @

tests/benchmarks/script/qscriptqobject/tst_qscriptqobject.cpp

raw symbols SCIP
1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28
29 #include <qtest.h>
30 #include <QtWidgets>
31 #include <QtScript>
32
33 #define ITERATION_COUNT 1e4
34
35 struct CustomType
36 {
37 int a;
38 };
39 Q_DECLARE_METATYPE(CustomType)
40
41 class PropertyTestObject : public QObject
42 {
43 Q_OBJECT
44 Q_ENUMS(EnumType)
45 Q_FLAGS(FlagsType)
46 Q_PROPERTY(bool boolProperty READ boolProperty WRITE setBoolProperty)
47 Q_PROPERTY(int intProperty READ intProperty WRITE setIntProperty)
48 Q_PROPERTY(double doubleProperty READ doubleProperty WRITE setDoubleProperty)
49 Q_PROPERTY(QString stringProperty READ stringProperty WRITE setStringProperty)
50 Q_PROPERTY(QVariant variantProperty READ variantProperty WRITE setVariantProperty)
51 Q_PROPERTY(QObject* qobjectProperty READ qobjectProperty WRITE setQObjectProperty)
52 Q_PROPERTY(CustomType customProperty READ customProperty WRITE setCustomProperty)
53 Q_PROPERTY(EnumType enumProperty READ enumProperty WRITE setEnumProperty)
54 Q_PROPERTY(FlagsType flagsProperty READ flagsProperty WRITE setFlagsProperty)
55 public:
56 enum EnumType {
57 NoEnumValue = 0,
58 FirstEnumValue = 1,
59 SecondEnumValue = 2,
60 ThirdEnumValue = 4
61 };
62 Q_DECLARE_FLAGS(FlagsType, EnumType)
63
64 PropertyTestObject(QObject *parent = 0)
65 : QObject(parent),
66 m_boolProperty(false),
67 m_intProperty(123),
68 m_doubleProperty(123),
69 m_stringProperty("hello"),
70 m_variantProperty(double(123)),
71 m_qobjectProperty(this),
72 m_enumProperty(SecondEnumValue),
73 m_flagsProperty(FirstEnumValue | ThirdEnumValue)
74 { }
75
76 bool boolProperty() const
77 { return m_boolProperty; }
78 void setBoolProperty(bool value)
79 { m_boolProperty = value; }
80
81 int intProperty() const
82 { return m_intProperty; }
83 void setIntProperty(int value)
84 { m_intProperty = value; }
85
86 int doubleProperty() const
87 { return m_doubleProperty; }
88 void setDoubleProperty(double value)
89 { m_doubleProperty = value; }
90
91 QString stringProperty() const
92 { return m_stringProperty; }
93 void setStringProperty(const QString &value)
94 { m_stringProperty = value; }
95
96 QVariant variantProperty() const
97 { return m_variantProperty; }
98 void setVariantProperty(const QVariant &value)
99 { m_variantProperty = value; }
100
101 QObject *qobjectProperty() const
102 { return m_qobjectProperty; }
103 void setQObjectProperty(QObject *qobject)
104 { m_qobjectProperty = qobject; }
105
106 CustomType customProperty() const
107 { return m_customProperty; }
108 void setCustomProperty(const CustomType &value)
109 { m_customProperty = value; }
110
111 EnumType enumProperty() const
112 { return m_enumProperty; }
113 void setEnumProperty(EnumType value)
114 { m_enumProperty = value; }
115
116 FlagsType flagsProperty() const
117 { return m_flagsProperty; }
118 void setFlagsProperty(FlagsType value)
119 { m_flagsProperty = value; }
120
121 private:
122 bool m_boolProperty;
123 int m_intProperty;
124 double m_doubleProperty;
125 QString m_stringProperty;
126 QVariant m_variantProperty;
127 QObject *m_qobjectProperty;
128 CustomType m_customProperty;
129 EnumType m_enumProperty;
130 FlagsType m_flagsProperty;
131 };
132
133 class SlotTestObject : public QObject
134 {
135 Q_OBJECT
136 public:
137 SlotTestObject(QObject *parent = 0)
138 : QObject(parent),
139 m_string(QString::fromLatin1("hello")),
140 m_variant(123)
141 { }
142
143 public Q_SLOTS:
144 void voidSlot() { }
145 void boolSlot(bool) { }
146 void intSlot(int) { }
147 void doubleSlot(double) { }
148 void stringSlot(const QString &) { }
149 void variantSlot(const QVariant &) { }
150 void qobjectSlot(QObject *) { }
151 void customTypeSlot(const CustomType &) { }
152
153 bool returnBoolSlot() { return true; }
154 int returnIntSlot() { return 123; }
155 double returnDoubleSlot() { return 123.0; }
156 QString returnStringSlot() { return m_string; }
157 QVariant returnVariantSlot() { return m_variant; }
158 QObject *returnQObjectSlot() { return this; }
159 CustomType returnCustomTypeSlot() { return m_custom; }
160
161 void fourDoubleSlot(double, double, double, double) { }
162 void sixDoubleSlot(double, double, double, double, double, double) { }
163 void eightDoubleSlot(double, double, double, double, double, double, double, double) { }
164
165 void fourStringSlot(const QString &, const QString &, const QString &, const QString &) { }
166 void sixStringSlot(const QString &, const QString &, const QString &, const QString &,
167 const QString &, const QString &) { }
168 void eightStringSlot(const QString &, const QString &, const QString &, const QString &,
169 const QString &, const QString &, const QString &, const QString &) { }
170
171 private:
172 QString m_string;
173 QVariant m_variant;
174 CustomType m_custom;
175 };
176
177 class SignalTestObject : public QObject
178 {
179 Q_OBJECT
180 public:
181 SignalTestObject(QObject *parent = 0)
182 : QObject(parent)
183 { }
184
185 void emitVoidSignal()
186 { emit voidSignal(); }
187 void emitBoolSignal(bool value)
188 { emit boolSignal(value); }
189 void emitIntSignal(int value)
190 { emit intSignal(value); }
191 void emitDoubleSignal(double value)
192 { emit doubleSignal(value); }
193 void emitStringSignal(const QString &value)
194 { emit stringSignal(value); }
195 void emitVariantSignal(const QVariant &value)
196 { emit variantSignal(value); }
197 void emitQObjectSignal(QObject *object)
198 { emit qobjectSignal(object); }
199 void emitCustomTypeSignal(const CustomType &value)
200 { emit customTypeSignal(value); }
201
202 Q_SIGNALS:
203 void voidSignal();
204 void boolSignal(bool);
205 void intSignal(int);
206 void doubleSignal(double);
207 void stringSignal(const QString &);
208 void variantSignal(const QVariant &);
209 void qobjectSignal(QObject *);
210 void customTypeSignal(const CustomType &);
211 };
212
213 class OverloadedSlotTestObject : public QObject
214 {
215 Q_OBJECT
216 public:
217 OverloadedSlotTestObject(QObject *parent = 0)
218 : QObject(parent)
219 { }
220
221 public Q_SLOTS:
222 void overloadedSlot() { }
223 void overloadedSlot(bool) { }
224 void overloadedSlot(double) { }
225 void overloadedSlot(const QString &) { }
226 };
227
228 class QtScriptablePropertyTestObject
229 : public PropertyTestObject, public QScriptable
230 {
231 };
232
233 class QtScriptableSlotTestObject
234 : public SlotTestObject, public QScriptable
235 {
236 };
237
238 class tst_QScriptQObject : public QObject
239 {
240 Q_OBJECT
241
242 public:
243 tst_QScriptQObject();
244 virtual ~tst_QScriptQObject();
245
246 private slots:
247 void initTestCase();
248
249 void readMetaProperty_data();
250 void readMetaProperty();
251
252 void writeMetaProperty_data();
253 void writeMetaProperty();
254
255 void readDynamicProperty_data();
256 void readDynamicProperty();
257
258 void writeDynamicProperty_data();
259 void writeDynamicProperty();
260
261 void readMethodByName_data();
262 void readMethodByName();
263
264 void readMethodBySignature_data();
265 void readMethodBySignature();
266
267 void readChild_data();
268 void readChild();
269
270 void readOneOfManyChildren_data();
271 void readOneOfManyChildren();
272
273 void readPrototypeProperty_data();
274 void readPrototypeProperty();
275
276 void readScriptProperty_data();
277 void readScriptProperty();
278
279 void readNoSuchProperty_data();
280 void readNoSuchProperty();
281
282 void readAllMetaProperties();
283
284 void callSlot_data();
285 void callSlot();
286
287 void callOverloadedSlot_data();
288 void callOverloadedSlot();
289
290 void voidSignalHandler();
291 void boolSignalHandler();
292 void intSignalHandler();
293 void doubleSignalHandler();
294 void stringSignalHandler();
295 void variantSignalHandler();
296 void qobjectSignalHandler();
297 void customTypeSignalHandler();
298
299 void emitSignal_data();
300 void emitSignal();
301
302 void readButtonMetaProperty_data();
303 void readButtonMetaProperty();
304
305 void writeButtonMetaProperty_data();
306 void writeButtonMetaProperty();
307
308 void readDynamicButtonProperty_data();
309 void readDynamicButtonProperty();
310
311 void writeDynamicButtonProperty_data();
312 void writeDynamicButtonProperty();
313
314 void readButtonMethodByName_data();
315 void readButtonMethodByName();
316
317 void readButtonMethodBySignature_data();
318 void readButtonMethodBySignature();
319
320 void readButtonChild_data();
321 void readButtonChild();
322
323 void readButtonPrototypeProperty_data();
324 void readButtonPrototypeProperty();
325
326 void readButtonScriptProperty_data();
327 void readButtonScriptProperty();
328
329 void readNoSuchButtonProperty_data();
330 void readNoSuchButtonProperty();
331
332 void callButtonMethod_data();
333 void callButtonMethod();
334
335 void readAllButtonMetaProperties();
336
337 void readQScriptableMetaProperty_data();
338 void readQScriptableMetaProperty();
339
340 void writeQScriptableMetaProperty_data();
341 void writeQScriptableMetaProperty();
342
343 void callQScriptableSlot_data();
344 void callQScriptableSlot();
345
346 private:
347 void readMetaProperty_dataHelper(const QMetaObject *mo);
348 void readMethodByName_dataHelper(const QMetaObject *mo);
349 void readMethodBySignature_dataHelper(const QMetaObject *mo);
350 void readAllMetaPropertiesHelper(QObject *o);
351
352 void readPropertyHelper(QScriptEngine &engine, const QScriptValue &object,
353 const QString &propertyName, const QString &argTemplate = ".%0");
354 void writePropertyHelper(QScriptEngine &engine, const QScriptValue &object,
355 const QString &propertyName, const QScriptValue &value,
356 const QString &argTemplate = ".%0");
357
358 void callMethodHelper(QScriptEngine &engine, QObject *object,
359 const QString &propertyName, const QString &arguments);
360 void signalHandlerHelper(QScriptEngine &engine, QObject *object, const char *signal);
361 };
362
363 tst_QScriptQObject::tst_QScriptQObject()
364 {
365 }
366
367 tst_QScriptQObject::~tst_QScriptQObject()
368 {
369 }
370
371 void tst_QScriptQObject::initTestCase()
372 {
373 qMetaTypeId<CustomType>();
374 }
375
376 void tst_QScriptQObject::readMetaProperty_dataHelper(const QMetaObject *mo)
377 {
378 QTest::addColumn<QString>("propertyName");
379
380 for (int i = 0; i < mo->propertyCount(); ++i) {
381 QMetaProperty prop = mo->property(i);
382 if (!qstrcmp(prop.name(), "default"))
383 continue; // skip reserved word
384 QTest::newRow(prop.name()) << prop.name();
385 }
386 }
387
388 void tst_QScriptQObject::readMethodByName_dataHelper(const QMetaObject *mo)
389 {
390 QTest::addColumn<QString>("propertyName");
391
392 QSet<QByteArray> uniqueNames;
393 for (int i = 0; i < mo->methodCount(); ++i) {
394 QMetaMethod method = mo->method(i);
395 if (method.access() == QMetaMethod::Private)
396 continue;
397 QByteArray signature = method.methodSignature();
398 QByteArray name = signature.left(signature.indexOf('('));
399 if (uniqueNames.contains(name))
400 continue;
401 QTest::newRow(name) << QString::fromLatin1(name);
402 uniqueNames.insert(name);
403 }
404 }
405
406 void tst_QScriptQObject::readMethodBySignature_dataHelper(const QMetaObject *mo)
407 {
408 QTest::addColumn<QString>("propertyName");
409
410 for (int i = 0; i < mo->methodCount(); ++i) {
411 QMetaMethod method = mo->method(i);
412 if (method.access() == QMetaMethod::Private)
413 continue;
414 QTest::newRow(method.methodSignature().constData()) << QString::fromLatin1(method.methodSignature().constData());
415 }
416 }
417
418 void tst_QScriptQObject::readAllMetaPropertiesHelper(QObject *o)
419 {
420 QString code = QString::fromLatin1(
421 "(function() {\n"
422 " for (var i = 0; i < 100; ++i) {\n");
423 const QMetaObject *mo = o->metaObject();
424 for (int i = 0; i < mo->propertyCount(); ++i) {
425 QMetaProperty prop = mo->property(i);
426 if (!qstrcmp(prop.name(), "default"))
427 continue; // skip reserved word
428 code.append(QString::fromLatin1(" this.%0;\n").arg(prop.name()));
429 }
430 code.append(
431 " }\n"
432 "})");
433
434 QScriptEngine engine;
435 QScriptValue fun = engine.evaluate(code);
436 QVERIFY(fun.isFunction());
437
438 QScriptValue wrapper = engine.newQObject(o);
439 QBENCHMARK {
440 fun.call(wrapper);
441 }
442 QVERIFY(!engine.hasUncaughtException());
443 }
444
445 void tst_QScriptQObject::readPropertyHelper(
446 QScriptEngine &engine, const QScriptValue &object,
447 const QString &propertyName, const QString &argTemplate)
448 {
449 QString code = QString::fromLatin1(
450 "(function() {\n"
451 " for (var i = 0; i < %0; ++i)\n"
452 " this%1;\n"
453 "})").arg(ITERATION_COUNT).arg(argTemplate.arg(propertyName));
454 QScriptValue fun = engine.evaluate(code);
455 QVERIFY(fun.isFunction());
456
457 QBENCHMARK {
458 fun.call(object);
459 }
460 QVERIFY(!engine.hasUncaughtException());
461 }
462
463 void tst_QScriptQObject::writePropertyHelper(
464 QScriptEngine &engine, const QScriptValue &object,
465 const QString &propertyName, const QScriptValue &value,
466 const QString &argTemplate)
467 {
468 QVERIFY(value.isValid());
469 QString code = QString::fromLatin1(
470 "(function(v) {\n"
471 " for (var i = 0; i < %0; ++i)\n"
472 " this%1 = v;\n"
473 "})").arg(ITERATION_COUNT).arg(argTemplate.arg(propertyName));
474 QScriptValue fun = engine.evaluate(code);
475 QVERIFY(fun.isFunction());
476
477 QScriptValueList args;
478 args << value;
479 QBENCHMARK {
480 fun.call(object, args);
481 }
482 QVERIFY(!engine.hasUncaughtException());
483 }
484
485 void tst_QScriptQObject::callMethodHelper(
486 QScriptEngine &engine, QObject *object,
487 const QString &propertyName, const QString &arguments)
488 {
489 QScriptValue wrapper = engine.newQObject(object);
490 QScriptValue method = wrapper.property(propertyName);
491 QVERIFY(method.isFunction());
492
493 // Generate code that calls the function directly; in this way
494 // only function call performance is measured, not function lookup
495 // as well.
496 QString code = QString::fromLatin1(
497 "(function(f) {\n"
498 " for (var i = 0; i < %0; ++i)\n"
499 " f(%1);\n"
500 "})").arg(ITERATION_COUNT).arg(arguments);
501 QScriptValue fun = engine.evaluate(code);
502 QVERIFY(fun.isFunction());
503
504 QScriptValueList args;
505 args << method;
506 QBENCHMARK {
507 fun.call(wrapper, args);
508 }
509 QVERIFY(!engine.hasUncaughtException());
510 }
511
512 void tst_QScriptQObject::signalHandlerHelper(
513 QScriptEngine &engine, QObject *object, const char *signal)
514 {
515 QScriptValue handler = engine.evaluate("(function(a) { return a; })");
516 QVERIFY(handler.isFunction());
517 QVERIFY(qScriptConnect(object, signal, QScriptValue(), handler));
518 }
519
520 void tst_QScriptQObject::readMetaProperty_data()
521 {
522 readMetaProperty_dataHelper(&PropertyTestObject::staticMetaObject);
523 }
524
525 // Reads a meta-object-defined property from JS. The purpose of this
526 // benchmark is to measure the overhead of reading a property from JS
527 // compared to calling the property getter directly from C++ without
528 // introspection or value conversion (that's the fastest we could
529 // possibly hope to get).
530 void tst_QScriptQObject::readMetaProperty()
531 {
532 QFETCH(QString, propertyName);
533
534 QScriptEngine engine;
535 PropertyTestObject testObject;
536 readPropertyHelper(engine, engine.newQObject(&testObject), propertyName);
537 }
538
539 void tst_QScriptQObject::writeMetaProperty_data()
540 {
541 readMetaProperty_data();
542 }
543
544 // Writes a meta-object-defined property from JS. The purpose of this
545 // benchmark is to measure the overhead of writing a property from JS
546 // compared to calling the property setter directly from C++ without
547 // introspection or value conversion (that's the fastest we could
548 // possibly hope to get).
549 void tst_QScriptQObject::writeMetaProperty()
550 {
551 QFETCH(QString, propertyName);
552
553 QScriptEngine engine;
554 PropertyTestObject testObject;
555 QScriptValue wrapper = engine.newQObject(&testObject);
556 QScriptValue value = wrapper.property(propertyName);
557 writePropertyHelper(engine, wrapper, propertyName, value);
558 }
559
560 void tst_QScriptQObject::readDynamicProperty_data()
561 {
562 QTest::addColumn<QVariant>("value");
563
564 QTest::newRow("bool") << QVariant(false);
565 QTest::newRow("int") << QVariant(123);
566 QTest::newRow("double") << QVariant(double(123.0));
567 QTest::newRow("string") << QVariant(QString::fromLatin1("hello"));
568 QTest::newRow("QObject*") << QVariant::fromValue((QObject*)this);
569 QTest::newRow("CustomType") << QVariant::fromValue(CustomType());
570 }
571
572 // Reads a dynamic property from JS. The purpose of this benchmark is
573 // to measure the overhead of reading a dynamic property from JS
574 // versus calling QObject::property(aDynamicProperty) directly from
575 // C++.
576 void tst_QScriptQObject::readDynamicProperty()
577 {
578 QFETCH(QVariant, value);
579
580 QObject testObject;
581 const char *propertyName = "dynamicProperty";
582 testObject.setProperty(propertyName, value);
583 QVERIFY(testObject.dynamicPropertyNames().contains(propertyName));
584
585 QScriptEngine engine;
586 readPropertyHelper(engine, engine.newQObject(&testObject), propertyName);
587 }
588
589 void tst_QScriptQObject::writeDynamicProperty_data()
590 {
591 readDynamicProperty_data();
592 }
593
594 // Writes an existing dynamic property from JS. The purpose of this
595 // benchmark is to measure the overhead of writing a dynamic property
596 // from JS versus calling QObject::setProperty(aDynamicProperty,
597 // aVariant) directly from C++.
598 void tst_QScriptQObject::writeDynamicProperty()
599 {
600 QFETCH(QVariant, value);
601
602 QObject testObject;
603 const char *propertyName = "dynamicProperty";
604 testObject.setProperty(propertyName, value);
605 QVERIFY(testObject.dynamicPropertyNames().contains(propertyName));
606
607 QScriptEngine engine;
608 writePropertyHelper(engine, engine.newQObject(&testObject), propertyName,
609 qScriptValueFromValue(&engine, value));
610 }
611
612 void tst_QScriptQObject::readMethodByName_data()
613 {
614 readMethodByName_dataHelper(&SlotTestObject::staticMetaObject);
615 }
616
617 // Reads a meta-object-defined method from JS by name. The purpose of
618 // this benchmark is to measure the overhead of resolving a method
619 // from JS (effectively, creating and returning a JS wrapper function
620 // object for a C++ method).
621 void tst_QScriptQObject::readMethodByName()
622 {
623 readMetaProperty();
624 }
625
626 void tst_QScriptQObject::readMethodBySignature_data()
627 {
628 readMethodBySignature_dataHelper(&SlotTestObject::staticMetaObject);
629 }
630
631 // Reads a meta-object-defined method from JS by signature. The
632 // purpose of this benchmark is to measure the overhead of resolving a
633 // method from JS (effectively, creating and returning a JS wrapper
634 // function object for a C++ method).
635 void tst_QScriptQObject::readMethodBySignature()
636 {
637 QFETCH(QString, propertyName);
638
639 QScriptEngine engine;
640 SlotTestObject testObject;
641 readPropertyHelper(engine, engine.newQObject(&testObject), propertyName, "['%0']");
642 }
643
644 void tst_QScriptQObject::readChild_data()
645 {
646 QTest::addColumn<QString>("propertyName");
647
648 QTest::newRow("child") << "child";
649 }
650
651 // Reads a child object from JS. The purpose of this benchmark is to
652 // measure the overhead of reading a child object from JS compared to
653 // calling e.g. qFindChild() directly from C++, when the test object
654 // is a plain QObject with only one child.
655 void tst_QScriptQObject::readChild()
656 {
657 QFETCH(QString, propertyName);
658
659 QScriptEngine engine;
660 QObject testObject;
661 QObject *child = new QObject(&testObject);
662 child->setObjectName(propertyName);
663 readPropertyHelper(engine, engine.newQObject(&testObject), propertyName);
664 }
665
666 void tst_QScriptQObject::readOneOfManyChildren_data()
667 {
668 QTest::addColumn<QString>("propertyName");
669
670 QTest::newRow("child0") << "child0";
671 QTest::newRow("child50") << "child50";
672 QTest::newRow("child99") << "child99";
673 }
674
675 // Reads a child object from JS for an object that has many
676 // children. The purpose of this benchmark is to measure the overhead
677 // of reading a child object from JS compared to calling
678 // e.g. qFindChild() directly from C++.
679 void tst_QScriptQObject::readOneOfManyChildren()
680 {
681 QFETCH(QString, propertyName);
682
683 QScriptEngine engine;
684 QObject testObject;
685 for (int i = 0; i < 100; ++i) {
686 QObject *child = new QObject(&testObject);
687 child->setObjectName(QString::fromLatin1("child%0").arg(i));
688 }
689 readPropertyHelper(engine, engine.newQObject(&testObject), propertyName);
690 }
691
692 void tst_QScriptQObject::readPrototypeProperty_data()
693 {
694 QTest::addColumn<QString>("propertyName");
695
696 // Inherited from Object.prototype.
697 QTest::newRow("hasOwnProperty") << "hasOwnProperty";
698 QTest::newRow("isPrototypeOf") << "isPrototypeOf";
699 QTest::newRow("propertyIsEnumerable") << "propertyIsEnumerable";
700 QTest::newRow("valueOf") << "valueOf";
701 }
702
703 // Reads a property that's inherited from a prototype object. The
704 // purpose of this benchmark is to measure the overhead of resolving a
705 // prototype property (i.e., how long it takes the binding to
706 // determine that the QObject doesn't have the property itself).
707 void tst_QScriptQObject::readPrototypeProperty()
708 {
709 QFETCH(QString, propertyName);
710
711 QScriptEngine engine;
712 PropertyTestObject testObject;
713 readPropertyHelper(engine, engine.newQObject(&testObject), propertyName);
714 }
715
716 void tst_QScriptQObject::readScriptProperty_data()
717 {
718 QTest::addColumn<QString>("propertyName");
719
720 QTest::newRow("scriptProperty") << "scriptProperty";
721 }
722
723 // Reads a JS (non-Qt) property of a wrapper object. The purpose of
724 // this benchmark is to measure the overhead of reading a property
725 // that only exists on the wrapper object, not on the underlying
726 // QObject.
727 void tst_QScriptQObject::readScriptProperty()
728 {
729 QFETCH(QString, propertyName);
730
731 QScriptEngine engine;
732 PropertyTestObject testObject;
733 QScriptValue wrapper = engine.newQObject(&testObject);
734 wrapper.setProperty(propertyName, 123);
735 QVERIFY(wrapper.property(propertyName).isValid());
736 QVERIFY(!testObject.property(propertyName.toLatin1()).isValid());
737
738 readPropertyHelper(engine, wrapper, propertyName);
739 }
740
741 void tst_QScriptQObject::readNoSuchProperty_data()
742 {
743 QTest::addColumn<QString>("propertyName");
744
745 QTest::newRow("noSuchProperty") << "noSuchProperty";
746 }
747
748 // Reads a non-existing (undefined) property of a wrapper object. The
749 // purpose of this benchmark is to measure the overhead of reading a
750 // property that doesn't exist (i.e., how long it takes the binding to
751 // determine this).
752 void tst_QScriptQObject::readNoSuchProperty()
753 {
754 readMetaProperty();
755 }
756
757 // Reads all meta-object-defined properties from JS. The purpose of
758 // this benchmark is to measure the overhead of reading different
759 // properties in sequence, not just the same one repeatedly (like
760 // readMetaProperty() does).
761 void tst_QScriptQObject::readAllMetaProperties()
762 {
763 PropertyTestObject testObject;
764 readAllMetaPropertiesHelper(&testObject);
765 }
766
767 void tst_QScriptQObject::callSlot_data()
768 {
769 QTest::addColumn<QString>("propertyName");
770 QTest::addColumn<QString>("arguments");
771
772 QTest::newRow("voidSlot()") << "voidSlot" << "";
773
774 QTest::newRow("boolSlot(true)") << "boolSlot" << "true";
775 QTest::newRow("intSlot(123)") << "intSlot" << "123";
776 QTest::newRow("doubleSlot(123)") << "doubleSlot" << "123";
777 QTest::newRow("stringSlot('hello')") << "stringSlot" << "'hello'";
778 QTest::newRow("variantSlot(123)") << "variantSlot" << "123";
779 QTest::newRow("qobjectSlot(this)") << "qobjectSlot" << "this"; // assumes 'this' is a QObject
780
781 QTest::newRow("returnBoolSlot()") << "returnBoolSlot" << "";
782 QTest::newRow("returnIntSlot()") << "returnIntSlot" << "";
783 QTest::newRow("returnDoubleSlot()") << "returnDoubleSlot" << "";
784 QTest::newRow("returnStringSlot()") << "returnStringSlot" << "";
785 QTest::newRow("returnVariantSlot()") << "returnVariantSlot" << "";
786 QTest::newRow("returnQObjectSlot()") << "returnQObjectSlot" << "";
787 QTest::newRow("returnCustomTypeSlot()") << "returnCustomTypeSlot" << "";
788
789 // Implicit conversion.
790 QTest::newRow("boolSlot(0)") << "boolSlot" << "0";
791 QTest::newRow("intSlot('123')") << "intSlot" << "'123'";
792 QTest::newRow("doubleSlot('123')") << "doubleSlot" << "'123'";
793 QTest::newRow("stringSlot(123)") << "stringSlot" << "123";
794
795 // Many arguments.
796 QTest::newRow("fourDoubleSlot(1,2,3,4)") << "fourDoubleSlot" << "1,2,3,4";
797 QTest::newRow("sixDoubleSlot(1,2,3,4,5,6)") << "sixDoubleSlot" << "1,2,3,4,5,6";
798 QTest::newRow("eightDoubleSlot(1,2,3,4,5,6,7,8)") << "eightDoubleSlot" << "1,2,3,4,5,6,7,8";
799
800 QTest::newRow("fourStringSlot('a','b','c','d')") << "fourStringSlot" << "'a','b','c','d'";
801 QTest::newRow("sixStringSlot('a','b','c','d','e','f')") << "sixStringSlot" << "'a','b','c','d','e','f'";
802 QTest::newRow("eightStringSlot('a','b','c','d','e','f','g','h')") << "eightStringSlot" << "'a','b','c','d','e','f','g','h'";
803 }
804
805 // Calls a slot from JS. The purpose of this benchmark is to measure
806 // the overhead of calling a slot from JS compared to calling the slot
807 // directly from C++ without introspection or value conversion (that's
808 // the fastest we could possibly hope to get). The slots themselves
809 // don't do any work.
810 void tst_QScriptQObject::callSlot()
811 {
812 QFETCH(QString, propertyName);
813 QFETCH(QString, arguments);
814
815 QScriptEngine engine;
816 SlotTestObject testObject;
817 callMethodHelper(engine, &testObject, propertyName, arguments);
818 }
819
820 void tst_QScriptQObject::callOverloadedSlot_data()
821 {
822 QTest::addColumn<QString>("propertyName");
823 QTest::addColumn<QString>("arguments");
824
825 QTest::newRow("overloadedSlot()") << "overloadedSlot" << "";
826 QTest::newRow("overloadedSlot(true)") << "overloadedSlot" << "true";
827 QTest::newRow("overloadedSlot(123)") << "overloadedSlot" << "123";
828 QTest::newRow("overloadedSlot('hello')") << "overloadedSlot" << "'hello'";
829 }
830
831 // Calls an overloaded slot from JS. The purpose of this benchmark is
832 // to measure the overhead of calling an overloaded slot from JS
833 // compared to calling the overloaded slot directly from C++ without
834 // introspection or value conversion (that's the fastest we could
835 // possibly hope to get).
836 void tst_QScriptQObject::callOverloadedSlot()
837 {
838 QFETCH(QString, propertyName);
839 QFETCH(QString, arguments);
840
841 QScriptEngine engine;
842 OverloadedSlotTestObject testObject;
843 callMethodHelper(engine, &testObject, propertyName, arguments);
844 }
845
846 // Benchmarks for JS signal handling. The purpose of these benchmarks
847 // is to measure the overhead of dispatching a Qt signal to JS code
848 // compared to a normal C++ signal-to-slot dispatch.
849
850 void tst_QScriptQObject::voidSignalHandler()
851 {
852 SignalTestObject testObject;
853 QScriptEngine engine;
854 signalHandlerHelper(engine, &testObject, SIGNAL(voidSignal()));
855 QBENCHMARK {
856 for (int i = 0; i < ITERATION_COUNT; ++i)
857 testObject.emitVoidSignal();
858 }
859 }
860
861 void tst_QScriptQObject::boolSignalHandler()
862 {
863 SignalTestObject testObject;
864 QScriptEngine engine;
865 signalHandlerHelper(engine, &testObject, SIGNAL(boolSignal(bool)));
866 QBENCHMARK {
867 for (int i = 0; i < ITERATION_COUNT; ++i)
868 testObject.emitBoolSignal(true);
869 }
870 }
871
872 void tst_QScriptQObject::intSignalHandler()
873 {
874 SignalTestObject testObject;
875 QScriptEngine engine;
876 signalHandlerHelper(engine, &testObject, SIGNAL(intSignal(int)));
877 QBENCHMARK {
878 for (int i = 0; i < ITERATION_COUNT; ++i)
879 testObject.emitIntSignal(123);
880 }
881 }
882
883 void tst_QScriptQObject::doubleSignalHandler()
884 {
885 SignalTestObject testObject;
886 QScriptEngine engine;
887 signalHandlerHelper(engine, &testObject, SIGNAL(doubleSignal(double)));
888 QBENCHMARK {
889 for (int i = 0; i < ITERATION_COUNT; ++i)
890 testObject.emitDoubleSignal(123.0);
891 }
892 }
893
894 void tst_QScriptQObject::stringSignalHandler()
895 {
896 SignalTestObject testObject;
897 QScriptEngine engine;
898 signalHandlerHelper(engine, &testObject, SIGNAL(stringSignal(QString)));
899 QString value = QString::fromLatin1("hello");
900 QBENCHMARK {
901 for (int i = 0; i < ITERATION_COUNT; ++i)
902 testObject.emitStringSignal(value);
903 }
904 }
905
906 void tst_QScriptQObject::variantSignalHandler()
907 {
908 SignalTestObject testObject;
909 QScriptEngine engine;
910 signalHandlerHelper(engine, &testObject, SIGNAL(variantSignal(QVariant)));
911 QVariant value = 123;
912 QBENCHMARK {
913 for (int i = 0; i < ITERATION_COUNT; ++i)
914 testObject.emitVariantSignal(value);
915 }
916 }
917
918 void tst_QScriptQObject::qobjectSignalHandler()
919 {
920 SignalTestObject testObject;
921 QScriptEngine engine;
922 signalHandlerHelper(engine, &testObject, SIGNAL(qobjectSignal(QObject*)));
923 QBENCHMARK {
924 for (int i = 0; i < ITERATION_COUNT; ++i)
925 testObject.emitQObjectSignal(this);
926 }
927 }
928
929 void tst_QScriptQObject::customTypeSignalHandler()
930 {
931 SignalTestObject testObject;
932 QScriptEngine engine;
933 signalHandlerHelper(engine, &testObject, SIGNAL(customTypeSignal(CustomType)));
934 CustomType value;
935 QBENCHMARK {
936 for (int i = 0; i < ITERATION_COUNT; ++i)
937 testObject.emitCustomTypeSignal(value);
938 }
939 }
940
941 void tst_QScriptQObject::emitSignal_data()
942 {
943 QTest::addColumn<QString>("propertyName");
944 QTest::addColumn<QString>("arguments");
945
946 QTest::newRow("voidSignal()") << "voidSignal" << "";
947
948 QTest::newRow("boolSignal(true)") << "boolSignal" << "true";
949 QTest::newRow("intSignal(123)") << "intSignal" << "123";
950 QTest::newRow("doubleSignal(123)") << "doubleSignal" << "123";
951 QTest::newRow("stringSignal('hello')") << "stringSignal" << "'hello'";
952 QTest::newRow("variantSignal(123)") << "variantSignal" << "123";
953 QTest::newRow("qobjectSignal(this)") << "qobjectSignal" << "this"; // assumes 'this' is a QObject
954 }
955
956 void tst_QScriptQObject::emitSignal()
957 {
958 QFETCH(QString, propertyName);
959 QFETCH(QString, arguments);
960
961 QScriptEngine engine;
962 SignalTestObject testObject;
963 callMethodHelper(engine, &testObject, propertyName, arguments);
964 }
965
966 void tst_QScriptQObject::readButtonMetaProperty_data()
967 {
968 readMetaProperty_dataHelper(&QPushButton::staticMetaObject);
969 }
970
971 // Reads a meta-object-defined property from JS. The purpose of this
972 // benchmark is to measure the overhead of reading a property from JS
973 // compared to calling the property getter directly from C++ without
974 // introspection or value conversion (that's the fastest we could
975 // possibly hope to get).
976 void tst_QScriptQObject::readButtonMetaProperty()
977 {
978 QFETCH(QString, propertyName);
979
980 QScriptEngine engine;
981 QPushButton pb;
982 readPropertyHelper(engine, engine.newQObject(&pb), propertyName);
983 }
984
985 void tst_QScriptQObject::writeButtonMetaProperty_data()
986 {
987 readButtonMetaProperty_data();
988 }
989
990 // Writes a meta-object-defined property from JS. The purpose of this
991 // benchmark is to measure the overhead of writing a property from JS
992 // compared to calling the property setter directly from C++ without
993 // introspection or value conversion (that's the fastest we could
994 // possibly hope to get).
995 void tst_QScriptQObject::writeButtonMetaProperty()
996 {
997 QFETCH(QString, propertyName);
998
999 QScriptEngine engine;
1000 QPushButton pb;
1001 QVariant value = pb.property(propertyName.toLatin1());
1002 writePropertyHelper(engine, engine.newQObject(&pb), propertyName,
1003 qScriptValueFromValue(&engine, value));
1004 }
1005
1006 void tst_QScriptQObject::readDynamicButtonProperty_data()
1007 {
1008 readDynamicProperty_data();
1009 }
1010
1011 // Reads a dynamic property from JS. The purpose of this benchmark is
1012 // to measure the overhead of reading a dynamic property from JS
1013 // versus calling QObject::property(aDynamicProperty) directly from
1014 // C++.
1015 void tst_QScriptQObject::readDynamicButtonProperty()
1016 {
1017 QFETCH(QVariant, value);
1018
1019 QPushButton pb;
1020 const char *propertyName = "dynamicProperty";
1021 pb.setProperty(propertyName, value);
1022 QVERIFY(pb.dynamicPropertyNames().contains(propertyName));
1023
1024 QScriptEngine engine;
1025 readPropertyHelper(engine, engine.newQObject(&pb), propertyName);
1026 }
1027
1028 void tst_QScriptQObject::writeDynamicButtonProperty_data()
1029 {
1030 readDynamicButtonProperty_data();
1031 }
1032
1033 // Writes an existing dynamic property from JS. The purpose of this
1034 // benchmark is to measure the overhead of writing a dynamic property
1035 // from JS versus calling QObject::setProperty(aDynamicProperty,
1036 // aVariant) directly from C++.
1037 void tst_QScriptQObject::writeDynamicButtonProperty()
1038 {
1039 QFETCH(QVariant, value);
1040
1041 QPushButton pb;
1042 const char *propertyName = "dynamicProperty";
1043 pb.setProperty(propertyName, value);
1044 QVERIFY(pb.dynamicPropertyNames().contains(propertyName));
1045
1046 QScriptEngine engine;
1047 writePropertyHelper(engine, engine.newQObject(&pb), propertyName,
1048 qScriptValueFromValue(&engine, value));
1049 }
1050
1051 void tst_QScriptQObject::readButtonMethodByName_data()
1052 {
1053 readMethodByName_dataHelper(&QPushButton::staticMetaObject);
1054 }
1055
1056 // Reads a meta-object-defined method from JS by name. The purpose of
1057 // this benchmark is to measure the overhead of resolving a method
1058 // from JS (effectively, creating and returning a JS wrapper function
1059 // object for a C++ method).
1060 void tst_QScriptQObject::readButtonMethodByName()
1061 {
1062 readButtonMetaProperty();
1063 }
1064
1065 void tst_QScriptQObject::readButtonMethodBySignature_data()
1066 {
1067 readMethodBySignature_dataHelper(&QPushButton::staticMetaObject);
1068 }
1069
1070 // Reads a meta-object-defined method from JS by signature. The
1071 // purpose of this benchmark is to measure the overhead of resolving a
1072 // method from JS (effectively, creating and returning a JS wrapper
1073 // function object for a C++ method).
1074 void tst_QScriptQObject::readButtonMethodBySignature()
1075 {
1076 QFETCH(QString, propertyName);
1077
1078 QScriptEngine engine;
1079 QPushButton pb;
1080 readPropertyHelper(engine, engine.newQObject(&pb), propertyName, "['%0']");
1081 }
1082
1083 void tst_QScriptQObject::readButtonChild_data()
1084 {
1085 QTest::addColumn<QString>("propertyName");
1086
1087 QTest::newRow("child") << "child";
1088 }
1089
1090 // Reads a child object from JS. The purpose of this benchmark is to
1091 // measure the overhead of reading a child object from JS compared to
1092 // calling e.g. qFindChild() directly from C++.
1093 void tst_QScriptQObject::readButtonChild()
1094 {
1095 QFETCH(QString, propertyName);
1096
1097 QScriptEngine engine;
1098 QPushButton pb;
1099 QObject *child = new QObject(&pb);
1100 child->setObjectName(propertyName);
1101 readPropertyHelper(engine, engine.newQObject(&pb), propertyName);
1102 }
1103
1104 void tst_QScriptQObject::readButtonPrototypeProperty_data()
1105 {
1106 readPrototypeProperty_data();
1107 }
1108
1109 // Reads a property that's inherited from a prototype object. The
1110 // purpose of this benchmark is to measure the overhead of resolving a
1111 // prototype property (i.e., how long does it take the binding to
1112 // determine that the QObject doesn't have the property itself).
1113 void tst_QScriptQObject::readButtonPrototypeProperty()
1114 {
1115 QFETCH(QString, propertyName);
1116
1117 QScriptEngine engine;
1118 QPushButton pb;
1119 readPropertyHelper(engine, engine.newQObject(&pb), propertyName);
1120 }
1121
1122 void tst_QScriptQObject::readButtonScriptProperty_data()
1123 {
1124 readScriptProperty_data();
1125 }
1126
1127 // Reads a JS (non-Qt) property of a wrapper object. The purpose of
1128 // this benchmark is to measure the overhead of reading a property
1129 // that only exists on the wrapper object, not on the underlying
1130 // QObject.
1131 void tst_QScriptQObject::readButtonScriptProperty()
1132 {
1133 QFETCH(QString, propertyName);
1134
1135 QScriptEngine engine;
1136 QPushButton pb;
1137 QScriptValue wrapper = engine.newQObject(&pb);
1138 wrapper.setProperty(propertyName, 123);
1139 QVERIFY(wrapper.property(propertyName).isValid());
1140 QVERIFY(!pb.property(propertyName.toLatin1()).isValid());
1141
1142 readPropertyHelper(engine, wrapper, propertyName);
1143 }
1144
1145 void tst_QScriptQObject::readNoSuchButtonProperty_data()
1146 {
1147 readNoSuchProperty_data();
1148 }
1149
1150 // Reads a non-existing (undefined) property of a wrapper object. The
1151 // purpose of this benchmark is to measure the overhead of reading a
1152 // property that doesn't exist (i.e., how long does it take the
1153 // binding to determine that it doesn't exist).
1154 void tst_QScriptQObject::readNoSuchButtonProperty()
1155 {
1156 readButtonMetaProperty();
1157 }
1158
1159 void tst_QScriptQObject::callButtonMethod_data()
1160 {
1161 QTest::addColumn<QString>("propertyName");
1162 QTest::addColumn<QString>("arguments");
1163
1164 QTest::newRow("click()") << "click" << "";
1165 QTest::newRow("animateClick(50)") << "animateClick" << "10";
1166 QTest::newRow("setChecked(true)") << "setChecked" << "true";
1167 QTest::newRow("close()") << "close" << "";
1168 QTest::newRow("setWindowTitle('foo')") << "setWindowTitle" << "'foo'";
1169 }
1170
1171 // Calls a slot from JS. The purpose of this benchmark is to measure
1172 // the overhead of calling a slot from JS compared to calling the slot
1173 // directly from C++ without introspection or value conversion (that's
1174 // the fastest we could possibly hope to get).
1175 void tst_QScriptQObject::callButtonMethod()
1176 {
1177 QFETCH(QString, propertyName);
1178 QFETCH(QString, arguments);
1179
1180 QScriptEngine engine;
1181 QPushButton pb;
1182 callMethodHelper(engine, &pb, propertyName, arguments);
1183 }
1184
1185 // Reads all meta-object-defined properties from JS. The purpose of
1186 // this benchmark is to measure the overhead of reading different
1187 // properties in sequence, not just the same one repeatedly (like
1188 // readButtonMetaProperty() does).
1189 void tst_QScriptQObject::readAllButtonMetaProperties()
1190 {
1191 QPushButton pb;
1192 readAllMetaPropertiesHelper(&pb);
1193 }
1194
1195 void tst_QScriptQObject::readQScriptableMetaProperty_data()
1196 {
1197 readMetaProperty_dataHelper(&QtScriptablePropertyTestObject::staticMetaObject);
1198 }
1199
1200 // Reads a meta-object-defined property from JS for an object that
1201 // subclasses QScriptable. The purpose of this benchmark is to measure
1202 // the overhead compared to reading a property of a non-QScriptable
1203 // (see readMetaProperty()).
1204 void tst_QScriptQObject::readQScriptableMetaProperty()
1205 {
1206 QFETCH(QString, propertyName);
1207
1208 QScriptEngine engine;
1209 QtScriptablePropertyTestObject testObject;
1210 readPropertyHelper(engine, engine.newQObject(&testObject), propertyName);
1211 }
1212
1213 void tst_QScriptQObject::writeQScriptableMetaProperty_data()
1214 {
1215 readMetaProperty_data();
1216 }
1217
1218 // Writes a meta-object-defined property from JS for an object that
1219 // subclasses QScriptable. The purpose of this benchmark is to measure
1220 // the overhead compared to writing a property of a non-QScriptable
1221 // object (see writeMetaProperty()).
1222 void tst_QScriptQObject::writeQScriptableMetaProperty()
1223 {
1224 QFETCH(QString, propertyName);
1225
1226 QScriptEngine engine;
1227 QtScriptablePropertyTestObject testObject;
1228 QVariant value = testObject.property(propertyName.toLatin1());
1229 writePropertyHelper(engine, engine.newQObject(&testObject), propertyName,
1230 qScriptValueFromValue(&engine, value));
1231 }
1232
1233 void tst_QScriptQObject::callQScriptableSlot_data()
1234 {
1235 callSlot_data();
1236 }
1237
1238 // Calls a slot from JS for an object that subclasses QScriptable. The
1239 // purpose of this benchmark is to measure the overhead compared to
1240 // calling a slot of a non-QScriptable object (see callSlot()).
1241 void tst_QScriptQObject::callQScriptableSlot()
1242 {
1243 QFETCH(QString, propertyName);
1244 QFETCH(QString, arguments);
1245
1246 QScriptEngine engine;
1247 QtScriptableSlotTestObject testObject;
1248 callMethodHelper(engine, &testObject, propertyName, arguments);
1249 }
1250
1251 QTEST_MAIN(tst_QScriptQObject)
1252 #include "tst_qscriptqobject.moc"