~loic.molinari/+junk/qtdeclarative-shadereffectsource-changes

« back to all changes in this revision

Viewing changes to tests/benchmarks/qml/script/tst_script.cpp

  • Committer: Loïc Molinari
  • Date: 2012-04-21 17:59:51 UTC
  • Revision ID: loic.molinari@canonical.com-20120421175951-bqx68caaf5zrp76l
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/
 
5
**
 
6
** This file is part of the test suite of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:LGPL$
 
9
** GNU Lesser General Public License Usage
 
10
** This file may be used under the terms of the GNU Lesser General Public
 
11
** License version 2.1 as published by the Free Software Foundation and
 
12
** appearing in the file LICENSE.LGPL included in the packaging of this
 
13
** file. Please review the following information to ensure the GNU Lesser
 
14
** General Public License version 2.1 requirements will be met:
 
15
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 
16
**
 
17
** In addition, as a special exception, Nokia gives you certain additional
 
18
** rights. These rights are described in the Nokia Qt LGPL Exception
 
19
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 
20
**
 
21
** GNU General Public License Usage
 
22
** Alternatively, this file may be used under the terms of the GNU General
 
23
** Public License version 3.0 as published by the Free Software Foundation
 
24
** and appearing in the file LICENSE.GPL included in the packaging of this
 
25
** file. Please review the following information to ensure the GNU General
 
26
** Public License version 3.0 requirements will be met:
 
27
** http://www.gnu.org/copyleft/gpl.html.
 
28
**
 
29
** Other Usage
 
30
** Alternatively, this file may be used in accordance with the terms and
 
31
** conditions contained in a signed written agreement between you and Nokia.
 
32
**
 
33
**
 
34
**
 
35
**
 
36
**
 
37
**
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
 
 
42
#include <qtest.h>
 
43
#include <QQmlEngine>
 
44
#include <QQmlComponent>
 
45
#include <private/qqmlengine_p.h>
 
46
#include <private/qquickrectangle_p.h>
 
47
#include <QJSEngine>
 
48
#include <QJSValue>
 
49
 
 
50
class tst_script : public QObject
 
51
{
 
52
    Q_OBJECT
 
53
public:
 
54
    tst_script() {}
 
55
 
 
56
private slots:
 
57
    void initTestCase();
 
58
 
 
59
    void property_js();
 
60
    void property_getter_js();
 
61
#if 0
 
62
   //no native functions for now
 
63
   void property_getter();
 
64
   void property_getter_qobject();
 
65
   void property_getter_qmetaproperty();
 
66
#endif
 
67
    void property_qobject();
 
68
    void property_qmlobject();
 
69
 
 
70
    void setproperty_js();
 
71
    void setproperty_qmlobject();
 
72
 
 
73
    void function_js();
 
74
#if 0
 
75
    //no native functions for now
 
76
    void function_cpp();
 
77
#endif
 
78
    void function_qobject();
 
79
    void function_qmlobject();
 
80
 
 
81
    void function_args_js();
 
82
#if 0
 
83
    //no native functions for now
 
84
    void function_args_cpp();
 
85
#endif
 
86
    void function_args_qobject();
 
87
    void function_args_qmlobject();
 
88
 
 
89
    void signal_unconnected();
 
90
    void signal_qml();
 
91
    void signal_args();
 
92
    void signal_unusedArgs();
 
93
    void signal_heavyArgsAccess();
 
94
    void signal_heavyIdAccess();
 
95
 
 
96
    void slot_simple();
 
97
    void slot_simple_js();
 
98
    void slot_complex();
 
99
    void slot_complex_js();
 
100
 
 
101
    void block_data();
 
102
    void block();
 
103
 
 
104
    void global_property_js();
 
105
    void global_property_qml();
 
106
    void global_property_qml_js();
 
107
 
 
108
    void scriptfile_property();
 
109
 
 
110
    void enums();
 
111
    void namespacedEnums();
 
112
    void scriptCall();
 
113
};
 
114
 
 
115
inline QUrl TEST_FILE(const QString &filename)
 
116
{
 
117
    return QUrl::fromLocalFile(QLatin1String(SRCDIR) + QLatin1String("/data/") + filename);
 
118
}
 
119
 
 
120
class TestObject : public QObject
 
121
{
 
122
    Q_OBJECT
 
123
    Q_PROPERTY(int x READ x WRITE setX)
 
124
 
 
125
public:
 
126
    TestObject(QObject *parent = 0);
 
127
 
 
128
    int x();
 
129
    void setX(int x) { m_x = x; }
 
130
 
 
131
    void emitMySignal() { emit mySignal(); }
 
132
    void emitMySignalWithArgs(int n) { emit mySignalWithArgs(n); }
 
133
 
 
134
signals:
 
135
    void mySignal();
 
136
    void mySignalWithArgs(int n);
 
137
 
 
138
public slots:
 
139
    int method() {
 
140
        return x();
 
141
    }
 
142
 
 
143
    int methodArgs(int val) {
 
144
        return val + x();
 
145
    }
 
146
 
 
147
private:
 
148
    int m_x;
 
149
};
 
150
QML_DECLARE_TYPE(TestObject);
 
151
 
 
152
TestObject::TestObject(QObject *parent)
 
153
: QObject(parent), m_x(0)
 
154
{
 
155
}
 
156
 
 
157
int TestObject::x()
 
158
{
 
159
    return m_x++;
 
160
}
 
161
 
 
162
void tst_script::initTestCase()
 
163
{
 
164
    qmlRegisterType<TestObject>("Qt.test", 1, 0, "TestObject");
 
165
}
 
166
 
 
167
 
 
168
#define PROPERTY_PROGRAM \
 
169
    "(function(testObject) { return (function() { " \
 
170
    "    var test = 0; " \
 
171
    "    for (var ii = 0; ii < 10000; ++ii) { " \
 
172
    "        test += testObject.x; " \
 
173
    "    } " \
 
174
    "    return test; " \
 
175
    "}); })"
 
176
 
 
177
void tst_script::property_js()
 
178
{
 
179
    QJSEngine engine;
 
180
 
 
181
    QJSValue v = engine.newObject();
 
182
    v.setProperty(QLatin1String("x"), 10);
 
183
 
 
184
    QJSValueList args;
 
185
    args << v;
 
186
    QJSValue prog = engine.evaluate(PROPERTY_PROGRAM).call(args);
 
187
    prog.call();
 
188
 
 
189
    QBENCHMARK {
 
190
        prog.call().toNumber();
 
191
    }
 
192
}
 
193
 
 
194
#if 0
 
195
static QJSValue property_getter_method(QScriptContext *, QJSEngine *engine)
 
196
{
 
197
    static int x = 0;
 
198
    return QJSValue(engine,x++);
 
199
}
 
200
 
 
201
void tst_script::property_getter()
 
202
{
 
203
    QJSEngine engine;
 
204
 
 
205
    QJSValue v = engine.newObject();
 
206
    v.setProperty(QLatin1String("x"), engine.newFunction(property_getter_method),
 
207
                  QJSValue::PropertyGetter);
 
208
 
 
209
    QJSValueList args;
 
210
    args << v;
 
211
    QJSValue prog = engine.evaluate(PROPERTY_PROGRAM).call(args);
 
212
    prog.call();
 
213
 
 
214
    QBENCHMARK {
 
215
        prog.call();
 
216
    }
 
217
}
 
218
 
 
219
static TestObject *property_getter_qobject_object = 0;
 
220
static QJSValue property_getter_qobject_method(QScriptContext *, QJSEngine *)
 
221
{
 
222
    static int idx = -1;
 
223
    if (idx == -1)
 
224
        idx = TestObject::staticMetaObject.indexOfProperty("x");
 
225
 
 
226
    int value = 0;
 
227
    void *args[] = { &value, 0 };
 
228
    QMetaObject::metacall(property_getter_qobject_object, QMetaObject::ReadProperty, idx, args);
 
229
 
 
230
    return QJSValue(value);
 
231
}
 
232
 
 
233
static QJSValue property_getter_qmetaproperty_method(QScriptContext *, QJSEngine *)
 
234
{
 
235
    static int idx = -1;
 
236
    if (idx == -1)
 
237
        idx = TestObject::staticMetaObject.indexOfProperty("x");
 
238
 
 
239
    int value = 0;
 
240
    value = property_getter_qobject_object->metaObject()->property(idx).read(property_getter_qobject_object).toInt();
 
241
 
 
242
    return QJSValue(value);
 
243
}
 
244
 
 
245
void tst_script::property_getter_qobject()
 
246
{
 
247
    QJSEngine engine;
 
248
 
 
249
    TestObject to;
 
250
    property_getter_qobject_object = &to;
 
251
    QJSValue v = engine.newObject();
 
252
    v.setProperty(QLatin1String("x"), engine.newFunction(property_getter_qobject_method),
 
253
                  QJSValue::PropertyGetter);
 
254
 
 
255
    QJSValueList args;
 
256
    args << v;
 
257
    QJSValue prog = engine.evaluate(PROPERTY_PROGRAM).call(args);
 
258
    prog.call();
 
259
 
 
260
    QBENCHMARK {
 
261
        prog.call();
 
262
    }
 
263
    property_getter_qobject_object = 0;
 
264
}
 
265
 
 
266
void tst_script::property_getter_qmetaproperty()
 
267
{
 
268
    QJSEngine engine;
 
269
 
 
270
    TestObject to;
 
271
    property_getter_qobject_object = &to;
 
272
    QJSValue v = engine.newObject();
 
273
    v.setProperty(QLatin1String("x"), engine.newFunction(property_getter_qmetaproperty_method),
 
274
                  QJSValue::PropertyGetter);
 
275
 
 
276
    QJSValueList args;
 
277
    args << v;
 
278
    QJSValue prog = engine.evaluate(PROPERTY_PROGRAM).call(args);
 
279
    prog.call();
 
280
 
 
281
    QBENCHMARK {
 
282
        prog.call();
 
283
    }
 
284
    property_getter_qobject_object = 0;
 
285
}
 
286
#endif
 
287
 
 
288
void tst_script::property_getter_js()
 
289
{
 
290
    QJSEngine engine;
 
291
 
 
292
    QJSValue v = engine.evaluate("(function() { var o = new Object; o._x = 0; o.__defineGetter__(\"x\", function() { return this._x++; }); return o; })").call();
 
293
 
 
294
    QJSValueList args;
 
295
    args << v;
 
296
    QJSValue prog = engine.evaluate(PROPERTY_PROGRAM).call(args);
 
297
    prog.call();
 
298
 
 
299
    QBENCHMARK {
 
300
        prog.call();
 
301
    }
 
302
}
 
303
 
 
304
void tst_script::property_qobject()
 
305
{
 
306
    QJSEngine engine;
 
307
 
 
308
    TestObject to;
 
309
    QJSValue v = engine.newQObject(&to);
 
310
 
 
311
    QJSValueList args;
 
312
    args << v;
 
313
    QJSValue prog = engine.evaluate(PROPERTY_PROGRAM).call(args);
 
314
    prog.call();
 
315
 
 
316
    QBENCHMARK {
 
317
        prog.call();
 
318
    }
 
319
}
 
320
 
 
321
void tst_script::property_qmlobject()
 
322
{
 
323
    QQmlEngine qmlengine;
 
324
 
 
325
    TestObject to;
 
326
    QV8Engine *engine = QQmlEnginePrivate::getV8Engine(&qmlengine);
 
327
    v8::HandleScope handle_scope;
 
328
    v8::Context::Scope scope(engine->context());
 
329
    QJSValue v = engine->scriptValueFromInternal(engine->qobjectWrapper()->newQObject(&to));
 
330
 
 
331
    QJSValueList args;
 
332
    args << v;
 
333
    QJSValue prog = qmlengine.evaluate(PROPERTY_PROGRAM).call(args);
 
334
    prog.call();
 
335
 
 
336
    QBENCHMARK {
 
337
        prog.call();
 
338
    }
 
339
}
 
340
 
 
341
#define SETPROPERTY_PROGRAM \
 
342
    "(function(testObject) { return (function() { " \
 
343
    "    for (var ii = 0; ii < 10000; ++ii) { " \
 
344
    "        testObject.x = ii; " \
 
345
    "    } " \
 
346
    "}); })"
 
347
 
 
348
void tst_script::setproperty_js()
 
349
{
 
350
    QJSEngine engine;
 
351
 
 
352
    QJSValue v = engine.newObject();
 
353
    v.setProperty(QLatin1String("x"), 0);
 
354
 
 
355
    QJSValueList args;
 
356
    args << v;
 
357
    QJSValue prog = engine.evaluate(SETPROPERTY_PROGRAM).call(args);
 
358
    prog.call();
 
359
 
 
360
    QBENCHMARK {
 
361
        prog.call();
 
362
    }
 
363
}
 
364
 
 
365
void tst_script::setproperty_qmlobject()
 
366
{
 
367
    QQmlEngine qmlengine;
 
368
 
 
369
    TestObject to;
 
370
 
 
371
    QV8Engine *engine = QQmlEnginePrivate::getV8Engine(&qmlengine);
 
372
    v8::HandleScope handle_scope;
 
373
    v8::Context::Scope scope(engine->context());
 
374
    QJSValue v = engine->scriptValueFromInternal(engine->qobjectWrapper()->newQObject(&to));
 
375
 
 
376
    QJSValueList args;
 
377
    args << v;
 
378
    QJSValue prog = qmlengine.evaluate(SETPROPERTY_PROGRAM).call(args);
 
379
    prog.call();
 
380
 
 
381
    QBENCHMARK {
 
382
        prog.call();
 
383
    }
 
384
}
 
385
 
 
386
#define FUNCTION_PROGRAM \
 
387
    "(function(testObject) { return (function() { " \
 
388
    "    var test = 0; " \
 
389
    "    for (var ii = 0; ii < 10000; ++ii) { " \
 
390
    "        test += testObject.method(); " \
 
391
    "    } " \
 
392
    "    return test; " \
 
393
    "}); })"
 
394
 
 
395
void tst_script::function_js()
 
396
{
 
397
    QJSEngine engine;
 
398
 
 
399
    QJSValue v = engine.evaluate("(function() { var o = new Object; o._x = 0; o.method = (function() { return this._x++; }); return o; })").call();
 
400
 
 
401
    QJSValueList args;
 
402
    args << v;
 
403
    QJSValue prog = engine.evaluate(FUNCTION_PROGRAM).call(args);
 
404
    prog.call();
 
405
 
 
406
    QBENCHMARK {
 
407
        prog.call();
 
408
    }
 
409
}
 
410
 
 
411
#if 0
 
412
static QJSValue function_method(QScriptContext *, QJSEngine *)
 
413
{
 
414
    static int x = 0;
 
415
    return QJSValue(x++);
 
416
}
 
417
 
 
418
void tst_script::function_cpp()
 
419
{
 
420
    QJSEngine engine;
 
421
 
 
422
    QJSValue v = engine.newObject();
 
423
    v.setProperty(QLatin1String("method"), engine.newFunction(function_method));
 
424
 
 
425
    QJSValueList args;
 
426
    args << v;
 
427
    QJSValue prog = engine.evaluate(FUNCTION_PROGRAM).call(args);
 
428
    prog.call();
 
429
 
 
430
    QBENCHMARK {
 
431
        prog.call();
 
432
    }
 
433
}
 
434
#endif
 
435
 
 
436
void tst_script::function_qobject()
 
437
{
 
438
    QJSEngine engine;
 
439
 
 
440
    TestObject to;
 
441
    QJSValue v = engine.newQObject(&to);
 
442
 
 
443
    QJSValueList args;
 
444
    args << v;
 
445
    QJSValue prog = engine.evaluate(FUNCTION_PROGRAM).call(args);
 
446
    prog.call();
 
447
 
 
448
    QBENCHMARK {
 
449
        prog.call();
 
450
    }
 
451
}
 
452
 
 
453
void tst_script::function_qmlobject()
 
454
{
 
455
    QQmlEngine qmlengine;
 
456
 
 
457
    TestObject to;
 
458
 
 
459
    QV8Engine *engine = QQmlEnginePrivate::getV8Engine(&qmlengine);
 
460
    v8::HandleScope handle_scope;
 
461
    v8::Context::Scope scope(engine->context());
 
462
    QJSValue v = engine->scriptValueFromInternal(engine->qobjectWrapper()->newQObject(&to));
 
463
 
 
464
    QJSValueList args;
 
465
    args << v;
 
466
    QJSValue prog = qmlengine.evaluate(FUNCTION_PROGRAM).call(args);
 
467
    prog.call();
 
468
 
 
469
    QBENCHMARK {
 
470
        prog.call();
 
471
    }
 
472
}
 
473
 
 
474
#define FUNCTION_ARGS_PROGRAM \
 
475
    "(function(testObject) { return (function() { " \
 
476
    "    var test = 0; " \
 
477
    "    for (var ii = 0; ii < 10000; ++ii) { " \
 
478
    "        test += testObject.methodArgs(ii); " \
 
479
    "    } " \
 
480
    "    return test; " \
 
481
    "}); })"
 
482
 
 
483
void tst_script::function_args_js()
 
484
{
 
485
    QJSEngine engine;
 
486
 
 
487
    QJSValue v = engine.evaluate("(function() { var o = new Object; o._x = 0; o.methodArgs = (function(a) { return a + this._x++; }); return o; })").call();
 
488
 
 
489
    QJSValueList args;
 
490
    args << v;
 
491
    QJSValue prog = engine.evaluate(FUNCTION_ARGS_PROGRAM).call(args);
 
492
    prog.call();
 
493
 
 
494
    QBENCHMARK {
 
495
        prog.call();
 
496
    }
 
497
}
 
498
 
 
499
#if 0
 
500
static QJSValue function_args_method(QScriptContext *ctxt, QJSEngine *)
 
501
{
 
502
    static int x = 0;
 
503
    return QJSValue(ctxt->argument(0).toNumber() + x++);
 
504
}
 
505
 
 
506
void tst_script::function_args_cpp()
 
507
{
 
508
    QJSEngine engine;
 
509
 
 
510
    QJSValue v = engine.newObject();
 
511
    v.setProperty(QLatin1String("methodArgs"), engine.newFunction(function_args_method));
 
512
 
 
513
    QJSValueList args;
 
514
    args << v;
 
515
    QJSValue prog = engine.evaluate(FUNCTION_ARGS_PROGRAM).call(args);
 
516
    prog.call();
 
517
 
 
518
    QBENCHMARK {
 
519
        prog.call();
 
520
    }
 
521
}
 
522
#endif
 
523
 
 
524
void tst_script::function_args_qobject()
 
525
{
 
526
    QJSEngine engine;
 
527
 
 
528
    TestObject to;
 
529
    QJSValue v = engine.newQObject(&to);
 
530
 
 
531
    QJSValueList args;
 
532
    args << v;
 
533
    QJSValue prog = engine.evaluate(FUNCTION_ARGS_PROGRAM).call(args);
 
534
    prog.call();
 
535
 
 
536
    QBENCHMARK {
 
537
        prog.call();
 
538
    }
 
539
}
 
540
 
 
541
void tst_script::function_args_qmlobject()
 
542
{
 
543
    QQmlEngine qmlengine;
 
544
 
 
545
    TestObject to;
 
546
 
 
547
    QV8Engine *engine = QQmlEnginePrivate::getV8Engine(&qmlengine);
 
548
    v8::HandleScope handle_scope;
 
549
    v8::Context::Scope scope(engine->context());
 
550
    QJSValue v = engine->scriptValueFromInternal(engine->qobjectWrapper()->newQObject(&to));
 
551
 
 
552
    QJSValueList args;
 
553
    args << v;
 
554
    QJSValue prog = qmlengine.evaluate(FUNCTION_ARGS_PROGRAM).call(args);
 
555
    prog.call();
 
556
 
 
557
    QBENCHMARK {
 
558
        prog.call();
 
559
    }
 
560
}
 
561
 
 
562
void tst_script::signal_unconnected()
 
563
{
 
564
    QQmlEngine engine;
 
565
    QQmlComponent component(&engine, TEST_FILE("signal_unconnected.qml"));
 
566
    TestObject *object = qobject_cast<TestObject *>(component.create());
 
567
    QVERIFY(object != 0);
 
568
 
 
569
    QBENCHMARK {
 
570
        object->emitMySignal();
 
571
    }
 
572
 
 
573
    delete object;
 
574
}
 
575
 
 
576
void tst_script::signal_qml()
 
577
{
 
578
    QQmlEngine engine;
 
579
    QQmlComponent component(&engine, TEST_FILE("signal_qml.qml"));
 
580
    TestObject *object = qobject_cast<TestObject *>(component.create());
 
581
    QVERIFY(object != 0);
 
582
 
 
583
    QBENCHMARK {
 
584
        object->emitMySignal();
 
585
    }
 
586
 
 
587
    delete object;
 
588
}
 
589
 
 
590
void tst_script::signal_args()
 
591
{
 
592
    QQmlEngine engine;
 
593
    QQmlComponent component(&engine, TEST_FILE("signal_args.qml"));
 
594
    TestObject *object = qobject_cast<TestObject *>(component.create());
 
595
    QVERIFY(object != 0);
 
596
 
 
597
    QBENCHMARK {
 
598
        object->emitMySignalWithArgs(11);
 
599
    }
 
600
 
 
601
    delete object;
 
602
}
 
603
 
 
604
void tst_script::signal_unusedArgs()
 
605
{
 
606
    QQmlEngine engine;
 
607
    QQmlComponent component(&engine, TEST_FILE("signal_unusedArgs.qml"));
 
608
    TestObject *object = qobject_cast<TestObject *>(component.create());
 
609
    QVERIFY(object != 0);
 
610
 
 
611
    QBENCHMARK {
 
612
        object->emitMySignalWithArgs(11);
 
613
    }
 
614
 
 
615
    delete object;
 
616
}
 
617
 
 
618
void tst_script::signal_heavyArgsAccess()
 
619
{
 
620
    QQmlEngine engine;
 
621
    QQmlComponent component(&engine, TEST_FILE("signal_heavyArgsAccess.qml"));
 
622
    TestObject *object = qobject_cast<TestObject *>(component.create());
 
623
    QVERIFY(object != 0);
 
624
 
 
625
    QBENCHMARK {
 
626
        object->emitMySignalWithArgs(11);
 
627
    }
 
628
 
 
629
    delete object;
 
630
}
 
631
 
 
632
void tst_script::signal_heavyIdAccess()
 
633
{
 
634
    QQmlEngine engine;
 
635
    QQmlComponent component(&engine, TEST_FILE("signal_heavyIdAccess.qml"));
 
636
    TestObject *object = qobject_cast<TestObject *>(component.create());
 
637
    QVERIFY(object != 0);
 
638
 
 
639
    QBENCHMARK {
 
640
        object->emitMySignalWithArgs(11);
 
641
    }
 
642
 
 
643
    delete object;
 
644
}
 
645
 
 
646
void tst_script::slot_simple()
 
647
{
 
648
    QQmlEngine engine;
 
649
    QQmlComponent component(&engine, TEST_FILE("slot_simple.qml"));
 
650
    TestObject *object = qobject_cast<TestObject *>(component.create());
 
651
    QVERIFY(object != 0);
 
652
 
 
653
    QBENCHMARK {
 
654
        object->emitMySignal();
 
655
    }
 
656
 
 
657
    delete object;
 
658
}
 
659
 
 
660
void tst_script::slot_simple_js()
 
661
{
 
662
    QQmlEngine engine;
 
663
    QQmlComponent component(&engine, TEST_FILE("slot_simple_js.qml"));
 
664
    TestObject *object = qobject_cast<TestObject *>(component.create());
 
665
    QVERIFY(object != 0);
 
666
 
 
667
    QBENCHMARK {
 
668
        object->emitMySignal();
 
669
    }
 
670
 
 
671
    delete object;
 
672
}
 
673
 
 
674
void tst_script::slot_complex()
 
675
{
 
676
    QQmlEngine engine;
 
677
    QQmlComponent component(&engine, TEST_FILE("slot_complex.qml"));
 
678
    TestObject *object = qobject_cast<TestObject *>(component.create());
 
679
    QVERIFY(object != 0);
 
680
 
 
681
    QBENCHMARK {
 
682
        object->emitMySignal();
 
683
    }
 
684
 
 
685
    delete object;
 
686
}
 
687
 
 
688
void tst_script::slot_complex_js()
 
689
{
 
690
    QQmlEngine engine;
 
691
    QQmlComponent component(&engine, TEST_FILE("slot_complex_js.qml"));
 
692
    TestObject *object = qobject_cast<TestObject *>(component.create());
 
693
    QVERIFY(object != 0);
 
694
 
 
695
    QBENCHMARK {
 
696
        object->emitMySignal();
 
697
    }
 
698
 
 
699
    delete object;
 
700
}
 
701
 
 
702
void tst_script::block_data()
 
703
{
 
704
    QTest::addColumn<QString>("methodName");
 
705
    QTest::newRow("direct") << "doSomethingDirect()";
 
706
    QTest::newRow("localObj") << "doSomethingLocalObj()";
 
707
    QTest::newRow("local") << "doSomethingLocal()";
 
708
}
 
709
 
 
710
void tst_script::block()
 
711
{
 
712
    QFETCH(QString, methodName);
 
713
    QQmlEngine engine;
 
714
    QQmlComponent component(&engine, TEST_FILE("block.qml"));
 
715
    QQuickRectangle *rect = qobject_cast<QQuickRectangle *>(component.create());
 
716
    QVERIFY(rect != 0);
 
717
 
 
718
    int index = rect->metaObject()->indexOfMethod(methodName.toUtf8());
 
719
    QVERIFY(index != -1);
 
720
    QMetaMethod method = rect->metaObject()->method(index);
 
721
 
 
722
    QBENCHMARK {
 
723
        method.invoke(rect, Qt::DirectConnection);
 
724
    }
 
725
 
 
726
    delete rect;
 
727
}
 
728
 
 
729
#define GLOBALPROPERTY_PROGRAM \
 
730
    "(function() { " \
 
731
    "    for (var ii = 0; ii < 10000; ++ii) { " \
 
732
    "        Math.sin(90); " \
 
733
    "    } " \
 
734
    "})"
 
735
 
 
736
void tst_script::global_property_js()
 
737
{
 
738
    QJSEngine engine;
 
739
 
 
740
    QJSValue prog = engine.evaluate(GLOBALPROPERTY_PROGRAM);
 
741
    prog.call();
 
742
 
 
743
    QBENCHMARK {
 
744
        prog.call();
 
745
    }
 
746
}
 
747
 
 
748
void tst_script::global_property_qml()
 
749
{
 
750
    QQmlEngine qmlengine;
 
751
 
 
752
    QJSValue prog = qmlengine.evaluate(GLOBALPROPERTY_PROGRAM);
 
753
    prog.call();
 
754
 
 
755
    QBENCHMARK {
 
756
        prog.call();
 
757
    }
 
758
}
 
759
 
 
760
void tst_script::global_property_qml_js()
 
761
{
 
762
    QQmlEngine engine;
 
763
    QQmlComponent component(&engine, TEST_FILE("global_prop.qml"));
 
764
    QQuickRectangle *rect = qobject_cast<QQuickRectangle *>(component.create());
 
765
    QVERIFY(rect != 0);
 
766
 
 
767
    int index = rect->metaObject()->indexOfMethod("triggered()");
 
768
    QVERIFY(index != -1);
 
769
    QMetaMethod method = rect->metaObject()->method(index);
 
770
 
 
771
    QBENCHMARK {
 
772
        method.invoke(rect, Qt::DirectConnection);
 
773
    }
 
774
 
 
775
    delete rect;
 
776
}
 
777
 
 
778
void tst_script::scriptfile_property()
 
779
{
 
780
    QQmlEngine engine;
 
781
    QQmlComponent component(&engine, TEST_FILE("global_prop.qml"));
 
782
    QQuickRectangle *rect = qobject_cast<QQuickRectangle *>(component.create());
 
783
    QVERIFY(rect != 0);
 
784
 
 
785
    int index = rect->metaObject()->indexOfMethod("incrementTriggered()");
 
786
    QVERIFY(index != -1);
 
787
    QMetaMethod method = rect->metaObject()->method(index);
 
788
 
 
789
    QBENCHMARK {
 
790
        method.invoke(rect, Qt::DirectConnection);
 
791
    }
 
792
 
 
793
    delete rect;
 
794
}
 
795
 
 
796
void tst_script::enums()
 
797
{
 
798
    QQmlEngine engine;
 
799
    QQmlComponent component(&engine, TEST_FILE("enums.qml"));
 
800
    QObject *o = component.create();
 
801
    QVERIFY(o != 0);
 
802
 
 
803
    int index = o->metaObject()->indexOfMethod("runtest()");
 
804
    QVERIFY(index != -1);
 
805
    QMetaMethod method = o->metaObject()->method(index);
 
806
 
 
807
    QBENCHMARK {
 
808
        method.invoke(o, Qt::DirectConnection);
 
809
    }
 
810
 
 
811
    delete o;
 
812
}
 
813
 
 
814
void tst_script::namespacedEnums()
 
815
{
 
816
    QQmlEngine engine;
 
817
    QQmlComponent component(&engine, TEST_FILE("namespacedEnums.qml"));
 
818
    QObject *o = component.create();
 
819
    QVERIFY(o != 0);
 
820
 
 
821
    int index = o->metaObject()->indexOfMethod("runtest()");
 
822
    QVERIFY(index != -1);
 
823
    QMetaMethod method = o->metaObject()->method(index);
 
824
 
 
825
    QBENCHMARK {
 
826
        method.invoke(o, Qt::DirectConnection);
 
827
    }
 
828
 
 
829
    delete o;
 
830
}
 
831
 
 
832
void tst_script::scriptCall()
 
833
{
 
834
    QQmlEngine engine;
 
835
    QQmlComponent component(&engine, TEST_FILE("scriptCall.qml"));
 
836
    QObject *o = component.create();
 
837
    QVERIFY(o != 0);
 
838
 
 
839
    int index = o->metaObject()->indexOfMethod("runtest()");
 
840
    QVERIFY(index != -1);
 
841
    QMetaMethod method = o->metaObject()->method(index);
 
842
 
 
843
    QBENCHMARK {
 
844
        method.invoke(o, Qt::DirectConnection);
 
845
    }
 
846
 
 
847
    delete o;
 
848
}
 
849
 
 
850
QTEST_MAIN(tst_script)
 
851
 
 
852
#include "tst_script.moc"